/
AdvancedEntitySpawner.java
210 lines (176 loc) · 7.53 KB
/
AdvancedEntitySpawner.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package com.sk89q.craftbook.gates.world;
import net.minecraft.server.Enchantment;
import net.minecraft.server.EntityLiving;
import net.minecraft.server.Item;
import net.minecraft.server.ItemStack;
import org.bukkit.Server;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.bukkit.craftbukkit.entity.CraftLivingEntity;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import com.sk89q.craftbook.ic.AbstractICFactory;
import com.sk89q.craftbook.ic.ChipState;
import com.sk89q.craftbook.ic.IC;
import com.sk89q.craftbook.ic.ICFactory;
import com.sk89q.craftbook.ic.RestrictedIC;
import com.sk89q.craftbook.util.SignUtil;
import com.sk89q.worldedit.Location;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.blocks.BlockID;
import com.sk89q.worldedit.bukkit.BukkitUtil;
public class AdvancedEntitySpawner extends CreatureSpawner {
public AdvancedEntitySpawner(Server server, Sign sign, ICFactory factory) {
super(server, sign, factory);
load();
}
Location location;
EntityType type = EntityType.PIG;
int amount = 1;
@Override
public String getTitle() {
return "Advanced Entity Spawner";
}
@Override
public String getSignTitle() {
return "ADV ENT SPAWNER";
}
public void load() {
type = EntityType.fromName(getSign().getLine(3).trim().split("\\*")[0]);
try {
amount = Integer.parseInt(getSign().getLine(3).trim().split("\\*")[1]);
}
catch(Exception e) {
amount = 1;
}
try {
double x, y, z;
x = Double.parseDouble(getSign().getLine(2).split(":")[0]);
y = Double.parseDouble(getSign().getLine(2).split(":")[1]);
z = Double.parseDouble(getSign().getLine(2).split(":")[2]);
x += getSign().getX();
y += getSign().getY();
z += getSign().getZ();
location = new Location(BukkitUtil.getLocalWorld(getSign().getWorld()), new Vector(x,y,z));
}
catch(Exception e){
location = new Location(BukkitUtil.getLocalWorld(getSign().getWorld()),
new Vector(getSign().getX(),getSign().getY(),getSign().getZ()));
}
}
@Override
public void trigger(ChipState chip) {
if(!chip.getInput(0))
return;
Block left = SignUtil.getLeftBlock(getSign().getBlock());
Sign effectSign = null;
if(left.getTypeId() == BlockID.WALL_SIGN) {
effectSign = (Sign) left.getState();
}
Block right = SignUtil.getRightBlock(getSign().getBlock());
Sign armourSign = null;
if(right.getTypeId() == BlockID.WALL_SIGN) {
armourSign = (Sign) right.getState();
}
for(int i = 0; i < amount; i++) {
Entity ent = getSign().getWorld().spawnEntity(BukkitUtil.toLocation(location), type);
if(armourSign != null) { //Apply armor
if(ent instanceof LivingEntity) {
CraftLivingEntity cle = (CraftLivingEntity) ent;
EntityLiving eliv = cle.getHandle();
for(int s = 0; s < 4; s++) {
try {
String bit = armourSign.getLine(s);
if(bit == null || bit.trim().length() == 0)
continue;
byte data = 0;
try {
data = Byte.parseByte(bit.split(";")[0].split(":")[1]);
}
catch(Exception e){}
ItemStack slot = new ItemStack(Item.byId[Integer.parseInt(bit.split(";")[0].split(":")[0])], 1, data);
try {
for(int e = 1; e < bit.split(";").length; e++) {
slot.addEnchantment(Enchantment.byId[Integer.parseInt(bit.split(";")[e].split(":")[0])],
Integer.parseInt(bit.split(";")[e].split(":")[1]));
}
}
catch(Exception e){}
eliv.setEquipment(s + 1, slot);
}
catch(Exception e){}
}
}
}
if(effectSign != null) { //Apply effects
for(int s = 0; s < 4; s++) {
try {
String bit = armourSign.getLine(s);
if(bit == null || bit.trim().length() == 0)
continue;
String[] data = bit.split(":");
if(data[0].equalsIgnoreCase("e"))
setEntityData(ent, data[1]);
else if(data[0].equalsIgnoreCase("p")) {
//TODO potion effects
}
else if(data[0].equalsIgnoreCase("v")) {
try {
double x, y, z;
x = Double.parseDouble(getSign().getLine(2).split(":")[0]);
y = Double.parseDouble(getSign().getLine(2).split(":")[1]);
z = Double.parseDouble(getSign().getLine(2).split(":")[2]);
ent.setVelocity(new org.bukkit.util.Vector(x,y,z));
}
catch(Exception e){
}
}
else if(data[0].equalsIgnoreCase("s")) {
if(!(ent instanceof LivingEntity))
continue;
CraftLivingEntity cle = (CraftLivingEntity) ent;
EntityLiving eliv = cle.getHandle();
byte d = 0;
try {
d = Byte.parseByte(bit.split(";")[0].split(":")[2]);
}
catch(Exception e){}
ItemStack slot = new ItemStack(Item.byId[Integer.parseInt(bit.split(";")[0].split(":")[1])], 1, d);
try {
for(int e = 1; e < bit.split(";").length; e++) {
slot.addEnchantment(Enchantment.byId[Integer.parseInt(bit.split(";")[e].split(":")[1])],
Integer.parseInt(bit.split(";")[e].split(":")[2]));
}
}
catch(Exception e){}
eliv.setEquipment(0, slot);
}
}
catch(Exception e){}
}
}
}
}
public static class Factory extends AbstractICFactory implements RestrictedIC {
public Factory(Server server) {
super(server);
}
@Override
public IC create(Sign sign) {
return new AdvancedEntitySpawner(getServer(), sign, this);
}
@Override
public String getDescription() {
return "Spawns a mob with many customizations.";
}
@Override
public String[] getLineHelp() {
String[] lines = new String[] {
"x:y:z",
"entitytype*amount"
};
return lines;
}
}
}