-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
-- Item models aren't working yet
- Loading branch information
Showing
20 changed files
with
225 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 63 additions & 8 deletions
71
src/main/java/choonster/testmod3/client/init/ModItemModelProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,84 @@ | ||
package choonster.testmod3.client.init; | ||
|
||
import choonster.testmod3.TestMod3; | ||
import choonster.testmod3.client.item.RevealHiddenBlocksItemPropertyFunction; | ||
import choonster.testmod3.client.item.TicksSinceLastUseItemPropertyFunction; | ||
import choonster.testmod3.init.ModItems; | ||
import net.minecraft.client.renderer.item.ClampedItemPropertyFunction; | ||
import choonster.testmod3.client.renderer.item.properties.numeric.TicksSinceLastUse; | ||
import com.mojang.serialization.MapCodec; | ||
import net.minecraft.client.renderer.item.properties.conditional.ConditionalItemModelProperties; | ||
import net.minecraft.client.renderer.item.properties.conditional.ConditionalItemModelProperty; | ||
import net.minecraft.client.renderer.item.properties.numeric.RangeSelectItemModelProperties; | ||
import net.minecraft.client.renderer.item.properties.numeric.RangeSelectItemModelProperty; | ||
import net.minecraft.client.renderer.item.properties.select.SelectItemModelProperties; | ||
import net.minecraft.client.renderer.item.properties.select.SelectItemModelProperty; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.util.ExtraCodecs; | ||
import net.minecraftforge.api.distmarker.Dist; | ||
import net.minecraftforge.eventbus.api.SubscribeEvent; | ||
import net.minecraftforge.fml.common.Mod; | ||
import net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus; | ||
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; | ||
import net.minecraftforge.fml.util.ObfuscationReflectionHelper; | ||
|
||
/** | ||
* Registers this mod's {@link ClampedItemPropertyFunction}s. | ||
* Registers this mod's {@link ConditionalItemModelProperty}, {@link RangeSelectItemModelProperty} and | ||
* {@link SelectItemModelProperty} implementations. | ||
* | ||
* @author Choonster | ||
*/ | ||
@Mod.EventBusSubscriber(modid = TestMod3.MODID, bus = Bus.MOD, value = Dist.CLIENT) | ||
public class ModItemModelProperties { | ||
private static final ExtraCodecs.LateBoundIdMapper<ResourceLocation, MapCodec<? extends SelectItemModelProperty.Type<?, ?>>> SELECT_ID_MAPPER; | ||
private static final ExtraCodecs.LateBoundIdMapper<ResourceLocation, MapCodec<? extends ConditionalItemModelProperty>> CONDITIONAL_ID_MAPPER; | ||
private static final ExtraCodecs.LateBoundIdMapper<ResourceLocation, MapCodec<? extends RangeSelectItemModelProperty>> RANGE_SELECT_ID_MAPPER; | ||
|
||
// TODO: Refactor if/when Forge adds a proper event for this | ||
@SubscribeEvent | ||
public static void registerItemModelProperties(final FMLClientSetupEvent event) { | ||
event.enqueueWork(() -> { | ||
RevealHiddenBlocksItemPropertyFunction.registerForItem(ModItems.HIDDEN_BLOCK_REVEALER.get()); | ||
TicksSinceLastUseItemPropertyFunction.registerForItem(ModItems.MODEL_TEST.get()); | ||
TicksSinceLastUseItemPropertyFunction.registerForItem(ModItems.SLINGSHOT.get()); | ||
// RevealHiddenBlocksItemPropertyFunction.registerForItem(ModItems.HIDDEN_BLOCK_REVEALER.get()); | ||
registerRangeSelect("ticks_since_last_use", TicksSinceLastUse.MAP_CODEC); | ||
|
||
// TicksSinceLastUse.registerForItem(ModItems.MODEL_TEST.get()); | ||
// TicksSinceLastUse.registerForItem(ModItems.SLINGSHOT.get()); | ||
}); | ||
} | ||
|
||
private static <T extends RangeSelectItemModelProperty> void registerRangeSelect( | ||
final String name, | ||
final MapCodec<T> mapCodec | ||
) { | ||
RANGE_SELECT_ID_MAPPER.put(ResourceLocation.fromNamespaceAndPath(TestMod3.MODID, name), mapCodec); | ||
} | ||
|
||
static { | ||
try { | ||
@SuppressWarnings("unchecked") final var selectIdMapper = | ||
(ExtraCodecs.LateBoundIdMapper<ResourceLocation, MapCodec<? extends SelectItemModelProperty.Type<?, ?>>>) | ||
ObfuscationReflectionHelper.findField( | ||
SelectItemModelProperties.class, | ||
"ID_MAPPER" | ||
).get(null); | ||
|
||
SELECT_ID_MAPPER = selectIdMapper; | ||
|
||
@SuppressWarnings("unchecked") final var conditionalIdMapper = | ||
(ExtraCodecs.LateBoundIdMapper<ResourceLocation, MapCodec<? extends ConditionalItemModelProperty>>) | ||
ObfuscationReflectionHelper.findField( | ||
ConditionalItemModelProperties.class, | ||
"ID_MAPPER" | ||
).get(null); | ||
|
||
CONDITIONAL_ID_MAPPER = conditionalIdMapper; | ||
|
||
@SuppressWarnings("unchecked") final var rangeSelectIdMapper = | ||
(ExtraCodecs.LateBoundIdMapper<ResourceLocation, MapCodec<? extends RangeSelectItemModelProperty>>) | ||
ObfuscationReflectionHelper.findField( | ||
RangeSelectItemModelProperties.class, | ||
"ID_MAPPER" | ||
).get(null); | ||
|
||
RANGE_SELECT_ID_MAPPER = rangeSelectIdMapper; | ||
} catch (final IllegalAccessException e) { | ||
throw new RuntimeException("Failed to initialise Item Property ID Mappers", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 0 additions & 50 deletions
50
src/main/java/choonster/testmod3/client/item/TicksSinceLastUseItemPropertyFunction.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...in/java/choonster/testmod3/client/renderer/item/properties/numeric/TicksSinceLastUse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package choonster.testmod3.client.renderer.item.properties.numeric; | ||
|
||
import choonster.testmod3.TestMod3; | ||
import choonster.testmod3.init.ModDataComponents; | ||
import choonster.testmod3.world.item.component.lastusetime.LastUseTimeProperties; | ||
import com.mojang.serialization.MapCodec; | ||
import net.minecraft.client.multiplayer.ClientLevel; | ||
import net.minecraft.client.renderer.item.properties.numeric.RangeSelectItemModelProperty; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.world.entity.LivingEntity; | ||
import net.minecraft.world.item.ItemStack; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.Optional; | ||
|
||
/** | ||
* {@link RangeSelectItemModelProperty} to get the ticks since the last use of the item, | ||
* as recorded by the item's {@link LastUseTimeProperties} component. | ||
* <p> | ||
* Returns 0.0 if the item was last used 0 ticks ago and 1.0 if the item was last used 20 or more ticks ago. | ||
* Returns 1.0 if the required information isn't available. | ||
*/ | ||
public record TicksSinceLastUse() implements RangeSelectItemModelProperty { | ||
public static final MapCodec<TicksSinceLastUse> MAP_CODEC = MapCodec.unit(new TicksSinceLastUse()); | ||
|
||
/** | ||
* The ID of this function. | ||
*/ | ||
@Deprecated(forRemoval = true) | ||
public static final ResourceLocation ID = ResourceLocation.fromNamespaceAndPath(TestMod3.MODID, "ticks_since_last_use"); | ||
|
||
@Override | ||
public float get( | ||
final ItemStack stack, | ||
@Nullable final ClientLevel clientLevel, | ||
@Nullable final LivingEntity entity, | ||
final int seed | ||
) { | ||
final var level = clientLevel != null ? clientLevel : entity != null ? entity.level() : null; | ||
|
||
if (level == null) { | ||
return 1.0f; | ||
} | ||
|
||
return Optional.ofNullable(stack.get(ModDataComponents.LAST_USE_TIME_PROPERTIES.get())) | ||
.map(properties -> (level.getGameTime() - properties.lastUseTime()) / 20.0f) | ||
.orElse(1.0f); | ||
} | ||
|
||
@Override | ||
public MapCodec<? extends RangeSelectItemModelProperty> type() { | ||
return MAP_CODEC; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...er/testmod3/client/item/package-info.java → ...item/properties/numeric/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/main/java/choonster/testmod3/data/TestMod3EquipmentAssetProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package choonster.testmod3.data; | ||
|
||
import choonster.testmod3.init.ModEquipmentAssets; | ||
import net.minecraft.client.resources.model.EquipmentClientInfo; | ||
import net.minecraft.data.CachedOutput; | ||
import net.minecraft.data.DataProvider; | ||
import net.minecraft.data.PackOutput; | ||
import net.minecraft.resources.ResourceKey; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.world.item.equipment.EquipmentAsset; | ||
|
||
import java.util.HashMap; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.function.BiConsumer; | ||
|
||
/** | ||
* Generates this mod's equipment assets. | ||
* | ||
* @author Choonster | ||
*/ | ||
public class TestMod3EquipmentAssetProvider implements DataProvider { | ||
private final PackOutput.PathProvider pathProvider; | ||
|
||
public TestMod3EquipmentAssetProvider(final PackOutput output) { | ||
pathProvider = output.createPathProvider(PackOutput.Target.RESOURCE_PACK, "equipment"); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<?> run(final CachedOutput output) { | ||
final var map = new HashMap<ResourceKey<EquipmentAsset>, EquipmentClientInfo>(); | ||
|
||
bootstrap((id, equipmentClientInfo) -> { | ||
if (map.putIfAbsent(id, equipmentClientInfo) != null) { | ||
throw new IllegalStateException("Tried to register equipment asset twice for id: " + id); | ||
} | ||
}); | ||
|
||
return DataProvider.saveAll(output, EquipmentClientInfo.CODEC, pathProvider::json, map); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "TestMod3 Equipment Asset Definitions"; | ||
} | ||
|
||
private static void bootstrap(final BiConsumer<ResourceKey<EquipmentAsset>, EquipmentClientInfo> consumer) { | ||
consumer.accept(ModEquipmentAssets.REPLACEMENT, onlyHumanoid("chainmail")); | ||
} | ||
|
||
private static EquipmentClientInfo onlyHumanoid(final String textureName) { | ||
return EquipmentClientInfo.builder() | ||
.addHumanoidLayers(ResourceLocation.withDefaultNamespace(textureName)) | ||
.build(); | ||
} | ||
} |
Oops, something went wrong.