Added option to limit health style symbols and more
- Added option to limit health style symbols - Using reflection for latest version
This commit is contained in:
parent
14a6f54bb8
commit
d3df0895aa
|
@ -54,8 +54,8 @@ Disabled regions:
|
|||
# Disable ActionHealth in regions where PvP is denied.
|
||||
Region PvP: true
|
||||
|
||||
# When set to true and style health is enabled this will limit the health symbols to 10.
|
||||
Limit Health: true
|
||||
# Limits the health style to a certain amount of symbols.
|
||||
Limit Health: 10
|
||||
|
||||
# Saves players /actionhealth toggle state.
|
||||
Remember Toggle: false
|
||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -108,18 +108,18 @@ public class HealthUtil {
|
|||
}
|
||||
|
||||
if (output.contains("{usestyle}")) {
|
||||
String style = "";
|
||||
int left = 10;
|
||||
double heart = maxHealth / 10;
|
||||
StringBuilder style = new StringBuilder();
|
||||
int left = plugin.settingsManager.limitHealth;
|
||||
double heart = maxHealth / plugin.settingsManager.limitHealth;
|
||||
double halfHeart = heart / 2;
|
||||
double tempHealth = health;
|
||||
|
||||
if (maxHealth != health && health >= 0 && !entity.isDead()) {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
for (int i = 0; i < plugin.settingsManager.limitHealth; i++) {
|
||||
if (tempHealth - heart > 0) {
|
||||
tempHealth = tempHealth - heart;
|
||||
|
||||
style = style + plugin.settingsManager.filledHeartIcon;
|
||||
style.append(plugin.settingsManager.filledHeartIcon);
|
||||
left--;
|
||||
} else {
|
||||
break;
|
||||
|
@ -127,25 +127,25 @@ public class HealthUtil {
|
|||
}
|
||||
|
||||
if (tempHealth > halfHeart) {
|
||||
style = style + plugin.settingsManager.filledHeartIcon;
|
||||
style.append(plugin.settingsManager.filledHeartIcon);
|
||||
left--;
|
||||
} else if (tempHealth > 0 && tempHealth <= halfHeart) {
|
||||
style = style + plugin.settingsManager.halfHeartIcon;
|
||||
style.append(plugin.settingsManager.halfHeartIcon);
|
||||
left--;
|
||||
}
|
||||
}
|
||||
|
||||
if (maxHealth != health) {
|
||||
for (int i = 0; i < left; i++) {
|
||||
style = style + plugin.settingsManager.emptyHeartIcon;
|
||||
style.append(plugin.settingsManager.emptyHeartIcon);
|
||||
}
|
||||
} else {
|
||||
for (int i = 0; i < left; i++) {
|
||||
style = style + plugin.settingsManager.filledHeartIcon;
|
||||
style.append(plugin.settingsManager.filledHeartIcon);
|
||||
}
|
||||
}
|
||||
|
||||
output = output.replace("{usestyle}", style);
|
||||
output = output.replace("{usestyle}", style.toString());
|
||||
}
|
||||
|
||||
if (plugin.settingsManager.placeholderAPI) {
|
||||
|
|
|
@ -1,16 +1,35 @@
|
|||
package com.zeshanaslam.actionhealth;
|
||||
|
||||
import net.minecraft.server.v1_12_R1.ChatMessageType;
|
||||
import net.minecraft.server.v1_12_R1.IChatBaseComponent;
|
||||
import net.minecraft.server.v1_12_R1.PacketPlayOutChat;
|
||||
import org.bukkit.craftbukkit.v1_12_R1.entity.CraftPlayer;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
public class PreAction {
|
||||
|
||||
private final String packageVersion = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3];
|
||||
|
||||
public PreAction(Player player, String message) {
|
||||
IChatBaseComponent cbc = IChatBaseComponent.ChatSerializer.a("{\"text\": \"" + message + "\"}");
|
||||
PacketPlayOutChat packetPlayOutChat = new PacketPlayOutChat(cbc, ChatMessageType.GAME_INFO);
|
||||
((CraftPlayer) player).getHandle().playerConnection.sendPacket(packetPlayOutChat);
|
||||
try {
|
||||
Object chatComponentText = getNMSClass("ChatComponentText").getConstructor(new Class[]{String.class}).newInstance(message);
|
||||
Object chatMessageType = getNMSClass("ChatMessageType").getField("GAME_INFO").get(null);
|
||||
Object packetPlayOutChat = getNMSClass("PacketPlayOutChat").getConstructor(new Class[]{getNMSClass("IChatBaseComponent"), getNMSClass("ChatMessageType")}).newInstance(chatComponentText, chatMessageType);
|
||||
Object getHandle = player.getClass().getMethod("getHandle", new Class[0]).invoke(player);
|
||||
Object playerConnection = getHandle.getClass().getField("playerConnection").get(getHandle);
|
||||
|
||||
playerConnection.getClass().getMethod("sendPacket", new Class[]{getNMSClass("Packet")}).invoke(playerConnection, packetPlayOutChat);
|
||||
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException | NoSuchFieldException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private Class<?> getNMSClass(String nmsClassName) {
|
||||
try {
|
||||
return Class.forName("net.minecraft.server." + packageVersion + "." + nmsClassName);
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,6 +37,7 @@ public class SettingsManager {
|
|||
public String enableMessage;
|
||||
public String disableMessage;
|
||||
public boolean placeholderAPI;
|
||||
public int limitHealth;
|
||||
|
||||
public SettingsManager(Main plugin) {
|
||||
// Clear settings for reloads
|
||||
|
@ -136,5 +137,13 @@ public class SettingsManager {
|
|||
} else {
|
||||
spectatorMode = true;
|
||||
}
|
||||
|
||||
if (plugin.getConfig().contains("Limit Health")) {
|
||||
if (plugin.getConfig().isBoolean("Limit Health")) {
|
||||
limitHealth = 10;
|
||||
} else {
|
||||
limitHealth = plugin.getConfig().getInt("Limit Health");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue