Skip to content

Instantly share code, notes, and snippets.

@Random832
Created September 14, 2022 19:15
Show Gist options
  • Save Random832/47784e08c238cf1b1fa8e8023c014331 to your computer and use it in GitHub Desktop.
Save Random832/47784e08c238cf1b1fa8e8023c014331 to your computer and use it in GitHub Desktop.
fake chest menu base class
package random832.gui.menu;
import com.mojang.authlib.GameProfile;
import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.Util;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.Container;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.*;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.common.util.FakePlayer;
import net.minecraftforge.common.util.FakePlayerFactory;
import net.minecraftforge.server.ServerLifecycleHooks;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.function.BiFunction;
/**
* Helper class for building server-side menus that use the chest menu on the client.
*/
@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
public abstract class ServerSideChest extends AbstractContainerMenu {
final ChestMenu chestMenu;
final FakePlayer chestPlayer;
/**
* @param menuFactory e.g. {@link ChestMenu#threeRows(int, Inventory)}
*/
protected ServerSideChest(int containerId, BiFunction<Integer, Inventory, ChestMenu> menuFactory) {
super(null, containerId);
ServerLevel overworld = ServerLifecycleHooks.getCurrentServer().overworld();
chestPlayer = FakePlayerFactory.get(overworld, new GameProfile(Util.NIL_UUID, "FakeChestUser"));
chestMenu = menuFactory.apply(-42, chestPlayer.getInventory());
}
protected final void fillExtraSlots() {
int n = slots.size();
while (n < chestMenu.slots.size()) {
addSlot(new EmptySlot());
}
}
@Override
public final MenuType<?> getType() {
return chestMenu.getType();
}
@Override
public void clicked(int slotIndex, int eventButton, ClickType clickType, Player player) {
for(int i=0; i<chestMenu.slots.size(); i++) {
chestMenu.setItem(i, -42, remoteSlots.get(i));
}
chestMenu.setRemoteCarried(getCarried());
chestMenu.clicked(slotIndex, eventButton, clickType, chestPlayer);
super.clicked(slotIndex, eventButton, clickType, player);
for (Slot slot : chestMenu.slots) {
setRemoteSlotNoCopy(slot.index, slot.getItem());
}
setRemoteCarried(chestMenu.getCarried());
}
private static class EmptySlot extends Slot {
private static final Container EMPTY_CONTAINER = new SimpleContainer(0);
public EmptySlot() {
super(EMPTY_CONTAINER, 0, 0, 0);
}
@Override
public ItemStack getItem() {
return ItemStack.EMPTY;
}
@Override
public boolean isSameInventory(Slot other) {
return other instanceof EmptySlot;
}
@Override
public boolean allowModification(Player p_150652_) {
return false;
}
@Override
public boolean mayPlace(ItemStack pStack) {
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment