Continue migrating terminology to "container"

Clean up documentation a bit, prettify
This commit is contained in:
Jikoo
2022-05-15 10:45:52 -04:00
parent 6ad6e0c6bc
commit 0b761a6fc3
16 changed files with 249 additions and 206 deletions

View File

@@ -36,11 +36,16 @@ import org.jetbrains.annotations.Nullable;
/** /**
* Interface defining behavior for the OpenInv plugin. * Interface defining behavior for the OpenInv plugin.
*
* @author Jikoo
*/ */
public interface IOpenInv { public interface IOpenInv {
/**
* Check if the server version is supported by OpenInv.
*
* @return true if the server version is supported
*/
boolean isSupportedVersion();
/** /**
* Check the configuration value for whether OpenInv saves player data when unloading players. This is exclusively * Check the configuration value for whether OpenInv saves player data when unloading players. This is exclusively
* for users who do not allow editing of inventories, only viewing, and wish to prevent any possibility of bugs such * for users who do not allow editing of inventories, only viewing, and wish to prevent any possibility of bugs such
@@ -51,8 +56,9 @@ public interface IOpenInv {
boolean disableSaving(); boolean disableSaving();
/** /**
* Check the configuration value for whether OpenInv allows offline access. This does not prevent other plugins from * Check the configuration value for whether OpenInv allows offline access. If true, OpenInv will not load or allow
* using existing loaded players while offline. * modification of players while they are not online. This does not prevent other plugins from using existing loaded
* players who have gone offline.
* *
* @return false unless configured otherwise * @return false unless configured otherwise
* @since 4.2.0 * @since 4.2.0
@@ -60,9 +66,9 @@ public interface IOpenInv {
boolean disableOfflineAccess(); boolean disableOfflineAccess();
/** /**
* Gets the active ISilentContainer implementation. * Get the active {@link IAnySilentContainer} implementation.
* *
* @return the ISilentContainer * @return the active implementation for the server version
* @throws IllegalStateException if the server version is unsupported * @throws IllegalStateException if the server version is unsupported
*/ */
@NotNull IAnySilentContainer getAnySilentContainer(); @NotNull IAnySilentContainer getAnySilentContainer();
@@ -76,18 +82,74 @@ public interface IOpenInv {
} }
/** /**
* Gets the provided player's AnyChest setting. * @deprecated Use {@link #getAnyContainerStatus(OfflinePlayer)}. Not all containers are chests.
*
* @param player the OfflinePlayer
* @return true if AnyChest is enabled
* @throws IllegalStateException if the server version is unsupported
*/ */
boolean getPlayerAnyChestStatus(@NotNull OfflinePlayer player); @Deprecated(forRemoval = true, since = "4.2.0")
default boolean getPlayerAnyChestStatus(@NotNull OfflinePlayer offline) {
return getAnyContainerStatus(offline);
}
/** /**
* Gets a unique identifier by which the OfflinePlayer can be referenced. Using the value * Get whether a user has AnyContainer mode enabled.
* returned to look up a Player will generally be much faster for later implementations.
* *
* @param offline the user to obtain the state of
* @return true if AnyContainer mode is enabled
*/
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.
*
* @param offline the user to set the state of
* @param status the state of the mode
*/
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.
*
* @param offline the user to obtain the state of
* @return true if SilentContainer mode is enabled
*/
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.
*
* @param offline the user to set the state of
* @param status the state of the mode
*/
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 * @param offline the OfflinePlayer
* @return the identifier * @return the identifier
* @throws IllegalStateException if the server version is unsupported * @throws IllegalStateException if the server version is unsupported
@@ -97,42 +159,35 @@ public interface IOpenInv {
} }
/** /**
* Gets a player's SilentChest setting. * Get an {@link ISpecialEnderChest} for a user.
* *
* @param offline the OfflinePlayer * @param player the {@link Player} owning the inventory
* @return true if SilentChest is enabled * @param online whether the owner is currently online
* @return the created inventory
* @throws IllegalStateException if the server version is unsupported * @throws IllegalStateException if the server version is unsupported
*/ * @throws InstantiationException if there was an issue creating the inventory
boolean getPlayerSilentChestStatus(@NotNull OfflinePlayer offline);
/**
* Gets an ISpecialEnderChest for the given Player.
*
* @param player the Player
* @param online true if the Player is currently online
* @return the ISpecialEnderChest
* @throws IllegalStateException if the server version is unsupported
* @throws InstantiationException if the ISpecialEnderChest could not be instantiated
*/ */
@NotNull ISpecialEnderChest getSpecialEnderChest(@NotNull Player player, boolean online) throws InstantiationException; @NotNull ISpecialEnderChest getSpecialEnderChest(@NotNull Player player, boolean online) throws InstantiationException;
/** /**
* Gets an ISpecialPlayerInventory for the given Player. * Get an {@link ISpecialPlayerInventory} for a user.
* *
* @param player the Player * @param player the {@link Player} owning the inventory
* @param online true if the Player is currently online * @param online whether the owner is currently online
* @return the ISpecialPlayerInventory * @return the created inventory
* @throws IllegalStateException if the server version is unsupported * @throws IllegalStateException if the server version is unsupported
* @throws InstantiationException if the ISpecialPlayerInventory could not be instantiated * @throws InstantiationException if there was an issue creating the inventory
*/ */
@NotNull ISpecialPlayerInventory getSpecialInventory(@NotNull Player player, boolean online) throws InstantiationException; @NotNull ISpecialPlayerInventory getSpecialInventory(@NotNull Player player, boolean online) throws InstantiationException;
/** /**
* Checks if the server version is supported by OpenInv. * Open an {@link ISpecialInventory} for a {@link Player}.
* *
* @return true if the server version is supported * @param player the viewer
* @param inventory the inventory to open
* @return the resulting {@link InventoryView}
*/ */
boolean isSupportedVersion(); @Nullable InventoryView openInventory(@NotNull Player player, @NotNull ISpecialInventory inventory);
/** /**
* Check if a {@link Player} is currently loaded by OpenInv. * Check if a {@link Player} is currently loaded by OpenInv.
@@ -144,23 +199,25 @@ public interface IOpenInv {
boolean isPlayerLoaded(@NotNull UUID playerUuid); boolean isPlayerLoaded(@NotNull UUID playerUuid);
/** /**
* Load a Player from an OfflinePlayer. May return null under some circumstances. * Load a {@link Player} from an {@link OfflinePlayer}. If the user has not played before or the default world for
* the server is not loaded, this will return {@code null}.
* *
* @param offline the OfflinePlayer to load a Player for * @param offline the {@code OfflinePlayer} to load a {@code Player} for
* @return the Player, or null * @return the loaded {@code Player}
* @throws IllegalStateException if the server version is unsupported * @throws IllegalStateException if the server version is unsupported
*/ */
@Nullable Player loadPlayer(@NotNull final OfflinePlayer offline); @Nullable Player loadPlayer(@NotNull final OfflinePlayer offline);
/** /**
* Get an OfflinePlayer by name. * Match an existing {@link OfflinePlayer}. If the name is a {@link UUID#toString() UUID string}, this will only
* <p> * return the user if they have actually played on the server before, unlike {@link Bukkit#getOfflinePlayer(UUID)}.
* Note: This method is potentially very heavily blocking. It should not ever be called on the *
* <p>This method is potentially very heavily blocking. It should not ever be called on the
* main thread, and if it is, a stack trace will be displayed alerting server owners to the * main thread, and if it is, a stack trace will be displayed alerting server owners to the
* call. * call.
* *
* @param name the name of the Player * @param name the string to match
* @return the OfflinePlayer with the closest matching name or null if no players have ever logged in * @return the user with the closest matching name
*/ */
default @Nullable OfflinePlayer matchPlayer(@NotNull String name) { default @Nullable OfflinePlayer matchPlayer(@NotNull String name) {
@@ -168,7 +225,7 @@ public interface IOpenInv {
if (Bukkit.getServer().isPrimaryThread()) { if (Bukkit.getServer().isPrimaryThread()) {
this.getLogger().warning("Call to OpenInv#matchPlayer made on the main thread!"); this.getLogger().warning("Call to OpenInv#matchPlayer made on the main thread!");
this.getLogger().warning("This can cause the server to hang, potentially severely."); this.getLogger().warning("This can cause the server to hang, potentially severely.");
this.getLogger().log(Level.WARNING, new Throwable("Current stack trace"), () -> "Current stack trace"); this.getLogger().log(Level.WARNING, "Current stack trace", new Throwable("Current stack trace"));
} }
OfflinePlayer player; OfflinePlayer player;
@@ -176,32 +233,38 @@ public interface IOpenInv {
try { try {
UUID uuid = UUID.fromString(name); UUID uuid = UUID.fromString(name);
player = Bukkit.getOfflinePlayer(uuid); player = Bukkit.getOfflinePlayer(uuid);
// Ensure player is a real player, otherwise return null // Ensure player is an existing player.
if (player.hasPlayedBefore() || player.isOnline()) { if (player.hasPlayedBefore() || player.isOnline()) {
return player; return player;
} }
// Return null otherwise.
return null;
} catch (IllegalArgumentException ignored) { } catch (IllegalArgumentException ignored) {
// Not a UUID // Not a UUID
} }
// Exact online match first.
player = Bukkit.getServer().getPlayerExact(name); player = Bukkit.getServer().getPlayerExact(name);
if (player != null) { if (player != null) {
return player; return player;
} }
// Exact offline match second - ensure offline access works when matchable users are online.
player = Bukkit.getServer().getOfflinePlayer(name); player = Bukkit.getServer().getOfflinePlayer(name);
if (player.hasPlayedBefore()) { if (player.hasPlayedBefore()) {
return player; return player;
} }
// Inexact online match.
player = Bukkit.getServer().getPlayer(name); player = Bukkit.getServer().getPlayer(name);
if (player != null) { if (player != null) {
return player; return player;
} }
// Finally, inexact offline match.
float bestMatch = 0; float bestMatch = 0;
for (OfflinePlayer offline : Bukkit.getServer().getOfflinePlayers()) { for (OfflinePlayer offline : Bukkit.getServer().getOfflinePlayers()) {
if (offline.getName() == null) { if (offline.getName() == null) {
@@ -226,20 +289,7 @@ public interface IOpenInv {
} }
/** /**
* Open an ISpecialInventory for a Player. * @deprecated OpenInv uses action bar chat for notifications. Whether they show is based on language settings.
*
* @param player the Player
* @param inventory the ISpecialInventory
* @return the InventoryView for the opened ISpecialInventory
*/
@Nullable InventoryView openInventory(@NotNull Player player, @NotNull ISpecialInventory inventory);
/**
* Check the configuration value for whether or not OpenInv displays a notification to the user
* when a container is activated with AnyChest.
*
* @return true unless configured otherwise
* @deprecated OpenInv uses action bar chat for notifications. Whether or not they show is based on language settings.
*/ */
@Deprecated(forRemoval = true) @Deprecated(forRemoval = true)
default boolean notifyAnyChest() { default boolean notifyAnyChest() {
@@ -288,28 +338,9 @@ public interface IOpenInv {
default void retainPlayer(@NotNull Player player, @NotNull Plugin plugin) {} default void retainPlayer(@NotNull Player player, @NotNull Plugin plugin) {}
/** /**
* Sets a player's AnyChest setting. * Forcibly close inventories of and unload any cached data for a user.
* *
* @param offline the OfflinePlayer * @param offline the {@link OfflinePlayer} to unload
* @param status the status
* @throws IllegalStateException if the server version is unsupported
*/
void setPlayerAnyChestStatus(@NotNull OfflinePlayer offline, boolean status);
/**
* Sets a player's SilentChest setting.
*
* @param offline the OfflinePlayer
* @param status the status
* @throws IllegalStateException if the server version is unsupported
*/
void setPlayerSilentChestStatus(@NotNull OfflinePlayer offline, boolean status);
/**
* Forcibly unload a cached Player's data.
*
* @param offline the OfflinePlayer to unload
* @throws IllegalStateException if the server version is unsupported
*/ */
void unload(@NotNull OfflinePlayer offline); void unload(@NotNull OfflinePlayer offline);

View File

@@ -33,39 +33,38 @@ import org.jetbrains.annotations.NotNull;
public interface IAnySilentContainer { public interface IAnySilentContainer {
/** /**
* Opens the container at the given coordinates for the Player. If you do not want blocked * Forcibly open the container at the given coordinates for the Player. This will open blocked containers! Be sure
* containers to open, be sure to check {@link #isAnyContainerNeeded(Block)} * to check {@link #isAnyContainerNeeded(Block)} first if that is not desirable.
* first.
* *
* @param player the Player opening the container * @param player the {@link Player} opening the container
* @param silent whether the container's noise is to be silenced * @param silent whether the container's noise is to be silenced
* @param block the Block * @param block the {@link Block} of the container
* @return true if the container can be opened * @return true if the container can be opened
*/ */
boolean activateContainer(@NotNull Player player, boolean silent, @NotNull Block block); boolean activateContainer(@NotNull Player player, boolean silent, @NotNull Block block);
/** /**
* Closes the Player's currently open container silently, if necessary. * Perform operations required to close the current container silently.
* *
* @param player the Player closing a container * @param player the {@link Player} closing a container
*/ */
void deactivateContainer(@NotNull Player player); void deactivateContainer(@NotNull Player player);
/** /**
* @deprecated use {@link #isAnyContainerNeeded(Block)}
* @param player the player opening the container * @param player the player opening the container
* @param block the block * @param block the {@link Block} of the container
* @return true if the container is blocked * @return true if the container is blocked
* @deprecated use {@link #isAnyContainerNeeded(Block)}
*/ */
@Deprecated @Deprecated(forRemoval = true, since = "4.1.9")
default boolean isAnyContainerNeeded(@NotNull Player player, @NotNull Block block) { default boolean isAnyContainerNeeded(@NotNull Player player, @NotNull Block block) {
return isAnyContainerNeeded(block); return isAnyContainerNeeded(block);
} }
/** /**
* Checks if the container at the given coordinates is blocked. * Check if the container at the given coordinates is blocked.
* *
* @param block the Block * @param block the {@link Block} of the container
* @return true if the container is blocked * @return true if the container is blocked
*/ */
default boolean isAnyContainerNeeded(@NotNull Block block) { default boolean isAnyContainerNeeded(@NotNull Block block) {
@@ -82,8 +81,8 @@ public interface IAnySilentContainer {
} }
// Shulker boxes require 1/2 a block clear in the direction they open. // Shulker boxes require 1/2 a block clear in the direction they open.
if (blockState instanceof ShulkerBox) { if (blockState instanceof ShulkerBox shulker) {
if (isShulkerBlocked((ShulkerBox) blockState)) { if (isShulkerBlocked(shulker)) {
return true; return true;
} }
} }
@@ -97,11 +96,10 @@ public interface IAnySilentContainer {
} }
BlockData blockData = block.getBlockData(); BlockData blockData = block.getBlockData();
if (!(blockData instanceof Chest) || ((Chest) blockData).getType() == Chest.Type.SINGLE) { if (!(blockData instanceof Chest chest) || ((Chest) blockData).getType() == Chest.Type.SINGLE) {
return false; return false;
} }
Chest chest = (Chest) blockData;
int ordinal = (chest.getFacing().ordinal() + 4 + (chest.getType() == Chest.Type.RIGHT ? -1 : 1)) % 4; int ordinal = (chest.getFacing().ordinal() + 4 + (chest.getType() == Chest.Type.RIGHT ? -1 : 1)) % 4;
BlockFace relativeFace = BlockFace.values()[ordinal]; BlockFace relativeFace = BlockFace.values()[ordinal];
org.bukkit.block.Block relative = block.getRelative(relativeFace); org.bukkit.block.Block relative = block.getRelative(relativeFace);
@@ -111,11 +109,10 @@ public interface IAnySilentContainer {
} }
BlockData relativeData = relative.getBlockData(); BlockData relativeData = relative.getBlockData();
if (!(relativeData instanceof Chest)) { if (!(relativeData instanceof Chest relativeChest)) {
return false; return false;
} }
Chest relativeChest = (Chest) relativeData;
if (relativeChest.getFacing() != chest.getFacing() if (relativeChest.getFacing() != chest.getFacing()
|| relativeChest.getType() != (chest.getType() == Chest.Type.RIGHT ? Chest.Type.LEFT : Chest.Type.RIGHT)) { || relativeChest.getType() != (chest.getType() == Chest.Type.RIGHT ? Chest.Type.LEFT : Chest.Type.RIGHT)) {
return false; return false;
@@ -124,25 +121,31 @@ public interface IAnySilentContainer {
return isChestBlocked(relative); return isChestBlocked(relative);
} }
boolean isShulkerBlocked(ShulkerBox block); /**
* Check if a {@link ShulkerBox} cannot be opened under ordinary circumstances.
*
* @param shulkerBox the shulker box container
* @return whether the container is blocked
*/
boolean isShulkerBlocked(@NotNull ShulkerBox shulkerBox);
/** /**
* Determine whether or not a chest is blocked. * Check if a chest cannot be opened under ordinary circumstances.
* *
* @param chest the chest block * @param chest the chest block
* @return true if the chest block cannot be opened under ordinary circumstances * @return whether the container is blocked
*/ */
default boolean isChestBlocked(Block chest) { default boolean isChestBlocked(@NotNull Block chest) {
org.bukkit.block.Block relative = chest.getRelative(0, 1, 0); org.bukkit.block.Block relative = chest.getRelative(0, 1, 0);
return relative.getType().isOccluding() return relative.getType().isOccluding()
|| chest.getWorld().getNearbyEntities(BoundingBox.of(relative), entity -> entity instanceof Cat).size() > 0; || chest.getWorld().getNearbyEntities(BoundingBox.of(relative), entity -> entity instanceof Cat).size() > 0;
} }
/** /**
* Checks if the given block is a container which can be unblocked or silenced. * Check if the given {@link Block} is a container which can be unblocked or silenced.
* *
* @param block the BlockState * @param block the potential container
* @return true if the Block is a supported container * @return true if the type is a supported container
*/ */
default boolean isAnySilentContainer(@NotNull Block block) { default boolean isAnySilentContainer(@NotNull Block block) {
if (block.getType() == Material.ENDER_CHEST) { if (block.getType() == Material.ENDER_CHEST) {

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2011-2021 lishid. All rights reserved. * Copyright (C) 2011-2022 lishid. All rights reserved.
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@@ -16,49 +16,47 @@
package com.lishid.openinv.internal; package com.lishid.openinv.internal;
import com.lishid.openinv.util.InventoryAccess;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@Deprecated /**
* @deprecated Use static {@link InventoryAccess} methods.
*/
@Deprecated(forRemoval = true)
public interface IInventoryAccess { public interface IInventoryAccess {
/** /**
* Gets an ISpecialEnderChest from an Inventory or null if the Inventory is not backed by an * @deprecated Use static {@link InventoryAccess} methods.
* ISpecialEnderChest.
*
* @param inventory the Inventory
* @return the ISpecialEnderChest or null
*/ */
@Deprecated @Deprecated(forRemoval = true)
@Nullable ISpecialEnderChest getSpecialEnderChest(@NotNull Inventory inventory); default @Nullable ISpecialEnderChest getSpecialEnderChest(@NotNull Inventory inventory) {
return InventoryAccess.getEnderChest(inventory);
}
/** /**
* Gets an ISpecialPlayerInventory from an Inventory or null if the Inventory is not backed by * @deprecated Use static {@link InventoryAccess} methods.
* an ISpecialPlayerInventory.
*
* @param inventory the Inventory
* @return the ISpecialPlayerInventory or null
*/ */
@Deprecated @Deprecated(forRemoval = true)
@Nullable ISpecialPlayerInventory getSpecialPlayerInventory(@NotNull Inventory inventory); default @Nullable ISpecialPlayerInventory getSpecialPlayerInventory(@NotNull Inventory inventory) {
return InventoryAccess.getPlayerInventory(inventory);
}
/** /**
* Check if an Inventory is an ISpecialEnderChest implementation. * @deprecated Use static {@link InventoryAccess} methods.
*
* @param inventory the Inventory
* @return true if the Inventory is backed by an ISpecialEnderChest
*/ */
@Deprecated @Deprecated(forRemoval = true)
boolean isSpecialEnderChest(@NotNull Inventory inventory); default boolean isSpecialEnderChest(@NotNull Inventory inventory) {
return InventoryAccess.isEnderChest(inventory);
}
/** /**
* Check if an Inventory is an ISpecialPlayerInventory implementation. * @deprecated Use static {@link InventoryAccess} methods.
*
* @param inventory the Inventory
* @return true if the Inventory is backed by an ISpecialPlayerInventory
*/ */
@Deprecated @Deprecated(forRemoval = true)
boolean isSpecialPlayerInventory(@NotNull Inventory inventory); default boolean isSpecialPlayerInventory(@NotNull Inventory inventory) {
return InventoryAccess.isPlayerInventory(inventory);
}
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2011-2021 lishid. All rights reserved. * Copyright (C) 2011-2022 lishid. All rights reserved.
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@@ -16,6 +16,9 @@
package com.lishid.openinv.internal; package com.lishid.openinv.internal;
/**
* An {@link ISpecialInventory} representing an ender chest.
*/
public interface ISpecialEnderChest extends ISpecialInventory { public interface ISpecialEnderChest extends ISpecialInventory {
} }

View File

@@ -21,40 +21,43 @@ import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/**
* Interface defining behavior for special inventories backed by other inventories' content listings.
*/
public interface ISpecialInventory { public interface ISpecialInventory {
/** /**
* Gets the Inventory associated with this ISpecialInventory. * Get the {@link Inventory} associated with this {@code ISpecialInventory}.
* *
* @return the Inventory * @return the Bukkit inventory
*/ */
@NotNull Inventory getBukkitInventory(); @NotNull Inventory getBukkitInventory();
/** /**
* Sets the Player associated with this ISpecialInventory online. * Set the owning {@link Player} instance to a newly-joined user.
* *
* @param player the Player coming online * @param player the user coming online
*/ */
void setPlayerOnline(@NotNull Player player); void setPlayerOnline(@NotNull Player player);
/** /**
* Sets the Player associated with this ISpecialInventory offline. * Mark the owner of the inventory offline.
*/ */
void setPlayerOffline(); void setPlayerOffline();
/** /**
* Gets whether this ISpecialInventory is in use. * Get whether the inventory is being viewed by any users.
* *
* @return true if the ISpecialInventory is in use * @return true if the inventory is being viewed
*/ */
default boolean isInUse() { default boolean isInUse() {
return !getBukkitInventory().getViewers().isEmpty(); return !getBukkitInventory().getViewers().isEmpty();
} }
/** /**
* Gets the Player associated with this ISpecialInventory. * Get the {@link Player} who owns the inventory.
* *
* @return the HumanEntity * @return the {@link HumanEntity} who owns the inventory
*/ */
@NotNull HumanEntity getPlayer(); @NotNull HumanEntity getPlayer();

View File

@@ -21,7 +21,7 @@ import org.bukkit.entity.HumanEntity;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
/** /**
* Interface defining behavior specific to a player inventory. * An {@link ISpecialInventory} representing a player inventory.
*/ */
public interface ISpecialPlayerInventory extends ISpecialInventory { public interface ISpecialPlayerInventory extends ISpecialInventory {

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2011-2021 lishid. All rights reserved. * Copyright (C) 2011-2022 lishid. All rights reserved.
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@@ -26,7 +26,7 @@ import org.bukkit.inventory.Inventory;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
public class InventoryAccess implements IInventoryAccess { public final class InventoryAccess implements IInventoryAccess {
private static Class<?> craftInventory = null; private static Class<?> craftInventory = null;
private static Method getInventory = null; private static Method getInventory = null;
@@ -35,16 +35,14 @@ public class InventoryAccess implements IInventoryAccess {
String packageName = Bukkit.getServer().getClass().getPackage().getName(); String packageName = Bukkit.getServer().getClass().getPackage().getName();
try { try {
craftInventory = Class.forName(packageName + ".inventory.CraftInventory"); craftInventory = Class.forName(packageName + ".inventory.CraftInventory");
} catch (ClassNotFoundException ignored) {}
try {
getInventory = craftInventory.getDeclaredMethod("getInventory"); getInventory = craftInventory.getDeclaredMethod("getInventory");
} catch (NoSuchMethodException ignored) {} } catch (ClassNotFoundException | NoSuchMethodException ignored) {}
} }
/** /**
* @deprecated use {@link #isUsable()} * @deprecated use {@link #isUsable()}
*/ */
@Deprecated @Deprecated(forRemoval = true)
public static boolean isUseable() { public static boolean isUseable() {
return isUsable(); return isUsable();
} }
@@ -53,25 +51,51 @@ public class InventoryAccess implements IInventoryAccess {
return craftInventory != null && getInventory != null; return craftInventory != null && getInventory != null;
} }
/**
* Check if an {@link Inventory} is an {@link ISpecialPlayerInventory} implementation.
*
* @param inventory the Bukkit inventory
* @return true if backed by the correct implementation
*/
public static boolean isPlayerInventory(@NotNull Inventory inventory) { public static boolean isPlayerInventory(@NotNull Inventory inventory) {
return getPlayerInventory(inventory) != null; return getPlayerInventory(inventory) != null;
} }
/**
* Get the {@link ISpecialPlayerInventory} backing an {@link Inventory}. Returns {@code null} if the inventory is
* not backed by the correct class.
*
* @param inventory the Bukkit inventory
* @return the backing implementation if available
*/
public static @Nullable ISpecialPlayerInventory getPlayerInventory(@NotNull Inventory inventory) { public static @Nullable ISpecialPlayerInventory getPlayerInventory(@NotNull Inventory inventory) {
return getSpecialInventory(ISpecialPlayerInventory.class, inventory); return getSpecialInventory(ISpecialPlayerInventory.class, inventory);
} }
/**
* Check if an {@link Inventory} is an {@link ISpecialEnderChest} implementation.
*
* @param inventory the Bukkit inventory
* @return true if backed by the correct implementation
*/
public static boolean isEnderChest(@NotNull Inventory inventory) { public static boolean isEnderChest(@NotNull Inventory inventory) {
return getEnderChest(inventory) != null; return getEnderChest(inventory) != null;
} }
/**
* Get the {@link ISpecialEnderChest} backing an {@link Inventory}. Returns {@code null} if the inventory is
* not backed by the correct class.
*
* @param inventory the Bukkit inventory
* @return the backing implementation if available
*/
public static @Nullable ISpecialEnderChest getEnderChest(@NotNull Inventory inventory) { public static @Nullable ISpecialEnderChest getEnderChest(@NotNull Inventory inventory) {
return getSpecialInventory(ISpecialEnderChest.class, inventory); return getSpecialInventory(ISpecialEnderChest.class, inventory);
} }
private static <T extends ISpecialInventory> @Nullable T getSpecialInventory(@NotNull Class<T> expected, @NotNull Inventory inventory) { private static <T extends ISpecialInventory> @Nullable T getSpecialInventory(@NotNull Class<T> expected, @NotNull Inventory inventory) {
Object inv; Object inv;
if (craftInventory != null && getInventory != null && craftInventory.isAssignableFrom(inventory.getClass())) { if (isUsable() && craftInventory.isAssignableFrom(inventory.getClass())) {
try { try {
inv = getInventory.invoke(inventory); inv = getInventory.invoke(inventory);
if (expected.isInstance(inv)) { if (expected.isInstance(inv)) {
@@ -90,28 +114,10 @@ public class InventoryAccess implements IInventoryAccess {
return null; return null;
} }
@Deprecated /**
@Override * @deprecated Do not create a new instance to use static methods.
public @Nullable ISpecialEnderChest getSpecialEnderChest(@NotNull Inventory inventory) { */
return getEnderChest(inventory); @Deprecated(forRemoval = true, since = "4.2.0")
} public InventoryAccess() {}
@Deprecated
@Override
public @Nullable ISpecialPlayerInventory getSpecialPlayerInventory(@NotNull Inventory inventory) {
return getPlayerInventory(inventory);
}
@Deprecated
@Override
public boolean isSpecialEnderChest(@NotNull Inventory inventory) {
return isEnderChest(inventory);
}
@Deprecated
@Override
public boolean isSpecialPlayerInventory(@NotNull Inventory inventory) {
return isPlayerInventory(inventory);
}
} }

View File

@@ -86,8 +86,8 @@ public class AnySilentContainer implements IAnySilentContainer {
} }
@Override @Override
public boolean isShulkerBlocked(ShulkerBox box) { public boolean isShulkerBlocked(@NotNull ShulkerBox shulkerBox) {
org.bukkit.World bukkitWorld = box.getWorld(); org.bukkit.World bukkitWorld = shulkerBox.getWorld();
if (!(bukkitWorld instanceof CraftWorld)) { if (!(bukkitWorld instanceof CraftWorld)) {
bukkitWorld = Bukkit.getWorld(bukkitWorld.getUID()); bukkitWorld = Bukkit.getWorld(bukkitWorld.getUID());
} }
@@ -99,7 +99,7 @@ public class AnySilentContainer implements IAnySilentContainer {
} }
final ServerLevel world = craftWorld.getHandle(); final ServerLevel world = craftWorld.getHandle();
final BlockPos blockPosition = new BlockPos(box.getX(), box.getY(), box.getZ()); final BlockPos blockPosition = new BlockPos(shulkerBox.getX(), shulkerBox.getY(), shulkerBox.getZ());
final BlockEntity tile = world.getBlockEntity(blockPosition); final BlockEntity tile = world.getBlockEntity(blockPosition);
if (!(tile instanceof ShulkerBoxBlockEntity shulkerBoxBlockEntity) if (!(tile instanceof ShulkerBoxBlockEntity shulkerBoxBlockEntity)

View File

@@ -86,8 +86,8 @@ public class AnySilentContainer implements IAnySilentContainer {
} }
@Override @Override
public boolean isShulkerBlocked(ShulkerBox box) { public boolean isShulkerBlocked(@NotNull ShulkerBox shulkerBox) {
org.bukkit.World bukkitWorld = box.getWorld(); org.bukkit.World bukkitWorld = shulkerBox.getWorld();
if (!(bukkitWorld instanceof CraftWorld)) { if (!(bukkitWorld instanceof CraftWorld)) {
bukkitWorld = Bukkit.getWorld(bukkitWorld.getUID()); bukkitWorld = Bukkit.getWorld(bukkitWorld.getUID());
} }
@@ -99,7 +99,7 @@ public class AnySilentContainer implements IAnySilentContainer {
} }
final ServerLevel world = craftWorld.getHandle(); final ServerLevel world = craftWorld.getHandle();
final BlockPos blockPosition = new BlockPos(box.getX(), box.getY(), box.getZ()); final BlockPos blockPosition = new BlockPos(shulkerBox.getX(), shulkerBox.getY(), shulkerBox.getZ());
final BlockEntity tile = world.getBlockEntity(blockPosition); final BlockEntity tile = world.getBlockEntity(blockPosition);
if (!(tile instanceof ShulkerBoxBlockEntity shulkerBoxBlockEntity) if (!(tile instanceof ShulkerBoxBlockEntity shulkerBoxBlockEntity)

View File

@@ -86,8 +86,8 @@ public class AnySilentContainer implements IAnySilentContainer {
} }
@Override @Override
public boolean isShulkerBlocked(ShulkerBox box) { public boolean isShulkerBlocked(@NotNull ShulkerBox shulkerBox) {
org.bukkit.World bukkitWorld = box.getWorld(); org.bukkit.World bukkitWorld = shulkerBox.getWorld();
if (!(bukkitWorld instanceof CraftWorld)) { if (!(bukkitWorld instanceof CraftWorld)) {
bukkitWorld = Bukkit.getWorld(bukkitWorld.getUID()); bukkitWorld = Bukkit.getWorld(bukkitWorld.getUID());
} }
@@ -99,7 +99,7 @@ public class AnySilentContainer implements IAnySilentContainer {
} }
final ServerLevel world = craftWorld.getHandle(); final ServerLevel world = craftWorld.getHandle();
final BlockPos blockPosition = new BlockPos(box.getX(), box.getY(), box.getZ()); final BlockPos blockPosition = new BlockPos(shulkerBox.getX(), shulkerBox.getY(), shulkerBox.getZ());
final BlockEntity tile = world.getBlockEntity(blockPosition); final BlockEntity tile = world.getBlockEntity(blockPosition);
if (!(tile instanceof ShulkerBoxBlockEntity shulkerBoxBlockEntity) if (!(tile instanceof ShulkerBoxBlockEntity shulkerBoxBlockEntity)

View File

@@ -53,7 +53,7 @@ record InventoryListener(OpenInv plugin) implements Listener {
return; return;
} }
if (this.plugin.getPlayerSilentChestStatus(player)) { if (this.plugin.getSilentContainerStatus(player)) {
this.plugin.getAnySilentContainer().deactivateContainer(player); this.plugin.getAnySilentContainer().deactivateContainer(player);
} }

View File

@@ -133,21 +133,21 @@ public class OpenInv extends JavaPlugin implements IOpenInv {
} }
@Override @Override
public boolean getPlayerAnyChestStatus(@NotNull final OfflinePlayer player) { public boolean getAnyContainerStatus(@NotNull final OfflinePlayer offline) {
boolean defaultState = false; boolean defaultState = false;
if (player.isOnline()) { if (offline.isOnline()) {
Player onlinePlayer = player.getPlayer(); Player onlinePlayer = offline.getPlayer();
if (onlinePlayer != null) { if (onlinePlayer != null) {
defaultState = Permissions.ANY_DEFAULT.hasPermission(onlinePlayer); defaultState = Permissions.ANY_DEFAULT.hasPermission(onlinePlayer);
} }
} }
return this.getConfig().getBoolean("toggles.any-chest." + this.getPlayerID(player), defaultState); return this.getConfig().getBoolean("toggles.any-chest." + offline.getUniqueId(), defaultState);
} }
@Override @Override
public boolean getPlayerSilentChestStatus(@NotNull final OfflinePlayer offline) { public boolean getSilentContainerStatus(@NotNull final OfflinePlayer offline) {
boolean defaultState = false; boolean defaultState = false;
if (offline.isOnline()) { if (offline.isOnline()) {
@@ -157,7 +157,7 @@ public class OpenInv extends JavaPlugin implements IOpenInv {
} }
} }
return this.getConfig().getBoolean("toggles.silent-chest." + this.getPlayerID(offline), defaultState); return this.getConfig().getBoolean("toggles.silent-chest." + offline.getUniqueId(), defaultState);
} }
@Override @Override
@@ -401,8 +401,8 @@ public class OpenInv extends JavaPlugin implements IOpenInv {
} }
@Override @Override
public void setPlayerAnyChestStatus(@NotNull final OfflinePlayer offline, final boolean status) { public void setAnyContainerStatus(@NotNull final OfflinePlayer offline, final boolean status) {
this.getConfig().set("toggles.any-chest." + this.getPlayerID(offline), status); this.getConfig().set("toggles.any-chest." + offline.getUniqueId(), status);
this.saveConfig(); this.saveConfig();
} }
@@ -515,8 +515,8 @@ public class OpenInv extends JavaPlugin implements IOpenInv {
} }
@Override @Override
public void setPlayerSilentChestStatus(@NotNull final OfflinePlayer offline, final boolean status) { public void setSilentContainerStatus(@NotNull final OfflinePlayer offline, final boolean status) {
this.getConfig().set("toggles.silent-chest." + this.getPlayerID(offline), status); this.getConfig().set("toggles.silent-chest." + offline.getUniqueId(), status);
this.saveConfig(); this.saveConfig();
} }

View File

@@ -61,14 +61,14 @@ record PlayerListener(OpenInv plugin) implements Listener {
} }
Player player = event.getPlayer(); Player player = event.getPlayer();
boolean any = Permissions.ANYCHEST.hasPermission(player) && plugin.getPlayerAnyChestStatus(player); boolean any = Permissions.ANYCHEST.hasPermission(player) && plugin.getAnyContainerStatus(player);
boolean needsAny = plugin.getAnySilentContainer().isAnyContainerNeeded(event.getClickedBlock()); boolean needsAny = plugin.getAnySilentContainer().isAnyContainerNeeded(event.getClickedBlock());
if (!any && needsAny) { if (!any && needsAny) {
return; return;
} }
boolean silent = Permissions.SILENT.hasPermission(player) && plugin.getPlayerSilentChestStatus(player); boolean silent = Permissions.SILENT.hasPermission(player) && plugin.getSilentContainerStatus(player);
// If anycontainer or silentcontainer is active // If anycontainer or silentcontainer is active
if (any || silent) { if (any || silent) {

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2011-2021 lishid. All rights reserved. * Copyright (C) 2011-2022 lishid. All rights reserved.
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@@ -39,15 +39,14 @@ public class ContainerSettingCommand implements TabExecutor {
@Override @Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (!(sender instanceof Player)) { if (!(sender instanceof Player player)) {
plugin.sendMessage(sender, "messages.error.consoleUnsupported"); plugin.sendMessage(sender, "messages.error.consoleUnsupported");
return true; return true;
} }
Player player = (Player) sender;
boolean any = command.getName().startsWith("any"); boolean any = command.getName().startsWith("any");
Predicate<Player> getSetting = any ? plugin::getPlayerAnyChestStatus : plugin::getPlayerSilentChestStatus; Predicate<Player> getSetting = any ? plugin::getAnyContainerStatus : plugin::getSilentContainerStatus;
BiConsumer<OfflinePlayer, Boolean> setSetting = any ? plugin::setPlayerAnyChestStatus : plugin::setPlayerSilentChestStatus; BiConsumer<OfflinePlayer, Boolean> setSetting = any ? plugin::setAnyContainerStatus : plugin::setSilentContainerStatus;
if (args.length > 0) { if (args.length > 0) {
args[0] = args[0].toLowerCase(); args[0] = args[0].toLowerCase();

View File

@@ -186,7 +186,7 @@ public class OpenInvCommand implements TabExecutor {
} }
// Record the target // Record the target
(openinv ? this.openInvHistory : this.openEnderHistory).put(player, this.plugin.getPlayerID(target)); (openinv ? this.openInvHistory : this.openEnderHistory).put(player, target.getUniqueId().toString());
// Create the inventory // Create the inventory
final ISpecialInventory inv; final ISpecialInventory inv;

View File

@@ -128,7 +128,7 @@ public record ConfigUpdater(OpenInv plugin) {
for (String playerName : keys) { for (String playerName : keys) {
OfflinePlayer player = plugin.matchPlayer(playerName); OfflinePlayer player = plugin.matchPlayer(playerName);
if (player != null) { if (player != null) {
toggles.put(plugin.getPlayerID(player), section.getBoolean(playerName + ".toggle", false)); toggles.put(player.getUniqueId().toString(), section.getBoolean(playerName + ".toggle", false));
} }
} }