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:
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.builder.LiteralArgumentBuilder;
|
||||
import net.minecraft.commands.CommandBuildContext;
|
||||
import net.minecraft.commands.CommandRuntimeException;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import net.minecraft.commands.Commands;
|
||||
import net.minecraft.network.chat.Component;
|
||||
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
|
||||
*/
|
||||
|
||||
public class CounterCommand
|
||||
{
|
||||
/**
|
||||
@@ -23,26 +22,24 @@ public class CounterCommand
|
||||
*/
|
||||
public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext commandBuildContext)
|
||||
{
|
||||
LiteralArgumentBuilder<CommandSourceStack> literalargumentbuilder = Commands.literal("counter").executes((context)
|
||||
-> listAllCounters(context.getSource(), false)).requires((player) ->
|
||||
CarpetSettings.hopperCounters);
|
||||
LiteralArgumentBuilder<CommandSourceStack> commandBuilder = literal("counter")
|
||||
.requires(c -> CarpetSettings.hopperCounters)
|
||||
.executes(c -> listAllCounters(c.getSource(), false))
|
||||
.then(literal("reset")
|
||||
.executes(c -> resetCounters(c.getSource())));
|
||||
|
||||
literalargumentbuilder.
|
||||
then((Commands.literal("reset").executes( (context)->
|
||||
resetCounter(context.getSource(), null))));
|
||||
for (DyeColor enumDyeColor: DyeColor.values())
|
||||
for (DyeColor dyeColor : DyeColor.values())
|
||||
{
|
||||
String color = enumDyeColor.toString();
|
||||
literalargumentbuilder.
|
||||
then((Commands.literal(color).executes( (context)-> displayCounter(context.getSource(), color, false))));
|
||||
literalargumentbuilder.then(Commands.literal(color).
|
||||
then(Commands.literal("reset").executes((context) ->
|
||||
resetCounter(context.getSource(), color))));
|
||||
literalargumentbuilder.then(Commands.literal(color).
|
||||
then(Commands.literal("realtime").executes((context) ->
|
||||
displayCounter(context.getSource(), color, true))));
|
||||
commandBuilder.then(
|
||||
literal(dyeColor.toString())
|
||||
.executes(c -> displayCounter(c.getSource(), dyeColor, false))
|
||||
.then(literal("reset")
|
||||
.executes(c -> resetCounter(c.getSource(), dyeColor)))
|
||||
.then(literal("realtime")
|
||||
.executes(c -> displayCounter(c.getSource(), dyeColor, true)))
|
||||
);
|
||||
}
|
||||
dispatcher.register(literalargumentbuilder);
|
||||
dispatcher.register(commandBuilder);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -52,10 +49,9 @@ public class CounterCommand
|
||||
* 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);
|
||||
if (counter == null) throw new CommandRuntimeException(Messenger.s("Unknown wool color: "+color));
|
||||
|
||||
for (Component message: counter.format(source.getServer(), realtime, false))
|
||||
{
|
||||
@@ -64,25 +60,22 @@ public class CounterCommand
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
private static int resetCounters(CommandSourceStack source)
|
||||
{
|
||||
HopperCounter.resetAll(source.getServer(), false);
|
||||
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);
|
||||
if (counter == null) throw new CommandRuntimeException(Messenger.s("Unknown wool color"));
|
||||
counter.reset(source.getServer());
|
||||
HopperCounter.getCounter(color).reset(source.getServer());
|
||||
Messenger.m(source, "w Restarted " + color + " counter");
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -99,5 +92,4 @@ public class CounterCommand
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -10,7 +10,6 @@ import it.unimi.dsi.fastutil.objects.Object2LongMap;
|
||||
import net.minecraft.ChatFormatting;
|
||||
import net.minecraft.core.Registry;
|
||||
import net.minecraft.core.RegistryAccess;
|
||||
import net.minecraft.core.registries.BuiltInRegistries;
|
||||
import net.minecraft.core.registries.Registries;
|
||||
import net.minecraft.network.chat.Component;
|
||||
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.Recipe;
|
||||
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.BeaconBeamBlock;
|
||||
import net.minecraft.world.level.block.Block;
|
||||
@@ -114,7 +112,7 @@ public class HopperCounter
|
||||
{
|
||||
if (startTick < 0)
|
||||
{
|
||||
startTick = server.getLevel(Level.OVERWORLD).getGameTime(); //OW
|
||||
startTick = server.overworld().getGameTime();
|
||||
startMillis = System.currentTimeMillis();
|
||||
}
|
||||
Item item = stack.getItem();
|
||||
@@ -128,7 +126,7 @@ public class HopperCounter
|
||||
public void reset(MinecraftServer server)
|
||||
{
|
||||
counter.clear();
|
||||
startTick = server.getLevel(Level.OVERWORLD).getGameTime(); //OW
|
||||
startTick = server.overworld().getGameTime();
|
||||
startMillis = System.currentTimeMillis();
|
||||
// pubSubProvider.publish();
|
||||
}
|
||||
@@ -175,7 +173,7 @@ public class HopperCounter
|
||||
*/
|
||||
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 (brief)
|
||||
@@ -391,6 +389,13 @@ public class HopperCounter
|
||||
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
|
||||
*/
|
||||
|
||||
@@ -107,8 +107,7 @@ public class WoolTool
|
||||
{
|
||||
DyeColor under = getWoolColorAtPosition(worldIn, pos.below());
|
||||
if (under == null) return;
|
||||
HopperCounter counter = HopperCounter.getCounter(under.toString());
|
||||
if (counter != null)
|
||||
HopperCounter counter = HopperCounter.getCounter(under);
|
||||
Messenger.send(placer, counter.format(worldIn.getServer(), false, false));
|
||||
}
|
||||
break;
|
||||
@@ -117,8 +116,7 @@ public class WoolTool
|
||||
{
|
||||
DyeColor under = getWoolColorAtPosition(worldIn, pos.below());
|
||||
if (under == null) return;
|
||||
HopperCounter counter = HopperCounter.getCounter(under.toString());
|
||||
if (counter == null) return;
|
||||
HopperCounter counter = HopperCounter.getCounter(under);
|
||||
counter.reset(placer.getServer());
|
||||
List<Component> res = new ArrayList<>();
|
||||
res.add(Messenger.s(String.format("%s counter reset",under.toString())));
|
||||
|
||||
Reference in new issue
Block a user