1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-01-10 09:20:28 +00:00

Add get/setUpgrade to IPocketAccess

We have similar methods in ITurtleAccess, so makes sense for them to be
here too.
This commit is contained in:
Jonathan Coates 2024-07-26 09:51:09 +01:00
parent 7285c32d58
commit 6c8e64ffcd
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
3 changed files with 36 additions and 10 deletions

View File

@ -6,10 +6,12 @@ package dan200.computercraft.api.pocket;
import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.api.upgrades.UpgradeBase; import dan200.computercraft.api.upgrades.UpgradeBase;
import dan200.computercraft.api.upgrades.UpgradeData;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.Entity;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.ApiStatus;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.Map; import java.util.Map;
@ -17,6 +19,7 @@ import java.util.Map;
/** /**
* Wrapper class for pocket computers. * Wrapper class for pocket computers.
*/ */
@ApiStatus.NonExtendable
public interface IPocketAccess { public interface IPocketAccess {
/** /**
* Gets the entity holding this item. * Gets the entity holding this item.
@ -64,6 +67,26 @@ public interface IPocketAccess {
*/ */
void setLight(int colour); void setLight(int colour);
/**
* Get the currently equipped upgrade.
*
* @return The currently equipped upgrade.
* @see #getUpgradeNBTData()
* @see #setUpgrade(UpgradeData)
*/
@Nullable
UpgradeData<IPocketUpgrade> getUpgrade();
/**
* Set the upgrade for this pocket computer, also updating the item stack.
* <p>
* 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<IPocketUpgrade> upgrade);
/** /**
* Get the upgrade-specific NBT. * Get the upgrade-specific NBT.
* <p> * <p>
@ -73,6 +96,7 @@ public interface IPocketAccess {
* @see #updateUpgradeNBTData() * @see #updateUpgradeNBTData()
* @see UpgradeBase#getUpgradeItem(CompoundTag) * @see UpgradeBase#getUpgradeItem(CompoundTag)
* @see UpgradeBase#getUpgradeData(ItemStack) * @see UpgradeBase#getUpgradeData(ItemStack)
* @see #getUpgrade()
*/ */
CompoundTag getUpgradeNBTData(); CompoundTag getUpgradeNBTData();

View File

@ -6,10 +6,10 @@ package dan200.computercraft.shared.pocket.apis;
import dan200.computercraft.api.lua.ILuaAPI; import dan200.computercraft.api.lua.ILuaAPI;
import dan200.computercraft.api.lua.LuaFunction; import dan200.computercraft.api.lua.LuaFunction;
import dan200.computercraft.api.pocket.IPocketAccess;
import dan200.computercraft.api.pocket.IPocketUpgrade; import dan200.computercraft.api.pocket.IPocketUpgrade;
import dan200.computercraft.api.upgrades.UpgradeData; import dan200.computercraft.api.upgrades.UpgradeData;
import dan200.computercraft.impl.PocketUpgrades; import dan200.computercraft.impl.PocketUpgrades;
import dan200.computercraft.shared.pocket.core.PocketServerComputer;
import net.minecraft.core.NonNullList; import net.minecraft.core.NonNullList;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
@ -34,10 +34,10 @@ import java.util.Objects;
* @cc.module pocket * @cc.module pocket
*/ */
public class PocketAPI implements ILuaAPI { public class PocketAPI implements ILuaAPI {
private final PocketServerComputer computer; private final IPocketAccess pocket;
public PocketAPI(PocketServerComputer computer) { public PocketAPI(IPocketAccess pocket) {
this.computer = computer; this.pocket = pocket;
} }
@Override @Override
@ -56,10 +56,10 @@ public class PocketAPI implements ILuaAPI {
*/ */
@LuaFunction(mainThread = true) @LuaFunction(mainThread = true)
public final Object[] equipBack() { public final Object[] equipBack() {
var entity = computer.getEntity(); var entity = pocket.getEntity();
if (!(entity instanceof Player player)) return new Object[]{ false, "Cannot find player" }; if (!(entity instanceof Player player)) return new Object[]{ false, "Cannot find player" };
var inventory = player.getInventory(); 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 // 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. // 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()); if (previousUpgrade != null) storeItem(player, previousUpgrade.getUpgradeItem());
// Set the new upgrade // Set the new upgrade
computer.setUpgrade(newUpgrade); pocket.setUpgrade(newUpgrade);
return new Object[]{ true }; return new Object[]{ true };
} }
@ -87,13 +87,13 @@ public class PocketAPI implements ILuaAPI {
*/ */
@LuaFunction(mainThread = true) @LuaFunction(mainThread = true)
public final Object[] unequipBack() { public final Object[] unequipBack() {
var entity = computer.getEntity(); var entity = pocket.getEntity();
if (!(entity instanceof Player player)) return new Object[]{ false, "Cannot find player" }; 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" }; if (previousUpgrade == null) return new Object[]{ false, "Nothing to unequip" };
computer.setUpgrade(null); pocket.setUpgrade(null);
storeItem(player, previousUpgrade.getUpgradeItem()); storeItem(player, previousUpgrade.getUpgradeItem());

View File

@ -111,6 +111,7 @@ public class PocketServerComputer extends ServerComputer implements IPocketAcces
return upgrade == null ? Map.of() : Collections.singletonMap(upgrade.getUpgradeID(), getPeripheral(ComputerSide.BACK)); return upgrade == null ? Map.of() : Collections.singletonMap(upgrade.getUpgradeID(), getPeripheral(ComputerSide.BACK));
} }
@Override
public @Nullable UpgradeData<IPocketUpgrade> getUpgrade() { public @Nullable UpgradeData<IPocketUpgrade> getUpgrade() {
return upgrade == null ? null : UpgradeData.of(upgrade, getUpgradeNBTData()); 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}. * @param upgrade The new upgrade to set it to, may be {@code null}.
*/ */
@Override
public void setUpgrade(@Nullable UpgradeData<IPocketUpgrade> upgrade) { public void setUpgrade(@Nullable UpgradeData<IPocketUpgrade> upgrade) {
synchronized (this) { synchronized (this) {
PocketComputerItem.setUpgrade(stack, upgrade); PocketComputerItem.setUpgrade(stack, upgrade);