-
Notifications
You must be signed in to change notification settings - Fork 36
/
PlotSquaredLink.java
171 lines (151 loc) · 6.44 KB
/
PlotSquaredLink.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package simplepets.brainsynder.links.impl;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import simple.brainsynder.utils.Reflection;
import simplepets.brainsynder.PetCore;
import simplepets.brainsynder.links.IPlotSquaredLink;
import simplepets.brainsynder.player.PetOwner;
import simplepets.brainsynder.reflection.ReflectionUtil;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.UUID;
public class PlotSquaredLink extends PluginLink implements IPlotSquaredLink {
private Object ps = null;
private Method plotArea, getPlot, hasOwner, isDenied;
private Constructor locCon = null;
private String packageName = "com.intellectualcrafters.plot";
public PlotSquaredLink() {
super("PlotSquared");
}
@Override public boolean onHook() {
String main = "PS";
Class<?> mainClass = null;
ClassLoader loader = PetCore.get().getLoader();
try {
mainClass = Class.forName(packageName+"."+main, false, loader);
// Server uses old P2 API
PetCore.get().debug("Server is using the old PlotSquared API");
}catch (Exception e) {
main = "PlotSquared";
packageName = "com.github.intellectualsites.plotsquared.plot";
try {
mainClass = Class.forName(packageName+"."+main, false, loader);
// Server uses new P2 API
PetCore.get().debug("Server is using the new PlotSquared API");
}catch (Exception e2) {
try {
packageName = "com.plotsquared.core";
mainClass = Class.forName(packageName+"."+main, false, loader);
// Server uses newer P2 API
PetCore.get().debug("Server is using the newer PlotSquared API");
ps = invoke(getMethod(mainClass, "get"), null);
// Objects are different here
Class<?> loc = getClass("location.Location");
plotArea = getMethod(mainClass, "getApplicablePlotArea", loc);
getPlot = getMethod(getClass("plot.PlotArea"), "getPlot", loc);
hasOwner = getMethod(getClass("plot.Plot"), "hasOwner");
isDenied = getMethod(getClass("plot.Plot"), "isDenied", UUID.class);
locCon = ReflectionUtil.getConstructor(loc, String.class, Integer.TYPE, Integer.TYPE, Integer.TYPE);
return true;
} catch (Exception ex) {
return false;
}
}
}
ps = invoke(getMethod(mainClass, "get"), null);
Class<?> loc = getClass("object.Location");
plotArea = getMethod(mainClass, "getApplicablePlotArea", loc);
getPlot = getMethod(getClass("object.PlotArea"), "getPlot", loc);
hasOwner = getMethod(getClass("object.Plot"), "hasOwner");
isDenied = getMethod(getClass("object.Plot"), "isDenied", UUID.class);
locCon = ReflectionUtil.getConstructor(loc, String.class, Integer.TYPE, Integer.TYPE, Integer.TYPE);
return true;
}
@Override public void onUnhook() {
ps = null;
plotArea = null;
getPlot = null;
hasOwner = null;
isDenied = null;
locCon = null;
super.onUnhook();
}
@Override
public boolean allowPetEntry(PetOwner owner, Location at) {
return fetchValue("Move", owner, at);
}
@Override
public boolean allowPetSpawn(PetOwner owner, Location at) {
return fetchValue("Spawn", owner, at);
}
@Override
public boolean allowPetRiding(PetOwner owner, Location at) {
return fetchValue("Riding", owner, at);
}
@Override
public boolean allowPetEntry(Location location) {
return allowPetEntry (null, location);
}
@Override
public boolean allowPetSpawn(Location at) {
return allowPetSpawn(null, at);
}
private boolean fetchValue (String section, PetOwner owner, Location at) {
if (!isHooked()) return true;
// Creates new location
Object loc = Reflection.initiateClass(locCon, at.getWorld().getName(), at.getBlockX(), at.getBlockY(), at.getBlockZ());
Object area = invoke(plotArea, ps, loc);
if (area == null) return true;
Object plot = invoke(getPlot, area, loc);
if (owner != null) {
if (owner.getPlayer().hasPermission(PetCore.get().getConfiguration().getString("PlotSquared.BypassPermission", false))) return true;
}
if (plot == null) {
return PetCore.get().getConfiguration().getBoolean("PlotSquared.On-Roads."+section);
}
if (!(boolean) invoke(hasOwner, plot))
return PetCore.get().getConfiguration().getBoolean("PlotSquared.On-Unclaimed-Plots."+section);
if (PetCore.get().getConfiguration().getBoolean("PlotSquared.Block-If-Denied."+section) && (owner != null))
return !(boolean)invoke(isDenied, plot, owner.getPlayer().getUniqueId());
return true;
}
private Method getMethod (String className, String method, Class<?>... params) {
try {
return Reflection.getMethod(Class.forName(packageName+"."+className), method, params);
}catch (Exception e){
return null;
}
}
private Method getMethod (Class<?> clazz, String method, Class<?>... params) {
try {
return Reflection.getMethod(clazz, method, params);
}catch (Exception e){
Bukkit.getLogger().severe("Could not get method '"+method+"' because: ");
e.printStackTrace();
return null;
}
}
private Class<?> getClass (String className) {
try {
return Class.forName(packageName+"."+className);
}catch (Exception e){
Bukkit.getLogger().severe("Could not get class '"+className+"' because: ");
e.printStackTrace();
return null;
}
}
private Object invoke(Method method, Object instance, Object... parameters) {
if (method == null) {
Bukkit.getLogger().severe("Method can not be null");
return null;
} else {
try {
return method.invoke(instance, parameters);
} catch (Exception e) {
Bukkit.getLogger().severe("Could not invoke method '"+method.getName()+"' because: ");
e.printStackTrace();
return null;
}
}
}
}