From 0dd80c3e5695b0c733a747f696862104931a731c Mon Sep 17 00:00:00 2001 From: malte0811 Date: Sat, 20 May 2017 18:50:04 +0200 Subject: [PATCH] Added a key ring, holds up to 10 keys by default texture might change, docs+recipe still to do --- .../malte0811/industrialWires/IWConfig.java | 2 + .../malte0811/industrialWires/Recipes.java | 8 +- .../crafting/RecipeKeyLock.java | 4 +- .../crafting/RecipeKeyRing.java | 157 ++++++++++++++++++ .../industrialWires/items/ItemKey.java | 59 ++++--- .../assets/industrialwires/lang/en_US.lang | 7 +- .../models/item/key/key_ring.json | 6 + .../textures/items/key_ring.png | Bin 0 -> 506 bytes 8 files changed, 215 insertions(+), 28 deletions(-) create mode 100644 src/main/java/malte0811/industrialWires/crafting/RecipeKeyRing.java create mode 100644 src/main/resources/assets/industrialwires/models/item/key/key_ring.json create mode 100644 src/main/resources/assets/industrialwires/textures/items/key_ring.png diff --git a/src/main/java/malte0811/industrialWires/IWConfig.java b/src/main/java/malte0811/industrialWires/IWConfig.java index d036129..b60d672 100644 --- a/src/main/java/malte0811/industrialWires/IWConfig.java +++ b/src/main/java/malte0811/industrialWires/IWConfig.java @@ -31,6 +31,8 @@ public class IWConfig { public static boolean enableConversion = true; public static MechConversion mech = new MechConversion(); + @Comment({"The highest number of keys that can be put on one key ring"}) + public static int maxKeysOnRing = 10; public static class MechConversion { @Comment({"The amount of EU that would be produced by an ideal converter from 1 IF (default: 0.25)"}) diff --git a/src/main/java/malte0811/industrialWires/Recipes.java b/src/main/java/malte0811/industrialWires/Recipes.java index 16bd066..c00e3f7 100644 --- a/src/main/java/malte0811/industrialWires/Recipes.java +++ b/src/main/java/malte0811/industrialWires/Recipes.java @@ -26,10 +26,7 @@ import blusunrize.immersiveengineering.common.blocks.stone.BlockTypes_StoneDecor import ic2.api.item.IC2Items; import malte0811.industrialWires.blocks.controlpanel.BlockTypes_Panel; import malte0811.industrialWires.controlpanel.PanelUtils; -import malte0811.industrialWires.crafting.RecipeCoilLength; -import malte0811.industrialWires.crafting.RecipeComponentCopy; -import malte0811.industrialWires.crafting.RecipeInitPC; -import malte0811.industrialWires.crafting.RecipeKeyLock; +import malte0811.industrialWires.crafting.*; import malte0811.industrialWires.wires.IC2Wiretype; import net.minecraft.init.Blocks; import net.minecraft.init.Items; @@ -91,11 +88,13 @@ public class Recipes { } private static void addCustomRecipes() { + RecipeSorter.register("industrialwires:key_ring", RecipeKeyRing.class, RecipeSorter.Category.SHAPELESS, "after:forge:shapelessore"); RecipeSorter.register("industrialwires:key_lock", RecipeKeyLock.class, RecipeSorter.Category.SHAPELESS, "after:forge:shapelessore"); RecipeSorter.register("industrialwires:cmp_copy", RecipeComponentCopy.class, RecipeSorter.Category.SHAPED, "after:forge:shapelessore"); RecipeSorter.register("industrialwires:coilLength", RecipeCoilLength.class, RecipeSorter.Category.SHAPELESS, "after:forge:shapelessore"); RecipeSorter.register("industrialwires:init_pc", RecipeInitPC.class, RecipeSorter.Category.SHAPED, "after:forge:shapedore"); GameRegistry.addRecipe(new RecipeKeyLock()); + GameRegistry.addRecipe(new RecipeKeyRing()); GameRegistry.addRecipe(new RecipeComponentCopy()); for (int i = 0; i < IC2Wiretype.IC2_TYPES.length; i++) { GameRegistry.addRecipe(new RecipeCoilLength(i)); @@ -103,6 +102,7 @@ public class Recipes { AssemblerHandler.registerRecipeAdapter(RecipeCoilLength.class, new AllRecipeAdapter<>()); AssemblerHandler.registerRecipeAdapter(RecipeComponentCopy.class, new AllRecipeAdapter<>()); AssemblerHandler.registerRecipeAdapter(RecipeKeyLock.class, new AllRecipeAdapter<>()); + AssemblerHandler.registerRecipeAdapter(RecipeKeyRing.class, new AllRecipeAdapter<>()); } private static void registerPanels() { // CONTROL PANELS diff --git a/src/main/java/malte0811/industrialWires/crafting/RecipeKeyLock.java b/src/main/java/malte0811/industrialWires/crafting/RecipeKeyLock.java index 2beac08..128475c 100644 --- a/src/main/java/malte0811/industrialWires/crafting/RecipeKeyLock.java +++ b/src/main/java/malte0811/industrialWires/crafting/RecipeKeyLock.java @@ -79,8 +79,8 @@ public class RecipeKeyLock implements IRecipe { boolean hasKey = false; for (int i = 0; i < inv.getSizeInventory(); i++) { ItemStack here = inv.getStackInSlot(i); - if (here!=null && here.getItem() == IndustrialWires.key) { - if (hasKey || ItemKey.idForKey(here) != 0) {//too many keys or non-blanks + if (here!=null && here.getItem() == IndustrialWires.key && here.getMetadata()==0) { + if (hasKey) {//too many keys return 0; } hasKey = true; diff --git a/src/main/java/malte0811/industrialWires/crafting/RecipeKeyRing.java b/src/main/java/malte0811/industrialWires/crafting/RecipeKeyRing.java new file mode 100644 index 0000000..ca49e78 --- /dev/null +++ b/src/main/java/malte0811/industrialWires/crafting/RecipeKeyRing.java @@ -0,0 +1,157 @@ +/* + * This file is part of Industrial Wires. + * Copyright (C) 2016-2017 malte0811 + * + * Industrial Wires is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Industrial Wires is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Industrial Wires. If not, see . + */ +package malte0811.industrialWires.crafting; + +import malte0811.industrialWires.IWConfig; +import malte0811.industrialWires.IndustrialWires; +import net.minecraft.inventory.InventoryCrafting; +import net.minecraft.item.ItemStack; +import net.minecraft.item.crafting.IRecipe; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; +import net.minecraft.world.World; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import static malte0811.industrialWires.items.ItemKey.*; + +public class RecipeKeyRing implements IRecipe { + + @Override + public boolean matches(@Nonnull InventoryCrafting inv, @Nonnull World worldIn) { + return getType(inv) != null; + } + + @Override + public ItemStack getCraftingResult(@Nonnull InventoryCrafting inv) { + Boolean recipeType = getType(inv); + if (recipeType==Boolean.TRUE) {//add key to ring + ItemStack ring = inv.getStackInSlot(getRingPos(inv)).copy(); + NBTTagCompound nbt = ring.getTagCompound(); + ItemStack key = inv.getStackInSlot(getKeyPos(inv)); + NBTTagCompound keyNBT = key.getTagCompound(); + if (nbt==null) { + nbt = new NBTTagCompound(); + ring.setTagCompound(nbt); + } + if (!nbt.hasKey(RING_KEYS)) { + nbt.setTag(RING_KEYS, new NBTTagList()); + } + if (keyNBT!=null) { + NBTTagList keys = nbt.getTagList(RING_KEYS, 10); + if (keys.tagCount()>= IWConfig.maxKeysOnRing) { + return null; + } + keys.appendTag(keyNBT.copy()); + nbt.setInteger(LOCK_ID, keyNBT.getInteger(LOCK_ID)); + nbt.setString(NAME, keyNBT.getString(NAME)); + } + return ring; + } else {//remove key from ring + ItemStack ring = inv.getStackInSlot(getRingPos(inv)).copy(); + NBTTagCompound nbt = ring.getTagCompound(); + ItemStack key = new ItemStack(IndustrialWires.key, 1, 1); + if (nbt!=null) { + NBTTagList keys = nbt.getTagList(RING_KEYS, 10); + if (keys.tagCount()>0) { + NBTTagCompound first = keys.getCompoundTagAt(keys.tagCount()-1); + key.setTagCompound(first); + return key; + } + } + } + return null; + } + + @Override + public int getRecipeSize() { + return 2; + } + + @Override + public ItemStack getRecipeOutput() { + return null; + } + + @Nonnull + @Override + public ItemStack[] getRemainingItems(@Nonnull InventoryCrafting inv) { + ItemStack[] ret = new ItemStack[inv.getSizeInventory()]; + if (getType(inv)==Boolean.FALSE) { + int ringId = getRingPos(inv); + ItemStack ring = inv.getStackInSlot(ringId).copy(); + NBTTagCompound nbt = ring.getTagCompound(); + if (nbt!=null) { + NBTTagList keys = nbt.getTagList(RING_KEYS, 10); + keys.removeTag(keys.tagCount()-1); + if (keys.tagCount() > 0) { + NBTTagCompound first = keys.getCompoundTagAt(0); + nbt.setInteger(LOCK_ID, first.getInteger(LOCK_ID)); + nbt.setString(NAME, first.getString(NAME)); + } else { + nbt.removeTag(LOCK_ID); + nbt.removeTag(NAME); + } + ret[ringId] = ring; + } + } + return ret; + } + + @Nullable + private Boolean getType(@Nonnull InventoryCrafting inv) { + boolean hasRing = false; + boolean hasKey = false; + for (int i = 0;i subItems) { + subItems.add(new ItemStack(this, 1, 0)); + subItems.add(new ItemStack(this, 1, 2)); + } + @Nonnull @Override public String getUnlocalizedName(ItemStack stack) { NBTTagCompound nbt = stack.getTagCompound(); - if (nbt==null||!nbt.hasKey(lockId)) { - return "item."+IndustrialWires.MODID+".key_raw"; - } - return super.getUnlocalizedName(stack); + return "item."+IndustrialWires.MODID+".key."+types[stack.getMetadata()]; } public static void setId(ItemStack stack, int lockID) { - stack.setTagInfo(lockId, new NBTTagInt(lockID)); + stack.setTagInfo(LOCK_ID, new NBTTagInt(lockID)); } public static int idForKey(@Nullable ItemStack held) { @@ -79,7 +84,7 @@ public class ItemKey extends Item implements INetGUIItem { } NBTTagCompound nbt = held.getTagCompound(); if (nbt!=null) { - return nbt.getInteger(lockId); + return nbt.getInteger(LOCK_ID); } return 0; } @@ -93,8 +98,24 @@ public class ItemKey extends Item implements INetGUIItem { @Override public ActionResult onItemRightClick(@Nonnull ItemStack stack, World worldIn, EntityPlayer playerIn, @Nonnull EnumHand hand) { ItemStack held = playerIn.getHeldItem(hand); - if (!worldIn.isRemote&&idForKey(held)!=0) { - playerIn.openGui(IndustrialWires.MODID, 1, worldIn, 0, 0, hand == EnumHand.MAIN_HAND ? 1 : 0); + if (!worldIn.isRemote) { + if (playerIn.isSneaking()&&held.getMetadata()==2) { + //select next key + NBTTagCompound nbt = held.getTagCompound(); + if (nbt!=null) { + NBTTagList allKeys = nbt.getTagList(RING_KEYS, 10); + if (allKeys.tagCount()>1) { + NBTTagCompound next = allKeys.getCompoundTagAt(0); + allKeys.removeTag(0); + allKeys.appendTag(next); + nbt.setInteger(LOCK_ID, next.getInteger(LOCK_ID)); + nbt.setString(NAME, next.getString(NAME)); + playerIn.inventory.markDirty(); + } + } + } else if (idForKey(held)!=0&&held.getMetadata()==1) { + playerIn.openGui(IndustrialWires.MODID, 1, worldIn, 0, 0, hand == EnumHand.MAIN_HAND ? 1 : 0); + } } return new ActionResult<>(EnumActionResult.SUCCESS, stack); } @@ -102,13 +123,13 @@ public class ItemKey extends Item implements INetGUIItem { @Override public void onChange(NBTTagCompound nbt, EntityPlayer p, EnumHand hand) { ItemStack held = p.getHeldItem(hand); - String name = nbt.getString("name"); + String name = nbt.getString(NAME); if (!name.trim().isEmpty()) { - held.setTagInfo("name", new NBTTagString(name)); + held.setTagInfo(NAME, new NBTTagString(name)); } else { NBTTagCompound heldNBT = held.getTagCompound(); if (heldNBT!=null) { - heldNBT.removeTag("name"); + heldNBT.removeTag(NAME); } } } diff --git a/src/main/resources/assets/industrialwires/lang/en_US.lang b/src/main/resources/assets/industrialwires/lang/en_US.lang index ae9ae5f..ec285fc 100644 --- a/src/main/resources/assets/industrialwires/lang/en_US.lang +++ b/src/main/resources/assets/industrialwires/lang/en_US.lang @@ -36,9 +36,10 @@ item.industrialwires.panel_component.variac.name=VariacĀ® item.industrialwires.panel_component.toggle_switch.name=Toggle Switch item.industrialwires.panel_component.toggle_switch_covered.name=Covered Toggle Switch item.industrialwires.panel_component.lock.name=Lock Switch -item.industrialwires.key.name=Key -item.industrialwires.key_named.name=Key for -item.industrialwires.key_raw.name=Blank Key +item.industrialwires.key.key.name=Key +item.industrialwires.key.key_named.name=Key for +item.industrialwires.key.blank_key.name=Blank Key +item.industrialwires.key.key_ring.name=Key Ring industrialwires.desc.wireLength=Wire length: %1s block(s) diff --git a/src/main/resources/assets/industrialwires/models/item/key/key_ring.json b/src/main/resources/assets/industrialwires/models/item/key/key_ring.json new file mode 100644 index 0000000..eb4ad1b --- /dev/null +++ b/src/main/resources/assets/industrialwires/models/item/key/key_ring.json @@ -0,0 +1,6 @@ +{ + "parent":"item/generated", + "textures": { + "layer0":"industrialwires:items/key_ring" + } +} \ No newline at end of file diff --git a/src/main/resources/assets/industrialwires/textures/items/key_ring.png b/src/main/resources/assets/industrialwires/textures/items/key_ring.png new file mode 100644 index 0000000000000000000000000000000000000000..0dca6b7bafbe4228c90092d73a882f4161a4369d GIT binary patch literal 506 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`Y)RhkEB2tPL$pH|d$ppa~4NJNQqer|4RUI~!Pz~Eeznpl#W zqEMb$lA+-4=^K!um&(q-z!>N0;uvCadg)}tzC#8AuJ>>FDP>DX`7z3}@|u5i)DVbu zadqtd;48rD5t3=iz$<&!jYV~HXxM4C8%esoHrw8q|Bsdn4E<+(=X`5N&2!g4n+fdA zjW(()wAOZ+>32EBzx~Vi&+3Qxkq_rTGMxAvv;DSiBCl_9}s z=7YWOLsq^1wUQ~bwu|HR)1c@RdD{<1br<*PoPJVbwbHO{)v5>`u}gsqR!vDg`LpI( zfki;5=;MzwE@m7lu(+~o-Ojl6{L`()_I!`X{&>%OlFFO!)qTg6T?0Fc`+ooZ_m4Mk zyYw*$Cyo_b(@(dW__8j@oYmg{O=Locpg8qQX{RJs!=yH_o5IiNoS~!8t)E sfd$HYFZ--ImA3ikx$CdLUVipdHoiHWnYq}hITsWKp00i_>zopr06#j%M*si- literal 0 HcmV?d00001