|
| 1 | +package xyz.xenondevs.inventoryaccess.r22; |
| 2 | + |
| 3 | +import net.minecraft.core.BlockPos; |
| 4 | +import net.minecraft.core.NonNullList; |
| 5 | +import net.minecraft.network.chat.Component; |
| 6 | +import net.minecraft.network.protocol.game.ClientboundContainerSetContentPacket; |
| 7 | +import net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket; |
| 8 | +import net.minecraft.network.protocol.game.ClientboundOpenScreenPacket; |
| 9 | +import net.minecraft.server.level.ServerPlayer; |
| 10 | +import net.minecraft.world.Container; |
| 11 | +import net.minecraft.world.entity.player.Player; |
| 12 | +import net.minecraft.world.inventory.AbstractContainerMenu; |
| 13 | +import net.minecraft.world.inventory.AnvilMenu; |
| 14 | +import net.minecraft.world.inventory.ContainerLevelAccess; |
| 15 | +import net.minecraft.world.inventory.MenuType; |
| 16 | +import net.minecraft.world.item.ItemStack; |
| 17 | +import org.bukkit.craftbukkit.v1_21_R3.entity.CraftPlayer; |
| 18 | +import org.bukkit.craftbukkit.v1_21_R3.event.CraftEventFactory; |
| 19 | +import org.bukkit.craftbukkit.v1_21_R3.inventory.CraftInventoryAnvil; |
| 20 | +import org.bukkit.craftbukkit.v1_21_R3.inventory.CraftItemStack; |
| 21 | +import org.bukkit.craftbukkit.v1_21_R3.inventory.view.CraftAnvilView; |
| 22 | +import org.bukkit.event.inventory.PrepareAnvilEvent; |
| 23 | +import org.bukkit.inventory.Inventory; |
| 24 | +import org.jetbrains.annotations.NotNull; |
| 25 | +import xyz.xenondevs.inventoryaccess.abstraction.inventory.AnvilInventory; |
| 26 | +import xyz.xenondevs.inventoryaccess.component.ComponentWrapper; |
| 27 | + |
| 28 | +import java.util.List; |
| 29 | +import java.util.function.Consumer; |
| 30 | + |
| 31 | +class AnvilInventoryImpl extends AnvilMenu implements AnvilInventory { |
| 32 | + |
| 33 | + private final List<Consumer<String>> renameHandlers; |
| 34 | + private final CraftAnvilView view; |
| 35 | + private final ServerPlayer player; |
| 36 | + |
| 37 | + private String text; |
| 38 | + private boolean open; |
| 39 | + |
| 40 | + public AnvilInventoryImpl(org.bukkit.entity.Player player, @NotNull ComponentWrapper title, List<Consumer<String>> renameHandlers) { |
| 41 | + this(((CraftPlayer) player).getHandle(), InventoryUtilsImpl.createNMSComponent(title), renameHandlers); |
| 42 | + } |
| 43 | + |
| 44 | + public AnvilInventoryImpl(ServerPlayer player, Component title, List<Consumer<String>> renameHandlers) { |
| 45 | + super(player.nextContainerCounter(), player.getInventory(), |
| 46 | + ContainerLevelAccess.create(player.level(), new BlockPos(0, 0, 0))); |
| 47 | + |
| 48 | + setTitle(title); |
| 49 | + this.renameHandlers = renameHandlers; |
| 50 | + this.player = player; |
| 51 | + |
| 52 | + CraftInventoryAnvil inventory = new CraftInventoryAnvil(access.getLocation(), inputSlots, resultSlots); |
| 53 | + this.view = new CraftAnvilView(player.getBukkitEntity(), inventory, this); |
| 54 | + } |
| 55 | + |
| 56 | + public void open() { |
| 57 | + open = true; |
| 58 | + |
| 59 | + // call the InventoryOpenEvent |
| 60 | + CraftEventFactory.callInventoryOpenEvent(player, this); |
| 61 | + |
| 62 | + // set active container |
| 63 | + player.containerMenu = this; |
| 64 | + |
| 65 | + // send open packet |
| 66 | + player.connection.send(new ClientboundOpenScreenPacket(containerId, MenuType.ANVIL, getTitle())); |
| 67 | + |
| 68 | + // send initial items |
| 69 | + NonNullList<ItemStack> itemsList = NonNullList.of(ItemStack.EMPTY, getItem(0), getItem(1), getItem(2)); |
| 70 | + player.connection.send(new ClientboundContainerSetContentPacket(getActiveWindowId(player), incrementStateId(), itemsList, ItemStack.EMPTY)); |
| 71 | + |
| 72 | + // init menu |
| 73 | + player.initMenu(this); |
| 74 | + } |
| 75 | + |
| 76 | + public void sendItem(int slot) { |
| 77 | + player.connection.send(new ClientboundContainerSetSlotPacket(getActiveWindowId(player), incrementStateId(), slot, getItem(slot))); |
| 78 | + } |
| 79 | + |
| 80 | + public void setItem(int slot, ItemStack item) { |
| 81 | + if (slot < 2) inputSlots.setItem(slot, item); |
| 82 | + else resultSlots.setItem(0, item); |
| 83 | + |
| 84 | + if (open) sendItem(slot); |
| 85 | + } |
| 86 | + |
| 87 | + private ItemStack getItem(int slot) { |
| 88 | + if (slot < 2) return inputSlots.getItem(slot); |
| 89 | + else return resultSlots.getItem(0); |
| 90 | + } |
| 91 | + |
| 92 | + private int getActiveWindowId(ServerPlayer player) { |
| 93 | + AbstractContainerMenu container = player.containerMenu; |
| 94 | + return container == null ? -1 : container.containerId; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void setItem(int slot, org.bukkit.inventory.ItemStack itemStack) { |
| 99 | + setItem(slot, CraftItemStack.asNMSCopy(itemStack)); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public @NotNull Inventory getBukkitInventory() { |
| 104 | + return view.getTopInventory(); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public String getRenameText() { |
| 109 | + return text; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public boolean isOpen() { |
| 114 | + return open; |
| 115 | + } |
| 116 | + |
| 117 | + // --- AnvilMenu --- |
| 118 | + |
| 119 | + @Override |
| 120 | + public CraftAnvilView getBukkitView() { |
| 121 | + return view; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Called every tick to see if the {@link Player} can still use that container. |
| 126 | + * (Used to for checking the distance between the {@link Player} and the container |
| 127 | + * and closing the window when the distance gets too big.) |
| 128 | + * |
| 129 | + * @param player The {@link Player} |
| 130 | + * @return If the {@link Player} can still use that container |
| 131 | + */ |
| 132 | + @Override |
| 133 | + public boolean stillValid(Player player) { |
| 134 | + return true; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Called when the rename text gets changed. |
| 139 | + * |
| 140 | + * @param s The new rename text |
| 141 | + */ |
| 142 | + @Override |
| 143 | + public boolean setItemName(String s) { |
| 144 | + // save rename text |
| 145 | + text = s; |
| 146 | + |
| 147 | + // call rename handlers |
| 148 | + if (renameHandlers != null) |
| 149 | + renameHandlers.forEach(handler -> handler.accept(s)); |
| 150 | + |
| 151 | + // the client expects the item to change to its new name and removes it from the inventory, so it needs to be sent again |
| 152 | + sendItem(2); |
| 153 | + |
| 154 | + return false; |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Called when the container is closed to give the items back. |
| 159 | + * |
| 160 | + * @param player The {@link Player} that closed this container |
| 161 | + */ |
| 162 | + @Override |
| 163 | + public void removed(Player player) { |
| 164 | + open = false; |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Called when the container gets closed to put items back into a players |
| 169 | + * inventory or drop them in the world. |
| 170 | + * |
| 171 | + * @param player The {@link Player} that closed this container |
| 172 | + * @param container The container |
| 173 | + */ |
| 174 | + @Override |
| 175 | + protected void clearContainer(Player player, Container container) { |
| 176 | + open = false; |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Called when both items in the {@link AnvilMenu#inputSlots} were set to create |
| 181 | + * the resulting product, calculate the level cost and call the {@link PrepareAnvilEvent}. |
| 182 | + */ |
| 183 | + @Override |
| 184 | + public void createResult() { |
| 185 | + // empty |
| 186 | + } |
| 187 | + |
| 188 | +} |
0 commit comments