2016-11-25 13:01:44 +00:00
|
|
|
/*
|
2020-02-01 13:03:15 +00:00
|
|
|
* Copyright (C) 2011-2020 lishid. All rights reserved.
|
2017-06-08 22:36:01 +00:00
|
|
|
*
|
2016-11-25 13:01:44 +00:00
|
|
|
* 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
|
2018-02-04 15:29:07 +00:00
|
|
|
* the Free Software Foundation, version 3.
|
2017-06-08 22:36:01 +00:00
|
|
|
*
|
2016-11-25 13:01:44 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2018-02-04 15:29:07 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2016-11-25 13:01:44 +00:00
|
|
|
* GNU General Public License for more details.
|
2017-06-08 22:36:01 +00:00
|
|
|
*
|
2016-11-25 13:01:44 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2016-12-01 02:26:56 +00:00
|
|
|
package com.lishid.openinv.util;
|
2016-11-25 13:01:44 +00:00
|
|
|
|
2016-12-01 02:26:56 +00:00
|
|
|
import com.lishid.openinv.internal.IAnySilentContainer;
|
|
|
|
import com.lishid.openinv.internal.IPlayerDataManager;
|
|
|
|
import com.lishid.openinv.internal.ISpecialEnderChest;
|
|
|
|
import com.lishid.openinv.internal.ISpecialPlayerInventory;
|
2019-04-29 00:37:05 +00:00
|
|
|
import java.lang.reflect.Constructor;
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
2016-11-25 13:01:44 +00:00
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.plugin.Plugin;
|
|
|
|
|
|
|
|
public class InternalAccessor {
|
|
|
|
|
2017-06-08 22:36:01 +00:00
|
|
|
private final Plugin plugin;
|
2016-11-25 21:42:06 +00:00
|
|
|
private final String version;
|
2016-11-25 13:01:44 +00:00
|
|
|
private boolean supported = false;
|
2017-06-08 22:36:01 +00:00
|
|
|
private IPlayerDataManager playerDataManager;
|
|
|
|
private IAnySilentContainer anySilentContainer;
|
|
|
|
|
|
|
|
public InternalAccessor(final Plugin plugin) {
|
2016-11-25 13:01:44 +00:00
|
|
|
this.plugin = plugin;
|
|
|
|
|
2016-11-25 21:42:06 +00:00
|
|
|
String packageName = plugin.getServer().getClass().getPackage().getName();
|
2017-06-08 22:36:01 +00:00
|
|
|
this.version = packageName.substring(packageName.lastIndexOf('.') + 1);
|
2016-11-25 13:01:44 +00:00
|
|
|
|
|
|
|
try {
|
2017-06-08 22:36:01 +00:00
|
|
|
Class.forName("com.lishid.openinv.internal." + this.version + ".SpecialPlayerInventory");
|
|
|
|
Class.forName("com.lishid.openinv.internal." + this.version + ".SpecialEnderChest");
|
|
|
|
this.playerDataManager = this.createObject(IPlayerDataManager.class, "PlayerDataManager");
|
|
|
|
this.anySilentContainer = this.createObject(IAnySilentContainer.class, "AnySilentContainer");
|
2020-11-15 18:26:59 +00:00
|
|
|
this.supported = InventoryAccess.isUsable();
|
2019-04-29 00:37:05 +00:00
|
|
|
} catch (Exception ignored) {}
|
2016-11-25 13:01:44 +00:00
|
|
|
}
|
|
|
|
|
2020-11-15 17:22:25 +00:00
|
|
|
public String getReleasesLink() {
|
|
|
|
switch (version) {
|
|
|
|
case "1_4_5":
|
|
|
|
case "1_4_6":
|
|
|
|
case "v1_4_R1":
|
|
|
|
case "v1_5_R2":
|
|
|
|
case "v1_5_R3":
|
|
|
|
case "v1_6_R1":
|
|
|
|
case "v1_6_R2":
|
|
|
|
case "v1_6_R3":
|
|
|
|
case "v1_7_R1":
|
|
|
|
case "v1_7_R2":
|
|
|
|
case "v1_7_R3":
|
|
|
|
case "v1_7_R4":
|
|
|
|
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":
|
|
|
|
case "v1_16_R3":
|
|
|
|
default:
|
|
|
|
return "https://github.com/lishid/OpenInv/releases";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-08 22:36:01 +00:00
|
|
|
private <T> T createObject(final Class<? extends T> assignableClass, final String className,
|
|
|
|
final Object... params) throws ClassCastException, ClassNotFoundException,
|
|
|
|
InstantiationException, IllegalAccessException, IllegalArgumentException,
|
|
|
|
InvocationTargetException, NoSuchMethodException, SecurityException {
|
|
|
|
// Fetch internal class if it exists.
|
|
|
|
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.
|
|
|
|
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(", ");
|
|
|
|
}
|
2018-02-04 15:29:07 +00:00
|
|
|
builder.delete(builder.length() - 2, builder.length());
|
2017-06-08 22:36:01 +00:00
|
|
|
|
|
|
|
String message = builder.append(']').toString();
|
|
|
|
this.plugin.getLogger().warning(message);
|
|
|
|
|
|
|
|
throw new IllegalArgumentException(message);
|
2016-11-25 13:01:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-08 22:36:01 +00:00
|
|
|
* Creates an instance of the IAnySilentContainer implementation for the current server version.
|
|
|
|
*
|
|
|
|
* @return the IAnySilentContainer
|
|
|
|
* @throws IllegalStateException if server version is unsupported
|
2016-11-25 13:01:44 +00:00
|
|
|
*/
|
2017-06-08 22:36:01 +00:00
|
|
|
public IAnySilentContainer getAnySilentContainer() {
|
|
|
|
if (!this.supported) {
|
|
|
|
throw new IllegalStateException(String.format("Unsupported server version %s!", this.version));
|
|
|
|
}
|
|
|
|
return this.anySilentContainer;
|
2016-11-25 13:01:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-08 22:36:01 +00:00
|
|
|
* Creates an instance of the IPlayerDataManager implementation for the current server version.
|
|
|
|
*
|
|
|
|
* @return the IPlayerDataManager
|
|
|
|
* @throws IllegalStateException if server version is unsupported
|
2016-11-25 13:01:44 +00:00
|
|
|
*/
|
2017-06-08 22:36:01 +00:00
|
|
|
public IPlayerDataManager getPlayerDataManager() {
|
|
|
|
if (!this.supported) {
|
|
|
|
throw new IllegalStateException(String.format("Unsupported server version %s!", this.version));
|
|
|
|
}
|
|
|
|
return this.playerDataManager;
|
2016-11-25 13:01:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-08 22:36:01 +00:00
|
|
|
* Gets the server implementation version. If not initialized, returns the string "null"
|
|
|
|
* instead.
|
|
|
|
*
|
|
|
|
* @return the version, or "null"
|
2016-11-25 13:01:44 +00:00
|
|
|
*/
|
2017-06-08 22:36:01 +00:00
|
|
|
public String getVersion() {
|
|
|
|
return this.version != null ? this.version : "null";
|
2016-11-25 13:01:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-08 22:36:01 +00:00
|
|
|
* Checks if the server implementation is supported.
|
|
|
|
*
|
|
|
|
* @return true if initialized for a supported server version
|
2016-11-25 13:01:44 +00:00
|
|
|
*/
|
2017-06-08 22:36:01 +00:00
|
|
|
public boolean isSupported() {
|
|
|
|
return this.supported;
|
2016-11-25 13:01:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an instance of the ISpecialEnderChest implementation for the given Player, or
|
|
|
|
* null if the current version is unsupported.
|
2017-06-08 22:36:01 +00:00
|
|
|
*
|
2016-11-25 13:01:44 +00:00
|
|
|
* @param player the Player
|
|
|
|
* @param online true if the Player is online
|
|
|
|
* @return the ISpecialEnderChest created
|
2017-06-08 22:36:01 +00:00
|
|
|
* @throws InstantiationException if the ISpecialEnderChest could not be instantiated
|
2016-11-25 13:01:44 +00:00
|
|
|
*/
|
2017-06-08 22:36:01 +00:00
|
|
|
public ISpecialEnderChest newSpecialEnderChest(final Player player, final boolean online) throws InstantiationException {
|
|
|
|
if (!this.supported) {
|
|
|
|
throw new IllegalStateException(String.format("Unsupported server version %s!", this.version));
|
|
|
|
}
|
2016-11-25 13:01:44 +00:00
|
|
|
try {
|
2017-06-08 22:36:01 +00:00
|
|
|
return this.createObject(ISpecialEnderChest.class, "SpecialEnderChest", player, online);
|
2016-11-25 13:01:44 +00:00
|
|
|
} catch (Exception e) {
|
2017-06-08 22:36:01 +00:00
|
|
|
throw new InstantiationException(String.format("Unable to create a new ISpecialEnderChest: %s", e.getMessage()));
|
2016-11-25 13:01:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-08 22:36:01 +00:00
|
|
|
/**
|
|
|
|
* Creates an instance of the ISpecialPlayerInventory implementation for the given Player..
|
|
|
|
*
|
|
|
|
* @param player the Player
|
|
|
|
* @param online true if the Player is online
|
|
|
|
* @return the ISpecialPlayerInventory created
|
|
|
|
* @throws InstantiationException if the ISpecialPlayerInventory could not be instantiated
|
|
|
|
*/
|
|
|
|
public ISpecialPlayerInventory newSpecialPlayerInventory(final Player player, final boolean online) throws InstantiationException {
|
|
|
|
if (!this.supported) {
|
|
|
|
throw new IllegalStateException(String.format("Unsupported server version %s!", this.version));
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
return this.createObject(ISpecialPlayerInventory.class, "SpecialPlayerInventory", player, online);
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new InstantiationException(String.format("Unable to create a new ISpecialPlayerInventory: %s", e.getMessage()));
|
2016-11-25 21:42:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-25 13:01:44 +00:00
|
|
|
}
|