Skip to content
This repository was archived by the owner on May 14, 2024. It is now read-only.

Commit df8b799

Browse files
author
Shynixn
committed
#97 Implemented process entity in 1.18.2 - 1.16.5.
1 parent e11d053 commit df8b799

File tree

4 files changed

+305
-2
lines changed

4 files changed

+305
-2
lines changed

structureblocklib-bukkit-core/bukkit-nms-116R3/src/main/java/com/github/shynixn/structureblocklib/bukkit/v1_16_R3/StructureWorldServiceImpl.java

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.shynixn.structureblocklib.bukkit.v1_16_R3;
22

3+
import com.github.shynixn.structureblocklib.api.entity.StructureEntity;
34
import com.github.shynixn.structureblocklib.api.entity.StructurePlaceMeta;
45
import com.github.shynixn.structureblocklib.api.entity.StructurePlacePart;
56
import com.github.shynixn.structureblocklib.api.entity.StructureReadMeta;
@@ -13,10 +14,16 @@
1314
import org.bukkit.craftbukkit.v1_16_R3.CraftWorld;
1415
import org.bukkit.craftbukkit.v1_16_R3.block.CraftBlock;
1516
import org.bukkit.craftbukkit.v1_16_R3.block.data.CraftBlockData;
17+
import org.bukkit.entity.Entity;
18+
import org.bukkit.event.entity.CreatureSpawnEvent;
1619
import org.jetbrains.annotations.NotNull;
1720
import org.jetbrains.annotations.Nullable;
1821

22+
import java.lang.reflect.Field;
23+
import java.util.List;
24+
import java.util.Optional;
1925
import java.util.Random;
26+
import java.util.UUID;
2027
import java.util.function.Function;
2128

2229
/**
@@ -47,8 +54,8 @@ public void placeStructureToWorld(StructurePlaceMeta meta, Object structure) thr
4754
}
4855

4956
DefinedStructure definedStructure = (DefinedStructure) structure;
50-
org.bukkit.World bukkitWorld = Bukkit.getWorld(meta.getLocation().getWorldName());
51-
World world = ((CraftWorld)bukkitWorld).getHandle();
57+
org.bukkit.World bukkitWorld = Bukkit.getWorld(meta.getLocation().getWorldName());
58+
World world = ((CraftWorld) bukkitWorld).getHandle();
5259
BlockPosition cornerBlock = new BlockPosition((int) meta.getLocation().getX(), (int) meta.getLocation().getY(), (int) meta.getLocation().getZ());
5360
DefinedStructureInfo info = new DefinedStructureInfo();
5461
info.a(!meta.isIncludeEntitiesEnabled());
@@ -70,6 +77,7 @@ public void placeStructureToWorld(StructurePlaceMeta meta, Object structure) thr
7077
}
7178

7279
executeProcessors(bukkitWorld, meta, info);
80+
executeEntityProcessor(meta, bukkitWorld, definedStructure);
7381
definedStructure.a((WorldAccess) world, cornerBlock, info, new Random());
7482
}
7583

@@ -147,6 +155,10 @@ public org.bukkit.block.Block getTargetBlock() {
147155
}
148156
}
149157

158+
if (!meta.isIncludeBlockEnabled()) {
159+
return null;
160+
}
161+
150162
return new DefinedStructure.BlockInfo(blockInfo1.a, targetBlockState.item, blockInfo1.c);
151163
}
152164

@@ -156,4 +168,71 @@ protected DefinedStructureStructureProcessorType<?> a() {
156168
}
157169
});
158170
}
171+
172+
/**
173+
* Executes the entity processors.
174+
*/
175+
private void executeEntityProcessor(StructurePlaceMeta meta, org.bukkit.World bukkitWorld, DefinedStructure definedStructure) {
176+
List<DefinedStructure.EntityInfo> structureEntityInfos;
177+
try {
178+
Field field = DefinedStructure.class.getDeclaredField("b");
179+
field.setAccessible(true);
180+
structureEntityInfos = (List<DefinedStructure.EntityInfo>) field.get(definedStructure);
181+
} catch (NoSuchFieldException | IllegalAccessException e) {
182+
throw new RuntimeException(e);
183+
}
184+
185+
for (DefinedStructure.EntityInfo entityInfo : structureEntityInfos) {
186+
final GenericWrapper<org.bukkit.entity.Entity> peekEntity = new GenericWrapper<>(null);
187+
StructureEntity<org.bukkit.entity.Entity, Location> structureEntity = new StructureEntity<org.bukkit.entity.Entity, Location>() {
188+
@Override
189+
public Optional<org.bukkit.entity.Entity> spawnEntity(Location location) {
190+
Optional<net.minecraft.server.v1_16_R3.Entity> optEntity = EntityTypes.a(entityInfo.c, ((CraftWorld) location.getWorld()).getHandle());
191+
if (optEntity.isPresent()) {
192+
optEntity.get().a_(UUID.randomUUID());
193+
((CraftWorld) location.getWorld()).addEntity(optEntity.get(), CreatureSpawnEvent.SpawnReason.CUSTOM);
194+
optEntity.get().getBukkitEntity().teleport(location);
195+
return Optional.of(optEntity.get().getBukkitEntity());
196+
}
197+
return Optional.empty();
198+
}
199+
200+
@Override
201+
public Optional<Entity> getEntity() {
202+
if (peekEntity.item == null) {
203+
Optional<net.minecraft.server.v1_16_R3.Entity> optEntity = EntityTypes.a(entityInfo.c, ((CraftWorld) bukkitWorld).getHandle());
204+
if (optEntity.isPresent()) {
205+
peekEntity.item = optEntity.get().getBukkitEntity();
206+
}
207+
}
208+
209+
return Optional.ofNullable(peekEntity.item);
210+
}
211+
212+
@Override
213+
public Location getSourceLocation() {
214+
BlockPosition sourcePos = entityInfo.b;
215+
return new Location(null, sourcePos.getX(), sourcePos.getY(), sourcePos.getZ());
216+
}
217+
218+
@Override
219+
public String getNbtData() {
220+
return entityInfo.c.toString();
221+
}
222+
};
223+
224+
for (Function<?, Boolean> processor : meta.getEntityProcessors()) {
225+
Function<Object, Boolean> processHandle = (Function<Object, Boolean>) processor;
226+
boolean result = processHandle.apply(structureEntity);
227+
228+
if (!result) {
229+
structureEntityInfos.remove(entityInfo);
230+
}
231+
}
232+
233+
if (peekEntity.item != null) {
234+
peekEntity.item.remove();
235+
}
236+
}
237+
}
159238
}

structureblocklib-bukkit-core/bukkit-nms-117R1/src/main/java/com/github/shynixn/structureblocklib/bukkit/v1_17_R1/StructureWorldServiceImpl.java

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.shynixn.structureblocklib.bukkit.v1_17_R1;
22

3+
import com.github.shynixn.structureblocklib.api.entity.StructureEntity;
34
import com.github.shynixn.structureblocklib.api.entity.StructurePlaceMeta;
45
import com.github.shynixn.structureblocklib.api.entity.StructurePlacePart;
56
import com.github.shynixn.structureblocklib.api.entity.StructureReadMeta;
@@ -10,6 +11,7 @@
1011
import net.minecraft.core.Vec3i;
1112
import net.minecraft.server.level.ServerLevel;
1213
import net.minecraft.util.Mth;
14+
import net.minecraft.world.entity.EntityType;
1315
import net.minecraft.world.level.LevelReader;
1416
import net.minecraft.world.level.block.Block;
1517
import net.minecraft.world.level.block.Blocks;
@@ -24,10 +26,16 @@
2426
import org.bukkit.craftbukkit.v1_17_R1.CraftWorld;
2527
import org.bukkit.craftbukkit.v1_17_R1.block.CraftBlock;
2628
import org.bukkit.craftbukkit.v1_17_R1.block.data.CraftBlockData;
29+
import org.bukkit.entity.Entity;
30+
import org.bukkit.event.entity.CreatureSpawnEvent;
2731
import org.jetbrains.annotations.NotNull;
2832
import org.jetbrains.annotations.Nullable;
2933

34+
import java.lang.reflect.Field;
35+
import java.util.List;
36+
import java.util.Optional;
3037
import java.util.Random;
38+
import java.util.UUID;
3139
import java.util.function.Function;
3240

3341
/**
@@ -81,6 +89,7 @@ public void placeStructureToWorld(StructurePlaceMeta meta, Object structure) thr
8189
}
8290

8391
executeProcessors(bukkitWorld, meta, info);
92+
executeEntityProcessor(meta, bukkitWorld, template);
8493
template.placeInWorld(world, cornerBlock, cornerBlock, info, new Random(), 2);
8594
}
8695

@@ -160,6 +169,10 @@ public org.bukkit.block.Block getTargetBlock() {
160169
}
161170
}
162171

172+
if (!meta.isIncludeBlockEnabled()) {
173+
return null;
174+
}
175+
163176
return new StructureTemplate.StructureBlockInfo(structureBlockInfo1.pos, targetBlockState.item, structureBlockInfo1.nbt);
164177
}
165178

@@ -169,4 +182,72 @@ protected StructureProcessorType<?> getType() {
169182
}
170183
});
171184
}
185+
186+
/**
187+
* Executes the entity processors.
188+
*/
189+
private void executeEntityProcessor(StructurePlaceMeta meta, World bukkitWorld, StructureTemplate template) {
190+
List<StructureTemplate.StructureEntityInfo> structureEntityInfos;
191+
try {
192+
Field field = StructureTemplate.class.getDeclaredField("n");
193+
field.setAccessible(true);
194+
structureEntityInfos = (List<StructureTemplate.StructureEntityInfo>) field.get(template);
195+
} catch (NoSuchFieldException | IllegalAccessException e) {
196+
throw new RuntimeException(e);
197+
}
198+
199+
for (StructureTemplate.StructureEntityInfo entityInfo : structureEntityInfos.toArray(new StructureTemplate.StructureEntityInfo[0])) {
200+
final GenericWrapper<Entity> peekEntity = new GenericWrapper<>(null);
201+
StructureEntity<Entity, Location> structureEntity = new StructureEntity<Entity, Location>() {
202+
@Override
203+
public Optional<Entity> spawnEntity(Location location) {
204+
Optional<net.minecraft.world.entity.Entity> optEntity = EntityType.create(entityInfo.nbt, ((CraftWorld) location.getWorld()).getHandle());
205+
if (optEntity.isPresent()) {
206+
optEntity.get().setUUID(UUID.randomUUID());
207+
((CraftWorld) location.getWorld()).addEntity(optEntity.get(), CreatureSpawnEvent.SpawnReason.CUSTOM);
208+
optEntity.get().getBukkitEntity().teleport(location);
209+
return Optional.of(optEntity.get().getBukkitEntity());
210+
}
211+
return Optional.empty();
212+
}
213+
214+
@Override
215+
public Optional<Entity> getEntity() {
216+
if (peekEntity.item == null) {
217+
Optional<net.minecraft.world.entity.Entity> optEntity = EntityType.create(entityInfo.nbt, ((CraftWorld) bukkitWorld).getHandle());
218+
if (optEntity.isPresent()) {
219+
peekEntity.item = optEntity.get().getBukkitEntity();
220+
}
221+
}
222+
223+
return Optional.ofNullable(peekEntity.item);
224+
}
225+
226+
@Override
227+
public Location getSourceLocation() {
228+
// noinspection UnnecessaryLocalVariable Explicit cast is necessary because otherwise the spigot mappings do not work.
229+
Vec3i sourcePos = entityInfo.blockPos;
230+
return new Location(null, sourcePos.getX(), sourcePos.getY(), sourcePos.getZ());
231+
}
232+
233+
@Override
234+
public String getNbtData() {
235+
return entityInfo.nbt.toString();
236+
}
237+
};
238+
239+
for (Function<?, Boolean> processor : meta.getEntityProcessors()) {
240+
Function<Object, Boolean> processHandle = (Function<Object, Boolean>) processor;
241+
boolean result = processHandle.apply(structureEntity);
242+
243+
if (!result) {
244+
structureEntityInfos.remove(entityInfo);
245+
}
246+
}
247+
248+
if (peekEntity.item != null) {
249+
peekEntity.item.remove();
250+
}
251+
}
252+
}
172253
}

structureblocklib-bukkit-core/bukkit-nms-118R1/src/main/java/com/github/shynixn/structureblocklib/bukkit/v1_18_R1/StructureWorldServiceImpl.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.shynixn.structureblocklib.bukkit.v1_18_R1;
22

3+
import com.github.shynixn.structureblocklib.api.entity.StructureEntity;
34
import com.github.shynixn.structureblocklib.api.entity.StructurePlaceMeta;
45
import com.github.shynixn.structureblocklib.api.entity.StructurePlacePart;
56
import com.github.shynixn.structureblocklib.api.entity.StructureReadMeta;
@@ -10,6 +11,7 @@
1011
import net.minecraft.core.Vec3i;
1112
import net.minecraft.server.level.ServerLevel;
1213
import net.minecraft.util.Mth;
14+
import net.minecraft.world.entity.EntityType;
1315
import net.minecraft.world.level.LevelReader;
1416
import net.minecraft.world.level.block.Block;
1517
import net.minecraft.world.level.block.Blocks;
@@ -24,10 +26,15 @@
2426
import org.bukkit.craftbukkit.v1_18_R1.CraftWorld;
2527
import org.bukkit.craftbukkit.v1_18_R1.block.CraftBlock;
2628
import org.bukkit.craftbukkit.v1_18_R1.block.data.CraftBlockData;
29+
import org.bukkit.entity.Entity;
30+
import org.bukkit.event.entity.CreatureSpawnEvent;
2731
import org.jetbrains.annotations.NotNull;
2832
import org.jetbrains.annotations.Nullable;
2933

34+
import java.util.List;
35+
import java.util.Optional;
3036
import java.util.Random;
37+
import java.util.UUID;
3138
import java.util.function.Function;
3239

3340
/**
@@ -81,6 +88,7 @@ public void placeStructureToWorld(StructurePlaceMeta meta, Object structure) thr
8188
}
8289

8390
executeProcessors(bukkitWorld, meta, info);
91+
executeEntityProcessor(meta, bukkitWorld, template.entityInfoList);
8492
template.placeInWorld(world, cornerBlock, cornerBlock, info, new Random(), 2);
8593
}
8694

@@ -160,6 +168,10 @@ public org.bukkit.block.Block getTargetBlock() {
160168
}
161169
}
162170

171+
if (!meta.isIncludeBlockEnabled()) {
172+
return null;
173+
}
174+
163175
return new StructureTemplate.StructureBlockInfo(structureBlockInfo1.pos, targetBlockState.item, structureBlockInfo1.nbt);
164176
}
165177

@@ -169,4 +181,63 @@ protected StructureProcessorType<?> getType() {
169181
}
170182
});
171183
}
184+
185+
/**
186+
* Executes the entity processors.
187+
*/
188+
private void executeEntityProcessor(StructurePlaceMeta meta, World bukkitWorld, List<StructureTemplate.StructureEntityInfo> structureEntityInfos) {
189+
for (StructureTemplate.StructureEntityInfo entityInfo : structureEntityInfos.toArray(new StructureTemplate.StructureEntityInfo[0])) {
190+
final GenericWrapper<Entity> peekEntity = new GenericWrapper<>(null);
191+
StructureEntity<Entity, Location> structureEntity = new StructureEntity<Entity, Location>() {
192+
@Override
193+
public Optional<Entity> spawnEntity(Location location) {
194+
Optional<net.minecraft.world.entity.Entity> optEntity = EntityType.create(entityInfo.nbt, ((CraftWorld) location.getWorld()).getHandle());
195+
if (optEntity.isPresent()) {
196+
optEntity.get().setUUID(UUID.randomUUID());
197+
((CraftWorld) location.getWorld()).addEntity(optEntity.get(), CreatureSpawnEvent.SpawnReason.CUSTOM);
198+
optEntity.get().getBukkitEntity().teleport(location);
199+
return Optional.of(optEntity.get().getBukkitEntity());
200+
}
201+
return Optional.empty();
202+
}
203+
204+
@Override
205+
public Optional<Entity> getEntity() {
206+
if (peekEntity.item == null) {
207+
Optional<net.minecraft.world.entity.Entity> optEntity = EntityType.create(entityInfo.nbt, ((CraftWorld) bukkitWorld).getHandle());
208+
if (optEntity.isPresent()) {
209+
peekEntity.item = optEntity.get().getBukkitEntity();
210+
}
211+
}
212+
213+
return Optional.ofNullable(peekEntity.item);
214+
}
215+
216+
@Override
217+
public Location getSourceLocation() {
218+
// noinspection UnnecessaryLocalVariable Explicit cast is necessary because otherwise the spigot mappings do not work.
219+
Vec3i sourcePos = entityInfo.blockPos;
220+
return new Location(null, sourcePos.getX(), sourcePos.getY(), sourcePos.getZ());
221+
}
222+
223+
@Override
224+
public String getNbtData() {
225+
return entityInfo.nbt.toString();
226+
}
227+
};
228+
229+
for (Function<?, Boolean> processor : meta.getEntityProcessors()) {
230+
Function<Object, Boolean> processHandle = (Function<Object, Boolean>) processor;
231+
boolean result = processHandle.apply(structureEntity);
232+
233+
if (!result) {
234+
structureEntityInfos.remove(entityInfo);
235+
}
236+
}
237+
238+
if (peekEntity.item != null) {
239+
peekEntity.item.remove();
240+
}
241+
}
242+
}
172243
}

0 commit comments

Comments
 (0)