This repository has been archived by the owner on Jan 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First public (pre) release
- Loading branch information
Showing
5 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |