From 62bc67a2b9b3247894a12e77c70648c393326303 Mon Sep 17 00:00:00 2001 From: Kuraky <38810662+Kuraky@users.noreply.github.com> Date: Wed, 29 Apr 2020 22:15:19 +0200 Subject: [PATCH] 0.1.0 beta First public (pre) release --- src/config.yml | 3 ++ .../kuraky/antihealthbars/AntiHealthbars.java | 35 ++++++++++++ .../antihealthbars/HealthbarAdapter.java | 53 +++++++++++++++++++ .../kuraky/antihealthbars/ReloadCommand.java | 23 ++++++++ src/plugin.yml | 14 +++++ 5 files changed, 128 insertions(+) create mode 100644 src/config.yml create mode 100644 src/me/kuraky/antihealthbars/AntiHealthbars.java create mode 100644 src/me/kuraky/antihealthbars/HealthbarAdapter.java create mode 100644 src/me/kuraky/antihealthbars/ReloadCommand.java create mode 100644 src/plugin.yml diff --git a/src/config.yml b/src/config.yml new file mode 100644 index 0000000..3d31b7f --- /dev/null +++ b/src/config.yml @@ -0,0 +1,3 @@ +#Custom health to be displayed instead +#Must be higher than 0 +custom-health: 20.0 \ No newline at end of file diff --git a/src/me/kuraky/antihealthbars/AntiHealthbars.java b/src/me/kuraky/antihealthbars/AntiHealthbars.java new file mode 100644 index 0000000..f3f446f --- /dev/null +++ b/src/me/kuraky/antihealthbars/AntiHealthbars.java @@ -0,0 +1,35 @@ +package me.kuraky.antihealthbars; + +import com.comphenix.protocol.ProtocolLibrary; +import org.bukkit.command.PluginCommand; +import org.bukkit.plugin.java.JavaPlugin; + +public class AntiHealthbars extends JavaPlugin { + + private float customHealth; + private static AntiHealthbars INSTANCE; + + @Override + public void onEnable() { + INSTANCE = this; + saveDefaultConfig(); + setCustomHealth(getConfig().getDouble("custom-health")); + ProtocolLibrary.getProtocolManager().addPacketListener(new HealthbarAdapter()); + + PluginCommand reloadCommand = getCommand("ahreload"); + if(reloadCommand != null) reloadCommand.setExecutor(new ReloadCommand()); + } + + public static AntiHealthbars getINSTANCE() { + return INSTANCE; + } + + public float getCustomHealth() { + return customHealth; + } + + public void setCustomHealth(double customHealth) { + if(customHealth < 0.00001) customHealth = 0.00001; + this.customHealth = (float) customHealth; + } +} diff --git a/src/me/kuraky/antihealthbars/HealthbarAdapter.java b/src/me/kuraky/antihealthbars/HealthbarAdapter.java new file mode 100644 index 0000000..d51d672 --- /dev/null +++ b/src/me/kuraky/antihealthbars/HealthbarAdapter.java @@ -0,0 +1,53 @@ +package me.kuraky.antihealthbars; + +import com.comphenix.protocol.PacketType; +import com.comphenix.protocol.ProtocolLibrary; +import com.comphenix.protocol.events.ListenerPriority; +import com.comphenix.protocol.events.PacketAdapter; +import com.comphenix.protocol.events.PacketContainer; +import com.comphenix.protocol.events.PacketEvent; +import net.minecraft.server.v1_15_R1.DataWatcher; + +import java.lang.reflect.Field; +import java.util.List; + +public class HealthbarAdapter extends PacketAdapter { + + public HealthbarAdapter() { + super(AntiHealthbars.getINSTANCE(), ListenerPriority.HIGHEST, PacketType.Play.Server.ENTITY_METADATA); + } + + @Override + public void onPacketSending(PacketEvent event) { + int entityId = event.getPacket().getIntegers().read(0); + int playerId = event.getPlayer().getEntityId(); + + if(entityId != playerId) { + Object packet = event.getPacket().deepClone().getHandle(); + try { + Field list = packet.getClass().getDeclaredField("b"); + list.setAccessible(true); + + List items = (List) list.get(packet); + + for (DataWatcher.Item item : items) { + if (item.a().hashCode() == 8) { //index for health in metadata + Field b = item.getClass().getDeclaredField("b"); //it's stored there + b.setAccessible(true); + try { + if ((Float) b.get(item) > 0) { //we don't want to change player's health if it's equal to 0, otherwise death animation won't play + b.set(item, AntiHealthbars.getINSTANCE().getCustomHealth()); + event.setPacket(PacketContainer.fromPacket(packet)); + } + } catch (ClassCastException | IllegalArgumentException ignored) { //the class cast exception will be catched a lot, due to 8 also being an index for other values + } + break; + } + } + } catch (NoSuchFieldException | IllegalAccessException e) { + e.printStackTrace(); + ProtocolLibrary.getProtocolManager().removePacketListener(this); + } + } + } +} diff --git a/src/me/kuraky/antihealthbars/ReloadCommand.java b/src/me/kuraky/antihealthbars/ReloadCommand.java new file mode 100644 index 0000000..60e7c78 --- /dev/null +++ b/src/me/kuraky/antihealthbars/ReloadCommand.java @@ -0,0 +1,23 @@ +package me.kuraky.antihealthbars; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; + +public class ReloadCommand implements CommandExecutor { + + @Override + public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) { + if(commandSender.hasPermission("antihealthbars.reload")) { + AntiHealthbars plugin = AntiHealthbars.getINSTANCE(); + + plugin.reloadConfig(); + + double customHealth = plugin.getConfig().getDouble("custom-health"); + plugin.setCustomHealth(customHealth); + + commandSender.sendMessage("§aReloaded, set health to §e" + customHealth); + } + return false; + } +} diff --git a/src/plugin.yml b/src/plugin.yml new file mode 100644 index 0000000..0f36609 --- /dev/null +++ b/src/plugin.yml @@ -0,0 +1,14 @@ +name: AntiHealthbars +main: me.kuraky.antihealthbars.AntiHealthbars +version: 0.1.0b +description: Blocks healthbar mods +api-version: 1.15 +author: Kuraky +depend: [ProtocolLib] +permissions: + antihealthbars.reload: + description: Allows reloading the config +commands: + ahreload: + description: Reloads the config + permission: antihealthbars.reload \ No newline at end of file