Skip to content
This repository has been archived by the owner on Jan 23, 2022. It is now read-only.

Commit

Permalink
0.1.0 beta
Browse files Browse the repository at this point in the history
First public (pre) release
  • Loading branch information
Kuraky authored Apr 29, 2020
1 parent 366c686 commit 62bc67a
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#Custom health to be displayed instead
#Must be higher than 0
custom-health: 20.0
35 changes: 35 additions & 0 deletions src/me/kuraky/antihealthbars/AntiHealthbars.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
53 changes: 53 additions & 0 deletions src/me/kuraky/antihealthbars/HealthbarAdapter.java
Original file line number Diff line number Diff line change
@@ -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<DataWatcher.Item> items = (List<DataWatcher.Item>) 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);
}
}
}
}
23 changes: 23 additions & 0 deletions src/me/kuraky/antihealthbars/ReloadCommand.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
14 changes: 14 additions & 0 deletions src/plugin.yml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 62bc67a

Please sign in to comment.