From d6ad8307855b43503ef1d0999b844898e61d1959 Mon Sep 17 00:00:00 2001 From: quat1024 Date: Thu, 26 Oct 2023 14:07:01 -0400 Subject: [PATCH] DyeablesRegistry --- .../vazkii/quark/base/handler/DyeHandler.java | 161 --------------- .../vazkii/quark/base/proxy/ClientProxy.java | 2 - .../vazkii/quark/base/proxy/CommonProxy.java | 1 - .../content/tweaks/entity/DyedItemFrame.java | 4 +- .../module/DyeableItemFramesModule.java | 11 +- src/main/java/vazkii/zeta/Zeta.java | 4 + .../java/vazkii/zeta/client/ZetaClient.java | 3 + .../java/vazkii/zeta/event/ZRegister.java | 12 ++ .../recipe/ZetaDyeRecipe.java} | 38 ++-- .../zeta/registry/DyeablesRegistry.java | 183 ++++++++++++++++++ .../vazkii/zeta/registry/ZetaRegistry.java | 6 +- .../java/vazkii/zetaimplforge/ForgeZeta.java | 2 +- zeta-todo/README.md | 28 ++- 13 files changed, 261 insertions(+), 194 deletions(-) delete mode 100644 src/main/java/vazkii/quark/base/handler/DyeHandler.java rename src/main/java/vazkii/{quark/base/recipe/DyeRecipe.java => zeta/recipe/ZetaDyeRecipe.java} (66%) create mode 100644 src/main/java/vazkii/zeta/registry/DyeablesRegistry.java diff --git a/src/main/java/vazkii/quark/base/handler/DyeHandler.java b/src/main/java/vazkii/quark/base/handler/DyeHandler.java deleted file mode 100644 index 0abe277139..0000000000 --- a/src/main/java/vazkii/quark/base/handler/DyeHandler.java +++ /dev/null @@ -1,161 +0,0 @@ -package vazkii.quark.base.handler; - -import net.minecraft.client.color.item.ItemColor; -import net.minecraft.client.renderer.item.ItemProperties; -import net.minecraft.client.renderer.item.ItemPropertyFunction; -import net.minecraft.core.BlockPos; -import net.minecraft.core.cauldron.CauldronInteraction; -import net.minecraft.resources.ResourceLocation; -import net.minecraft.world.InteractionHand; -import net.minecraft.world.InteractionResult; -import net.minecraft.world.entity.player.Player; -import net.minecraft.world.item.DyeItem; -import net.minecraft.world.item.DyeableLeatherItem; -import net.minecraft.world.item.Item; -import net.minecraft.world.item.ItemStack; -import net.minecraft.world.level.Level; -import net.minecraft.world.level.block.LayeredCauldronBlock; -import net.minecraft.world.level.block.state.BlockState; -import net.minecraftforge.registries.ForgeRegistries; -import vazkii.quark.base.Quark; -import vazkii.zeta.module.ZetaModule; -import vazkii.quark.base.recipe.DyeRecipe; -import vazkii.zeta.event.ZRegister; -import vazkii.zeta.event.bus.LoadEvent; -import vazkii.zeta.client.event.ZAddItemColorHandlers; - -import javax.annotation.Nonnull; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.function.Supplier; - -public final class DyeHandler { - - private static final Map> dyeableConditions = new HashMap<>(); - - private static final DyeableLeatherItem SURROGATE = new DyeableLeatherItem() {}; - - @LoadEvent - public static void register(ZRegister event) { - ForgeRegistries.RECIPE_SERIALIZERS.register(Quark.MOD_ID + ":dye_item", DyeRecipe.SERIALIZER); - } - - @Nonnull - private static InteractionResult cauldronInteract(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, ItemStack stack) { - if(!isDyed(stack)) - return InteractionResult.PASS; - - if(!level.isClientSide) { - SURROGATE.clearColor(stack); -// p_175632_.awardStat(Stats.CLEAN_ARMOR); - LayeredCauldronBlock.lowerFillLevel(state, level, pos); - } - - return InteractionResult.sidedSuccess(level.isClientSide); - } - - public static void addAlwaysDyeable(Item item) { - addDyeable(item, () -> true); - } - - public static void addDyeable(Item item, ZetaModule module) { - addDyeable(item, () -> module.enabled); - } - - public static void addDyeable(Item item, Supplier cond) { - dyeableConditions.put(item, cond); - } - - public static boolean isDyeable(ItemStack stack) { - Item item = stack.getItem(); - return dyeableConditions.containsKey(item) && dyeableConditions.get(item).get(); - } - - public static boolean isDyed(ItemStack stack) { - return isDyeable(stack) && SURROGATE.hasCustomColor(stack); - } - - public static int getDye(ItemStack stack) { - return SURROGATE.getColor(stack); - } - - public static void applyDye(ItemStack stack, int color) { - if(isDyeable(stack)) - SURROGATE.setColor(stack, color); - } - - // Copy of DyeableLeatherItem but for our system - public static ItemStack dyeItem(ItemStack stack, List dyes) { - ItemStack itemstack = ItemStack.EMPTY; - int[] aint = new int[3]; - int i = 0; - int j = 0; - - if(isDyeable(stack)) { - itemstack = stack.copy(); - itemstack.setCount(1); - if (SURROGATE.hasCustomColor(stack)) { - int k = SURROGATE.getColor(itemstack); - float f = (float)(k >> 16 & 255) / 255.0F; - float f1 = (float)(k >> 8 & 255) / 255.0F; - float f2 = (float)(k & 255) / 255.0F; - i += (int)(Math.max(f, Math.max(f1, f2)) * 255.0F); - aint[0] += (int)(f * 255.0F); - aint[1] += (int)(f1 * 255.0F); - aint[2] += (int)(f2 * 255.0F); - ++j; - } - - for(DyeItem dyeitem : dyes) { - float[] afloat = dyeitem.getDyeColor().getTextureDiffuseColors(); - int i2 = (int)(afloat[0] * 255.0F); - int l = (int)(afloat[1] * 255.0F); - int i1 = (int)(afloat[2] * 255.0F); - i += Math.max(i2, Math.max(l, i1)); - aint[0] += i2; - aint[1] += l; - aint[2] += i1; - ++j; - } - - int j1 = aint[0] / j; - int k1 = aint[1] / j; - int l1 = aint[2] / j; - float f3 = (float)i / (float)j; - float f4 = (float)Math.max(j1, Math.max(k1, l1)); - j1 = (int)((float)j1 * f3 / f4); - k1 = (int)((float)k1 * f3 / f4); - l1 = (int)((float)l1 * f3 / f4); - int j2 = (j1 << 8) + k1; - j2 = (j2 << 8) + l1; - SURROGATE.setColor(itemstack, j2); - - return itemstack; - } - - return ItemStack.EMPTY; - } - - public static class Client { - @LoadEvent - public static void colorHandlers(ZAddItemColorHandlers event) { - ItemPropertyFunction fun = (s, e, l, i) -> DyeHandler.isDyed(s) ? 1 : 0; - ItemColor color = (s, l) -> { - if(l != 0 || !isDyed(s)) - return 0xFFFFFF; - - return SURROGATE.getColor(s); - }; - - ResourceLocation res = new ResourceLocation("quark_dyed"); - for(Item item : dyeableConditions.keySet()) { - ItemProperties.register(item, res, fun); - - event.register(color, item); - - CauldronInteraction.WATER.put(item, DyeHandler::cauldronInteract); - } - } - } -} diff --git a/src/main/java/vazkii/quark/base/proxy/ClientProxy.java b/src/main/java/vazkii/quark/base/proxy/ClientProxy.java index 1723ac2b0c..6903aaa072 100644 --- a/src/main/java/vazkii/quark/base/proxy/ClientProxy.java +++ b/src/main/java/vazkii/quark/base/proxy/ClientProxy.java @@ -26,7 +26,6 @@ import vazkii.quark.base.client.handler.RequiredModTooltipHandler; import vazkii.zeta.client.TopLayerTooltipHandler; import vazkii.quark.base.handler.ContributorRewardHandler; -import vazkii.quark.base.handler.DyeHandler; import vazkii.quark.base.handler.MiscUtil; import vazkii.quark.base.handler.RenderLayerHandler; import vazkii.quark.base.handler.WoodSetHandler; @@ -59,7 +58,6 @@ public void start() { Quark.ZETA.loadBus .subscribe(ContributorRewardHandler.Client.class) - .subscribe(DyeHandler.Client.class) .subscribe(RenderLayerHandler.Client.class) .subscribe(WoodSetHandler.Client.class); diff --git a/src/main/java/vazkii/quark/base/proxy/CommonProxy.java b/src/main/java/vazkii/quark/base/proxy/CommonProxy.java index 16240da3ee..d573b68383 100644 --- a/src/main/java/vazkii/quark/base/proxy/CommonProxy.java +++ b/src/main/java/vazkii/quark/base/proxy/CommonProxy.java @@ -47,7 +47,6 @@ public void start() { .subscribe(BrewingHandler.class) .subscribe(ContributorRewardHandler.class) .subscribe(CreativeTabHandler.class) - .subscribe(DyeHandler.class) .subscribe(ModuleLoader.INSTANCE) .subscribe(QuarkNetwork.class) .subscribe(QuarkSounds.class) diff --git a/src/main/java/vazkii/quark/content/tweaks/entity/DyedItemFrame.java b/src/main/java/vazkii/quark/content/tweaks/entity/DyedItemFrame.java index ab01825686..d06b823eb9 100644 --- a/src/main/java/vazkii/quark/content/tweaks/entity/DyedItemFrame.java +++ b/src/main/java/vazkii/quark/content/tweaks/entity/DyedItemFrame.java @@ -13,7 +13,7 @@ import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; import net.minecraft.world.level.Level; -import vazkii.quark.base.handler.DyeHandler; +import vazkii.quark.base.Quark; import vazkii.quark.content.tweaks.module.DyeableItemFramesModule; public class DyedItemFrame extends ItemFrame { @@ -94,7 +94,7 @@ public SoundEvent getRotateItemSound() { @Override protected ItemStack getFrameItemStack() { ItemStack stack = new ItemStack(isGlow() ? Items.GLOW_ITEM_FRAME : Items.ITEM_FRAME); - DyeHandler.applyDye(stack, getColor()); + Quark.ZETA.dyeables.applyDye(stack, getColor()); return stack; } diff --git a/src/main/java/vazkii/quark/content/tweaks/module/DyeableItemFramesModule.java b/src/main/java/vazkii/quark/content/tweaks/module/DyeableItemFramesModule.java index a55f8744c0..273b4ad2c1 100644 --- a/src/main/java/vazkii/quark/content/tweaks/module/DyeableItemFramesModule.java +++ b/src/main/java/vazkii/quark/content/tweaks/module/DyeableItemFramesModule.java @@ -28,7 +28,6 @@ import net.minecraftforge.event.entity.player.PlayerInteractEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; import vazkii.quark.base.Quark; -import vazkii.quark.base.handler.DyeHandler; import vazkii.quark.base.module.LoadModule; import vazkii.zeta.module.ZetaModule; import vazkii.quark.base.module.hint.Hint; @@ -56,10 +55,10 @@ public final void register(ZRegister event) { .setShouldReceiveVelocityUpdates(false) .setCustomClientFactory((spawnEntity, world) -> new DyedItemFrame(entityType, world)) .build("dyed_item_frame"); - Quark.ZETA.registry.register(entityType, "dyed_item_frame", Registry.ENTITY_TYPE_REGISTRY); + event.getRegistry().register(entityType, "dyed_item_frame", Registry.ENTITY_TYPE_REGISTRY); - DyeHandler.addDyeable(Items.ITEM_FRAME, this); - DyeHandler.addDyeable(Items.GLOW_ITEM_FRAME, this); + Quark.ZETA.dyeables.register(Items.ITEM_FRAME, this); + Quark.ZETA.dyeables.register(Items.GLOW_ITEM_FRAME, this); } @LoadEvent @@ -80,7 +79,7 @@ public void onUse(PlayerInteractEvent.RightClickBlock event) { InteractionHand hand = event.getHand(); ItemStack stack = player.getItemInHand(hand); - if((stack.is(Items.ITEM_FRAME) || stack.is(Items.GLOW_ITEM_FRAME)) && DyeHandler.isDyed(stack)) { + if((stack.is(Items.ITEM_FRAME) || stack.is(Items.GLOW_ITEM_FRAME)) && Quark.ZETA.dyeables.isDyed(stack)) { BlockHitResult blockhit = event.getHitVec(); UseOnContext context = new UseOnContext(player, hand, blockhit); @@ -111,7 +110,7 @@ private InteractionResult useOn(UseOnContext context) { return InteractionResult.FAIL; Level level = context.getLevel(); - HangingEntity hangingentity = new DyedItemFrame(level, blockpos1, direction, DyeHandler.getDye(itemstack), itemstack.is(Items.GLOW_ITEM_FRAME)); + HangingEntity hangingentity = new DyedItemFrame(level, blockpos1, direction, Quark.ZETA.dyeables.getDye(itemstack), itemstack.is(Items.GLOW_ITEM_FRAME)); CompoundTag compoundtag = itemstack.getTag(); if(compoundtag != null) diff --git a/src/main/java/vazkii/zeta/Zeta.java b/src/main/java/vazkii/zeta/Zeta.java index 2cc7a89db5..3bba7d3767 100644 --- a/src/main/java/vazkii/zeta/Zeta.java +++ b/src/main/java/vazkii/zeta/Zeta.java @@ -18,6 +18,7 @@ import vazkii.zeta.module.ZetaCategory; import vazkii.zeta.module.ZetaModuleManager; import vazkii.zeta.network.ZetaNetworkHandler; +import vazkii.zeta.registry.DyeablesRegistry; import vazkii.zeta.registry.ZetaRegistry; import vazkii.zeta.util.ZetaSide; @@ -34,6 +35,7 @@ public Zeta(String modid, Logger log) { this.playBus = new ZetaEventBus<>(PlayEvent.class, IZetaPlayEvent.class, null); this.modules = new ZetaModuleManager(this); this.registry = createRegistry(modid); + this.dyeables = new DyeablesRegistry(this); this.ticker_SHOULD_NOT_BE_HERE = new ClientTicker(); } @@ -45,7 +47,9 @@ public Zeta(String modid, Logger log) { public final ZetaEventBus loadBus; public final ZetaEventBus playBus; public final ZetaModuleManager modules; + public final ZetaRegistry registry; + public final DyeablesRegistry dyeables; //TODO: move into ZetaRegistry? public WeirdConfigSingleton weirdConfigSingleton; //Should probably split this up into various parts public IZetaConfigInternals configInternals; diff --git a/src/main/java/vazkii/zeta/client/ZetaClient.java b/src/main/java/vazkii/zeta/client/ZetaClient.java index 58edd9560f..b9c998a12f 100644 --- a/src/main/java/vazkii/zeta/client/ZetaClient.java +++ b/src/main/java/vazkii/zeta/client/ZetaClient.java @@ -6,6 +6,7 @@ import vazkii.zeta.event.bus.IZetaLoadEvent; import vazkii.zeta.event.bus.IZetaPlayEvent; import vazkii.zeta.event.bus.ZetaEventBus; +import vazkii.zeta.registry.DyeablesRegistry; public abstract class ZetaClient { public ZetaClient(Zeta zeta) { @@ -17,6 +18,7 @@ public ZetaClient(Zeta zeta) { this.ticker = zeta.ticker_SHOULD_NOT_BE_HERE; //TODO, move ClientTicker into actual client code this.clientConfigManager = new ClientConfigManager(); this.topLayerTooltipHandler = new TopLayerTooltipHandler(); + this.clientDyeablesRegistry = zeta.dyeables.new Client(this); playBus.subscribe(topLayerTooltipHandler); } @@ -30,6 +32,7 @@ public ZetaClient(Zeta zeta) { public final ClientTicker ticker; public final ClientConfigManager clientConfigManager; public final TopLayerTooltipHandler topLayerTooltipHandler; + public final DyeablesRegistry.Client clientDyeablesRegistry; public abstract void start(); } diff --git a/src/main/java/vazkii/zeta/event/ZRegister.java b/src/main/java/vazkii/zeta/event/ZRegister.java index dbf2a952cf..8d3534d7a8 100644 --- a/src/main/java/vazkii/zeta/event/ZRegister.java +++ b/src/main/java/vazkii/zeta/event/ZRegister.java @@ -1,7 +1,19 @@ package vazkii.zeta.event; import vazkii.zeta.event.bus.IZetaLoadEvent; +import vazkii.zeta.registry.ZetaRegistry; +@SuppressWarnings("ClassCanBeRecord") public class ZRegister implements IZetaLoadEvent { + public final ZetaRegistry registry; + + public ZRegister(ZetaRegistry registry) { + this.registry = registry; + } + + public ZetaRegistry getRegistry() { + return registry; + } + public static class Post implements IZetaLoadEvent { } } diff --git a/src/main/java/vazkii/quark/base/recipe/DyeRecipe.java b/src/main/java/vazkii/zeta/recipe/ZetaDyeRecipe.java similarity index 66% rename from src/main/java/vazkii/quark/base/recipe/DyeRecipe.java rename to src/main/java/vazkii/zeta/recipe/ZetaDyeRecipe.java index b1511196e5..80c2088b9f 100644 --- a/src/main/java/vazkii/quark/base/recipe/DyeRecipe.java +++ b/src/main/java/vazkii/zeta/recipe/ZetaDyeRecipe.java @@ -1,9 +1,8 @@ -package vazkii.quark.base.recipe; +package vazkii.zeta.recipe; import java.util.List; import com.google.common.collect.Lists; - import net.minecraft.resources.ResourceLocation; import net.minecraft.world.inventory.CraftingContainer; import net.minecraft.world.item.DyeItem; @@ -13,18 +12,19 @@ import net.minecraft.world.item.crafting.RecipeSerializer; import net.minecraft.world.item.crafting.SimpleRecipeSerializer; import net.minecraft.world.level.Level; -import vazkii.quark.base.handler.DyeHandler; - -// copy of ArmorDyeRecipe but tweaked for our system -public class DyeRecipe extends CustomRecipe { - - public static final SimpleRecipeSerializer SERIALIZER = new SimpleRecipeSerializer<>(DyeRecipe::new); - - public DyeRecipe(ResourceLocation p_43757_) { - super(p_43757_); +import vazkii.zeta.registry.DyeablesRegistry; + +// copy of ArmorDyeRecipe +public class ZetaDyeRecipe extends CustomRecipe { + protected final DyeablesRegistry dyeablesRegistry; + protected final SimpleRecipeSerializer serializer; + + public ZetaDyeRecipe(ResourceLocation id, DyeablesRegistry dyeablesRegistry) { + super(id); + this.dyeablesRegistry = dyeablesRegistry; + this.serializer = new SimpleRecipeSerializer<>(id_ -> new ZetaDyeRecipe(id_, dyeablesRegistry)); } - @Override public boolean matches(CraftingContainer p_43769_, Level p_43770_) { ItemStack itemstack = ItemStack.EMPTY; List list = Lists.newArrayList(); @@ -32,7 +32,7 @@ public boolean matches(CraftingContainer p_43769_, Level p_43770_) { for(int i = 0; i < p_43769_.getContainerSize(); ++i) { ItemStack itemstack1 = p_43769_.getItem(i); if (!itemstack1.isEmpty()) { - if (DyeHandler.isDyeable(itemstack1)) { // <- changed + if (dyeablesRegistry.isDyeable(itemstack1)) { // <- changed if (!itemstack.isEmpty()) { return false; } @@ -60,7 +60,7 @@ public ItemStack assemble(CraftingContainer p_43767_) { ItemStack itemstack1 = p_43767_.getItem(i); if (!itemstack1.isEmpty()) { Item item = itemstack1.getItem(); - if (DyeHandler.isDyeable(itemstack1)) { + if (dyeablesRegistry.isDyeable(itemstack1)) { // <- changed if (!itemstack.isEmpty()) { return ItemStack.EMPTY; } @@ -76,16 +76,18 @@ public ItemStack assemble(CraftingContainer p_43767_) { } } - return !itemstack.isEmpty() && !list.isEmpty() ? DyeHandler.dyeItem(itemstack, list) : ItemStack.EMPTY; + return !itemstack.isEmpty() && !list.isEmpty() ? + dyeablesRegistry.dyeItem(itemstack, list) : // <- changed + ItemStack.EMPTY; } @Override - public boolean canCraftInDimensions(int p_43759_, int p_43760_) { - return p_43759_ * p_43760_ >= 2; + public boolean canCraftInDimensions(int width, int height) { + return width * height >= 2; } @Override public RecipeSerializer getSerializer() { - return SERIALIZER; + return serializer; } } diff --git a/src/main/java/vazkii/zeta/registry/DyeablesRegistry.java b/src/main/java/vazkii/zeta/registry/DyeablesRegistry.java new file mode 100644 index 0000000000..3c41379f86 --- /dev/null +++ b/src/main/java/vazkii/zeta/registry/DyeablesRegistry.java @@ -0,0 +1,183 @@ +package vazkii.zeta.registry; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Supplier; + +import net.minecraft.client.color.item.ItemColor; +import net.minecraft.client.renderer.item.ClampedItemPropertyFunction; +import net.minecraft.client.renderer.item.ItemProperties; +import net.minecraft.core.BlockPos; +import net.minecraft.core.Registry; +import net.minecraft.core.cauldron.CauldronInteraction; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.InteractionHand; +import net.minecraft.world.InteractionResult; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.DyeItem; +import net.minecraft.world.item.DyeableLeatherItem; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.LayeredCauldronBlock; +import net.minecraft.world.level.block.state.BlockState; +import vazkii.zeta.Zeta; +import vazkii.zeta.client.ZetaClient; +import vazkii.zeta.client.event.ZAddItemColorHandlers; +import vazkii.zeta.event.ZRegister; +import vazkii.zeta.event.bus.LoadEvent; +import vazkii.zeta.module.ZetaModule; +import vazkii.zeta.recipe.ZetaDyeRecipe; + +public class DyeablesRegistry { + protected final Zeta z; + + public final Map> dyeableConditions = new HashMap<>(); + public final DyeableLeatherItem SURROGATE = new DyeableLeatherItem() {}; //Simply an accessor for various DyeableLeatherItem default methods + + public DyeablesRegistry(Zeta z) { + this.z = z; + z.loadBus.subscribe(this); + } + + @LoadEvent + public void register(ZRegister event) { + ResourceLocation id = event.getRegistry().newResourceLocation("dye_item"); + ZetaDyeRecipe recipe = new ZetaDyeRecipe(id, this); + event.getRegistry().register(recipe.getSerializer(), id, Registry.RECIPE_SERIALIZER_REGISTRY); + } + + @LoadEvent + public void registerPost(ZRegister.Post event) { + WashingInteraction wosh = new WashingInteraction(); + for(Item item : dyeableConditions.keySet()) + CauldronInteraction.WATER.put(item, wosh); + } + + class WashingInteraction implements CauldronInteraction { + //Copy of CauldronInteraction.DYED_ITEM + @Override + public InteractionResult interact(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, ItemStack stack) { + if(!isDyed(stack)) + return InteractionResult.PASS; + + if(!level.isClientSide) { + SURROGATE.clearColor(stack); + //player.awardStat(Stats.CLEAN_ARMOR); + LayeredCauldronBlock.lowerFillLevel(state, level, pos); + } + + return InteractionResult.sidedSuccess(level.isClientSide); + } + } + + public void register(Item item) { + register(item, () -> true); + } + + public void register(Item item, ZetaModule module) { + register(item, () -> module.enabled); + } + + public void register(Item item, Supplier cond) { + dyeableConditions.put(item, cond); + } + + public boolean isDyeable(ItemStack stack) { + Item item = stack.getItem(); + return dyeableConditions.containsKey(item) && dyeableConditions.get(item).get(); + } + + public boolean isDyed(ItemStack stack) { + return isDyeable(stack) && SURROGATE.hasCustomColor(stack); + } + + public int getDye(ItemStack stack) { + return SURROGATE.getColor(stack); + } + + public void applyDye(ItemStack stack, int color) { + if(isDyeable(stack)) + SURROGATE.setColor(stack, color); + } + + public int getColor(ItemStack stack) { + return isDyed(stack) ? SURROGATE.getColor(stack) : 0xFF_FF_FF; + } + + // Copy of DyeableLeatherItem + public ItemStack dyeItem(ItemStack stack, List dyes) { + ItemStack itemstack; + int[] aint = new int[3]; + int i = 0; + int j = 0; + + if(isDyeable(stack)) { + itemstack = stack.copy(); + itemstack.setCount(1); + if(SURROGATE.hasCustomColor(stack)) { + int k = SURROGATE.getColor(itemstack); + float f = (float) (k >> 16 & 255) / 255.0F; + float f1 = (float) (k >> 8 & 255) / 255.0F; + float f2 = (float) (k & 255) / 255.0F; + i += (int) (Math.max(f, Math.max(f1, f2)) * 255.0F); + aint[0] += (int) (f * 255.0F); + aint[1] += (int) (f1 * 255.0F); + aint[2] += (int) (f2 * 255.0F); + ++j; + } + + for(DyeItem dyeitem : dyes) { + float[] afloat = dyeitem.getDyeColor().getTextureDiffuseColors(); + int i2 = (int) (afloat[0] * 255.0F); + int l = (int) (afloat[1] * 255.0F); + int i1 = (int) (afloat[2] * 255.0F); + i += Math.max(i2, Math.max(l, i1)); + aint[0] += i2; + aint[1] += l; + aint[2] += i1; + ++j; + } + + int j1 = aint[0] / j; + int k1 = aint[1] / j; + int l1 = aint[2] / j; + float f3 = (float) i / (float) j; + float f4 = (float) Math.max(j1, Math.max(k1, l1)); + j1 = (int) ((float) j1 * f3 / f4); + k1 = (int) ((float) k1 * f3 / f4); + l1 = (int) ((float) l1 * f3 / f4); + int j2 = (j1 << 8) + k1; + j2 = (j2 << 8) + l1; + SURROGATE.setColor(itemstack, j2); + + return itemstack; + } + + return ItemStack.EMPTY; + } + + //TODO: uhhh i think this is good enough for classloading ... + public class Client { + protected final ZetaClient zc; + + public Client(ZetaClient zc) { + this.zc = zc; + } + + @LoadEvent + public void colorHandlers(ZAddItemColorHandlers event) { + ClampedItemPropertyFunction isDyed = (stack, level, entity, i) -> isDyed(stack) ? 1 : 0; + ItemColor color = (stack, layer) -> layer == 0 ? getColor(stack) : 0xFF_FF_FF; + + //apparently ItemPropertyFunctions are weird and can only be assigned to the minecraft: namespace + ResourceLocation isDyedId = new ResourceLocation("minecraft", zc.zeta.modid + "_dyed"); + + for(Item item : dyeableConditions.keySet()) { + ItemProperties.register(item, isDyedId, isDyed); + event.register(color, item); + } + } + } +} diff --git a/src/main/java/vazkii/zeta/registry/ZetaRegistry.java b/src/main/java/vazkii/zeta/registry/ZetaRegistry.java index 4d8dfdcd31..6ab04b53e8 100644 --- a/src/main/java/vazkii/zeta/registry/ZetaRegistry.java +++ b/src/main/java/vazkii/zeta/registry/ZetaRegistry.java @@ -56,10 +56,14 @@ public ResourceLocation getInternalName(Object obj) { } public void register(T obj, String resloc, ResourceKey> registry) { + register(obj, newResourceLocation(resloc), registry); + } + + public void register(T obj, ResourceLocation id, ResourceKey> registry) { if(obj == null) throw new IllegalArgumentException("Can't register null object."); - setInternalName(obj, newResourceLocation(resloc)); + setInternalName(obj, id); defers.put(registry.location(), () -> obj); } diff --git a/src/main/java/vazkii/zetaimplforge/ForgeZeta.java b/src/main/java/vazkii/zetaimplforge/ForgeZeta.java index 0b04203fa5..7684101ed3 100644 --- a/src/main/java/vazkii/zetaimplforge/ForgeZeta.java +++ b/src/main/java/vazkii/zetaimplforge/ForgeZeta.java @@ -105,7 +105,7 @@ public void registerHighest(RegisterEvent e) { if(registerDone) return; - loadBus.fire(new ZRegister()); + loadBus.fire(new ZRegister(registry)); loadBus.fire(new ZRegister.Post()); registerDone = true; diff --git a/zeta-todo/README.md b/zeta-todo/README.md index 391a3f2845..8a20ad8065 100644 --- a/zeta-todo/README.md +++ b/zeta-todo/README.md @@ -150,6 +150,30 @@ Obsolete things: # can you add a note to the todo about datagen -Sure I can do that +Sure I can do that. quark datagen should be real -quark datagen should be real \ No newline at end of file +# (quat) ok more notes page + +## handler vs util + +in my head "handler" implies its main purpose is to subscribe to events, and "util" is a collection of static methods + +handlers can be stateful. ideally util classes should be entirely static methods with no static fields. because we're trying to be more careful about `static` usage (so multiple instances of Zeta can be initialized by different uncooperating mods) + +## config + +the config reloading situation is not great imo. There's `IConfigType`, at this point all of the GUI stuff has been ditched and it means exactly two things: + +* The `@Config` annotation scanner should descend into me and make a subcategory with my @Config fields +* Give me a call back when the config gets reloaded (also give me the module i belong to). + +This is nice since the callback is guaranteed to run immediately after all the `@Config` fields have been updated. But the `ZetaModule`/`ConfigFlagManager` arguments are sorta indicative of a leaky abstraction (why don't config subsections already get access to the module they belong to?), and it's a system only available to these special `IConfigType` subsections but not to modules or other bits of code. + +Everything else is relegated to the `ZConfigChanged` event, which doesn't have as strong of an ordering guarantee. + +## Event bus ordering, in general + +Zeta takes a very event-bus-centric approach to things, but: + +* there is a big risk of hidden dependencies, where handler A has to run before handler B but nobody notices +* when you *do* need strong ordering guarantees, it is pretty hard to express them (you need pre/post events, or to cram multiple tasks into a single event handler) \ No newline at end of file