Skip to content

Commit

Permalink
Complete rework of config system. Exclude shulker boxes from healing …
Browse files Browse the repository at this point in the history
…system
  • Loading branch information
ArkoSammy12 committed Apr 28, 2024
1 parent e7b7f7a commit 8adf630
Show file tree
Hide file tree
Showing 49 changed files with 1,016 additions and 754 deletions.
6 changes: 4 additions & 2 deletions src/main/java/xd/arkosammy/creeperhealing/CreeperHealing.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public class CreeperHealing implements ModInitializer {
// TODO: Remake entire config system somehow. Make it more generic?
@Override
public void onInitialize() {
ConfigManager.init();
//ConfigManager.init();
ConfigManager.getInstance().init();
ServerLifecycleEvents.SERVER_STARTING.register(CreeperHealing::onServerStarting);
ServerLifecycleEvents.SERVER_STOPPING.register(CreeperHealing::onServerStopping);
ServerTickEvents.END_SERVER_TICK.register(server -> ExplosionManager.getInstance().tick(server));
Expand All @@ -34,7 +35,8 @@ private static void onServerStarting(MinecraftServer server) {
private static void onServerStopping(MinecraftServer server) {
ExplosionManager.getInstance().storeExplosions(server);
ExplosionManager.getInstance().getExplosionEvents().clear();
ConfigManager.updateConfigFile();
//ConfigManager.updateConfigFile();
ConfigManager.getInstance().saveToFile();
}

}
23 changes: 13 additions & 10 deletions src/main/java/xd/arkosammy/creeperhealing/blocks/AffectedBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
import net.minecraft.world.World;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import xd.arkosammy.creeperhealing.config.DelaysConfig;
import xd.arkosammy.creeperhealing.config.PreferencesConfig;
import xd.arkosammy.creeperhealing.config.ReplaceMapConfig;
import xd.arkosammy.creeperhealing.explosions.AbstractExplosionEvent;
import xd.arkosammy.creeperhealing.config.ConfigManager;
import xd.arkosammy.creeperhealing.config.settings.BlockPlacementDelaySetting;
import xd.arkosammy.creeperhealing.config.settings.enums.PreferencesSettings;
import xd.arkosammy.creeperhealing.config.tables.ReplaceMapTable;
import xd.arkosammy.creeperhealing.util.ExplosionUtils;
import xd.arkosammy.creeperhealing.util.SerializedAffectedBlock;

Expand Down Expand Up @@ -47,13 +48,13 @@ public AffectedBlock(BlockPos pos, BlockState state, RegistryKey<World> registry

public static AffectedBlock newAffectedBlock(BlockPos pos, BlockState state, World world){
if(state.contains(Properties.DOUBLE_BLOCK_HALF) || state.contains(Properties.BED_PART)){
return new DoubleAffectedBlock(pos, state, world.getRegistryKey(), DelaysConfig.getBlockPlacementDelayAsTicks(), false);
return new DoubleAffectedBlock(pos, state, world.getRegistryKey(), BlockPlacementDelaySetting.getAsTicks(), false);
} else {
BlockEntity blockEntity = world.getBlockEntity(pos);
if(blockEntity != null && PreferencesConfig.RESTORE_BLOCK_NBT.getEntry().getValue()){
return new AffectedBlock(pos, state, world.getRegistryKey(), blockEntity.createNbtWithId(), DelaysConfig.getBlockPlacementDelayAsTicks(), false);
if(blockEntity != null && ConfigManager.getInstance().getAsBooleanSetting(PreferencesSettings.RESTORE_BLOCK_NBT.getName()).getValue()){
return new AffectedBlock(pos, state, world.getRegistryKey(), blockEntity.createNbtWithId(), BlockPlacementDelaySetting.getAsTicks(), false);
} else {
return new AffectedBlock(pos, state, world.getRegistryKey(), null, DelaysConfig.getBlockPlacementDelayAsTicks(), false);
return new AffectedBlock(pos, state, world.getRegistryKey(), null, BlockPlacementDelaySetting.getAsTicks(), false);
}
}
}
Expand Down Expand Up @@ -116,8 +117,10 @@ public void tryHealing(MinecraftServer server, AbstractExplosionEvent currentExp
//Check if the block we are about to try placing is in the replace-map.
//If it is, switch the state for the corresponding one in the replace-map.
String blockIdentifier = Registries.BLOCK.getId(state.getBlock()).toString();
if(ReplaceMapConfig.getReplaceMap().containsKey(blockIdentifier)){
state = Registries.BLOCK.get(new Identifier(ReplaceMapConfig.getReplaceMap().get(blockIdentifier))).getStateWithProperties(state);
Optional<String> replaceMapValueOptional = ReplaceMapTable.getFromKey(blockIdentifier);
if(replaceMapValueOptional.isPresent()){
String replaceMapValue = replaceMapValueOptional.get();
state = Registries.BLOCK.get(new Identifier(replaceMapValue)).getStateWithProperties(state);
stateReplaced = true;
}
if(!this.shouldHealBlock(world, this.pos)){
Expand All @@ -127,7 +130,7 @@ public void tryHealing(MinecraftServer server, AbstractExplosionEvent currentExp
ExplosionUtils.pushEntitiesUpwards(world, pos, false);
}
if(state.getBlock() instanceof FallingBlock){
ExplosionUtils.FALLING_BLOCK_SCHEDULE_TICK.set(PreferencesConfig.MAKE_FALLING_BLOCKS_FALL.getEntry().getValue());
ExplosionUtils.FALLING_BLOCK_SCHEDULE_TICK.set(ConfigManager.getInstance().getAsBooleanSetting(PreferencesSettings.MAKE_FALLING_BLOCKS_FALL.getName()).getValue());
}
world.setBlockState(pos, state);
if(this.nbt != null && !stateReplaced) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.World;
import xd.arkosammy.creeperhealing.config.ReplaceMapConfig;
import xd.arkosammy.creeperhealing.explosions.AbstractExplosionEvent;
import xd.arkosammy.creeperhealing.config.tables.ReplaceMapTable;
import xd.arkosammy.creeperhealing.util.ExplosionUtils;

import java.util.Optional;

public class DoubleAffectedBlock extends AffectedBlock {

public static final String TYPE = "double_affected_block";
Expand All @@ -33,7 +35,8 @@ public void tryHealing(MinecraftServer server, AbstractExplosionEvent currentExp

BlockState state = this.getState();
String blockIdentifier = Registries.BLOCK.getId(state.getBlock()).toString();
if(ReplaceMapConfig.getReplaceMap().containsKey(blockIdentifier)){
Optional<String> replaceMapValueOptional = ReplaceMapTable.getFromKey(blockIdentifier);
if(replaceMapValueOptional.isPresent()){
super.tryHealing(server, currentExplosionEvent);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static void registerCommands(CommandDispatcher<ServerCommandSource> dispa

private static void reload(CommandContext<ServerCommandSource> ctx) throws IOException {
//If this returns true, then the config file exists, and we can update our values from it
if(ConfigManager.reloadValuesFromConfig(ctx)) ctx.getSource().sendMessage(Text.literal("ConfigManager successfully reloaded"));
if(ConfigManager.getInstance().reloadFromFile()) ctx.getSource().sendMessage(Text.literal("Config successfully reloaded"));
else ctx.getSource().sendMessage(Text.literal("Found no existing config file to reload values from").formatted(Formatting.RED));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import xd.arkosammy.creeperhealing.config.DelaysConfig;
import xd.arkosammy.creeperhealing.config.ConfigManager;
import xd.arkosammy.creeperhealing.config.settings.BlockPlacementDelaySetting;
import xd.arkosammy.creeperhealing.config.settings.DoubleSetting;
import xd.arkosammy.creeperhealing.config.settings.HealDelaySetting;
import xd.arkosammy.creeperhealing.config.settings.enums.DelaysSettings;
import xd.arkosammy.creeperhealing.util.ExplosionManager;

public final class DelaysCommands {
Expand Down Expand Up @@ -65,8 +69,9 @@ static void register(LiteralCommandNode<ServerCommandSource> creeperHealingNode)
}

private static int setExplosionHealDelayCommand(CommandContext<ServerCommandSource> ctx) {
if(Math.round(Math.max(DoubleArgumentType.getDouble(ctx, "seconds"), 0) * 20L) != 0) {
DelaysConfig.EXPLOSION_HEAL_DELAY.getEntry().setValue(DoubleArgumentType.getDouble(ctx, "seconds"));
double value = DoubleArgumentType.getDouble(ctx, "seconds");
if(Math.round(Math.max(value, 0) * 20L) != 0) {
ConfigManager.getInstance().getAsDoubleSetting(DelaysSettings.EXPLOSION_HEAL_DELAY.getName()).setValue(value);
ctx.getSource().sendMessage(Text.literal("Explosion heal delay has been set to: " + DoubleArgumentType.getDouble(ctx, "seconds") + " second(s)"));
} else {
ctx.getSource().sendMessage(Text.literal("Cannot set explosion heal delay to a very low value").formatted(Formatting.RED));
Expand All @@ -75,8 +80,9 @@ private static int setExplosionHealDelayCommand(CommandContext<ServerCommandSour
}

private static int setBlockPlacementDelayCommand(CommandContext<ServerCommandSource> ctx) {
if (Math.round(Math.max(DoubleArgumentType.getDouble(ctx, "seconds"), 0) * 20L) != 0) {
DelaysConfig.BLOCK_PLACEMENT_DELAY.getEntry().setValue(DoubleArgumentType.getDouble(ctx, "seconds"));
double value = DoubleArgumentType.getDouble(ctx, "seconds");
if (Math.round(Math.max(value, 0) * 20L) != 0) {
ConfigManager.getInstance().getAsDoubleSetting(DelaysSettings.BLOCK_PLACEMENT_DELAY.getName()).setValue(value);
ExplosionManager.getInstance().updateAffectedBlocksTimers();
ctx.getSource().sendMessage(Text.literal("Block placement delay has been set to: " + DoubleArgumentType.getDouble(ctx, "seconds") + " second(s)"));
} else {
Expand All @@ -86,12 +92,24 @@ private static int setBlockPlacementDelayCommand(CommandContext<ServerCommandSou
}

private static int getExplosionHealDelayCommand(CommandContext<ServerCommandSource> ctx){
ctx.getSource().sendMessage(Text.literal("Explosion heal delay currently set to: " + ((double)DelaysConfig.getExplosionHealDelayAsTicks() / 20) + " second(s)"));

DoubleSetting setting = ConfigManager.getInstance().getAsDoubleSetting(DelaysSettings.EXPLOSION_HEAL_DELAY.getName());
if(setting instanceof HealDelaySetting doubleSetting){
ctx.getSource().sendMessage(Text.literal("Explosion heal delay currently set to: " + doubleSetting.getValue() + " second(s)"));
} else {
ctx.getSource().sendMessage(Text.literal("Error getting explosion heal delay setting").formatted(Formatting.RED));
}
return Command.SINGLE_SUCCESS;
}

private static int getBlockPlacementDelayCommand(CommandContext<ServerCommandSource> ctx){
ctx.getSource().sendMessage(Text.literal("Block placement delay currently set to: " + ((double) DelaysConfig.getBlockPlacementDelayAsTicks() / 20) + " second(s)"));

DoubleSetting setting = ConfigManager.getInstance().getAsDoubleSetting(DelaysSettings.BLOCK_PLACEMENT_DELAY.getName());
if(setting instanceof BlockPlacementDelaySetting doubleSetting){
ctx.getSource().sendMessage(Text.literal("Block placement delay currently set to: " + doubleSetting.getValue() + " second(s)"));
} else {
ctx.getSource().sendMessage(Text.literal("Error getting block placement delay setting").formatted(Formatting.RED));
}
return Command.SINGLE_SUCCESS;
}

Expand Down
Loading

0 comments on commit 8adf630

Please sign in to comment.