Skip to content

Commit 4285f89

Browse files
committed
Access PvPManager through reflection, and drop combat tag reloaded
support
1 parent ad410d0 commit 4285f89

File tree

2 files changed

+50
-45
lines changed

2 files changed

+50
-45
lines changed

base/pom.xml

100644100755
Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,6 @@
2020
<scope>provided</scope>
2121
<optional>true</optional>
2222
</dependency>
23-
<dependency>
24-
<groupId>net.techcable</groupId>
25-
<artifactId>combattag</artifactId>
26-
<version>1.0.13-SNAPSHOT</version>
27-
<scope>provided</scope>
28-
<optional>true</optional>
29-
</dependency>
30-
<dependency>
31-
<groupId>PvPManager</groupId>
32-
<artifactId>PvPManager</artifactId>
33-
<version>2.4.7</version>
34-
<scope>provided</scope>
35-
<optional>true</optional>
36-
</dependency>
3723
<!-- Compatibility -->
3824
<dependency>
3925
<groupId>net.techcable.spawnshield</groupId>

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

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,14 @@
2222
*/
2323
package net.techcable.spawnshield;
2424

25-
import me.NoChance.PvPManager.Config.Variables;
26-
import me.NoChance.PvPManager.Managers.PlayerHandler;
27-
import me.NoChance.PvPManager.PvPManager;
2825
import org.bukkit.Bukkit;
2926
import org.bukkit.entity.Entity;
3027
import org.bukkit.entity.Player;
3128

3229
import com.trc202.CombatTag.CombatTag;
3330
import com.trc202.CombatTagApi.CombatTagApi;
3431

32+
import org.bukkit.plugin.Plugin;
3533
import techcable.minecraft.combattag.CombatTagAPI;
3634

3735
import java.lang.reflect.Constructor;
@@ -58,16 +56,18 @@ private CombatAPI() {}
5856
public static boolean isTagged(Player player) {
5957
if (hasCombatTag()) {
6058
return getCombatTagApi().isInCombat(player);
61-
} else if (hasCombatTagReloaded()) {
62-
return CombatTagAPI.isTagged(player);
6359
} else if (hasPvpManager()) {
64-
return getPlayerHandler().get(player).isInCombat();
60+
if (isInCombatMethod == null) {
61+
isInCombatMethod = makeMethod(Reflection.getClass("me.NoChance.PvPManager.PvPlayer"), "isInCombat");
62+
}
63+
return callMethod(isInCombatMethod, getPvpPlayer(player));
6564
} else if (hasCombatTagPlus()) {
6665
return getRemainingTagTime(player) < 1;
6766
} else {
6867
return false;
6968
}
7069
}
70+
private static Method isInCombatMethod;
7171

7272
/**
7373
* Returns the time a player has left in combat
@@ -81,10 +81,16 @@ public static boolean isTagged(Player player) {
8181
public static long getRemainingTagTime(Player player) {
8282
if (hasCombatTag()) {
8383
return getCombatTagApi().getRemainingTagTime(player);
84-
} else if (hasCombatTagReloaded()) {
85-
return CombatTagAPI.getRemainingTagTime(player);
8684
} else if (hasPvpManager()) {
87-
long timeLeft = (System.currentTimeMillis() - getPlayerHandler().get(player).getTaggedTime()) - Variables.timeInCombat * 1000; //Very Hacky -- PvPManager doesn't have a public api
85+
if (getTaggedTimeMethod == null) {
86+
getTaggedTimeMethod = makeMethod(Reflection.getClass("me.NoChance.PvPManager.PvPlayer"), "getTaggedTime");
87+
}
88+
long taggedTime = callMethod(getTaggedTimeMethod, getPvpPlayer(player));
89+
if (timeInCombatField == null) {
90+
timeInCombatField = makeField(Reflection.getClass("me.NoChance.PvPManager.Config.Variables"), "timeInCombat");
91+
}
92+
long timeInCombat = getField(timeInCombatField, null);
93+
long timeLeft = (System.currentTimeMillis() - taggedTime) - timeInCombat * 1000; //Very Hacky -- PvPManager doesn't have a public api
8894
if (timeLeft < 1) return -1; //Not tagged
8995
return timeLeft;
9096
} else if (hasCombatTagPlus()){
@@ -99,7 +105,9 @@ public static long getRemainingTagTime(Player player) {
99105
return -2;
100106
}
101107
}
102-
private Method getTagDurationMethod;
108+
private static Field timeInCombatField;
109+
private static Method getTaggedTimeMethod;
110+
private static Method getTagDurationMethod;
103111

104112
/**
105113
* Checks if an entity is a NPC
@@ -109,8 +117,6 @@ public static long getRemainingTagTime(Player player) {
109117
public static boolean isNPC(Entity entity) {
110118
if (hasCombatTag()) {
111119
return getCombatTagApi().isNPC(entity);
112-
} else if (hasCombatTagReloaded()) {
113-
return CombatTagAPI.isNPC(entity);
114120
} else if (hasCombatTagPlus()) {
115121
if (isNpcMethod == null) {
116122
isNpcMethod = makeMethod(Reflection.getClass("net.minelink.ctplus.compat.api.NpcPlayerHelper"), "isNpc", Player.class);
@@ -133,18 +139,20 @@ public static boolean isNPC(Entity entity) {
133139
public static void tag(Player player) {
134140
if (hasCombatTag()) {
135141
getCombatTagApi().tagPlayer(player);
136-
} else if (hasCombatTagReloaded()) {
137-
CombatTagAPI.addTagged(player);
138142
} else if (hasPvpManager()) {
139-
getPlayerHandler().tag(getPlayerHandler().get(player));
143+
if (playerHandlerTagMethod == null) {
144+
playerHandlerTagMethod = makeMethod(Reflection.getClass("me.NoChance.PvPManager.Managers.PlayerHandler"), "tag", Reflection.getClass("me.NoChance.PvPManager.PvPlayer"));
145+
}
146+
callMethod(playerHandlerTagMethod, getPlayerHandler(), getPvpPlayer(player));
140147
} else if (hasCombatTagPlus()) {
141148
if (tagMethod == null) {
142-
tagMethod = makeMethod(Reflection.getClass("net.minelink.ctplus.TagManager"), Player.class, Player.class);
149+
tagMethod = makeMethod(Reflection.getClass("net.minelink.ctplus.TagManager"), "tag", Player.class, Player.class);
143150
}
144151
callMethod(tagMethod, getTagManager(), player, null); //Will probably work
145152
}
146153
}
147-
private static Constructor tagMethod;
154+
private static Method tagMethod;
155+
private static Method playerHandlerTagMethod;
148156

149157
/**
150158
* UnTag this player
@@ -153,10 +161,11 @@ public static void tag(Player player) {
153161
public static void unTag(Player player) {
154162
if (hasCombatTag()) {
155163
getCombatTagApi().untagPlayer(player);
156-
} else if (hasCombatTagReloaded()) {
157-
CombatTagAPI.removeTagged(player);
158164
} else if (hasPvpManager()) {
159-
getPlayerHandler().untag(getPlayerHandler().get(player));
165+
if (playerHandlerUntagMethod == null) {
166+
playerHandlerUntagMethod = makeMethod(Reflection.getClass("me.NoChance.PvPManager.Managers.PlayerHandler"), "untag", Reflection.getClass("me.NoChance.PvPManager.PvPlayer"));
167+
}
168+
callMethod(playerHandlerUntagMethod, getPlayerHandler(), getPvpPlayer(player));
160169
} else if (hasCombatTagPlus()) {
161170
if (untagMethod == null) {
162171
untagMethod = makeMethod(Reflection.getClass("net.minelink.ctplus.TagManager"), "untag", UUID.class);
@@ -165,14 +174,15 @@ public static void unTag(Player player) {
165174
}
166175
}
167176
private static Method untagMethod;
168-
177+
private static Method playerHandlerUntagMethod;
178+
169179
/**
170180
* Return wether a combat-tagging plugin is installed
171181
* Only CombatTag, CombatTagReloaded, and PvPManager are currently supported
172182
* @return true if a combat tag plugin is installed
173183
*/
174184
public static boolean isInstalled() {
175-
return hasCombatTag() || hasCombatTagReloaded() || hasPvpManager() || hasCombatTagPlus();
185+
return hasCombatTag() || hasPvpManager() || hasCombatTagPlus();
176186
}
177187

178188
//Internal
@@ -195,25 +205,34 @@ private static Object getTagManager() {
195205
return callMethod(getTagManagerMethod, plugin);
196206
}
197207
public static Object getTag(UUID playerId) {
198-
Object tagManger = getTagManager();
208+
Object tagManager = getTagManager();
199209
if (getTagMethod == null) {
200210
getTagMethod = makeMethod(Reflection.getClass("net.minelink.ctplus.TagManager"), "getTag", UUID.class);
201211
}
202212
return callMethod(getTagMethod, tagManager, playerId);
203213
}
204214
private static Method getTagMethod;
205-
206-
private static PlayerHandler getPlayerHandler() {
207-
PvPManager plugin = (PvPManager) Bukkit.getPluginManager().getPlugin("PvPManager");
208-
return plugin.getPlayerHandler();
215+
216+
private static Object getPlayerHandler() {
217+
Plugin plugin = Bukkit.getPluginManager().getPlugin("PvPManager");
218+
if (getPlayerHandlerMethod == null) {
219+
getPlayerHandlerMethod = makeMethod(Reflection.getClass("me.NoChance.PvPManager.PvPManager"), "getPlayerHandler");
220+
}
221+
return callMethod(getPlayerHandlerMethod, plugin);
209222
}
210-
223+
private static Method getPlayerHandlerMethod;
224+
225+
private static Object getPvpPlayer(Player player) {
226+
if (playerHandlerGetMethod == null) {
227+
playerHandlerGetMethod = makeMethod(Reflection.getClass("me.NoChance.PvPManager.Managers.PlayerHandler"), "get", Player.class);
228+
}
229+
return callMethod(playerHandlerGetMethod, getPlayerHandler(), player);
230+
}
231+
private static Method playerHandlerGetMethod;
232+
211233
private static boolean hasCombatTag() {
212234
return Bukkit.getPluginManager().getPlugin("CombatTag") != null;
213235
}
214-
private static boolean hasCombatTagReloaded() {
215-
return Bukkit.getPluginManager().getPlugin("CombatTagReloaded") != null;
216-
}
217236
private static boolean hasPvpManager() {
218237
return Bukkit.getPluginManager().getPlugin("PvPManager") != null;
219238
}

0 commit comments

Comments
 (0)