Skip to content

Commit 4e62ca1

Browse files
Adding a drop-mode parameter and changing the placement-mode signature
1 parent bb86ca2 commit 4e62ca1

20 files changed

Lines changed: 272 additions & 126 deletions

src/main/java/me/general_breddok/blockdisplaycreator/BlockDisplayCreator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ private void registerDataAdapters() {
183183
yamlStore.register(TypeTokens.DYE_COLOR, PersistentDataTypes.DYE_COLOR);
184184
yamlStore.register(TypeTokens.DIRECTED_VECTOR, PersistentDataTypes.DIRECTED_VECTOR);
185185
yamlStore.register(TypeTokens.CUSTOM_BLOCK_PLACEMENT_MODE, PersistentDataTypes.CUSTOM_BLOCK_PLACEMENT_MODE);
186+
yamlStore.register(TypeTokens.CUSTOM_BLOCK_DROP_MODE, PersistentDataTypes.CUSTOM_BLOCK_DROP_MODE);
186187
}
187188

188189
@Override

src/main/java/me/general_breddok/blockdisplaycreator/common/DeprecatedFeatureAdapter.java

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

33
import lombok.experimental.UtilityClass;
44
import me.general_breddok.blockdisplaycreator.BlockDisplayCreator;
5+
import me.general_breddok.blockdisplaycreator.custom.block.BDCCustomBlockService;
6+
import me.general_breddok.blockdisplaycreator.custom.block.CustomBlockService;
57
import me.general_breddok.blockdisplaycreator.data.yaml.YamlConfigFile;
68
import org.bukkit.configuration.ConfigurationSection;
79
import org.jetbrains.annotations.ApiStatus;
@@ -19,4 +21,11 @@ public UUID checkMissingCustomBlockUUID(UUID uuid) {
1921
}
2022
return uuid;
2123
}
24+
25+
public String checkMissingServiceClass(String serviceClass) {
26+
if (serviceClass == null) {
27+
return BDCCustomBlockService.class.getName();
28+
}
29+
return serviceClass;
30+
}
2231
}

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@ public interface AbstractCustomBlock extends DeepCloneable<AbstractCustomBlock>
3636

3737
/**
3838
* Gets the class name of the service that handles the custom block's behavior.
39-
*
39+
* <p>
40+
* <b> Note: </b> {@link Class#getName()} is used to get the class name of the service
4041
* @return Class name of the service as a String.
41-
*
42-
* <p> <b> Note: </b> {@link Class#getName()} is used to get the class name of the service </p>
4342
*/
4443
@NotNull
4544
String getServiceClassName();
@@ -92,11 +91,6 @@ public interface AbstractCustomBlock extends DeepCloneable<AbstractCustomBlock>
9291

9392
void setSaveSystem(String saveSystem);
9493

95-
@Nullable
96-
CustomBlockPlacementMode getPlacementMode();
97-
98-
void setPlacementMode(CustomBlockPlacementMode placementMode);
99-
10094

10195

10296
default List<Display> spawnDisplay(@NotNull Location location, @Nullable Predicate<Display> iteration) {

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ public class BDCAbstractCustomBlock implements AbstractCustomBlock {
4040
@Nullable
4141
CustomBlockStageSettings stageSettings;
4242
String saveSystem;
43-
@Nullable
44-
CustomBlockPlacementMode placementMode;
4543

4644
public BDCAbstractCustomBlock(String name, GroupSummoner<Display> displaySummoner, List<ConfiguredInteraction> configuredInteractions) {
4745
this(name, displaySummoner, configuredInteractions, List.of());
@@ -97,8 +95,7 @@ public BDCAbstractCustomBlock(String name, String serviceClassName, GroupSummone
9795
permissions,
9896
soundGroup,
9997
stageSettings,
100-
"yaml-file",
101-
CustomBlockPlacementMode.DEFAULT
98+
"yaml-file"
10299
);
103100
}
104101

@@ -120,8 +117,7 @@ public BDCAbstractCustomBlock clone() {
120117
this.permissions,
121118
this.soundGroup,
122119
this.stageSettings,
123-
this.saveSystem,
124-
this.placementMode
120+
this.saveSystem
125121
);
126122
}
127123
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class BDCCustomBlock extends BDCAbstractCustomBlock implements CustomBloc
3939

4040

4141
public BDCCustomBlock(AbstractCustomBlock acb, Location location, CustomBlockRotation rotation, List<Display> displays, List<Interaction> interactions, List<Shulker> collisions, UUID uuid) {
42-
super(acb.getName(), acb.getServiceClassName(), acb.getDisplaySummoner(), acb.getConfiguredInteractions(), acb.getConfiguredCollisions(), acb.getItem(), acb.getCentralMaterial(), acb.getSidesCount(), acb.getPermissions(), acb.getSoundGroup(), acb.getStageSettings(), acb.getSaveSystem(), acb.getPlacementMode());
42+
super(acb.getName(), acb.getServiceClassName(), acb.getDisplaySummoner(), acb.getConfiguredInteractions(), acb.getConfiguredCollisions(), acb.getItem(), acb.getCentralMaterial(), acb.getSidesCount(), acb.getPermissions(), acb.getSoundGroup(), acb.getStageSettings(), acb.getSaveSystem());
4343
this.location = location;
4444
this.rotation = rotation;
4545
this.displays = displays;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package me.general_breddok.blockdisplaycreator.custom.block;
2+
3+
import lombok.*;
4+
import lombok.experimental.FieldDefaults;
5+
import me.general_breddok.blockdisplaycreator.custom.CommandBundle;
6+
7+
@Getter
8+
@Setter
9+
@EqualsAndHashCode
10+
@AllArgsConstructor
11+
@FieldDefaults(level = AccessLevel.PRIVATE)
12+
public class BDCCustomBlockBreakSettings implements CustomBlockBreakSettings {
13+
DropMode dropMode;
14+
CommandBundle commandBundle;
15+
16+
public BDCCustomBlockBreakSettings(DropMode dropMode) {
17+
this(dropMode, null);
18+
}
19+
20+
public BDCCustomBlockBreakSettings() {
21+
this(DropMode.ON_GROUND, null);
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package me.general_breddok.blockdisplaycreator.custom.block;
2+
3+
import lombok.*;
4+
import lombok.experimental.FieldDefaults;
5+
import me.general_breddok.blockdisplaycreator.custom.CommandBundle;
6+
7+
@Getter
8+
@Setter
9+
@EqualsAndHashCode
10+
@AllArgsConstructor
11+
@FieldDefaults(level = AccessLevel.PRIVATE)
12+
public class BDCCustomBlockPlaceSettings implements CustomBlockPlaceSettings {
13+
PlacementMode placementMode;
14+
CommandBundle commandBundle;
15+
16+
public BDCCustomBlockPlaceSettings(PlacementMode placementMode) {
17+
this(placementMode, null);
18+
}
19+
20+
public BDCCustomBlockPlaceSettings() {
21+
this(PlacementMode.DEFAULT, null);
22+
}
23+
}

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import me.general_breddok.blockdisplaycreator.rotation.EntityRotation;
2424
import me.general_breddok.blockdisplaycreator.util.ChatUtil;
2525
import me.general_breddok.blockdisplaycreator.util.EventUtil;
26+
import me.general_breddok.blockdisplaycreator.util.ItemUtil;
2627
import me.general_breddok.blockdisplaycreator.util.OperationUtil;
2728
import me.general_breddok.blockdisplaycreator.world.WorldSelection;
2829
import org.bukkit.Bukkit;
@@ -364,7 +365,7 @@ public CustomBlock placeBlock(@NotNull AbstractCustomBlock abstractCustomBlock,
364365

365366
CustomBlockStageSettings stageSettings = abstractCustomBlock.getStageSettings();
366367
if (stageSettings != null) {
367-
CommandBundle placeCommandBundle = stageSettings.getPlaceCommandBundle();
368+
CommandBundle placeCommandBundle = stageSettings.getPlaceSettings().getCommandBundle();
368369
if (placeCommandBundle != null) {
369370
CommandBundle.CommandSource commandSource = placeCommandBundle.getCommandSource();
370371
if (commandSource == CommandBundle.CommandSource.PLAYER && player != null) {
@@ -395,8 +396,13 @@ public static void applyDisplayTranslationRotation(@NotNull GroupSummoner<Displa
395396
@Override
396397
public boolean breakBlock(@NotNull CustomBlock customBlock, @Nullable Player player, CustomBlockOption... options) throws IllegalArgumentException {
397398
Location location = customBlock.getLocation();
399+
World world = location.getWorld();
398400
LocationPlaceholder locationPlaceholder = new LocationPlaceholder(location);
399401

402+
if (world == null) {
403+
throw new IllegalArgumentException("Location's world cannot be null");
404+
}
405+
400406
if (!isCustomBlockOnLocation(location)) {
401407
throw new IllegalArgumentException(locationPlaceholder.apply(StringMessagesValue.CUSTOM_BLOCK_BREAK_FAIL_REASON_NO_CUSTOM_BLOCK));
402408
}
@@ -419,7 +425,7 @@ public boolean breakBlock(@NotNull CustomBlock customBlock, @Nullable Player pla
419425

420426
CustomBlockStageSettings stageSettings = customBlock.getStageSettings();
421427
if (stageSettings != null) {
422-
CommandBundle breakCommandBundle = stageSettings.getBreakCommandBundle();
428+
CommandBundle breakCommandBundle = stageSettings.getBreakSettings().getCommandBundle();
423429
if (breakCommandBundle != null) {
424430
breakCommandBundle.execute(player, new CustomBlockPlaceholder[]{new CustomBlockPlaceholder(customBlock)});
425431
}
@@ -448,7 +454,23 @@ public boolean breakBlock(@NotNull CustomBlock customBlock, @Nullable Player pla
448454
clearCustomBlockData(customBlockData);
449455

450456
if (dropItem) {
451-
location.getWorld().dropItemNaturally(location, customBlock.getItem());
457+
ItemStack customBlockItem = customBlock.getItem();
458+
customBlockItem.setAmount(1);
459+
460+
461+
if (stageSettings != null) {
462+
463+
CustomBlockBreakSettings.DropMode dropMode = stageSettings.getBreakSettings().getDropMode();
464+
if (dropMode == CustomBlockBreakSettings.DropMode.INVENTORY && player != null) {
465+
466+
ItemUtil.distributeItem(player, customBlockItem);
467+
} else if (dropMode == CustomBlockBreakSettings.DropMode.ON_GROUND) {
468+
469+
world.dropItemNaturally(location, customBlockItem);
470+
}
471+
} else {
472+
world.dropItemNaturally(location, customBlockItem);
473+
}
452474
}
453475

454476
if (!silentBreak) {
@@ -461,6 +483,7 @@ public boolean breakBlock(@NotNull CustomBlock customBlock, @Nullable Player pla
461483
public static CustomBlockData createCustomBlockData(CustomBlock customBlock, List<UUID> displayVehicleUuids) {
462484
CustomBlockData customBlockData = new CustomBlockData(customBlock.getLocation().getBlock(), BlockDisplayCreator.getInstance());
463485
customBlockData.set(CustomBlockKey.NAME, PersistentDataType.STRING, customBlock.getName());
486+
customBlockData.set(CustomBlockKey.SERVICE_CLASS, PersistentDataType.STRING, BDCCustomBlockService.class.getName());
464487
customBlockData.set(CustomBlockKey.DISPLAY_UUID, PersistentDataTypes.UUID_ARRAY, displayVehicleUuids.toArray(UUID[]::new));
465488
customBlockData.set(CustomBlockKey.INTERACTION_UUID, PersistentDataTypes.UUID_ARRAY, customBlock.getInteractions().stream().map(Entity::getUniqueId).toList().toArray(UUID[]::new));
466489
customBlockData.set(CustomBlockKey.COLLISION_UUID, PersistentDataTypes.UUID_ARRAY, customBlock.getCollisions().stream().map(Entity::getUniqueId).toList().toArray(UUID[]::new));
@@ -495,6 +518,7 @@ private static boolean triggerCustomBlockBreakEvent(CustomBlock customBlock, Pla
495518

496519
public static void clearCustomBlockData(CustomBlockData customBlockData) {
497520
customBlockData.remove(CustomBlockKey.NAME);
521+
customBlockData.remove(CustomBlockKey.SERVICE_CLASS);
498522
customBlockData.remove(CustomBlockKey.DISPLAY_UUID);
499523
customBlockData.remove(CustomBlockKey.INTERACTION_UUID);
500524
customBlockData.remove(CustomBlockKey.COLLISION_UUID);

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@
1212
@AllArgsConstructor
1313
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true)
1414
public class BDCCustomBlockStageSettings implements CustomBlockStageSettings {
15-
CommandBundle placeCommandBundle;
16-
CommandBundle breakCommandBundle;
15+
CustomBlockPlaceSettings placeSettings;
16+
CustomBlockBreakSettings breakSettings;
17+
18+
public BDCCustomBlockStageSettings() {
19+
this(new BDCCustomBlockPlaceSettings(), new BDCCustomBlockBreakSettings());
20+
}
21+
22+
public BDCCustomBlockStageSettings(CustomBlockPlaceSettings placeSettings) {
23+
this(placeSettings, new BDCCustomBlockBreakSettings());
24+
}
25+
26+
public BDCCustomBlockStageSettings(CustomBlockBreakSettings breakSettings) {
27+
this(new BDCCustomBlockPlaceSettings(), breakSettings);
28+
}
1729
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package me.general_breddok.blockdisplaycreator.custom.block;
2+
3+
import lombok.AccessLevel;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Getter;
6+
import lombok.experimental.FieldDefaults;
7+
import me.general_breddok.blockdisplaycreator.custom.CommandBundle;
8+
import org.jetbrains.annotations.Nullable;
9+
10+
public interface CustomBlockBreakSettings {
11+
12+
DropMode getDropMode();
13+
14+
void setDropMode(DropMode dropMode);
15+
16+
@Nullable
17+
CommandBundle getCommandBundle();
18+
19+
void setCommandBundle(@Nullable CommandBundle breakCommandBundle);
20+
21+
/**
22+
* Defines how items are dropped when a custom block is broken by a player.
23+
*/
24+
enum DropMode {
25+
26+
/**
27+
* Item dropped on the ground at the block's location.
28+
*/
29+
ON_GROUND,
30+
31+
/**
32+
* Items are placed directly into the player's inventory.
33+
* If the inventory is full, items will drop at the player's location.
34+
*/
35+
INVENTORY;
36+
}
37+
}

0 commit comments

Comments
 (0)