diff --git a/internal/v1_21_R1/pom.xml b/internal/v1_21_R1/pom.xml new file mode 100644 index 00000000..165be1a2 --- /dev/null +++ b/internal/v1_21_R1/pom.xml @@ -0,0 +1,81 @@ + + + + + 4.0.0 + + + openinvparent + com.lishid + ../../pom.xml + 4.4.6-SNAPSHOT + + + openinvadapter1_21_R1 + OpenInvAdapter1_21_R1 + + + 21 + 21 + 1.21-R0.1-SNAPSHOT + + + + + org.spigotmc + spigot-api + ${spigot.version} + + + spigot + org.spigotmc + provided + ${spigot.version} + remapped-mojang + + + openinvapi + com.lishid + provided + + + openinvplugincore + com.lishid + + + annotations + org.jetbrains + + + + + + + maven-shade-plugin + + + maven-compiler-plugin + + + net.md-5 + specialsource-maven-plugin + + + + + diff --git a/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/AnySilentContainer.java b/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/AnySilentContainer.java new file mode 100644 index 00000000..d03f25aa --- /dev/null +++ b/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/AnySilentContainer.java @@ -0,0 +1,207 @@ +/* + * Copyright (C) 2011-2023 lishid. All rights reserved. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.lishid.openinv.internal.v1_21_R1; + +import com.lishid.openinv.OpenInv; +import com.lishid.openinv.internal.IAnySilentContainer; +import com.lishid.openinv.util.ReflectionHelper; +import net.minecraft.core.BlockPos; +import net.minecraft.network.chat.Component; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.server.level.ServerPlayerGameMode; +import net.minecraft.world.MenuProvider; +import net.minecraft.world.SimpleMenuProvider; +import net.minecraft.world.inventory.ChestMenu; +import net.minecraft.world.inventory.MenuType; +import net.minecraft.world.inventory.PlayerEnderChestContainer; +import net.minecraft.world.level.GameType; +import net.minecraft.world.level.block.BarrelBlock; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.ChestBlock; +import net.minecraft.world.level.block.ShulkerBoxBlock; +import net.minecraft.world.level.block.TrappedChestBlock; +import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraft.world.level.block.entity.EnderChestBlockEntity; +import net.minecraft.world.level.block.entity.RandomizableContainerBlockEntity; +import net.minecraft.world.level.block.state.BlockState; +import org.bukkit.GameMode; +import org.bukkit.Material; +import org.bukkit.Statistic; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.lang.reflect.Field; +import java.util.logging.Logger; + +public class AnySilentContainer implements IAnySilentContainer { + + private @Nullable Field serverPlayerGameModeGameType; + + public AnySilentContainer() { + try { + try { + this.serverPlayerGameModeGameType = ServerPlayerGameMode.class.getDeclaredField("b"); + this.serverPlayerGameModeGameType.setAccessible(true); + } catch (NoSuchFieldException e) { + Logger logger = OpenInv.getPlugin(OpenInv.class).getLogger(); + logger.warning("ServerPlayerGameMode#gameModeForPlayer's obfuscated name has changed!"); + logger.warning("Please report this at https://github.com/Jikoo/OpenInv/issues"); + logger.warning("Attempting to fall through using reflection. Please verify that SilentContainer does not fail."); + // N.B. gameModeForPlayer is (for now) declared before previousGameModeForPlayer so silent shouldn't break. + this.serverPlayerGameModeGameType = ReflectionHelper.grabFieldByType(ServerPlayerGameMode.class, GameType.class); + } + } catch (SecurityException e) { + Logger logger = OpenInv.getPlugin(OpenInv.class).getLogger(); + logger.warning("Unable to directly write player game mode! SilentContainer will fail."); + logger.log(java.util.logging.Level.WARNING, "Error obtaining GameType field", e); + } + } + + @Override + public boolean activateContainer( + @NotNull final Player bukkitPlayer, + final boolean silentchest, + @NotNull final org.bukkit.block.Block bukkitBlock) { + + // Silent ender chest is API-only + if (silentchest && bukkitBlock.getType() == Material.ENDER_CHEST) { + bukkitPlayer.openInventory(bukkitPlayer.getEnderChest()); + bukkitPlayer.incrementStatistic(Statistic.ENDERCHEST_OPENED); + return true; + } + + ServerPlayer player = PlayerDataManager.getHandle(bukkitPlayer); + + final net.minecraft.world.level.Level level = player.level(); + final BlockPos blockPos = new BlockPos(bukkitBlock.getX(), bukkitBlock.getY(), bukkitBlock.getZ()); + final BlockEntity blockEntity = level.getBlockEntity(blockPos); + + if (blockEntity == null) { + return false; + } + + if (blockEntity instanceof EnderChestBlockEntity enderChestTile) { + // Anychest ender chest. See net.minecraft.world.level.block.EnderChestBlock + PlayerEnderChestContainer enderChest = player.getEnderChestInventory(); + enderChest.setActiveChest(enderChestTile); + player.openMenu(new SimpleMenuProvider((containerCounter, playerInventory, ignored) -> { + MenuType containers = PlayerDataManager.getContainers(enderChest.getContainerSize()); + int rows = enderChest.getContainerSize() / 9; + return new ChestMenu(containers, containerCounter, playerInventory, enderChest, rows); + }, Component.translatable(("container.enderchest")))); + bukkitPlayer.incrementStatistic(Statistic.ENDERCHEST_OPENED); + return true; + } + + if (!(blockEntity instanceof MenuProvider menuProvider)) { + return false; + } + + BlockState blockState = level.getBlockState(blockPos); + Block block = blockState.getBlock(); + + if (block instanceof ChestBlock chestBlock) { + + // boolean flag: do not check if chest is blocked + menuProvider = chestBlock.getMenuProvider(blockState, level, blockPos, true); + + if (menuProvider == null) { + OpenInv.getPlugin(OpenInv.class).sendSystemMessage(bukkitPlayer, "messages.error.lootNotGenerated"); + return false; + } + + if (block instanceof TrappedChestBlock) { + bukkitPlayer.incrementStatistic(Statistic.TRAPPED_CHEST_TRIGGERED); + } else { + bukkitPlayer.incrementStatistic(Statistic.CHEST_OPENED); + } + } + + if (block instanceof ShulkerBoxBlock) { + bukkitPlayer.incrementStatistic(Statistic.SHULKER_BOX_OPENED); + } + + if (block instanceof BarrelBlock) { + bukkitPlayer.incrementStatistic(Statistic.OPEN_BARREL); + } + + // AnyChest only - SilentChest not active, container unsupported, or unnecessary. + if (!silentchest || player.gameMode.getGameModeForPlayer() == GameType.SPECTATOR) { + player.openMenu(menuProvider); + return true; + } + + // SilentChest requires access to setting players' game mode directly. + if (this.serverPlayerGameModeGameType == null) { + return false; + } + + if (blockEntity instanceof RandomizableContainerBlockEntity lootable) { + if (lootable.lootTable != null) { + OpenInv.getPlugin(OpenInv.class).sendSystemMessage(bukkitPlayer, "messages.error.lootNotGenerated"); + return false; + } + } + + GameType gameType = player.gameMode.getGameModeForPlayer(); + this.forceGameType(player, GameType.SPECTATOR); + player.openMenu(menuProvider); + this.forceGameType(player, gameType); + return true; + } + + @Override + public void deactivateContainer(@NotNull final Player bukkitPlayer) { + if (this.serverPlayerGameModeGameType == null || bukkitPlayer.getGameMode() == GameMode.SPECTATOR) { + return; + } + + ServerPlayer player = PlayerDataManager.getHandle(bukkitPlayer); + + // Force game mode change without informing plugins or players. + // Regular game mode set calls GameModeChangeEvent and is cancellable. + GameType gameType = player.gameMode.getGameModeForPlayer(); + this.forceGameType(player, GameType.SPECTATOR); + + // ServerPlayer#closeContainer cannot be called without entering an + // infinite loop because this method is called during inventory close. + // From ServerPlayer#closeContainer -> CraftEventFactory#handleInventoryCloseEvent + player.containerMenu.transferTo(player.inventoryMenu, player.getBukkitEntity()); + // From ServerPlayer#closeContainer + player.doCloseContainer(); + // Regular inventory close will handle the rest - packet sending, etc. + + // Revert forced game mode. + this.forceGameType(player, gameType); + } + + private void forceGameType(final ServerPlayer player, final GameType gameMode) { + if (this.serverPlayerGameModeGameType == null) { + // No need to warn repeatedly, error on startup and lack of function should be enough. + return; + } + try { + this.serverPlayerGameModeGameType.setAccessible(true); + this.serverPlayerGameModeGameType.set(player.gameMode, gameMode); + } catch (IllegalArgumentException | IllegalAccessException e) { + Logger logger = OpenInv.getPlugin(OpenInv.class).getLogger(); + logger.log(java.util.logging.Level.WARNING, "Error bypassing GameModeChangeEvent", e); + } + } + +} diff --git a/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/OpenPlayer.java b/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/OpenPlayer.java new file mode 100644 index 00000000..6a10ada3 --- /dev/null +++ b/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/OpenPlayer.java @@ -0,0 +1,185 @@ +/* + * Copyright (C) 2011-2023 lishid. All rights reserved. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.lishid.openinv.internal.v1_21_R1; + +import com.mojang.logging.LogUtils; +import net.minecraft.Util; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.NbtIo; +import net.minecraft.nbt.NumericTag; +import net.minecraft.nbt.Tag; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.level.storage.PlayerDataStorage; +import org.bukkit.craftbukkit.v1_21_R1.CraftServer; +import org.bukkit.craftbukkit.v1_21_R1.entity.CraftPlayer; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Set; + +public class OpenPlayer extends CraftPlayer { + + /** + * List of tags to always reset when saving. + * + * @see net.minecraft.world.entity.Entity#saveWithoutId(CompoundTag) + * @see net.minecraft.server.level.ServerPlayer#addAdditionalSaveData(CompoundTag) + * @see net.minecraft.world.entity.player.Player#addAdditionalSaveData(CompoundTag) + * @see net.minecraft.world.entity.LivingEntity#addAdditionalSaveData(CompoundTag) + */ + private static final Set RESET_TAGS = Set.of( + // Entity#saveWithoutId(CompoundTag) + "CustomName", + "CustomNameVisible", + "Silent", + "NoGravity", + "Glowing", + "TicksFrozen", + "HasVisualFire", + "Tags", + "Passengers", + // ServerPlayer#addAdditionalSaveData(CompoundTag) + // Intentional omissions to prevent mount loss: Attach, Entity, and RootVehicle + "warden_spawn_tracker", + "enteredNetherPosition", + "SpawnX", + "SpawnY", + "SpawnZ", + "SpawnForced", + "SpawnAngle", + "SpawnDimension", + "raid_omen_position", + // Player#addAdditionalSaveData(CompoundTag) + "ShoulderEntityLeft", + "ShoulderEntityRight", + "LastDeathLocation", + "current_explosion_impact_pos", + // LivingEntity#addAdditionalSaveData(CompoundTag) + "active_effects", + "SleepingX", + "SleepingY", + "SleepingZ", + "Brain" + ); + + public OpenPlayer(CraftServer server, ServerPlayer entity) { + super(server, entity); + } + + @Override + public void loadData() { + PlayerDataManager.loadData(getHandle()); + } + + @Override + public void saveData() { + ServerPlayer player = this.getHandle(); + // See net.minecraft.world.level.storage.PlayerDataStorage#save(EntityHuman) + try { + PlayerDataStorage worldNBTStorage = player.server.getPlayerList().playerIo; + + CompoundTag oldData = isOnline() ? null : worldNBTStorage.load(player.getName().getString(), player.getStringUUID()).orElse(null); + CompoundTag playerData = getWritableTag(oldData); + playerData = player.saveWithoutId(playerData); + setExtraData(playerData); + + if (oldData != null) { + // Revert certain special data values when offline. + revertSpecialValues(playerData, oldData); + } + + Path playerDataDir = worldNBTStorage.getPlayerDir().toPath(); + Path tempFile = Files.createTempFile(playerDataDir, player.getStringUUID() + "-", ".dat"); + NbtIo.writeCompressed(playerData, tempFile); + Path dataFile = playerDataDir.resolve(player.getStringUUID() + ".dat"); + Path backupFile = playerDataDir.resolve(player.getStringUUID() + ".dat_old"); + Util.safeReplaceFile(dataFile, tempFile, backupFile); + } catch (Exception e) { + LogUtils.getLogger().warn("Failed to save player data for {}: {}", player.getScoreboardName(), e); + } + } + + @Contract("null -> new") + private @NotNull CompoundTag getWritableTag(@Nullable CompoundTag oldData) { + if (oldData == null) { + return new CompoundTag(); + } + + // Copy old data. This is a deep clone, so operating on it should be safe. + oldData = oldData.copy(); + + // Remove vanilla/server data that is not written every time. + oldData.getAllKeys() + .removeIf(key -> RESET_TAGS.contains(key) || key.startsWith("Bukkit")); + + return oldData; + } + + private void revertSpecialValues(@NotNull CompoundTag newData, @NotNull CompoundTag oldData) { + // Revert automatic updates to play timestamps. + copyValue(oldData, newData, "bukkit", "lastPlayed", NumericTag.class); + copyValue(oldData, newData, "Paper", "LastSeen", NumericTag.class); + copyValue(oldData, newData, "Paper", "LastLogin", NumericTag.class); + } + + private void copyValue( + @NotNull CompoundTag source, + @NotNull CompoundTag target, + @NotNull String container, + @NotNull String key, + @NotNull Class tagType) { + CompoundTag oldContainer = getTag(source, container, CompoundTag.class); + CompoundTag newContainer = getTag(target, container, CompoundTag.class); + + // New container being null means the server implementation doesn't store this data. + if (newContainer == null) { + return; + } + + // If old tag exists, copy it to new location, removing otherwise. + setTag(newContainer, key, getTag(oldContainer, key, tagType)); + } + + private @Nullable T getTag( + @Nullable CompoundTag container, + @NotNull String key, + @NotNull Class dataType) { + if (container == null) { + return null; + } + Tag value = container.get(key); + if (value == null || !dataType.isAssignableFrom(value.getClass())) { + return null; + } + return dataType.cast(value); + } + + private void setTag( + @NotNull CompoundTag container, + @NotNull String key, + @Nullable T data) { + if (data == null) { + container.remove(key); + } else { + container.put(key, data); + } + } + +} diff --git a/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/PlayerDataManager.java b/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/PlayerDataManager.java new file mode 100644 index 00000000..a66f3ddf --- /dev/null +++ b/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/PlayerDataManager.java @@ -0,0 +1,332 @@ +/* + * Copyright (C) 2011-2023 lishid. All rights reserved. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.lishid.openinv.internal.v1_21_R1; + +import com.lishid.openinv.OpenInv; +import com.lishid.openinv.internal.IPlayerDataManager; +import com.lishid.openinv.internal.ISpecialInventory; +import com.lishid.openinv.internal.OpenInventoryView; +import com.mojang.authlib.GameProfile; +import com.mojang.serialization.Dynamic; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.NbtOps; +import net.minecraft.network.chat.Component; +import net.minecraft.network.protocol.game.ClientboundOpenScreenPacket; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.level.ClientInformation; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.player.ChatVisiblity; +import net.minecraft.world.inventory.AbstractContainerMenu; +import net.minecraft.world.inventory.MenuType; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.dimension.DimensionType; +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; +import org.bukkit.Server; +import org.bukkit.World; +import org.bukkit.craftbukkit.v1_21_R1.CraftServer; +import org.bukkit.craftbukkit.v1_21_R1.CraftWorld; +import org.bukkit.craftbukkit.v1_21_R1.entity.CraftPlayer; +import org.bukkit.craftbukkit.v1_21_R1.event.CraftEventFactory; +import org.bukkit.craftbukkit.v1_21_R1.inventory.CraftContainer; +import org.bukkit.entity.Player; +import org.bukkit.inventory.InventoryView; +import org.bukkit.plugin.java.JavaPlugin; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.lang.reflect.Field; +import java.util.UUID; +import java.util.logging.Logger; + +public class PlayerDataManager implements IPlayerDataManager { + + private static boolean paper; + + static { + try { + Class.forName("io.papermc.paper.configuration.Configuration"); + paper = true; + } catch (ClassNotFoundException ignored) { + paper = false; + } + } + + private @Nullable Field bukkitEntity; + + public PlayerDataManager() { + try { + bukkitEntity = Entity.class.getDeclaredField("bukkitEntity"); + } catch (NoSuchFieldException e) { + Logger logger = JavaPlugin.getPlugin(OpenInv.class).getLogger(); + logger.warning("Unable to obtain field to inject custom save process - certain player data may be lost when saving!"); + logger.log(java.util.logging.Level.WARNING, e.getMessage(), e); + bukkitEntity = null; + } + } + + public static @NotNull ServerPlayer getHandle(final Player player) { + if (player instanceof CraftPlayer) { + return ((CraftPlayer) player).getHandle(); + } + + Server server = player.getServer(); + ServerPlayer nmsPlayer = null; + + if (server instanceof CraftServer) { + nmsPlayer = ((CraftServer) server).getHandle().getPlayer(player.getUniqueId()); + } + + if (nmsPlayer == null) { + // Could use reflection to examine fields, but it's honestly not worth the bother. + throw new RuntimeException("Unable to fetch EntityPlayer from Player implementation " + player.getClass().getName()); + } + + return nmsPlayer; + } + + @Override + public @Nullable Player loadPlayer(@NotNull final OfflinePlayer offline) { + // Ensure player has data + if (!offline.hasPlayedBefore()) { + return null; + } + + MinecraftServer server = ((CraftServer) Bukkit.getServer()).getServer(); + ServerLevel worldServer = server.getLevel(Level.OVERWORLD); + + if (worldServer == null) { + return null; + } + + // Create a new ServerPlayer. + ServerPlayer entity = createNewPlayer(server, worldServer, offline); + + // Stop listening for advancement progression - if this is not cleaned up, loading causes a memory leak. + entity.getAdvancements().stopListening(); + + // Try to load the player's data. + if (loadData(entity)) { + // If data is loaded successfully, return the Bukkit entity. + return entity.getBukkitEntity(); + } + + return null; + } + + private @NotNull ServerPlayer createNewPlayer( + @NotNull MinecraftServer server, + @NotNull ServerLevel worldServer, + @NotNull final OfflinePlayer offline) { + // See net.minecraft.server.players.PlayerList#canPlayerLogin(ServerLoginPacketListenerImpl, GameProfile) + // See net.minecraft.server.network.ServerLoginPacketListenerImpl#handleHello(ServerboundHelloPacket) + GameProfile profile = new GameProfile(offline.getUniqueId(), + offline.getName() != null ? offline.getName() : offline.getUniqueId().toString()); + + ClientInformation dummyInfo = new ClientInformation( + "en_us", + 1, // Reduce distance just in case. + ChatVisiblity.HIDDEN, // Don't accept chat. + false, + ServerPlayer.DEFAULT_MODEL_CUSTOMIZATION, + ServerPlayer.DEFAULT_MAIN_HAND, + true, + false // Don't list in player list (not that this player is in the list anyway). + ); + + ServerPlayer entity = new ServerPlayer(server, worldServer, profile, dummyInfo); + + try { + injectPlayer(entity); + } catch (IllegalAccessException e) { + JavaPlugin.getPlugin(OpenInv.class).getLogger().log( + java.util.logging.Level.WARNING, + e, + () -> "Unable to inject ServerPlayer, certain player data may be lost when saving!"); + } + + return entity; + } + + static boolean loadData(@NotNull ServerPlayer player) { + // See CraftPlayer#loadData + CompoundTag loadedData = player.server.getPlayerList().playerIo.load(player).orElse(null); + + if (loadedData == null) { + // Exceptions with loading are logged by Mojang. + return false; + } + + // Read basic data into the player. + player.load(loadedData); + // Also read "extra" data. + player.readAdditionalSaveData(loadedData); + // Game type settings are also loaded separately. + player.loadGameTypes(loadedData); + + if (paper) { + // Paper: world is not loaded by ServerPlayer#load(CompoundTag). + parseWorld(player, loadedData); + } + + return true; + } + + private static void parseWorld(@NotNull ServerPlayer player, @NotNull CompoundTag loadedData) { + // See PlayerList#placeNewPlayer + World bukkitWorld; + if (loadedData.contains("WorldUUIDMost") && loadedData.contains("WorldUUIDLeast")) { + // Modern Bukkit world. + bukkitWorld = Bukkit.getServer().getWorld(new UUID(loadedData.getLong("WorldUUIDMost"), loadedData.getLong("WorldUUIDLeast"))); + } else if (loadedData.contains("world", net.minecraft.nbt.Tag.TAG_STRING)) { + // Legacy Bukkit world. + bukkitWorld = Bukkit.getServer().getWorld(loadedData.getString("world")); + } else { + // Vanilla player data. + DimensionType.parseLegacy(new Dynamic<>(NbtOps.INSTANCE, loadedData.get("Dimension"))) + .resultOrPartial(JavaPlugin.getPlugin(OpenInv.class).getLogger()::warning) + .map(player.server::getLevel) + // If ServerLevel exists, set, otherwise move to spawn. + .ifPresentOrElse(player::setServerLevel, () -> player.spawnIn(null)); + return; + } + if (bukkitWorld == null) { + player.spawnIn(null); + return; + } + player.setServerLevel(((CraftWorld) bukkitWorld).getHandle()); + } + + private void injectPlayer(ServerPlayer player) throws IllegalAccessException { + if (bukkitEntity == null) { + return; + } + + bukkitEntity.setAccessible(true); + + bukkitEntity.set(player, new OpenPlayer(player.server.server, player)); + } + + @Override + public @NotNull Player inject(@NotNull Player player) { + try { + ServerPlayer nmsPlayer = getHandle(player); + if (nmsPlayer.getBukkitEntity() instanceof OpenPlayer openPlayer) { + return openPlayer; + } + injectPlayer(nmsPlayer); + return nmsPlayer.getBukkitEntity(); + } catch (IllegalAccessException e) { + JavaPlugin.getPlugin(OpenInv.class).getLogger().log( + java.util.logging.Level.WARNING, + e, + () -> "Unable to inject ServerPlayer, certain player data may be lost when saving!"); + return player; + } + } + + @Override + public @Nullable InventoryView openInventory(@NotNull Player player, @NotNull ISpecialInventory inventory) { + + ServerPlayer nmsPlayer = getHandle(player); + + if (nmsPlayer.connection == null) { + return null; + } + + InventoryView view = getView(player, inventory); + + if (view == null) { + return player.openInventory(inventory.getBukkitInventory()); + } + + AbstractContainerMenu container = new CraftContainer(view, nmsPlayer, nmsPlayer.nextContainerCounter()) { + @Override + public MenuType getType() { + return getContainers(inventory.getBukkitInventory().getSize()); + } + }; + + container.setTitle(Component.literal(view.getTitle())); + container = CraftEventFactory.callInventoryOpenEvent(nmsPlayer, container); + + if (container == null) { + return null; + } + + nmsPlayer.connection.send(new ClientboundOpenScreenPacket(container.containerId, container.getType(), + Component.literal(container.getBukkitView().getTitle()))); + nmsPlayer.containerMenu = container; + nmsPlayer.initMenu(container); + + return container.getBukkitView(); + + } + + private @Nullable InventoryView getView(Player player, ISpecialInventory inventory) { + if (inventory instanceof SpecialEnderChest) { + return new OpenInventoryView(player, inventory, "container.enderchest", "'s Ender Chest"); + } else if (inventory instanceof SpecialPlayerInventory) { + return new OpenInventoryView(player, inventory, "container.player", "'s Inventory"); + } else { + return null; + } + } + + static @NotNull MenuType getContainers(int inventorySize) { + + return switch (inventorySize) { + case 9 -> MenuType.GENERIC_9x1; + case 18 -> MenuType.GENERIC_9x2; + case 36 -> MenuType.GENERIC_9x4; // PLAYER + case 41, 45 -> MenuType.GENERIC_9x5; + case 54 -> MenuType.GENERIC_9x6; + default -> MenuType.GENERIC_9x3; // Default 27-slot inventory + }; + } + + @Override + public int convertToPlayerSlot(InventoryView view, int rawSlot) { + int topSize = view.getTopInventory().getSize(); + if (topSize <= rawSlot) { + // Slot is not inside special inventory, use Bukkit logic. + return view.convertSlot(rawSlot); + } + + // Main inventory, slots 0-26 -> 9-35 + if (rawSlot < 27) { + return rawSlot + 9; + } + // Hotbar, slots 27-35 -> 0-8 + if (rawSlot < 36) { + return rawSlot - 27; + } + // Armor, slots 36-39 -> 39-36 + if (rawSlot < 40) { + return 36 + (39 - rawSlot); + } + // Off hand + if (rawSlot == 40) { + return 40; + } + // Drop slots, "out of inventory" + return -1; + } + +} diff --git a/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/SpecialEnderChest.java b/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/SpecialEnderChest.java new file mode 100644 index 00000000..512f2d95 --- /dev/null +++ b/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/SpecialEnderChest.java @@ -0,0 +1,352 @@ +/* + * Copyright (C) 2011-2023 lishid. All rights reserved. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.lishid.openinv.internal.v1_21_R1; + +import com.lishid.openinv.internal.ISpecialEnderChest; +import net.minecraft.core.HolderLookup; +import net.minecraft.core.NonNullList; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.ListTag; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.ContainerHelper; +import net.minecraft.world.ContainerListener; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.entity.player.StackedContents; +import net.minecraft.world.inventory.PlayerEnderChestContainer; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.block.entity.EnderChestBlockEntity; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_21_R1.entity.CraftHumanEntity; +import org.bukkit.craftbukkit.v1_21_R1.inventory.CraftInventory; +import org.bukkit.entity.HumanEntity; +import org.bukkit.inventory.InventoryHolder; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.List; +import java.util.function.Predicate; +import java.util.stream.Collectors; + +public class SpecialEnderChest extends PlayerEnderChestContainer implements ISpecialEnderChest { + + private final CraftInventory inventory; + private ServerPlayer owner; + private NonNullList items; + private boolean playerOnline; + + public SpecialEnderChest(final org.bukkit.entity.Player player, final Boolean online) { + super(PlayerDataManager.getHandle(player)); + this.inventory = new CraftInventory(this); + this.owner = PlayerDataManager.getHandle(player); + this.playerOnline = online; + this.items = this.owner.getEnderChestInventory().items; + } + + @Override + public @NotNull CraftInventory getBukkitInventory() { + return inventory; + } + + @Override + public void setPlayerOffline() { + this.playerOnline = false; + } + + @Override + public void setPlayerOnline(@NotNull final org.bukkit.entity.Player player) { + if (this.playerOnline) { + return; + } + + ServerPlayer offlinePlayer = this.owner; + ServerPlayer onlinePlayer = PlayerDataManager.getHandle(player); + + // Set owner to new player. + this.owner = onlinePlayer; + + // Set player's ender chest contents to our modified contents. + PlayerEnderChestContainer onlineEnderChest = onlinePlayer.getEnderChestInventory(); + for (int i = 0; i < onlineEnderChest.getContainerSize(); ++i) { + onlineEnderChest.setItem(i, this.items.get(i)); + } + + // Set our item array to the new inventory's array. + this.items = onlineEnderChest.items; + + // Add viewers to new inventory. + onlineEnderChest.transaction.addAll(offlinePlayer.getEnderChestInventory().transaction); + + this.playerOnline = true; + } + + @Override + public @NotNull org.bukkit.entity.Player getPlayer() { + return owner.getBukkitEntity(); + } + + @Override + public void setChanged() { + this.owner.getEnderChestInventory().setChanged(); + } + + @Override + public List getContents() { + return this.items; + } + + @Override + public void onOpen(CraftHumanEntity who) { + this.owner.getEnderChestInventory().onOpen(who); + } + + @Override + public void onClose(CraftHumanEntity who) { + this.owner.getEnderChestInventory().onClose(who); + } + + @Override + public List getViewers() { + return this.owner.getEnderChestInventory().getViewers(); + } + + @Override + public boolean stillValid(Player player) { + return true; + } + + @Override + public void setActiveChest(EnderChestBlockEntity enderChest) { + this.owner.getEnderChestInventory().setActiveChest(enderChest); + } + + @Override + public boolean isActiveChest(EnderChestBlockEntity enderChest) { + return this.owner.getEnderChestInventory().isActiveChest(enderChest); + } + + @Override + public int getMaxStackSize() { + return this.owner.getEnderChestInventory().getMaxStackSize(); + } + + @Override + public void setMaxStackSize(int i) { + this.owner.getEnderChestInventory().setMaxStackSize(i); + } + + @Override + public InventoryHolder getOwner() { + return this.owner.getEnderChestInventory().getOwner(); + } + + @Override + public @Nullable Location getLocation() { + return null; + } + + @Override + public void addListener(ContainerListener listener) { + this.owner.getEnderChestInventory().addListener(listener); + } + + @Override + public void removeListener(ContainerListener listener) { + this.owner.getEnderChestInventory().removeListener(listener); + } + + @Override + public ItemStack getItem(int i) { + return i >= 0 && i < this.items.size() ? this.items.get(i) : ItemStack.EMPTY; + } + + @Override + public ItemStack removeItem(int i, int j) { + ItemStack itemstack = ContainerHelper.removeItem(this.items, i, j); + if (!itemstack.isEmpty()) { + this.setChanged(); + } + + return itemstack; + } + + @Override + public ItemStack addItem(ItemStack itemstack) { + ItemStack localItem = itemstack.copy(); + this.moveItemToOccupiedSlotsWithSameType(localItem); + if (localItem.isEmpty()) { + return ItemStack.EMPTY; + } else { + this.moveItemToEmptySlots(localItem); + return localItem.isEmpty() ? ItemStack.EMPTY : localItem; + } + } + + @Override + public boolean canAddItem(ItemStack itemstack) { + for (ItemStack itemstack1 : this.items) { + if (itemstack1.isEmpty() || ItemStack.isSameItemSameComponents(itemstack1, itemstack) && itemstack1.getCount() < itemstack1.getMaxStackSize()) { + return true; + } + } + + return false; + } + + private void moveItemToEmptySlots(ItemStack itemstack) { + for(int i = 0; i < this.getContainerSize(); ++i) { + ItemStack localItem = this.getItem(i); + if (localItem.isEmpty()) { + this.setItem(i, itemstack.copy()); + itemstack.setCount(0); + return; + } + } + } + + private void moveItemToOccupiedSlotsWithSameType(ItemStack itemstack) { + for(int i = 0; i < this.getContainerSize(); ++i) { + ItemStack localItem = this.getItem(i); + if (ItemStack.isSameItemSameComponents(localItem, itemstack)) { + this.moveItemsBetweenStacks(itemstack, localItem); + if (itemstack.isEmpty()) { + return; + } + } + } + } + + private void moveItemsBetweenStacks(ItemStack itemstack, ItemStack itemstack1) { + int i = Math.min(this.getMaxStackSize(), itemstack1.getMaxStackSize()); + int j = Math.min(itemstack.getCount(), i - itemstack1.getCount()); + if (j > 0) { + itemstack1.grow(j); + itemstack.shrink(j); + this.setChanged(); + } + } + + @Override + public ItemStack removeItemNoUpdate(int i) { + ItemStack itemstack = this.items.get(i); + if (itemstack.isEmpty()) { + return ItemStack.EMPTY; + } else { + this.items.set(i, ItemStack.EMPTY); + return itemstack; + } + } + + @Override + public void setItem(int i, ItemStack itemstack) { + this.items.set(i, itemstack); + if (!itemstack.isEmpty() && itemstack.getCount() > this.getMaxStackSize()) { + itemstack.setCount(this.getMaxStackSize()); + } + + this.setChanged(); + } + + @Override + public int getContainerSize() { + return this.owner.getEnderChestInventory().getContainerSize(); + } + + @Override + public boolean isEmpty() { + return this.items.stream().allMatch(ItemStack::isEmpty); + } + + @Override + public void startOpen(Player player) { + } + + @Override + public void stopOpen(Player player) { + } + + @Override + public boolean canPlaceItem(int i, ItemStack itemstack) { + return true; + } + + @Override + public void clearContent() { + this.items.clear(); + this.setChanged(); + } + + @Override + public void fillStackedContents(StackedContents stackedContents) { + for (ItemStack itemstack : this.items) { + stackedContents.accountStack(itemstack); + } + + } + + @Override + public List removeAllItems() { + List list = this.items.stream().filter(Predicate.not(ItemStack::isEmpty)).collect(Collectors.toList()); + this.clearContent(); + return list; + } + + @Override + public ItemStack removeItemType(Item item, int i) { + ItemStack itemstack = new ItemStack(item, 0); + + for(int j = this.getContainerSize() - 1; j >= 0; --j) { + ItemStack localItem = this.getItem(j); + if (localItem.getItem().equals(item)) { + int k = i - itemstack.getCount(); + ItemStack splitItem = localItem.split(k); + itemstack.grow(splitItem.getCount()); + if (itemstack.getCount() == i) { + break; + } + } + } + + if (!itemstack.isEmpty()) { + this.setChanged(); + } + + return itemstack; + } + + @Override + public String toString() { + return this.items.stream().filter((itemStack) -> !itemStack.isEmpty()).toList().toString(); + } + + @Override + public void fromTag(ListTag listTag, HolderLookup.Provider holderLookup) { + for (int i = 0; i < this.getContainerSize(); ++i) { + this.setItem(i, ItemStack.EMPTY); + } + + for (int i = 0; i < listTag.size(); ++i) { + CompoundTag compoundTag = listTag.getCompound(i); + int j = compoundTag.getByte("Slot") & 255; + if (j < this.getContainerSize()) { + this.setItem(j, ItemStack.parseOptional(holderLookup, compoundTag)); + } + } + + } + +} diff --git a/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/SpecialPlayerInventory.java b/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/SpecialPlayerInventory.java new file mode 100644 index 00000000..4bfb7b1c --- /dev/null +++ b/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/SpecialPlayerInventory.java @@ -0,0 +1,768 @@ +/* + * Copyright (C) 2011-2023 lishid. All rights reserved. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.lishid.openinv.internal.v1_21_R1; + +import com.google.common.collect.ImmutableList; +import com.lishid.openinv.internal.ISpecialPlayerInventory; +import net.minecraft.CrashReport; +import net.minecraft.CrashReportCategory; +import net.minecraft.ReportedException; +import net.minecraft.core.NonNullList; +import net.minecraft.core.component.DataComponents; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.ListTag; +import net.minecraft.network.chat.Component; +import net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.tags.TagKey; +import net.minecraft.world.Container; +import net.minecraft.world.ContainerHelper; +import net.minecraft.world.entity.player.Inventory; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.entity.player.StackedContents; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.block.state.BlockState; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_21_R1.entity.CraftHumanEntity; +import org.bukkit.craftbukkit.v1_21_R1.inventory.CraftInventory; +import org.bukkit.entity.HumanEntity; +import org.bukkit.inventory.InventoryHolder; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.function.Function; +import java.util.function.Predicate; +import java.util.stream.Collectors; + +public class SpecialPlayerInventory extends Inventory implements ISpecialPlayerInventory { + + private final CraftInventory inventory; + private boolean playerOnline; + private Player player; + private NonNullList items; + private NonNullList armor; + private NonNullList offhand; + private List> compartments; + + public SpecialPlayerInventory(@NotNull org.bukkit.entity.Player bukkitPlayer, @NotNull Boolean online) { + super(PlayerDataManager.getHandle(bukkitPlayer)); + this.inventory = new CraftInventory(this); + this.playerOnline = online; + this.player = super.player; + this.selected = player.getInventory().selected; + this.items = this.player.getInventory().items; + this.armor = this.player.getInventory().armor; + this.offhand = this.player.getInventory().offhand; + this.compartments = ImmutableList.of(this.items, this.armor, this.offhand); + } + + @Override + public void setPlayerOnline(@NotNull org.bukkit.entity.Player player) { + if (this.playerOnline) { + return; + } + + Player offlinePlayer = this.player; + Player onlinePlayer = PlayerDataManager.getHandle(player); + onlinePlayer.getInventory().transaction.addAll(this.transaction); + + // Set owner to new player. + this.player = onlinePlayer; + + // Set player's inventory contents to our modified contents. + Inventory onlineInventory = onlinePlayer.getInventory(); + for (int i = 0; i < getContainerSize(); ++i) { + onlineInventory.setItem(i, getRawItem(i)); + } + onlineInventory.selected = this.selected; + + // Set our item arrays to the new inventory's arrays. + this.items = onlineInventory.items; + this.armor = onlineInventory.armor; + this.offhand = onlineInventory.offhand; + this.compartments = ImmutableList.of(this.items, this.armor, this.offhand); + + // Add existing viewers to new viewer list. + Inventory offlineInventory = offlinePlayer.getInventory(); + // Remove self from listing - player is always a viewer of their own inventory, prevent duplicates. + offlineInventory.transaction.remove(offlinePlayer.getBukkitEntity()); + onlineInventory.transaction.addAll(offlineInventory.transaction); + + this.playerOnline = true; + } + + @Override + public @NotNull CraftInventory getBukkitInventory() { + return this.inventory; + } + + @Override + public void setPlayerOffline() { + this.playerOnline = false; + } + + @Override + public @NotNull HumanEntity getPlayer() { + return this.player.getBukkitEntity(); + } + + private @NotNull ItemStack getRawItem(int i) { + if (i < 0) { + return ItemStack.EMPTY; + } + + NonNullList list; + for (Iterator> iterator = this.compartments.iterator(); iterator.hasNext(); i -= list.size()) { + list = iterator.next(); + if (i < list.size()) { + return list.get(i); + } + } + + return ItemStack.EMPTY; + } + + private void setRawItem(int i, @NotNull ItemStack itemStack) { + if (i < 0) { + return; + } + + NonNullList list; + for (Iterator> iterator = this.compartments.iterator(); iterator.hasNext(); i -= list.size()) { + list = iterator.next(); + if (i < list.size()) { + list.set(i, itemStack); + } + } + } + + private record IndexedCompartment(@Nullable NonNullList compartment, int index) {} + + private @NotNull SpecialPlayerInventory.IndexedCompartment getIndexedContent(int index) { + if (index < items.size()) { + return new IndexedCompartment(items, getReversedItemSlotNum(index)); + } + + index -= items.size(); + + if (index < armor.size()) { + return new IndexedCompartment(armor, getReversedArmorSlotNum(index)); + } + + index -= armor.size(); + + if (index < offhand.size()) { + return new IndexedCompartment(offhand, index); + } + + index -= offhand.size(); + + return new IndexedCompartment(null, index); + } + + private int getReversedArmorSlotNum(final int i) { + if (i == 0) { + return 3; + } + if (i == 1) { + return 2; + } + if (i == 2) { + return 1; + } + if (i == 3) { + return 0; + } + return i; + } + + private int getReversedItemSlotNum(final int i) { + if (i >= 27) { + return i - 27; + } + return i + 9; + } + + @Override + public boolean contains(Predicate predicate) { + return this.compartments.stream().flatMap(NonNullList::stream).anyMatch(predicate); + } + + @Override + public List getArmorContents() { + return this.armor; + } + + @Override + public void onOpen(CraftHumanEntity who) { + this.player.getInventory().onOpen(who); + } + + @Override + public void onClose(CraftHumanEntity who) { + this.player.getInventory().onClose(who); + } + + @Override + public List getViewers() { + return this.player.getInventory().getViewers(); + } + + @Override + public InventoryHolder getOwner() { + return this.player.getBukkitEntity(); + } + + @Override + public int getMaxStackSize() { + return this.player.getInventory().getMaxStackSize(); + } + + @Override + public void setMaxStackSize(int size) { + this.player.getInventory().setMaxStackSize(size); + } + + @Override + public Location getLocation() { + return this.player.getBukkitEntity().getLocation(); + } + + @Override + public boolean hasCustomName() { + return false; + } + + @Override + public List getContents() { + return this.compartments.stream().flatMap(Collection::stream).collect(Collectors.toList()); + } + + @Override + public ItemStack getSelected() { + return isHotbarSlot(this.selected) ? this.items.get(this.selected) : ItemStack.EMPTY; + } + + private boolean hasRemainingSpaceForItem(ItemStack itemstack, ItemStack itemstack1) { + return !itemstack.isEmpty() && ItemStack.isSameItemSameComponents(itemstack, itemstack1) && itemstack.isStackable() && itemstack.getCount() < itemstack.getMaxStackSize() && itemstack.getCount() < this.getMaxStackSize(); + } + + @Override + public int canHold(ItemStack itemstack) { + int remains = itemstack.getCount(); + + for (int i = 0; i < this.items.size(); ++i) { + ItemStack itemstack1 = this.getRawItem(i); + if (itemstack1.isEmpty()) { + return itemstack.getCount(); + } + + if (this.hasRemainingSpaceForItem(itemstack1, itemstack)) { + remains -= (itemstack1.getMaxStackSize() < this.getMaxStackSize() ? itemstack1.getMaxStackSize() : this.getMaxStackSize()) - itemstack1.getCount(); + } + + if (remains <= 0) { + return itemstack.getCount(); + } + } + + ItemStack offhandItemStack = this.getRawItem(this.items.size() + this.armor.size()); + if (this.hasRemainingSpaceForItem(offhandItemStack, itemstack)) { + remains -= (offhandItemStack.getMaxStackSize() < this.getMaxStackSize() ? offhandItemStack.getMaxStackSize() : this.getMaxStackSize()) - offhandItemStack.getCount(); + } + + return remains <= 0 ? itemstack.getCount() : itemstack.getCount() - remains; + } + + @Override + public int getFreeSlot() { + for(int i = 0; i < this.items.size(); ++i) { + if (this.items.get(i).isEmpty()) { + return i; + } + } + + return -1; + } + + @Override + public void setPickedItem(ItemStack itemstack) { + int i = this.findSlotMatchingItem(itemstack); + if (isHotbarSlot(i)) { + this.selected = i; + } else if (i == -1) { + this.selected = this.getSuitableHotbarSlot(); + if (!this.items.get(this.selected).isEmpty()) { + int j = this.getFreeSlot(); + if (j != -1) { + this.items.set(j, this.items.get(this.selected)); + } + } + + this.items.set(this.selected, itemstack); + } else { + this.pickSlot(i); + } + + } + + @Override + public void pickSlot(int i) { + this.selected = this.getSuitableHotbarSlot(); + ItemStack itemstack = this.items.get(this.selected); + this.items.set(this.selected, this.items.get(i)); + this.items.set(i, itemstack); + } + + @Override + public int findSlotMatchingItem(ItemStack itemstack) { + for(int i = 0; i < this.items.size(); ++i) { + if (!this.items.get(i).isEmpty() && ItemStack.isSameItemSameComponents(itemstack, this.items.get(i))) { + return i; + } + } + + return -1; + } + + @Override + public int findSlotMatchingUnusedItem(ItemStack itemStack) { + for(int i = 0; i < this.items.size(); ++i) { + ItemStack localItem = this.items.get(i); + if (!this.items.get(i).isEmpty() && ItemStack.isSameItemSameComponents(itemStack, this.items.get(i)) && !this.items.get(i).isDamaged() && !localItem.isEnchanted() && !localItem.has(DataComponents.CUSTOM_NAME)) { + return i; + } + } + + return -1; + } + + @Override + public int getSuitableHotbarSlot() { + int i; + int j; + for(j = 0; j < 9; ++j) { + i = (this.selected + j) % 9; + if (this.items.get(i).isEmpty()) { + return i; + } + } + + for(j = 0; j < 9; ++j) { + i = (this.selected + j) % 9; + if (!this.items.get(i).isEnchanted()) { + return i; + } + } + + return this.selected; + } + + @Override + public void swapPaint(double d0) { + int i = (int) Math.signum(d0); + + this.selected -= i; + + while (this.selected < 0) { + this.selected += 9; + } + + while(this.selected >= 9) { + this.selected -= 9; + } + } + + @Override + public int clearOrCountMatchingItems(Predicate predicate, int i, Container container) { + byte b0 = 0; + boolean flag = i == 0; + int j = b0 + ContainerHelper.clearOrCountMatchingItems(this, predicate, i - b0, flag); + j += ContainerHelper.clearOrCountMatchingItems(container, predicate, i - j, flag); + ItemStack itemstack = this.player.containerMenu.getCarried(); + j += ContainerHelper.clearOrCountMatchingItems(itemstack, predicate, i - j, flag); + if (itemstack.isEmpty()) { + this.player.containerMenu.setCarried(ItemStack.EMPTY); + } + + return j; + } + + private int addResource(ItemStack itemstack) { + int i = this.getSlotWithRemainingSpace(itemstack); + if (i == -1) { + i = this.getFreeSlot(); + } + + return i == -1 ? itemstack.getCount() : this.addResource(i, itemstack); + } + + private int addResource(int i, ItemStack itemstack) { + int j = itemstack.getCount(); + ItemStack localItemStack = this.getRawItem(i); + if (localItemStack.isEmpty()) { + localItemStack = itemstack.copyWithCount(0); + this.setRawItem(i, localItemStack); + } + + int k = Math.min(j, this.getMaxStackSize(localItemStack) - localItemStack.getCount()); + + if (k != 0) { + j -= k; + localItemStack.grow(k); + localItemStack.setPopTime(5); + } + + return j; + } + + @Override + public int getSlotWithRemainingSpace(ItemStack itemstack) { + if (this.hasRemainingSpaceForItem(this.getRawItem(this.selected), itemstack)) { + return this.selected; + } else if (this.hasRemainingSpaceForItem(this.getRawItem(40), itemstack)) { + return 40; + } else { + for(int i = 0; i < this.items.size(); ++i) { + if (this.hasRemainingSpaceForItem(this.items.get(i), itemstack)) { + return i; + } + } + + return -1; + } + } + + @Override + public void tick() { + for (NonNullList compartment : this.compartments) { + for (int i = 0; i < compartment.size(); ++i) { + if (!compartment.get(i).isEmpty()) { + compartment.get(i).inventoryTick(this.player.level(), this.player, i, this.selected == i); + } + } + } + + } + + @Override + public boolean add(ItemStack itemStack) { + return this.add(-1, itemStack); + } + + @Override + public boolean add(int i, ItemStack itemStack) { + if (itemStack.isEmpty()) { + return false; + } else { + try { + if (itemStack.isDamaged()) { + if (i == -1) { + i = this.getFreeSlot(); + } + + if (i >= 0) { + this.items.set(i, itemStack.copy()); + this.items.get(i).setPopTime(5); + itemStack.setCount(0); + return true; + } else if (this.player.hasInfiniteMaterials()) { + itemStack.setCount(0); + return true; + } else { + return false; + } + } else { + int j; + do { + j = itemStack.getCount(); + if (i == -1) { + itemStack.setCount(this.addResource(itemStack)); + } else { + itemStack.setCount(this.addResource(i, itemStack)); + } + } while(!itemStack.isEmpty() && itemStack.getCount() < j); + + if (itemStack.getCount() == j && this.player.hasInfiniteMaterials()) { + itemStack.setCount(0); + return true; + } else { + return itemStack.getCount() < j; + } + } + } catch (Throwable var6) { + CrashReport crashReport = CrashReport.forThrowable(var6, "Adding item to inventory"); + CrashReportCategory crashReportCategory = crashReport.addCategory("Item being added"); + crashReportCategory.setDetail("Item ID", Item.getId(itemStack.getItem())); + crashReportCategory.setDetail("Item data", itemStack.getDamageValue()); + crashReportCategory.setDetail("Item name", () -> itemStack.getHoverName().getString()); + throw new ReportedException(crashReport); + } + } + } + + @Override + public void placeItemBackInInventory(ItemStack itemStack) { + this.placeItemBackInInventory(itemStack, true); + } + + @Override + public void placeItemBackInInventory(ItemStack itemStack, boolean flag) { + while(true) { + if (!itemStack.isEmpty()) { + int i = this.getSlotWithRemainingSpace(itemStack); + if (i == -1) { + i = this.getFreeSlot(); + } + + if (i != -1) { + int j = itemStack.getMaxStackSize() - this.getRawItem(i).getCount(); + if (this.add(i, itemStack.split(j)) && flag && this.player instanceof ServerPlayer) { + ((ServerPlayer)this.player).connection.send(new ClientboundContainerSetSlotPacket(-2, 0, i, this.getRawItem(i))); + } + continue; + } + + this.player.drop(itemStack, false); + } + + return; + } + } + + @Override + public ItemStack removeItem(int rawIndex, final int j) { + IndexedCompartment indexedCompartment = getIndexedContent(rawIndex); + + if (indexedCompartment.compartment() == null + || indexedCompartment.compartment().get(indexedCompartment.index()).isEmpty()) { + return ItemStack.EMPTY; + } + + return ContainerHelper.removeItem(indexedCompartment.compartment(), indexedCompartment.index(), j); + } + + @Override + public void removeItem(ItemStack itemStack) { + for (NonNullList compartment : this.compartments) { + for (int i = 0; i < compartment.size(); ++i) { + if (compartment.get(i) == itemStack) { + compartment.set(i, ItemStack.EMPTY); + break; + } + } + } + } + + @Override + public ItemStack removeItemNoUpdate(int rawIndex) { + IndexedCompartment indexedCompartment = getIndexedContent(rawIndex); + + if (indexedCompartment.compartment() == null) { + return ItemStack.EMPTY; + } + + ItemStack removed = indexedCompartment.compartment().set(indexedCompartment.index(), ItemStack.EMPTY); + + if (removed.isEmpty()) { + return ItemStack.EMPTY; + } + + return removed; + } + + @Override + public void setItem(int rawIndex, final ItemStack itemStack) { + IndexedCompartment indexedCompartment = getIndexedContent(rawIndex); + + if (indexedCompartment.compartment() == null) { + this.player.drop(itemStack, true); + return; + } + + indexedCompartment.compartment().set(indexedCompartment.index(), itemStack); + } + + @Override + public float getDestroySpeed(BlockState blockState) { + return this.items.get(this.selected).getDestroySpeed(blockState); + } + + @Override + public ListTag save(ListTag listTag) { + for (int i = 0; i < this.items.size(); ++i) { + if (!this.items.get(i).isEmpty()) { + CompoundTag compoundTag = new CompoundTag(); + compoundTag.putByte("Slot", (byte)i); + this.items.get(i).save(this.player.registryAccess(), compoundTag); + listTag.add(compoundTag); + } + } + + for (int i = 0; i < this.armor.size(); ++i) { + if (!this.armor.get(i).isEmpty()) { + CompoundTag compoundTag = new CompoundTag(); + compoundTag.putByte("Slot", (byte)(i + 100)); + this.armor.get(i).save(this.player.registryAccess(), compoundTag); + listTag.add(compoundTag); + } + } + + for (int i = 0; i < this.offhand.size(); ++i) { + if (!this.offhand.get(i).isEmpty()) { + CompoundTag compoundTag = new CompoundTag(); + compoundTag.putByte("Slot", (byte)(i + 150)); + this.offhand.get(i).save(this.player.registryAccess(), compoundTag); + listTag.add(compoundTag); + } + } + + return listTag; + } + + @Override + public void load(ListTag listTag) { + this.items.clear(); + this.armor.clear(); + this.offhand.clear(); + + for(int i = 0; i < listTag.size(); ++i) { + CompoundTag compoundTag = listTag.getCompound(i); + int j = compoundTag.getByte("Slot") & 255; + ItemStack itemstack = ItemStack.parse(this.player.registryAccess(), compoundTag).orElse(ItemStack.EMPTY); + if (j < this.items.size()) { + this.items.set(j, itemstack); + } else if (j >= 100 && j < this.armor.size() + 100) { + this.armor.set(j - 100, itemstack); + } else if (j >= 150 && j < this.offhand.size() + 150) { + this.offhand.set(j - 150, itemstack); + } + } + + } + + @Override + public int getContainerSize() { + return 45; + } + + @Override + public boolean isEmpty() { + return !contains(itemStack -> !itemStack.isEmpty()); + } + + @Override + public ItemStack getItem(int rawIndex) { + IndexedCompartment indexedCompartment = getIndexedContent(rawIndex); + + if (indexedCompartment.compartment() == null) { + return ItemStack.EMPTY; + } + + return indexedCompartment.compartment().get(indexedCompartment.index()); + } + + @Override + public Component getName() { + return this.player.getName(); + } + + @Override + public ItemStack getArmor(int index) { + return this.armor.get(index); + } + + @Override + public void dropAll() { + for (NonNullList compartment : this.compartments) { + for (int i = 0; i < compartment.size(); ++i) { + ItemStack itemstack = compartment.get(i); + if (!itemstack.isEmpty()) { + this.player.drop(itemstack, true, false); + compartment.set(i, ItemStack.EMPTY); + } + } + } + } + + @Override + public void setChanged() { + super.setChanged(); + } + + @Override + public int getTimesChanged() { + return super.getTimesChanged(); + } + + @Override + public boolean stillValid(Player player) { + return true; + } + + @Override + public boolean contains(ItemStack itemstack) { + return contains(itemStack -> !itemStack.isEmpty() && ItemStack.isSameItemSameComponents(itemstack, itemStack)); + } + + @Override + public boolean contains(TagKey tagKey) { + return contains(itemStack -> !itemStack.isEmpty() && itemStack.is(tagKey)); + } + + @Override + public void replaceWith(Inventory inventory) { + Function getter; + + if (inventory instanceof SpecialPlayerInventory specialPlayerInventory) { + getter = specialPlayerInventory::getRawItem; + } else { + getter = inventory::getItem; + } + + for(int i = 0; i < this.getContainerSize(); ++i) { + this.setRawItem(i, getter.apply(i)); + } + + this.selected = inventory.selected; + } + + @Override + public void clearContent() { + for (NonNullList compartment : this.compartments) { + compartment.clear(); + } + } + + @Override + public void fillStackedContents(StackedContents stackedContents) { + for (ItemStack itemstack : this.items) { + stackedContents.accountSimpleStack(itemstack); + } + } + + @Override + public ItemStack removeFromSelected(boolean dropWholeStack) { + ItemStack itemstack = this.getSelected(); + return itemstack.isEmpty() ? ItemStack.EMPTY : this.removeItem(this.selected, dropWholeStack ? itemstack.getCount() : 1); + } + +} diff --git a/plugin/src/main/java/com/lishid/openinv/InternalAccessor.java b/plugin/src/main/java/com/lishid/openinv/InternalAccessor.java index 778589c9..a04ac0c3 100644 --- a/plugin/src/main/java/com/lishid/openinv/InternalAccessor.java +++ b/plugin/src/main/java/com/lishid/openinv/InternalAccessor.java @@ -59,20 +59,21 @@ private void checkSupported() { } private @Nullable String getVersionPackage() { - if (BukkitVersions.MINECRAFT.lessThan(Version.of(1, 20, 3))) { // Min supported version: 1.20.3 + if (BukkitVersions.MINECRAFT.lessThan(Version.of(1, 20, 4)) // Min supported version: 1.20.4 + || BukkitVersions.MINECRAFT.equals(Version.of(1, 20, 5))) { // 1.20.5 not supported at all. return null; } - if (BukkitVersions.MINECRAFT.lessThanOrEqual(Version.of(1, 20, 4))) { // 1.20.3, 1.20.4 + if (BukkitVersions.MINECRAFT.equals(Version.of(1, 20, 4))) { // 1.20.4 return "v1_20_R3"; } - if (BukkitVersions.MINECRAFT.greaterThanOrEqual(Version.of(1, 21))) { - return null; + if (BukkitVersions.MINECRAFT.equals(Version.of(1, 20, 6))) { // 1.20.6 + return "v1_20_R4"; } - if (BukkitVersions.MINECRAFT.greaterThan(Version.of(1, 20, 6))) { - return null; // TODO: use at your own risk flag? + if (BukkitVersions.MINECRAFT.greaterThan(Version.of(1, 21))) { + return null; } - // 1.20.5, 1.20.6 - return "v1_20_R4"; + // 1.21 + return "v1_21_R1"; } public String getReleasesLink() { @@ -136,9 +137,12 @@ public String getReleasesLink() { if (BukkitVersions.MINECRAFT.lessThanOrEqual(Version.of(1, 20, 1))) { // 1.20, 1.20.1 return "https://github.com/Jikoo/OpenInv/releases/tag/4.4.1"; } - if (BukkitVersions.MINECRAFT.equals(Version.of(1, 20, 3))) { // 1.20.3 + if (BukkitVersions.MINECRAFT.lessThanOrEqual(Version.of(1, 20, 3))) { // 1.20.2, 1.20.3 return "https://github.com/Jikoo/OpenInv/releases/tag/4.4.3"; } + if (BukkitVersions.MINECRAFT.equals(Version.of(1, 20, 5))) { // 1.20.5 shouldn't be used. + return "https://github.com/Jikoo/OpenInv/releases"; + } return "https://github.com/Jikoo/OpenInv/releases"; } diff --git a/plugin/src/main/java/com/lishid/openinv/OpenInv.java b/plugin/src/main/java/com/lishid/openinv/OpenInv.java index 9ff7ee5c..6ab067ec 100644 --- a/plugin/src/main/java/com/lishid/openinv/OpenInv.java +++ b/plugin/src/main/java/com/lishid/openinv/OpenInv.java @@ -33,18 +33,6 @@ import com.lishid.openinv.util.StringMetric; import com.lishid.openinv.util.lang.LanguageManager; import com.lishid.openinv.util.lang.Replacement; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.Map; -import java.util.Objects; -import java.util.UUID; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.Future; -import java.util.function.Consumer; -import java.util.function.Predicate; -import java.util.logging.Level; -import java.util.stream.Stream; import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.chat.TextComponent; import org.bukkit.Bukkit; @@ -63,6 +51,19 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.Map; +import java.util.Objects; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; +import java.util.function.Consumer; +import java.util.function.Predicate; +import java.util.logging.Level; +import java.util.stream.Stream; + /** * Open other player's inventory * @@ -136,7 +137,6 @@ public void onEnable() { Class.forName("org.bukkit.entity.Player$Spigot"); isSpigot = true; } catch (ClassNotFoundException e) { - e.printStackTrace(); isSpigot = false; } @@ -318,7 +318,7 @@ public boolean isPlayerLoaded(@NotNull UUID playerUuid) { try { player = future.get(); } catch (InterruptedException | ExecutionException e) { - e.printStackTrace(); + getLogger().log(Level.WARNING, e.getMessage(), e); return null; } @@ -510,7 +510,7 @@ public void sendSystemMessage(@NotNull Player player, @NotNull String key) { return; } - player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(message)); + player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacy(message)); } /** diff --git a/plugin/src/main/java/com/lishid/openinv/internal/OpenInventoryView.java b/plugin/src/main/java/com/lishid/openinv/internal/OpenInventoryView.java index ea7c438e..c5743a77 100644 --- a/plugin/src/main/java/com/lishid/openinv/internal/OpenInventoryView.java +++ b/plugin/src/main/java/com/lishid/openinv/internal/OpenInventoryView.java @@ -18,7 +18,6 @@ import com.lishid.openinv.OpenInv; import com.lishid.openinv.util.lang.Replacement; -import java.util.Objects; import org.bukkit.entity.HumanEntity; import org.bukkit.entity.Player; import org.bukkit.event.inventory.InventoryType; @@ -26,6 +25,8 @@ import org.bukkit.inventory.InventoryView; import org.jetbrains.annotations.NotNull; +import java.util.Objects; + public class OpenInventoryView extends InventoryView { private final @NotNull Player player; @@ -68,17 +69,28 @@ public OpenInventoryView( @Override public @NotNull String getTitle() { if (title == null) { - HumanEntity owner = inventory.getPlayer(); - - String localTitle = OpenInv.getPlugin(OpenInv.class) - .getLocalizedMessage( - player, - titleKey, - new Replacement("%player%", owner.getName())); - title = Objects.requireNonNullElseGet(localTitle, () -> owner.getName() + titleDefaultSuffix); + title = getOriginalTitle(); } return title; } + @NotNull + @Override + public String getOriginalTitle() { + HumanEntity owner = inventory.getPlayer(); + + String localTitle = OpenInv.getPlugin(OpenInv.class) + .getLocalizedMessage( + player, + titleKey, + new Replacement("%player%", owner.getName())); + return Objects.requireNonNullElseGet(localTitle, () -> owner.getName() + titleDefaultSuffix); + } + + @Override + public void setTitle(@NotNull String title) { + this.title = title; + } + } diff --git a/pom.xml b/pom.xml index d8ad20b3..a83101f7 100644 --- a/pom.xml +++ b/pom.xml @@ -42,6 +42,7 @@ plugin internal/v1_20_R3 internal/v1_20_R4 + internal/v1_21_R1 assembly @@ -82,7 +83,7 @@ spigot-api org.spigotmc provided - 1.18.2-R0.1-SNAPSHOT + 1.20.4-R0.1-SNAPSHOT openinvapi @@ -105,7 +106,7 @@ planarwrappers com.github.jikoo - 3.2.2 + 3.2.3