⚡ Moved class definitions out of methods
This commit is contained in:
parent
3ca16d49b8
commit
c6cf6c2c3b
3 changed files with 19 additions and 41 deletions
|
@ -31,7 +31,6 @@ import org.jetbrains.annotations.Nullable;
|
||||||
import tech.sbdevelopment.mapreflectionapi.exceptions.MapLimitExceededException;
|
import tech.sbdevelopment.mapreflectionapi.exceptions.MapLimitExceededException;
|
||||||
|
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
@ -40,24 +39,12 @@ import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
public class MapManager {
|
public class MapManager {
|
||||||
protected final Set<Integer> OCCUPIED_IDS = new HashSet<>();
|
protected final Set<Integer> OCCUPIED_IDS = new HashSet<>();
|
||||||
private final List<MapWrapper> MANAGED_MAPS = new CopyOnWriteArrayList<>();
|
private final List<MapWrapper> MANAGED_MAPS = new CopyOnWriteArrayList<>();
|
||||||
private final Class<?> wrapperClass;
|
|
||||||
|
|
||||||
public MapManager(JavaPlugin plugin) throws IllegalStateException {
|
public MapManager(JavaPlugin plugin) throws IllegalStateException {
|
||||||
String packageName = Bukkit.getServer().getClass().getPackage().getName();
|
String packageName = Bukkit.getServer().getClass().getPackage().getName();
|
||||||
String version = packageName.substring(packageName.lastIndexOf('.') + 1);
|
String version = packageName.substring(packageName.lastIndexOf('.') + 1);
|
||||||
|
|
||||||
plugin.getLogger().info("Initializing the map manager for Minecraft version " + version + "...");
|
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)) {
|
|
||||||
wrapperClass = clazz;
|
|
||||||
} else {
|
|
||||||
throw new IllegalStateException("Plugin corrupted! Detected invalid MapWrapper class.");
|
|
||||||
}
|
|
||||||
} catch (Exception ex) {
|
|
||||||
throw new IllegalStateException("This Spigot version (" + version + ") is not supported! Contact the developer to get support.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
|
@ -74,15 +61,9 @@ public class MapManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
private MapWrapper wrapNewImage(ArrayImage image) {
|
private MapWrapper wrapNewImage(ArrayImage image) {
|
||||||
try {
|
MapWrapper wrapper = new MapWrapper(image);
|
||||||
MapWrapper wrapper = (MapWrapper) wrapperClass.getDeclaredConstructor(ArrayImage.class).newInstance(image);
|
|
||||||
MANAGED_MAPS.add(wrapper);
|
MANAGED_MAPS.add(wrapper);
|
||||||
return wrapper;
|
return wrapper;
|
||||||
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException |
|
|
||||||
InvocationTargetException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void unwrapImage(MapWrapper wrapper) {
|
public void unwrapImage(MapWrapper wrapper) {
|
||||||
|
|
|
@ -63,6 +63,9 @@ public class MapSender {
|
||||||
}, 0, 2);
|
}, 0, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static final Class<?> packetPlayOutMapClass = ReflectionUtils.getNMSClass("network.protocol.game", "PacketPlayOutMap");
|
||||||
|
private static final Class<?> worldMapData = ReflectionUtils.supports(17) ? ReflectionUtils.getNMSClass("world.level.saveddata.maps", "WorldMap") : null;
|
||||||
|
|
||||||
public static void sendMap(final int id0, final ArrayImage content, final Player player) {
|
public static void sendMap(final int id0, final ArrayImage content, final Player player) {
|
||||||
if (player == null || !player.isOnline()) {
|
if (player == null || !player.isOnline()) {
|
||||||
List<QueuedMap> toRemove = new ArrayList<>();
|
List<QueuedMap> toRemove = new ArrayList<>();
|
||||||
|
@ -79,12 +82,10 @@ public class MapSender {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Class<?> packetClass = ReflectionUtils.getNMSClass("network.protocol.game", "PacketPlayOutMap");
|
|
||||||
final int id = -id0;
|
final int id = -id0;
|
||||||
Object packet;
|
Object packet;
|
||||||
if (ReflectionUtils.supports(17)) { //1.17+
|
if (ReflectionUtils.supports(17)) { //1.17+
|
||||||
Class<?> worldMapClass = ReflectionUtils.getNMSClass("world.level.saveddata.maps", "WorldMap");
|
Object updateData = ReflectionUtil.callConstructor(worldMapData,
|
||||||
Object updateData = ReflectionUtil.callConstructor(worldMapClass,
|
|
||||||
content.minX, //X pos
|
content.minX, //X pos
|
||||||
content.minY, //Y pos
|
content.minY, //Y pos
|
||||||
content.maxX, //X size (2nd X pos)
|
content.maxX, //X size (2nd X pos)
|
||||||
|
@ -92,7 +93,7 @@ public class MapSender {
|
||||||
content.array //Data
|
content.array //Data
|
||||||
);
|
);
|
||||||
|
|
||||||
packet = ReflectionUtil.callConstructor(packetClass,
|
packet = ReflectionUtil.callConstructor(packetPlayOutMapClass,
|
||||||
id, //ID
|
id, //ID
|
||||||
(byte) 0, //Scale
|
(byte) 0, //Scale
|
||||||
false, //Show icons
|
false, //Show icons
|
||||||
|
@ -100,7 +101,7 @@ public class MapSender {
|
||||||
updateData
|
updateData
|
||||||
);
|
);
|
||||||
} else if (ReflectionUtils.supports(14)) { //1.16-1.14
|
} else if (ReflectionUtils.supports(14)) { //1.16-1.14
|
||||||
packet = ReflectionUtil.callConstructor(packetClass,
|
packet = ReflectionUtil.callConstructor(packetPlayOutMapClass,
|
||||||
id, //ID
|
id, //ID
|
||||||
(byte) 0, //Scale
|
(byte) 0, //Scale
|
||||||
false, //Tracking position
|
false, //Tracking position
|
||||||
|
@ -113,7 +114,7 @@ public class MapSender {
|
||||||
content.maxY //Y size (2nd Y pos)
|
content.maxY //Y size (2nd Y pos)
|
||||||
);
|
);
|
||||||
} else { //1.13-
|
} else { //1.13-
|
||||||
packet = ReflectionUtil.callConstructor(packetClass,
|
packet = ReflectionUtil.callConstructor(packetPlayOutMapClass,
|
||||||
id, //ID
|
id, //ID
|
||||||
(byte) 0, //Scale
|
(byte) 0, //Scale
|
||||||
false, //???
|
false, //???
|
||||||
|
|
|
@ -43,6 +43,15 @@ public class MapWrapper {
|
||||||
this.content = image;
|
this.content = image;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static final Class<?> craftStackClass = ReflectionUtils.getCraftClass("CraftItemStack");
|
||||||
|
private static final Class<?> setSlotPacketClass = ReflectionUtils.getNMSClass("network.protocol.game", "PacketPlayOutSetSlot");
|
||||||
|
private static final Class<?> tagCompoundClass = ReflectionUtils.getCraftClass("NBTTagCompound");
|
||||||
|
private static final Class<?> entityClass = ReflectionUtils.getNMSClass("world.entity", "Entity");
|
||||||
|
private static final Class<?> dataWatcherClass = ReflectionUtils.getNMSClass("network.syncher", "DataWatcher");
|
||||||
|
private static final Class<?> entityMetadataPacketClass = ReflectionUtils.getNMSClass("network.protocol.game", "PacketPlayOutEntityMetadata");
|
||||||
|
private static final Class<?> entityItemFrameClass = ReflectionUtils.getNMSClass("world.entity.decoration", "EntityItemFrame");
|
||||||
|
private static final Class<?> dataWatcherItemClass = ReflectionUtils.getNMSClass("network.syncher", "DataWatcher$Item");
|
||||||
|
|
||||||
protected MapController controller = new MapController() {
|
protected MapController controller = new MapController() {
|
||||||
private final Map<UUID, Integer> viewers = new HashMap<>();
|
private final Map<UUID, Integer> viewers = new HashMap<>();
|
||||||
|
|
||||||
|
@ -134,10 +143,8 @@ public class MapWrapper {
|
||||||
|
|
||||||
ItemStack stack = new ItemStack(ReflectionUtils.supports(13) ? Material.FILLED_MAP : Material.MAP, 1);
|
ItemStack stack = new ItemStack(ReflectionUtils.supports(13) ? Material.FILLED_MAP : Material.MAP, 1);
|
||||||
|
|
||||||
Class<?> craftStackClass = ReflectionUtils.getCraftClass("CraftItemStack");
|
|
||||||
Object nmsStack = ReflectionUtil.callMethod(craftStackClass, "asNMSCopy", stack);
|
Object nmsStack = ReflectionUtil.callMethod(craftStackClass, "asNMSCopy", stack);
|
||||||
|
|
||||||
Class<?> setSlotPacketClass = ReflectionUtils.getNMSClass("network.protocol.game", "PacketPlayOutSetSlot");
|
|
||||||
Object packet;
|
Object packet;
|
||||||
if (ReflectionUtils.supports(17)) { //1.17+
|
if (ReflectionUtils.supports(17)) { //1.17+
|
||||||
int stateId = (int) ReflectionUtil.callMethod(inventoryMenu, ReflectionUtils.supports(18) ? "j" : "getStateId");
|
int stateId = (int) ReflectionUtil.callMethod(inventoryMenu, ReflectionUtils.supports(18) ? "j" : "getStateId");
|
||||||
|
@ -240,26 +247,17 @@ public class MapWrapper {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendItemFramePacket(Player player, int entityId, ItemStack stack, int mapId) {
|
private void sendItemFramePacket(Player player, int entityId, ItemStack stack, int mapId) {
|
||||||
Class<?> craftStackClass = ReflectionUtils.getCraftClass("CraftItemStack");
|
|
||||||
Object nmsStack = ReflectionUtil.callMethod(craftStackClass, "asNMSCopy", stack);
|
Object nmsStack = ReflectionUtil.callMethod(craftStackClass, "asNMSCopy", stack);
|
||||||
|
|
||||||
Object nbtObject = ReflectionUtil.callMethod(nmsStack, ReflectionUtils.supports(19) ? "v" : ReflectionUtils.supports(18) ? "u" : ReflectionUtils.supports(13) ? "getOrCreateTag" : "getTag");
|
Object nbtObject = ReflectionUtil.callMethod(nmsStack, ReflectionUtils.supports(19) ? "v" : ReflectionUtils.supports(18) ? "u" : ReflectionUtils.supports(13) ? "getOrCreateTag" : "getTag");
|
||||||
|
|
||||||
if (!ReflectionUtils.supports(13) && nbtObject == null) { //1.12 has no getOrCreate, call create if null!
|
if (!ReflectionUtils.supports(13) && nbtObject == null) { //1.12 has no getOrCreate, call create if null!
|
||||||
Class<?> tagCompoundClass = ReflectionUtils.getCraftClass("NBTTagCompound");
|
|
||||||
Object tagCompound = ReflectionUtil.callConstructor(tagCompoundClass);
|
Object tagCompound = ReflectionUtil.callConstructor(tagCompoundClass);
|
||||||
|
|
||||||
ReflectionUtil.callMethod(nbtObject, "setTag", tagCompound);
|
ReflectionUtil.callMethod(nbtObject, "setTag", tagCompound);
|
||||||
}
|
}
|
||||||
|
|
||||||
ReflectionUtil.callMethod(nbtObject, ReflectionUtils.supports(18) ? "a" : "setInt", "map", mapId);
|
ReflectionUtil.callMethod(nbtObject, ReflectionUtils.supports(18) ? "a" : "setInt", "map", mapId);
|
||||||
|
|
||||||
Class<?> entityClass = ReflectionUtils.getNMSClass("world.entity", "Entity");
|
|
||||||
|
|
||||||
Class<?> dataWatcherClass = ReflectionUtils.getNMSClass("network.syncher", "DataWatcher");
|
|
||||||
Object dataWatcher = ReflectionUtil.callConstructor(dataWatcherClass, entityClass.cast(null));
|
Object dataWatcher = ReflectionUtil.callConstructor(dataWatcherClass, entityClass.cast(null));
|
||||||
|
|
||||||
Class<?> entityMetadataPacketClass = ReflectionUtils.getNMSClass("network.protocol.game", "PacketPlayOutEntityMetadata");
|
|
||||||
Object packet = ReflectionUtil.callConstructor(entityMetadataPacketClass,
|
Object packet = ReflectionUtil.callConstructor(entityMetadataPacketClass,
|
||||||
entityId,
|
entityId,
|
||||||
dataWatcher, //dummy watcher!
|
dataWatcher, //dummy watcher!
|
||||||
|
@ -267,9 +265,7 @@ public class MapWrapper {
|
||||||
);
|
);
|
||||||
|
|
||||||
List<Object> list = new ArrayList<>();
|
List<Object> list = new ArrayList<>();
|
||||||
Class<?> entityItemFrameClass = ReflectionUtils.getNMSClass("world.entity.decoration", "EntityItemFrame");
|
|
||||||
Object dataWatcherObject = ReflectionUtil.getDeclaredField(entityItemFrameClass, ReflectionUtils.supports(17) ? "ao" : ReflectionUtils.supports(14) ? "ITEM" : ReflectionUtils.supports(13) ? "e" : "c");
|
Object dataWatcherObject = ReflectionUtil.getDeclaredField(entityItemFrameClass, ReflectionUtils.supports(17) ? "ao" : ReflectionUtils.supports(14) ? "ITEM" : ReflectionUtils.supports(13) ? "e" : "c");
|
||||||
Class<?> dataWatcherItemClass = ReflectionUtils.getNMSClass("network.syncher", "DataWatcher$Item");
|
|
||||||
Object dataWatcherItem = ReflectionUtil.callConstructor(dataWatcherItemClass, dataWatcherObject, nmsStack);
|
Object dataWatcherItem = ReflectionUtil.callConstructor(dataWatcherItemClass, dataWatcherObject, nmsStack);
|
||||||
list.add(dataWatcherItem);
|
list.add(dataWatcherItem);
|
||||||
ReflectionUtil.setDeclaredField(packet, "b", list);
|
ReflectionUtil.setDeclaredField(packet, "b", list);
|
||||||
|
|
Loading…
Add table
Reference in a new issue