Skip to content
This repository has been archived by the owner on Jul 1, 2018. It is now read-only.

Commit

Permalink
Rewrite some of the pocket upgrade system
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
SquidDev committed Mar 2, 2017
1 parent 7ab4781 commit 8ce970e
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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;
Expand All @@ -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();
Expand Down
51 changes: 37 additions & 14 deletions src/main/java/org/squiddev/cctweaks/core/pocket/PocketRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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);
}

Expand All @@ -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) {
Expand All @@ -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)) {
Expand All @@ -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<IPocketUpgrade> getUpgrades() {
return upgrades.values();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@
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;
import net.minecraftforge.fml.relauncher.ReflectionHelper;
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;
Expand Down Expand Up @@ -54,11 +52,7 @@ public void register(@Nonnull IModRegistry registry) {
List<PocketUpgradeWrapper> 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
70 changes: 70 additions & 0 deletions src/main/java/org/squiddev/cctweaks/pocket/PocketModem.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8ce970e

Please sign in to comment.