/
TownyPlayerExtension.java
106 lines (91 loc) · 3.54 KB
/
TownyPlayerExtension.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
package net.gnomeffinway.depenizen.extensions.towny;
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.TownyUniverse;
import net.aufdemrand.denizen.objects.Element;
import net.aufdemrand.denizen.objects.dObject;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.gnomeffinway.depenizen.extensions.dObjectExtension;
import net.gnomeffinway.depenizen.objects.dNation;
import net.gnomeffinway.depenizen.objects.dTown;
public class TownyPlayerExtension extends dObjectExtension {
public static boolean describes(dObject pl) {
return pl instanceof dPlayer;
}
public static TownyPlayerExtension getFrom(dObject pl) {
if (!describes(pl)) return null;
else return new TownyPlayerExtension((dPlayer) pl);
}
private TownyPlayerExtension(dPlayer pl) { player = pl; }
dPlayer player = null;
@Override
public String getAttribute(Attribute attribute) {
Resident resident;
try {
resident = TownyUniverse.getDataSource().getResident(player.getName());
} catch (NotRegisteredException e) {
if (!attribute.hasAlternative())
dB.echoError("'" + player.getName() + "' is not registered in Towny!");
return null;
}
// <--[tag]
// @attribute <p@player.has_nation>
// @returns Element(Boolean)
// @description
// Returns whether the player is part of a nation.
// @plugin Depenizen, Towny
// -->
if (attribute.startsWith("has_nation")) {
return new Element(resident.hasNation()).getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <p@player.has_town>
// @returns Element(Boolean)
// @description
// Returns whether the player is part of a town.
// @plugin Depenizen, Towny
// -->
if (attribute.startsWith("has_town")) {
return new Element(resident.hasTown()).getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <p@player.nation>
// @returns dNation
// @description
// Returns the player's nation.
// @plugin Depenizen, Towny
// -->
if (attribute.startsWith("nation")) {
try {
if (resident.hasNation())
return new dNation(resident.getTown().getNation()).getAttribute(attribute.fulfill(1));
else
return null;
} catch (NotRegisteredException e) {
if (!attribute.hasAlternative())
dB.echoError("'" + player.getName() + "' is not registered to a nation in Towny!");
}
}
// <--[tag]
// @attribute <p@player.town>
// @returns dTown
// @description
// Returns the player's town.
// @plugin Depenizen, Towny
// -->
if (attribute.startsWith("town")) {
try {
if (resident.hasTown())
return new dTown(resident.getTown()).getAttribute(attribute.fulfill(1));
else
return null;
} catch (NotRegisteredException e) {
if (!attribute.hasAlternative())
dB.echoError("'" + player.getName() + "' is not registered to a town in Towny!");
}
}
return null;
}
}