Skip to content

Commit 2f1c17d

Browse files
committed
Improved old code for spawn entity spawning
1 parent 88f310c commit 2f1c17d

File tree

3 files changed

+216
-185
lines changed

3 files changed

+216
-185
lines changed

src/main/java/dev/lars/utilsmanager/UtilsManager.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.comphenix.protocol.ProtocolLibrary;
44
import com.comphenix.protocol.ProtocolManager;
5-
import dev.lars.utilsmanager.entity.EntitySummons;
65
import dev.lars.utilsmanager.entity.HeartDisplayManager;
76
import dev.lars.utilsmanager.features.backpack.BackpackManager;
87
import dev.lars.utilsmanager.features.court.CourtManager;
@@ -35,7 +34,6 @@ public final class UtilsManager extends JavaPlugin {
3534
private BackpackManager backpackManager;
3635
private TablistManager tablistManager;
3736
private RankManager rankManager;
38-
private EntitySummons entitySummons;
3937
private HeartDisplayManager heartDisplayManager;
4038
private QuestManager questManager;
4139
private BanManager banManager;
@@ -78,7 +76,6 @@ private void initializeManagers() {
7876
new Timer();
7977
new RealTime();
8078
new BedListener();
81-
entitySummons = new EntitySummons();
8279
heartDisplayManager = new HeartDisplayManager();
8380
courtManager = new CourtManager();
8481
banManager = new BanManager();
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
package dev.lars.utilsmanager.entity;
2+
3+
import dev.lars.utilsmanager.utils.Statements;
4+
import net.kyori.adventure.text.Component;
5+
import net.kyori.adventure.text.format.NamedTextColor;
6+
import org.bukkit.*;
7+
import org.bukkit.entity.*;
8+
import org.bukkit.inventory.ItemStack;
9+
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
import java.util.function.Consumer;
13+
14+
public class EntitySpawner {
15+
16+
private final World world;
17+
private final List<Entity> spawned = new ArrayList<>();
18+
private final ItemStack netheriteSword = new ItemStack(Material.NETHERITE_SWORD);
19+
20+
public EntitySpawner() {
21+
world = Bukkit.getWorld("world");
22+
}
23+
24+
public void spawnAll() {
25+
try {
26+
world.loadChunk(12, 7);
27+
world.loadChunk(12, 8);
28+
world.loadChunk(4, 2);
29+
world.loadChunk(4, 4);
30+
world.loadChunk(25, 1);
31+
world.loadChunk(26, 31);
32+
33+
Location ls1sheep = loc(5, 134, 22);
34+
Location ls1pig = loc(8, 134, 25);
35+
Location ls2strider = loc(-1, 135, 19);
36+
Location ls2wither = loc(-3.5, 134, 15.5);
37+
Location ls3creeper = loc(-9.5, 134, 22.5);
38+
Location ls3spider = loc(-12.5, 134, 26.5);
39+
Location ls3minecart = loc(-9, 135, 26);
40+
Location ls4enderman = loc(-4, 134, 35);
41+
Location ls4endcrystal = loc(-1.5, 136, 32.5);
42+
Location ls4endmilb = loc(-4, 135, 30);
43+
Location ls4endmilb2 = loc(0, 135, 30);
44+
45+
Location leenderman1 = loc(207.5, 73, 139.5);
46+
Location leenderman2 = loc(207.5, 73, 132.5);
47+
Location leenderman3 = loc(207.5, 73, 120.5);
48+
Location leenderman4 = loc(207.5, 73, 113.5);
49+
50+
Location lwchicken = loc(78.5, 64, 42.5);
51+
Location lwchicken2 = loc(69.5, 65, 76.5);
52+
Location lwchicken3 = loc(-86.5, 65, -7.5);
53+
Location lwboat1 = loc(110, 63, 52);
54+
Location lwboat2 = loc(76.5, 63, 42.5);
55+
Location lwboat3 = loc(79, 63, 42.5);
56+
Location lwboat4 = loc(-108, 63, 21.5);
57+
Location lwboat5 = loc(-106, 63, 21.5);
58+
59+
Location lnfisher = loc(-132.5, 74, -22);
60+
61+
Sheep s1sheep = spawn(ls1sheep, EntityType.SHEEP, e -> {
62+
((Sheep) e).customName(Component.text("Larsi"));
63+
((Sheep) e).setCustomNameVisible(false);
64+
((Sheep) e).setColor(DyeColor.WHITE);
65+
((Sheep) e).setSilent(true);
66+
});
67+
68+
Pig s1spig = spawn(ls1pig, EntityType.PIG, e -> {
69+
((Pig) e).customName(Component.text("Larsi"));
70+
((Pig) e).setCustomNameVisible(false);
71+
((Pig) e).setSilent(true);
72+
});
73+
74+
Strider s2strider = spawn(ls2strider, EntityType.STRIDER, e -> {
75+
((Strider) e).customName(Component.text("Larsi"));
76+
((Strider) e).setCustomNameVisible(false);
77+
((Strider) e).setSilent(true);
78+
});
79+
80+
WitherSkeleton s2wither = spawn(ls2wither, EntityType.WITHER_SKELETON, e -> {
81+
((WitherSkeleton) e).customName(Component.text("Larsi"));
82+
((WitherSkeleton) e).setCustomNameVisible(false);
83+
((WitherSkeleton) e).getEquipment().setItemInMainHand(netheriteSword);
84+
((WitherSkeleton) e).getEquipment().setItemInOffHand(netheriteSword.clone());
85+
((WitherSkeleton) e).setSilent(true);
86+
});
87+
88+
Creeper s3creeper = spawn(ls3creeper, EntityType.CREEPER, e -> {
89+
((Creeper) e).customName(Component.text("Larsi"));
90+
((Creeper) e).setCustomNameVisible(false);
91+
});
92+
93+
CaveSpider s3spider = spawn(ls3spider, EntityType.CAVE_SPIDER, e -> {
94+
((CaveSpider) e).customName(Component.text("Larsi"));
95+
((CaveSpider) e).setCustomNameVisible(false);
96+
((CaveSpider) e).setSilent(true);
97+
});
98+
99+
Minecart s3minecart = spawn(ls3minecart, EntityType.MINECART, e -> {
100+
((Minecart) e).customName(Component.text("Minecart"));
101+
((Minecart) e).setCustomNameVisible(false);
102+
});
103+
104+
Enderman s4enderman = spawn(ls4enderman, EntityType.ENDERMAN, e -> {
105+
((Enderman) e).customName(Component.text("Aelxibexi"));
106+
((Enderman) e).setCustomNameVisible(false);
107+
((Enderman) e).setAI(false);
108+
((Enderman) e).setSilent(true);
109+
((Enderman) e).setRotation(-135f, 0f);
110+
});
111+
112+
EnderCrystal s4endcrystal = spawn(ls4endcrystal, EntityType.END_CRYSTAL, e -> {
113+
((EnderCrystal) e).setShowingBottom(false);
114+
});
115+
116+
Endermite s4endmite = spawn(ls4endmilb, EntityType.ENDERMITE, e -> {
117+
((Endermite) e).customName(Component.text("Alexi"));
118+
((Endermite) e).setCustomNameVisible(false);
119+
((Endermite) e).setSilent(true);
120+
});
121+
122+
Endermite s4endmite2 = spawn(ls4endmilb2, EntityType.ENDERMITE, e -> {
123+
((Endermite) e).customName(Component.text("Alexi"));
124+
((Endermite) e).setCustomNameVisible(false);
125+
((Endermite) e).setSilent(true);
126+
});
127+
128+
Enderman eenderman1 = spawn(leenderman1, EntityType.ENDERMAN, e -> {
129+
((Enderman) e).customName(Component.text("Alexibexi"));
130+
((Enderman) e).setCustomNameVisible(false);
131+
((Enderman) e).setAI(false);
132+
((Enderman) e).setRotation(90f, 0f);
133+
});
134+
Enderman eenderman2 = spawn(leenderman2, EntityType.ENDERMAN, e -> {
135+
((Enderman) e).customName(Component.text("Alexibexi"));
136+
((Enderman) e).setCustomNameVisible(false);
137+
((Enderman) e).setAI(false);
138+
((Enderman) e).setRotation(90f, 0f);
139+
});
140+
Enderman eenderman3 = spawn(leenderman3, EntityType.ENDERMAN, e -> {
141+
((Enderman) e).customName(Component.text("Alexibexi"));
142+
((Enderman) e).setCustomNameVisible(false);
143+
((Enderman) e).setAI(false);
144+
((Enderman) e).setRotation(90f, 0f);
145+
});
146+
Enderman eenderman4 = spawn(leenderman4, EntityType.ENDERMAN, e -> {
147+
((Enderman) e).customName(Component.text("Alexibexi"));
148+
((Enderman) e).setCustomNameVisible(false);
149+
((Enderman) e).setAI(false);
150+
((Enderman) e).setRotation(90f, 0f);
151+
});
152+
153+
Chicken wchicken1 = spawn(lwchicken, EntityType.CHICKEN, e -> {
154+
((Chicken) e).customName(Component.text("Kjelli"));
155+
((Chicken) e).setCustomNameVisible(false);
156+
});
157+
Chicken wchicken2 = spawn(lwchicken2, EntityType.CHICKEN, e -> {
158+
((Chicken) e).customName(Component.text("Kjelli"));
159+
((Chicken) e).setCustomNameVisible(false);
160+
});
161+
Chicken wchicken3 = spawn(lwchicken3, EntityType.CHICKEN, e -> {
162+
((Chicken) e).customName(Component.text("Kjelli"));
163+
((Chicken) e).setCustomNameVisible(false);
164+
});
165+
166+
Boat wboat1 = spawn(lwboat1, EntityType.OAK_BOAT, e -> {
167+
((Boat) e).setRotation(0f, 0f);
168+
});
169+
Boat wboat2 = spawn(lwboat2, EntityType.OAK_BOAT, e -> {
170+
((Boat) e).setRotation(90f, 0f);
171+
});
172+
Boat wboat3 = spawn(lwboat3, EntityType.OAK_BOAT, e -> {
173+
((Boat) e).setRotation(90f, 0f);
174+
((Boat) e).addPassenger(wchicken1);
175+
});
176+
Boat wboat4 = spawn(lwboat4, EntityType.OAK_BOAT, e -> {
177+
((Boat) e).setRotation(90f, 0f);
178+
});
179+
Boat wboat5 = spawn(lwboat5, EntityType.OAK_BOAT, e -> {
180+
((Boat) e).setRotation(90f, 0f);
181+
});
182+
183+
Bukkit.getConsoleSender().sendMessage(Statements.getPrefix().append(Component.text("Summoned all entities", NamedTextColor.BLUE)));
184+
185+
} catch (Exception ex) {
186+
Bukkit.getConsoleSender().sendMessage(Component.text("Failed to spawn entities: " + ex.getMessage(), NamedTextColor.RED));
187+
ex.printStackTrace();
188+
}
189+
}
190+
191+
public void despawnAll() {
192+
for (Entity e : new ArrayList<>(spawned)) {
193+
if (e != null && !e.isDead()) {
194+
e.remove();
195+
}
196+
}
197+
spawned.clear();
198+
}
199+
200+
private Location loc(double x, double y, double z) {
201+
return new Location(world, x, y, z);
202+
}
203+
204+
@SuppressWarnings("unchecked")
205+
private <T extends Entity> T spawn(Location location, EntityType type, Consumer<T> configurator) {
206+
Entity e = world.spawnEntity(location, type);
207+
try {
208+
configurator.accept((T) e);
209+
} catch (ClassCastException cce) {
210+
Bukkit.getConsoleSender().sendMessage(Component.text("Entity type mismatch while configuring: " + type, NamedTextColor.RED));
211+
cce.printStackTrace();
212+
}
213+
spawned.add(e);
214+
return (T) e;
215+
}
216+
}

0 commit comments

Comments
 (0)