diff --git a/projects/common-api/src/main/java/dan200/computercraft/api/pocket/IPocketAccess.java b/projects/common-api/src/main/java/dan200/computercraft/api/pocket/IPocketAccess.java index 99ac916b9..cf16a836e 100644 --- a/projects/common-api/src/main/java/dan200/computercraft/api/pocket/IPocketAccess.java +++ b/projects/common-api/src/main/java/dan200/computercraft/api/pocket/IPocketAccess.java @@ -6,10 +6,12 @@ package dan200.computercraft.api.pocket; import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.api.upgrades.UpgradeBase; +import dan200.computercraft.api.upgrades.UpgradeData; import net.minecraft.nbt.CompoundTag; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.entity.Entity; import net.minecraft.world.item.ItemStack; +import org.jetbrains.annotations.ApiStatus; import javax.annotation.Nullable; import java.util.Map; @@ -17,6 +19,7 @@ import java.util.Map; /** * Wrapper class for pocket computers. */ +@ApiStatus.NonExtendable public interface IPocketAccess { /** * Gets the entity holding this item. @@ -64,6 +67,26 @@ public interface IPocketAccess { */ void setLight(int colour); + /** + * Get the currently equipped upgrade. + * + * @return The currently equipped upgrade. + * @see #getUpgradeNBTData() + * @see #setUpgrade(UpgradeData) + */ + @Nullable + UpgradeData getUpgrade(); + + /** + * Set the upgrade for this pocket computer, also updating the item stack. + *

+ * Note this method is not thread safe - it must be called from the server thread. + * + * @param upgrade The new upgrade to set it to, may be {@code null}. + * @see #getUpgrade() + */ + void setUpgrade(@Nullable UpgradeData upgrade); + /** * Get the upgrade-specific NBT. *

@@ -73,6 +96,7 @@ public interface IPocketAccess { * @see #updateUpgradeNBTData() * @see UpgradeBase#getUpgradeItem(CompoundTag) * @see UpgradeBase#getUpgradeData(ItemStack) + * @see #getUpgrade() */ CompoundTag getUpgradeNBTData(); diff --git a/projects/common/src/main/java/dan200/computercraft/shared/pocket/apis/PocketAPI.java b/projects/common/src/main/java/dan200/computercraft/shared/pocket/apis/PocketAPI.java index a0749450d..67b171c37 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/pocket/apis/PocketAPI.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/pocket/apis/PocketAPI.java @@ -6,10 +6,10 @@ package dan200.computercraft.shared.pocket.apis; import dan200.computercraft.api.lua.ILuaAPI; import dan200.computercraft.api.lua.LuaFunction; +import dan200.computercraft.api.pocket.IPocketAccess; import dan200.computercraft.api.pocket.IPocketUpgrade; import dan200.computercraft.api.upgrades.UpgradeData; import dan200.computercraft.impl.PocketUpgrades; -import dan200.computercraft.shared.pocket.core.PocketServerComputer; import net.minecraft.core.NonNullList; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; @@ -34,10 +34,10 @@ import java.util.Objects; * @cc.module pocket */ public class PocketAPI implements ILuaAPI { - private final PocketServerComputer computer; + private final IPocketAccess pocket; - public PocketAPI(PocketServerComputer computer) { - this.computer = computer; + public PocketAPI(IPocketAccess pocket) { + this.pocket = pocket; } @Override @@ -56,10 +56,10 @@ public class PocketAPI implements ILuaAPI { */ @LuaFunction(mainThread = true) public final Object[] equipBack() { - var entity = computer.getEntity(); + var entity = pocket.getEntity(); if (!(entity instanceof Player player)) return new Object[]{ false, "Cannot find player" }; var inventory = player.getInventory(); - var previousUpgrade = computer.getUpgrade(); + var previousUpgrade = pocket.getUpgrade(); // Attempt to find the upgrade, starting in the main segment, and then looking in the opposite // one. We start from the position the item is currently in and loop round to the start. @@ -73,7 +73,7 @@ public class PocketAPI implements ILuaAPI { if (previousUpgrade != null) storeItem(player, previousUpgrade.getUpgradeItem()); // Set the new upgrade - computer.setUpgrade(newUpgrade); + pocket.setUpgrade(newUpgrade); return new Object[]{ true }; } @@ -87,13 +87,13 @@ public class PocketAPI implements ILuaAPI { */ @LuaFunction(mainThread = true) public final Object[] unequipBack() { - var entity = computer.getEntity(); + var entity = pocket.getEntity(); if (!(entity instanceof Player player)) return new Object[]{ false, "Cannot find player" }; - var previousUpgrade = computer.getUpgrade(); + var previousUpgrade = pocket.getUpgrade(); if (previousUpgrade == null) return new Object[]{ false, "Nothing to unequip" }; - computer.setUpgrade(null); + pocket.setUpgrade(null); storeItem(player, previousUpgrade.getUpgradeItem()); diff --git a/projects/common/src/main/java/dan200/computercraft/shared/pocket/core/PocketServerComputer.java b/projects/common/src/main/java/dan200/computercraft/shared/pocket/core/PocketServerComputer.java index 90c6127e5..7231e1c15 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/pocket/core/PocketServerComputer.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/pocket/core/PocketServerComputer.java @@ -111,6 +111,7 @@ public class PocketServerComputer extends ServerComputer implements IPocketAcces return upgrade == null ? Map.of() : Collections.singletonMap(upgrade.getUpgradeID(), getPeripheral(ComputerSide.BACK)); } + @Override public @Nullable UpgradeData getUpgrade() { return upgrade == null ? null : UpgradeData.of(upgrade, getUpgradeNBTData()); } @@ -122,6 +123,7 @@ public class PocketServerComputer extends ServerComputer implements IPocketAcces * * @param upgrade The new upgrade to set it to, may be {@code null}. */ + @Override public void setUpgrade(@Nullable UpgradeData upgrade) { synchronized (this) { PocketComputerItem.setUpgrade(stack, upgrade);