From 8ce970ed66708e4cea905ffc0c2dec8c8b275f2d Mon Sep 17 00:00:00 2001 From: SquidDev Date: Thu, 2 Mar 2017 19:55:17 +0000 Subject: [PATCH] Rewrite some of the pocket upgrade system Wireless modems now have a special upgrade object. This still uses an upgrade id of 0 and is not registered for crafting. However we elusively use upgrade methods now, and so avoid some special handling for them. This means things like the PocketAPIExtensions class does not need special support for modems too. --- .../core/patch/ItemPocketComputer_Patch.java | 22 ------ .../core/pocket/CraftingPocketUpgrade.java | 4 +- .../core/pocket/PocketAPIExtensions.java | 22 +++--- .../cctweaks/core/pocket/PocketRegistry.java | 51 ++++++++++---- .../cctweaks/integration/jei/JeiCCTweaks.java | 10 +-- .../cctweaks/pocket/PocketEnderModem.java | 2 +- .../squiddev/cctweaks/pocket/PocketModem.java | 70 +++++++++++++++++++ .../cctweaks/pocket/PocketWirelessBridge.java | 2 +- 8 files changed, 122 insertions(+), 61 deletions(-) create mode 100644 src/main/java/org/squiddev/cctweaks/pocket/PocketModem.java diff --git a/src/main/java/org/squiddev/cctweaks/core/patch/ItemPocketComputer_Patch.java b/src/main/java/org/squiddev/cctweaks/core/patch/ItemPocketComputer_Patch.java index a2fd6560..ef0d7a0f 100644 --- a/src/main/java/org/squiddev/cctweaks/core/patch/ItemPocketComputer_Patch.java +++ b/src/main/java/org/squiddev/cctweaks/core/patch/ItemPocketComputer_Patch.java @@ -2,13 +2,10 @@ import com.google.common.base.Objects; import dan200.computercraft.ComputerCraft; -import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.shared.computer.core.ComputerFamily; import dan200.computercraft.shared.computer.core.ServerComputer; import dan200.computercraft.shared.pocket.items.ItemPocketComputer; -import dan200.computercraft.shared.pocket.peripherals.PocketModemPeripheral; import net.minecraft.entity.Entity; -import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; @@ -84,23 +81,6 @@ public void onUpdate(ItemStack stack, World world, Entity entity, int slotNum, b // Update pocket hooks PocketHooks.update(entity, stack, computer); - - IPeripheral peripheral = computer.getPeripheral(2); - if (peripheral instanceof PocketModemPeripheral) { - PocketModemPeripheral modem = (PocketModemPeripheral) peripheral; - if (entity instanceof EntityLivingBase) { - EntityLivingBase player = (EntityLivingBase) entity; - modem.setLocation(world, player.posX, player.posY + player.getEyeHeight(), player.posZ); - } else { - modem.setLocation(world, entity.posX, entity.posY, entity.posZ); - } - boolean modemLight = modem.isActive(); - NBTTagCompound modemNBT = computer.getUserData(); - if (modemNBT.getBoolean("modemLight") != modemLight) { - modemNBT.setBoolean("modemLight", modemLight); - computer.updateUserData(); - } - } } } else { createClientComputer(stack); @@ -138,8 +118,6 @@ private ServerComputer createServerComputer(World world, IInventory inventory, I computer.addAPI(new PocketAPIExtensions(computer)); PocketHooks.create(inventory, stack, computer); - - if (getHasModem(stack)) computer.setPeripheral(2, new PocketModemPeripheral(false)); ComputerCraft.serverComputerRegistry.add(instanceID, computer); if (inventory != null) inventory.markDirty(); diff --git a/src/main/java/org/squiddev/cctweaks/core/pocket/CraftingPocketUpgrade.java b/src/main/java/org/squiddev/cctweaks/core/pocket/CraftingPocketUpgrade.java index 6a02c541..6e9e7548 100644 --- a/src/main/java/org/squiddev/cctweaks/core/pocket/CraftingPocketUpgrade.java +++ b/src/main/java/org/squiddev/cctweaks/core/pocket/CraftingPocketUpgrade.java @@ -55,8 +55,8 @@ public ItemStack getCraftingResult(InventoryCrafting inventory) { pocket = pocket.copy(); if (!pocket.hasTagCompound()) pocket.setTagCompound(new NBTTagCompound()); NBTTagCompound tag = pocket.getTagCompound(); - tag.setShort("upgrade", PocketRegistry.FLAG); - tag.setString("upgrade_name", upgrade.getUpgradeID().toString()); + + PocketRegistry.instance.setToItemStack(pocket, upgrade); // Ensure a new computer is created tag.removeTag("instanceID"); diff --git a/src/main/java/org/squiddev/cctweaks/core/pocket/PocketAPIExtensions.java b/src/main/java/org/squiddev/cctweaks/core/pocket/PocketAPIExtensions.java index 503dc439..a92e8339 100644 --- a/src/main/java/org/squiddev/cctweaks/core/pocket/PocketAPIExtensions.java +++ b/src/main/java/org/squiddev/cctweaks/core/pocket/PocketAPIExtensions.java @@ -9,8 +9,8 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NBTTagCompound; import org.squiddev.cctweaks.api.pocket.IPocketUpgrade; +import org.squiddev.cctweaks.pocket.PocketModem; public class PocketAPIExtensions extends PocketAPI { private final ServerComputer computer; @@ -49,7 +49,12 @@ public Object[] callMethod(ILuaContext context, int method, Object[] arguments) for (int i = 0; i < size; i++) { ItemStack invStack = inventory.getStackInSlot((i + held) % size); if (invStack != null) { - newUpgrade = PocketRegistry.instance.getFromItemStack(invStack); + if (InventoryUtil.areItemsStackable(invStack, PocketModem.INSTANCE.getCraftingItem())) { + newUpgrade = PocketModem.INSTANCE; + } else { + newUpgrade = PocketRegistry.instance.getFromItemStack(invStack); + } + if (newUpgrade != null && newUpgrade != previousUpgrade) { // Consume an item from this stack and exit the loop invStack = invStack.copy(); @@ -75,12 +80,7 @@ public Object[] callMethod(ILuaContext context, int method, Object[] arguments) } // Set the new upgrade - NBTTagCompound tag = pocketStack.getTagCompound(); - if (tag == null) pocketStack.setTagCompound(tag = new NBTTagCompound()); - - tag.setShort("upgrade", PocketRegistry.FLAG); - tag.setString("upgrade_name", newUpgrade.getUpgradeID().toString()); - + PocketRegistry.instance.setToItemStack(pocketStack, newUpgrade); access.setUpgrade(newUpgrade, computer); return null; @@ -100,11 +100,7 @@ public Object[] callMethod(ILuaContext context, int method, Object[] arguments) if (previousUpgrade == null) throw new LuaException("Nothing to unequip"); - NBTTagCompound tag = pocketStack.getTagCompound(); - if (tag == null) pocketStack.setTagCompound(tag = new NBTTagCompound()); - - tag.removeTag("upgrade"); - tag.removeTag("upgrade_name"); + PocketRegistry.instance.setToItemStack(pocketStack, null); access.setUpgrade(null, computer); ItemStack stack = previousUpgrade.getCraftingItem(); diff --git a/src/main/java/org/squiddev/cctweaks/core/pocket/PocketRegistry.java b/src/main/java/org/squiddev/cctweaks/core/pocket/PocketRegistry.java index a79cc361..2c116b4f 100644 --- a/src/main/java/org/squiddev/cctweaks/core/pocket/PocketRegistry.java +++ b/src/main/java/org/squiddev/cctweaks/core/pocket/PocketRegistry.java @@ -8,14 +8,17 @@ import org.squiddev.cctweaks.api.pocket.IPocketRegistry; import org.squiddev.cctweaks.api.pocket.IPocketUpgrade; import org.squiddev.cctweaks.core.utils.DebugLogger; +import org.squiddev.cctweaks.pocket.PocketModem; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.util.Collection; import java.util.HashMap; import java.util.Map; public final class PocketRegistry implements IPocketRegistry { public static final short FLAG = 0xFF; + public static final short MODEM = 1; public static final PocketRegistry instance = new PocketRegistry(); @@ -50,8 +53,6 @@ public IPocketUpgrade getUpgrade(ItemStack stack, IInventory inventory) { if (!tag.hasKey("upgrade")) return null; int id = tag.getShort("upgrade"); - if (id == 0 || id == 1) return null; - return getUpgradeInternal(tag, id, inventory); } @@ -62,23 +63,27 @@ public String getUpgradeAdjective(ItemStack stack, IInventory inventory) { if (!tag.hasKey("upgrade")) return null; int id = tag.getShort("upgrade"); - if (id == 0) return null; - if (id == 1) return "upgrade.computercraft:wireless_modem.adjective"; - IPocketUpgrade upgrade = getUpgradeInternal(tag, id, inventory); return upgrade == null ? null : upgrade.getUnlocalisedAdjective(); } private IPocketUpgrade getUpgradeInternal(NBTTagCompound tag, int id, IInventory inventory) { IPocketUpgrade upgrade; - if (id == FLAG) { - upgrade = upgrades.get(tag.getString("upgrade_name")); - } else { - upgrade = legacyUpgrades.get(id); - if (upgrade != null) { - tag.setInteger("upgrade", FLAG); - tag.setString("upgrade_name", upgrade.getUpgradeID().toString()); - } + switch (id) { + case 0: + return null; + case MODEM: + return PocketModem.INSTANCE; + case FLAG: + upgrade = upgrades.get(tag.getString("upgrade_name")); + break; + default: + upgrade = legacyUpgrades.get(id); + if (upgrade != null) { + tag.setInteger("upgrade", FLAG); + tag.setString("upgrade_name", upgrade.getUpgradeID().toString()); + } + break; } if (upgrade == null) { @@ -94,7 +99,8 @@ private IPocketUpgrade getUpgradeInternal(NBTTagCompound tag, int id, IInventory return upgrade; } - public IPocketUpgrade getFromItemStack(ItemStack stack) { + @Nullable + public IPocketUpgrade getFromItemStack(@Nonnull ItemStack stack) { for (IPocketUpgrade entry : upgrades.values()) { ItemStack craftingStack = entry.getCraftingItem(); if (craftingStack != null && InventoryUtil.areItemsStackable(stack, craftingStack)) { @@ -105,6 +111,23 @@ public IPocketUpgrade getFromItemStack(ItemStack stack) { return null; } + public void setToItemStack(@Nonnull ItemStack stack, @Nullable IPocketUpgrade upgrade) { + NBTTagCompound tag = stack.getTagCompound(); + if (tag == null) stack.setTagCompound(tag = new NBTTagCompound()); + + tag.removeTag("upgrade_info"); + if (upgrade == null) { + tag.setInteger("upgrade", 0); + tag.removeTag("upgrade_name"); + } else if (upgrade == PocketModem.INSTANCE) { + tag.setInteger("upgrade", 1); + tag.removeTag("upgrade_name"); + } else { + tag.setInteger("upgrade", FLAG); + tag.setString("upgrade_name", upgrade.getUpgradeID().toString()); + } + } + public Collection getUpgrades() { return upgrades.values(); } diff --git a/src/main/java/org/squiddev/cctweaks/integration/jei/JeiCCTweaks.java b/src/main/java/org/squiddev/cctweaks/integration/jei/JeiCCTweaks.java index 8722a8ab..9a0d9179 100644 --- a/src/main/java/org/squiddev/cctweaks/integration/jei/JeiCCTweaks.java +++ b/src/main/java/org/squiddev/cctweaks/integration/jei/JeiCCTweaks.java @@ -4,9 +4,6 @@ import dan200.computercraft.ComputerCraft; import dan200.computercraft.api.turtle.ITurtleUpgrade; import dan200.computercraft.shared.computer.core.ComputerFamily; -import dan200.computercraft.shared.peripheral.PeripheralType; -import dan200.computercraft.shared.peripheral.common.PeripheralItemFactory; -import dan200.computercraft.shared.pocket.items.PocketComputerItemFactory; import dan200.computercraft.shared.proxy.CCTurtleProxyCommon; import mezz.jei.api.*; import net.minecraft.item.Item; @@ -14,6 +11,7 @@ import org.squiddev.cctweaks.api.pocket.IPocketUpgrade; import org.squiddev.cctweaks.core.pocket.PocketRegistry; import org.squiddev.cctweaks.core.registry.Registry; +import org.squiddev.cctweaks.pocket.PocketModem; import javax.annotation.Nonnull; import java.util.Collection; @@ -54,11 +52,7 @@ public void register(@Nonnull IModRegistry registry) { List pocketWrappers = Lists.newArrayListWithExpectedSize((pocketUpgrades.size() + 1) * PocketUpgradeWrapper.FAMILIES.length); for (ComputerFamily family : PocketUpgradeWrapper.FAMILIES) { // Hack to ensure the default modem is added too - pocketWrappers.add(new PocketUpgradeWrapper( - PocketComputerItemFactory.create(-1, null, family, false), - PeripheralItemFactory.create(PeripheralType.WirelessModem, null, 1), - PocketComputerItemFactory.create(-1, null, family, true) - )); + pocketWrappers.add(new PocketUpgradeWrapper(PocketModem.INSTANCE, family)); for (IPocketUpgrade upgrade : pocketUpgrades) { pocketWrappers.add(new PocketUpgradeWrapper(upgrade, family)); diff --git a/src/main/java/org/squiddev/cctweaks/pocket/PocketEnderModem.java b/src/main/java/org/squiddev/cctweaks/pocket/PocketEnderModem.java index 8a662217..cede444f 100644 --- a/src/main/java/org/squiddev/cctweaks/pocket/PocketEnderModem.java +++ b/src/main/java/org/squiddev/cctweaks/pocket/PocketEnderModem.java @@ -26,7 +26,7 @@ public class PocketEnderModem extends Module implements IPocketUpgrade { @Nonnull @Override public ResourceLocation getUpgradeID() { - return new ResourceLocation("cctweaks:advancedModem"); + return new ResourceLocation(CCTweaks.RESOURCE_DOMAIN, "advancedModem"); } @Nonnull diff --git a/src/main/java/org/squiddev/cctweaks/pocket/PocketModem.java b/src/main/java/org/squiddev/cctweaks/pocket/PocketModem.java new file mode 100644 index 00000000..6525c4ca --- /dev/null +++ b/src/main/java/org/squiddev/cctweaks/pocket/PocketModem.java @@ -0,0 +1,70 @@ +package org.squiddev.cctweaks.pocket; + +import dan200.computercraft.api.peripheral.IPeripheral; +import dan200.computercraft.shared.peripheral.PeripheralType; +import dan200.computercraft.shared.peripheral.common.PeripheralItemFactory; +import dan200.computercraft.shared.pocket.peripherals.PocketModemPeripheral; +import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.item.ItemStack; +import net.minecraft.util.ResourceLocation; +import net.minecraft.world.World; +import org.squiddev.cctweaks.CCTweaks; +import org.squiddev.cctweaks.api.pocket.IPocketAccess; +import org.squiddev.cctweaks.api.pocket.IPocketUpgrade; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +public class PocketModem implements IPocketUpgrade { + public static final PocketModem INSTANCE = new PocketModem(); + + private PocketModem() { + } + + @Nonnull + @Override + public ResourceLocation getUpgradeID() { + return new ResourceLocation(CCTweaks.RESOURCE_DOMAIN, "modem"); + } + + @Nonnull + @Override + public String getUnlocalisedAdjective() { + return "upgrade.computercraft:wireless_modem.adjective"; + } + + @Nullable + @Override + public ItemStack getCraftingItem() { + return PeripheralItemFactory.create(PeripheralType.WirelessModem, null, 1); + } + + @Nullable + @Override + public IPeripheral createPeripheral(@Nonnull IPocketAccess access) { + return new PocketModemPeripheral(false); + } + + @Override + public void update(@Nonnull IPocketAccess access, @Nullable IPeripheral peripheral) { + if (peripheral instanceof PocketModemPeripheral) { + Entity entity = access.getEntity(); + + PocketModemPeripheral modem = (PocketModemPeripheral) peripheral; + if (entity instanceof EntityLivingBase) { + EntityLivingBase player = (EntityLivingBase) entity; + modem.setLocation(entity.getEntityWorld(), player.posX, player.posY + player.getEyeHeight(), player.posZ); + } else if (entity != null) { + modem.setLocation(entity.getEntityWorld(), entity.posX, entity.posY, entity.posZ); + } + + access.setModemLight(modem.isActive()); + } + } + + @Override + public boolean onRightClick(@Nonnull World world, @Nonnull IPocketAccess access, @Nullable IPeripheral peripheral) { + return false; + } +} diff --git a/src/main/java/org/squiddev/cctweaks/pocket/PocketWirelessBridge.java b/src/main/java/org/squiddev/cctweaks/pocket/PocketWirelessBridge.java index be5a6a0f..3969b68a 100644 --- a/src/main/java/org/squiddev/cctweaks/pocket/PocketWirelessBridge.java +++ b/src/main/java/org/squiddev/cctweaks/pocket/PocketWirelessBridge.java @@ -33,7 +33,7 @@ public class PocketWirelessBridge extends Module implements IPocketUpgrade { @Nonnull @Override public ResourceLocation getUpgradeID() { - return new ResourceLocation("cctweaks:wirelessBridge"); + return new ResourceLocation(CCTweaks.RESOURCE_DOMAIN, "wirelessBridge"); } @Nonnull