Skip to content

Commit

Permalink
Allow to store nbt of all kinds of blocks, not just container blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
ArkoSammy12 committed Feb 17, 2024
1 parent 43dc021 commit 3361834
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public static AffectedBlock newAffectedBlock(BlockPos pos, BlockState state, Wor
return new DoubleAffectedBlock(pos, state, world.getRegistryKey(), DelaysConfig.getBlockPlacementDelayAsTicks(), false);
} else {
BlockEntity blockEntity = world.getBlockEntity(pos);
if(blockEntity instanceof Inventory && PreferencesConfig.HEAL_BLOCK_INVENTORIES.getEntry().getValue()){
return new AffectedBlock(pos, state, world.getRegistryKey(), blockEntity.createNbtWithId() ,DelaysConfig.getBlockPlacementDelayAsTicks(), false);
if(blockEntity != null && PreferencesConfig.RESTORE_BLOCK_NBT.getEntry().getValue()){
return new AffectedBlock(pos, state, world.getRegistryKey(), blockEntity.createNbtWithId(), DelaysConfig.getBlockPlacementDelayAsTicks(), false);
} else {
return new AffectedBlock(pos, state, world.getRegistryKey(), null, DelaysConfig.getBlockPlacementDelayAsTicks(), false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ static void register(LiteralCommandNode<ServerCommandSource> creeperHealingNode)
.build();

//Heal block inventories node
LiteralCommandNode<ServerCommandSource> healBlockInventoriesNode = CommandManager
.literal("heal_block_inventories")
.executes(PreferencesCommands::getHealBlockInventoriesCommand)
LiteralCommandNode<ServerCommandSource> restoreBlockNbtNode = CommandManager
.literal("restore_block_nbt")
.executes(PreferencesCommands::getRestoreBlockNbtCommand)
.requires(serverCommandSource -> serverCommandSource.hasPermissionLevel(4))
.build();

Expand Down Expand Up @@ -59,9 +59,9 @@ static void register(LiteralCommandNode<ServerCommandSource> creeperHealingNode)
.executes(PreferencesCommands::getEnableWhitelistCommand)
.build();

ArgumentCommandNode<ServerCommandSource, Boolean> healBlockInventoriesArgumentNode = CommandManager
ArgumentCommandNode<ServerCommandSource, Boolean> restoreBlockNbtArgumentNode = CommandManager
.argument("value", BoolArgumentType.bool())
.executes(PreferencesCommands::setHealBlockInvenotriesCommand)
.executes(PreferencesCommands::setRestoreBlockNbtCommand)
.requires(serverCommandSource -> serverCommandSource.hasPermissionLevel(4))
.build();

Expand Down Expand Up @@ -97,23 +97,23 @@ static void register(LiteralCommandNode<ServerCommandSource> creeperHealingNode)
creeperHealingNode.addChild(settingsNode);

//Preferences commands nodes
settingsNode.addChild(healBlockInventoriesNode);
settingsNode.addChild(restoreBlockNbtNode);
settingsNode.addChild(shouldPlaySoundOnBlockPlacementNode);
settingsNode.addChild(healOnHealingPotionSplashNode);
settingsNode.addChild(healOnRegenerationPotionSplash);
settingsNode.addChild(enableWhitelistNode);

//Argument nodes
healBlockInventoriesNode.addChild(healBlockInventoriesArgumentNode);
restoreBlockNbtNode.addChild(restoreBlockNbtArgumentNode);
shouldPlaySoundOnBlockPlacementNode.addChild(playSoundOnBlockPlacementArgumentNode);
healOnHealingPotionSplashNode.addChild(healOnHealingPotionSplashArgumentNode);
healOnRegenerationPotionSplash.addChild(healOnRegenerationPotionSplashArgumentNode);
enableWhitelistNode.addChild(enableWhitelistArgumentNode);

}

private static int setHealBlockInvenotriesCommand(CommandContext<ServerCommandSource> ctx){
PreferencesConfig.HEAL_BLOCK_INVENTORIES.getEntry().setValue(BoolArgumentType.getBool(ctx, "value"));
private static int setRestoreBlockNbtCommand(CommandContext<ServerCommandSource> ctx){
PreferencesConfig.RESTORE_BLOCK_NBT.getEntry().setValue(BoolArgumentType.getBool(ctx, "value"));
ctx.getSource().sendMessage(Text.literal("Heal block inventories has been set to: " + BoolArgumentType.getBool(ctx, "value")));
return Command.SINGLE_SUCCESS;
}
Expand Down Expand Up @@ -142,8 +142,8 @@ private static int setEnableWhitelistCommand(CommandContext<ServerCommandSource>
return Command.SINGLE_SUCCESS;
}

private static int getHealBlockInventoriesCommand(CommandContext<ServerCommandSource> ctx){
ctx.getSource().sendMessage(Text.literal("Heal block inventories currently set to: " + PreferencesConfig.HEAL_BLOCK_INVENTORIES.getEntry().getValue()));
private static int getRestoreBlockNbtCommand(CommandContext<ServerCommandSource> ctx){
ctx.getSource().sendMessage(Text.literal("Heal block inventories currently set to: " + PreferencesConfig.RESTORE_BLOCK_NBT.getEntry().getValue()));
return Command.SINGLE_SUCCESS;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

public enum PreferencesConfig {

HEAL_BLOCK_INVENTORIES(new ConfigEntry<>("heal_block_inventories", false, """
(Default = false) Whether a container block such as a chest should have its inventory restored when healed. This option also prevents the container's items from being dropped.""")),
RESTORE_BLOCK_NBT(new ConfigEntry<>("restore_block_nbt", false, """
(Default = false) Whether to restore block nbt data upon healing. This option prevents container blocks like chests from dropping their inventories. Does not apply when the healed block is different from the destroyed block due to a replace map entry.""")),
BLOCK_PLACEMENT_SOUND_EFFECT(new ConfigEntry<>("block_placement_sound_effect", true, """
(Default = true) Whether a block placement sound effect should be played when a block is healed.""")),
HEAL_ON_HEALING_POTION_SPLASH(new ConfigEntry<>("heal_on_healing_potion_splash", true, """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private void storeCurrentExplosionIfNeeded(CallbackInfo ci){
@Inject(method = "affectWorld", at = @At(value = "HEAD"))
private void setDropItemsThreadLocal(boolean particles, CallbackInfo ci){
ExplosionUtils.DROP_EXPLOSION_ITEMS.set(true);
ExplosionUtils.DROP_BLOCK_INVENTORY_ITEMS.set(!PreferencesConfig.HEAL_BLOCK_INVENTORIES.getEntry().getValue());
ExplosionUtils.DROP_BLOCK_INVENTORY_ITEMS.set(!PreferencesConfig.RESTORE_BLOCK_NBT.getEntry().getValue());
}

@Inject(method = "affectWorld", at = @At(value = "RETURN"))
Expand Down

0 comments on commit 3361834

Please sign in to comment.