Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new xp cooldown field resolving method #31

Merged
merged 8 commits into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/*
* MIT License
*
*
* Copyright (c) 2020-2022 CuukyOfficial
*
*
* 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
Expand Down Expand Up @@ -64,10 +64,10 @@ protected void initLocale() throws NoSuchFieldException, SecurityException, Ille
}

@Override
protected void initXp() {
protected void initXp(Player player) {
try {
Class<?> entityHuman = Class.forName("net.minecraft.entity.player.EntityPlayer");
this.xpCooldownField = entityHuman.getDeclaredField("field_71090_bL");
Class<?> entityHumanClass = Class.forName("net.minecraft.entity.player.EntityPlayer");
this.xpCooldownField = entityHumanClass.getDeclaredField("field_71090_bL");
} catch (Throwable t) {
t.printStackTrace();
}
Expand Down

This file was deleted.

105 changes: 61 additions & 44 deletions src/main/java/de/varoplugin/cfw/version/OneSevenVersionAdapter.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/*
* MIT License
*
*
* Copyright (c) 2020-2022 CuukyOfficial
*
*
* 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
Expand All @@ -28,18 +28,15 @@
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Properties;
import java.util.*;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.*;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.Sign;
import org.bukkit.plugin.Plugin;
Expand All @@ -65,7 +62,8 @@ class OneSevenVersionAdapter implements VersionAdapter {
OneSevenVersionAdapter() {
try {
this.init();
} catch (IllegalArgumentException | NoSuchMethodException | SecurityException | ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) {
} catch (IllegalArgumentException | NoSuchMethodException | SecurityException | ClassNotFoundException |
NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}
Expand All @@ -77,7 +75,53 @@ protected void init() throws IllegalArgumentException, NoSuchMethodException, Se
this.initRespawn();
this.initNetworkManager();
this.initLocale();
this.initXp();
}

protected void initXp(Player player) {
this.searchXpField(player, VersionUtils.getNmsClass() + ".EntityHuman", VersionUtils.getNmsClass() + ".EntityExperienceOrb");
}

protected void searchXpField(Player player, String humanClassName, String experienceOrbClassName) {
try {
Class<?> entityHumanClass = Class.forName(humanClassName);
Map<Field, Integer> values = new HashMap<>();
Object craftPlayer = this.getHandle(player);

// Get values of all int fields of player
for (Field field : entityHumanClass.getDeclaredFields()) {
if (field.getType() == int.class) {
field.setAccessible(true);
values.put(field, field.getInt(craftPlayer));
}
}


// Invoke method which sets the value of xp cooldown to 2
Location randomLoc = new Location(player.getWorld(), 0, 0, 0);
ExperienceOrb orb = (ExperienceOrb) player.getWorld().spawnEntity(randomLoc, EntityType.EXPERIENCE_ORB);
orb.setExperience(0);
Class<?> entityExperienceOrbClass = Class.forName(experienceOrbClassName);
for (Method method : entityExperienceOrbClass.getDeclaredMethods()) {
if (method.getParameterTypes().length == 1 && method.getParameterTypes()[0] == entityHumanClass) {
CuukyOfficial marked this conversation as resolved.
Show resolved Hide resolved
method.setAccessible(true);
method.invoke(this.getHandle(orb), craftPlayer);
}
}

orb.remove();

// Compare values and use the field with changed value
for (Field field : values.keySet()) {
if (field.getInt(craftPlayer) != values.get(field)) {
this.xpCooldownField = field;
return;
}
}

throw new Error("Unable to find xp cooldown field");
} catch (SecurityException | IllegalAccessException | InvocationTargetException | ClassNotFoundException e) {
throw new Error(e);
}
}

protected void initServerProperties() throws ClassNotFoundException, NoSuchMethodException, SecurityException, NoSuchFieldException {
Expand Down Expand Up @@ -118,38 +162,6 @@ protected void initLocale() throws NoSuchFieldException, SecurityException, Ille
this.localeField.setAccessible(true);
}

protected void initXp() {
this.initXp(VersionUtils.getNmsClass() + ".EntityHuman", VersionUtils.getNmsClass() + ".FoodMetaData");
}

protected void initXp(String entityHumanName, String foodMetaName) {
// this is EXTREMELY unsafe
try {
int fieldNum = 0;
for (Field field : Class.forName(entityHumanName).getDeclaredFields())
if (fieldNum == 0 && field.getType() == Class.forName(foodMetaName))
fieldNum = 1;
// This is for 1.19+, but I don't want to create a new version adapter class for one single line of code (Yes, I am lazy)
else if (fieldNum == 1 && field.getType().getName().equals("net.minecraft.world.entity.monster.warden.WardenSpawnTracker"))
;
else if (fieldNum == 1 && field.getType() == int.class)
fieldNum = 2;
else if (fieldNum == 2 && field.getType() == float.class)
fieldNum = 3;
else if (fieldNum == 3 && field.getType() == float.class)
fieldNum = 4;
else if (fieldNum == 4 && field.getType() == int.class) {
this.xpCooldownField = field;
return;
} else
fieldNum = 0;

throw new Error("Unable to find xp cooldown field");
} catch (SecurityException | ClassNotFoundException e) {
throw new Error(e);
}
}

protected Object getHandle(Entity entity) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
return this.entityHandleMethod.invoke(entity);
}
Expand Down Expand Up @@ -263,6 +275,10 @@ public void removeAi(LivingEntity entity) {

@Override
public void setXpCooldown(Player player, int cooldown) {
if (this.xpCooldownField == null) {
this.initXp(player);
}

try {
this.xpCooldownField.set(this.getHandle(player), cooldown);
} catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
Expand Down Expand Up @@ -317,7 +333,8 @@ public void forceClearWorlds() {
if (!Bukkit.unloadWorld(world, false))
throw new Error("Unable to unload world " + world.getName());
}
} catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException | InvocationTargetException | NoSuchMethodException e) {
} catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException |
InvocationTargetException | NoSuchMethodException e) {
throw new Error(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/*
* MIT License
*
*
* Copyright (c) 2020-2022 CuukyOfficial
*
*
* 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
Expand Down Expand Up @@ -50,8 +50,10 @@ protected void initRespawn() {
}

@Override
protected void initXp() {
this.initXp("net.minecraft.world.entity.player.EntityHuman", "net.minecraft.world.food.FoodMetaData");
protected void initXp(Player player) {
String entityHumanClass = VersionUtils.getNmsClass() + ".world.entity.player.EntityHuman";
String entityExperienceOrbClass = VersionUtils.getNmsClass() + ".world.entity.EntityExperienceOrb";
CuukyOfficial marked this conversation as resolved.
Show resolved Hide resolved
this.searchXpField(player, entityHumanClass, entityExperienceOrbClass);
}

@Override
Expand Down Expand Up @@ -95,7 +97,8 @@ public Properties getServerProperties() {
return (Properties) field.get(dedicatedServerProp);
}
throw new Error("missing properties field");
} catch (NoSuchMethodException | IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
} catch (NoSuchMethodException | IllegalArgumentException | IllegalAccessException |
InvocationTargetException e) {
throw new Error(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
public class OneTwentyVersionAdapter extends OneNineteenVersionAdapter {

@Override
protected void initXp() {
// nop
protected void initXp(Player player) {
// Not needed, since Player#setExpCooldown is available
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
public enum ServerSoftware {

MAGMA_1_18("Magma 1.18.2", SpigotVersionAdapter::new, "org.magmafoundation.magma.helpers.EnumJ17Helper", "Magma"),
MAGMA_1_12("Magma", versionSupplier -> new MagmaOneTwelveVersionAdapter(), "org.magmafoundation.magma.Magma", "Magma"),
MAGMA_1_12("Magma", null, "org.magmafoundation.magma.Magma", "Magma"),
CuukyOfficial marked this conversation as resolved.
Show resolved Hide resolved
CRUCIBLE("Crucible", versionSupplier -> new CrucibleVersionAdapter(), "io.github.crucible.CrucibleCommand", "Crucible"),
URANIUM("Uranium", null, null, "Uranium"),
THERMOS("Thermos", null, "thermos.Thermos", "Thermos"),
Expand Down
Loading