Skip to content

Commit

Permalink
Easier color handler accessor, and BucketsShowInhabitantsModule
Browse files Browse the repository at this point in the history
  • Loading branch information
quat1024 committed Oct 28, 2023
1 parent 6043a3a commit 63202cf
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,182 +4,172 @@
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.renderer.item.ItemProperties;
import net.minecraft.client.renderer.item.ItemPropertyFunction;
import net.minecraft.core.Holder;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.Tag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.animal.TropicalFish;
import net.minecraft.world.entity.animal.axolotl.Axolotl;
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.registries.ForgeRegistries;
import vazkii.zeta.event.bus.LoadEvent;
import vazkii.zeta.client.event.ZAddItemColorHandlers;
import vazkii.zeta.client.event.ZClientSetup;
import vazkii.zeta.module.ZetaLoadModule;
import vazkii.zeta.util.ItemNBTHelper;
import vazkii.quark.base.Quark;
import vazkii.quark.base.module.LoadModule;
import vazkii.zeta.module.ZetaModule;
import vazkii.quark.base.module.config.Config;
import vazkii.quark.content.mobs.entity.Crab;
import vazkii.quark.content.mobs.module.CrabsModule;
import vazkii.quark.content.tools.item.SlimeInABucketItem;
import vazkii.quark.content.tools.module.SlimeInABucketModule;
import vazkii.quark.mixin.client.accessor.AccessorItemColors;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.UUID;
import java.util.function.BooleanSupplier;
import java.util.function.IntUnaryOperator;

@LoadModule(category = "client")
@ZetaLoadModule(category = "client")
public class BucketsShowInhabitantsModule extends ZetaModule {

@Config
public static boolean showAxolotls = true;
public boolean showAxolotls = true;
@Config
public static boolean showCrabs = true;
public boolean showCrabs = true;
@Config
public static boolean showTropicalFish = true;
public boolean showTropicalFish = true;
@Config
public static boolean showShinySlime = true;

@LoadEvent
@OnlyIn(Dist.CLIENT)
public void clientSetup(ZClientSetup e) {
e.enqueueWork(() -> {
ItemProperties.register(Items.AXOLOTL_BUCKET, new ResourceLocation(Quark.MOD_ID, "variant"),
new MobBucketVariantProperty(Axolotl.Variant.BY_ID.length, () -> showAxolotls));
ItemProperties.register(CrabsModule.crab_bucket, new ResourceLocation(Quark.MOD_ID, "variant"),
new MobBucketVariantProperty(Crab.COLORS, () -> showCrabs));

ItemProperties.register(SlimeInABucketModule.slime_in_a_bucket, new ResourceLocation(Quark.MOD_ID, "shiny"),
new ShinyMobBucketProperty(() -> showShinySlime && VariantAnimalTexturesModule.enabled() && VariantAnimalTexturesModule.enableShinySlime));

ItemProperties.register(Items.TROPICAL_FISH_BUCKET, new ResourceLocation(Quark.MOD_ID, "base"),
new TropicalFishBucketVariantProperty(TropicalFish::getBaseVariant, () -> showTropicalFish));
ItemProperties.register(Items.TROPICAL_FISH_BUCKET, new ResourceLocation(Quark.MOD_ID, "pattern"),
new TropicalFishBucketVariantProperty(TropicalFish::getPatternVariant, () -> showTropicalFish));
});
}
public boolean showShinySlime = true;

@ZetaLoadModule(clientReplacement = true)
public static class Client extends BucketsShowInhabitantsModule {

@LoadEvent
public void clientSetup(ZClientSetup e) {
e.enqueueWork(() -> {
ItemProperties.register(Items.AXOLOTL_BUCKET, new ResourceLocation(Quark.MOD_ID, "variant"),
new MobBucketVariantProperty(Axolotl.Variant.BY_ID.length, () -> showAxolotls));
ItemProperties.register(CrabsModule.crab_bucket, new ResourceLocation(Quark.MOD_ID, "variant"),
new MobBucketVariantProperty(Crab.COLORS, () -> showCrabs));

ItemProperties.register(SlimeInABucketModule.slime_in_a_bucket, new ResourceLocation(Quark.MOD_ID, "shiny"),
new ShinyMobBucketProperty(() -> showShinySlime && VariantAnimalTexturesModule.enabled() && VariantAnimalTexturesModule.enableShinySlime));

ItemProperties.register(Items.TROPICAL_FISH_BUCKET, new ResourceLocation(Quark.MOD_ID, "base"),
new TropicalFishBucketVariantProperty(TropicalFish::getBaseVariant, () -> showTropicalFish));
ItemProperties.register(Items.TROPICAL_FISH_BUCKET, new ResourceLocation(Quark.MOD_ID, "pattern"),
new TropicalFishBucketVariantProperty(TropicalFish::getPatternVariant, () -> showTropicalFish));
});
}

@LoadEvent
@OnlyIn(Dist.CLIENT)
public void registerItemColors(ZAddItemColorHandlers evt) {
Holder.Reference<Item> tropicalBucket = ForgeRegistries.ITEMS.getDelegateOrThrow(Items.TROPICAL_FISH_BUCKET);
ItemColor parent = ((AccessorItemColors) evt.getItemColors()).quark$getItemColors().get(tropicalBucket);
evt.register(new TropicalFishBucketColor(parent, () -> showTropicalFish), Items.TROPICAL_FISH_BUCKET);
}
@LoadEvent
public void registerItemColors(ZAddItemColorHandlers evt) {
ItemColor parent = evt.getItemColor(Items.TROPICAL_FISH_BUCKET);
evt.register(new TropicalFishBucketColor(parent, () -> showTropicalFish), Items.TROPICAL_FISH_BUCKET);
}

@SuppressWarnings("deprecation")
@OnlyIn(Dist.CLIENT)
private class MobBucketVariantProperty implements ItemPropertyFunction {
private class MobBucketVariantProperty implements ItemPropertyFunction {

private final int maxVariants;
private final BooleanSupplier featureEnabled;
private final int maxVariants;
private final BooleanSupplier featureEnabled;

public MobBucketVariantProperty(int maxVariants, BooleanSupplier featureEnabled) {
this.maxVariants = maxVariants;
this.featureEnabled = featureEnabled;
}
public MobBucketVariantProperty(int maxVariants, BooleanSupplier featureEnabled) {
this.maxVariants = maxVariants;
this.featureEnabled = featureEnabled;
}

@Override
public float call(@Nonnull ItemStack stack, @Nullable ClientLevel level, @Nullable LivingEntity entity, int id) {
if (!enabled || !featureEnabled.getAsBoolean())
return 0;
@Override
public float call(@Nonnull ItemStack stack, @Nullable ClientLevel level, @Nullable LivingEntity entity, int id) {
if(!enabled || !featureEnabled.getAsBoolean())
return 0;

return ItemNBTHelper.getInt(stack, Axolotl.VARIANT_TAG, 0) % maxVariants;
return ItemNBTHelper.getInt(stack, Axolotl.VARIANT_TAG, 0) % maxVariants;
}
}
}

@SuppressWarnings("deprecation")
@OnlyIn(Dist.CLIENT)
private class ShinyMobBucketProperty implements ItemPropertyFunction {
private class ShinyMobBucketProperty implements ItemPropertyFunction {

private final BooleanSupplier featureEnabled;
private final BooleanSupplier featureEnabled;

public ShinyMobBucketProperty(BooleanSupplier featureEnabled) {
this.featureEnabled = featureEnabled;
}
public ShinyMobBucketProperty(BooleanSupplier featureEnabled) {
this.featureEnabled = featureEnabled;
}

@Override
public float call(@Nonnull ItemStack stack, @Nullable ClientLevel level, @Nullable LivingEntity entity, int id) {
if (!enabled || !featureEnabled.getAsBoolean())
return 0;
@Override
public float call(@Nonnull ItemStack stack, @Nullable ClientLevel level, @Nullable LivingEntity entity, int id) {
if(!enabled || !featureEnabled.getAsBoolean())
return 0;

CompoundTag data = ItemNBTHelper.getCompound(stack, SlimeInABucketItem.TAG_ENTITY_DATA, true);
if (data != null && data.hasUUID("UUID")) {
UUID uuid = data.getUUID("UUID");
if (VariantAnimalTexturesModule.isShiny(uuid))
return 1;
}
CompoundTag data = ItemNBTHelper.getCompound(stack, SlimeInABucketItem.TAG_ENTITY_DATA, true);
if(data != null && data.hasUUID("UUID")) {
UUID uuid = data.getUUID("UUID");
if(VariantAnimalTexturesModule.isShiny(uuid))
return 1;
}

return 0;
return 0;
}
}
}

@SuppressWarnings("deprecation")
@OnlyIn(Dist.CLIENT)
private class TropicalFishBucketVariantProperty implements ItemPropertyFunction {
private class TropicalFishBucketVariantProperty implements ItemPropertyFunction {

private final IntUnaryOperator extractor;
private final BooleanSupplier featureEnabled;
private final IntUnaryOperator extractor;
private final BooleanSupplier featureEnabled;

public TropicalFishBucketVariantProperty(IntUnaryOperator extractor, BooleanSupplier featureEnabled) {
this.extractor = extractor;
this.featureEnabled = featureEnabled;
}
public TropicalFishBucketVariantProperty(IntUnaryOperator extractor, BooleanSupplier featureEnabled) {
this.extractor = extractor;
this.featureEnabled = featureEnabled;
}

@Override
public float call(@Nonnull ItemStack stack, @Nullable ClientLevel level, @Nullable LivingEntity entity, int id) {
if (!enabled || !featureEnabled.getAsBoolean())
return 0;
@Override
public float call(@Nonnull ItemStack stack, @Nullable ClientLevel level, @Nullable LivingEntity entity, int id) {
if(!enabled || !featureEnabled.getAsBoolean())
return 0;

CompoundTag tag = stack.getTag();
if (tag != null && tag.contains(TropicalFish.BUCKET_VARIANT_TAG, Tag.TAG_INT)) {
int variant = tag.getInt(TropicalFish.BUCKET_VARIANT_TAG);
return extractor.applyAsInt(variant) + 1;
}
CompoundTag tag = stack.getTag();
if(tag != null && tag.contains(TropicalFish.BUCKET_VARIANT_TAG, Tag.TAG_INT)) {
int variant = tag.getInt(TropicalFish.BUCKET_VARIANT_TAG);
return extractor.applyAsInt(variant) + 1;
}

return 0;
return 0;
}
}
}

@OnlyIn(Dist.CLIENT)
private class TropicalFishBucketColor implements ItemColor {
private class TropicalFishBucketColor implements ItemColor {

@Nullable
private final ItemColor parent;
private final BooleanSupplier featureEnabled;
@Nullable
private final ItemColor parent;
private final BooleanSupplier featureEnabled;

public TropicalFishBucketColor(@Nullable ItemColor parent, BooleanSupplier featureEnabled) {
this.parent = parent;
this.featureEnabled = featureEnabled;
}
public TropicalFishBucketColor(@Nullable ItemColor parent, BooleanSupplier featureEnabled) {
this.parent = parent;
this.featureEnabled = featureEnabled;
}

@Override
public int getColor(@Nonnull ItemStack stack, int layer) {
if (enabled && featureEnabled.getAsBoolean() && (layer == 1 || layer == 2)) {
CompoundTag tag = stack.getTag();
if (tag != null && tag.contains(TropicalFish.BUCKET_VARIANT_TAG, Tag.TAG_INT)) {
int variant = tag.getInt(TropicalFish.BUCKET_VARIANT_TAG);
@Override
public int getColor(@Nonnull ItemStack stack, int layer) {
if(enabled && featureEnabled.getAsBoolean() && (layer == 1 || layer == 2)) {
CompoundTag tag = stack.getTag();
if(tag != null && tag.contains(TropicalFish.BUCKET_VARIANT_TAG, Tag.TAG_INT)) {
int variant = tag.getInt(TropicalFish.BUCKET_VARIANT_TAG);

DyeColor dyeColor = layer == 1 ? TropicalFish.getBaseColor(variant) : TropicalFish.getPatternColor(variant);
float[] colorComponents = dyeColor.getTextureDiffuseColors();
DyeColor dyeColor = layer == 1 ? TropicalFish.getBaseColor(variant) : TropicalFish.getPatternColor(variant);
float[] colorComponents = dyeColor.getTextureDiffuseColors();

return ((int) (colorComponents[0] * 255) << 16) |
((int) (colorComponents[1] * 255) << 8) |
(int) (colorComponents[2] * 255);
return ((int) (colorComponents[0] * 255) << 16) |
((int) (colorComponents[1] * 255) << 8) |
(int) (colorComponents[2] * 255);
}
}
}

return parent != null ? parent.getColor(stack, layer) : -1;
return parent != null ? parent.getColor(stack, layer) : -1;
}
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import net.minecraft.client.color.item.ItemColor;
import net.minecraft.client.color.item.ItemColors;
import net.minecraft.world.level.ItemLike;
import org.jetbrains.annotations.Nullable;
import vazkii.zeta.event.bus.IZetaLoadEvent;

public interface ZAddItemColorHandlers extends IZetaLoadEvent {
void register(ItemColor c, ItemLike... items);
ItemColors getItemColors();

@Nullable ItemColor getItemColor(ItemLike itemlike);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

import net.minecraft.client.color.item.ItemColor;
import net.minecraft.client.color.item.ItemColors;
import net.minecraft.core.Holder;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.ItemLike;
import net.minecraftforge.client.event.RegisterColorHandlersEvent;
import net.minecraftforge.registries.ForgeRegistries;
import org.jetbrains.annotations.Nullable;
import vazkii.quark.mixin.client.accessor.AccessorItemColors;
import vazkii.zeta.event.bus.FiredAs;
import vazkii.zeta.client.event.ZAddItemColorHandlers;

Expand All @@ -18,4 +23,11 @@ public void register(ItemColor c, ItemLike... items) {
public ItemColors getItemColors() {
return e.getItemColors();
}

@Override
public @Nullable ItemColor getItemColor(ItemLike item) {
//forge changes the itemcolors guts to support registry replacement i guess
Holder.Reference<Item> frog = ForgeRegistries.ITEMS.getDelegateOrThrow(item.asItem());
return ((AccessorItemColors) getItemColors()).quark$getItemColors().get(frog);
}
}

0 comments on commit 63202cf

Please sign in to comment.