From 7536edc2b944faafbcce7aa80c0d30d43f3b7cc9 Mon Sep 17 00:00:00 2001 From: Ghoulboy Date: Tue, 18 May 2021 00:32:57 +0200 Subject: [PATCH 1/3] Adding Javadocs to CounterCommand.java and relevant files (#838) * adding class javadoc Co-authored-by: gnembon <41132274+gnembon@users.noreply.github.com> --- .../java/carpet/commands/CounterCommand.java | 24 +++++++ .../carpet/fakes/IngredientInterface.java | 4 ++ .../carpet/fakes/RecipeManagerInterface.java | 5 ++ .../java/carpet/helpers/HopperCounter.java | 72 +++++++++++++++++++ .../carpet/mixins/HopperBlockEntityMixin.java | 8 ++- src/main/java/carpet/utils/WoolTool.java | 23 ++++++ 6 files changed, 135 insertions(+), 1 deletion(-) diff --git a/src/main/java/carpet/commands/CounterCommand.java b/src/main/java/carpet/commands/CounterCommand.java index 18ad4481..973bfa01 100644 --- a/src/main/java/carpet/commands/CounterCommand.java +++ b/src/main/java/carpet/commands/CounterCommand.java @@ -11,8 +11,15 @@ import net.minecraft.server.command.CommandManager; import net.minecraft.util.DyeColor; import net.minecraft.text.BaseText; +/** + * Class for the /counter command which allows to use hoppers pointing into wool + */ + public class CounterCommand { + /** + * The method used to register the command and make it available for the players to use. + */ public static void register(CommandDispatcher dispatcher) { LiteralArgumentBuilder literalargumentbuilder = CommandManager.literal("counter").executes((context) @@ -37,6 +44,13 @@ public class CounterCommand dispatcher.register(literalargumentbuilder); } + /** + * A method to prettily display the contents of a counter to the player + * @param color The counter colour whose contents we are querying. + * @param realtime Whether or not to display it as in-game time or IRL time, which accounts for less than 20TPS which + * would make it slower than IRL + */ + private static int displayCounter(ServerCommandSource source, String color, boolean realtime) { HopperCounter counter = HopperCounter.getCounter(color); @@ -49,6 +63,11 @@ 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(ServerCommandSource source, String color) { if (color == null) @@ -66,6 +85,11 @@ public class CounterCommand return 1; } + /** + * A method to prettily display all the counters to the player + * @param realtime Whether or not to display it as in-game time or IRL time, which accounts for less than 20TPS which + * would make it slower than IRL + */ private static int listAllCounters(ServerCommandSource source, boolean realtime) { for (BaseText message: HopperCounter.formatAll(source.getMinecraftServer(), realtime)) diff --git a/src/main/java/carpet/fakes/IngredientInterface.java b/src/main/java/carpet/fakes/IngredientInterface.java index d910883e..76f4fff2 100644 --- a/src/main/java/carpet/fakes/IngredientInterface.java +++ b/src/main/java/carpet/fakes/IngredientInterface.java @@ -7,5 +7,9 @@ import java.util.List; public interface IngredientInterface { + /** + * Gets all the stacks of the ingredients for a given item recipe. Also used for {@link carpet.helpers.HopperCounter#guessColor} + * to guess the colour of an item to display it prettily + */ List> getRecipeStacks(); } diff --git a/src/main/java/carpet/fakes/RecipeManagerInterface.java b/src/main/java/carpet/fakes/RecipeManagerInterface.java index 77f45d21..d61db8a1 100644 --- a/src/main/java/carpet/fakes/RecipeManagerInterface.java +++ b/src/main/java/carpet/fakes/RecipeManagerInterface.java @@ -1,5 +1,6 @@ package carpet.fakes; +import net.minecraft.item.Item; import net.minecraft.recipe.Recipe; import net.minecraft.recipe.RecipeType; import net.minecraft.util.Identifier; @@ -8,5 +9,9 @@ import java.util.List; public interface RecipeManagerInterface { + /** + * Gets all the recipes for a given item. Also used for {@link carpet.helpers.HopperCounter#guessColor} to guess the + * colour of an item to display it prettily + */ List> getAllMatching(RecipeType type, Identifier output); } diff --git a/src/main/java/carpet/helpers/HopperCounter.java b/src/main/java/carpet/helpers/HopperCounter.java index 0f6a02c3..9f0626d4 100644 --- a/src/main/java/carpet/helpers/HopperCounter.java +++ b/src/main/java/carpet/helpers/HopperCounter.java @@ -42,9 +42,22 @@ import java.util.Locale; import java.util.Map; import java.util.stream.Collectors; +/** + * The actual object residing in each hopper counter which makes them count the items and saves them. There is one for each + * colour in MC. + */ + public class HopperCounter { + /** + * A map of all the {@link HopperCounter} counters. + */ public static final Map COUNTERS; + + /** + * The default display colour of each item, which makes them look nicer when printing the counter contents to the chat + */ + public static final TextColor WHITE = TextColor.fromFormatting(Formatting.WHITE); static @@ -57,10 +70,29 @@ public class HopperCounter COUNTERS = Maps.immutableEnumMap(counterMap); } + /** + * The counter's colour, determined by the colour of wool it's pointing into + */ public final DyeColor color; + /** + * The string which is passed into {@link Messenger#m} which makes each counter name be displayed in the colour of + * that counter. + */ private final String prettyColour; + /** + * All the items stored within the counter, as a map of {@link Item} mapped to a {@code long} of the amount of items + * stored thus far of that item type. + */ private final Object2LongMap counter = new Object2LongLinkedOpenHashMap<>(); + /** + * The starting tick of the counter, used to calculate in-game time. Only initialised when the first item enters the + * counter + */ private long startTick; + /** + * The starting millisecond of the counter, used to calculate IRl time. Only initialised when the first item enters + * the counter + */ private long startMillis; // private PubSubInfoProvider pubSubProvider; @@ -71,6 +103,11 @@ public class HopperCounter this.prettyColour = WoolTool.Material2DyeName.getOrDefault(color.getMaterialColor(),"w ") + color.getName(); } + /** + * Method used to add items to the counter. Note that this is when the {@link HopperCounter#startTick} and + * {@link HopperCounter#startMillis} variables are initialised, so you can place the counters and then start the farm + * after all the collection is sorted out. + */ public void add(MinecraftServer server, ItemStack stack) { if (startTick < 0) @@ -83,6 +120,9 @@ public class HopperCounter // pubSubProvider.publish(); } + /** + * Resets the counter, clearing its items but keeping the clock running. + */ public void reset(MinecraftServer server) { counter.clear(); @@ -91,6 +131,10 @@ public class HopperCounter // pubSubProvider.publish(); } + /** + * Resets all counters, clearing their items. + * @param fresh Whether or not to start the clocks going immediately or later. + */ public static void resetAll(MinecraftServer server, boolean fresh) { for (HopperCounter counter : COUNTERS.values()) @@ -100,6 +144,9 @@ public class HopperCounter } } + /** + * Prints all the counters to chat, nicely formatted, and you can choose whether to diplay in in game time or IRL time + */ public static List formatAll(MinecraftServer server, boolean realtime) { List text = new ArrayList<>(); @@ -120,6 +167,10 @@ public class HopperCounter return text; } + /** + * Prints a single counter's contents and timings to chat, with the option to keep it short (so no item breakdown, + * only rates). Again, realtime displays IRL time as opposed to in game time. + */ public List format(MinecraftServer server, boolean realTime, boolean brief) { long ticks = Math.max(realTime ? (System.currentTimeMillis() - startMillis) / 50 : server.getWorld(World.OVERWORLD).getTime() - startTick, 1); //OW @@ -172,6 +223,10 @@ public class HopperCounter return items; } + /** + * Converts a colour to have a low brightness and uniform colour, so when it prints the items in different colours + * it's not too flashy and bright, but enough that it's not dull to look at. + */ public static int appropriateColor(int color) { if (color == 0) return MaterialColor.WHITE.color; @@ -184,6 +239,10 @@ public class HopperCounter return (r << 16) + (g << 8) + b; } + /** + * Maps items that don't get a good block to reference for colour, or those that colour is wrong to a number of blocks, so we can get their colours easily with the + * {@link Block#getDefaultMaterialColor()} method as these items have those same colours. + */ private static final ImmutableMap DEFAULTS = new ImmutableMap.Builder() .put(Items.DANDELION, Blocks.YELLOW_WOOL) .put(Items.POPPY, Blocks.RED_WOOL) @@ -269,6 +328,9 @@ public class HopperCounter //.put(Items.,Blocks.) .build(); + /** + * Gets the colour to print an item in when printing its count in a hopper counter. + */ public static TextColor fromItem(Item item) { if (DEFAULTS.containsKey(item)) return TextColor.fromRgb(appropriateColor(DEFAULTS.get(item).getDefaultMaterialColor().color)); @@ -292,6 +354,10 @@ public class HopperCounter return null; } + /** + * Guesses the item's colour from the item itself. It first calls {@link HopperCounter#fromItem} to see if it has a + * valid colour there, if not just makes a guess, and if that fails just returns null + */ public static TextColor guessColor(Item item) { TextColor direct = fromItem(item); @@ -319,6 +385,9 @@ public class HopperCounter return null; } + /** + * Returns the hopper counter from the colour name, if not null + */ public static HopperCounter getCounter(String color) { try @@ -332,6 +401,9 @@ public class HopperCounter } } + /** + * The total number of items in the counter + */ public long getTotalItems() { return counter.isEmpty()?0:counter.values().stream().mapToLong(Long::longValue).sum(); diff --git a/src/main/java/carpet/mixins/HopperBlockEntityMixin.java b/src/main/java/carpet/mixins/HopperBlockEntityMixin.java index 265eef8a..bf184a2b 100644 --- a/src/main/java/carpet/mixins/HopperBlockEntityMixin.java +++ b/src/main/java/carpet/mixins/HopperBlockEntityMixin.java @@ -16,6 +16,9 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import carpet.helpers.HopperCounter; import carpet.utils.WoolTool; +/** + * The {@link Mixin} which removes items in a hopper if it points into a wool counter, and calls {@link HopperCounter#add} + */ @Mixin(HopperBlockEntity.class) public abstract class HopperBlockEntityMixin extends LootableContainerBlockEntity { @@ -36,6 +39,9 @@ public abstract class HopperBlockEntityMixin extends LootableContainerBlockEntit @Shadow public abstract void setStack(int slot, ItemStack stack); + /** + * A method to remove items from hoppers pointing into wool and count them via {@link HopperCounter#add} method + */ @Inject(method = "insert", at = @At("HEAD"), cancellable = true) private void onInsert(CallbackInfoReturnable cir) { @@ -58,4 +64,4 @@ public abstract class HopperBlockEntityMixin extends LootableContainerBlockEntit } } } -} \ No newline at end of file +} diff --git a/src/main/java/carpet/utils/WoolTool.java b/src/main/java/carpet/utils/WoolTool.java index cbe6fdf6..ca1c33af 100644 --- a/src/main/java/carpet/utils/WoolTool.java +++ b/src/main/java/carpet/utils/WoolTool.java @@ -18,10 +18,23 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; +/** + * A series of utility functions and variables for dealing predominantly with hopper counters and determining which counter + * to add their items to, as well as helping dealing with carpet functionality. + */ public class WoolTool { + /** + * A map of the {@link MaterialColor} to the {@link DyeColor} which is used in {@link WoolTool#getWoolColorAtPosition} + * to get the colour of wool at a position. + */ private static final HashMap Material2Dye = new HashMap<>(); + /** + * A map of all the wool colours to their respective colours in the {@link Messenger#m} format so the name of the counter + * gets printed in colour. + */ + public static final HashMap Material2DyeName = new HashMap(){{ put(MaterialColor.WHITE, "w "); put(MaterialColor.ORANGE, "#F9801D "); @@ -49,6 +62,13 @@ public class WoolTool } } + /** + * The method which gets triggered when a player places a carpet, and decides what to do based on the carpet's colour: + *
    + *
  • Red - Resets the counter of the colour of wool underneath the carpet (if there is no wool, then nothing happens)
  • + *
  • Green - Prints the contents of the counter of the colour of wool underneath the carpet
  • + *
+ */ public static void carpetPlacedAction(DyeColor color, PlayerEntity placer, BlockPos pos, ServerWorld worldIn) { if (!CarpetSettings.carpets) @@ -113,6 +133,9 @@ public class WoolTool } } + /** + * Gets the colour of wool at the position, for hoppers to be able to decide whether to add their items to the global counter. + */ public static DyeColor getWoolColorAtPosition(World worldIn, BlockPos pos) { BlockState state = worldIn.getBlockState(pos); From f32b8e4050b219356648c958fec07fbd88c7e284 Mon Sep 17 00:00:00 2001 From: gnembon Date: Tue, 18 May 2021 01:06:55 -0300 Subject: [PATCH 2/3] semicolon fixes #863 --- src/main/resources/assets/carpet/scripts/shapes.scl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/assets/carpet/scripts/shapes.scl b/src/main/resources/assets/carpet/scripts/shapes.scl index adad5bcb..e15a065a 100644 --- a/src/main/resources/assets/carpet/scripts/shapes.scl +++ b/src/main/resources/assets/carpet/scripts/shapes.scl @@ -93,7 +93,7 @@ draw_prism(args)->( positions += _ + offset_vector * offset ) ); - ) + ); positions ); From edc90133b2418f1ac4aeaf01017388470f2ed4ea Mon Sep 17 00:00:00 2001 From: gnembon Date: Tue, 18 May 2021 21:18:53 -0300 Subject: [PATCH 3/3] fix run position, fixes #866 --- src/main/java/carpet/script/api/Auxiliary.java | 10 +++++----- .../java/carpet/script/utils/SnoopyCommandSource.java | 11 ++++++----- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/main/java/carpet/script/api/Auxiliary.java b/src/main/java/carpet/script/api/Auxiliary.java index b3a95b65..70a34283 100644 --- a/src/main/java/carpet/script/api/Auxiliary.java +++ b/src/main/java/carpet/script/api/Auxiliary.java @@ -600,16 +600,15 @@ public class Auxiliary { return new FormattedTextValue(Messenger.c(values.stream().map(Value::getString).toArray())); }); - expression.addContextFunction("run", 1, (c, t, lv) -> { - BlockPos target = ((CarpetContext)c).origin; - Vec3d posf = new Vec3d((double)target.getX()+0.5D, target.getY(),(double)target.getZ()+0.5D); + expression.addContextFunction("run", 1, (c, t, lv) -> + { ServerCommandSource s = ((CarpetContext)c).s; try { Text[] error = {null}; List output = new ArrayList<>(); Value retval = new NumericValue(s.getMinecraftServer().getCommandManager().execute( - new SnoopyCommandSource(s, posf, error, output), + new SnoopyCommandSource(s, error, output), lv.get(0).getString()) ); return ListValue.of( @@ -624,7 +623,8 @@ public class Auxiliary { } }); - expression.addContextFunction("save", 0, (c, t, lv) -> { + expression.addContextFunction("save", 0, (c, t, lv) -> + { ServerCommandSource s = ((CarpetContext)c).s; s.getMinecraftServer().getPlayerManager().saveAllPlayerData(); s.getMinecraftServer().save(true,true,true); diff --git a/src/main/java/carpet/script/utils/SnoopyCommandSource.java b/src/main/java/carpet/script/utils/SnoopyCommandSource.java index 3242f91d..5092c371 100644 --- a/src/main/java/carpet/script/utils/SnoopyCommandSource.java +++ b/src/main/java/carpet/script/utils/SnoopyCommandSource.java @@ -38,13 +38,13 @@ public class SnoopyCommandSource extends ServerCommandSource private final Text[] error; private final List chatOutput; - public SnoopyCommandSource(ServerCommandSource original, Vec3d pos, Text[] error, List chatOutput) + public SnoopyCommandSource(ServerCommandSource original, Text[] error, List chatOutput) { - super(CommandOutput.DUMMY, pos, Vec2f.ZERO, original.getWorld(), CarpetSettings.runPermissionLevel, + super(CommandOutput.DUMMY, original.getPosition(), original.getRotation(), original.getWorld(), CarpetSettings.runPermissionLevel, original.getName(), original.getDisplayName(), original.getMinecraftServer(), original.getEntity(), false, (ctx, succ, res) -> { }, EntityAnchorArgumentType.EntityAnchor.FEET); this.output = CommandOutput.DUMMY; - this.position = pos; + this.position = original.getPosition(); this.world = original.getWorld(); this.level = CarpetSettings.runPermissionLevel; this.simpleName = original.getName(); @@ -53,7 +53,7 @@ public class SnoopyCommandSource extends ServerCommandSource this.entity = original.getEntity(); this.resultConsumer = (ctx, succ, res) -> { }; this.entityAnchor = original.getEntityAnchor(); - this.rotation = Vec2f.ZERO; + this.rotation = original.getRotation(); this.error = error; this.chatOutput = chatOutput; } @@ -73,7 +73,7 @@ public class SnoopyCommandSource extends ServerCommandSource this.entity = player; this.resultConsumer = (ctx, succ, res) -> { }; this.entityAnchor = EntityAnchorArgumentType.EntityAnchor.FEET; - this.rotation = Vec2f.ZERO; + this.rotation = player.getRotationClient(); // not a client call really this.error = error; this.chatOutput = output; } @@ -158,6 +158,7 @@ public class SnoopyCommandSource extends ServerCommandSource return new SnoopyCommandSource(output, position, rotation, world, level, simpleName, name, server, entity, resultConsumer, entityAnchor, error, chatOutput); } + @Override public ServerCommandSource withLookingAt(Vec3d position) throws CommandSyntaxException { Vec3d vec3d = this.entityAnchor.positionAt(this); double d = position.x - vec3d.x;