Skip to content

Commit 465983c

Browse files
Added place and break custom block commands
1 parent 5005dc7 commit 465983c

9 files changed

Lines changed: 226 additions & 51 deletions

File tree

src/main/java/me/general_breddok/blockdisplaycreator/command/capi/BlockDisplayCreatorCAPICommand.java

Lines changed: 160 additions & 5 deletions
Large diffs are not rendered by default.

src/main/java/me/general_breddok/blockdisplaycreator/custom/block/BDCCustomBlock.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ public void setLocation(@NotNull Location location, CustomBlockOption... options
248248

249249
boolean loadChunk = false;
250250
boolean replaceCustomBlock = false;
251-
boolean replaceIndestructible = false;
251+
boolean breakSolidMaterial = false;
252252

253253
for (CustomBlockOption option : options) {
254254
if (option == null) {
@@ -257,8 +257,8 @@ public void setLocation(@NotNull Location location, CustomBlockOption... options
257257

258258
if (option == CustomBlockPlaceOption.REPLACE_CUSTOM_BLOCK) {
259259
replaceCustomBlock = true;
260-
} else if (option == CustomBlockPlaceOption.BREAK_INDESTRUCTIBLE_MATERIAL) {
261-
replaceIndestructible = true;
260+
} else if (option == CustomBlockPlaceOption.BREAK_SOLID_MATERIAL) {
261+
breakSolidMaterial = true;
262262
} else if (option == CustomBlockPlaceOption.LOAD_CHUNK) {
263263
loadChunk = true;
264264
}
@@ -272,8 +272,8 @@ public void setLocation(@NotNull Location location, CustomBlockOption... options
272272
}
273273
}
274274

275-
if (!WorldSelection.isDestructible(location.getBlock())) {
276-
if (replaceIndestructible) {
275+
if (!WorldSelection.isEphemeral(location.getBlock())) {
276+
if (breakSolidMaterial) {
277277
location.getBlock().setType(centralMaterial);
278278
} else {
279279
return;

src/main/java/me/general_breddok/blockdisplaycreator/custom/block/BDCCustomBlockService.java

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.bukkit.Bukkit;
2626
import org.bukkit.Location;
2727
import org.bukkit.Material;
28+
import org.bukkit.World;
2829
import org.bukkit.block.Block;
2930
import org.bukkit.entity.*;
3031
import org.bukkit.inventory.ItemStack;
@@ -209,13 +210,18 @@ public CustomBlock getCustomBlock(@NotNull Shulker collision, Object... data) {
209210
* {@inheritDoc}
210211
*/
211212
@Override
212-
public CustomBlock placeBlock(@NotNull AbstractCustomBlock abstractCustomBlock, @NotNull Location location, @NotNull CustomBlockRotation rotation, @Nullable Player player, CustomBlockOption... options) {
213+
public CustomBlock placeBlock(@NotNull AbstractCustomBlock abstractCustomBlock, @NotNull Location location, @NotNull CustomBlockRotation rotation, @Nullable Player player, CustomBlockOption... options) throws IllegalArgumentException {
213214
Material centralMaterial = abstractCustomBlock.getCentralMaterial();
214-
Block block = location.getBlock();
215-
Material originalType = block.getType();
215+
World world = location.getWorld();
216+
int chunkX = location.getBlockX() >> 4;
217+
int chunkZ = location.getBlockZ() >> 4;
218+
219+
if (world == null) {
220+
throw new IllegalArgumentException("Location's world cannot be null");
221+
}
216222

217223
boolean replaceCustomBlock = false;
218-
boolean replaceIndestructible = false;
224+
boolean breakSolidMaterial = false;
219225
boolean silentPlace = false;
220226
boolean loadChunk = false;
221227

@@ -227,28 +233,31 @@ public CustomBlock placeBlock(@NotNull AbstractCustomBlock abstractCustomBlock,
227233

228234
if (option == CustomBlockPlaceOption.REPLACE_CUSTOM_BLOCK) {
229235
replaceCustomBlock = true;
230-
} else if (option == CustomBlockPlaceOption.BREAK_INDESTRUCTIBLE_MATERIAL) {
231-
replaceIndestructible = true;
236+
} else if (option == CustomBlockPlaceOption.BREAK_SOLID_MATERIAL) {
237+
breakSolidMaterial = true;
232238
} else if (option == CustomBlockPlaceOption.SILENT_PLACE) {
233239
silentPlace = true;
234240
} else if (option == CustomBlockPlaceOption.LOAD_CHUNK) {
235241
loadChunk = true;
236242
}
237243
}
238244

239-
if (!location.getChunk().isLoaded()) {
245+
if (!world.isChunkLoaded(chunkX, chunkZ)) {
240246
if (loadChunk) {
241247
location.getChunk().load();
242248
} else {
243-
return null;
249+
throw new IllegalArgumentException("Location " + location.getX() + ", " + location.getY() + ", " + location.getZ() + " " + " in world " + world.getName() + " is not loaded. Use CustomBlockPlaceOption.LOAD_CHUNK to load it automatically.");
244250
}
245251
}
246252

247-
if (!WorldSelection.isDestructible(originalType)) {
248-
if (replaceIndestructible) {
253+
Block block = location.getBlock();
254+
Material originalType = block.getType();
255+
256+
if (!WorldSelection.isEphemeral(originalType)) {
257+
if (breakSolidMaterial) {
249258
block.setType(centralMaterial);
250259
} else {
251-
return null;
260+
throw new IllegalArgumentException("Cannot place custom block at " + location.getBlockX() + ", " + location.getBlockY() + ", " + location.getBlockZ() + " in world " + world.getName() + " because the block is solid and CustomBlockPlaceOption.BREAK_SOLID_MATERIAL is not set.");
252261
}
253262
} else {
254263
block.setType(centralMaterial);
@@ -259,15 +268,15 @@ public CustomBlock placeBlock(@NotNull AbstractCustomBlock abstractCustomBlock,
259268
if (replaceCustomBlock) {
260269
this.breakBlock(this.getCustomBlock(location), player);
261270
} else {
262-
return null;
271+
throw new IllegalArgumentException("Cannot place custom block at " + location.getBlockX() + ", " + location.getBlockY() + ", " + location.getBlockZ() + " in world " + world.getName() + " because a custom block is already present at this location. Use CustomBlockPlaceOption.REPLACE_CUSTOM_BLOCK to replace it.");
263272
}
264273
}
265274

266275

267276

268277
List<UUID> displayVehicleUuids = new ArrayList<>();
269278

270-
BlockDisplay centerEntity = location.getWorld().spawn(location, BlockDisplay.class);
279+
BlockDisplay centerEntity = world.spawn(location, BlockDisplay.class);
271280

272281
List<Display> displays = abstractCustomBlock.spawnDisplay(location, centerEntity, display -> {
273282
if (!display.isInsideVehicle()) {
@@ -297,7 +306,7 @@ public CustomBlock placeBlock(@NotNull AbstractCustomBlock abstractCustomBlock,
297306

298307
if (displays.isEmpty()) {
299308
block.setType(originalType);
300-
ChatUtil.log("&6[BlockDisplayCreator] &4Something went wrong, custom block at location " + location.getBlockX() + " " + location.getBlockY() + " " + location.getBlockZ() + " in %s world was not placed due to missing display entities. You may have specified the spawn command incorrectly.", location.getWorld().getName());
309+
ChatUtil.log("&6[BlockDisplayCreator] &4Something went wrong, custom block at location " + location.getBlockX() + " " + location.getBlockY() + " " + location.getBlockZ() + " in %s world was not placed due to missing display entities. You may have specified the spawn command incorrectly.", world.getName());
301310
return null;
302311
}
303312

@@ -411,11 +420,11 @@ public static void applyDisplayTranslationRotation(@NotNull GroupSummoner<Displa
411420
* {@inheritDoc}
412421
*/
413422
@Override
414-
public boolean breakBlock(@NotNull CustomBlock customBlock, @Nullable Player player, CustomBlockOption... options) {
423+
public boolean breakBlock(@NotNull CustomBlock customBlock, @Nullable Player player, CustomBlockOption... options) throws IllegalArgumentException {
415424
Location location = customBlock.getLocation();
416425

417426
if (!isCustomBlockOnLocation(location)) {
418-
return false;
427+
throw new IllegalArgumentException("Cannot break custom block at " + location.getBlockX() + ", " + location.getBlockY() + ", " + location.getBlockZ() + " in world " + location.getWorld().getName() + " because no custom block is present at this location.");
419428
}
420429

421430
boolean dropItem = false;

src/main/java/me/general_breddok/blockdisplaycreator/custom/block/CustomBlockService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public interface CustomBlockService {
8484
* @return the placed {@link CustomBlock} if successful, otherwise null.
8585
*/
8686
@Nullable
87-
CustomBlock placeBlock(@NotNull AbstractCustomBlock abstractCustomBlock, @NotNull Location location, @NotNull CustomBlockRotation rotation, @Nullable Player player, CustomBlockOption... options);
87+
CustomBlock placeBlock(@NotNull AbstractCustomBlock abstractCustomBlock, @NotNull Location location, @NotNull CustomBlockRotation rotation, @Nullable Player player, CustomBlockOption... options) throws IllegalArgumentException;
8888

8989
/**
9090
* Breaks the specified custom block.
@@ -94,7 +94,7 @@ public interface CustomBlockService {
9494
* @param options additional breaking options.
9595
* @return true if the block was successfully broken, false otherwise.
9696
*/
97-
boolean breakBlock(@NotNull CustomBlock customBlock, @Nullable Player player, CustomBlockOption... options);
97+
boolean breakBlock(@NotNull CustomBlock customBlock, @Nullable Player player, CustomBlockOption... options) throws IllegalArgumentException;
9898

9999
/**
100100
* Creates an item associated with a custom block.

src/main/java/me/general_breddok/blockdisplaycreator/custom/block/option/CustomBlockPlaceOption.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
public enum CustomBlockPlaceOption implements CustomBlockOption {
44
REPLACE_CUSTOM_BLOCK,
5-
BREAK_INDESTRUCTIBLE_MATERIAL,
5+
BREAK_SOLID_MATERIAL,
66
SILENT_PLACE,
77
LOAD_CHUNK
88
}

src/main/java/me/general_breddok/blockdisplaycreator/listener/block/BlockBreakListener.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@ public void onBlockBreak(BlockBreakEvent event) {
8282
options[0] = CustomBlockBreakOption.DROP_ITEM;
8383
}
8484

85-
customBlockService.breakBlock(customBlock, player, options);
85+
try {
86+
customBlockService.breakBlock(customBlock, player, options);
87+
} catch (IllegalArgumentException ignore) {
88+
}
8689
}
8790

8891
private static boolean checkAccess(Player player, CustomBlock customBlock) {

src/main/java/me/general_breddok/blockdisplaycreator/listener/entity/EntityDamageByEntityListener.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
109109
options[0] = CustomBlockBreakOption.DROP_ITEM;
110110
}
111111

112-
customBlockService.breakBlock(customBlock, player, options);
112+
try {
113+
customBlockService.breakBlock(customBlock, player, options);
114+
} catch (IllegalArgumentException ignore) {
115+
}
113116
}
114117
}

src/main/java/me/general_breddok/blockdisplaycreator/listener/player/PlayerInteractListener.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,11 @@ public void onPlayerInteract(PlayerInteractEvent event) {
119119
}
120120

121121

122-
CustomBlock customBlock = customBlockService.placeBlock(abstractCustomBlock, blockLocation, customBlockRotation, player);
122+
CustomBlock customBlock = null;
123+
try {
124+
customBlock = customBlockService.placeBlock(abstractCustomBlock, blockLocation, customBlockRotation, player);
125+
} catch (IllegalArgumentException ignore) {
126+
}
123127

124128
if (customBlock != null && !(player.getGameMode() == GameMode.CREATIVE || player.getGameMode() == GameMode.SPECTATOR)) {
125129
item.setAmount(item.getAmount() - 1);

src/main/java/me/general_breddok/blockdisplaycreator/world/WorldSelection.java

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,22 @@
2020
@ApiStatus.Obsolete
2121
public class WorldSelection extends BoundingBox {
2222

23-
private static final List<Material> destructibleMaterials = new ArrayList<>();
23+
private static final List<Material> ephemeralMaterials = new ArrayList<>();
2424

2525
static {
26-
destructibleMaterials.add(Material.LAVA);
27-
destructibleMaterials.add(Material.WATER);
28-
destructibleMaterials.add(getShortGrass());
29-
destructibleMaterials.add(Material.TALL_GRASS);
30-
destructibleMaterials.add(Material.FERN);
31-
destructibleMaterials.add(Material.LARGE_FERN);
32-
destructibleMaterials.add(Material.SEAGRASS);
33-
destructibleMaterials.add(Material.TALL_SEAGRASS);
34-
destructibleMaterials.add(Material.AIR);
35-
destructibleMaterials.add(Material.CAVE_AIR);
36-
destructibleMaterials.add(Material.VOID_AIR);
37-
destructibleMaterials.add(Material.LIGHT);
26+
ephemeralMaterials.add(Material.LAVA);
27+
ephemeralMaterials.add(Material.WATER);
28+
29+
ephemeralMaterials.add(getShortGrass());
30+
ephemeralMaterials.add(Material.TALL_GRASS);
31+
ephemeralMaterials.add(Material.FERN);
32+
ephemeralMaterials.add(Material.LARGE_FERN);
33+
ephemeralMaterials.add(Material.SEAGRASS);
34+
ephemeralMaterials.add(Material.TALL_SEAGRASS);
35+
ephemeralMaterials.add(Material.AIR);
36+
ephemeralMaterials.add(Material.CAVE_AIR);
37+
ephemeralMaterials.add(Material.VOID_AIR);
38+
ephemeralMaterials.add(Material.LIGHT);
3839
}
3940

4041
private World world;
@@ -57,16 +58,16 @@ public WorldSelection(BoundingBox boundingBox, World world) {
5758
this(boundingBox.getMin().toLocation(world), boundingBox.getMax().toLocation(world), world);
5859
}
5960

60-
public static boolean isDestructible(List<Location> locationList) {
61-
return locationList.stream().allMatch(location -> isDestructible(location.getBlock()));
61+
public static boolean isEphemeral(List<Location> locationList) {
62+
return locationList.stream().allMatch(location -> isEphemeral(location.getBlock()));
6263
}
6364

64-
public static boolean isDestructible(Block block) {
65-
return isDestructible(block.getType());
65+
public static boolean isEphemeral(Block block) {
66+
return isEphemeral(block.getType());
6667
}
6768

68-
public static boolean isDestructible(Material material) {
69-
return destructibleMaterials.contains(material);
69+
public static boolean isEphemeral(Material material) {
70+
return ephemeralMaterials.contains(material);
7071
}
7172

7273
public Location getMinLocation() {

0 commit comments

Comments
 (0)