Skip to content

Commit

Permalink
Make /inv view work for offline players (#177)
Browse files Browse the repository at this point in the history
Signed-off-by: Lyfts <[email protected]>
  • Loading branch information
Lyfts authored Jan 17, 2025
1 parent ac730c9 commit b145fba
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 12 deletions.
43 changes: 36 additions & 7 deletions src/main/java/serverutils/command/CmdInv.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,25 @@
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.stream.Collectors;

import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraftforge.common.util.Constants;

import com.gtnewhorizon.gtnhlib.config.ConfigurationManager;

import serverutils.ServerUtilitiesConfig;
import serverutils.lib.command.CmdBase;
import serverutils.lib.command.CmdTreeBase;
import serverutils.lib.command.CmdTreeHelp;
import serverutils.lib.command.CommandUtils;
import serverutils.lib.data.ForgePlayer;
import serverutils.lib.data.Universe;

public class CmdInv extends CmdTreeBase {

Expand All @@ -27,21 +33,44 @@ public CmdView() {
}

@Override
public List<String> getCommandAliases() {
return Collections.singletonList("edit");
public List<String> addTabCompletionOptions(ICommandSender sender, String[] args) {
if (args.length == 1) {
return Universe.get().getPlayers().stream().map(ForgePlayer::getName).collect(Collectors.toList());
}
return super.addTabCompletionOptions(sender, args);
}

@Override
public boolean isUsernameIndex(String[] args, int index) {
return index == 0;
public List<String> getCommandAliases() {
return Collections.singletonList("edit");
}

@Override
public void processCommand(ICommandSender sender, String[] args) throws CommandException {
checkArgs(sender, args, 1);
EntityPlayerMP self = getCommandSenderAsPlayer(sender);
EntityPlayerMP other = CommandUtils.getForgePlayer(sender, args[0]).getCommandPlayer(sender);
self.displayGUIChest(new InvSeeInventory(other.inventory, other));
ForgePlayer other = Universe.get().getPlayer(args[0]);

if (other == null || other.isFake() || other.getPlayerNBT() == null) {
throw new CommandException("commands.generic.player.notFound", args[0]);
}

if (other.isOnline()) {
self.displayGUIChest(new InvSeeInventory(other.getPlayer().inventory, other.getPlayer()));
} else {
NBTTagCompound tag = other.getPlayerNBT();
InventoryPlayer playerInv = new InventoryPlayer(null);
playerInv.readFromNBT(tag.getTagList("Inventory", Constants.NBT.TAG_COMPOUND));
InvSeeInventory invSee = new InvSeeInventory(playerInv, null);
invSee.setSaveCallback(inv -> {
InventoryPlayer invPlayer = inv.getPlayerInv();
NBTTagList invTag = new NBTTagList();
invPlayer.writeToNBT(invTag);
tag.setTag("Inventory", invTag);
other.setPlayerNBT(tag);
});
self.displayGUIChest(invSee);
}
}
}

Expand Down
23 changes: 18 additions & 5 deletions src/main/java/serverutils/command/InvSeeInventory.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package serverutils.command;

import java.lang.reflect.Method;
import java.util.function.Consumer;

import javax.annotation.Nullable;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;

Expand All @@ -20,6 +22,8 @@ public class InvSeeInventory implements IInventory {
private final EntityPlayerMP player;
private final IInventory baubles;
private static Method getBaubles = null;
public boolean hasChanged;
private Consumer<InvSeeInventory> saveCallback;

public InvSeeInventory(IInventory inv, @Nullable EntityPlayerMP ep) {
inventory = inv;
Expand Down Expand Up @@ -81,7 +85,11 @@ public boolean hasCustomInventoryName() {
}

@Override
public void closeInventory() {}
public void closeInventory() {
if (hasChanged && saveCallback != null) {
saveCallback.accept(this);
}
}

@Override
public ItemStack getStackInSlotOnClosing(int index) {
Expand All @@ -105,6 +113,7 @@ public int getInventoryStackLimit() {

@Override
public void markDirty() {
hasChanged = true;
inventory.markDirty();

if (player != null) {
Expand All @@ -115,10 +124,6 @@ public void markDirty() {
}
}

public boolean isUsableByPlayer(EntityPlayer player) {
return true;
}

@Override
public boolean isItemValidForSlot(int index, ItemStack stack) {
int slot = slotMapping[index];
Expand All @@ -131,6 +136,14 @@ public void clear() {
InvUtils.clear(baubles);
}

public void setSaveCallback(Consumer<InvSeeInventory> callback) {
saveCallback = callback;
}

public InventoryPlayer getPlayerInv() {
return (InventoryPlayer) inventory;
}

public static IInventory getBaubles(EntityPlayer player) {
IInventory ot = null;

Expand Down

0 comments on commit b145fba

Please sign in to comment.