diff options
author | Johann Bernhardt <johann.bernhardt@tum.de> | 2021-12-12 19:38:06 +0100 |
---|---|---|
committer | Johann Bernhardt <johann.bernhardt@tum.de> | 2021-12-12 19:38:06 +0100 |
commit | 311ab89f93558233a40079f7cb16605b141b5346 (patch) | |
tree | c5f44ef47f441a57c5f57aa801f639c7879ed760 /src/main/java/gtPlusPlus/plugin/agrichem/item | |
parent | 896143b96132f5ac54aa8d8f7386f27487e5e530 (diff) | |
download | GT5-Unofficial-311ab89f93558233a40079f7cb16605b141b5346.tar.gz GT5-Unofficial-311ab89f93558233a40079f7cb16605b141b5346.tar.bz2 GT5-Unofficial-311ab89f93558233a40079f7cb16605b141b5346.zip |
Move sources and resources
Diffstat (limited to 'src/main/java/gtPlusPlus/plugin/agrichem/item')
3 files changed, 785 insertions, 0 deletions
diff --git a/src/main/java/gtPlusPlus/plugin/agrichem/item/algae/ItemAgrichemBase.java b/src/main/java/gtPlusPlus/plugin/agrichem/item/algae/ItemAgrichemBase.java new file mode 100644 index 0000000000..abeebc054d --- /dev/null +++ b/src/main/java/gtPlusPlus/plugin/agrichem/item/algae/ItemAgrichemBase.java @@ -0,0 +1,312 @@ +package gtPlusPlus.plugin.agrichem.item.algae; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.util.List; + +import cpw.mods.fml.common.registry.GameRegistry; +import gtPlusPlus.core.item.chemistry.general.ItemGenericChemBase; +import gtPlusPlus.core.lib.CORE; +import gtPlusPlus.core.util.minecraft.ItemUtils; +import gtPlusPlus.core.util.minecraft.OreDictUtils; +import gtPlusPlus.core.util.reflect.ReflectionUtils; +import net.minecraft.client.renderer.texture.IIconRegister; +import net.minecraft.client.renderer.texture.TextureAtlasSprite; +import net.minecraft.creativetab.CreativeTabs; +import net.minecraft.entity.Entity; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.EnumRarity; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.util.EnumChatFormatting; +import net.minecraft.util.IIcon; +import net.minecraft.world.World; +import net.minecraftforge.oredict.OreDictionary; + +public class ItemAgrichemBase extends Item { + + final protected IIcon base[]; + + /* + * 0 - Algae Biomass + * 1 - Green Algae Biomass + * 2 - Brown Algae Biomass + * 3 - Golden-Brown Algae Biomass + * 4 - Red Algae Biomass + * 5 - Cellulose Fiber + * 6 - Golden-Brown Cellulose Fiber + * 7 - Red Cellulose Fiber + * 8 - Compost + * 9 - Wood Pellet + * 10 - Wood Brick + * 11 - Cellulose Pulp + * 12 - Raw Bio Resin + * 13 - Catalyst Carrier + * 14 - Green Metal Catalyst + * 15 - Alginic Acid + * 16 - Alumina + * 17 - Aluminium Pellet + * 18 - Sodium Aluminate + * 19 - Sodium Hydroxide // Exists in Newer GT + * 20 - Sodium Carbonate + * 21 - Lithium Chloride + * 22 - Pellet Mold + * 23 - Clean Aluminium Mix + * 24 - Pinecone + * 25 - Crushed Pine + */ + + public ItemAgrichemBase() { + this.setHasSubtypes(true); + this.setNoRepair(); + this.setMaxStackSize(64); + this.setMaxDamage(0); + base = new IIcon[26]; + this.setUnlocalizedName("BasicAgrichemItem"); + GameRegistry.registerItem(this, this.getUnlocalizedName()); + } + + @Override + public boolean isDamageable() { + return false; + } + + @Override + public boolean shouldRotateAroundWhenRendering() { + return super.shouldRotateAroundWhenRendering(); + } + + @Override + public void onUpdate(ItemStack p_77663_1_, World p_77663_2_, Entity p_77663_3_, int p_77663_4_, boolean p_77663_5_) { + super.onUpdate(p_77663_1_, p_77663_2_, p_77663_3_, p_77663_4_, p_77663_5_); + } + + @Override + public String getItemStackDisplayName(ItemStack aStack) { + return super.getItemStackDisplayName(aStack); + } + + @Override + public EnumRarity getRarity(ItemStack p_77613_1_) { + return EnumRarity.common; + } + + @Override + public boolean requiresMultipleRenderPasses() { + return false; + } + + private static boolean mHasCheckedForSodiumHydroxide = false; + private static boolean mShowSodiumHydroxide = true; + + private static boolean checkSodiumHydroxide() { + if (mHasCheckedForSodiumHydroxide) { + return mShowSodiumHydroxide; + } + else { + if (OreDictUtils.containsValidEntries("dustSodiumHydroxide_GT5U") + || OreDictUtils.containsValidEntries("dustSodiumHydroxide")) { + List<ItemStack> aTest = OreDictionary.getOres( + "dustSodiumHydroxide", false + ); + if (aTest.isEmpty()) { + aTest = OreDictionary.getOres( + "dustSodiumHydroxide_GT5U", false + ); + if (!aTest.isEmpty()) { + mShowSodiumHydroxide = false; + } + } + else { + mShowSodiumHydroxide = false; + } + } + } + mHasCheckedForSodiumHydroxide = true; + return mShowSodiumHydroxide; + } + + @Override + public void getSubItems(Item aItem, CreativeTabs p_150895_2_, List aList) { + for (int i=0;i<base.length;i++) { + if (i == 19) { + // Only show if it doesn't exist. + if (checkSodiumHydroxide()) { + aList.add(ItemUtils.simpleMetaStack(aItem, i, 1)); + } + } + else { + aList.add(ItemUtils.simpleMetaStack(aItem, i, 1)); + } + } + } + + @Override + public boolean getIsRepairable(ItemStack p_82789_1_, ItemStack p_82789_2_) { + return false; + } + + @Override + public boolean isRepairable() { + return false; + } + + @Override + public boolean isBookEnchantable(ItemStack stack, ItemStack book) { + return false; + } + + @Override + public int getDisplayDamage(ItemStack stack) { + return stack.getItemDamage(); + } + + @Override + public int getItemEnchantability() { + return 0; + } + + @Override + public int getItemEnchantability(ItemStack stack) { + return 0; + } + + @Override + public void registerIcons(final IIconRegister u) { + for (int i=0;i<this.base.length;i++) { + String aPath = CORE.MODID + ":" + "bioscience/MetaItem1/"+i; + this.base[i] = u.registerIcon(aPath); + } + } + + + private boolean isTextureValid(String aPath) { + if (aPath == null) { + return false; + } + else if (aPath.indexOf(92) == -1) { + Constructor aTextureAtlasSprite = ReflectionUtils.getConstructor( + TextureAtlasSprite.class, String.class + ); + if (aTextureAtlasSprite != null) { + try { + TextureAtlasSprite aTestAtlas = (TextureAtlasSprite) aTextureAtlasSprite.newInstance( + aPath + ); + if (aTestAtlas != null) { + return true; + } + } + catch (InstantiationException | IllegalAccessException + | IllegalArgumentException + | InvocationTargetException e) { + } + } + } + return false; + } + + @Override + public IIcon getIconFromDamageForRenderPass(final int damage, final int pass) { + return this.base[damage]; + } + + @Override + public IIcon getIconFromDamage(int damage) { + return this.base[damage]; + } + + @Override + public IIcon getIcon(ItemStack stack, int renderPass, EntityPlayer player, ItemStack usingItem, int useRemaining) { + return this.base[stack.getItemDamage()]; + } + + @Override + public IIcon getIcon(ItemStack stack, int pass) { + return this.base[stack.getItemDamage()]; + } + + @Override + public String getUnlocalizedName(ItemStack stack) { + return super.getUnlocalizedName() + "." + stack.getItemDamage(); + } + + + + @Override + public double getDurabilityForDisplay(ItemStack aStack) { + if (ItemUtils.isCatalyst(aStack)) { + if (aStack.getTagCompound() == null || aStack.getTagCompound().hasNoTags()){ + createCatalystNBT(aStack); + } + double currentDamage = getCatalystDamage(aStack); + return currentDamage / getCatalystMaxDamage(aStack); + } + else { + return 1D; + } + } + + @SuppressWarnings("unchecked") + @Override + public void addInformation(ItemStack aStack, EntityPlayer player, List list, boolean bool) { + boolean aHasSpecialTooltips = false; + int aMaxDamage = 0; + int aDamageSegment = 0; + int aDam = 0; + EnumChatFormatting durability = EnumChatFormatting.GRAY; + if (ItemUtils.isCatalyst(aStack)) { + list.add(EnumChatFormatting.GRAY+"Active Reaction Agent"); + aMaxDamage = getCatalystMaxDamage(aStack); + aDamageSegment = aMaxDamage / 5; + aDam = aMaxDamage-getCatalystDamage(aStack); + aHasSpecialTooltips = true; + } + if (aHasSpecialTooltips) { + if (aDam > aDamageSegment * 3){ + durability = EnumChatFormatting.GREEN; + } + else if (aDam > aDamageSegment * 2){ + durability = EnumChatFormatting.YELLOW; + } + else if (aDam > aDamageSegment){ + durability = EnumChatFormatting.GOLD; + } + else if (aDam >= 0){ + durability = EnumChatFormatting.RED; + } + list.add(durability+""+(aDam)+EnumChatFormatting.GRAY+" / "+aMaxDamage); + } + super.addInformation(aStack, player, list, bool); + } + + @Override + public boolean showDurabilityBar(ItemStack aStack) { + if (ItemUtils.isCatalyst(aStack)) { + int aDam = getCatalystDamage(aStack); + return aDam > 0; + } + return false; + } + + public static boolean createCatalystNBT(ItemStack rStack){ + return ItemGenericChemBase.createCatalystNBT(rStack); + } + + public static int getCatalystDamage(ItemStack aStack) { + return ItemGenericChemBase.getCatalystDamage(aStack); + } + + public static int getCatalystMaxDamage(ItemStack aStack) { + return ItemGenericChemBase.getCatalystMaxDamage(aStack); + } + + public static void setCatalystDamage(ItemStack aStack,int aAmount) { + ItemGenericChemBase.setCatalystDamage(aStack, aAmount); + } + + public static int getMaxCatalystDurability(ItemStack aStack) { + return ItemGenericChemBase.getMaxCatalystDurability(aStack); + } + +} diff --git a/src/main/java/gtPlusPlus/plugin/agrichem/item/algae/ItemAlgaeBase.java b/src/main/java/gtPlusPlus/plugin/agrichem/item/algae/ItemAlgaeBase.java new file mode 100644 index 0000000000..6b31a42809 --- /dev/null +++ b/src/main/java/gtPlusPlus/plugin/agrichem/item/algae/ItemAlgaeBase.java @@ -0,0 +1,235 @@ +package gtPlusPlus.plugin.agrichem.item.algae; + +import java.util.List; + +import cpw.mods.fml.common.registry.GameRegistry; +import gtPlusPlus.api.objects.data.AutoMap; +import gtPlusPlus.core.lib.CORE; +import gtPlusPlus.core.util.minecraft.ItemUtils; +import gtPlusPlus.plugin.agrichem.AlgaeDefinition; +import gtPlusPlus.plugin.agrichem.IAlgalItem; +import gtPlusPlus.plugin.agrichem.logic.AlgaeGeneticData; +import gtPlusPlus.plugin.agrichem.logic.AlgaeGrowthRequirement; +import net.minecraft.client.renderer.texture.IIconRegister; +import net.minecraft.creativetab.CreativeTabs; +import net.minecraft.entity.Entity; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.EnumRarity; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.util.EnumChatFormatting; +import net.minecraft.util.IIcon; +import net.minecraft.world.World; + +public class ItemAlgaeBase extends Item implements IAlgalItem { + + protected IIcon base; + protected IIcon overlay; + + public ItemAlgaeBase() { + this.setHasSubtypes(true); + this.setMaxDamage(127); + this.setNoRepair(); + this.setMaxStackSize(32); + this.setUnlocalizedName("BasicAlgaeItem"); + GameRegistry.registerItem(this, this.getUnlocalizedName()); + } + + @Override + public boolean isDamageable() { + return false; + } + + @Override + public boolean shouldRotateAroundWhenRendering() { + return super.shouldRotateAroundWhenRendering(); + } + + @Override + public void onUpdate(ItemStack p_77663_1_, World p_77663_2_, Entity p_77663_3_, int p_77663_4_, boolean p_77663_5_) { + if (!p_77663_1_.hasTagCompound() || p_77663_1_.getTagCompound().hasNoTags()) { + p_77663_1_ = initNBT(p_77663_1_); + } + super.onUpdate(p_77663_1_, p_77663_2_, p_77663_3_, p_77663_4_, p_77663_5_); + } + + @Override + public String getItemStackDisplayName(ItemStack aStack) { + return EnumChatFormatting.UNDERLINE+super.getItemStackDisplayName(aStack); + } + + @Override + public void addInformation(ItemStack aStack, EntityPlayer p_77624_2_, List aList, boolean p_77624_4_) { + int aDam = aStack.getItemDamage(); + try { + aList.add(AlgaeDefinition.getByIndex(aDam).mSimpleName); + if (!aStack.hasTagCompound() || aStack.getTagCompound().hasNoTags()) { + aStack = initNBT(aStack); + } + else { + NBTTagCompound aNBT = aStack.getTagCompound(); + boolean mRequiresLight = aNBT.getBoolean("mRequiresLight"); + boolean mSaltWater = aNBT.getBoolean("mSaltWater"); + boolean mFreshWater = aNBT.getBoolean("mFreshWater"); + byte mTempTolerance = aNBT.getByte("mTempTolerance"); + float mFertility = aNBT.getFloat("mFertility"); + float mProductionSpeed = aNBT.getFloat("mProductionSpeed"); + byte mLifespan = aNBT.getByte("mLifespan"); + int mGeneration = aNBT.getInteger("mGeneration"); + + aList.add("Requires Light: "+mRequiresLight); + aList.add("Salt Water: "+mSaltWater); + aList.add("Fresh Water: "+mFreshWater); + aList.add("Temp Tolerance: "+mTempTolerance); + aList.add("Growth: "+mFertility); + aList.add("Production: "+mProductionSpeed); + aList.add("Lifespan in days: "+mLifespan); + aList.add("Generation: "+mGeneration); + } + } + catch (Throwable t) { + t.printStackTrace(); + } + super.addInformation(aStack, p_77624_2_, aList, p_77624_4_); + } + + @Override + public EnumRarity getRarity(ItemStack p_77613_1_) { + return EnumRarity.uncommon; + } + + @Override + public boolean requiresMultipleRenderPasses() { + return true; + } + + @Override + public void getSubItems(Item aItem, CreativeTabs p_150895_2_, List aList) { + for (int i=0;i<AlgaeDefinition.values().length;i++) { + aList.add(ItemUtils.simpleMetaStack(aItem, i, 1)); + } + } + + @Override + public boolean getIsRepairable(ItemStack p_82789_1_, ItemStack p_82789_2_) { + return false; + } + + @Override + public boolean isRepairable() { + return false; + } + + @Override + public boolean isBookEnchantable(ItemStack stack, ItemStack book) { + return false; + } + + @Override + public int getDisplayDamage(ItemStack stack) { + return stack.getItemDamage(); + } + + @Override + public boolean showDurabilityBar(ItemStack stack) { + return false; + } + + @Override + public int getItemEnchantability() { + return 0; + } + + @Override + public int getItemEnchantability(ItemStack stack) { + return 0; + } + + @Override + public int getColorFromItemStack(ItemStack aStack, int aMeta) { + return AlgaeDefinition.getByIndex(aStack.getItemDamage()).mColour; + } + + @Override + public IIcon getIconFromDamageForRenderPass(final int damage, final int pass) { + if(pass == 0) { + return this.base; + } + return this.overlay; + } + + @Override + public void registerIcons(final IIconRegister i) { + this.base = i.registerIcon(CORE.MODID + ":" + "bioscience/BasicAlgae"); + this.overlay = i.registerIcon(CORE.MODID + ":" + "bioscience/BasicAlgae" + "_Overlay"); + } + + public static ItemStack initNBT(ItemStack aFreshAlgae) { + NBTTagCompound aNewTag = new NBTTagCompound(); + ItemAlgaeBase aItem; + if (aFreshAlgae.getItem() instanceof ItemAlgaeBase) { + aItem = (ItemAlgaeBase) aFreshAlgae.getItem(); + if (!aFreshAlgae.hasTagCompound()) { + AlgaeGeneticData y = aItem.getSpeciesData(aFreshAlgae); + aNewTag = y.writeToNBT(); + aFreshAlgae.setTagCompound(aNewTag); + } + else { + aNewTag = aFreshAlgae.getTagCompound(); + } + } + return aFreshAlgae; + } + + @Override + public AlgaeDefinition getAlgaeType(ItemStack aStack) { + return AlgaeDefinition.getByIndex(aStack != null ? aStack.getItemDamage() : 3); + } + + @Override + public AlgaeGeneticData getSpeciesData(ItemStack aStack) { + NBTTagCompound aTag; + if (!aStack.hasTagCompound() || aStack.getTagCompound().hasNoTags()) { + aTag = new NBTTagCompound(); + AlgaeGeneticData aGenes; + if (aStack.getItemDamage() < 3 || aStack.getItemDamage() > 5) { + aGenes = new AlgaeGeneticData(); + aTag = aGenes.writeToNBT(); + } + else { + byte aTemp, aLifespan; + float aFert, aSpeed; + + int aDam = aStack.getItemDamage(); + aTemp = (byte) (aDam == 3 ? 0 : aDam == 4 ? 2 : 1); + aLifespan = (byte) (aDam == 3 ? 1 : aDam == 4 ? 3f : 2f); + aFert = (float) (aDam == 3 ? 2f : aDam == 4 ? 1f : 1.75f); + aSpeed = (float) (aDam == 3 ? 1f : aDam == 4 ? 1.5f : 2f); + + aGenes = new AlgaeGeneticData( + true, true, + AlgaeDefinition.getByIndex(aDam).mSaltWater, AlgaeDefinition.getByIndex(aDam).mFreshWater, + aTemp, + aFert, + aSpeed, + aLifespan, + 0, + new AutoMap<AlgaeGrowthRequirement>()); + aTag = aGenes.writeToNBT(); + } + } + else { + aTag = aStack.getTagCompound(); + } + + + + + return new AlgaeGeneticData(aTag); + } + + + + +} diff --git a/src/main/java/gtPlusPlus/plugin/agrichem/item/algae/ItemBioChip.java b/src/main/java/gtPlusPlus/plugin/agrichem/item/algae/ItemBioChip.java new file mode 100644 index 0000000000..f876839f5f --- /dev/null +++ b/src/main/java/gtPlusPlus/plugin/agrichem/item/algae/ItemBioChip.java @@ -0,0 +1,238 @@ +package gtPlusPlus.plugin.agrichem.item.algae; + +import java.util.*; +import java.util.function.BiFunction; +import java.util.function.Predicate; + +import cpw.mods.fml.common.FMLCommonHandler; +import cpw.mods.fml.common.registry.GameRegistry; +import gregtech.api.GregTech_API; +import gregtech.api.enums.GT_Values; +import gregtech.api.gui.GT_GUIDialogSelectItem; +import gregtech.api.interfaces.INetworkUpdatableItem; +import gregtech.api.net.GT_Packet_UpdateItem; +import gregtech.api.objects.XSTR; +import gregtech.api.util.GT_LanguageManager; +import gregtech.api.util.GT_Utility; +import gtPlusPlus.core.lib.CORE; +import gtPlusPlus.core.util.minecraft.ItemUtils; +import net.minecraft.client.renderer.texture.IIconRegister; +import net.minecraft.creativetab.CreativeTabs; +import net.minecraft.entity.Entity; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.item.EnumRarity; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.util.*; +import net.minecraft.world.World; +import net.minecraftforge.common.util.Constants; +import net.minecraftforge.common.util.FakePlayer; +import org.apache.commons.lang3.tuple.Pair; + +public class ItemBioChip extends Item implements INetworkUpdatableItem { + private static final List<ItemStack> ALL_VARIANTS = new ArrayList<>(); + + protected IIcon base; + + public ItemBioChip() { + this.setHasSubtypes(true); + this.setNoRepair(); + this.setMaxStackSize(64); + this.setMaxDamage(0); + this.setUnlocalizedName("BioRecipeSelector"); + GameRegistry.registerItem(this, this.getUnlocalizedName()); + ALL_VARIANTS.add(new ItemStack(this, 0, 0)); + for (int i = 1; i <= 24; i++) { + ItemStack aStack = new ItemStack(this, 0, i); + ALL_VARIANTS.add(aStack); + } + } + + @Override + public boolean isDamageable() { + return false; + } + + @Override + public boolean shouldRotateAroundWhenRendering() { + return super.shouldRotateAroundWhenRendering(); + } + + @Override + public void onUpdate(ItemStack p_77663_1_, World p_77663_2_, Entity p_77663_3_, int p_77663_4_, boolean p_77663_5_) { + super.onUpdate(p_77663_1_, p_77663_2_, p_77663_3_, p_77663_4_, p_77663_5_); + } + + @Override + public String getItemStackDisplayName(ItemStack aStack) { + return super.getItemStackDisplayName(aStack); + } + + @Override + public void addInformation(ItemStack aStack, EntityPlayer p_77624_2_, List aList, boolean p_77624_4_) { + try { + aList.add("Configuration == "+aStack.getItemDamage()); + aList.add(GT_LanguageManager.addStringLocalization(new StringBuilder().append(getUnlocalizedName()).append(".tooltip.0").toString(), "Right click to reconfigure")); + aList.add(GT_LanguageManager.addStringLocalization(new StringBuilder().append(getUnlocalizedName()).append(".tooltip.1").toString(), "Needs a screwdriver or circuit programming tool")); + } + catch (Throwable t) { + t.printStackTrace(); + } + super.addInformation(aStack, p_77624_2_, aList, p_77624_4_); + } + + @Override + public EnumRarity getRarity(ItemStack p_77613_1_) { + return EnumRarity.common; + } + + @Override + public void getSubItems(Item aItem, CreativeTabs p_150895_2_, List aList) { + aList.add(ItemUtils.simpleMetaStack(aItem, 0, 1)); + } + + @Override + public boolean getIsRepairable(ItemStack p_82789_1_, ItemStack p_82789_2_) { + return false; + } + + @Override + public boolean isRepairable() { + return false; + } + + @Override + public boolean isBookEnchantable(ItemStack stack, ItemStack book) { + return false; + } + + @Override + public int getDisplayDamage(ItemStack stack) { + return stack.getItemDamage(); + } + + @Override + public boolean showDurabilityBar(ItemStack stack) { + return false; + } + + @Override + public int getItemEnchantability() { + return 0; + } + + @Override + public int getItemEnchantability(ItemStack stack) { + return 0; + } + + @Override + public void registerIcons(final IIconRegister u) { + this.base = u.registerIcon(CORE.MODID + ":" + "bioscience/BioCircuit"); + } + + @Override + public IIcon getIconFromDamageForRenderPass(final int damage, final int pass) { + return this.base; + } + + @Override + public IIcon getIconFromDamage(int damage) { + return this.base; + } + + @Override + public IIcon getIcon(ItemStack stack, int renderPass, EntityPlayer player, ItemStack usingItem, int useRemaining) { + return this.base; + } + + @Override + public IIcon getIcon(ItemStack stack, int pass) { + return this.base; + } + + @Override + public String getUnlocalizedName(ItemStack stack) { + return super.getUnlocalizedName(); + } + + @Override + public boolean receive(ItemStack stack, EntityPlayerMP player, NBTTagCompound tag) { + int meta = tag.hasKey("meta", Constants.NBT.TAG_BYTE) ? tag.getByte("meta") : -1; + if (meta < 0 || meta > 24) + return true; + + if (!player.capabilities.isCreativeMode) { + Pair<Integer, BiFunction<ItemStack, EntityPlayerMP, ItemStack>> toolIndex = findConfiguratorInInv(player); + if (toolIndex == null) return true; + + ItemStack[] mainInventory = player.inventory.mainInventory; + mainInventory[toolIndex.getKey()] = toolIndex.getValue().apply(mainInventory[toolIndex.getKey()], player); + } + stack.setItemDamage(meta); + + return true; + } + + @Override + public boolean onItemUse(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side, float xOffset, float yOffset, float zOffset) { + // nothing on server side or fake player + if (player instanceof FakePlayer || !world.isRemote) return false; + // check if any screwdriver + ItemStack configuratorStack; + if (player.capabilities.isCreativeMode) { + configuratorStack = null; + } else { + Pair<Integer, ?> configurator = findConfiguratorInInv(player); + if (configurator == null) { + int count; + try { + count = Integer.parseInt(StatCollector.translateToLocal("GT5U.item.programmed_circuit.no_screwdriver.count")); + } catch (NumberFormatException e) { + player.addChatComponentMessage(new ChatComponentText("Error in translation GT5U.item.programmed_circuit.no_screwdriver.count: " + e.getMessage())); + count = 1; + } + player.addChatComponentMessage(new ChatComponentTranslation("GT5U.item.programmed_circuit.no_screwdriver." + XSTR.XSTR_INSTANCE.nextInt(count))); + return false; + } + configuratorStack = player.inventory.mainInventory[configurator.getKey()]; + } + openSelectorGui(configuratorStack, stack.getItemDamage()); + return true; + } + + private void openSelectorGui(ItemStack configurator, int meta) { + FMLCommonHandler.instance().showGuiScreen(new GT_GUIDialogSelectItem( + StatCollector.translateToLocal("GT5U.item.programmed_circuit.select.header"), + configurator, + null, + ItemBioChip::onConfigured, + ALL_VARIANTS, + meta, + true + )); + } + + private static void onConfigured(ItemStack stack) { + NBTTagCompound tag = new NBTTagCompound(); + tag.setByte("meta", (byte) stack.getItemDamage()); + GT_Values.NW.sendToServer(new GT_Packet_UpdateItem(tag)); + } + + private static Pair<Integer, BiFunction<ItemStack, EntityPlayerMP, ItemStack>> findConfiguratorInInv(EntityPlayer player) { + ItemStack[] mainInventory = player.inventory.mainInventory; + for (int j = 0, mainInventoryLength = mainInventory.length; j < mainInventoryLength; j++) { + ItemStack toolStack = mainInventory[j]; + + if (!GT_Utility.isStackValid(toolStack)) + continue; + + for (Map.Entry<Predicate<ItemStack>, BiFunction<ItemStack, EntityPlayerMP, ItemStack>> p : GregTech_API.sCircuitProgrammerList.entrySet()) + if (p.getKey().test(toolStack)) + return Pair.of(j, p.getValue()); + } + return null; + } +} |