Skip to content

Commit

Permalink
Remove hardcoded version tags
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzimmermann committed Aug 19, 2024
1 parent 0b62268 commit 9d4ad8c
Show file tree
Hide file tree
Showing 23 changed files with 106 additions and 238 deletions.
8 changes: 3 additions & 5 deletions src/main/java/de/codingair/codingapi/nms/NmsCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,15 @@ private static void test(Class<?> c) {
try {
runNmsLoader(c);
} catch (Throwable t) {
throw new NmsCheckError("Could not initialize class: " + c.getName() + ". Version=" + Version.get() + ", Type=" + Version.type() + ", Bukkit='" + Bukkit.getVersion() + "'", t);
throw new NmsCheckError("Could not initialize class: " + c.getName() + ". Version=" + Version.versionTag() + ", Type=" + Version.type() + ", Bukkit='" + Bukkit.getVersion() + "'", t);
}
}

private static void testVersion() {
try {
if (Version.get() == null) {
throw new NmsCheckError("Could not initialize version reader. Bukkit='" + Bukkit.getVersion() + "'");
}
Version.get();
} catch (Throwable t) {
throw new NmsCheckError("Could not initialize version reader. Bukkit='" + Bukkit.getVersion() + "'", t);
throw new NmsCheckError("Could not initialize version reader. Bukkit='" + Bukkit.getVersion() + "'");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class ParticlePacket {
private static final IReflection.MethodAccessor toNMS;

static {
if (Version.get().isBiggerThan(Version.v1_12)) {
if (Version.atLeast(13)) {
packetPlayOutWorldParticles = IReflection.getClass(IReflection.ServerPacket.PACKETS, "PacketPlayOutWorldParticles");
particleParam = IReflection.getClass(IReflection.ServerPacket.PARTICLES, "ParticleParam");
craftParticle = IReflection.getClass(IReflection.ServerPacket.CRAFTBUKKIT_PACKAGE, "CraftParticle");
Expand Down Expand Up @@ -60,7 +60,7 @@ public ParticlePacket initialize(Location loc) {
Class<?> packetClass = IReflection.getClass(IReflection.ServerPacket.PACKETS, "PacketPlayOutWorldParticles");
Constructor<?> packetConstructor = IReflection.getConstructor(packetClass).getConstructor();

if (Version.get().isBiggerThan(Version.v1_12)) {
if (Version.after(12)) {
Object data = null;
float offsetX = 0, offsetY = 0, offsetZ = 0, extra = 0;
int count = 1;
Expand Down Expand Up @@ -175,7 +175,7 @@ else if (particle == Particle.NOTE) {
}

public boolean available() {
if (Version.get().isBiggerThan(Version.v1_12)) return this.particle != null && this.particle.getName_v1_13() != null;
if (Version.after(12)) return this.particle != null && this.particle.getName_v1_13() != null;
Class<?> enumParticle = IReflection.getClass(IReflection.ServerPacket.MINECRAFT_PACKAGE, "EnumParticle");
return enumParticle.getEnumConstants().length - 1 >= this.particle.getId();
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/de/codingair/codingapi/player/Hologram.java
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ public static class HologramPackets {
SPAWN_PACKET_CONSTRUCTOR = IReflection.getConstructor(PacketUtils.PacketPlayOutSpawnEntityLivingClass, PacketUtils.EntityLivingClass);
if (SPAWN_PACKET_CONSTRUCTOR == null) throw new NullPointerException("Constructor could not be found.");

if (Version.get() == Version.v1_17)
if (Version.equals(17))
DESTROY_PACKET_CONSTRUCTOR = IReflection.getConstructor(PacketUtils.PacketPlayOutEntityDestroyClass, int.class);
else
DESTROY_PACKET_CONSTRUCTOR = IReflection.getConstructor(PacketUtils.PacketPlayOutEntityDestroyClass, int[].class);
Expand Down Expand Up @@ -683,10 +683,10 @@ public static void sendDataWatcher(Player player, Object armorStand) {

public static void destroy(Player player, Object armorStand) {
Object packet;
if (Version.get() == Version.v1_17) {
if (Version.equals(17)) {
packet = DESTROY_PACKET_CONSTRUCTOR.newInstance(PacketUtils.EntityPackets.getId(armorStand));
} else {
packet = DESTROY_PACKET_CONSTRUCTOR.newInstance((Object) new int[]{PacketUtils.EntityPackets.getId(armorStand)});
packet = DESTROY_PACKET_CONSTRUCTOR.newInstance(new int[]{PacketUtils.EntityPackets.getId(armorStand)});
}

PacketUtils.sendPacket(packet, player);
Expand All @@ -705,7 +705,7 @@ public static void spawn(Player player, Object armorStand) {
} else packet = SPAWN_PACKET_CONSTRUCTOR.newInstance(armorStand);
PacketUtils.sendPacket(packet, player);

if (Version.get().isBiggerThan(Version.v1_14)) sendDataWatcher(player, armorStand);
if (Version.after(14)) sendDataWatcher(player, armorStand);
}

public static void setLocation(Object armorStand, Location location) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/de/codingair/codingapi/player/MessageAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ public static void sendActionBar(Player p, String message) {

Object bar;

if (Version.get().isBiggerThan(15)) {
if (Version.after(15)) {
Class<?> type = IReflection.getClass(IReflection.ServerPacket.CHAT, "ChatMessageType");
IReflection.MethodAccessor a = IReflection.getMethod(type, "a", type, new Class[] {byte.class});
IReflection.ConstructorAccessor constructor = IReflection.getConstructor(packet, PacketUtils.IChatBaseComponentClass, type, UUID.class);

bar = constructor.newInstance(com, a.invoke(null, (byte) 2), UUID.randomUUID());
} else if (Version.get().isBiggerThan(11)) {
} else if (Version.after(11)) {
Class<?> type = IReflection.getClass(IReflection.ServerPacket.CHAT, "ChatMessageType");
IReflection.MethodAccessor a = IReflection.getMethod(type, "a", type, new Class[] {byte.class});
IReflection.ConstructorAccessor constructor = IReflection.getConstructor(packet, PacketUtils.IChatBaseComponentClass, type);
Expand Down Expand Up @@ -110,7 +110,7 @@ public static void sendTitle(Player p, String msg1, String msg2, int fadeIn, int
Class<?> enumTitle = IReflection.getClass(IReflection.ServerPacket.PACKETS, "PacketPlayOutTitle$EnumTitleAction");
IReflection.ConstructorAccessor constructor = IReflection.getConstructor(packet, enumTitle, ChatMessageClass, Integer.class, Integer.class, Integer.class);

int i = Version.get().isBiggerThan(Version.v1_10) ? 1 : 0;
int i = Version.after(10) ? 1 : 0;

Object resetP = !reset ? null : constructor.newInstance(enumTitle.getEnumConstants()[i + 4], PacketUtils.getChatMessage("DUMMY"), fadeIn, stay, fadeOut);
Object clearP = !clear ? null : constructor.newInstance(enumTitle.getEnumConstants()[i + 3], PacketUtils.getChatMessage("DUMMY"), fadeIn, stay, fadeOut);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ TextComponent build() {
if (this.hover != null && this.hover.length > 0) {
component.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, hover));
}
component.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, Version.since(19, "", "/") + PREFIX + this.uniqueId + (type == null ? "" : "#" + type)));
component.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, Version.choose("", 19, "/") + PREFIX + this.uniqueId + (type == null ? "" : "#" + type)));

return component;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class AnvilGUI implements Removable {
PACKET_PLAY_OUT_OPEN_WINDOW_CLASS = IReflection.getClass(IReflection.ServerPacket.PACKETS, "PacketPlayOutOpenWindow");
CONTAINER_CLASS = IReflection.getClass(IReflection.ServerPacket.INVENTORY, "Container");

if (Version.get().isBiggerThan(Version.v1_13)) {
if (Version.after(13)) {
Class<?> containerAccessClass = IReflection.getClass(IReflection.ServerPacket.INVENTORY, "ContainerAccess");
ANVIL_CONTAINER_CON = IReflection.getConstructor(containerAnvilClass, int.class, PLAYER_INVENTORY_CLASS, containerAccessClass);
} else {
Expand Down Expand Up @@ -124,7 +124,7 @@ public AnvilGUI(JavaPlugin plugin, Player player, AnvilListener listener, String
this.title = title == null ? "Repair & Name" : title;

registerBukkitListener();
if (Version.get().isBiggerThan(8))
if (Version.after(8))
Bukkit.getPluginManager().registerEvents(prepareListener = new PrepareAnvilEventHelp(), this.plugin);
}

Expand Down Expand Up @@ -265,7 +265,7 @@ public AnvilGUI open() {
int c = (int) NEXT_CONTAINER_COUNTER.invoke(entityPlayer);

Object container;
if (Version.get().isBiggerThan(Version.v1_13)) {
if (Version.after(13)) {
Class<?> containerAccessClass = IReflection.getClass(IReflection.ServerPacket.INVENTORY, "ContainerAccess");
IReflection.MethodAccessor at = IReflection.getMethod(containerAccessClass, containerAccessClass, new Class[]{WORLD_CLASS, BLOCK_POSITION_CLASS});

Expand All @@ -286,7 +286,7 @@ public AnvilGUI open() {
}

try {
if (Version.get().isBiggerThan(Version.v1_13)) {
if (Version.after(13)) {
String genericField = Version.choose("ANVIL", "ANVIL", 17, "h", 20.4, "i");

IReflection.FieldAccessor<?> generic = IReflection.getField(InventoryUtils.CONTAINERS_CLASS, genericField);
Expand All @@ -301,7 +301,7 @@ public AnvilGUI open() {
PacketUtils.sendPacket(this.player, packetPlayOutOpenWindowCon.newInstance(c, "minecraft:anvil", CHAT_MESSAGE_CON.newInstance("AnvilGUI", new Object[]{}), 0));
}
} catch (Exception e) {
plugin.getLogger().log(Level.SEVERE, "Error: Cannot open the AnvilGUI in " + Version.get().name() + "!", e);
plugin.getLogger().log(Level.SEVERE, "Error: Cannot open the AnvilGUI in " + Version.versionTag() + "!", e);
}

InventoryUtils.setActiveContainer(entityPlayer, container);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ public class InventoryUtils {

if (Version.atLeast(20.5)) UPDATE_INVENTORY = null;
else if (Version.atLeast(17)) {
UPDATE_INVENTORY = IReflection.getMethod(CONTAINER_CLASS, Version.since(18, "updateInventory", "b"));
UPDATE_INVENTORY = IReflection.getMethod(CONTAINER_CLASS, Version.choose("updateInventory", 18, "b"));
} else {
UPDATE_INVENTORY = IReflection.getMethod(PacketUtils.EntityPlayerClass, "updateInventory", new Class[]{CONTAINER_CLASS});
}

WINDOW_ID = IReflection.getField(CONTAINER_CLASS, Version.choose("containerId", "windowId", 17, "j"));
ACTIVE_CONTAINER = IReflection.getField(PacketUtils.EntityHumanClass, CONTAINER_CLASS, 1);

if (Version.get().isBiggerThan(Version.v1_13)) {
if (Version.after(13)) {
TITLE = IReflection.getField(CONTAINER_CLASS, "title");

CONTAINERS_CLASS = IReflection.getClass(IReflection.ServerPacket.INVENTORY, Version.choose("MenuType", "Containers"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ public void setClosingForGUI(boolean closingForGUI) {
}

public static boolean updateInventory(Player player) {
if(Version.get().isBiggerThan(Version.v1_13)) return false;
if(Version.after(13)) return false;
player.updateInventory();
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class Interface {
@Deprecated
public Interface(InventoryHolder owner, String title, int size, Plugin plugin) {
this.title = title;
if(this.title.length() > 32 && !Version.get().isBiggerThan(Version.v1_8)) this.title = this.title.substring(0, 32);
if(this.title.length() > 32 && !Version.after(8)) this.title = this.title.substring(0, 32);

this.inventory = Bukkit.createInventory(owner, size, this.oldTitle = getTitle());
if(plugin != null && !GUIListener.isRegistered()) GUIListener.register(plugin);
Expand Down Expand Up @@ -277,7 +277,7 @@ public boolean setSize(int size) {
void rebuildInventory() {
Inventory inventory = Bukkit.createInventory(getHolder(), getSize(), getTitle());
inventory.setContents(this.inventory.getContents());
if(Version.get().isBiggerThan(Version.v1_9)) inventory.setStorageContents(this.inventory.getStorageContents());
if(Version.after(9)) inventory.setStorageContents(this.inventory.getStorageContents());
inventory.setMaxStackSize(this.inventory.getMaxStackSize());
this.inventory = inventory;
}
Expand All @@ -290,7 +290,7 @@ public void setTitle(String title, boolean update) {
if(title == null || title.equals(this.title)) return;

this.title = title;
if(this.title.length() > 32 && !Version.get().isBiggerThan(Version.v1_8)) this.title = this.title.substring(0, 32);
if(this.title.length() > 32 && !Version.after(8)) this.title = this.title.substring(0, 32);

if(update) updateTitle();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public abstract class SignGUI {
static {
packetClass = IReflection.getClass(IReflection.ServerPacket.PACKETS, "PacketPlayInUpdateSign");

if (Version.get().isBiggerThan(8))
if (Version.after(8))
updatePacket = IReflection.getClass(IReflection.ServerPacket.PACKETS, "PacketPlayOutTileEntityData");
else updatePacket = IReflection.getClass(IReflection.ServerPacket.PACKETS, "PacketPlayOutUpdateSign");

Expand Down Expand Up @@ -104,7 +104,7 @@ public void onClose(InventoryCloseEvent e) {
}

public void open() {
if (Version.get().equals(Version.v1_7)) {
if (Version.before(8)) {
throw new IllegalStateException("The SignEditor does not work on 1.7!");
}

Expand Down Expand Up @@ -292,7 +292,7 @@ private void prepareSignEditing(@Nullable Sign sign) {
if (sign != null) {
Object tileEntity;

if (Version.get().isBiggerThan(Version.v1_11)) {
if (Version.after(11)) {
IReflection.MethodAccessor getTileEntity = IReflection.getMethod(sign.getClass(), "getTileEntity");
tileEntity = getTileEntity.invoke(sign);
} else tileEntity = IReflection.getField(sign.getClass(), "sign").get(sign);
Expand All @@ -304,7 +304,7 @@ private void prepareSignEditing(@Nullable Sign sign) {
IReflection.FieldAccessor<UUID> id = IReflection.getNonStaticField(PacketUtils.TileEntitySignClass, UUID.class, 0);
id.set(tileEntity, player.getUniqueId());
} else {
IReflection.FieldAccessor<?> owner = IReflection.getField(PacketUtils.TileEntitySignClass, Version.since(13, "h", "g", "j", "c"));
IReflection.FieldAccessor<?> owner = IReflection.getField(PacketUtils.TileEntitySignClass, Version.choose("h", 13, "g", 14, "j", 15, "c"));
owner.set(tileEntity, PacketUtils.getEntityPlayer(this.player));
}
}
Expand All @@ -314,7 +314,7 @@ private void openEditor(@NotNull Location signLocation) {
Packet packet = new Packet(PacketUtils.PacketPlayOutOpenSignEditorClass, player);

Object location = PacketUtils.getBlockPosition(signLocation);
packet.initialize(location, Version.since(20, Packet.IGNORE, true));
packet.initialize(location, Version.choose(Packet.IGNORE, 20, true));
packet.send();
}

Expand Down
42 changes: 6 additions & 36 deletions src/main/java/de/codingair/codingapi/server/Environment.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static boolean canBeEntered(Material m) {
}

public static boolean isWaterFluid(Block b) {
if(Version.get().isBiggerThan(12)) {
if(Version.after(12)) {
if (b.getBlockData() instanceof Waterlogged) {
Waterlogged w = (Waterlogged) b.getBlockData();
return w.isWaterlogged() && !b.getType().isSolid();
Expand Down Expand Up @@ -81,7 +81,7 @@ public static void playRandomFireworkEffect(Location loc) {

public static Sound getBreakSoundOf(Block b) {
Optional<Sound> cachedSound = CACHE.getIfPresent(b.getType());
if (cachedSound != null) return cachedSound.orElse(null);
if (cachedSound != null && cachedSound.isPresent()) return cachedSound.orElse(null);

Object w = PacketUtils.getWorldServer(b.getWorld());

Expand All @@ -92,7 +92,7 @@ public static Sound getBreakSoundOf(Block b) {
Object block = getBlock.invoke(blockType);

String key;
if(Version.get().isBiggerThan(Version.v1_8)) {
if(Version.after(8)) {
IReflection.FieldAccessor<?> getSoundEffectType = IReflection.getField(PacketUtils.BlockClass, "stepSound");

Class<?> soundEffectTypeClass = IReflection.getClass(IReflection.ServerPacket.BLOCK, "SoundEffectType");
Expand All @@ -101,43 +101,13 @@ public static Sound getBreakSoundOf(Block b) {

Object soundEffectType = getSoundEffectType.get(block);

String f;
switch(Version.get()) {
case v1_15:
f = "z";
break;
case v1_14:
f = "y";
break;
case v1_13:
f = "q";
break;
default:
f = "o";
break;
}

IReflection.FieldAccessor<?> getSoundEffect = IReflection.getField(soundEffectTypeClass, f);
IReflection.FieldAccessor<?> getSoundEffect = IReflection.getField(soundEffectTypeClass, Version.choose("o", 13, "q", 14, "y", 15, "z"));
Object soundEffect = getSoundEffect.get(soundEffectType);

switch(Version.get()) {
case v1_15:
case v1_14:
case v1_13:
f = "a";
break;
default:
f = "b";
break;
}

IReflection.FieldAccessor<?> getMCKey = IReflection.getField(soundEffectClass, f);
IReflection.FieldAccessor<?> getMCKey = IReflection.getField(soundEffectClass, Version.choose("b", 13, "a", 16, "b"));
Object mcKey = getMCKey.get(soundEffect);

if(Version.get().isBiggerThan(Version.v1_14)) f = "key";
else f = "a";

IReflection.FieldAccessor<String> getKey = IReflection.getField(minecraftKeyClass, f);
IReflection.FieldAccessor<String> getKey = IReflection.getField(minecraftKeyClass, Version.choose("a", 15, "key"));
key = getKey.get(mcKey);
} else {
IReflection.FieldAccessor<?> getStepSound = IReflection.getField(PacketUtils.BlockClass, "stepSound");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public ModernBlock(Block block) {
private void prepareBlock() {
Bukkit.getScheduler().runTask(API.getInstance().getMainPlugin(), () -> {
if(this.data != null) {
if(!Version.get().isBiggerThan(Version.v1_12)) this.data.setTypeAndDataTo(this.block, this.material, (byte) this.data.getData(block), false);
if(!Version.after(12)) this.data.setTypeAndDataTo(this.block, this.material, (byte) this.data.getData(block), false);
else {
block.setType(material);
this.data.setDataTo(block, this.data.getData(block));
Expand Down
Loading

0 comments on commit 9d4ad8c

Please sign in to comment.