[Idea]: Folia support for OpenInv #196

Closed
reabuc wants to merge 137 commits from master into master
66 changed files with 6046 additions and 1971 deletions
Showing only changes of commit 525982ce64 - Show all commits

View File

@@ -14,28 +14,28 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package com.lishid.openinv.util; package com.lishid.openinv;
import com.lishid.openinv.internal.IAnySilentContainer; import com.lishid.openinv.internal.IAnySilentContainer;
import com.lishid.openinv.internal.IPlayerDataManager; import com.lishid.openinv.internal.IPlayerDataManager;
import com.lishid.openinv.internal.ISpecialEnderChest; import com.lishid.openinv.internal.ISpecialEnderChest;
import com.lishid.openinv.internal.ISpecialInventory; import com.lishid.openinv.internal.ISpecialInventory;
import com.lishid.openinv.internal.ISpecialPlayerInventory; import com.lishid.openinv.internal.ISpecialPlayerInventory;
import com.lishid.openinv.util.InventoryAccess;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
public class InternalAccessor { class InternalAccessor {
private final Plugin plugin; private final @NotNull Plugin plugin;
private final String version; private final String version;
private boolean supported = false; private boolean supported = false;
private IPlayerDataManager playerDataManager; private IPlayerDataManager playerDataManager;
private IAnySilentContainer anySilentContainer; private IAnySilentContainer anySilentContainer;
public InternalAccessor(final Plugin plugin) { InternalAccessor(@NotNull Plugin plugin) {
this.plugin = plugin; this.plugin = plugin;
String packageName = plugin.getServer().getClass().getPackage().getName(); String packageName = plugin.getServer().getClass().getPackage().getName();
@@ -51,58 +51,29 @@ public class InternalAccessor {
} }
public String getReleasesLink() { public String getReleasesLink() {
switch (version) {
case "1_4_5": return switch (version) {
case "1_4_6": case "1_4_5", "1_4_6", "v1_4_R1", "v1_5_R2", "v1_5_R3", "v1_6_R1", "v1_6_R2", "v1_6_R3",
case "v1_4_R1": "v1_7_R1", "v1_7_R2", "v1_7_R3", "v1_7_R4", "v1_8_R1", "v1_8_R2",
case "v1_5_R2": "v1_9_R1", "v1_9_R2", "v1_10_R1", "v1_11_R1", "v1_12_R1"
case "v1_5_R3": -> "https://github.com/lishid/OpenInv/releases/tag/4.0.0 (OpenInv-legacy)";
case "v1_6_R1": case "v1_13_R1" -> "https://github.com/lishid/OpenInv/releases/tag/4.0.0";
case "v1_6_R2": case "v1_13_R2" -> "https://github.com/lishid/OpenInv/releases/tag/4.0.7";
case "v1_6_R3": case "v1_14_R1" -> "https://github.com/lishid/OpenInv/releases/tag/4.1.1";
case "v1_7_R1": case "v1_16_R1" -> "https://github.com/lishid/OpenInv/releases/tag/4.1.4";
case "v1_7_R2": case "v1_8_R3", "v1_15_R1", "v1_16_R2" -> "https://github.com/lishid/OpenInv/releases/tag/4.1.5";
case "v1_7_R3": case "v1_16_R3" -> "https://github.com/Jikoo/OpenInv/releases/tag/4.1.8";
case "v1_7_R4": default -> "https://github.com/Jikoo/OpenInv/releases";
case "v1_8_R1": };
case "v1_8_R2":
case "v1_9_R1":
case "v1_9_R2":
case "v1_10_R1":
case "v1_11_R1":
case "v1_12_R1":
return "https://github.com/lishid/OpenInv/releases/tag/4.0.0 (OpenInv-legacy)";
case "v1_13_R1":
return "https://github.com/lishid/OpenInv/releases/tag/4.0.0";
case "v1_13_R2":
return "https://github.com/lishid/OpenInv/releases/tag/4.0.7";
case "v1_14_R1":
return "https://github.com/lishid/OpenInv/releases/tag/4.1.1";
case "v1_16_R1":
return "https://github.com/lishid/OpenInv/releases/tag/4.1.4";
case "v1_8_R3":
case "v1_15_R1":
case "v1_16_R2":
return "https://github.com/lishid/OpenInv/releases/tag/4.1.5";
case "v1_16_R3":
return "https://github.com/Jikoo/OpenInv/releases/tag/4.1.8";
case "v1_17_R1":
default:
return "https://github.com/Jikoo/OpenInv/releases";
}
} }
private <T> T createObject(final Class<? extends T> assignableClass, final String className, private @NotNull <T> T createObject(
final Object... params) throws ClassCastException, ClassNotFoundException, @NotNull Class<? extends T> assignableClass,
InstantiationException, IllegalAccessException, IllegalArgumentException, @NotNull String className,
InvocationTargetException, NoSuchMethodException, SecurityException { @NotNull Object @NotNull ... params)
throws ClassCastException, ReflectiveOperationException {
// Fetch internal class if it exists. // Fetch internal class if it exists.
Class<?> internalClass = Class.forName("com.lishid.openinv.internal." + this.version + "." + className); Class<?> internalClass = Class.forName("com.lishid.openinv.internal." + this.version + "." + className);
if (!assignableClass.isAssignableFrom(internalClass)) {
String message = String.format("Found class %s but cannot cast to %s!", internalClass.getName(), assignableClass.getName());
this.plugin.getLogger().warning(message);
throw new IllegalStateException(message);
}
// Quick return: no parameters, no need to fiddle about finding the correct constructor. // Quick return: no parameters, no need to fiddle about finding the correct constructor.
if (params.length == 0) { if (params.length == 0) {
@@ -133,10 +104,10 @@ public class InternalAccessor {
String message = builder.append(']').toString(); String message = builder.append(']').toString();
this.plugin.getLogger().warning(message); this.plugin.getLogger().warning(message);
throw new IllegalArgumentException(message); throw new NoSuchMethodException(message);
} }
private <T extends ISpecialInventory> T createSpecialInventory( private @NotNull <T extends ISpecialInventory> T createSpecialInventory(
@NotNull Class<? extends T> assignableClass, @NotNull Class<? extends T> assignableClass,
@NotNull String className, @NotNull String className,
@NotNull Player player, @NotNull Player player,
@@ -159,7 +130,7 @@ public class InternalAccessor {
* @return the IAnySilentContainer * @return the IAnySilentContainer
* @throws IllegalStateException if server version is unsupported * @throws IllegalStateException if server version is unsupported
*/ */
public IAnySilentContainer getAnySilentContainer() { public @NotNull IAnySilentContainer getAnySilentContainer() {
if (!this.supported) { if (!this.supported) {
throw new IllegalStateException(String.format("Unsupported server version %s!", this.version)); throw new IllegalStateException(String.format("Unsupported server version %s!", this.version));
} }
@@ -172,7 +143,7 @@ public class InternalAccessor {
* @return the IPlayerDataManager * @return the IPlayerDataManager
* @throws IllegalStateException if server version is unsupported * @throws IllegalStateException if server version is unsupported
*/ */
public IPlayerDataManager getPlayerDataManager() { public @NotNull IPlayerDataManager getPlayerDataManager() {
if (!this.supported) { if (!this.supported) {
throw new IllegalStateException(String.format("Unsupported server version %s!", this.version)); throw new IllegalStateException(String.format("Unsupported server version %s!", this.version));
} }
@@ -180,13 +151,12 @@ public class InternalAccessor {
} }
/** /**
* Gets the server implementation version. If not initialized, returns the string "null" * Gets the server implementation version.
* instead.
* *
* @return the version, or "null" * @return the version
*/ */
public String getVersion() { public @NotNull String getVersion() {
return this.version != null ? this.version : "null"; return this.version;
} }
/** /**

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
@@ -14,9 +14,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package com.lishid.openinv.listeners; package com.lishid.openinv;
import com.lishid.openinv.OpenInv;
import com.lishid.openinv.internal.ISpecialPlayerInventory; import com.lishid.openinv.internal.ISpecialPlayerInventory;
import com.lishid.openinv.util.InventoryAccess; import com.lishid.openinv.util.InventoryAccess;
import com.lishid.openinv.util.Permissions; import com.lishid.openinv.util.Permissions;
@@ -45,22 +44,14 @@ import org.jetbrains.annotations.Nullable;
* *
* @author Jikoo * @author Jikoo
*/ */
public class InventoryListener implements Listener { record InventoryListener(OpenInv plugin) implements Listener {
private final OpenInv plugin;
public InventoryListener(final OpenInv plugin) {
this.plugin = plugin;
}
@EventHandler @EventHandler
public void onInventoryClose(@NotNull final InventoryCloseEvent event) { public void onInventoryClose(@NotNull final InventoryCloseEvent event) {
if (!(event.getPlayer() instanceof Player)) { if (!(event.getPlayer() instanceof Player player)) {
return; return;
} }
Player player = (Player) event.getPlayer();
if (this.plugin.getPlayerSilentChestStatus(player)) { if (this.plugin.getPlayerSilentChestStatus(player)) {
this.plugin.getAnySilentContainer().deactivateContainer(player); this.plugin.getAnySilentContainer().deactivateContainer(player);
} }

View File

@@ -14,9 +14,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package com.lishid.openinv.listeners; package com.lishid.openinv;
import com.lishid.openinv.OpenInv;
import com.lishid.openinv.util.Permissions; import com.lishid.openinv.util.Permissions;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.Event.Result; import org.bukkit.event.Event.Result;
@@ -28,32 +27,27 @@ import org.bukkit.event.player.PlayerChangedWorldEvent;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import org.jetbrains.annotations.NotNull;
public class PlayerListener implements Listener { record PlayerListener(OpenInv plugin) implements Listener {
private final OpenInv plugin;
public PlayerListener(OpenInv plugin) {
this.plugin = plugin;
}
@EventHandler(priority = EventPriority.LOWEST) @EventHandler(priority = EventPriority.LOWEST)
public void onPlayerJoin(final PlayerJoinEvent event) { public void onPlayerJoin(@NotNull PlayerJoinEvent event) {
plugin.setPlayerOnline(event.getPlayer()); plugin.setPlayerOnline(event.getPlayer());
} }
@EventHandler(priority = EventPriority.MONITOR) @EventHandler(priority = EventPriority.MONITOR)
public void onPlayerQuit(PlayerQuitEvent event) { public void onPlayerQuit(@NotNull PlayerQuitEvent event) {
plugin.setPlayerOffline(event.getPlayer()); plugin.setPlayerOffline(event.getPlayer());
} }
@EventHandler @EventHandler
public void onWorldChange(PlayerChangedWorldEvent event) { public void onWorldChange(@NotNull PlayerChangedWorldEvent event) {
plugin.changeWorld(event.getPlayer()); plugin.changeWorld(event.getPlayer());
} }
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerInteract(PlayerInteractEvent event) { public void onPlayerInteract(@NotNull PlayerInteractEvent event) {
// Do not cancel 3rd party plugins' custom events // Do not cancel 3rd party plugins' custom events
if (!PlayerInteractEvent.class.equals(event.getClass())) { if (!PlayerInteractEvent.class.equals(event.getClass())) {