Skip to content

Commit

Permalink
Separated "explosion_heal_delay" and "block_placement_delay" command …
Browse files Browse the repository at this point in the history
…settings into their own sub-command category, named "delays"
  • Loading branch information
ArkoSammy12 committed Oct 29, 2023
1 parent 9a6769c commit 0989904
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import xd.arkosammy.commands.categories.DelaysCommands;
import xd.arkosammy.commands.categories.ExplosionSourcesCommands;
import xd.arkosammy.commands.categories.ModeCommands;
import xd.arkosammy.commands.categories.PreferencesCommands;
Expand Down Expand Up @@ -49,6 +50,7 @@ public static void registerCommands(CommandDispatcher<ServerCommandSource> dispa
ExplosionSourcesCommands.register(creeperHealingNode);
ModeCommands.register(creeperHealingNode);
PreferencesCommands.register(creeperHealingNode);
DelaysCommands.register(creeperHealingNode);
}

private static void reload(CommandContext<ServerCommandSource> ctx) throws IOException {
Expand Down
98 changes: 98 additions & 0 deletions src/main/java/xd/arkosammy/commands/categories/DelaysCommands.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package xd.arkosammy.commands.categories;

import com.mojang.brigadier.Command;
import com.mojang.brigadier.arguments.DoubleArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.tree.ArgumentCommandNode;
import com.mojang.brigadier.tree.LiteralCommandNode;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import xd.arkosammy.configuration.tables.DelaysConfig;
import xd.arkosammy.explosions.AffectedBlock;

public final class DelaysCommands {

private DelaysCommands(){}

public static void register(LiteralCommandNode<ServerCommandSource> creeperHealingNode){

LiteralCommandNode<ServerCommandSource> delaysNode = CommandManager
.literal("delays")
.requires(serverCommandSource -> serverCommandSource.hasPermissionLevel(4))
.build();

//Explosion Heal Delay node
LiteralCommandNode<ServerCommandSource> explosionHealDelayNode = CommandManager
.literal("explosion_heal_delay")
.executes(DelaysCommands::getExplosionHealDelayCommand)
.requires(serverCommandSource -> serverCommandSource.hasPermissionLevel(4))
.build();

//Block Placement Delay node
LiteralCommandNode<ServerCommandSource> blockPlacementDelayNode = CommandManager
.literal("block_placement_delay")
.executes(DelaysCommands::getBlockPlacementDelayCommand)
.requires(serverCommandSource -> serverCommandSource.hasPermissionLevel(4))
.build();

//Explosion heal delay argument node
ArgumentCommandNode<ServerCommandSource, Double> explosionHealDelayArgumentNode = CommandManager
.argument("seconds", DoubleArgumentType.doubleArg())
.executes(DelaysCommands::setExplosionHealDelayCommand)
.requires(serverCommandSource -> serverCommandSource.hasPermissionLevel(4))
.build();

//Block placement delay argument node
ArgumentCommandNode<ServerCommandSource, Double> blockPlacementDelayArgumentNode = CommandManager
.argument("seconds", DoubleArgumentType.doubleArg())
.executes(DelaysCommands::setBlockPlacementDelayCommand)
.requires(serverCommandSource -> serverCommandSource.hasPermissionLevel(4))
.build();

//Root node connection
creeperHealingNode.addChild(delaysNode);

//Delays command nodes
delaysNode.addChild(explosionHealDelayNode);
delaysNode.addChild(blockPlacementDelayNode);

//Argument nodes
explosionHealDelayNode.addChild(explosionHealDelayArgumentNode);
blockPlacementDelayNode.addChild(blockPlacementDelayArgumentNode);

}

private static int setExplosionHealDelayCommand(CommandContext<ServerCommandSource> ctx) {
if(Math.round(Math.max(DoubleArgumentType.getDouble(ctx, "seconds"), 0) * 20L) != 0) {
DelaysConfig.setExplosionHealDelay(DoubleArgumentType.getDouble(ctx, "seconds"));
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));
}
return Command.SINGLE_SUCCESS;
}

private static int setBlockPlacementDelayCommand(CommandContext<ServerCommandSource> ctx) {
if (Math.round(Math.max(DoubleArgumentType.getDouble(ctx, "seconds"), 0) * 20L) != 0) {
DelaysConfig.setBlockPlacementDelay(DoubleArgumentType.getDouble(ctx, "seconds"));
AffectedBlock.updateAffectedBlocksTimers();
ctx.getSource().sendMessage(Text.literal("Block placement delay has been set to: " + DoubleArgumentType.getDouble(ctx, "seconds") + " second(s)"));
} else {
ctx.getSource().sendMessage(Text.literal("Cannot set block placement delay to a very low value").formatted(Formatting.RED));
}
return Command.SINGLE_SUCCESS;
}

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

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@

import com.mojang.brigadier.Command;
import com.mojang.brigadier.arguments.BoolArgumentType;
import com.mojang.brigadier.arguments.DoubleArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.tree.ArgumentCommandNode;
import com.mojang.brigadier.tree.LiteralCommandNode;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import xd.arkosammy.configuration.tables.DelaysConfig;
import xd.arkosammy.configuration.tables.PreferencesConfig;
import xd.arkosammy.explosions.AffectedBlock;


public final class PreferencesCommands {
Expand All @@ -21,26 +17,12 @@ private PreferencesCommands(){}

public static void register(LiteralCommandNode<ServerCommandSource> creeperHealingNode){

//Settings node
//Preferences node
LiteralCommandNode<ServerCommandSource> settingsNode = CommandManager
.literal("preferences")
.requires(serverCommandSource -> serverCommandSource.hasPermissionLevel(4))
.build();

//Explosion Heal Delay node
LiteralCommandNode<ServerCommandSource> explosionHealDelayNode = CommandManager
.literal("explosion_heal_delay")
.executes(PreferencesCommands::getExplosionHealDelayCommand)
.requires(serverCommandSource -> serverCommandSource.hasPermissionLevel(4))
.build();

//Block Placement Delay node
LiteralCommandNode<ServerCommandSource> blockPlacementDelayNode = CommandManager
.literal("block_placement_delay")
.executes(PreferencesCommands::getBlockPlacementDelayCommand)
.requires(serverCommandSource -> serverCommandSource.hasPermissionLevel(4))
.build();

//Heal on Flowing Water node
LiteralCommandNode<ServerCommandSource> shouldHealOnFlowingWaterNode = CommandManager
.literal("heal_on_flowing_water")
Expand Down Expand Up @@ -104,20 +86,6 @@ public static void register(LiteralCommandNode<ServerCommandSource> creeperHeali
.executes(PreferencesCommands::getEnableWhitelist)
.build();

//Explosion heal delay argument node
ArgumentCommandNode<ServerCommandSource, Double> explosionHealDelayArgumentNode = CommandManager
.argument("seconds", DoubleArgumentType.doubleArg())
.executes(PreferencesCommands::setExplosionHealDelayCommand)
.requires(serverCommandSource -> serverCommandSource.hasPermissionLevel(4))
.build();

//Block placement delay argument node
ArgumentCommandNode<ServerCommandSource, Double> blockPlacementDelayArgumentNode = CommandManager
.argument("seconds", DoubleArgumentType.doubleArg())
.executes(PreferencesCommands::setBlockPlacementDelayCommand)
.requires(serverCommandSource -> serverCommandSource.hasPermissionLevel(4))
.build();

//Heal on flowing water argument node
ArgumentCommandNode<ServerCommandSource, Boolean> healOnFlowingWaterArgumentNode = CommandManager
.argument("value", BoolArgumentType.bool())
Expand Down Expand Up @@ -184,9 +152,7 @@ public static void register(LiteralCommandNode<ServerCommandSource> creeperHeali
//Root node connection
creeperHealingNode.addChild(settingsNode);

//Settings commands nodes
settingsNode.addChild(explosionHealDelayNode);
settingsNode.addChild(blockPlacementDelayNode);
//Preferences commands nodes
settingsNode.addChild(dropItemsOnCreeperExplosionsNode);
settingsNode.addChild(shouldHealOnFlowingWaterNode);
settingsNode.addChild(shouldHealOnSourceWaterNode);
Expand All @@ -198,8 +164,6 @@ public static void register(LiteralCommandNode<ServerCommandSource> creeperHeali
settingsNode.addChild(enableWhitelistNode);

//Argument nodes
explosionHealDelayNode.addChild(explosionHealDelayArgumentNode);
blockPlacementDelayNode.addChild(blockPlacementDelayArgumentNode);
shouldHealOnFlowingWaterNode.addChild(healOnFlowingWaterArgumentNode);
shouldHealOnSourceWaterNode.addChild(healOnSourceWaterArgumentNode);
shouldHealOnFlowingLavaNode.addChild(healOnFlowingLavaArgumentNode);
Expand All @@ -212,27 +176,6 @@ public static void register(LiteralCommandNode<ServerCommandSource> creeperHeali

}

private static int setExplosionHealDelayCommand(CommandContext<ServerCommandSource> ctx) {
if(Math.round(Math.max(DoubleArgumentType.getDouble(ctx, "seconds"), 0) * 20L) != 0) {
DelaysConfig.setExplosionHealDelay(DoubleArgumentType.getDouble(ctx, "seconds"));
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));
}
return Command.SINGLE_SUCCESS;
}

private static int setBlockPlacementDelayCommand(CommandContext<ServerCommandSource> ctx) {
if (Math.round(Math.max(DoubleArgumentType.getDouble(ctx, "seconds"), 0) * 20L) != 0) {
DelaysConfig.setBlockPlacementDelay(DoubleArgumentType.getDouble(ctx, "seconds"));
AffectedBlock.updateAffectedBlocksTimers();
ctx.getSource().sendMessage(Text.literal("Block placement delay has been set to: " + DoubleArgumentType.getDouble(ctx, "seconds") + " second(s)"));
} else {
ctx.getSource().sendMessage(Text.literal("Cannot set block placement delay to a very low value").formatted(Formatting.RED));
}
return Command.SINGLE_SUCCESS;
}

private static int setHealOnFlowingWaterCommand(CommandContext<ServerCommandSource> ctx) {
PreferencesConfig.setHealOnFlowingWater(BoolArgumentType.getBool(ctx, "value"));
ctx.getSource().sendMessage(Text.literal("Heal on flowing water has been set to: " + BoolArgumentType.getBool(ctx, "value")));
Expand Down Expand Up @@ -287,16 +230,6 @@ private static int setEnableWhitelist(CommandContext<ServerCommandSource> ctx){
return Command.SINGLE_SUCCESS;
}

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

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

private static int getShouldHealOnFlowingWaterCommand(CommandContext<ServerCommandSource> ctx){
ctx.getSource().sendMessage(Text.literal("Heal on flowing water currently set to: " + PreferencesConfig.getHealOnFlowingWater()));
return Command.SINGLE_SUCCESS;
Expand Down

0 comments on commit 0989904

Please sign in to comment.