aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/ItemCustomizeManager.java
diff options
context:
space:
mode:
authorNopoTheGamer <40329022+NopoTheGamer@users.noreply.github.com>2023-03-12 02:05:14 +1100
committerGitHub <noreply@github.com>2023-03-11 16:05:14 +0100
commitfd063280c717e974bc9ac35f730e61b1be9de856 (patch)
tree1a9682ff3c573abc1e47a9f4bcec97128f37696a /src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/ItemCustomizeManager.java
parent95e17c191b8a0bdfa5af09a9cf658fe741ecaafc (diff)
downloadnotenoughupdates-fd063280c717e974bc9ac35f730e61b1be9de856.tar.gz
notenoughupdates-fd063280c717e974bc9ac35f730e61b1be9de856.tar.bz2
notenoughupdates-fd063280c717e974bc9ac35f730e61b1be9de856.zip
Custom items in /neucustomize (#609)
* should work :tm:, probably breaks other mods, needs to be added to /neurename gui * Improved compatibility with other mods probably * Added a text box to /neurename * Polish up everything * Fix skulls have durability bar * Should be everything finished * add support for metadata and skulls rendering properly * Made gui look nice * fix leather not being coloured while moving it * Fixed normal helmets not rendering * fix pumpkin not showing overlay * Fixed skulls showing as null by default on most other items * added support to randomize the subitems with ":?" * Add support for custom armour besides the head * move custom armour function * code dupe glitch * A small sign in the permanent fight against duplication of code. * clear cache on world unload --------- Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com>
Diffstat (limited to 'src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/ItemCustomizeManager.java')
-rw-r--r--src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/ItemCustomizeManager.java115
1 files changed, 114 insertions, 1 deletions
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/ItemCustomizeManager.java b/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/ItemCustomizeManager.java
index dad801ec..ecab042c 100644
--- a/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/ItemCustomizeManager.java
+++ b/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/ItemCustomizeManager.java
@@ -22,16 +22,23 @@ package io.github.moulberry.notenoughupdates.miscfeatures;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.github.moulberry.notenoughupdates.NEUManager;
-import io.github.moulberry.notenoughupdates.NotEnoughUpdates;
+import io.github.moulberry.notenoughupdates.autosubscribe.NEUAutoSubscribe;
import io.github.moulberry.notenoughupdates.core.ChromaColour;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GlStateManager;
+import net.minecraft.client.renderer.entity.layers.LayerArmorBase;
import net.minecraft.client.renderer.texture.DynamicTexture;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.client.resources.IResourceManager;
import net.minecraft.client.resources.IResourceManagerReloadListener;
+import net.minecraft.entity.EntityLivingBase;
+import net.minecraft.init.Items;
+import net.minecraft.item.Item;
+import net.minecraft.item.ItemArmor;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
+import net.minecraftforge.event.world.WorldEvent;
+import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL14;
@@ -46,9 +53,12 @@ import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
import java.util.HashMap;
+import java.util.Random;
import java.util.function.Consumer;
+@NEUAutoSubscribe
public class ItemCustomizeManager {
public static class ReloadListener implements IResourceManagerReloadListener {
@Override
@@ -81,6 +91,9 @@ public class ItemCustomizeManager {
public String customGlintColour = DEFAULT_GLINT_COLOR;
public String customLeatherColour = null;
+
+ public String defaultItem = null;
+ public String customItem = null;
}
public static void putItemData(String uuid, ItemData data) {
@@ -313,4 +326,104 @@ public class ItemCustomizeManager {
} catch (Exception ignored) {
}
}
+
+ public static Item getCustomItem(ItemStack stack) {
+ ItemData data = getDataForItem(stack);
+ if (data == null || data.customItem == null || data.customItem.length() == 0) return stack.getItem();
+ Item newItem = Item.getByNameOrId(data.customItem.split(":")[0]);
+ if (newItem == null) return stack.getItem();
+ return newItem;
+ }
+
+ public static Item getCustomItem(ItemStack stack, String newItemString) {
+ Item newItem = Item.getByNameOrId(newItemString.split(":")[0]);
+ if (newItem == null) return stack.getItem();
+ return newItem;
+ }
+
+ static Random random = new Random();
+ static HashMap<Integer, Long> lastUpdate = new HashMap<>();
+ static HashMap<Integer, Integer> damageMap = new HashMap<>();
+
+ public static int getCustomItemDamage(ItemStack stack) {
+ ItemData data = getDataForItem(stack);
+ if (data == null || data.customItem == null || data.customItem.length() == 0) return stack.getMetadata();
+ try {
+ String damageString = data.customItem.split(":")[1];
+ if (damageString.equals("?")) {
+ ArrayList<ItemStack> list = new ArrayList<>();
+ getCustomItem(stack).getSubItems(getCustomItem(stack), null, list);
+ if (damageMap.get(stack.getTagCompound().hashCode()) == null || System.currentTimeMillis() - lastUpdate.get(stack.getTagCompound().hashCode()) > 250) {
+ damageMap.put(stack.getTagCompound().hashCode(), random.nextInt(list.size()));
+
+ lastUpdate.put(stack.getTagCompound().hashCode(), System.currentTimeMillis());
+ }
+ return damageMap.get(stack.getTagCompound().hashCode());
+ }
+ return Integer.parseInt(data.customItem.split(":")[1]);
+ } catch (Exception e) {
+ if (Item.getByNameOrId(data.defaultItem) == Items.skull && getCustomItem(stack) != Items.skull) return 0;
+ return stack.getMetadata();
+ }
+ }
+
+ @SubscribeEvent
+ public void onWorldUnload(WorldEvent.Unload event) {
+ damageMap.clear();
+ lastUpdate.clear();
+ }
+
+ public static boolean shouldRenderLeatherColour(ItemStack stack) {
+ ItemData data = getDataForItem(stack);
+ if (data == null || data.customItem == null || data.customItem.length() == 0) return stack.getItem() instanceof ItemArmor &&
+ ((ItemArmor) stack.getItem()).getArmorMaterial() == ItemArmor.ArmorMaterial.LEATHER;
+ Item item = Item.getByNameOrId(data.customItem);
+ if (item == null) return stack.getItem() instanceof ItemArmor &&
+ ((ItemArmor) stack.getItem()).getArmorMaterial() == ItemArmor.ArmorMaterial.LEATHER;
+ return item instanceof ItemArmor &&
+ ((ItemArmor) item).getArmorMaterial() == ItemArmor.ArmorMaterial.LEATHER;
+ }
+
+ public static boolean hasCustomItem(ItemStack stack) {
+ ItemData data = getDataForItem(stack);
+ if (data == null || data.customItem == null || data.customItem.length() == 0 || data.defaultItem == null || data.customItem.equals(data.defaultItem)) return false;
+ Item item = Item.getByNameOrId(data.customItem.split(":")[0]);
+ Item defaultItem = Item.getByNameOrId(data.defaultItem);
+ if (item == null) {
+ data.customItem = null;
+ return false;
+ }
+ return defaultItem != item;
+ }
+
+ public static ItemStack useCustomArmour(LayerArmorBase<?> instance, EntityLivingBase entitylivingbaseIn, int armorSlot) {
+ ItemStack stack = instance.getCurrentArmor(entitylivingbaseIn, armorSlot);
+ if (stack == null) return stack;
+ ItemStack newStack = stack.copy();
+ newStack.setItem(ItemCustomizeManager.getCustomItem(newStack));
+ newStack.setItemDamage(ItemCustomizeManager.getCustomItemDamage(newStack));
+ if (armorSlot != 4) {
+ if (newStack.getItem() instanceof ItemArmor) return newStack;
+ else return stack;
+ }
+ return newStack;
+ }
+
+ public static ItemStack useCustomItem(ItemStack stack) {
+ if (stack == null) return stack;
+ if (!ItemCustomizeManager.hasCustomItem(stack)) return stack;
+ ItemStack newStack = stack.copy();
+ newStack.setItem(ItemCustomizeManager.getCustomItem(newStack));
+ newStack.setItemDamage(ItemCustomizeManager.getCustomItemDamage(newStack));
+ return newStack;
+ }
+
+ public static ItemStack setHeadArmour(EntityLivingBase instance, int i) {
+ if (instance.getCurrentArmor(3) == null) return null;
+ ItemStack stack = instance.getCurrentArmor(3).copy();
+ stack.setItem(ItemCustomizeManager.getCustomItem(stack));
+ stack.setItemDamage(ItemCustomizeManager.getCustomItemDamage(stack));
+ return stack;
+ }
+
}