🎨 Some minor quality improvements
This commit is contained in:
parent
8b48084ef6
commit
9725b9a5aa
3 changed files with 30 additions and 69 deletions
|
@ -112,7 +112,7 @@ public class MapReflectionAPI extends JavaPlugin {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
getLogger().info("Found " + occupiedIDs + " occupied Map IDs." + (occupiedIDs > 0 ? " These will not be used." : ""));
|
getLogger().info("Found " + occupiedIDs + " occupied Map IDs" + (occupiedIDs > 0 ? ", these will not be used." : "."));
|
||||||
}
|
}
|
||||||
|
|
||||||
getLogger().info("Registering the listeners...");
|
getLogger().info("Registering the listeners...");
|
||||||
|
|
|
@ -23,6 +23,7 @@
|
||||||
|
|
||||||
package tech.sbdevelopment.mapreflectionapi.api;
|
package tech.sbdevelopment.mapreflectionapi.api;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import tech.sbdevelopment.mapreflectionapi.MapReflectionAPI;
|
import tech.sbdevelopment.mapreflectionapi.MapReflectionAPI;
|
||||||
|
@ -30,7 +31,6 @@ import tech.sbdevelopment.mapreflectionapi.utils.ReflectionUtil;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
public class MapSender {
|
public class MapSender {
|
||||||
private static final List<QueuedMap> sendQueue = new ArrayList<>();
|
private static final List<QueuedMap> sendQueue = new ArrayList<>();
|
||||||
|
@ -123,7 +123,7 @@ public class MapSender {
|
||||||
|
|
||||||
packet = ReflectionUtil.callConstructor(packetPlayOutMapClass,
|
packet = ReflectionUtil.callConstructor(packetPlayOutMapClass,
|
||||||
id, //ID
|
id, //ID
|
||||||
(byte) 0, //Scale
|
(byte) 0, //Scale, 0 = 1 block per pixel
|
||||||
false, //Show icons
|
false, //Show icons
|
||||||
new ArrayList<>(), //Icons
|
new ArrayList<>(), //Icons
|
||||||
updateData
|
updateData
|
||||||
|
@ -131,7 +131,7 @@ public class MapSender {
|
||||||
} else if (ReflectionUtil.supports(14)) { //1.16-1.14
|
} else if (ReflectionUtil.supports(14)) { //1.16-1.14
|
||||||
packet = ReflectionUtil.callConstructor(packetPlayOutMapClass,
|
packet = ReflectionUtil.callConstructor(packetPlayOutMapClass,
|
||||||
id, //ID
|
id, //ID
|
||||||
(byte) 0, //Scale
|
(byte) 0, //Scale, 0 = 1 block per pixel
|
||||||
false, //Tracking position
|
false, //Tracking position
|
||||||
false, //Locked
|
false, //Locked
|
||||||
new ArrayList<>(), //Icons
|
new ArrayList<>(), //Icons
|
||||||
|
@ -144,7 +144,7 @@ public class MapSender {
|
||||||
} else { //1.13-
|
} else { //1.13-
|
||||||
packet = ReflectionUtil.callConstructor(packetPlayOutMapClass,
|
packet = ReflectionUtil.callConstructor(packetPlayOutMapClass,
|
||||||
id, //ID
|
id, //ID
|
||||||
(byte) 0, //Scale
|
(byte) 0, //Scale, 0 = 1 block per pixel
|
||||||
false, //???
|
false, //???
|
||||||
new ArrayList<>(), //Icons
|
new ArrayList<>(), //Icons
|
||||||
content.array, //Data
|
content.array, //Data
|
||||||
|
@ -158,38 +158,10 @@ public class MapSender {
|
||||||
ReflectionUtil.sendPacket(player, packet);
|
ReflectionUtil.sendPacket(player, packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
static final class QueuedMap {
|
static final class QueuedMap {
|
||||||
private final int id;
|
private final int id;
|
||||||
private final ArrayImage image;
|
private final ArrayImage image;
|
||||||
private final Player player;
|
private final Player player;
|
||||||
|
|
||||||
QueuedMap(int id, ArrayImage image, Player player) {
|
|
||||||
this.id = id;
|
|
||||||
this.image = image;
|
|
||||||
this.player = player;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object obj) {
|
|
||||||
if (obj == this) return true;
|
|
||||||
if (obj == null || obj.getClass() != this.getClass()) return false;
|
|
||||||
QueuedMap that = (QueuedMap) obj;
|
|
||||||
return this.id == that.id &&
|
|
||||||
Objects.equals(this.image, that.image) &&
|
|
||||||
Objects.equals(this.player, that.player);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
return Objects.hash(id, image, player);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "QueuedMap[" +
|
|
||||||
"id=" + id + ", " +
|
|
||||||
"image=" + image + ", " +
|
|
||||||
"player=" + player + ']';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,7 +28,6 @@ import org.bukkit.entity.ItemFrame;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.meta.ItemMeta;
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
import org.bukkit.inventory.meta.MapMeta;
|
|
||||||
import org.bukkit.metadata.FixedMetadataValue;
|
import org.bukkit.metadata.FixedMetadataValue;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import tech.sbdevelopment.mapreflectionapi.MapReflectionAPI;
|
import tech.sbdevelopment.mapreflectionapi.MapReflectionAPI;
|
||||||
|
@ -43,7 +42,7 @@ public class MapWrapper {
|
||||||
private static final String REFERENCE_METADATA = "MAP_WRAPPER_REF";
|
private static final String REFERENCE_METADATA = "MAP_WRAPPER_REF";
|
||||||
protected ArrayImage content;
|
protected ArrayImage content;
|
||||||
|
|
||||||
public static Material MAP_MATERIAL;
|
private static final Material MAP_MATERIAL;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
MAP_MATERIAL = ReflectionUtil.supports(13) ? Material.FILLED_MAP : Material.MAP;
|
MAP_MATERIAL = ReflectionUtil.supports(13) ? Material.FILLED_MAP : Material.MAP;
|
||||||
|
@ -60,7 +59,7 @@ public class MapWrapper {
|
||||||
|
|
||||||
private static final Class<?> craftStackClass = ReflectionUtil.getCraftClass("inventory.CraftItemStack");
|
private static final Class<?> craftStackClass = ReflectionUtil.getCraftClass("inventory.CraftItemStack");
|
||||||
private static final Class<?> setSlotPacketClass = ReflectionUtil.getNMSClass("network.protocol.game", "PacketPlayOutSetSlot");
|
private static final Class<?> setSlotPacketClass = ReflectionUtil.getNMSClass("network.protocol.game", "PacketPlayOutSetSlot");
|
||||||
private static final Class<?> tagCompoundClass = ReflectionUtil.getNMSClass("nbt", "NBTTagCompound");
|
//private static final Class<?> tagCompoundClass = ReflectionUtil.getNMSClass("nbt", "NBTTagCompound");
|
||||||
private static final Class<?> entityClass = ReflectionUtil.getNMSClass("world.entity", "Entity");
|
private static final Class<?> entityClass = ReflectionUtil.getNMSClass("world.entity", "Entity");
|
||||||
private static final Class<?> dataWatcherClass = ReflectionUtil.getNMSClass("network.syncher", "DataWatcher");
|
private static final Class<?> dataWatcherClass = ReflectionUtil.getNMSClass("network.syncher", "DataWatcher");
|
||||||
private static final Class<?> entityMetadataPacketClass = ReflectionUtil.getNMSClass("network.protocol.game", "PacketPlayOutEntityMetadata");
|
private static final Class<?> entityMetadataPacketClass = ReflectionUtil.getNMSClass("network.protocol.game", "PacketPlayOutEntityMetadata");
|
||||||
|
@ -166,8 +165,6 @@ public class MapWrapper {
|
||||||
slot = 8 - (slot - 36);
|
slot = 8 - (slot - 36);
|
||||||
}
|
}
|
||||||
|
|
||||||
Object playerHandle = ReflectionUtil.getHandle(player);
|
|
||||||
|
|
||||||
String inventoryMenuName;
|
String inventoryMenuName;
|
||||||
if (ReflectionUtil.supports(19)) { //1.19
|
if (ReflectionUtil.supports(19)) { //1.19
|
||||||
inventoryMenuName = "bT";
|
inventoryMenuName = "bT";
|
||||||
|
@ -178,7 +175,7 @@ public class MapWrapper {
|
||||||
} else { //1.12-1.16
|
} else { //1.12-1.16
|
||||||
inventoryMenuName = "defaultContainer";
|
inventoryMenuName = "defaultContainer";
|
||||||
}
|
}
|
||||||
Object inventoryMenu = ReflectionUtil.getField(playerHandle, inventoryMenuName);
|
Object inventoryMenu = ReflectionUtil.getField(ReflectionUtil.getHandle(player), inventoryMenuName);
|
||||||
// int windowId = (int) ReflectionUtil.getField(inventoryMenu, ReflectionUtil.supports(17) ? "j" : "windowId");
|
// int windowId = (int) ReflectionUtil.getField(inventoryMenu, ReflectionUtil.supports(17) ? "j" : "windowId");
|
||||||
|
|
||||||
ItemStack stack;
|
ItemStack stack;
|
||||||
|
@ -208,7 +205,7 @@ public class MapWrapper {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
ReflectionUtil.sendPacket(player, packet);
|
ReflectionUtil.sendPacketSync(player, packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -218,8 +215,9 @@ public class MapWrapper {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void showInHand(Player player, boolean force) {
|
public void showInHand(Player player, boolean force) {
|
||||||
if (player.getInventory().getItemInMainHand().getType() != (ReflectionUtil.supports(13) ? Material.FILLED_MAP : Material.MAP) && !force)
|
if (player.getInventory().getItemInMainHand().getType() != MAP_MATERIAL && !force)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
showInInventory(player, player.getInventory().getHeldItemSlot(), force);
|
showInInventory(player, player.getInventory().getHeldItemSlot(), force);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -235,8 +233,9 @@ public class MapWrapper {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void showInFrame(Player player, ItemFrame frame, boolean force) {
|
public void showInFrame(Player player, ItemFrame frame, boolean force) {
|
||||||
if (frame.getItem().getType() != (ReflectionUtil.supports(13) ? Material.FILLED_MAP : Material.MAP) && !force)
|
if (frame.getItem().getType() != MAP_MATERIAL && !force)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
showInFrame(player, frame.getEntityId());
|
showInFrame(player, frame.getEntityId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -249,7 +248,7 @@ public class MapWrapper {
|
||||||
public void showInFrame(Player player, int entityId, String debugInfo) {
|
public void showInFrame(Player player, int entityId, String debugInfo) {
|
||||||
if (!isViewing(player)) return;
|
if (!isViewing(player)) return;
|
||||||
|
|
||||||
ItemStack stack = new ItemStack(ReflectionUtil.supports(13) ? Material.FILLED_MAP : Material.MAP, 1);
|
ItemStack stack = new ItemStack(MAP_MATERIAL, 1);
|
||||||
if (debugInfo != null) {
|
if (debugInfo != null) {
|
||||||
ItemMeta itemMeta = stack.getItemMeta();
|
ItemMeta itemMeta = stack.getItemMeta();
|
||||||
itemMeta.setDisplayName(debugInfo);
|
itemMeta.setDisplayName(debugInfo);
|
||||||
|
@ -295,36 +294,26 @@ public class MapWrapper {
|
||||||
|
|
||||||
if (craftEntity.getClass().isAssignableFrom(itemFrameClass))
|
if (craftEntity.getClass().isAssignableFrom(itemFrameClass))
|
||||||
return (ItemFrame) itemFrameClass.cast(craftEntity);
|
return (ItemFrame) itemFrameClass.cast(craftEntity);
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Object createCraftItemStack(ItemStack stack, int mapId) {
|
private Object createCraftItemStack(@NotNull ItemStack stack, int mapId) {
|
||||||
Object nmsStack;
|
if (mapId < 0) return null;
|
||||||
if (ReflectionUtil.supports(16)) { //TODO Check why 1.16+ can use this, and 1.15- requires NMS
|
|
||||||
MapMeta meta = (MapMeta) stack.getItemMeta();
|
Object nmsStack = ReflectionUtil.callMethod(craftStackClass, "asNMSCopy", stack);
|
||||||
meta.setMapId(mapId);
|
|
||||||
stack.setItemMeta(meta);
|
|
||||||
nmsStack = ReflectionUtil.callMethod(craftStackClass, "asNMSCopy", stack);
|
|
||||||
} else {
|
|
||||||
nmsStack = ReflectionUtil.callMethod(craftStackClass, "asNMSCopy", stack);
|
|
||||||
|
|
||||||
if (ReflectionUtil.supports(13)) {
|
if (ReflectionUtil.supports(13)) {
|
||||||
// String nbtObjectName;
|
String nbtObjectName;
|
||||||
// if (ReflectionUtil.supports(19)) { //1.19
|
if (ReflectionUtil.supports(19)) { //1.19
|
||||||
// nbtObjectName = "v";
|
nbtObjectName = "v";
|
||||||
// } else if (ReflectionUtil.supports(18)) { //1.18
|
} else if (ReflectionUtil.supports(18)) { //1.18
|
||||||
// nbtObjectName = ReflectionUtil.VER_MINOR == 1 ? "t" : "u"; //1.18.1 = t, 1.18(.2) = u
|
nbtObjectName = ReflectionUtil.VER_MINOR == 1 ? "t" : "u"; //1.18.1 = t, 1.18(.2) = u
|
||||||
// } else { //1.13 - 1.17
|
} else { //1.13 - 1.17
|
||||||
// nbtObjectName = "getOrCreateTag";
|
nbtObjectName = "getOrCreateTag";
|
||||||
// }
|
|
||||||
Object nbtObject = ReflectionUtil.callMethod(nmsStack,
|
|
||||||
//nbtObjectName
|
|
||||||
"getOrCreateTag");
|
|
||||||
|
|
||||||
ReflectionUtil.callMethod(nbtObject,
|
|
||||||
//ReflectionUtil.supports(18) ? "a" :
|
|
||||||
"setInt", "map", mapId);
|
|
||||||
}
|
}
|
||||||
|
Object nbtObject = ReflectionUtil.callMethod(nmsStack, nbtObjectName);
|
||||||
|
ReflectionUtil.callMethod(nbtObject, ReflectionUtil.supports(18) ? "a" : "setInt", "map", mapId);
|
||||||
}
|
}
|
||||||
return nmsStack;
|
return nmsStack;
|
||||||
}
|
}
|
||||||
|
@ -360,7 +349,7 @@ public class MapWrapper {
|
||||||
list.add(dataWatcherItem);
|
list.add(dataWatcherItem);
|
||||||
ReflectionUtil.setDeclaredField(packet, "b", list);
|
ReflectionUtil.setDeclaredField(packet, "b", list);
|
||||||
|
|
||||||
ReflectionUtil.sendPacket(player, packet);
|
ReflectionUtil.sendPacketSync(player, packet);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue