♻️ Improved the API

This commit is contained in:
SBDeveloper 2022-06-30 19:26:51 +02:00
parent 2c65469bfb
commit bfaa99e5dd
29 changed files with 444 additions and 341 deletions

View file

@ -61,15 +61,19 @@ public class MapReflectionAPI extends JavaPlugin {
return;
}
getLogger().info("Initializing the packet handler...");
packetListener = PacketListener.construct();
try {
packetListener = PacketListener.construct(this);
} catch (IllegalStateException e) {
getLogger().log(Level.SEVERE, e.getMessage(), e);
Bukkit.getPluginManager().disablePlugin(this);
return;
}
packetListener.init(this);
getLogger().info("Initializing the map manager...");
try {
mapManager = new MapManager();
mapManager = new MapManager(this);
} catch (IllegalStateException e) {
e.printStackTrace();
getLogger().log(Level.SEVERE, e.getMessage(), e);
Bukkit.getPluginManager().disablePlugin(this);
return;
}

View file

@ -26,6 +26,7 @@ package tech.sbdevelopment.mapreflectionapi.api;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.Nullable;
import tech.sbdevelopment.mapreflectionapi.exceptions.MapLimitExceededException;
@ -41,10 +42,12 @@ public class MapManager {
private final List<MapWrapper> MANAGED_MAPS = new CopyOnWriteArrayList<>();
private final Class<?> wrapperClass;
public MapManager() throws IllegalStateException {
public MapManager(JavaPlugin plugin) throws IllegalStateException {
String packageName = Bukkit.getServer().getClass().getPackage().getName();
String version = packageName.substring(packageName.lastIndexOf('.') + 1);
plugin.getLogger().info("Initializing the map manager for Minecraft version " + version + "...");
try {
final Class<?> clazz = Class.forName("tech.sbdevelopment.mapreflectionapi.nms.MapWrapper_" + version);
if (MapWrapper.class.isAssignableFrom(clazz)) {
@ -53,7 +56,7 @@ public class MapManager {
throw new IllegalStateException("Plugin corrupted! Detected invalid MapWrapper class.");
}
} catch (Exception ex) {
throw new IllegalStateException("This Spigot version is not supported! Contact the developer to get support.");
throw new IllegalStateException("This Spigot version (" + version + ") is not supported! Contact the developer to get support.");
}
}
@ -72,7 +75,7 @@ public class MapManager {
private MapWrapper wrapNewImage(ArrayImage image) {
try {
MapWrapper wrapper = (MapWrapper) wrapperClass.getDeclaredConstructor().newInstance();
MapWrapper wrapper = (MapWrapper) wrapperClass.getDeclaredConstructor(ArrayImage.class).newInstance(image);
MANAGED_MAPS.add(wrapper);
return wrapper;
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException |

View file

@ -23,8 +23,16 @@
package tech.sbdevelopment.mapreflectionapi.api;
public interface MapWrapper {
MapController getController();
public abstract class MapWrapper {
protected ArrayImage content;
ArrayImage getContent();
public MapWrapper(ArrayImage image) {
this.content = image;
}
public abstract MapController getController();
public ArrayImage getContent() {
return content;
}
}

View file

@ -85,6 +85,8 @@ public class MapInteractEvent extends Event implements Cancellable {
}
public ItemFrame getFrame() {
if (getMapWrapper() == null) return null;
if (frame == null) {
frame = getMapWrapper().getController().getItemFrameById(player.getWorld(), entityID);
}

View file

@ -31,26 +31,25 @@ import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.Vector;
import tech.sbdevelopment.mapreflectionapi.api.MapWrapper;
import java.lang.reflect.Field;
public abstract class PacketListener implements Listener {
protected JavaPlugin plugin;
public static PacketListener construct() {
public static PacketListener construct(JavaPlugin plugin) throws IllegalStateException {
String packageName = Bukkit.getServer().getClass().getPackage().getName();
String version = packageName.substring(packageName.lastIndexOf('.') + 1);
plugin.getLogger().info("Initializing the packet handler for Minecraft version " + version + "...");
try {
final Class<?> clazz = Class.forName("tech.sbdevelopment.mapreflectionapi.nms.MapWrapper_" + version);
if (MapWrapper.class.isAssignableFrom(clazz)) {
final Class<?> clazz = Class.forName("tech.sbdevelopment.mapreflectionapi.nms.PacketListener_" + version);
if (PacketListener.class.isAssignableFrom(clazz)) {
return (PacketListener) clazz.getDeclaredConstructor().newInstance();
} else {
throw new IllegalStateException("Plugin corrupted! Detected invalid MapWrapper class.");
throw new IllegalStateException("Plugin corrupted! Detected invalid PacketListener class.");
}
} catch (Exception ex) {
throw new IllegalStateException("This Spigot version is not supported! Contact the developer to get support.");
throw new IllegalStateException("This Minecraft version (" + version + ") is not supported! Contact the developer to get support.");
}
}
@ -83,16 +82,4 @@ public abstract class PacketListener implements Listener {
return false;
}
}
protected Object getField(Object packet, String field) throws NoSuchFieldException, IllegalAccessException {
Field f = packet.getClass().getDeclaredField(field);
f.setAccessible(true);
return f.get(packet);
}
protected void setField(Object packet, String field, Object value) throws NoSuchFieldException, IllegalAccessException {
Field f = packet.getClass().getDeclaredField(field);
f.setAccessible(true);
f.set(packet, value);
}
}

View file

@ -0,0 +1,54 @@
/*
* This file is part of MapReflectionAPI.
* Copyright (c) 2022 inventivetalent / SBDevelopment - All Rights Reserved
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package tech.sbdevelopment.mapreflectionapi.util;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class ReflectionUtil {
public static Object getField(Object packet, String field) throws NoSuchFieldException, IllegalAccessException {
Field f = packet.getClass().getDeclaredField(field);
f.setAccessible(true);
return f.get(packet);
}
public static Object getField(Class<?> clazz, String field) throws NoSuchFieldException, IllegalAccessException {
Field f = clazz.getDeclaredField(field);
f.setAccessible(true);
return f.get(null);
}
public static void setField(Object packet, String field, Object value) throws NoSuchFieldException, IllegalAccessException {
Field f = packet.getClass().getDeclaredField(field);
f.setAccessible(true);
f.set(packet, value);
}
public static Object getValue(Object packet, String method) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
Method m = packet.getClass().getDeclaredMethod(method, null);
m.setAccessible(true);
return m.invoke(packet, null);
}
}