Skip to content

Commit fdd0d54

Browse files
committed
Support UUIDs for mob owner functions
1 parent 9eb4800 commit fdd0d54

File tree

4 files changed

+84
-34
lines changed

4 files changed

+84
-34
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package com.laytonsmith.abstraction;
22

3+
import java.util.UUID;
4+
35
public interface MCAnimalTamer extends AbstractionObject {
46

57
String getName();
8+
9+
UUID getUniqueID();
610
}

src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCAnimalTamer.java

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22

33
import com.laytonsmith.abstraction.AbstractionObject;
44
import com.laytonsmith.abstraction.MCAnimalTamer;
5-
import com.laytonsmith.abstraction.MCHumanEntity;
6-
import com.laytonsmith.abstraction.MCOfflinePlayer;
7-
import com.laytonsmith.abstraction.bukkit.entities.BukkitMCHumanEntity;
8-
import org.bukkit.OfflinePlayer;
95
import org.bukkit.entity.AnimalTamer;
10-
import org.bukkit.entity.HumanEntity;
6+
7+
import java.util.UUID;
118

129
public class BukkitMCAnimalTamer implements MCAnimalTamer {
1310

@@ -35,28 +32,6 @@ public AnimalTamer _tamer() {
3532
return at;
3633
}
3734

38-
public MCOfflinePlayer getOfflinePlayer() {
39-
if(at instanceof OfflinePlayer) {
40-
return new BukkitMCOfflinePlayer((OfflinePlayer) at);
41-
}
42-
return null;
43-
}
44-
45-
public boolean isOfflinePlayer() {
46-
return at instanceof OfflinePlayer;
47-
}
48-
49-
public boolean isHumanEntity() {
50-
return at instanceof HumanEntity;
51-
}
52-
53-
public MCHumanEntity getHumanEntity() {
54-
if(at instanceof HumanEntity) {
55-
return new BukkitMCHumanEntity((HumanEntity) at);
56-
}
57-
return null;
58-
}
59-
6035
@Override
6136
public String toString() {
6237
return at.toString();
@@ -76,4 +51,9 @@ public int hashCode() {
7651
public String getName() {
7752
return at.getName();
7853
}
54+
55+
@Override
56+
public UUID getUniqueID() {
57+
return at.getUniqueId();
58+
}
7959
}

src/main/java/com/laytonsmith/abstraction/bukkit/entities/BukkitMCHumanEntity.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import org.bukkit.inventory.Inventory;
2121
import org.bukkit.inventory.Merchant;
2222

23+
import java.util.UUID;
24+
2325
public class BukkitMCHumanEntity extends BukkitMCLivingEntity implements MCHumanEntity {
2426

2527
HumanEntity he;
@@ -38,6 +40,11 @@ public String getName() {
3840
return he.getName();
3941
}
4042

43+
@Override
44+
public UUID getUniqueID() {
45+
return he.getUniqueId();
46+
}
47+
4148
@Override
4249
public void closeInventory() {
4350
he.closeInventory();

src/main/java/com/laytonsmith/core/functions/MobManagement.java

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public static String docs() {
6767
}
6868

6969
@api(environments = {CommandHelperEnvironment.class})
70+
@seealso({get_mob_owner_uuid.class, set_mob_owner.class})
7071
public static class get_mob_owner extends AbstractFunction {
7172

7273
@Override
@@ -82,13 +83,14 @@ public Integer[] numArgs() {
8283
@Override
8384
public String docs() {
8485
return "string {entityUUID} Returns the owner's name, or null if the mob is unowned."
85-
+ "An UntameableMobException is thrown if mob isn't tameable to begin with.";
86+
+ " Use {{function|get_mob_owner_uuid}} to get the owner's unique id."
87+
+ " An UntameableMobException is thrown if mob isn't tameable to begin with.";
8688
}
8789

8890
@Override
8991
public Class<? extends CREThrowable>[] thrown() {
9092
return new Class[]{CREUntameableMobException.class, CRELengthException.class,
91-
CREBadEntityException.class, CREIllegalArgumentException.class};
93+
CREBadEntityException.class, CREFormatException.class};
9294
}
9395

9496
@Override
@@ -122,6 +124,63 @@ public Mixed exec(Target t, Environment environment, Mixed... args) throws Confi
122124
}
123125

124126
@api(environments = {CommandHelperEnvironment.class})
127+
@seealso({get_mob_owner.class, set_mob_owner.class})
128+
public static class get_mob_owner_uuid extends AbstractFunction {
129+
130+
@Override
131+
public String getName() {
132+
return "get_mob_owner_uuid";
133+
}
134+
135+
@Override
136+
public Integer[] numArgs() {
137+
return new Integer[]{1};
138+
}
139+
140+
@Override
141+
public String docs() {
142+
return "string {entityUUID} Returns the owner's UUID, or null if the mob is unowned."
143+
+ "An UntameableMobException is thrown if mob isn't tameable to begin with.";
144+
}
145+
146+
@Override
147+
public Class<? extends CREThrowable>[] thrown() {
148+
return new Class[]{CREUntameableMobException.class, CRELengthException.class,
149+
CREBadEntityException.class, CREFormatException.class};
150+
}
151+
152+
@Override
153+
public boolean isRestricted() {
154+
return true;
155+
}
156+
157+
@Override
158+
public MSVersion since() {
159+
return MSVersion.V3_3_5;
160+
}
161+
162+
@Override
163+
public Boolean runAsync() {
164+
return false;
165+
}
166+
167+
@Override
168+
public Mixed exec(Target t, Environment environment, Mixed... args) throws ConfigRuntimeException {
169+
MCLivingEntity mob = Static.getLivingEntity(args[0], t);
170+
if(!mob.isTameable()) {
171+
throw new CREUntameableMobException("The specified entity is not tameable", t);
172+
}
173+
MCAnimalTamer owner = ((MCTameable) mob).getOwner();
174+
if(owner == null) {
175+
return CNull.NULL;
176+
} else {
177+
return new CString(owner.getUniqueID().toString(), t);
178+
}
179+
}
180+
}
181+
182+
@api(environments = {CommandHelperEnvironment.class})
183+
@seealso({get_mob_owner.class, get_mob_owner_uuid.class})
125184
public static class set_mob_owner extends AbstractFunction {
126185

127186
@Override
@@ -136,15 +195,15 @@ public Integer[] numArgs() {
136195

137196
@Override
138197
public String docs() {
139-
return "void {entityUUID, player} Sets the tameable mob to the specified player. Offline players are"
140-
+ " supported, but this means that partial matches are NOT supported. You must type the player's"
141-
+ " name exactly. Setting the player to null will untame the mob.";
198+
return "void {entityUUID, player} Sets the tameable mob to the specified player."
199+
+ " The player argument supports offline players, so it should be a UUID or an exact player name."
200+
+ " Setting the player to null will untame the mob.";
142201
}
143202

144203
@Override
145204
public Class<? extends CREThrowable>[] thrown() {
146205
return new Class[]{CREUntameableMobException.class, CRELengthException.class,
147-
CREBadEntityException.class, CREIllegalArgumentException.class};
206+
CREBadEntityException.class, CREFormatException.class};
148207
}
149208

150209
@Override
@@ -173,7 +232,7 @@ public Mixed exec(Target t, Environment environment, Mixed... args) throws Confi
173232
if(player instanceof CNull) {
174233
mct.setOwner(null);
175234
} else {
176-
mct.setOwner(Static.getServer().getOfflinePlayer(player.val()));
235+
mct.setOwner(Static.GetUser(player, t));
177236
}
178237
return CVoid.VOID;
179238
}

0 commit comments

Comments
 (0)