[Idea]: Folia support for OpenInv #196
84
internal/v1_18_R2/pom.xml
Normal file
84
internal/v1_18_R2/pom.xml
Normal file
@@ -0,0 +1,84 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
~ Copyright (C) 2011-2022 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 <http://www.gnu.org/licenses/>.
|
||||
-->
|
||||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<artifactId>openinvparent</artifactId>
|
||||
<groupId>com.lishid</groupId>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
<version>4.1.10-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>openinvadapter1_18_R2</artifactId>
|
||||
<name>OpenInvAdapter1_18_R2</name>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<spigot.version>1.18.2-R0.1-SNAPSHOT</spigot.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.spigotmc</groupId>
|
||||
<artifactId>spigot-api</artifactId>
|
||||
<version>${spigot.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<artifactId>spigot</artifactId>
|
||||
<groupId>org.spigotmc</groupId>
|
||||
<scope>provided</scope>
|
||||
<version>${spigot.version}</version>
|
||||
<classifier>remapped-mojang</classifier>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<artifactId>openinvapi</artifactId>
|
||||
<groupId>com.lishid</groupId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<artifactId>openinvplugincore</artifactId>
|
||||
<groupId>com.lishid</groupId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<artifactId>annotations</artifactId>
|
||||
<groupId>org.jetbrains</groupId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<!-- Cannot use minimizeJar until maven-shade-plugin 3.3.0 releases; ASM used does not support Java 17. -->
|
||||
<configuration>
|
||||
<minimizeJar>false</minimizeJar>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>net.md-5</groupId>
|
||||
<artifactId>specialsource-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@@ -0,0 +1,281 @@
|
||||
/*
|
||||
* Copyright (C) 2011-2022 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.lishid.openinv.internal.v1_18_R2;
|
||||
|
||||
import com.lishid.openinv.OpenInv;
|
||||
import com.lishid.openinv.internal.IAnySilentContainer;
|
||||
import com.lishid.openinv.util.ReflectionHelper;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Optional;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.network.chat.TranslatableComponent;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.server.level.ServerPlayerGameMode;
|
||||
import net.minecraft.world.CompoundContainer;
|
||||
import net.minecraft.world.MenuProvider;
|
||||
import net.minecraft.world.SimpleMenuProvider;
|
||||
import net.minecraft.world.entity.monster.Shulker;
|
||||
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.DoubleBlockCombiner;
|
||||
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.ChestBlockEntity;
|
||||
import net.minecraft.world.level.block.entity.EnderChestBlockEntity;
|
||||
import net.minecraft.world.level.block.entity.RandomizableContainerBlockEntity;
|
||||
import net.minecraft.world.level.block.entity.ShulkerBoxBlockEntity;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraft.world.phys.AABB;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Statistic;
|
||||
import org.bukkit.block.ShulkerBox;
|
||||
import org.bukkit.craftbukkit.v1_18_R2.CraftWorld;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.InventoryView;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class AnySilentContainer implements IAnySilentContainer {
|
||||
|
||||
private @Nullable Field serverPlayerGameModeGameType;
|
||||
|
||||
public AnySilentContainer() {
|
||||
try {
|
||||
try {
|
||||
// IDE warns about field not existing, but SpecialSource does not remap strings used in reflection.
|
||||
// The warning is not suppressed as a reminder that it must manually be checked on updates.
|
||||
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(Level.WARNING, "Error obtaining GameType field", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isShulkerBlocked(ShulkerBox box) {
|
||||
org.bukkit.World bukkitWorld = box.getWorld();
|
||||
if (!(bukkitWorld instanceof CraftWorld)) {
|
||||
bukkitWorld = Bukkit.getWorld(bukkitWorld.getUID());
|
||||
}
|
||||
|
||||
if (!(bukkitWorld instanceof CraftWorld craftWorld)) {
|
||||
Exception exception = new IllegalStateException("AnySilentContainer access attempted on an unknown world!");
|
||||
OpenInv.getPlugin(OpenInv.class).getLogger().log(Level.WARNING, exception.getMessage(), exception);
|
||||
return false;
|
||||
}
|
||||
|
||||
final ServerLevel world = craftWorld.getHandle();
|
||||
final BlockPos blockPosition = new BlockPos(box.getX(), box.getY(), box.getZ());
|
||||
final BlockEntity tile = world.getBlockEntity(blockPosition);
|
||||
|
||||
if (!(tile instanceof ShulkerBoxBlockEntity shulkerBoxBlockEntity)
|
||||
|| shulkerBoxBlockEntity.getAnimationStatus() != ShulkerBoxBlockEntity.AnimationStatus.CLOSED) {
|
||||
return false;
|
||||
}
|
||||
|
||||
BlockState blockState = world.getBlockState(blockPosition);
|
||||
|
||||
// See net.minecraft.world.level.block.ShulkerBoxBlock#canOpen
|
||||
AABB boundingBox = Shulker.getProgressDeltaAabb(blockState.getValue(ShulkerBoxBlock.FACING), 0.0F, 0.5F)
|
||||
.move(blockPosition)
|
||||
.deflate(1.0E-6D);
|
||||
return !world.noCollision(boundingBox);
|
||||
}
|
||||
|
||||
@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 ServerLevel level = player.getLevel();
|
||||
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);
|
||||
}, new TranslatableComponent("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
|
||||
Optional<MenuProvider> menuOptional = chestBlock.combine(blockState, level, blockPos, true).apply(
|
||||
// Combiner is a copy of private ChestBlock.MENU_PROVIDER_COMBINER
|
||||
new DoubleBlockCombiner.Combiner<ChestBlockEntity, Optional<MenuProvider>>() {
|
||||
@Override
|
||||
public Optional<MenuProvider> acceptDouble(ChestBlockEntity localChest1, ChestBlockEntity localChest2) {
|
||||
CompoundContainer doubleChest = new CompoundContainer(localChest1, localChest2);
|
||||
return Optional.of(new ChestBlock.DoubleInventory(localChest1, localChest2, doubleChest));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<MenuProvider> acceptSingle(ChestBlockEntity localChest) {
|
||||
return Optional.of(localChest);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<MenuProvider> acceptNone() {
|
||||
return Optional.empty();
|
||||
}
|
||||
});
|
||||
|
||||
if (menuOptional.isEmpty()) {
|
||||
OpenInv.getPlugin(OpenInv.class).sendSystemMessage(bukkitPlayer, "messages.error.lootNotGenerated");
|
||||
return false;
|
||||
}
|
||||
|
||||
menuProvider = menuOptional.get();
|
||||
|
||||
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) {
|
||||
return;
|
||||
}
|
||||
|
||||
InventoryView view = bukkitPlayer.getOpenInventory();
|
||||
switch (view.getType()) {
|
||||
case CHEST:
|
||||
case ENDER_CHEST:
|
||||
case SHULKER_BOX:
|
||||
case BARREL:
|
||||
break;
|
||||
default:
|
||||
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) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright (C) 2011-2022 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.lishid.openinv.internal.v1_18_R2;
|
||||
|
||||
import java.io.File;
|
||||
import net.minecraft.Util;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.NbtIo;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.world.level.storage.PlayerDataStorage;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.bukkit.craftbukkit.v1_18_R2.CraftServer;
|
||||
import org.bukkit.craftbukkit.v1_18_R2.entity.CraftPlayer;
|
||||
|
||||
public class OpenPlayer extends CraftPlayer {
|
||||
|
||||
public OpenPlayer(CraftServer server, ServerPlayer entity) {
|
||||
super(server, entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadData() {
|
||||
// See CraftPlayer#loadData
|
||||
CompoundTag loaded = this.server.getHandle().playerIo.load(this.getHandle());
|
||||
if (loaded != null) {
|
||||
readExtraData(loaded);
|
||||
}
|
||||
}
|
||||
|
||||
@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 playerData = player.saveWithoutId(new CompoundTag());
|
||||
setExtraData(playerData);
|
||||
|
||||
if (!isOnline()) {
|
||||
// Special case: save old vehicle data
|
||||
CompoundTag oldData = worldNBTStorage.load(player);
|
||||
|
||||
if (oldData != null && oldData.contains("RootVehicle", 10)) {
|
||||
// See net.minecraft.server.PlayerList#a(NetworkManager, EntityPlayer) and net.minecraft.server.EntityPlayer#b(NBTTagCompound)
|
||||
playerData.put("RootVehicle", oldData.getCompound("RootVehicle"));
|
||||
}
|
||||
}
|
||||
|
||||
File file = File.createTempFile(player.getStringUUID() + "-", ".dat", worldNBTStorage.getPlayerDir());
|
||||
NbtIo.writeCompressed(playerData, file);
|
||||
File file1 = new File(worldNBTStorage.getPlayerDir(), player.getStringUUID() + ".dat");
|
||||
File file2 = new File(worldNBTStorage.getPlayerDir(), player.getStringUUID() + ".dat_old");
|
||||
Util.safeReplaceFile(file1, file, file2);
|
||||
} catch (Exception e) {
|
||||
LogManager.getLogger().warn("Failed to save player data for {}", player.getName().getString());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,230 @@
|
||||
/*
|
||||
* Copyright (C) 2011-2022 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.lishid.openinv.internal.v1_18_R2;
|
||||
|
||||
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 java.lang.reflect.Field;
|
||||
import java.util.logging.Logger;
|
||||
import net.minecraft.network.chat.TextComponent;
|
||||
import net.minecraft.network.protocol.game.ClientboundOpenScreenPacket;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.inventory.AbstractContainerMenu;
|
||||
import net.minecraft.world.inventory.MenuType;
|
||||
import net.minecraft.world.level.Level;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.craftbukkit.v1_18_R2.CraftServer;
|
||||
import org.bukkit.craftbukkit.v1_18_R2.entity.CraftPlayer;
|
||||
import org.bukkit.craftbukkit.v1_18_R2.event.CraftEventFactory;
|
||||
import org.bukkit.craftbukkit.v1_18_R2.inventory.CraftContainer;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.InventoryView;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class PlayerDataManager implements IPlayerDataManager {
|
||||
|
||||
private @Nullable Field bukkitEntity;
|
||||
|
||||
public PlayerDataManager() {
|
||||
try {
|
||||
bukkitEntity = Entity.class.getDeclaredField("bukkitEntity");
|
||||
} catch (NoSuchFieldException e) {
|
||||
Logger logger = OpenInv.getPlugin(OpenInv.class).getLogger();
|
||||
logger.warning("Unable to obtain field to inject custom save process - players' mounts may be deleted when loaded.");
|
||||
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 provided Player implementation");
|
||||
}
|
||||
|
||||
return nmsPlayer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Player loadPlayer(@NotNull final OfflinePlayer offline) {
|
||||
// Ensure player has data
|
||||
if (!offline.hasPlayedBefore()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Create a profile and entity to load the player data
|
||||
// See net.minecraft.server.PlayerList#attemptLogin
|
||||
GameProfile profile = new GameProfile(offline.getUniqueId(),
|
||||
offline.getName() != null ? offline.getName() : offline.getUniqueId().toString());
|
||||
MinecraftServer server = ((CraftServer) Bukkit.getServer()).getServer();
|
||||
ServerLevel worldServer = server.getLevel(Level.OVERWORLD);
|
||||
|
||||
if (worldServer == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
ServerPlayer entity = new ServerPlayer(server, worldServer, profile);
|
||||
|
||||
try {
|
||||
injectPlayer(entity);
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// Get the bukkit entity
|
||||
Player target = entity.getBukkitEntity();
|
||||
if (target != null) {
|
||||
// Load data
|
||||
target.loadData();
|
||||
}
|
||||
// Return the entity
|
||||
return target;
|
||||
}
|
||||
|
||||
void injectPlayer(ServerPlayer player) throws IllegalAccessException {
|
||||
if (bukkitEntity == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
bukkitEntity.setAccessible(true);
|
||||
|
||||
bukkitEntity.set(player, new OpenPlayer(player.server.server, player));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Player inject(@NotNull Player player) {
|
||||
try {
|
||||
ServerPlayer nmsPlayer = getHandle(player);
|
||||
injectPlayer(nmsPlayer);
|
||||
return nmsPlayer.getBukkitEntity();
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
return player;
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public 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(new TextComponent(view.getTitle()));
|
||||
container = CraftEventFactory.callInventoryOpenEvent(nmsPlayer, container);
|
||||
|
||||
if (container == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
nmsPlayer.connection.send(new ClientboundOpenScreenPacket(container.containerId, container.getType(),
|
||||
new TextComponent(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;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,344 @@
|
||||
/*
|
||||
* Copyright (C) 2011-2022 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.lishid.openinv.internal.v1_18_R2;
|
||||
|
||||
import com.lishid.openinv.internal.ISpecialEnderChest;
|
||||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
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_18_R2.entity.CraftHumanEntity;
|
||||
import org.bukkit.craftbukkit.v1_18_R2.inventory.CraftInventory;
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.inventory.InventoryHolder;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class SpecialEnderChest extends PlayerEnderChestContainer implements ISpecialEnderChest {
|
||||
|
||||
private final CraftInventory inventory;
|
||||
private ServerPlayer owner;
|
||||
private NonNullList<ItemStack> 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 boolean isInUse() {
|
||||
return !this.getViewers().isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPlayerOffline() {
|
||||
this.playerOnline = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPlayerOnline(@NotNull final org.bukkit.entity.Player player) {
|
||||
if (!this.playerOnline) {
|
||||
try {
|
||||
this.owner = PlayerDataManager.getHandle(player);
|
||||
PlayerEnderChestContainer enderChest = owner.getEnderChestInventory();
|
||||
for (int i = 0; i < enderChest.getContainerSize(); ++i) {
|
||||
enderChest.setItem(i, this.items.get(i));
|
||||
}
|
||||
this.items = enderChest.items;
|
||||
enderChest.transaction.addAll(this.transaction);
|
||||
} catch (Exception ignored) {}
|
||||
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<ItemStack> 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<HumanEntity> 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.isSameItemSameTags(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.isSameItemSameTags(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<ItemStack> removeAllItems() {
|
||||
List<ItemStack> 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()).collect(Collectors.toList()).toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromTag(ListTag listTag) {
|
||||
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.of(compoundTag));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,794 @@
|
||||
/*
|
||||
* Copyright (C) 2011-2022 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.lishid.openinv.internal.v1_18_R2;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.lishid.openinv.internal.ISpecialPlayerInventory;
|
||||
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;
|
||||
import net.minecraft.CrashReport;
|
||||
import net.minecraft.CrashReportCategory;
|
||||
import net.minecraft.ReportedException;
|
||||
import net.minecraft.core.NonNullList;
|
||||
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.damagesource.DamageSource;
|
||||
import net.minecraft.world.entity.EquipmentSlot;
|
||||
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.ArmorItem;
|
||||
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_18_R2.entity.CraftHumanEntity;
|
||||
import org.bukkit.craftbukkit.v1_18_R2.inventory.CraftInventory;
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.inventory.InventoryHolder;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class SpecialPlayerInventory extends Inventory implements ISpecialPlayerInventory {
|
||||
|
||||
private final CraftInventory inventory;
|
||||
private boolean playerOnline;
|
||||
private Player player;
|
||||
private NonNullList<ItemStack> items;
|
||||
private NonNullList<ItemStack> armor;
|
||||
private NonNullList<ItemStack> offhand;
|
||||
private List<NonNullList<ItemStack>> 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) {
|
||||
Player entityPlayer = PlayerDataManager.getHandle(player);
|
||||
entityPlayer.getInventory().transaction.addAll(this.transaction);
|
||||
this.player = entityPlayer;
|
||||
for (int i = 0; i < getContainerSize(); ++i) {
|
||||
this.player.getInventory().setItem(i, getRawItem(i));
|
||||
}
|
||||
this.player.getInventory().selected = this.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);
|
||||
this.playerOnline = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull CraftInventory getBukkitInventory() {
|
||||
return this.inventory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPlayerOffline() {
|
||||
this.playerOnline = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInUse() {
|
||||
List<HumanEntity> viewers = this.getViewers();
|
||||
return viewers.size() > 1 || !viewers.isEmpty() && !viewers.get(0).getUniqueId().equals(this.player.getUUID());
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull HumanEntity getPlayer() {
|
||||
return this.player.getBukkitEntity();
|
||||
}
|
||||
|
||||
private @NotNull ItemStack getRawItem(int i) {
|
||||
if (i < 0) {
|
||||
return ItemStack.EMPTY;
|
||||
}
|
||||
|
||||
NonNullList<ItemStack> list;
|
||||
for (Iterator<NonNullList<ItemStack>> 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<ItemStack> list;
|
||||
for (Iterator<NonNullList<ItemStack>> iterator = this.compartments.iterator(); iterator.hasNext(); i -= list.size()) {
|
||||
list = iterator.next();
|
||||
if (i < list.size()) {
|
||||
list.set(i, itemStack);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static record IndexedCompartment(@Nullable NonNullList<ItemStack> 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;
|
||||
}
|
||||
|
||||
private boolean contains(Predicate<ItemStack> predicate) {
|
||||
return this.compartments.stream().flatMap(NonNullList::stream).anyMatch(predicate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ItemStack> 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<HumanEntity> 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<ItemStack> 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.isSameItemSameTags(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.isSameItemSameTags(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.isSameItemSameTags(itemStack, this.items.get(i)) && !this.items.get(i).isDamaged() && !localItem.isEnchanted() && !localItem.hasCustomHoverName()) {
|
||||
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) {
|
||||
if (d0 > 0.0D) {
|
||||
d0 = 1.0D;
|
||||
}
|
||||
|
||||
if (d0 < 0.0D) {
|
||||
d0 = -1.0D;
|
||||
}
|
||||
|
||||
this.selected = (int) (this.selected - d0);
|
||||
|
||||
while (this.selected < 0) {
|
||||
this.selected += 9;
|
||||
}
|
||||
|
||||
while(this.selected >= 9) {
|
||||
this.selected -= 9;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int clearOrCountMatchingItems(Predicate<ItemStack> 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) {
|
||||
Item item = itemstack.getItem();
|
||||
int j = itemstack.getCount();
|
||||
ItemStack localItemStack = this.getRawItem(i);
|
||||
if (localItemStack.isEmpty()) {
|
||||
localItemStack = new ItemStack(item, 0);
|
||||
if (itemstack.hasTag()) {
|
||||
// hasTag ensures tag not null
|
||||
//noinspection ConstantConditions
|
||||
localItemStack.setTag(itemstack.getTag().copy());
|
||||
}
|
||||
|
||||
this.setRawItem(i, localItemStack);
|
||||
}
|
||||
|
||||
int k = Math.min(j, localItemStack.getMaxStackSize() - localItemStack.getCount());
|
||||
|
||||
if (k > this.getMaxStackSize() - localItemStack.getCount()) {
|
||||
k = this.getMaxStackSize() - 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<ItemStack> 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.getAbilities().instabuild) {
|
||||
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.getAbilities().instabuild) {
|
||||
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<ItemStack> 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(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(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(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.of(compoundTag);
|
||||
if (!itemstack.isEmpty()) {
|
||||
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 hurtArmor(DamageSource damagesource, float damage, int[] armorIndices) {
|
||||
if (damage > 0.0F) {
|
||||
damage /= 4.0F;
|
||||
if (damage < 1.0F) {
|
||||
damage = 1.0F;
|
||||
}
|
||||
|
||||
for (int index : armorIndices) {
|
||||
ItemStack itemstack = this.armor.get(index);
|
||||
if ((!damagesource.isFire() || !itemstack.getItem().isFireResistant()) && itemstack.getItem() instanceof ArmorItem) {
|
||||
itemstack.hurtAndBreak((int) damage, this.player, localPlayer -> localPlayer.broadcastBreakEvent(EquipmentSlot.byTypeAndIndex(EquipmentSlot.Type.ARMOR, index)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dropAll() {
|
||||
for (NonNullList<ItemStack> 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.sameItem(itemstack));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(TagKey<Item> tagKey) {
|
||||
|
||||
return contains(itemStack -> !itemStack.isEmpty() && itemStack.is(tagKey));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void replaceWith(Inventory inventory) {
|
||||
Function<Integer, ItemStack> 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<ItemStack> 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);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user