Skip to content

Commit ad410d0

Browse files
committed
First attempt at combat tag plus support.
1 parent 0f80e75 commit ad410d0

File tree

1 file changed

+76
-4
lines changed

1 file changed

+76
-4
lines changed

base/src/main/java/net/techcable/spawnshield/CombatAPI.java

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@
3434

3535
import techcable.minecraft.combattag.CombatTagAPI;
3636

37+
import java.lang.reflect.Constructor;
38+
import java.lang.reflect.Field;
39+
import java.lang.reflect.Method;
40+
import java.util.UUID;
41+
42+
import net.techcable.techutils.Reflection;
43+
44+
import static net.techcable.techutils.Reflection.*;
45+
3746
/**
3847
* API to interface with Combat Tag, Combat Tag Reloaded, and PvPManager
3948
*
@@ -53,6 +62,8 @@ public static boolean isTagged(Player player) {
5362
return CombatTagAPI.isTagged(player);
5463
} else if (hasPvpManager()) {
5564
return getPlayerHandler().get(player).isInCombat();
65+
} else if (hasCombatTagPlus()) {
66+
return getRemainingTagTime(player) < 1;
5667
} else {
5768
return false;
5869
}
@@ -76,10 +87,19 @@ public static long getRemainingTagTime(Player player) {
7687
long timeLeft = (System.currentTimeMillis() - getPlayerHandler().get(player).getTaggedTime()) - Variables.timeInCombat * 1000; //Very Hacky -- PvPManager doesn't have a public api
7788
if (timeLeft < 1) return -1; //Not tagged
7889
return timeLeft;
90+
} else if (hasCombatTagPlus()){
91+
Object tag = getTag(player.getUniqueId());
92+
if (getTagDurationMethod == null) {
93+
getTagDurationMethod = makeMethod(Reflection.getClass("net.minelink.ctplus.Tag"), "getTagDuration");
94+
}
95+
int timeLeft = callMethod(getTagDurationMethod, tag);
96+
if (timeLeft < 1) return -1;
97+
return timeLeft * 1000;
7998
} else {
80-
return -2; //Not installed
99+
return -2;
81100
}
82101
}
102+
private Method getTagDurationMethod;
83103

84104
/**
85105
* Checks if an entity is a NPC
@@ -91,10 +111,20 @@ public static boolean isNPC(Entity entity) {
91111
return getCombatTagApi().isNPC(entity);
92112
} else if (hasCombatTagReloaded()) {
93113
return CombatTagAPI.isNPC(entity);
114+
} else if (hasCombatTagPlus()) {
115+
if (isNpcMethod == null) {
116+
isNpcMethod = makeMethod(Reflection.getClass("net.minelink.ctplus.compat.api.NpcPlayerHelper"), "isNpc", Player.class);
117+
}
118+
if (entity instanceof Player) {
119+
return callMethod(isNpcMethod, getNpcPlayerHelper(), (Player)entity);
120+
} else {
121+
return false;
122+
}
94123
} else {
95124
return false; //Not installed or PvPManager
96125
}
97126
}
127+
private static Method isNpcMethod;
98128

99129
/**
100130
* Tag this player
@@ -107,8 +137,14 @@ public static void tag(Player player) {
107137
CombatTagAPI.addTagged(player);
108138
} else if (hasPvpManager()) {
109139
getPlayerHandler().tag(getPlayerHandler().get(player));
140+
} else if (hasCombatTagPlus()) {
141+
if (tagMethod == null) {
142+
tagMethod = makeMethod(Reflection.getClass("net.minelink.ctplus.TagManager"), Player.class, Player.class);
143+
}
144+
callMethod(tagMethod, getTagManager(), player, null); //Will probably work
110145
}
111146
}
147+
private static Constructor tagMethod;
112148

113149
/**
114150
* UnTag this player
@@ -120,20 +156,53 @@ public static void unTag(Player player) {
120156
} else if (hasCombatTagReloaded()) {
121157
CombatTagAPI.removeTagged(player);
122158
} else if (hasPvpManager()) {
123-
getPlayerHandler().untag(getPlayerHandler().get(player));
159+
getPlayerHandler().untag(getPlayerHandler().get(player));
160+
} else if (hasCombatTagPlus()) {
161+
if (untagMethod == null) {
162+
untagMethod = makeMethod(Reflection.getClass("net.minelink.ctplus.TagManager"), "untag", UUID.class);
163+
}
164+
callMethod(untagMethod, getTagManager(), player.getUniqueId());
124165
}
125166
}
167+
private static Method untagMethod;
126168

127169
/**
128170
* Return wether a combat-tagging plugin is installed
129171
* Only CombatTag, CombatTagReloaded, and PvPManager are currently supported
130172
* @return true if a combat tag plugin is installed
131173
*/
132174
public static boolean isInstalled() {
133-
return hasCombatTag() || hasCombatTagReloaded() || hasPvpManager();
175+
return hasCombatTag() || hasCombatTagReloaded() || hasPvpManager() || hasCombatTagPlus();
134176
}
135177

136178
//Internal
179+
180+
private static Method getNpcPlayerHelperMethod;
181+
private static Object getNpcPlayerHelper() {
182+
Object plugin = Bukkit.getPluginManager().getPlugin("CombatTagPlus");
183+
if (getNpcPlayerHelperMethod == null) {
184+
getNpcPlayerHelperMethod = makeMethod(Reflection.getClass("net.minelink.ctplus.CombatTagPlus"), "getNpcPlayerHelper");
185+
}
186+
return callMethod(getNpcPlayerHelperMethod, plugin);
187+
}
188+
189+
private static Method getTagManagerMethod;
190+
private static Object getTagManager() {
191+
Object plugin = Bukkit.getPluginManager().getPlugin("CombatTagPlus");
192+
if (getTagManagerMethod == null) {
193+
getTagManagerMethod = makeMethod(Reflection.getClass("net.minelink.ctplus.CombatTagPlus"), "getTagManager");
194+
}
195+
return callMethod(getTagManagerMethod, plugin);
196+
}
197+
public static Object getTag(UUID playerId) {
198+
Object tagManger = getTagManager();
199+
if (getTagMethod == null) {
200+
getTagMethod = makeMethod(Reflection.getClass("net.minelink.ctplus.TagManager"), "getTag", UUID.class);
201+
}
202+
return callMethod(getTagMethod, tagManager, playerId);
203+
}
204+
private static Method getTagMethod;
205+
137206
private static PlayerHandler getPlayerHandler() {
138207
PvPManager plugin = (PvPManager) Bukkit.getPluginManager().getPlugin("PvPManager");
139208
return plugin.getPlayerHandler();
@@ -148,6 +217,9 @@ private static boolean hasCombatTagReloaded() {
148217
private static boolean hasPvpManager() {
149218
return Bukkit.getPluginManager().getPlugin("PvPManager") != null;
150219
}
220+
private static boolean hasCombatTagPlus() {
221+
return Bukkit.getPluginManager().getPlugin("CombatTagPlus") != null;
222+
}
151223

152224
private static CombatTagApi combatTagApi;
153225
private static CombatTagApi getCombatTagApi() {
@@ -157,4 +229,4 @@ private static CombatTagApi getCombatTagApi() {
157229
}
158230
return combatTagApi;
159231
}
160-
}
232+
}

0 commit comments

Comments
 (0)