diff --git a/README.MD b/README.MD index 66956203..7354892e 100644 --- a/README.MD +++ b/README.MD @@ -49,11 +49,9 @@ As OpenInv is compiled against Mojang's mappings, you must run BuildTools with t `java -jar BuildTools.jar --remapped --rev $serverVersion` `$serverVersion` is the version of the server, i.e. `1.18.1` -To compile for a single version, specify the module you are targeting: -`mvn -pl $moduleName -am clean install` -`$moduleName` is the name of the module, i.e. `internal/v1_18_R1`. +To compile just the API, execute Maven as usual: +`mvn clean package` -To compile for a set of versions, use a profile. Select a profile using the `-P` argument: +To compile the full plugin, use the provided profile with `-P all`: `mvn clean package -am -P all` -The only provided profile is `all`. The final file is `target/OpenInv.jar` For more information, check out the [official Maven guide](http://maven.apache.org/guides/introduction/introduction-to-profiles.html). diff --git a/api/src/main/java/com/lishid/openinv/IOpenInv.java b/api/src/main/java/com/lishid/openinv/IOpenInv.java index d1e0155f..7c4e1bc8 100644 --- a/api/src/main/java/com/lishid/openinv/IOpenInv.java +++ b/api/src/main/java/com/lishid/openinv/IOpenInv.java @@ -17,21 +17,19 @@ package com.lishid.openinv; import com.lishid.openinv.internal.IAnySilentContainer; -import com.lishid.openinv.internal.IInventoryAccess; import com.lishid.openinv.internal.ISpecialEnderChest; import com.lishid.openinv.internal.ISpecialInventory; import com.lishid.openinv.internal.ISpecialPlayerInventory; -import com.lishid.openinv.util.InventoryAccess; -import java.util.UUID; -import java.util.logging.Logger; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; import org.bukkit.inventory.InventoryView; -import org.bukkit.plugin.Plugin; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.UUID; +import java.util.logging.Logger; + /** * Interface defining behavior for the OpenInv plugin. */ @@ -80,22 +78,6 @@ public interface IOpenInv { */ @NotNull IAnySilentContainer getAnySilentContainer(); - /** - * @deprecated Use static {@link InventoryAccess} methods. - */ - @Deprecated(forRemoval = true) - default @NotNull IInventoryAccess getInventoryAccess() { - return new InventoryAccess(); - } - - /** - * @deprecated Use {@link #getAnyContainerStatus(OfflinePlayer)}. Not all containers are chests. - */ - @Deprecated(forRemoval = true, since = "4.2.0") - default boolean getPlayerAnyChestStatus(@NotNull OfflinePlayer offline) { - return getAnyContainerStatus(offline); - } - /** * Get whether a user has AnyContainer mode enabled. * @@ -104,14 +86,6 @@ default boolean getPlayerAnyChestStatus(@NotNull OfflinePlayer offline) { */ boolean getAnyContainerStatus(@NotNull OfflinePlayer offline); - /** - * @deprecated Use {@link #setAnyContainerStatus(OfflinePlayer, boolean)}. Not all containers are chests. - */ - @Deprecated(forRemoval = true, since = "4.2.0") - default void setPlayerAnyChestStatus(@NotNull OfflinePlayer offline, boolean status) { - setAnyContainerStatus(offline, status); - } - /** * Set whether a user has AnyContainer mode enabled. * @@ -120,14 +94,6 @@ default void setPlayerAnyChestStatus(@NotNull OfflinePlayer offline, boolean sta */ void setAnyContainerStatus(@NotNull OfflinePlayer offline, boolean status); - /** - * @deprecated Use {@link #getSilentContainerStatus(OfflinePlayer)}. Not all containers are chests. - */ - @Deprecated(forRemoval = true, since = "4.2.0") - default boolean getPlayerSilentChestStatus(@NotNull OfflinePlayer offline) { - return getSilentContainerStatus(offline); - } - /** * Get whether a user has SilentContainer mode enabled. * @@ -136,14 +102,6 @@ default boolean getPlayerSilentChestStatus(@NotNull OfflinePlayer offline) { */ boolean getSilentContainerStatus(@NotNull OfflinePlayer offline); - /** - * @deprecated Use {@link #setSilentContainerStatus(OfflinePlayer, boolean)}. Not all containers are chests. - */ - @Deprecated(forRemoval = true, since = "4.2.0") - default void setPlayerSilentChestStatus(@NotNull OfflinePlayer offline, boolean status) { - setSilentContainerStatus(offline, status); - } - /** * Set whether a user has SilentContainer mode enabled. * @@ -152,20 +110,6 @@ default void setPlayerSilentChestStatus(@NotNull OfflinePlayer offline, boolean */ void setSilentContainerStatus(@NotNull OfflinePlayer offline, boolean status); - /** - * Get a unique identifier by which the OfflinePlayer can be referenced. - * - * @deprecated Use {@link OfflinePlayer#getUniqueId()} and {@link UUID#toString()}. This was necessary for non-UUID - * versions of Minecraft, but support for them has been dropped for years. - * @param offline the OfflinePlayer - * @return the identifier - * @throws IllegalStateException if the server version is unsupported - */ - @Deprecated(forRemoval = true) - default @NotNull String getPlayerID(@NotNull OfflinePlayer offline) { - return offline.getUniqueId().toString(); - } - /** * Get an {@link ISpecialEnderChest} for a user. * @@ -229,55 +173,6 @@ default void setPlayerSilentChestStatus(@NotNull OfflinePlayer offline, boolean */ @Nullable OfflinePlayer matchPlayer(@NotNull String name); - /** - * @deprecated OpenInv uses action bar chat for notifications. Whether they show is based on language settings. - */ - @Deprecated(forRemoval = true) - default boolean notifyAnyChest() { - return true; - } - - /** - * @deprecated OpenInv uses action bar chat for notifications. Whether they show is based on language settings. - */ - @Deprecated(forRemoval = true) - default boolean notifySilentChest() { - return true; - } - - /** - * @deprecated see {@link #retainPlayer(Player, Plugin)} - */ - @Deprecated(forRemoval = true, since = "4.2.0") - default void releasePlayer(@NotNull Player player, @NotNull Plugin plugin) {} - - /** - * @deprecated OpenInv no longer uses an internal cache beyond maintaining copies of currently open inventories. - * If you wish to use/modify a player, ensure either {@link IOpenInv#isPlayerLoaded(UUID)} is false or the player - * instance is the same memory address as the one in use by OpenInv. - *
-     *  public @NotNull Player savePlayerData(@NotNull Player player) {
-     *     IOpenInv openInv = ...
-     *     if (!openInv.disableSaving() && openInv.isPlayerLoaded(player.getUniqueId())) {
-     *         Player openInvLoadedPlayer = openInv.loadPlayer(myInUsePlayer);
-     *         if (openInvLoadedPlayer != player) {
-     *             // The copy loaded by OpenInv is not the same as our loaded copy. Push our changes.
-     *             copyPlayerModifications(player, openInvLoadedPlayer);
-     *         }
-     *         // OpenInv will handle saving data when the player is unloaded.
-     *         // Optionally, to be sure our changes will persist, save now.
-     *         // openInvLoadedPlayer.saveData();
-     *         return openInvLoadedPlayer;
-     *     }
-     *
-     *     player.saveData();
-     *     return player;
-     * }
-     * 
- */ - @Deprecated(forRemoval = true, since = "4.2.0") - default void retainPlayer(@NotNull Player player, @NotNull Plugin plugin) {} - /** * Forcibly close inventories of and unload any cached data for a user. * diff --git a/api/src/main/java/com/lishid/openinv/event/OpenPlayerSaveEvent.java b/api/src/main/java/com/lishid/openinv/event/OpenPlayerSaveEvent.java index a36a6c96..f20c635a 100644 --- a/api/src/main/java/com/lishid/openinv/event/OpenPlayerSaveEvent.java +++ b/api/src/main/java/com/lishid/openinv/event/OpenPlayerSaveEvent.java @@ -1,35 +1,34 @@ package com.lishid.openinv.event; +import com.google.errorprone.annotations.RestrictedApi; import com.lishid.openinv.internal.ISpecialInventory; import org.bukkit.entity.Player; -import org.bukkit.event.Cancellable; -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; /** - * Event fired before OpenInv saves a player's data. + * Event fired before OpenInv saves a player's data when closing an {@link ISpecialInventory}. */ -public class OpenPlayerSaveEvent extends Event implements Cancellable { +public class OpenPlayerSaveEvent extends PlayerSaveEvent { - private static final HandlerList HANDLERS = new HandlerList(); - - private final Player player; private final ISpecialInventory inventory; - private boolean cancelled = false; - - public OpenPlayerSaveEvent(@NotNull Player player, @NotNull ISpecialInventory inventory) { - this.player = player; - this.inventory = inventory; - } /** - * Get the {@link Player} whose data is being saved. + * Construct a new {@code OpenPlayerSaveEvent}. + * + *

The constructor is not considered part of the API, and may be subject to change.

* - * @return player the Player whose data is being saved + * @param player the player to be saved + * @param inventory the {@link ISpecialInventory} being closed */ - public @NotNull Player getPlayer() { - return player; + @RestrictedApi( + explanation = "Constructor is not considered part of the API and may be subject to change.", + link = "", + allowedOnPath = ".*/com/lishid/openinv/event/OpenEvents.java") + @ApiStatus.Internal + OpenPlayerSaveEvent(@NotNull Player player, @NotNull ISpecialInventory inventory) { + super(player); + this.inventory = inventory; } /** @@ -41,34 +40,4 @@ public OpenPlayerSaveEvent(@NotNull Player player, @NotNull ISpecialInventory in return inventory; } - /** - * Get whether the event is cancelled. - * - * @return true if the event is cancelled - */ - @Override - public boolean isCancelled() { - return cancelled; - } - - /** - * Set whether the event is cancelled. - * - * @param cancel whether the event is cancelled - */ - @Override - public void setCancelled(boolean cancel) { - this.cancelled = cancel; - } - - @NotNull - @Override - public HandlerList getHandlers() { - return HANDLERS; - } - - public static HandlerList getHandlerList() { - return HANDLERS; - } - } diff --git a/api/src/main/java/com/lishid/openinv/event/PlayerSaveEvent.java b/api/src/main/java/com/lishid/openinv/event/PlayerSaveEvent.java new file mode 100644 index 00000000..4ca1d715 --- /dev/null +++ b/api/src/main/java/com/lishid/openinv/event/PlayerSaveEvent.java @@ -0,0 +1,67 @@ +package com.lishid.openinv.event; + + +import com.google.errorprone.annotations.RestrictedApi; +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.HandlerList; +import org.bukkit.event.player.PlayerEvent; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NotNull; + +/** + * Event fired before a {@link Player} loaded via OpenInv is saved. + */ +public class PlayerSaveEvent extends PlayerEvent implements Cancellable { + + private static final HandlerList HANDLERS = new HandlerList(); + + private boolean cancelled = false; + + /** + * Construct a new {@code PlayerSaveEvent}. + * + *

The constructor is not considered part of the API, and may be subject to change.

+ * + * @param player the player to be saved + */ + @RestrictedApi( + explanation = "Constructor is not considered part of the API and may be subject to change.", + link = "", + allowedOnPath = ".*/com/lishid/openinv/event/(OpenPlayerSaveEvent|OpenEvents).java") + @ApiStatus.Internal + PlayerSaveEvent(@NotNull Player player) { + super(player); + } + + /** + * Get whether the event is cancelled. + * + * @return true if the event is cancelled + */ + @Override + public boolean isCancelled() { + return cancelled; + } + + /** + * Set whether the event is cancelled. + * + * @param cancel whether the event is cancelled + */ + @Override + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + } + + @NotNull + @Override + public HandlerList getHandlers() { + return HANDLERS; + } + + public static HandlerList getHandlerList() { + return HANDLERS; + } + +} diff --git a/api/src/main/java/com/lishid/openinv/internal/IAnySilentContainer.java b/api/src/main/java/com/lishid/openinv/internal/IAnySilentContainer.java index 99026bc3..d4bccb91 100644 --- a/api/src/main/java/com/lishid/openinv/internal/IAnySilentContainer.java +++ b/api/src/main/java/com/lishid/openinv/internal/IAnySilentContainer.java @@ -51,17 +51,6 @@ public interface IAnySilentContainer { */ void deactivateContainer(@NotNull Player player); - /** - * @param player the player opening the container - * @param block the {@link Block} of the container - * @return true if the container is blocked - * @deprecated use {@link #isAnyContainerNeeded(Block)} - */ - @Deprecated(forRemoval = true, since = "4.1.9") - default boolean isAnyContainerNeeded(@NotNull Player player, @NotNull Block block) { - return isAnyContainerNeeded(block); - } - /** * Check if the container at the given coordinates is blocked. * @@ -99,9 +88,14 @@ default boolean isAnyContainerNeeded(@NotNull Block block) { return false; } - int ordinal = (chest.getFacing().ordinal() + 4 + (chest.getType() == Chest.Type.RIGHT ? -1 : 1)) % 4; - BlockFace relativeFace = BlockFace.values()[ordinal]; - org.bukkit.block.Block relative = block.getRelative(relativeFace); + BlockFace relativeFace = switch (chest.getFacing()) { + case NORTH -> chest.getType() == Chest.Type.RIGHT ? BlockFace.WEST : BlockFace.EAST; + case EAST -> chest.getType() == Chest.Type.RIGHT ? BlockFace.NORTH : BlockFace.SOUTH; + case SOUTH -> chest.getType() == Chest.Type.RIGHT ? BlockFace.EAST : BlockFace.WEST; + case WEST -> chest.getType() == Chest.Type.RIGHT ? BlockFace.SOUTH : BlockFace.NORTH; + default -> BlockFace.SELF; + }; + Block relative = block.getRelative(relativeFace); if (relative.getType() != block.getType()) { return false; @@ -120,18 +114,6 @@ default boolean isAnyContainerNeeded(@NotNull Block block) { return isChestBlocked(relative); } - /** - * Check if a {@link ShulkerBox} cannot be opened under ordinary circumstances. - * - * @deprecated Use {@link #isShulkerBlocked(Block)}. - * @param shulkerBox the shulker box container - * @return whether the container is blocked - */ - @Deprecated(since = "4.4.4", forRemoval = true) - default boolean isShulkerBlocked(@NotNull ShulkerBox shulkerBox) { - return isShulkerBlocked(shulkerBox.getBlock()); - } - /** * Check if a shulker box block cannot be opened under ordinary circumstances. * @@ -176,7 +158,7 @@ default boolean isAnySilentContainer(@NotNull Block block) { * @return true if the type is a supported container */ default boolean isAnySilentContainer(@NotNull BlockState blockState) { - return blockState instanceof InventoryHolder holder && isAnySilentContainer(holder) + return (blockState instanceof InventoryHolder holder && isAnySilentContainer(holder)) || blockState instanceof EnderChest; } diff --git a/api/src/main/java/com/lishid/openinv/internal/IInventoryAccess.java b/api/src/main/java/com/lishid/openinv/internal/IInventoryAccess.java deleted file mode 100644 index 7f29a070..00000000 --- a/api/src/main/java/com/lishid/openinv/internal/IInventoryAccess.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * 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 . - */ - -package com.lishid.openinv.internal; - -import com.lishid.openinv.util.InventoryAccess; -import org.bukkit.inventory.Inventory; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -/** - * @deprecated Use static {@link InventoryAccess} methods. - */ -@Deprecated(forRemoval = true) -public interface IInventoryAccess { - - /** - * @deprecated Use static {@link InventoryAccess} methods. - */ - @Deprecated(forRemoval = true) - default @Nullable ISpecialEnderChest getSpecialEnderChest(@NotNull Inventory inventory) { - return InventoryAccess.getEnderChest(inventory); - } - - /** - * @deprecated Use static {@link InventoryAccess} methods. - */ - @Deprecated(forRemoval = true) - default @Nullable ISpecialPlayerInventory getSpecialPlayerInventory(@NotNull Inventory inventory) { - return InventoryAccess.getPlayerInventory(inventory); - } - - /** - * @deprecated Use static {@link InventoryAccess} methods. - */ - @Deprecated(forRemoval = true) - default boolean isSpecialEnderChest(@NotNull Inventory inventory) { - return InventoryAccess.isEnderChest(inventory); - } - - /** - * @deprecated Use static {@link InventoryAccess} methods. - */ - @Deprecated(forRemoval = true) - default boolean isSpecialPlayerInventory(@NotNull Inventory inventory) { - return InventoryAccess.isPlayerInventory(inventory); - } - -} diff --git a/api/src/main/java/com/lishid/openinv/util/InventoryAccess.java b/api/src/main/java/com/lishid/openinv/util/InventoryAccess.java index e05bbe1e..466cae61 100644 --- a/api/src/main/java/com/lishid/openinv/util/InventoryAccess.java +++ b/api/src/main/java/com/lishid/openinv/util/InventoryAccess.java @@ -16,40 +16,23 @@ package com.lishid.openinv.util; -import com.lishid.openinv.internal.IInventoryAccess; +import com.google.errorprone.annotations.RestrictedApi; import com.lishid.openinv.internal.ISpecialEnderChest; import com.lishid.openinv.internal.ISpecialInventory; import com.lishid.openinv.internal.ISpecialPlayerInventory; -import org.bukkit.Bukkit; import org.bukkit.inventory.Inventory; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.lang.reflect.Method; +import java.util.function.BiFunction; -public final class InventoryAccess implements IInventoryAccess { +public final class InventoryAccess { - private static Class craftInventory = null; - private static Method getInventory = null; - - static { - String packageName = Bukkit.getServer().getClass().getPackage().getName(); - try { - craftInventory = Class.forName(packageName + ".inventory.CraftInventory"); - getInventory = craftInventory.getDeclaredMethod("getInventory"); - } catch (ClassNotFoundException | NoSuchMethodException ignored) {} - } - - /** - * @deprecated use {@link #isUsable()} - */ - @Deprecated(forRemoval = true) - public static boolean isUseable() { - return isUsable(); - } + private static @Nullable BiFunction, ISpecialInventory> provider; public static boolean isUsable() { - return craftInventory != null && getInventory != null; + return provider != null; } /** @@ -70,7 +53,7 @@ public static boolean isPlayerInventory(@NotNull Inventory inventory) { * @return the backing implementation if available */ public static @Nullable ISpecialPlayerInventory getPlayerInventory(@NotNull Inventory inventory) { - return getSpecialInventory(ISpecialPlayerInventory.class, inventory); + return provider == null ? null : (ISpecialPlayerInventory) provider.apply(inventory, ISpecialPlayerInventory.class); } /** @@ -91,7 +74,7 @@ public static boolean isEnderChest(@NotNull Inventory inventory) { * @return the backing implementation if available */ public static @Nullable ISpecialEnderChest getEnderChest(@NotNull Inventory inventory) { - return getSpecialInventory(ISpecialEnderChest.class, inventory); + return provider == null ? null : (ISpecialEnderChest) provider.apply(inventory, ISpecialEnderChest.class); } /** @@ -102,34 +85,20 @@ public static boolean isEnderChest(@NotNull Inventory inventory) { * @return the backing implementation if available */ public static @Nullable ISpecialInventory getInventory(@NotNull Inventory inventory) { - return getSpecialInventory(ISpecialInventory.class, inventory); + return provider == null ? null : provider.apply(inventory, ISpecialInventory.class); } - private static @Nullable T getSpecialInventory(@NotNull Class expected, @NotNull Inventory inventory) { - Object inv; - if (isUsable() && craftInventory.isAssignableFrom(inventory.getClass())) { - try { - inv = getInventory.invoke(inventory); - if (expected.isInstance(inv)) { - return expected.cast(inv); - } - } catch (ReflectiveOperationException ignored) {} - } - - // Use reflection to find the IInventory - inv = ReflectionHelper.grabObjectByType(inventory, expected); - - if (expected.isInstance(inv)) { - return expected.cast(inv); - } - - return null; + @RestrictedApi( + explanation = "Not part of the API.", + link = "", + allowedOnPath = ".*/com/lishid/openinv/util/InternalAccessor.java") + @ApiStatus.Internal + static void setProvider(@Nullable BiFunction, ISpecialInventory> provider) { + InventoryAccess.provider = provider; } - /** - * @deprecated Do not create a new instance to use static methods. - */ - @Deprecated(forRemoval = true, since = "4.2.0") - public InventoryAccess() {} + private InventoryAccess() { + throw new IllegalStateException("Cannot create instance of utility class."); + } } diff --git a/assembly/pom.xml b/assembly/pom.xml deleted file mode 100644 index 3fd0775e..00000000 --- a/assembly/pom.xml +++ /dev/null @@ -1,58 +0,0 @@ - - - - 4.0.0 - - - com.lishid - openinvparent - 5.0.0-SNAPSHOT - - - openinvassembly - OpenInvAssembly - pom - - - ../target - OpenInv - - - - maven-assembly-plugin - - - reactor-uberjar - package - - single - - - false - - src/assembly/reactor-uberjar.xml - - - - - - - - - - diff --git a/assembly/src/assembly/reactor-uberjar.xml b/assembly/src/assembly/reactor-uberjar.xml deleted file mode 100644 index cb400f35..00000000 --- a/assembly/src/assembly/reactor-uberjar.xml +++ /dev/null @@ -1,54 +0,0 @@ - - - - - reactor-uberjar - - - jar - - - false - - - - - true - - - / - true - - - - META-INF/** - - - - false - - - - - - diff --git a/common/pom.xml b/common/pom.xml new file mode 100644 index 00000000..09389665 --- /dev/null +++ b/common/pom.xml @@ -0,0 +1,30 @@ + + + 4.0.0 + + + com.lishid + openinvparent + 5.0.0-SNAPSHOT + + + openinvcommon + + + + annotations + org.jetbrains + + + org.spigotmc + spigot-api + + + openinvapi + com.lishid + + + + diff --git a/common/src/main/java/com/lishid/openinv/event/OpenEvents.java b/common/src/main/java/com/lishid/openinv/event/OpenEvents.java new file mode 100644 index 00000000..77349ccf --- /dev/null +++ b/common/src/main/java/com/lishid/openinv/event/OpenEvents.java @@ -0,0 +1,32 @@ +package com.lishid.openinv.event; + +import com.lishid.openinv.internal.ISpecialInventory; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.jetbrains.annotations.NotNull; + +/** + * Construct and call events. + */ +public final class OpenEvents { + + public static boolean saveCancelled(@NotNull Player player) { + return call(new PlayerSaveEvent(player)); + } + + public static boolean saveCancelled(@NotNull Player player, @NotNull ISpecialInventory inventory) { + return call(new OpenPlayerSaveEvent(player, inventory)); + } + + private static boolean call(T event) { + Bukkit.getPluginManager().callEvent(event); + return event.isCancelled(); + } + + private OpenEvents() { + throw new IllegalStateException("Cannot create instance of utility class."); + } + +} diff --git a/common/src/main/java/com/lishid/openinv/internal/Accessor.java b/common/src/main/java/com/lishid/openinv/internal/Accessor.java new file mode 100644 index 00000000..e57a4afb --- /dev/null +++ b/common/src/main/java/com/lishid/openinv/internal/Accessor.java @@ -0,0 +1,24 @@ +package com.lishid.openinv.internal; + +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public interface Accessor { + + @NotNull + PlayerManager getPlayerManager(); + + @NotNull IAnySilentContainer getAnySilentContainer(); + + @NotNull ISpecialPlayerInventory createPlayerInventory(@NotNull Player player); + + @NotNull ISpecialEnderChest createEnderChest(@NotNull Player player); + + @Nullable T get(@NotNull Inventory bukkitInventory, @NotNull Class clazz); + + void reload(@NotNull ConfigurationSection config); + +} diff --git a/plugin/src/main/java/com/lishid/openinv/internal/InventoryViewTitle.java b/common/src/main/java/com/lishid/openinv/internal/InventoryViewTitle.java similarity index 82% rename from plugin/src/main/java/com/lishid/openinv/internal/InventoryViewTitle.java rename to common/src/main/java/com/lishid/openinv/internal/InventoryViewTitle.java index 7bff29a1..ccd8fd13 100644 --- a/plugin/src/main/java/com/lishid/openinv/internal/InventoryViewTitle.java +++ b/common/src/main/java/com/lishid/openinv/internal/InventoryViewTitle.java @@ -1,6 +1,6 @@ package com.lishid.openinv.internal; -import com.lishid.openinv.OpenInv; +import com.lishid.openinv.util.lang.LanguageManager; import com.lishid.openinv.util.lang.Replacement; import org.bukkit.entity.HumanEntity; import org.bukkit.entity.Player; @@ -22,11 +22,13 @@ public enum InventoryViewTitle { this.defaultSuffix = defaultSuffix; } - public @NotNull String getTitle(@NotNull Player viewer, @NotNull ISpecialInventory inventory) { + public @NotNull String getTitle( + @NotNull LanguageManager lang, + @NotNull Player viewer, + @NotNull ISpecialInventory inventory) { HumanEntity owner = inventory.getPlayer(); - String localTitle = OpenInv.getPlugin(OpenInv.class) - .getLocalizedMessage( + String localTitle = lang.getLocalizedMessage( viewer, localizationKey, new Replacement("%player%", owner.getName())); diff --git a/plugin/src/main/java/com/lishid/openinv/internal/OpenInventoryView.java b/common/src/main/java/com/lishid/openinv/internal/OpenInventoryView.java similarity index 100% rename from plugin/src/main/java/com/lishid/openinv/internal/OpenInventoryView.java rename to common/src/main/java/com/lishid/openinv/internal/OpenInventoryView.java diff --git a/plugin/src/main/java/com/lishid/openinv/internal/PlaceholderParser.java b/common/src/main/java/com/lishid/openinv/internal/PlaceholderParser.java similarity index 100% rename from plugin/src/main/java/com/lishid/openinv/internal/PlaceholderParser.java rename to common/src/main/java/com/lishid/openinv/internal/PlaceholderParser.java diff --git a/plugin/src/main/java/com/lishid/openinv/internal/IPlayerDataManager.java b/common/src/main/java/com/lishid/openinv/internal/PlayerManager.java similarity index 97% rename from plugin/src/main/java/com/lishid/openinv/internal/IPlayerDataManager.java rename to common/src/main/java/com/lishid/openinv/internal/PlayerManager.java index 37bb1bf0..d1d3c6c0 100644 --- a/plugin/src/main/java/com/lishid/openinv/internal/IPlayerDataManager.java +++ b/common/src/main/java/com/lishid/openinv/internal/PlayerManager.java @@ -22,7 +22,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -public interface IPlayerDataManager { +public interface PlayerManager { /** * Loads a Player for an OfflinePlayer. diff --git a/plugin/src/main/java/com/lishid/openinv/util/Permissions.java b/common/src/main/java/com/lishid/openinv/util/Permissions.java similarity index 100% rename from plugin/src/main/java/com/lishid/openinv/util/Permissions.java rename to common/src/main/java/com/lishid/openinv/util/Permissions.java diff --git a/api/src/main/java/com/lishid/openinv/util/ReflectionHelper.java b/common/src/main/java/com/lishid/openinv/util/ReflectionHelper.java similarity index 93% rename from api/src/main/java/com/lishid/openinv/util/ReflectionHelper.java rename to common/src/main/java/com/lishid/openinv/util/ReflectionHelper.java index 7c283749..2c60ffe0 100644 --- a/api/src/main/java/com/lishid/openinv/util/ReflectionHelper.java +++ b/common/src/main/java/com/lishid/openinv/util/ReflectionHelper.java @@ -16,16 +16,15 @@ package com.lishid.openinv.util; -import java.lang.reflect.Field; import org.jetbrains.annotations.Nullable; +import java.lang.reflect.Field; + /** * A utility for making reflection easier. */ public final class ReflectionHelper { - private ReflectionHelper() {} - /** * Grab an {@link Object} stored in a {@link Field} of another {@code Object}. * @@ -59,8 +58,8 @@ private ReflectionHelper() {} */ public static @Nullable Field grabFieldByType(Class holderType, Class fieldType) { for (Field field : holderType.getDeclaredFields()) { - field.setAccessible(true); if (fieldType.isAssignableFrom(field.getType())) { + field.setAccessible(true); return field; } } @@ -72,4 +71,8 @@ private ReflectionHelper() {} return null; } + private ReflectionHelper() { + throw new IllegalStateException("Cannot create instance of utility class."); + } + } diff --git a/plugin/src/main/java/com/lishid/openinv/util/lang/LanguageManager.java b/common/src/main/java/com/lishid/openinv/util/lang/LanguageManager.java similarity index 79% rename from plugin/src/main/java/com/lishid/openinv/util/lang/LanguageManager.java rename to common/src/main/java/com/lishid/openinv/util/lang/LanguageManager.java index 4438977b..6e5fe207 100644 --- a/plugin/src/main/java/com/lishid/openinv/util/lang/LanguageManager.java +++ b/common/src/main/java/com/lishid/openinv/util/lang/LanguageManager.java @@ -16,7 +16,18 @@ package com.lishid.openinv.util.lang; -import com.lishid.openinv.OpenInv; +import net.md_5.bungee.api.ChatMessageType; +import net.md_5.bungee.api.chat.TextComponent; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import org.bukkit.configuration.Configuration; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + import java.io.BufferedReader; import java.io.File; import java.io.IOException; @@ -26,15 +37,10 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.function.Predicate; import java.util.logging.Level; -import org.bukkit.ChatColor; -import org.bukkit.configuration.Configuration; -import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.configuration.file.YamlConfiguration; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; /** * A simple language manager supporting both custom and bundled languages. @@ -43,11 +49,11 @@ */ public class LanguageManager { - private final OpenInv plugin; + private final Plugin plugin; private final String defaultLocale; private final Map locales; - public LanguageManager(@NotNull OpenInv plugin, @NotNull String defaultLocale) { + public LanguageManager(@NotNull Plugin plugin, @NotNull String defaultLocale) { this.plugin = plugin; this.defaultLocale = defaultLocale; this.locales = new HashMap<>(); @@ -191,7 +197,7 @@ private void addTranslationFallthrough( } public @Nullable String getValue(@NotNull String key, @Nullable String locale) { - String value = getOrLoadLocale(locale == null ? defaultLocale : locale.toLowerCase()).getString(key); + String value = getOrLoadLocale(locale == null ? defaultLocale : locale.toLowerCase(Locale.ROOT)).getString(key); if (value == null || value.isEmpty()) { return null; } @@ -215,4 +221,59 @@ private void addTranslationFallthrough( return value; } + public @Nullable String getLocalizedMessage(@NotNull CommandSender sender, @NotNull String key) { + return getValue(key, getLocale(sender)); + } + + public @Nullable String getLocalizedMessage( + @NotNull CommandSender sender, + @NotNull String key, + Replacement @NotNull ... replacements) { + return getValue(key, getLocale(sender), replacements); + } + + private @NotNull String getLocale(@NotNull CommandSender sender) { + if (sender instanceof Player) { + return ((Player) sender).getLocale(); + } else { + return plugin.getConfig().getString("settings.locale", "en_us"); + } + } + + public void sendMessage(@NotNull CommandSender sender, @NotNull String key) { + String message = getLocalizedMessage(sender, key); + + if (message != null && !message.isEmpty()) { + sender.sendMessage(message); + } + } + + public void sendMessage(@NotNull CommandSender sender, @NotNull String key, Replacement @NotNull... replacements) { + String message = getLocalizedMessage(sender, key, replacements); + + if (message != null && !message.isEmpty()) { + sender.sendMessage(message); + } + } + + public void sendSystemMessage(@NotNull Player player, @NotNull String key) { + String message = getLocalizedMessage(player, key); + + if (message == null) { + return; + } + + int newline = message.indexOf('\n'); + if (newline != -1) { + // No newlines in action bar chat. + message = message.substring(0, newline); + } + + if (message.isEmpty()) { + return; + } + + player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacy(message)); + } + } diff --git a/plugin/src/main/java/com/lishid/openinv/util/lang/Replacement.java b/common/src/main/java/com/lishid/openinv/util/lang/Replacement.java similarity index 100% rename from plugin/src/main/java/com/lishid/openinv/util/lang/Replacement.java rename to common/src/main/java/com/lishid/openinv/util/lang/Replacement.java diff --git a/internal/v1_20_R3/pom.xml b/internal/v1_20_R3/pom.xml index be4bb10b..53508df9 100644 --- a/internal/v1_20_R3/pom.xml +++ b/internal/v1_20_R3/pom.xml @@ -30,8 +30,8 @@ OpenInvAdapter1_20_R3 - 17 - 17 + 17 + 17 1.20.4-R0.1-SNAPSHOT @@ -51,10 +51,9 @@ openinvapi com.lishid - provided - openinvplugincore + openinvcommon com.lishid @@ -65,9 +64,6 @@ - - maven-shade-plugin - maven-compiler-plugin diff --git a/internal/v1_20_R3/src/main/java/com/lishid/openinv/internal/v1_20_R3/AnySilentContainer.java b/internal/v1_20_R3/src/main/java/com/lishid/openinv/internal/v1_20_R3/AnySilentContainer.java index eb6d8fef..403b3ed8 100644 --- a/internal/v1_20_R3/src/main/java/com/lishid/openinv/internal/v1_20_R3/AnySilentContainer.java +++ b/internal/v1_20_R3/src/main/java/com/lishid/openinv/internal/v1_20_R3/AnySilentContainer.java @@ -16,9 +16,9 @@ package com.lishid.openinv.internal.v1_20_R3; -import com.lishid.openinv.OpenInv; import com.lishid.openinv.internal.IAnySilentContainer; import com.lishid.openinv.util.ReflectionHelper; +import com.lishid.openinv.util.lang.LanguageManager; import net.minecraft.core.BlockPos; import net.minecraft.network.chat.Component; import net.minecraft.server.level.ServerPlayer; @@ -50,15 +50,18 @@ public class AnySilentContainer implements IAnySilentContainer { + private final @NotNull Logger logger; + private final @NotNull LanguageManager lang; private @Nullable Field serverPlayerGameModeGameType; - public AnySilentContainer() { + public AnySilentContainer(@NotNull Logger logger, @NotNull LanguageManager lang) { + this.logger = logger; + this.lang = lang; 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."); @@ -66,7 +69,6 @@ public AnySilentContainer() { 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); } @@ -85,7 +87,7 @@ public boolean activateContainer( return true; } - ServerPlayer player = PlayerDataManager.getHandle(bukkitPlayer); + ServerPlayer player = PlayerManager.getHandle(bukkitPlayer); final net.minecraft.world.level.Level level = player.level(); final BlockPos blockPos = new BlockPos(bukkitBlock.getX(), bukkitBlock.getY(), bukkitBlock.getZ()); @@ -100,10 +102,10 @@ public boolean activateContainer( PlayerEnderChestContainer enderChest = player.getEnderChestInventory(); enderChest.setActiveChest(enderChestTile); player.openMenu(new SimpleMenuProvider((containerCounter, playerInventory, ignored) -> { - MenuType containers = PlayerDataManager.getContainers(enderChest.getContainerSize()); + MenuType containers = PlayerManager.getContainers(enderChest.getContainerSize()); int rows = enderChest.getContainerSize() / 9; return new ChestMenu(containers, containerCounter, playerInventory, enderChest, rows); - }, Component.translatable(("container.enderchest")))); + }, Component.translatable("container.enderchest"))); bukkitPlayer.incrementStatistic(Statistic.ENDERCHEST_OPENED); return true; } @@ -121,7 +123,7 @@ public boolean activateContainer( menuProvider = chestBlock.getMenuProvider(blockState, level, blockPos, true); if (menuProvider == null) { - OpenInv.getPlugin(OpenInv.class).sendSystemMessage(bukkitPlayer, "messages.error.lootNotGenerated"); + lang.sendSystemMessage(bukkitPlayer, "messages.error.lootNotGenerated"); return false; } @@ -153,7 +155,7 @@ public boolean activateContainer( if (blockEntity instanceof RandomizableContainerBlockEntity lootable) { if (lootable.lootTable != null) { - OpenInv.getPlugin(OpenInv.class).sendSystemMessage(bukkitPlayer, "messages.error.lootNotGenerated"); + lang.sendSystemMessage(bukkitPlayer, "messages.error.lootNotGenerated"); return false; } } @@ -171,7 +173,7 @@ public void deactivateContainer(@NotNull final Player bukkitPlayer) { return; } - ServerPlayer player = PlayerDataManager.getHandle(bukkitPlayer); + ServerPlayer player = PlayerManager.getHandle(bukkitPlayer); // Force game mode change without informing plugins or players. // Regular game mode set calls GameModeChangeEvent and is cancellable. @@ -199,7 +201,6 @@ private void forceGameType(final ServerPlayer player, final GameType gameMode) { 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_20_R3/src/main/java/com/lishid/openinv/internal/v1_20_R3/InternalAccessor.java b/internal/v1_20_R3/src/main/java/com/lishid/openinv/internal/v1_20_R3/InternalAccessor.java new file mode 100644 index 00000000..47e199da --- /dev/null +++ b/internal/v1_20_R3/src/main/java/com/lishid/openinv/internal/v1_20_R3/InternalAccessor.java @@ -0,0 +1,64 @@ +package com.lishid.openinv.internal.v1_20_R3; + +import com.lishid.openinv.internal.Accessor; +import com.lishid.openinv.internal.IAnySilentContainer; +import com.lishid.openinv.internal.ISpecialEnderChest; +import com.lishid.openinv.internal.ISpecialInventory; +import com.lishid.openinv.internal.ISpecialPlayerInventory; +import com.lishid.openinv.util.lang.LanguageManager; +import net.minecraft.world.Container; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.craftbukkit.v1_20_R3.inventory.CraftInventory; +import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.logging.Logger; + +public class InternalAccessor implements Accessor { + + private final @NotNull PlayerManager manager; + private final @NotNull AnySilentContainer anySilentContainer; + + public InternalAccessor(@NotNull Logger logger, @NotNull LanguageManager lang) { + manager = new PlayerManager(logger, lang); + anySilentContainer = new AnySilentContainer(logger, lang); + } + + @Override + public @NotNull PlayerManager getPlayerManager() { + return manager; + } + + @Override + public @NotNull IAnySilentContainer getAnySilentContainer() { + return anySilentContainer; + } + + @Override + public @NotNull ISpecialPlayerInventory createPlayerInventory(@NotNull Player player) { + return new SpecialPlayerInventory(player, player.isOnline()); + } + + @Override + public @NotNull ISpecialEnderChest createEnderChest(@NotNull Player player) { + return new SpecialEnderChest(player, player.isOnline()); + } + + @Override + public @Nullable T get(@NotNull Inventory bukkitInventory, @NotNull Class clazz) { + if (!(bukkitInventory instanceof CraftInventory craftInventory)) { + return null; + } + Container container = craftInventory.getInventory(); + if (clazz.isInstance(container)) { + return clazz.cast(container); + } + return null; + } + + @Override + public void reload(@NotNull ConfigurationSection config) {} + +} diff --git a/internal/v1_20_R3/src/main/java/com/lishid/openinv/internal/v1_20_R3/OpenPlayer.java b/internal/v1_20_R3/src/main/java/com/lishid/openinv/internal/v1_20_R3/OpenPlayer.java index 2861c2bc..52d553af 100644 --- a/internal/v1_20_R3/src/main/java/com/lishid/openinv/internal/v1_20_R3/OpenPlayer.java +++ b/internal/v1_20_R3/src/main/java/com/lishid/openinv/internal/v1_20_R3/OpenPlayer.java @@ -16,6 +16,7 @@ package com.lishid.openinv.internal.v1_20_R3; +import com.lishid.openinv.event.OpenEvents; import com.mojang.logging.LogUtils; import net.minecraft.Util; import net.minecraft.nbt.CompoundTag; @@ -70,17 +71,24 @@ public class OpenPlayer extends CraftPlayer { "Brain" ); - public OpenPlayer(CraftServer server, ServerPlayer entity) { + private final PlayerManager manager; + + OpenPlayer(CraftServer server, ServerPlayer entity, PlayerManager manager) { super(server, entity); + this.manager = manager; } @Override public void loadData() { - PlayerDataManager.loadData(getHandle()); + manager.loadData(getHandle()); } @Override public void saveData() { + if (OpenEvents.saveCancelled(this)) { + return; + } + ServerPlayer player = this.getHandle(); // See net.minecraft.world.level.storage.PlayerDataStorage#save(EntityHuman) try { diff --git a/internal/v1_20_R3/src/main/java/com/lishid/openinv/internal/v1_20_R3/PlayerDataManager.java b/internal/v1_20_R3/src/main/java/com/lishid/openinv/internal/v1_20_R3/PlayerManager.java similarity index 93% rename from internal/v1_20_R3/src/main/java/com/lishid/openinv/internal/v1_20_R3/PlayerDataManager.java rename to internal/v1_20_R3/src/main/java/com/lishid/openinv/internal/v1_20_R3/PlayerManager.java index 17ed8e4e..7f02bb7d 100644 --- a/internal/v1_20_R3/src/main/java/com/lishid/openinv/internal/v1_20_R3/PlayerDataManager.java +++ b/internal/v1_20_R3/src/main/java/com/lishid/openinv/internal/v1_20_R3/PlayerManager.java @@ -16,11 +16,10 @@ package com.lishid.openinv.internal.v1_20_R3; -import com.lishid.openinv.OpenInv; -import com.lishid.openinv.internal.IPlayerDataManager; import com.lishid.openinv.internal.ISpecialInventory; import com.lishid.openinv.internal.InventoryViewTitle; import com.lishid.openinv.internal.OpenInventoryView; +import com.lishid.openinv.util.lang.LanguageManager; import com.mojang.authlib.GameProfile; import com.mojang.serialization.Dynamic; import net.minecraft.nbt.CompoundTag; @@ -48,7 +47,6 @@ import org.bukkit.craftbukkit.v1_20_R3.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; @@ -56,7 +54,7 @@ import java.util.UUID; import java.util.logging.Logger; -public class PlayerDataManager implements IPlayerDataManager { +public class PlayerManager implements com.lishid.openinv.internal.PlayerManager { private static boolean paper; @@ -69,13 +67,16 @@ public class PlayerDataManager implements IPlayerDataManager { } } + private final @NotNull Logger logger; + private final @NotNull LanguageManager lang; private @Nullable Field bukkitEntity; - public PlayerDataManager() { + public PlayerManager(@NotNull Logger logger, @NotNull LanguageManager lang) { + this.logger = logger; + this.lang = lang; 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; @@ -156,7 +157,7 @@ public PlayerDataManager() { try { injectPlayer(entity); } catch (IllegalAccessException e) { - JavaPlugin.getPlugin(OpenInv.class).getLogger().log( + logger.log( java.util.logging.Level.WARNING, e, () -> "Unable to inject ServerPlayer, certain player data may be lost when saving!"); @@ -165,7 +166,7 @@ public PlayerDataManager() { return entity; } - static boolean loadData(@NotNull ServerPlayer player) { + boolean loadData(@NotNull ServerPlayer player) { // See CraftPlayer#loadData CompoundTag loadedData = player.server.getPlayerList().playerIo.load(player); @@ -189,7 +190,7 @@ static boolean loadData(@NotNull ServerPlayer player) { return true; } - private static void parseWorld(@NotNull ServerPlayer player, @NotNull CompoundTag loadedData) { + private void parseWorld(@NotNull ServerPlayer player, @NotNull CompoundTag loadedData) { // See PlayerList#placeNewPlayer World bukkitWorld; if (loadedData.contains("WorldUUIDMost") && loadedData.contains("WorldUUIDLeast")) { @@ -201,7 +202,7 @@ private static void parseWorld(@NotNull ServerPlayer player, @NotNull CompoundTa } else { // Vanilla player data. DimensionType.parseLegacy(new Dynamic<>(NbtOps.INSTANCE, loadedData.get("Dimension"))) - .resultOrPartial(JavaPlugin.getPlugin(OpenInv.class).getLogger()::warning) + .resultOrPartial(logger::warning) .map(player.server::getLevel) // If ServerLevel exists, set, otherwise move to spawn. .ifPresentOrElse(player::setServerLevel, () -> player.spawnIn(null)); @@ -221,7 +222,7 @@ private void injectPlayer(ServerPlayer player) throws IllegalAccessException { bukkitEntity.setAccessible(true); - bukkitEntity.set(player, new OpenPlayer(player.server.server, player)); + bukkitEntity.set(player, new OpenPlayer(player.server.server, player, this)); } @Override @@ -234,7 +235,7 @@ private void injectPlayer(ServerPlayer player) throws IllegalAccessException { injectPlayer(nmsPlayer); return nmsPlayer.getBukkitEntity(); } catch (IllegalAccessException e) { - JavaPlugin.getPlugin(OpenInv.class).getLogger().log( + logger.log( java.util.logging.Level.WARNING, e, () -> "Unable to inject ServerPlayer, certain player data may be lost when saving!"); @@ -257,7 +258,7 @@ private void injectPlayer(ServerPlayer player) throws IllegalAccessException { return player.openInventory(inventory.getBukkitInventory()); } - InventoryView view = new OpenInventoryView(player, inventory, title.getTitle(player, inventory)); + InventoryView view = new OpenInventoryView(player, inventory, title.getTitle(lang, player, inventory)); AbstractContainerMenu container = new CraftContainer(view, nmsPlayer, nmsPlayer.nextContainerCounter()) { @Override public MenuType getType() { diff --git a/internal/v1_20_R3/src/main/java/com/lishid/openinv/internal/v1_20_R3/SpecialEnderChest.java b/internal/v1_20_R3/src/main/java/com/lishid/openinv/internal/v1_20_R3/SpecialEnderChest.java index 941e7979..2b8b3f38 100644 --- a/internal/v1_20_R3/src/main/java/com/lishid/openinv/internal/v1_20_R3/SpecialEnderChest.java +++ b/internal/v1_20_R3/src/main/java/com/lishid/openinv/internal/v1_20_R3/SpecialEnderChest.java @@ -49,9 +49,9 @@ public class SpecialEnderChest extends PlayerEnderChestContainer implements ISpe private boolean playerOnline; public SpecialEnderChest(final org.bukkit.entity.Player player, final Boolean online) { - super(PlayerDataManager.getHandle(player)); + super(PlayerManager.getHandle(player)); this.inventory = new CraftInventory(this); - this.owner = PlayerDataManager.getHandle(player); + this.owner = PlayerManager.getHandle(player); this.playerOnline = online; this.items = this.owner.getEnderChestInventory().items; } @@ -73,7 +73,7 @@ public void setPlayerOnline(@NotNull final org.bukkit.entity.Player player) { } ServerPlayer offlinePlayer = this.owner; - ServerPlayer onlinePlayer = PlayerDataManager.getHandle(player); + ServerPlayer onlinePlayer = PlayerManager.getHandle(player); // Set owner to new player. this.owner = onlinePlayer; @@ -198,7 +198,7 @@ public ItemStack addItem(ItemStack itemstack) { @Override public boolean canAddItem(ItemStack itemstack) { for (ItemStack itemstack1 : this.items) { - if (itemstack1.isEmpty() || ItemStack.isSameItemSameTags(itemstack1, itemstack) && itemstack1.getCount() < itemstack1.getMaxStackSize()) { + if (itemstack1.isEmpty() || (ItemStack.isSameItemSameTags(itemstack1, itemstack) && itemstack1.getCount() < itemstack1.getMaxStackSize())) { return true; } } diff --git a/internal/v1_20_R3/src/main/java/com/lishid/openinv/internal/v1_20_R3/SpecialPlayerInventory.java b/internal/v1_20_R3/src/main/java/com/lishid/openinv/internal/v1_20_R3/SpecialPlayerInventory.java index 5eac3b1b..e4c278b2 100644 --- a/internal/v1_20_R3/src/main/java/com/lishid/openinv/internal/v1_20_R3/SpecialPlayerInventory.java +++ b/internal/v1_20_R3/src/main/java/com/lishid/openinv/internal/v1_20_R3/SpecialPlayerInventory.java @@ -66,7 +66,7 @@ public class SpecialPlayerInventory extends Inventory implements ISpecialPlayerI private List> compartments; public SpecialPlayerInventory(@NotNull org.bukkit.entity.Player bukkitPlayer, @NotNull Boolean online) { - super(PlayerDataManager.getHandle(bukkitPlayer)); + super(PlayerManager.getHandle(bukkitPlayer)); this.inventory = new CraftInventory(this); this.playerOnline = online; this.player = super.player; @@ -84,7 +84,7 @@ public void setPlayerOnline(@NotNull org.bukkit.entity.Player player) { } Player offlinePlayer = this.player; - Player onlinePlayer = PlayerDataManager.getHandle(player); + Player onlinePlayer = PlayerManager.getHandle(player); onlinePlayer.getInventory().transaction.addAll(this.transaction); // Set owner to new player. diff --git a/internal/v1_20_R4/pom.xml b/internal/v1_20_R4/pom.xml index 666318e0..5e8673a7 100644 --- a/internal/v1_20_R4/pom.xml +++ b/internal/v1_20_R4/pom.xml @@ -30,8 +30,8 @@ OpenInvAdapter1_20_R4 - 21 - 21 + 21 + 21 1.20.6-R0.1-SNAPSHOT @@ -51,10 +51,9 @@ openinvapi com.lishid - provided - openinvplugincore + openinvcommon com.lishid @@ -65,9 +64,6 @@ - - maven-shade-plugin - maven-compiler-plugin diff --git a/internal/v1_20_R4/src/main/java/com/lishid/openinv/internal/v1_20_R4/AnySilentContainer.java b/internal/v1_20_R4/src/main/java/com/lishid/openinv/internal/v1_20_R4/AnySilentContainer.java index 7731837d..f04de59b 100644 --- a/internal/v1_20_R4/src/main/java/com/lishid/openinv/internal/v1_20_R4/AnySilentContainer.java +++ b/internal/v1_20_R4/src/main/java/com/lishid/openinv/internal/v1_20_R4/AnySilentContainer.java @@ -16,9 +16,9 @@ package com.lishid.openinv.internal.v1_20_R4; -import com.lishid.openinv.OpenInv; import com.lishid.openinv.internal.IAnySilentContainer; import com.lishid.openinv.util.ReflectionHelper; +import com.lishid.openinv.util.lang.LanguageManager; import net.minecraft.core.BlockPos; import net.minecraft.network.chat.Component; import net.minecraft.server.level.ServerPlayer; @@ -50,15 +50,18 @@ public class AnySilentContainer implements IAnySilentContainer { + private final @NotNull Logger logger; + private final @NotNull LanguageManager lang; private @Nullable Field serverPlayerGameModeGameType; - public AnySilentContainer() { + public AnySilentContainer(@NotNull Logger logger, @NotNull LanguageManager lang) { + this.logger = logger; + this.lang = lang; 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."); @@ -66,7 +69,6 @@ public AnySilentContainer() { 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); } @@ -85,7 +87,7 @@ public boolean activateContainer( return true; } - ServerPlayer player = PlayerDataManager.getHandle(bukkitPlayer); + ServerPlayer player = PlayerManager.getHandle(bukkitPlayer); final net.minecraft.world.level.Level level = player.level(); final BlockPos blockPos = new BlockPos(bukkitBlock.getX(), bukkitBlock.getY(), bukkitBlock.getZ()); @@ -100,10 +102,10 @@ public boolean activateContainer( PlayerEnderChestContainer enderChest = player.getEnderChestInventory(); enderChest.setActiveChest(enderChestTile); player.openMenu(new SimpleMenuProvider((containerCounter, playerInventory, ignored) -> { - MenuType containers = PlayerDataManager.getContainers(enderChest.getContainerSize()); + MenuType containers = PlayerManager.getContainers(enderChest.getContainerSize()); int rows = enderChest.getContainerSize() / 9; return new ChestMenu(containers, containerCounter, playerInventory, enderChest, rows); - }, Component.translatable(("container.enderchest")))); + }, Component.translatable("container.enderchest"))); bukkitPlayer.incrementStatistic(Statistic.ENDERCHEST_OPENED); return true; } @@ -121,7 +123,7 @@ public boolean activateContainer( menuProvider = chestBlock.getMenuProvider(blockState, level, blockPos, true); if (menuProvider == null) { - OpenInv.getPlugin(OpenInv.class).sendSystemMessage(bukkitPlayer, "messages.error.lootNotGenerated"); + lang.sendSystemMessage(bukkitPlayer, "messages.error.lootNotGenerated"); return false; } @@ -153,7 +155,7 @@ public boolean activateContainer( if (blockEntity instanceof RandomizableContainerBlockEntity lootable) { if (lootable.lootTable != null) { - OpenInv.getPlugin(OpenInv.class).sendSystemMessage(bukkitPlayer, "messages.error.lootNotGenerated"); + lang.sendSystemMessage(bukkitPlayer, "messages.error.lootNotGenerated"); return false; } } @@ -171,7 +173,7 @@ public void deactivateContainer(@NotNull final Player bukkitPlayer) { return; } - ServerPlayer player = PlayerDataManager.getHandle(bukkitPlayer); + ServerPlayer player = PlayerManager.getHandle(bukkitPlayer); // Force game mode change without informing plugins or players. // Regular game mode set calls GameModeChangeEvent and is cancellable. @@ -199,7 +201,6 @@ private void forceGameType(final ServerPlayer player, final GameType gameMode) { 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_20_R4/src/main/java/com/lishid/openinv/internal/v1_20_R4/InternalAccessor.java b/internal/v1_20_R4/src/main/java/com/lishid/openinv/internal/v1_20_R4/InternalAccessor.java new file mode 100644 index 00000000..ff599644 --- /dev/null +++ b/internal/v1_20_R4/src/main/java/com/lishid/openinv/internal/v1_20_R4/InternalAccessor.java @@ -0,0 +1,64 @@ +package com.lishid.openinv.internal.v1_20_R4; + +import com.lishid.openinv.internal.Accessor; +import com.lishid.openinv.internal.IAnySilentContainer; +import com.lishid.openinv.internal.ISpecialEnderChest; +import com.lishid.openinv.internal.ISpecialInventory; +import com.lishid.openinv.internal.ISpecialPlayerInventory; +import com.lishid.openinv.util.lang.LanguageManager; +import net.minecraft.world.Container; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.craftbukkit.v1_20_R4.inventory.CraftInventory; +import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.logging.Logger; + +public class InternalAccessor implements Accessor { + + private final @NotNull PlayerManager manager; + private final @NotNull AnySilentContainer anySilentContainer; + + public InternalAccessor(@NotNull Logger logger, @NotNull LanguageManager lang) { + manager = new PlayerManager(logger, lang); + anySilentContainer = new AnySilentContainer(logger, lang); + } + + @Override + public @NotNull PlayerManager getPlayerManager() { + return manager; + } + + @Override + public @NotNull IAnySilentContainer getAnySilentContainer() { + return anySilentContainer; + } + + @Override + public @NotNull ISpecialPlayerInventory createPlayerInventory(@NotNull Player player) { + return new SpecialPlayerInventory(player, player.isOnline()); + } + + @Override + public @NotNull ISpecialEnderChest createEnderChest(@NotNull Player player) { + return new SpecialEnderChest(player, player.isOnline()); + } + + @Override + public @Nullable T get(@NotNull Inventory bukkitInventory, @NotNull Class clazz) { + if (!(bukkitInventory instanceof CraftInventory craftInventory)) { + return null; + } + Container container = craftInventory.getInventory(); + if (clazz.isInstance(container)) { + return clazz.cast(container); + } + return null; + } + + @Override + public void reload(@NotNull ConfigurationSection config) {} + +} diff --git a/internal/v1_20_R4/src/main/java/com/lishid/openinv/internal/v1_20_R4/OpenPlayer.java b/internal/v1_20_R4/src/main/java/com/lishid/openinv/internal/v1_20_R4/OpenPlayer.java index be7fbb47..9223fa19 100644 --- a/internal/v1_20_R4/src/main/java/com/lishid/openinv/internal/v1_20_R4/OpenPlayer.java +++ b/internal/v1_20_R4/src/main/java/com/lishid/openinv/internal/v1_20_R4/OpenPlayer.java @@ -16,6 +16,7 @@ package com.lishid.openinv.internal.v1_20_R4; +import com.lishid.openinv.event.OpenEvents; import com.mojang.logging.LogUtils; import net.minecraft.Util; import net.minecraft.nbt.CompoundTag; @@ -80,17 +81,24 @@ public class OpenPlayer extends CraftPlayer { "Brain" ); - public OpenPlayer(CraftServer server, ServerPlayer entity) { + private final PlayerManager manager; + + OpenPlayer(CraftServer server, ServerPlayer entity, PlayerManager manager) { super(server, entity); + this.manager = manager; } @Override public void loadData() { - PlayerDataManager.loadData(getHandle()); + manager.loadData(getHandle()); } @Override public void saveData() { + if (OpenEvents.saveCancelled(this)) { + return; + } + ServerPlayer player = this.getHandle(); // See net.minecraft.world.level.storage.PlayerDataStorage#save(EntityHuman) try { diff --git a/internal/v1_20_R4/src/main/java/com/lishid/openinv/internal/v1_20_R4/PlayerDataManager.java b/internal/v1_20_R4/src/main/java/com/lishid/openinv/internal/v1_20_R4/PlayerManager.java similarity index 93% rename from internal/v1_20_R4/src/main/java/com/lishid/openinv/internal/v1_20_R4/PlayerDataManager.java rename to internal/v1_20_R4/src/main/java/com/lishid/openinv/internal/v1_20_R4/PlayerManager.java index 430fa975..2acab9a3 100644 --- a/internal/v1_20_R4/src/main/java/com/lishid/openinv/internal/v1_20_R4/PlayerDataManager.java +++ b/internal/v1_20_R4/src/main/java/com/lishid/openinv/internal/v1_20_R4/PlayerManager.java @@ -16,11 +16,10 @@ package com.lishid.openinv.internal.v1_20_R4; -import com.lishid.openinv.OpenInv; -import com.lishid.openinv.internal.IPlayerDataManager; import com.lishid.openinv.internal.ISpecialInventory; import com.lishid.openinv.internal.InventoryViewTitle; import com.lishid.openinv.internal.OpenInventoryView; +import com.lishid.openinv.util.lang.LanguageManager; import com.mojang.authlib.GameProfile; import com.mojang.serialization.Dynamic; import net.minecraft.nbt.CompoundTag; @@ -48,7 +47,6 @@ import org.bukkit.craftbukkit.v1_20_R4.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; @@ -56,7 +54,7 @@ import java.util.UUID; import java.util.logging.Logger; -public class PlayerDataManager implements IPlayerDataManager { +public class PlayerManager implements com.lishid.openinv.internal.PlayerManager { private static boolean paper; @@ -69,13 +67,16 @@ public class PlayerDataManager implements IPlayerDataManager { } } + private final @NotNull Logger logger; + private final @NotNull LanguageManager lang; private @Nullable Field bukkitEntity; - public PlayerDataManager() { + public PlayerManager(@NotNull Logger logger, @NotNull LanguageManager lang) { + this.logger = logger; + this.lang = lang; 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; @@ -156,7 +157,7 @@ public PlayerDataManager() { try { injectPlayer(entity); } catch (IllegalAccessException e) { - JavaPlugin.getPlugin(OpenInv.class).getLogger().log( + logger.log( java.util.logging.Level.WARNING, e, () -> "Unable to inject ServerPlayer, certain player data may be lost when saving!"); @@ -165,7 +166,7 @@ public PlayerDataManager() { return entity; } - static boolean loadData(@NotNull ServerPlayer player) { + boolean loadData(@NotNull ServerPlayer player) { // See CraftPlayer#loadData CompoundTag loadedData = player.server.getPlayerList().playerIo.load(player).orElse(null); @@ -189,7 +190,7 @@ static boolean loadData(@NotNull ServerPlayer player) { return true; } - private static void parseWorld(@NotNull ServerPlayer player, @NotNull CompoundTag loadedData) { + private void parseWorld(@NotNull ServerPlayer player, @NotNull CompoundTag loadedData) { // See PlayerList#placeNewPlayer World bukkitWorld; if (loadedData.contains("WorldUUIDMost") && loadedData.contains("WorldUUIDLeast")) { @@ -201,7 +202,7 @@ private static void parseWorld(@NotNull ServerPlayer player, @NotNull CompoundTa } else { // Vanilla player data. DimensionType.parseLegacy(new Dynamic<>(NbtOps.INSTANCE, loadedData.get("Dimension"))) - .resultOrPartial(JavaPlugin.getPlugin(OpenInv.class).getLogger()::warning) + .resultOrPartial(logger::warning) .map(player.server::getLevel) // If ServerLevel exists, set, otherwise move to spawn. .ifPresentOrElse(player::setServerLevel, () -> player.spawnIn(null)); @@ -221,7 +222,7 @@ private void injectPlayer(ServerPlayer player) throws IllegalAccessException { bukkitEntity.setAccessible(true); - bukkitEntity.set(player, new OpenPlayer(player.server.server, player)); + bukkitEntity.set(player, new OpenPlayer(player.server.server, player, this)); } @Override @@ -234,7 +235,7 @@ private void injectPlayer(ServerPlayer player) throws IllegalAccessException { injectPlayer(nmsPlayer); return nmsPlayer.getBukkitEntity(); } catch (IllegalAccessException e) { - JavaPlugin.getPlugin(OpenInv.class).getLogger().log( + logger.log( java.util.logging.Level.WARNING, e, () -> "Unable to inject ServerPlayer, certain player data may be lost when saving!"); @@ -257,7 +258,7 @@ private void injectPlayer(ServerPlayer player) throws IllegalAccessException { return player.openInventory(inventory.getBukkitInventory()); } - InventoryView view = new OpenInventoryView(player, inventory, title.getTitle(player, inventory)); + InventoryView view = new OpenInventoryView(player, inventory, title.getTitle(lang, player, inventory)); AbstractContainerMenu container = new CraftContainer(view, nmsPlayer, nmsPlayer.nextContainerCounter()) { @Override public MenuType getType() { diff --git a/internal/v1_20_R4/src/main/java/com/lishid/openinv/internal/v1_20_R4/SpecialEnderChest.java b/internal/v1_20_R4/src/main/java/com/lishid/openinv/internal/v1_20_R4/SpecialEnderChest.java index 712a68af..d38ac428 100644 --- a/internal/v1_20_R4/src/main/java/com/lishid/openinv/internal/v1_20_R4/SpecialEnderChest.java +++ b/internal/v1_20_R4/src/main/java/com/lishid/openinv/internal/v1_20_R4/SpecialEnderChest.java @@ -50,9 +50,9 @@ public class SpecialEnderChest extends PlayerEnderChestContainer implements ISpe private boolean playerOnline; public SpecialEnderChest(final org.bukkit.entity.Player player, final Boolean online) { - super(PlayerDataManager.getHandle(player)); + super(PlayerManager.getHandle(player)); this.inventory = new CraftInventory(this); - this.owner = PlayerDataManager.getHandle(player); + this.owner = PlayerManager.getHandle(player); this.playerOnline = online; this.items = this.owner.getEnderChestInventory().items; } @@ -74,7 +74,7 @@ public void setPlayerOnline(@NotNull final org.bukkit.entity.Player player) { } ServerPlayer offlinePlayer = this.owner; - ServerPlayer onlinePlayer = PlayerDataManager.getHandle(player); + ServerPlayer onlinePlayer = PlayerManager.getHandle(player); // Set owner to new player. this.owner = onlinePlayer; @@ -199,7 +199,7 @@ public ItemStack addItem(ItemStack itemstack) { @Override public boolean canAddItem(ItemStack itemstack) { for (ItemStack itemstack1 : this.items) { - if (itemstack1.isEmpty() || ItemStack.isSameItemSameComponents(itemstack1, itemstack) && itemstack1.getCount() < itemstack1.getMaxStackSize()) { + if (itemstack1.isEmpty() || (ItemStack.isSameItemSameComponents(itemstack1, itemstack) && itemstack1.getCount() < itemstack1.getMaxStackSize())) { return true; } } diff --git a/internal/v1_20_R4/src/main/java/com/lishid/openinv/internal/v1_20_R4/SpecialPlayerInventory.java b/internal/v1_20_R4/src/main/java/com/lishid/openinv/internal/v1_20_R4/SpecialPlayerInventory.java index a447f63d..155f4224 100644 --- a/internal/v1_20_R4/src/main/java/com/lishid/openinv/internal/v1_20_R4/SpecialPlayerInventory.java +++ b/internal/v1_20_R4/src/main/java/com/lishid/openinv/internal/v1_20_R4/SpecialPlayerInventory.java @@ -63,7 +63,7 @@ public class SpecialPlayerInventory extends Inventory implements ISpecialPlayerI private List> compartments; public SpecialPlayerInventory(@NotNull org.bukkit.entity.Player bukkitPlayer, @NotNull Boolean online) { - super(PlayerDataManager.getHandle(bukkitPlayer)); + super(PlayerManager.getHandle(bukkitPlayer)); this.inventory = new CraftInventory(this); this.playerOnline = online; this.player = super.player; @@ -81,7 +81,7 @@ public void setPlayerOnline(@NotNull org.bukkit.entity.Player player) { } Player offlinePlayer = this.player; - Player onlinePlayer = PlayerDataManager.getHandle(player); + Player onlinePlayer = PlayerManager.getHandle(player); onlinePlayer.getInventory().transaction.addAll(this.transaction); // Set owner to new player. diff --git a/internal/v1_21_R1/pom.xml b/internal/v1_21_R1/pom.xml index 2d2b6b3e..b3aa9c15 100644 --- a/internal/v1_21_R1/pom.xml +++ b/internal/v1_21_R1/pom.xml @@ -30,8 +30,8 @@ OpenInvAdapter1_21_R1 - 21 - 21 + 21 + 21 1.21-R0.1-SNAPSHOT @@ -51,10 +51,9 @@ openinvapi com.lishid - provided - openinvplugincore + openinvcommon com.lishid @@ -65,9 +64,6 @@ - - maven-shade-plugin - maven-compiler-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 index d03f25aa..15faeff8 100644 --- 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 @@ -16,9 +16,9 @@ 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 com.lishid.openinv.util.lang.LanguageManager; import net.minecraft.core.BlockPos; import net.minecraft.network.chat.Component; import net.minecraft.server.level.ServerPlayer; @@ -50,15 +50,18 @@ public class AnySilentContainer implements IAnySilentContainer { + private final @NotNull Logger logger; + private final @NotNull LanguageManager lang; private @Nullable Field serverPlayerGameModeGameType; - public AnySilentContainer() { + public AnySilentContainer(@NotNull Logger logger, @NotNull LanguageManager lang) { + this.logger = logger; + this.lang = lang; 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."); @@ -66,7 +69,6 @@ public AnySilentContainer() { 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); } @@ -85,7 +87,7 @@ public boolean activateContainer( return true; } - ServerPlayer player = PlayerDataManager.getHandle(bukkitPlayer); + ServerPlayer player = PlayerManager.getHandle(bukkitPlayer); final net.minecraft.world.level.Level level = player.level(); final BlockPos blockPos = new BlockPos(bukkitBlock.getX(), bukkitBlock.getY(), bukkitBlock.getZ()); @@ -100,10 +102,10 @@ public boolean activateContainer( PlayerEnderChestContainer enderChest = player.getEnderChestInventory(); enderChest.setActiveChest(enderChestTile); player.openMenu(new SimpleMenuProvider((containerCounter, playerInventory, ignored) -> { - MenuType containers = PlayerDataManager.getContainers(enderChest.getContainerSize()); + MenuType containers = PlayerManager.getContainers(enderChest.getContainerSize()); int rows = enderChest.getContainerSize() / 9; return new ChestMenu(containers, containerCounter, playerInventory, enderChest, rows); - }, Component.translatable(("container.enderchest")))); + }, Component.translatable("container.enderchest"))); bukkitPlayer.incrementStatistic(Statistic.ENDERCHEST_OPENED); return true; } @@ -121,7 +123,7 @@ public boolean activateContainer( menuProvider = chestBlock.getMenuProvider(blockState, level, blockPos, true); if (menuProvider == null) { - OpenInv.getPlugin(OpenInv.class).sendSystemMessage(bukkitPlayer, "messages.error.lootNotGenerated"); + lang.sendSystemMessage(bukkitPlayer, "messages.error.lootNotGenerated"); return false; } @@ -153,7 +155,7 @@ public boolean activateContainer( if (blockEntity instanceof RandomizableContainerBlockEntity lootable) { if (lootable.lootTable != null) { - OpenInv.getPlugin(OpenInv.class).sendSystemMessage(bukkitPlayer, "messages.error.lootNotGenerated"); + lang.sendSystemMessage(bukkitPlayer, "messages.error.lootNotGenerated"); return false; } } @@ -171,7 +173,7 @@ public void deactivateContainer(@NotNull final Player bukkitPlayer) { return; } - ServerPlayer player = PlayerDataManager.getHandle(bukkitPlayer); + ServerPlayer player = PlayerManager.getHandle(bukkitPlayer); // Force game mode change without informing plugins or players. // Regular game mode set calls GameModeChangeEvent and is cancellable. @@ -199,7 +201,6 @@ private void forceGameType(final ServerPlayer player, final GameType gameMode) { 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/InternalAccessor.java b/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/InternalAccessor.java new file mode 100644 index 00000000..59f3481a --- /dev/null +++ b/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/InternalAccessor.java @@ -0,0 +1,78 @@ +package com.lishid.openinv.internal.v1_21_R1; + +import com.lishid.openinv.internal.Accessor; +import com.lishid.openinv.internal.IAnySilentContainer; +import com.lishid.openinv.internal.ISpecialEnderChest; +import com.lishid.openinv.internal.ISpecialInventory; +import com.lishid.openinv.internal.ISpecialPlayerInventory; +import com.lishid.openinv.internal.v1_21_R1.inventory.OpenInventory; +import com.lishid.openinv.internal.v1_21_R1.inventory.Placeholders; +import com.lishid.openinv.util.lang.LanguageManager; +import net.minecraft.world.Container; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.craftbukkit.v1_21_R1.inventory.CraftInventory; +import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.logging.Level; +import java.util.logging.Logger; + +public class InternalAccessor implements Accessor { + + private final @NotNull Logger logger; + private final @NotNull PlayerManager manager; + private final @NotNull AnySilentContainer anySilentContainer; + + public InternalAccessor(@NotNull Logger logger, @NotNull LanguageManager lang) { + this.logger = logger; + manager = new PlayerManager(logger, lang); + anySilentContainer = new AnySilentContainer(logger, lang); + } + + @Override + public @NotNull PlayerManager getPlayerManager() { + return manager; + } + + @Override + public @NotNull IAnySilentContainer getAnySilentContainer() { + return anySilentContainer; + } + + @Override + public @NotNull ISpecialPlayerInventory createPlayerInventory(@NotNull Player player) { + return new OpenInventory(player); + } + + @Override + public @NotNull ISpecialEnderChest createEnderChest(@NotNull Player player) { + return new SpecialEnderChest(player, player.isOnline()); + } + + @Override + public @Nullable T get(@NotNull Inventory bukkitInventory, @NotNull Class clazz) { + if (!(bukkitInventory instanceof CraftInventory craftInventory)) { + return null; + } + Container container = craftInventory.getInventory(); + if (clazz.isInstance(container)) { + return clazz.cast(container); + } + return null; + } + + @Override + public void reload(@NotNull ConfigurationSection config) { + ConfigurationSection placeholders = config.getConfigurationSection("placeholders"); + if (placeholders != null) { + try { + Placeholders.load(placeholders); + } catch (Exception e) { + logger.log(Level.WARNING, "Caught exception loading placeholder overrides!", 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 index 6a10ada3..ac07285d 100644 --- 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 @@ -16,6 +16,7 @@ package com.lishid.openinv.internal.v1_21_R1; +import com.lishid.openinv.event.OpenEvents; import com.mojang.logging.LogUtils; import net.minecraft.Util; import net.minecraft.nbt.CompoundTag; @@ -79,17 +80,24 @@ public class OpenPlayer extends CraftPlayer { "Brain" ); - public OpenPlayer(CraftServer server, ServerPlayer entity) { + private final PlayerManager manager; + + OpenPlayer(CraftServer server, ServerPlayer entity, PlayerManager manager) { super(server, entity); + this.manager = manager; } @Override public void loadData() { - PlayerDataManager.loadData(getHandle()); + manager.loadData(getHandle()); } @Override public void saveData() { + if (OpenEvents.saveCancelled(this)) { + return; + } + ServerPlayer player = this.getHandle(); // See net.minecraft.world.level.storage.PlayerDataStorage#save(EntityHuman) try { 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/PlayerManager.java similarity index 93% rename from internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/PlayerDataManager.java rename to internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/PlayerManager.java index 4660d80a..2c81a192 100644 --- 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/PlayerManager.java @@ -16,13 +16,11 @@ package com.lishid.openinv.internal.v1_21_R1; -import com.github.jikoo.planarwrappers.function.TriFunction; -import com.lishid.openinv.OpenInv; -import com.lishid.openinv.internal.IPlayerDataManager; import com.lishid.openinv.internal.ISpecialInventory; import com.lishid.openinv.internal.InventoryViewTitle; import com.lishid.openinv.internal.OpenInventoryView; import com.lishid.openinv.internal.v1_21_R1.inventory.OpenInventory; +import com.lishid.openinv.util.lang.LanguageManager; import com.mojang.authlib.GameProfile; import com.mojang.serialization.Dynamic; import net.minecraft.nbt.CompoundTag; @@ -39,6 +37,7 @@ import net.minecraft.world.inventory.MenuType; import net.minecraft.world.level.Level; import net.minecraft.world.level.dimension.DimensionType; +import org.apache.commons.lang3.function.TriFunction; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.Server; @@ -50,7 +49,6 @@ 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; @@ -58,7 +56,7 @@ import java.util.UUID; import java.util.logging.Logger; -public class PlayerDataManager implements IPlayerDataManager { +public class PlayerManager implements com.lishid.openinv.internal.PlayerManager { private static boolean paper; private static TriFunction viewProvider; @@ -78,13 +76,16 @@ public class PlayerDataManager implements IPlayerDataManager { } } + private final @NotNull Logger logger; + private final @NotNull LanguageManager lang; private @Nullable Field bukkitEntity; - public PlayerDataManager() { + public PlayerManager(@NotNull Logger logger, @NotNull LanguageManager lang) { + this.logger = logger; + this.lang = lang; 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; @@ -165,7 +166,7 @@ public PlayerDataManager() { try { injectPlayer(entity); } catch (IllegalAccessException e) { - JavaPlugin.getPlugin(OpenInv.class).getLogger().log( + logger.log( java.util.logging.Level.WARNING, e, () -> "Unable to inject ServerPlayer, certain player data may be lost when saving!"); @@ -174,7 +175,7 @@ public PlayerDataManager() { return entity; } - static boolean loadData(@NotNull ServerPlayer player) { + boolean loadData(@NotNull ServerPlayer player) { // See CraftPlayer#loadData CompoundTag loadedData = player.server.getPlayerList().playerIo.load(player).orElse(null); @@ -198,7 +199,7 @@ static boolean loadData(@NotNull ServerPlayer player) { return true; } - private static void parseWorld(@NotNull ServerPlayer player, @NotNull CompoundTag loadedData) { + private void parseWorld(@NotNull ServerPlayer player, @NotNull CompoundTag loadedData) { // See PlayerList#placeNewPlayer World bukkitWorld; if (loadedData.contains("WorldUUIDMost") && loadedData.contains("WorldUUIDLeast")) { @@ -210,7 +211,7 @@ private static void parseWorld(@NotNull ServerPlayer player, @NotNull CompoundTa } else { // Vanilla player data. DimensionType.parseLegacy(new Dynamic<>(NbtOps.INSTANCE, loadedData.get("Dimension"))) - .resultOrPartial(JavaPlugin.getPlugin(OpenInv.class).getLogger()::warning) + .resultOrPartial(logger::warning) .map(player.server::getLevel) // If ServerLevel exists, set, otherwise move to spawn. .ifPresentOrElse(player::setServerLevel, () -> player.spawnIn(null)); @@ -230,7 +231,7 @@ private void injectPlayer(ServerPlayer player) throws IllegalAccessException { bukkitEntity.setAccessible(true); - bukkitEntity.set(player, new OpenPlayer(player.server.server, player)); + bukkitEntity.set(player, new OpenPlayer(player.server.server, player, this)); } @Override @@ -243,7 +244,7 @@ private void injectPlayer(ServerPlayer player) throws IllegalAccessException { injectPlayer(nmsPlayer); return nmsPlayer.getBukkitEntity(); } catch (IllegalAccessException e) { - JavaPlugin.getPlugin(OpenInv.class).getLogger().log( + logger.log( java.util.logging.Level.WARNING, e, () -> "Unable to inject ServerPlayer, certain player data may be lost when saving!"); @@ -292,7 +293,7 @@ private void injectPlayer(ServerPlayer player) throws IllegalAccessException { return bukkitPlayer.openInventory(inventory.getBukkitInventory()); } - String originalTitle = viewTitle.getTitle(bukkitPlayer, inventory); + String originalTitle = viewTitle.getTitle(lang, bukkitPlayer, inventory); InventoryView view = viewProvider.apply(bukkitPlayer, inventory, originalTitle); Component title = Component.literal(originalTitle); AbstractContainerMenu container = new CraftContainer(view, player, player.nextContainerCounter()) { 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 index 512f2d95..a7d74c49 100644 --- 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 @@ -50,9 +50,9 @@ public class SpecialEnderChest extends PlayerEnderChestContainer implements ISpe private boolean playerOnline; public SpecialEnderChest(final org.bukkit.entity.Player player, final Boolean online) { - super(PlayerDataManager.getHandle(player)); + super(PlayerManager.getHandle(player)); this.inventory = new CraftInventory(this); - this.owner = PlayerDataManager.getHandle(player); + this.owner = PlayerManager.getHandle(player); this.playerOnline = online; this.items = this.owner.getEnderChestInventory().items; } @@ -74,7 +74,7 @@ public void setPlayerOnline(@NotNull final org.bukkit.entity.Player player) { } ServerPlayer offlinePlayer = this.owner; - ServerPlayer onlinePlayer = PlayerDataManager.getHandle(player); + ServerPlayer onlinePlayer = PlayerManager.getHandle(player); // Set owner to new player. this.owner = onlinePlayer; @@ -199,7 +199,7 @@ public ItemStack addItem(ItemStack itemstack) { @Override public boolean canAddItem(ItemStack itemstack) { for (ItemStack itemstack1 : this.items) { - if (itemstack1.isEmpty() || ItemStack.isSameItemSameComponents(itemstack1, itemstack) && itemstack1.getCount() < itemstack1.getMaxStackSize()) { + if (itemstack1.isEmpty() || (ItemStack.isSameItemSameComponents(itemstack1, itemstack) && itemstack1.getCount() < itemstack1.getMaxStackSize())) { return true; } } diff --git a/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/inventory/ContainerSlotCrafting.java b/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/inventory/ContainerSlotCrafting.java index f3f241cf..a071e123 100644 --- a/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/inventory/ContainerSlotCrafting.java +++ b/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/inventory/ContainerSlotCrafting.java @@ -102,7 +102,7 @@ private SlotCrafting(Container container, int index, int x, int y) { @Override ItemStack getOrDefault() { - return isAvailable() ? items.get(ContainerSlotCrafting.this.index) : PlaceholderManager.survivalOnly(holder); + return isAvailable() ? items.get(ContainerSlotCrafting.this.index) : Placeholders.survivalOnly(holder); } @Override diff --git a/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/inventory/ContainerSlotCraftingResult.java b/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/inventory/ContainerSlotCraftingResult.java index 6b25d985..3bf9fb43 100644 --- a/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/inventory/ContainerSlotCraftingResult.java +++ b/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/inventory/ContainerSlotCraftingResult.java @@ -31,7 +31,7 @@ public Slot asMenuSlot(Container container, int index, int x, int y) { @Override ItemStack getOrDefault() { if (!ContainerSlotCrafting.isAvailable(holder)) { - return PlaceholderManager.survivalOnly(holder); + return Placeholders.survivalOnly(holder); } InventoryMenu inventoryMenu = holder.inventoryMenu; return inventoryMenu.getSlot(inventoryMenu.getResultSlotIndex()).getItem(); diff --git a/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/inventory/ContainerSlotCursor.java b/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/inventory/ContainerSlotCursor.java index 65a9e33c..33b83df6 100644 --- a/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/inventory/ContainerSlotCursor.java +++ b/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/inventory/ContainerSlotCursor.java @@ -85,10 +85,10 @@ private SlotCursor(Container container, int index, int x, int y) { @Override ItemStack getOrDefault() { if (!isAvailable()) { - return PlaceholderManager.survivalOnly(holder); + return Placeholders.survivalOnly(holder); } ItemStack carried = holder.containerMenu.getCarried(); - return carried.isEmpty() ? PlaceholderManager.cursor : carried; + return carried.isEmpty() ? Placeholders.cursor : carried; } @Override diff --git a/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/inventory/ContainerSlotDrop.java b/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/inventory/ContainerSlotDrop.java index 355ccd1f..e7fb80c5 100644 --- a/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/inventory/ContainerSlotDrop.java +++ b/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/inventory/ContainerSlotDrop.java @@ -63,8 +63,8 @@ private SlotDrop(Container container, int index, int x, int y) { @Override ItemStack getOrDefault() { return holder.connection != null && !holder.connection.isDisconnected() - ? PlaceholderManager.drop - : PlaceholderManager.blockedOffline; + ? Placeholders.drop + : Placeholders.blockedOffline; } @Override diff --git a/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/inventory/ContainerSlotEquipment.java b/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/inventory/ContainerSlotEquipment.java index 68ed1eb2..99f8b39d 100644 --- a/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/inventory/ContainerSlotEquipment.java +++ b/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/inventory/ContainerSlotEquipment.java @@ -19,11 +19,11 @@ class ContainerSlotEquipment extends ContainerSlotList { ContainerSlotEquipment(ServerPlayer holder, int index, EquipmentSlot equipmentSlot) { super(holder, index, InventoryType.SlotType.ARMOR); placeholder = switch (equipmentSlot) { - case HEAD -> PlaceholderManager.emptyHelmet; - case CHEST -> PlaceholderManager.emptyChestplate; - case LEGS -> PlaceholderManager.emptyLeggings; - case FEET -> PlaceholderManager.emptyBoots; - default -> PlaceholderManager.emptyOffHand; + case HEAD -> Placeholders.emptyHelmet; + case CHEST -> Placeholders.emptyChestplate; + case LEGS -> Placeholders.emptyLeggings; + case FEET -> Placeholders.emptyBoots; + default -> Placeholders.emptyOffHand; }; this.equipmentSlot = equipmentSlot; } diff --git a/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/inventory/ContainerSlotUninteractable.java b/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/inventory/ContainerSlotUninteractable.java index f27579c8..48b57709 100644 --- a/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/inventory/ContainerSlotUninteractable.java +++ b/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/inventory/ContainerSlotUninteractable.java @@ -64,11 +64,13 @@ static class SlotUninteractable extends MenuSlotPlaceholder { @Override ItemStack getOrDefault() { - return PlaceholderManager.notSlot; + return Placeholders.notSlot; } + @Override public void onQuickCraft(ItemStack var0, ItemStack var1) {} + @Override public void onTake(Player var0, ItemStack var1) {} @Override @@ -76,6 +78,7 @@ public boolean mayPlace(ItemStack var0) { return false; } + @Override public ItemStack getItem() { return ItemStack.EMPTY; } @@ -85,22 +88,29 @@ public boolean hasItem() { return false; } + @Override public void setByPlayer(ItemStack newStack) {} + @Override public void setByPlayer(ItemStack newStack, ItemStack oldStack) {} + @Override public void set(ItemStack var0) {} + @Override public void setChanged() {} + @Override public int getMaxStackSize() { return 0; } + @Override public int getMaxStackSize(ItemStack itemStack) { return 0; } + @Override public ItemStack remove(int amount) { return ItemStack.EMPTY; } @@ -110,22 +120,27 @@ public boolean mayPickup(Player var0) { return false; } + @Override public boolean isActive() { return false; } + @Override public Optional tryRemove(int var0, int var1, Player var2) { return Optional.empty(); } + @Override public ItemStack safeTake(int var0, int var1, Player var2) { return ItemStack.EMPTY; } + @Override public ItemStack safeInsert(ItemStack itemStack) { return itemStack; } + @Override public ItemStack safeInsert(ItemStack itemStack, int amount) { return itemStack; } @@ -135,10 +150,12 @@ public boolean allowModification(Player var0) { return false; } + @Override public int getContainerSlot() { return this.slot; } + @Override public boolean isHighlightable() { return false; } diff --git a/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/inventory/OpenInventory.java b/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/inventory/OpenInventory.java index ab0336d5..5bdea862 100644 --- a/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/inventory/OpenInventory.java +++ b/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/inventory/OpenInventory.java @@ -1,7 +1,7 @@ package com.lishid.openinv.internal.v1_21_R1.inventory; import com.lishid.openinv.internal.ISpecialPlayerInventory; -import com.lishid.openinv.internal.v1_21_R1.PlayerDataManager; +import com.lishid.openinv.internal.v1_21_R1.PlayerManager; import net.minecraft.ChatFormatting; import net.minecraft.core.NonNullList; import net.minecraft.network.chat.Component; @@ -38,7 +38,7 @@ public class OpenInventory implements Container, Nameable, MenuProvider, ISpecia public List transaction = new ArrayList<>(); public OpenInventory(@NotNull org.bukkit.entity.Player bukkitPlayer) { - owner = PlayerDataManager.getHandle(bukkitPlayer); + owner = PlayerManager.getHandle(bukkitPlayer); // Get total size, rounding up to nearest 9 for client compatibility. int rawSize = owner.getInventory().getContainerSize() + owner.inventoryMenu.getCraftSlots().getContainerSize() + 1; @@ -175,7 +175,7 @@ public Slot asMenuSlot(Container container, int index, int x, int y) { return new SlotUninteractable(container, index, x, y) { @Override ItemStack getOrDefault() { - return PlaceholderManager.craftingOutput; + return Placeholders.craftingOutput; } }; } @@ -201,7 +201,7 @@ public ServerPlayer getOwnerHandle() { public @NotNull Component getTitle(@Nullable ServerPlayer viewer) { MutableComponent component = Component.empty(); // Prefix for use with custom bitmap image fonts. - if (viewer == owner) { + if (owner.equals(viewer)) { component.append( Component.translatableWithFallback("openinv.container.inventory.self", "") .withStyle(style -> style @@ -230,7 +230,7 @@ public ServerPlayer getOwnerHandle() { @Override public void setPlayerOnline(@NotNull org.bukkit.entity.Player player) { - ServerPlayer newOwner = PlayerDataManager.getHandle(player); + ServerPlayer newOwner = PlayerManager.getHandle(player); // Only transfer regular inventory - crafting and cursor slots are transient. newOwner.getInventory().replaceWith(owner.getInventory()); owner = newOwner; diff --git a/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/inventory/OpenInventoryMenu.java b/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/inventory/OpenInventoryMenu.java index 2cad157e..a0eef7ff 100644 --- a/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/inventory/OpenInventoryMenu.java +++ b/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/inventory/OpenInventoryMenu.java @@ -190,6 +190,7 @@ public InventoryType.SlotType getSlotType(int slot) { private ItemStack remoteCarried = ItemStack.EMPTY; private boolean suppressRemoteUpdates; + @Override protected Slot addSlot(Slot slot) { slot.index = this.slots.size(); this.slots.add(slot); @@ -198,18 +199,21 @@ protected Slot addSlot(Slot slot) { return slot; } + @Override protected DataSlot addDataSlot(DataSlot dataSlot) { this.dataSlots.add(dataSlot); this.remoteDataSlots.add(0); return dataSlot; } + @Override protected void addDataSlots(ContainerData containerData) { for (int i = 0; i < containerData.getCount(); i++) { this.addDataSlot(DataSlot.forContainer(containerData, i)); } } + @Override public void addSlotListener(ContainerListener containerListener) { if (!this.containerListeners.contains(containerListener)) { this.containerListeners.add(containerListener); @@ -217,11 +221,13 @@ public void addSlotListener(ContainerListener containerListener) { } } + @Override public void setSynchronizer(ContainerSynchronizer containerSynchronizer) { this.synchronizer = containerSynchronizer; this.sendAllDataToRemote(); } + @Override public void sendAllDataToRemote() { for (int index = 0; index < slots.size(); ++index) { Slot slot = slots.get(index); @@ -239,6 +245,7 @@ public void sendAllDataToRemote() { } } + @Override public void broadcastCarriedItem() { this.remoteCarried = this.getCarried().copy(); if (this.synchronizer != null) { @@ -246,10 +253,12 @@ public void broadcastCarriedItem() { } } + @Override public void removeSlotListener(ContainerListener containerListener) { this.containerListeners.remove(containerListener); } + @Override public void broadcastChanges() { for (int index = 0; index < this.slots.size(); ++index) { Slot slot = this.slots.get(index); @@ -272,6 +281,7 @@ public void broadcastChanges() { } } + @Override public void broadcastFullState() { for (int index = 0; index < this.slots.size(); ++index) { ItemStack itemstack = this.slots.get(index).getItem(); @@ -340,14 +350,17 @@ private void synchronizeCarriedToRemote() { } } + @Override public void setRemoteCarried(ItemStack itemstack) { this.remoteCarried = itemstack.copy(); } + @Override public void suppressRemoteUpdates() { this.suppressRemoteUpdates = true; } + @Override public void resumeRemoteUpdates() { this.suppressRemoteUpdates = false; } @@ -429,6 +442,7 @@ public boolean stillValid(Player player) { * @param topDown whether to start at the top of the range or bottom * @return whether the stack was modified as a result of being quick-moved */ + @Override protected boolean moveItemStackTo(ItemStack itemStack, int rangeLow, int rangeHigh, boolean topDown) { boolean modified = false; boolean stackable = itemStack.isStackable(); diff --git a/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/inventory/PlaceholderManager.java b/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/inventory/Placeholders.java similarity index 96% rename from internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/inventory/PlaceholderManager.java rename to internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/inventory/Placeholders.java index 9a594460..48b91ba7 100644 --- a/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/inventory/PlaceholderManager.java +++ b/internal/v1_21_R1/src/main/java/com/lishid/openinv/internal/v1_21_R1/inventory/Placeholders.java @@ -1,6 +1,5 @@ package com.lishid.openinv.internal.v1_21_R1.inventory; -import com.lishid.openinv.internal.PlaceholderParser; import net.minecraft.core.Registry; import net.minecraft.core.RegistryAccess; import net.minecraft.core.component.DataComponents; @@ -28,7 +27,7 @@ import java.util.List; import java.util.Optional; -public class PlaceholderManager implements PlaceholderParser { +public final class Placeholders { private static final CustomModelData DEFAULT_CUSTOM_MODEL_DATA = new CustomModelData(9999); static final @NotNull EnumMap BLOCKED_GAME_TYPE = new EnumMap<>(GameType.class); @@ -54,8 +53,7 @@ public class PlaceholderManager implements PlaceholderParser { } } - @Override - public void load(@NotNull ConfigurationSection section) throws Exception { + public static void load(@NotNull ConfigurationSection section) throws Exception { craftingOutput = parse(section, "crafting-output", craftingOutput); cursor = parse(section, "cursor", cursor); drop = parse(section, "drop", drop); @@ -70,7 +68,7 @@ public void load(@NotNull ConfigurationSection section) throws Exception { BLOCKED_GAME_TYPE.put(GameType.SPECTATOR, parse(section, "blocked.spectator", BLOCKED_GAME_TYPE.get(GameType.SPECTATOR))); } - private @NotNull ItemStack parse( + private static @NotNull ItemStack parse( @NotNull ConfigurationSection section, @NotNull String path, @NotNull ItemStack defaultStack) throws Exception { @@ -178,4 +176,8 @@ private static ItemStack defaultBlockedOffline() { return itemStack; } + private Placeholders() { + throw new IllegalStateException("Cannot create instance of utility class."); + } + } diff --git a/plugin/pom.xml b/plugin/pom.xml index 2a7b9df6..381a5018 100644 --- a/plugin/pom.xml +++ b/plugin/pom.xml @@ -24,29 +24,52 @@ 5.0.0-SNAPSHOT - openinvplugincore + openinvplugin OpenInvPlugin + + spigot-api + org.spigotmc + + + planarwrappers + com.github.jikoo + openinvapi com.lishid - annotations - org.jetbrains + openinvcommon + com.lishid - spigot-api - org.spigotmc + com.lishid + openinvadapter1_21_R1 + 5.0.0-SNAPSHOT + compile - planarwrappers - com.github.jikoo + com.lishid + openinvadapter1_20_R4 + 5.0.0-SNAPSHOT + compile + + + com.lishid + openinvadapter1_20_R3 + 5.0.0-SNAPSHOT + compile + + + annotations + org.jetbrains + OpenInv src/main/resources @@ -86,6 +109,29 @@ maven-compiler-plugin + + maven-resources-plugin + + + copy-final-jar + package + + copy-resources + + + ${project.parent.build.directory} + + + ${project.build.directory} + + ${project.build.finalName}.jar + + + + + + + diff --git a/plugin/src/main/java/com/lishid/openinv/InventoryListener.java b/plugin/src/main/java/com/lishid/openinv/InventoryListener.java index 0e425fb1..390277b2 100644 --- a/plugin/src/main/java/com/lishid/openinv/InventoryListener.java +++ b/plugin/src/main/java/com/lishid/openinv/InventoryListener.java @@ -16,6 +16,7 @@ package com.lishid.openinv; +import com.google.errorprone.annotations.Keep; import com.lishid.openinv.internal.ISpecialEnderChest; import com.lishid.openinv.internal.ISpecialInventory; import com.lishid.openinv.internal.ISpecialPlayerInventory; @@ -44,6 +45,7 @@ */ record InventoryListener(OpenInv plugin) implements Listener { + @Keep @EventHandler private void onInventoryClose(@NotNull final InventoryCloseEvent event) { if (!(event.getPlayer() instanceof Player player)) { @@ -68,11 +70,13 @@ private void onInventoryClose(@NotNull final InventoryCloseEvent event) { } } + @Keep @EventHandler(priority = EventPriority.LOWEST) private void onInventoryClick(@NotNull final InventoryClickEvent event) { handleInventoryInteract(event); } + @Keep @EventHandler(priority = EventPriority.LOWEST) private void onInventoryDrag(@NotNull final InventoryDragEvent event) { handleInventoryInteract(event); diff --git a/plugin/src/main/java/com/lishid/openinv/LegacyInventoryListener.java b/plugin/src/main/java/com/lishid/openinv/LegacyInventoryListener.java index 9e49ca54..a92c92e6 100644 --- a/plugin/src/main/java/com/lishid/openinv/LegacyInventoryListener.java +++ b/plugin/src/main/java/com/lishid/openinv/LegacyInventoryListener.java @@ -1,5 +1,6 @@ package com.lishid.openinv; +import com.google.errorprone.annotations.Keep; import com.lishid.openinv.internal.ISpecialEnderChest; import com.lishid.openinv.internal.ISpecialInventory; import com.lishid.openinv.internal.ISpecialPlayerInventory; @@ -30,6 +31,7 @@ record LegacyInventoryListener(OpenInv plugin) implements Listener { + @Keep @EventHandler private void onInventoryClose(@NotNull final InventoryCloseEvent event) { if (!(event.getPlayer() instanceof Player player)) { @@ -54,6 +56,7 @@ private void onInventoryClose(@NotNull final InventoryCloseEvent event) { } } + @Keep @EventHandler(priority = EventPriority.LOWEST) private void onInventoryClick(@NotNull final InventoryClickEvent event) { if (handleInventoryInteract(event)) { @@ -79,11 +82,10 @@ private void onInventoryClick(@NotNull final InventoryClickEvent event) { event.setCurrentItem(null); // Complete add action in same tick after event completion. - this.plugin.getServer().getScheduler().runTask(this.plugin, () -> { - player.getInventory().addItem(clone); - }); + this.plugin.getServer().getScheduler().runTask(this.plugin, () -> player.getInventory().addItem(clone)); } + @Keep @EventHandler(priority = EventPriority.LOWEST) private void onInventoryDrag(@NotNull final InventoryDragEvent event) { if (handleInventoryInteract(event)) { diff --git a/plugin/src/main/java/com/lishid/openinv/OpenInv.java b/plugin/src/main/java/com/lishid/openinv/OpenInv.java index a945e8fc..f6c9f160 100644 --- a/plugin/src/main/java/com/lishid/openinv/OpenInv.java +++ b/plugin/src/main/java/com/lishid/openinv/OpenInv.java @@ -25,18 +25,16 @@ import com.lishid.openinv.commands.SearchContainerCommand; import com.lishid.openinv.commands.SearchEnchantCommand; import com.lishid.openinv.commands.SearchInvCommand; -import com.lishid.openinv.event.OpenPlayerSaveEvent; +import com.lishid.openinv.event.OpenEvents; import com.lishid.openinv.internal.IAnySilentContainer; import com.lishid.openinv.internal.ISpecialEnderChest; import com.lishid.openinv.internal.ISpecialInventory; import com.lishid.openinv.internal.ISpecialPlayerInventory; import com.lishid.openinv.util.ConfigUpdater; +import com.lishid.openinv.util.InternalAccessor; import com.lishid.openinv.util.Permissions; import com.lishid.openinv.util.StringMetric; import com.lishid.openinv.util.lang.LanguageManager; -import com.lishid.openinv.util.lang.Replacement; -import net.md_5.bungee.api.ChatMessageType; -import net.md_5.bungee.api.chat.TextComponent; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.command.Command; @@ -87,7 +85,7 @@ public void reloadConfig() { super.reloadConfig(); this.offlineHandler = disableOfflineAccess() ? OfflineHandler.REMOVE_AND_CLOSE : OfflineHandler.REQUIRE_PERMISSIONS; if (this.accessor != null && this.accessor.isSupported()) { - this.accessor.reload(); + this.accessor.reload(this.getConfig()); } } @@ -130,12 +128,8 @@ public void onEnable() { // Save default configuration if not present. this.saveDefaultConfig(); - // Get plugin manager - PluginManager pm = this.getServer().getPluginManager(); - - this.accessor = new InternalAccessor(this); - this.languageManager = new LanguageManager(this, "en_us"); + this.accessor = new InternalAccessor(getLogger(), languageManager); try { Class.forName("org.bukkit.entity.Player$Spigot"); @@ -151,20 +145,22 @@ public void onEnable() { // Update existing configuration. May require internal access. new ConfigUpdater(this).checkForUpdates(); + // Get plugin manager + PluginManager pm = this.getServer().getPluginManager(); // Register listeners if (BukkitVersions.MINECRAFT.lessThan(Version.of(1, 21))) { pm.registerEvents(new LegacyInventoryListener(this), this); } else { - pm.registerEvents(new PlayerListener(this), this); + pm.registerEvents(new PlayerListener(this, languageManager), this); } pm.registerEvents(new InventoryListener(this), this); // Register commands to their executors - this.setCommandExecutor(new OpenInvCommand(this), "openinv", "openender"); - this.setCommandExecutor(new SearchContainerCommand(this), "searchcontainer"); - this.setCommandExecutor(new SearchInvCommand(this), "searchinv", "searchender"); - this.setCommandExecutor(new SearchEnchantCommand(this), "searchenchant"); - this.setCommandExecutor(new ContainerSettingCommand(this), "silentcontainer", "anycontainer"); + this.setCommandExecutor(new OpenInvCommand(this, languageManager), "openinv", "openender"); + this.setCommandExecutor(new SearchContainerCommand(this, languageManager), "searchcontainer"); + this.setCommandExecutor(new SearchInvCommand(languageManager), "searchinv", "searchender"); + this.setCommandExecutor(new SearchEnchantCommand(languageManager), "searchenchant"); + this.setCommandExecutor(new ContainerSettingCommand(this, languageManager), "silentcontainer", "anycontainer"); } else { this.sendVersionError(this.getLogger()::warning); @@ -182,9 +178,15 @@ private void setCommandExecutor(@NotNull CommandExecutor executor, String @NotNu } private void sendVersionError(@NotNull Consumer messageMethod) { - if (!this.accessor.isSupported()) { - messageMethod.accept("Your server version (" + this.accessor.getVersion() + ") is not supported."); - messageMethod.accept("Please download the correct version of OpenInv here: " + this.accessor.getReleasesLink()); + if (!accessor.isSupported()) { + messageMethod.accept("Your server version (" + accessor.getVersion() + ") is not supported."); + messageMethod.accept("Please download the correct version of OpenInv here: " + accessor.getReleasesLink()); + + // We check this property late so users can use jars that were remapped by Paper already. + if (Boolean.getBoolean("paper.disable-plugin-rewriting")) { + messageMethod.accept("OpenInv uses Spigot-mapped internals, but you have disabled plugin rewriting in Paper!"); + messageMethod.accept("Please set system property 'paper.disable-plugin-rewriting' to false."); + } } if (!isSpigot) { messageMethod.accept("OpenInv requires that you use Spigot or a Spigot fork. Per the 1.14 update thread"); @@ -242,22 +244,20 @@ public void setSilentContainerStatus(@NotNull final OfflinePlayer offline, final } @Override - public @NotNull ISpecialEnderChest getSpecialEnderChest(@NotNull final Player player, final boolean online) - throws InstantiationException { + public @NotNull ISpecialEnderChest getSpecialEnderChest(@NotNull final Player player, final boolean online) { UUID key = player.getUniqueId(); if (this.enderChests.containsKey(key)) { return this.enderChests.get(key); } - ISpecialEnderChest inv = this.accessor.newSpecialEnderChest(player, online); + ISpecialEnderChest inv = this.accessor.newSpecialEnderChest(player); this.enderChests.put(key, inv); return inv; } @Override - public @NotNull ISpecialPlayerInventory getSpecialInventory(@NotNull final Player player, final boolean online) - throws InstantiationException { + public @NotNull ISpecialPlayerInventory getSpecialInventory(@NotNull final Player player, final boolean online) { UUID key = player.getUniqueId(); if (this.inventories.containsKey(key)) { @@ -436,61 +436,6 @@ private void kickCrossWorldViewers(@NotNull Player player, @NotNull ISpecialInve && !Objects.equals(viewer.getWorld(), player.getWorld())); } - public @Nullable String getLocalizedMessage(@NotNull CommandSender sender, @NotNull String key) { - return this.languageManager.getValue(key, getLocale(sender)); - } - - public @Nullable String getLocalizedMessage( - @NotNull CommandSender sender, - @NotNull String key, - Replacement @NotNull ... replacements) { - return this.languageManager.getValue(key, getLocale(sender), replacements); - } - - private @NotNull String getLocale(@NotNull CommandSender sender) { - if (sender instanceof Player) { - return ((Player) sender).getLocale(); - } else { - return this.getConfig().getString("settings.locale", "en_us"); - } - } - - public void sendMessage(@NotNull CommandSender sender, @NotNull String key) { - String message = getLocalizedMessage(sender, key); - - if (message != null && !message.isEmpty()) { - sender.sendMessage(message); - } - } - - public void sendMessage(@NotNull CommandSender sender, @NotNull String key, Replacement @NotNull... replacements) { - String message = getLocalizedMessage(sender, key, replacements); - - if (message != null && !message.isEmpty()) { - sender.sendMessage(message); - } - } - - public void sendSystemMessage(@NotNull Player player, @NotNull String key) { - String message = getLocalizedMessage(player, key); - - if (message == null) { - return; - } - - int newline = message.indexOf('\n'); - if (newline != -1) { - // No newlines in action bar chat. - message = message.substring(0, newline); - } - - if (message.isEmpty()) { - return; - } - - player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacy(message)); - } - /** * Method for handling a Player going offline. * @@ -562,10 +507,7 @@ void handleCloseInventory(@NotNull ISpecialInventory inventory) { return; } - OpenPlayerSaveEvent event = new OpenPlayerSaveEvent(player, current); - getServer().getPluginManager().callEvent(event); - - if (!event.isCancelled()) { + if (!OpenEvents.saveCancelled(player, current)) { this.accessor.getPlayerDataManager().inject(player).saveData(); } }); @@ -638,8 +580,8 @@ private void setPlayerOnline( inventory, viewer -> !Permissions.ACCESS_ONLINE.hasPermission(viewer) - || !Permissions.ACCESS_CROSSWORLD.hasPermission(viewer) - && !Objects.equals(viewer.getWorld(), inventory.getPlayer().getWorld())); + || (!Permissions.ACCESS_CROSSWORLD.hasPermission(viewer) + && !Objects.equals(viewer.getWorld(), inventory.getPlayer().getWorld()))); } static void ejectViewers(@NotNull ISpecialInventory inventory, @NotNull Predicate<@NotNull HumanEntity> predicate) { diff --git a/plugin/src/main/java/com/lishid/openinv/PlayerListener.java b/plugin/src/main/java/com/lishid/openinv/PlayerListener.java index 64ad0592..ddf868ad 100644 --- a/plugin/src/main/java/com/lishid/openinv/PlayerListener.java +++ b/plugin/src/main/java/com/lishid/openinv/PlayerListener.java @@ -16,7 +16,9 @@ package com.lishid.openinv; +import com.google.errorprone.annotations.Keep; import com.lishid.openinv.util.Permissions; +import com.lishid.openinv.util.lang.LanguageManager; import org.bukkit.entity.Player; import org.bukkit.event.Event.Result; import org.bukkit.event.EventHandler; @@ -29,23 +31,35 @@ import org.bukkit.event.player.PlayerQuitEvent; import org.jetbrains.annotations.NotNull; -record PlayerListener(OpenInv plugin) implements Listener { +class PlayerListener implements Listener { + private final @NotNull OpenInv plugin; + private final @NotNull LanguageManager lang; + + PlayerListener(@NotNull OpenInv plugin, @NotNull LanguageManager lang) { + this.plugin = plugin; + this.lang = lang; + } + + @Keep @EventHandler(priority = EventPriority.LOWEST) private void onPlayerJoin(@NotNull PlayerJoinEvent event) { plugin.setPlayerOnline(event.getPlayer()); } + @Keep @EventHandler(priority = EventPriority.MONITOR) private void onPlayerQuit(@NotNull PlayerQuitEvent event) { plugin.setPlayerOffline(event.getPlayer()); } + @Keep @EventHandler private void onWorldChange(@NotNull PlayerChangedWorldEvent event) { plugin.changeWorld(event.getPlayer()); } + @Keep @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) private void onPlayerInteract(@NotNull PlayerInteractEvent event) { @@ -74,11 +88,11 @@ private void onPlayerInteract(@NotNull PlayerInteractEvent event) { if (any || silent) { if (plugin.getAnySilentContainer().activateContainer(player, silent, event.getClickedBlock())) { if (silent && needsAny) { - plugin.sendSystemMessage(player, "messages.info.containerBlockedSilent"); + lang.sendSystemMessage(player, "messages.info.containerBlockedSilent"); } else if (needsAny) { - plugin.sendSystemMessage(player, "messages.info.containerBlocked"); + lang.sendSystemMessage(player, "messages.info.containerBlocked"); } else if (silent) { - plugin.sendSystemMessage(player, "messages.info.containerSilent"); + lang.sendSystemMessage(player, "messages.info.containerSilent"); } } event.setCancelled(true); diff --git a/plugin/src/main/java/com/lishid/openinv/commands/ContainerSettingCommand.java b/plugin/src/main/java/com/lishid/openinv/commands/ContainerSettingCommand.java index c8dd3c7d..02e5b759 100644 --- a/plugin/src/main/java/com/lishid/openinv/commands/ContainerSettingCommand.java +++ b/plugin/src/main/java/com/lishid/openinv/commands/ContainerSettingCommand.java @@ -16,13 +16,10 @@ package com.lishid.openinv.commands; -import com.lishid.openinv.OpenInv; +import com.lishid.openinv.IOpenInv; import com.lishid.openinv.util.TabCompleter; +import com.lishid.openinv.util.lang.LanguageManager; import com.lishid.openinv.util.lang.Replacement; -import java.util.Collections; -import java.util.List; -import java.util.function.BiConsumer; -import java.util.function.Predicate; import org.bukkit.OfflinePlayer; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -30,18 +27,26 @@ import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; +import java.util.Collections; +import java.util.List; +import java.util.Locale; +import java.util.function.BiConsumer; +import java.util.function.Predicate; + public class ContainerSettingCommand implements TabExecutor { - private final OpenInv plugin; + private final @NotNull IOpenInv plugin; + private final @NotNull LanguageManager lang; - public ContainerSettingCommand(final OpenInv plugin) { + public ContainerSettingCommand(@NotNull IOpenInv plugin, @NotNull LanguageManager lang) { this.plugin = plugin; + this.lang = lang; } @Override public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { if (!(sender instanceof Player player)) { - plugin.sendMessage(sender, "messages.error.consoleUnsupported"); + lang.sendMessage(sender, "messages.error.consoleUnsupported"); return true; } @@ -50,7 +55,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command BiConsumer setSetting = any ? plugin::setAnyContainerStatus : plugin::setSilentContainerStatus; if (args.length > 0) { - args[0] = args[0].toLowerCase(); + args[0] = args[0].toLowerCase(Locale.ENGLISH); if (args[0].equals("on")) { setSetting.accept(player, true); @@ -65,12 +70,12 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command setSetting.accept(player, !getSetting.test(player)); } - String onOff = plugin.getLocalizedMessage(player, getSetting.test(player) ? "messages.info.on" : "messages.info.off"); + String onOff = lang.getLocalizedMessage(player, getSetting.test(player) ? "messages.info.on" : "messages.info.off"); if (onOff == null) { onOff = String.valueOf(getSetting.test(player)); } - plugin.sendMessage( + lang.sendMessage( sender, "messages.info.settingState", new Replacement("%setting%", any ? "AnyContainer" : "SilentContainer"), diff --git a/plugin/src/main/java/com/lishid/openinv/commands/OpenInvCommand.java b/plugin/src/main/java/com/lishid/openinv/commands/OpenInvCommand.java index fba649e7..f082c959 100644 --- a/plugin/src/main/java/com/lishid/openinv/commands/OpenInvCommand.java +++ b/plugin/src/main/java/com/lishid/openinv/commands/OpenInvCommand.java @@ -20,6 +20,7 @@ import com.lishid.openinv.internal.ISpecialInventory; import com.lishid.openinv.util.Permissions; import com.lishid.openinv.util.TabCompleter; +import com.lishid.openinv.util.lang.LanguageManager; import com.lishid.openinv.util.lang.Replacement; import org.bukkit.OfflinePlayer; import org.bukkit.command.Command; @@ -38,12 +39,14 @@ public class OpenInvCommand implements TabExecutor { - private final OpenInv plugin; + private final @NotNull OpenInv plugin; + private final @NotNull LanguageManager lang; private final HashMap openInvHistory = new HashMap<>(); private final HashMap openEnderHistory = new HashMap<>(); - public OpenInvCommand(final OpenInv plugin) { + public OpenInvCommand(@NotNull OpenInv plugin, @NotNull LanguageManager lang) { this.plugin = plugin; + this.lang = lang; } @Override @@ -56,7 +59,7 @@ public boolean onCommand(@NotNull final CommandSender sender, @NotNull final Com } if (!(sender instanceof Player player)) { - plugin.sendMessage(sender, "messages.error.consoleUnsupported"); + lang.sendMessage(sender, "messages.error.consoleUnsupported"); return true; } @@ -86,8 +89,8 @@ public boolean onCommand(@NotNull final CommandSender sender, @NotNull final Com public void run() { final OfflinePlayer offlinePlayer = OpenInvCommand.this.plugin.matchPlayer(name); - if (offlinePlayer == null || !offlinePlayer.hasPlayedBefore() && !offlinePlayer.isOnline()) { - plugin.sendMessage(player, "messages.error.invalidPlayer"); + if (offlinePlayer == null || (!offlinePlayer.hasPlayedBefore() && !offlinePlayer.isOnline())) { + lang.sendMessage(player, "messages.error.invalidPlayer"); return; } @@ -144,20 +147,20 @@ private void openInventory(final Player player, final OfflinePlayer target, bool // Try loading the player's data onlineTarget = this.plugin.loadPlayer(target); } else { - plugin.sendMessage(player, "messages.error.permissionPlayerOffline"); + lang.sendMessage(player, "messages.error.permissionPlayerOffline"); return; } } else { if (Permissions.ACCESS_ONLINE.hasPermission(player)) { onlineTarget = target.getPlayer(); } else { - plugin.sendMessage(player, "messages.error.permissionPlayerOnline"); + lang.sendMessage(player, "messages.error.permissionPlayerOnline"); return; } } if (onlineTarget == null) { - plugin.sendMessage(player, "messages.error.invalidPlayer"); + lang.sendMessage(player, "messages.error.invalidPlayer"); return; } @@ -165,14 +168,14 @@ private void openInventory(final Player player, final OfflinePlayer target, bool if (onlineTarget.equals(player)) { // Permission for opening own inventory. if (!(openinv ? Permissions.INVENTORY_OPEN_SELF : Permissions.ENDERCHEST_OPEN_SELF).hasPermission(player)) { - plugin.sendMessage(player, "messages.error.permissionOpenSelf"); + lang.sendMessage(player, "messages.error.permissionOpenSelf"); return; } } else { // Permission for opening others' inventories. if (!(openinv ? Permissions.INVENTORY_OPEN_OTHER : Permissions.ENDERCHEST_OPEN_OTHER).hasPermission(player)) { - plugin.sendMessage(player, "messages.error.permissionOpenOther"); + lang.sendMessage(player, "messages.error.permissionOpenOther"); return; } @@ -181,7 +184,7 @@ private void openInventory(final Player player, final OfflinePlayer target, bool String permission = "openinv.access.level." + level; if (onlineTarget.hasPermission(permission) && !player.hasPermission(permission)) { - plugin.sendMessage( + lang.sendMessage( player, "messages.error.permissionExempt", new Replacement("%target%", onlineTarget.getDisplayName())); @@ -192,7 +195,7 @@ private void openInventory(final Player player, final OfflinePlayer target, bool // Crossworld check if (!Permissions.ACCESS_CROSSWORLD.hasPermission(player) && !onlineTarget.getWorld().equals(player.getWorld())) { - plugin.sendMessage( + lang.sendMessage( player, "messages.error.permissionCrossWorld", new Replacement("%target%", onlineTarget.getDisplayName())); @@ -210,7 +213,7 @@ private void openInventory(final Player player, final OfflinePlayer target, bool try { inv = openinv ? this.plugin.getSpecialInventory(onlineTarget, online) : this.plugin.getSpecialEnderChest(onlineTarget, online); } catch (Exception e) { - plugin.sendMessage(player, "messages.error.commandException"); + lang.sendMessage(player, "messages.error.commandException"); plugin.getLogger().log(Level.WARNING, "Unable to create ISpecialInventory", e); return; } diff --git a/plugin/src/main/java/com/lishid/openinv/commands/SearchContainerCommand.java b/plugin/src/main/java/com/lishid/openinv/commands/SearchContainerCommand.java index b524b2f9..fd61fc22 100644 --- a/plugin/src/main/java/com/lishid/openinv/commands/SearchContainerCommand.java +++ b/plugin/src/main/java/com/lishid/openinv/commands/SearchContainerCommand.java @@ -16,11 +16,9 @@ package com.lishid.openinv.commands; -import com.lishid.openinv.OpenInv; import com.lishid.openinv.util.TabCompleter; +import com.lishid.openinv.util.lang.LanguageManager; import com.lishid.openinv.util.lang.Replacement; -import java.util.Collections; -import java.util.List; import org.bukkit.Chunk; import org.bukkit.Material; import org.bukkit.World; @@ -30,23 +28,30 @@ import org.bukkit.command.TabExecutor; import org.bukkit.entity.Player; import org.bukkit.inventory.InventoryHolder; +import org.bukkit.plugin.Plugin; import org.jetbrains.annotations.NotNull; +import java.util.Collections; +import java.util.List; +import java.util.Locale; + /** * Command for searching containers in a radius of chunks. */ public class SearchContainerCommand implements TabExecutor { - private final OpenInv plugin; + private final @NotNull Plugin plugin; + private final @NotNull LanguageManager lang; - public SearchContainerCommand(OpenInv plugin) { + public SearchContainerCommand(@NotNull Plugin plugin, @NotNull LanguageManager lang) { this.plugin = plugin; + this.lang = lang; } @Override public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { if (!(sender instanceof Player senderPlayer)) { - plugin.sendMessage(sender, "messages.error.consoleUnsupported"); + lang.sendMessage(sender, "messages.error.consoleUnsupported"); return true; } @@ -55,10 +60,10 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command return false; } - Material material = Material.getMaterial(args[0].toUpperCase()); + Material material = Material.matchMaterial(args[0]); if (material == null) { - plugin.sendMessage( + lang.sendMessage( sender, "messages.error.invalidMaterial", new Replacement("%target%", args[0])); @@ -97,7 +102,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command if (!holder.getInventory().contains(material)) { continue; } - locations.append(holder.getInventory().getType().name().toLowerCase()).append(" (") + locations.append(holder.getInventory().getType().name().toLowerCase(Locale.ENGLISH)).append(" (") .append(tileEntity.getX()).append(',').append(tileEntity.getY()).append(',') .append(tileEntity.getZ()).append("), "); } @@ -108,14 +113,14 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command if (!locations.isEmpty()) { locations.delete(locations.length() - 2, locations.length()); } else { - plugin.sendMessage( + lang.sendMessage( sender, "messages.info.container.noMatches", new Replacement("%target%", material.name())); return true; } - plugin.sendMessage( + lang.sendMessage( sender, "messages.info.container.matches", new Replacement("%target%", material.name()), diff --git a/plugin/src/main/java/com/lishid/openinv/commands/SearchEnchantCommand.java b/plugin/src/main/java/com/lishid/openinv/commands/SearchEnchantCommand.java index 8abaec9e..3e66acca 100644 --- a/plugin/src/main/java/com/lishid/openinv/commands/SearchEnchantCommand.java +++ b/plugin/src/main/java/com/lishid/openinv/commands/SearchEnchantCommand.java @@ -16,9 +16,10 @@ package com.lishid.openinv.commands; -import com.lishid.openinv.OpenInv; import com.lishid.openinv.util.TabCompleter; +import com.lishid.openinv.util.lang.LanguageManager; import com.lishid.openinv.util.lang.Replacement; +import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.NamespacedKey; import org.bukkit.Registry; @@ -35,6 +36,7 @@ import java.util.Collections; import java.util.List; +import java.util.Locale; /** * Command adding the ability to search online players' inventories for enchantments of a specific @@ -44,10 +46,10 @@ */ public class SearchEnchantCommand implements TabExecutor { - private final OpenInv plugin; + private final LanguageManager lang; - public SearchEnchantCommand(OpenInv plugin) { - this.plugin = plugin; + public SearchEnchantCommand(LanguageManager lang) { + this.lang = lang; } @Override @@ -63,9 +65,11 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command try { level = Integer.parseInt(argument); continue; - } catch (NumberFormatException ignored) {} + } catch (NumberFormatException ignored) { + // Not a level being specified. + } - argument = argument.toLowerCase(); + argument = argument.toLowerCase(Locale.ENGLISH); NamespacedKey key = NamespacedKey.fromString(argument); if (key == null) { continue; @@ -83,7 +87,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command } StringBuilder players = new StringBuilder(); - for (Player player : plugin.getServer().getOnlinePlayers()) { + for (Player player : Bukkit.getServer().getOnlinePlayers()) { boolean flagInventory = containsEnchantment(player.getInventory(), enchant, level); boolean flagEnder = containsEnchantment(player.getEnderChest(), enchant, level); @@ -110,14 +114,14 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command // Matches found, delete trailing comma and space players.delete(players.length() - 2, players.length()); } else { - plugin.sendMessage( + lang.sendMessage( sender, "messages.info.player.noMatches", new Replacement("%target%", (enchant != null ? enchant.getKey().toString() : "") + " >= " + level)); return true; } - plugin.sendMessage( + lang.sendMessage( sender, "messages.info.player.matches", new Replacement("%target%", (enchant != null ? enchant.getKey().toString() : "") + " >= " + level), diff --git a/plugin/src/main/java/com/lishid/openinv/commands/SearchInvCommand.java b/plugin/src/main/java/com/lishid/openinv/commands/SearchInvCommand.java index f40588c5..234d4ead 100644 --- a/plugin/src/main/java/com/lishid/openinv/commands/SearchInvCommand.java +++ b/plugin/src/main/java/com/lishid/openinv/commands/SearchInvCommand.java @@ -16,9 +16,10 @@ package com.lishid.openinv.commands; -import com.lishid.openinv.OpenInv; import com.lishid.openinv.util.TabCompleter; +import com.lishid.openinv.util.lang.LanguageManager; import com.lishid.openinv.util.lang.Replacement; +import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -33,10 +34,10 @@ public class SearchInvCommand implements TabExecutor { - private final OpenInv plugin; + private final LanguageManager lang; - public SearchInvCommand(OpenInv plugin) { - this.plugin = plugin; + public SearchInvCommand(LanguageManager lang) { + this.lang = lang; } @Override @@ -45,11 +46,11 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command Material material = null; if (args.length >= 1) { - material = Material.getMaterial(args[0].toUpperCase()); + material = Material.matchMaterial(args[0]); } if (material == null) { - plugin.sendMessage( + lang.sendMessage( sender, "messages.error.invalidMaterial", new Replacement("%target%", args.length > 0 ? args[0] : "null")); @@ -62,7 +63,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command try { count = Integer.parseInt(args[1]); } catch (NumberFormatException ex) { - plugin.sendMessage( + lang.sendMessage( sender, "messages.error.invalidNumber", new Replacement("%target%", args[1])); @@ -72,7 +73,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command StringBuilder players = new StringBuilder(); boolean searchInv = command.getName().equals("searchinv"); - for (Player player : plugin.getServer().getOnlinePlayers()) { + for (Player player : Bukkit.getServer().getOnlinePlayers()) { Inventory inventory = searchInv ? player.getInventory() : player.getEnderChest(); int total = 0; for (ItemStack itemStack : inventory.getContents()) { @@ -90,14 +91,14 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command if (!players.isEmpty()) { players.delete(players.length() - 2, players.length()); } else { - plugin.sendMessage( + lang.sendMessage( sender, "messages.info.player.noMatches", new Replacement("%target%", material.name())); return true; } - plugin.sendMessage( + lang.sendMessage( sender, "messages.info.player.matches", new Replacement("%target%", material.name()), diff --git a/plugin/src/main/java/com/lishid/openinv/InternalAccessor.java b/plugin/src/main/java/com/lishid/openinv/util/InternalAccessor.java similarity index 50% rename from plugin/src/main/java/com/lishid/openinv/InternalAccessor.java rename to plugin/src/main/java/com/lishid/openinv/util/InternalAccessor.java index 63c2a067..55cea7b0 100644 --- a/plugin/src/main/java/com/lishid/openinv/InternalAccessor.java +++ b/plugin/src/main/java/com/lishid/openinv/util/InternalAccessor.java @@ -14,76 +14,44 @@ * along with this program. If not, see . */ -package com.lishid.openinv; +package com.lishid.openinv.util; import com.github.jikoo.planarwrappers.util.version.BukkitVersions; import com.github.jikoo.planarwrappers.util.version.Version; +import com.lishid.openinv.internal.Accessor; import com.lishid.openinv.internal.IAnySilentContainer; -import com.lishid.openinv.internal.IPlayerDataManager; import com.lishid.openinv.internal.ISpecialEnderChest; -import com.lishid.openinv.internal.ISpecialInventory; import com.lishid.openinv.internal.ISpecialPlayerInventory; -import com.lishid.openinv.internal.PlaceholderParser; -import com.lishid.openinv.util.InventoryAccess; +import com.lishid.openinv.internal.PlayerManager; +import com.lishid.openinv.util.lang.LanguageManager; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.Player; -import org.bukkit.plugin.Plugin; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.lang.reflect.Constructor; -import java.util.logging.Level; +import java.util.logging.Logger; -class InternalAccessor { +public class InternalAccessor { - private final @NotNull Plugin plugin; - private final @Nullable String versionPackage; - private boolean supported = false; - private IPlayerDataManager playerDataManager; - private IAnySilentContainer anySilentContainer; - private @Nullable PlaceholderParser placeholderParser; + private @Nullable Accessor internal; - InternalAccessor(@NotNull Plugin plugin) { - this.plugin = plugin; - this.versionPackage = getVersionPackage(); - checkSupported(); - } + public InternalAccessor(@NotNull Logger logger, @NotNull LanguageManager lang) { - private void checkSupported() { - if (versionPackage == null) { - return; - } try { - try { - Class.forName("com.lishid.openinv.internal." + this.versionPackage + ".inventory.OpenInventory"); - this.placeholderParser = createObject(PlaceholderParser.class, "inventory.PlaceholderManager"); - } catch (ClassNotFoundException e) { - Class.forName("com.lishid.openinv.internal." + this.versionPackage + ".SpecialPlayerInventory"); + if (BukkitVersions.MINECRAFT.equals(Version.of(1, 21))) { + internal = new com.lishid.openinv.internal.v1_21_R1.InternalAccessor(logger, lang); + } else if (BukkitVersions.MINECRAFT.equals(Version.of(1, 20, 4))) { + internal = new com.lishid.openinv.internal.v1_20_R3.InternalAccessor(logger, lang); + } else if (BukkitVersions.MINECRAFT.equals(Version.of(1, 20, 6))) { + internal = new com.lishid.openinv.internal.v1_20_R4.InternalAccessor(logger, lang); } - Class.forName("com.lishid.openinv.internal." + this.versionPackage + ".inventory.OpenInventory"); - Class.forName("com.lishid.openinv.internal." + this.versionPackage + ".SpecialEnderChest"); - this.playerDataManager = this.createObject(IPlayerDataManager.class, "PlayerDataManager"); - this.anySilentContainer = this.createObject(IAnySilentContainer.class, "AnySilentContainer"); - this.supported = InventoryAccess.isUsable(); - } catch (Exception ignored) {} - } - - private @Nullable String getVersionPackage() { - 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.equals(Version.of(1, 20, 4))) { // 1.20.4 - return "v1_20_R3"; - } - if (BukkitVersions.MINECRAFT.equals(Version.of(1, 20, 6))) { // 1.20.6 - return "v1_20_R4"; - } - if (BukkitVersions.MINECRAFT.greaterThan(Version.of(1, 21))) { - return null; + if (internal != null) { + InventoryAccess.setProvider(internal::get); + } + } catch (NoClassDefFoundError | Exception e) { + internal = null; + InventoryAccess.setProvider(null); } - // 1.21 - return "v1_21_R1"; } public String getReleasesLink() { @@ -150,68 +118,9 @@ public String getReleasesLink() { 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"; } - private @NotNull T createObject( - @NotNull Class assignableClass, - @NotNull String className, - @NotNull Object @NotNull ... params) - throws ClassCastException, ReflectiveOperationException { - // Fetch internal class if it exists. - Class internalClass = Class.forName("com.lishid.openinv.internal." + this.versionPackage + "." + className); - - // Quick return: no parameters, no need to fiddle about finding the correct constructor. - if (params.length == 0) { - return assignableClass.cast(internalClass.getConstructor().newInstance()); - } - - // Search constructors for one matching the given parameters - nextConstructor: for (Constructor constructor : internalClass.getConstructors()) { - Class[] requiredClasses = constructor.getParameterTypes(); - if (requiredClasses.length != params.length) { - continue; - } - for (int i = 0; i < params.length; ++i) { - if (!requiredClasses[i].isAssignableFrom(params[i].getClass())) { - continue nextConstructor; - } - } - return assignableClass.cast(constructor.newInstance(params)); - } - - StringBuilder builder = new StringBuilder("Found class ").append(internalClass.getName()) - .append(" but cannot find any matching constructors for ["); - for (Object object : params) { - builder.append(object.getClass().getName()).append(", "); - } - builder.delete(builder.length() - 2, builder.length()); - - String message = builder.append(']').toString(); - this.plugin.getLogger().warning(message); - - throw new NoSuchMethodException(message); - } - - private @NotNull T createSpecialInventory( - @NotNull Class assignableClass, - @NotNull String className, - @NotNull Object @NotNull ... params) throws InstantiationException { - if (!this.supported) { - throw new IllegalStateException(String.format("Unsupported server version %s!", BukkitVersions.MINECRAFT)); - } - try { - return this.createObject(assignableClass, className, params); - } catch (Exception original) { - InstantiationException exception = new InstantiationException(String.format("Unable to create a new %s", className)); - exception.initCause(original.fillInStackTrace()); - throw exception; - } - } - /** * Creates an instance of the IAnySilentContainer implementation for the current server version. * @@ -219,10 +128,10 @@ public String getReleasesLink() { * @throws IllegalStateException if server version is unsupported */ public @NotNull IAnySilentContainer getAnySilentContainer() { - if (!this.supported) { + if (internal == null) { throw new IllegalStateException(String.format("Unsupported server version %s!", BukkitVersions.MINECRAFT)); } - return this.anySilentContainer; + return internal.getAnySilentContainer(); } /** @@ -231,28 +140,19 @@ public String getReleasesLink() { * @return the IPlayerDataManager * @throws IllegalStateException if server version is unsupported */ - public @NotNull IPlayerDataManager getPlayerDataManager() { - if (!this.supported) { + public @NotNull PlayerManager getPlayerDataManager() { + if (internal == null) { throw new IllegalStateException(String.format("Unsupported server version %s!", BukkitVersions.MINECRAFT)); } - return this.playerDataManager; + return internal.getPlayerManager(); } /** * Reload internal features. */ - void reload() { - if (placeholderParser == null) { - return; - } - - ConfigurationSection placeholders = plugin.getConfig().getConfigurationSection("placeholders"); - if (placeholders != null) { - try { - placeholderParser.load(placeholders); - } catch (Exception e) { - plugin.getLogger().log(Level.WARNING, "Caught exception loading placeholder overrides!", e); - } + public void reload(ConfigurationSection config) { + if (internal != null) { + internal.reload(config); } } @@ -271,35 +171,33 @@ void reload() { * @return true if initialized for a supported server version */ public boolean isSupported() { - return this.supported; + return internal != null; } /** - * Creates an instance of the ISpecialEnderChest implementation for the given Player, or - * null if the current version is unsupported. + * Creates an instance of the ISpecialEnderChest implementation for the given Player. * * @param player the Player - * @param online true if the Player is online * @return the ISpecialEnderChest created - * @throws InstantiationException if the ISpecialEnderChest could not be instantiated */ - public ISpecialEnderChest newSpecialEnderChest(final Player player, final boolean online) throws InstantiationException { - return this.createSpecialInventory(ISpecialEnderChest.class, "SpecialEnderChest", player, online); + public ISpecialEnderChest newSpecialEnderChest(final Player player) { + if (internal == null) { + throw new IllegalStateException(String.format("Unsupported server version %s!", BukkitVersions.MINECRAFT)); + } + return internal.createEnderChest(player); } /** - * Creates an instance of the ISpecialPlayerInventory implementation for the given Player.. + * Creates an instance of the ISpecialPlayerInventory implementation for the given Player. * * @param player the Player * @return the ISpecialPlayerInventory created - * @throws InstantiationException if the ISpecialPlayerInventory could not be instantiated */ - public ISpecialPlayerInventory newSpecialPlayerInventory(final Player player) throws InstantiationException { - try { - return this.createSpecialInventory(ISpecialPlayerInventory.class, "inventory.OpenInventory", player); - } catch (InstantiationException ignored) { - return this.createSpecialInventory(ISpecialPlayerInventory.class, "SpecialPlayerInventory", player, player.isOnline()); + public ISpecialPlayerInventory newSpecialPlayerInventory(final Player player) { + if (internal == null) { + throw new IllegalStateException(String.format("Unsupported server version %s!", BukkitVersions.MINECRAFT)); } + return internal.createPlayerInventory(player); } } diff --git a/api/src/main/java/com/lishid/openinv/util/StringMetric.java b/plugin/src/main/java/com/lishid/openinv/util/StringMetric.java similarity index 100% rename from api/src/main/java/com/lishid/openinv/util/StringMetric.java rename to plugin/src/main/java/com/lishid/openinv/util/StringMetric.java diff --git a/plugin/src/main/java/com/lishid/openinv/util/TabCompleter.java b/plugin/src/main/java/com/lishid/openinv/util/TabCompleter.java index 882274fe..f61be773 100644 --- a/plugin/src/main/java/com/lishid/openinv/util/TabCompleter.java +++ b/plugin/src/main/java/com/lishid/openinv/util/TabCompleter.java @@ -16,20 +16,21 @@ package com.lishid.openinv.util; +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.util.StringUtil; + import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Locale; import java.util.function.Function; -import org.bukkit.Bukkit; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -import org.bukkit.util.StringUtil; /** * Utility class for common tab completions. */ -public class TabCompleter { +public final class TabCompleter { /** * Offer tab completions for whole numbers. @@ -67,7 +68,7 @@ public static List completeEnum(String argument, Class List completions = new ArrayList<>(); for (Enum enumConstant : enumClazz.getEnumConstants()) { - String name = enumConstant.name().toLowerCase(); + String name = enumConstant.name().toLowerCase(Locale.ENGLISH); if (name.startsWith(argument)) { completions.add(name); } @@ -133,7 +134,7 @@ public static List completeObject(String argument, Function completions = new ArrayList<>(); for (T option : options) { - String optionString = converter.apply(option).toLowerCase(); + String optionString = converter.apply(option).toLowerCase(Locale.ENGLISH); if (optionString.startsWith(argument)) { completions.add(optionString); } @@ -142,6 +143,8 @@ public static List completeObject(String argument, Functionopeninvparent OpenInv http://dev.bukkit.org/bukkit-plugins/openinv/ + + common + 5.0.0-SNAPSHOT pom UTF-8 - 17 - 17 + 17 17 unknown @@ -39,23 +41,21 @@ all api - plugin + common internal/v1_21_R1 internal/v1_20_R4 internal/v1_20_R3 - assembly + plugin - default + api-only true api - plugin - assembly @@ -89,23 +89,18 @@ openinvapi com.lishid compile - 5.0.0-SNAPSHOT + ${project.version} - openinvplugincore + openinvcommon com.lishid compile - 5.0.0-SNAPSHOT - - - com.lishid - openinvapi - - + ${project.version} planarwrappers com.github.jikoo + compile 3.2.3 @@ -126,39 +121,55 @@ maven-shade-plugin - - - - - com.lishid:openinv* - - ** - - - - *:* - - - META-INF/MANIFEST.MF - - - - true - - - - package - - shade - - - org.apache.maven.plugins 3.6.0 + + maven-resources-plugin + org.apache.maven.plugins + 3.3.1 + + + + maven-jar-plugin + org.apache.maven.plugins + 3.4.1 + + + + spigot + + + + + maven-compiler-plugin + + + + com.google.errorprone + error_prone_core + 2.28.0 + + + + -XDcompilePolicy=simple + -Xplugin:ErrorProne + -J--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED + -J--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED + -J--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED + -J--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED + -J--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED + -J--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED + -J--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED + -J--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED + -J--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED + -J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED + + true + org.apache.maven.plugins 3.13.0 @@ -170,9 +181,7 @@ - net.md-5 specialsource-maven-plugin - 2.0.3 package @@ -201,6 +210,8 @@ + net.md-5 + 2.0.3