Cleanup counter command (#1695)

- Import `Commands.literal`
- Use more readable syntax for Brigadier command
- Use `DyeColor` directly instead of toString -> fromString everywhere
  - Also updates `WoolTool` to use the new `HopperCounter` getter that supports it
- Split `resetCounter` in two, one for single and one for all

Also: HopperCounter now uses `.overworld()` and its `//OW` comments are removed
This commit is contained in:
altrisi authored and GitHub committed 2023-04-08 20:14:26 +02:00
1 parent 8cd60db5e4
commit 51f213fa15
3 files changed
+40 -45

No files matched your search

@@ -6,16 +6,15 @@ import carpet.utils.Messenger;
import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.minecraft.commands.CommandBuildContext; import net.minecraft.commands.CommandBuildContext;
import net.minecraft.commands.CommandRuntimeException;
import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Component;
import net.minecraft.world.item.DyeColor; import net.minecraft.world.item.DyeColor;
import static net.minecraft.commands.Commands.literal;
/** /**
* Class for the /counter command which allows to use hoppers pointing into wool * Class for the /counter command which allows to use hoppers pointing into wool
*/ */
public class CounterCommand public class CounterCommand
{ {
/** /**
@@ -23,26 +22,24 @@ public class CounterCommand
*/ */
public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext commandBuildContext) public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext commandBuildContext)
{ {
LiteralArgumentBuilder<CommandSourceStack> literalargumentbuilder = Commands.literal("counter").executes((context) LiteralArgumentBuilder<CommandSourceStack> commandBuilder = literal("counter")
-> listAllCounters(context.getSource(), false)).requires((player) -> .requires(c -> CarpetSettings.hopperCounters)
CarpetSettings.hopperCounters); .executes(c -> listAllCounters(c.getSource(), false))
.then(literal("reset")
.executes(c -> resetCounters(c.getSource())));
literalargumentbuilder. for (DyeColor dyeColor : DyeColor.values())
then((Commands.literal("reset").executes( (context)->
resetCounter(context.getSource(), null))));
for (DyeColor enumDyeColor: DyeColor.values())
{ {
String color = enumDyeColor.toString(); commandBuilder.then(
literalargumentbuilder. literal(dyeColor.toString())
then((Commands.literal(color).executes( (context)-> displayCounter(context.getSource(), color, false)))); .executes(c -> displayCounter(c.getSource(), dyeColor, false))
literalargumentbuilder.then(Commands.literal(color). .then(literal("reset")
then(Commands.literal("reset").executes((context) -> .executes(c -> resetCounter(c.getSource(), dyeColor)))
resetCounter(context.getSource(), color)))); .then(literal("realtime")
literalargumentbuilder.then(Commands.literal(color). .executes(c -> displayCounter(c.getSource(), dyeColor, true)))
then(Commands.literal("realtime").executes((context) -> );
displayCounter(context.getSource(), color, true))));
} }
dispatcher.register(literalargumentbuilder); dispatcher.register(commandBuilder);
} }
/** /**
@@ -52,10 +49,9 @@ public class CounterCommand
* would make it slower than IRL * would make it slower than IRL
*/ */
private static int displayCounter(CommandSourceStack source, String color, boolean realtime) private static int displayCounter(CommandSourceStack source, DyeColor color, boolean realtime)
{ {
HopperCounter counter = HopperCounter.getCounter(color); HopperCounter counter = HopperCounter.getCounter(color);
if (counter == null) throw new CommandRuntimeException(Messenger.s("Unknown wool color: "+color));
for (Component message: counter.format(source.getServer(), realtime, false)) for (Component message: counter.format(source.getServer(), realtime, false))
{ {
@@ -64,25 +60,22 @@ public class CounterCommand
return 1; return 1;
} }
/** private static int resetCounters(CommandSourceStack source)
* A method to reset the counter's timer to 0 and empty its items. If the {@code color} parameter is {@code null},
* it will reset all counters.
* @param color The counter whose contents we want to reset
*/
private static int resetCounter(CommandSourceStack source, String color)
{
if (color == null)
{ {
HopperCounter.resetAll(source.getServer(), false); HopperCounter.resetAll(source.getServer(), false);
Messenger.m(source, "w Restarted all counters"); Messenger.m(source, "w Restarted all counters");
return 1;
} }
else
/**
* A method to reset the counter's timer to 0 and empty its items
*
* @param color The counter whose contents we want to reset
*/
private static int resetCounter(CommandSourceStack source, DyeColor color)
{ {
HopperCounter counter = HopperCounter.getCounter(color); HopperCounter.getCounter(color).reset(source.getServer());
if (counter == null) throw new CommandRuntimeException(Messenger.s("Unknown wool color"));
counter.reset(source.getServer());
Messenger.m(source, "w Restarted " + color + " counter"); Messenger.m(source, "w Restarted " + color + " counter");
}
return 1; return 1;
} }
@@ -99,5 +92,4 @@ public class CounterCommand
} }
return 1; return 1;
} }
} }
@@ -10,7 +10,6 @@ import it.unimi.dsi.fastutil.objects.Object2LongMap;
import net.minecraft.ChatFormatting; import net.minecraft.ChatFormatting;
import net.minecraft.core.Registry; import net.minecraft.core.Registry;
import net.minecraft.core.RegistryAccess; import net.minecraft.core.RegistryAccess;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries; import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent; import net.minecraft.network.chat.MutableComponent;
@@ -27,7 +26,6 @@ import net.minecraft.world.item.Items;
import net.minecraft.world.item.crafting.Ingredient; import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.Recipe; import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.RecipeType; import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.AbstractBannerBlock; import net.minecraft.world.level.block.AbstractBannerBlock;
import net.minecraft.world.level.block.BeaconBeamBlock; import net.minecraft.world.level.block.BeaconBeamBlock;
import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Block;
@@ -114,7 +112,7 @@ public class HopperCounter
{ {
if (startTick < 0) if (startTick < 0)
{ {
startTick = server.getLevel(Level.OVERWORLD).getGameTime(); //OW startTick = server.overworld().getGameTime();
startMillis = System.currentTimeMillis(); startMillis = System.currentTimeMillis();
} }
Item item = stack.getItem(); Item item = stack.getItem();
@@ -128,7 +126,7 @@ public class HopperCounter
public void reset(MinecraftServer server) public void reset(MinecraftServer server)
{ {
counter.clear(); counter.clear();
startTick = server.getLevel(Level.OVERWORLD).getGameTime(); //OW startTick = server.overworld().getGameTime();
startMillis = System.currentTimeMillis(); startMillis = System.currentTimeMillis();
// pubSubProvider.publish(); // pubSubProvider.publish();
} }
@@ -175,7 +173,7 @@ public class HopperCounter
*/ */
public List<Component> format(MinecraftServer server, boolean realTime, boolean brief) public List<Component> format(MinecraftServer server, boolean realTime, boolean brief)
{ {
long ticks = Math.max(realTime ? (System.currentTimeMillis() - startMillis) / 50 : server.getLevel(Level.OVERWORLD).getGameTime() - startTick, 1); //OW long ticks = Math.max(realTime ? (System.currentTimeMillis() - startMillis) / 50 : server.overworld().getGameTime() - startTick, 1);
if (startTick < 0 || ticks == 0) if (startTick < 0 || ticks == 0)
{ {
if (brief) if (brief)
@@ -391,6 +389,13 @@ public class HopperCounter
return null; return null;
} }
/**
* Returns the hopper counter for the given color
*/
public static HopperCounter getCounter(DyeColor color) {
return COUNTERS.get(color);
}
/** /**
* Returns the hopper counter from the colour name, if not null * Returns the hopper counter from the colour name, if not null
*/ */
+2 -4
View File
@@ -107,8 +107,7 @@ public class WoolTool
{ {
DyeColor under = getWoolColorAtPosition(worldIn, pos.below()); DyeColor under = getWoolColorAtPosition(worldIn, pos.below());
if (under == null) return; if (under == null) return;
HopperCounter counter = HopperCounter.getCounter(under.toString()); HopperCounter counter = HopperCounter.getCounter(under);
if (counter != null)
Messenger.send(placer, counter.format(worldIn.getServer(), false, false)); Messenger.send(placer, counter.format(worldIn.getServer(), false, false));
} }
break; break;
@@ -117,8 +116,7 @@ public class WoolTool
{ {
DyeColor under = getWoolColorAtPosition(worldIn, pos.below()); DyeColor under = getWoolColorAtPosition(worldIn, pos.below());
if (under == null) return; if (under == null) return;
HopperCounter counter = HopperCounter.getCounter(under.toString()); HopperCounter counter = HopperCounter.getCounter(under);
if (counter == null) return;
counter.reset(placer.getServer()); counter.reset(placer.getServer());
List<Component> res = new ArrayList<>(); List<Component> res = new ArrayList<>();
res.add(Messenger.s(String.format("%s counter reset",under.toString()))); res.add(Messenger.s(String.format("%s counter reset",under.toString())));