Added a key ring, holds up to 10 keys by default

texture might change, docs+recipe still to do
This commit is contained in:
malte0811 2017-05-20 18:50:04 +02:00
parent 9ba2dcb0b6
commit 0dd80c3e56
8 changed files with 215 additions and 28 deletions

View file

@ -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)"})

View file

@ -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

View file

@ -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;

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
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<inv.getSizeInventory();i++) {
ItemStack here = inv.getStackInSlot(i);
if (here!=null&&here.getItem()==IndustrialWires.key) {
if (here.getMetadata()==1 && !hasKey) {//key
hasKey = true;
continue;
} else if (here.getMetadata()==2 && !hasRing) {//ring
hasRing = true;
continue;
}
return null;
} else if (here!=null) {
return null;
}
}
return hasRing?hasKey:null;
}
private int getRingPos(@Nonnull InventoryCrafting inv) {
for (int i = 0;i<inv.getSizeInventory();i++) {
ItemStack here = inv.getStackInSlot(i);
if (here!=null&& here.getItem()==IndustrialWires.key&&here.getMetadata()==2) {
return i;
}
}
return -1;
}
private int getKeyPos(@Nonnull InventoryCrafting inv) {
for (int i = 0;i<inv.getSizeInventory();i++) {
ItemStack here = inv.getStackInSlot(i);
if (here!=null && here.getItem()==IndustrialWires.key&&here.getMetadata()==1) {
return i;
}
}
return -1;
}
}

View file

@ -20,25 +20,27 @@ package malte0811.industrialWires.items;
import malte0811.industrialWires.IndustrialWires;
import net.minecraft.client.resources.I18n;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagInt;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.nbt.NBTTagString;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.*;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.registry.GameRegistry;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;
public class ItemKey extends Item implements INetGUIItem {
private static final String lockId = "lockId";
public static final String[] types = {"blank_key", "key"};
public static final String LOCK_ID = "lockId";
public static final String RING_KEYS = "ringkeys";
public static final String NAME = "name";
public static final String[] types = {"blank_key", "key", "key_ring"};
public ItemKey() {
setUnlocalizedName(IndustrialWires.MODID + ".key");
@ -53,24 +55,27 @@ public class ItemKey extends Item implements INetGUIItem {
@Override
public String getItemStackDisplayName(@Nonnull ItemStack stack) {
NBTTagCompound nbt = stack.getTagCompound();
if (nbt!=null&&nbt.hasKey("name")) {
return I18n.format("item."+IndustrialWires.MODID+".key_named.name")+" "+nbt.getString("name");
if (nbt!=null&&nbt.hasKey(NAME)&&!nbt.getString(NAME).trim().isEmpty()) {
return I18n.format("item."+IndustrialWires.MODID+".key.key_named.name")+" "+nbt.getString(NAME);
}
return super.getItemStackDisplayName(stack);
}
@Override
public void getSubItems(@Nonnull Item itemIn, CreativeTabs tab, List<ItemStack> 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<ItemStack> 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);
}
}
}

View file

@ -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)

View file

@ -0,0 +1,6 @@
{
"parent":"item/generated",
"textures": {
"layer0":"industrialwires:items/key_ring"
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 506 B