Skip to content

Commit

Permalink
add fillContainer debug command,
Browse files Browse the repository at this point in the history
make item grid scale configurable

closes #301
  • Loading branch information
deirn committed Dec 5, 2024
1 parent 7727d33 commit 1fa30f1
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package mcp.mobius.waila.fabric;

import mcp.mobius.waila.command.ServerCommand;
import net.fabricmc.fabric.api.transfer.v1.item.ItemStorage;
import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant;
import net.fabricmc.fabric.api.transfer.v1.transaction.Transaction;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import org.jetbrains.annotations.Nullable;

public class FabricServerCommand extends ServerCommand {

@Override
protected @Nullable String fillContainer(ServerLevel world, BlockPos pos, ServerPlayer player) {
var storage = ItemStorage.SIDED.find(world, pos, Direction.UP);
if (storage == null) return "No storage at " + pos.toShortString();

try (var tx = Transaction.openOuter()) {
while (true) {
var offHandStack = player.getOffhandItem();
var item = !offHandStack.isEmpty()
? offHandStack.getItem()
: BuiltInRegistries.ITEM.getRandom(world.random).orElseThrow().value();

if (storage.insert(ItemVariant.of(item), item.getDefaultMaxStackSize(), tx) == 0L) break;
}
tx.commit();
return null;
}
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package mcp.mobius.waila.fabric;

import mcp.mobius.waila.Waila;
import mcp.mobius.waila.command.ServerCommand;
import mcp.mobius.waila.config.PluginConfig;
import mcp.mobius.waila.debug.DumpGenerator;
import mcp.mobius.waila.network.Packets;
Expand All @@ -27,7 +26,7 @@ public void onInitialize() {
Packets.initServer();

CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) ->
new ServerCommand().register(dispatcher));
new FabricServerCommand().register(dispatcher));

ServerLifecycleEvents.SERVER_STARTING.register(server -> PluginConfig.reload());
ServerLifecycleEvents.SERVER_STOPPED.register(server -> onServerStopped());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package mcp.mobius.waila.neo;

import mcp.mobius.waila.command.ServerCommand;
import net.minecraft.core.BlockPos;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.capabilities.Capabilities;
import org.jetbrains.annotations.Nullable;

public class NeoServerCommand extends ServerCommand {

@Override
protected @Nullable String fillContainer(ServerLevel world, BlockPos pos, ServerPlayer player) {
var handler = world.getCapability(Capabilities.ItemHandler.BLOCK, pos, null);
if (handler == null) return "No storage at " + pos.toShortString();
var offHandStack = player.getOffhandItem();

var size = handler.getSlots();
for (var i = 0; i < size; i++) {
var item = !offHandStack.isEmpty()
? offHandStack.getItem()
: BuiltInRegistries.ITEM.getRandom(world.random).orElseThrow().value();

handler.insertItem(i, new ItemStack(item, item.getDefaultMaxStackSize()), false);
}

return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import mcp.mobius.waila.Waila;
import mcp.mobius.waila.api.WailaConstants;
import mcp.mobius.waila.command.ServerCommand;
import mcp.mobius.waila.config.PluginConfig;
import mcp.mobius.waila.debug.DumpGenerator;
import mcp.mobius.waila.network.Packets;
Expand Down Expand Up @@ -60,7 +59,7 @@ static void tagReload(TagsUpdatedEvent event) {

@SubscribeEvent
static void registerCommands(RegisterCommandsEvent event) {
new ServerCommand().register(event.getDispatcher());
new NeoServerCommand().register(event.getDispatcher());
}

}
Expand Down
23 changes: 18 additions & 5 deletions src/api/java/mcp/mobius/waila/api/component/ItemListComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,45 +20,58 @@ public ItemListComponent(List<ItemStack> items) {
}

public ItemListComponent(List<ItemStack> items, int maxHeight) {
this(items, maxHeight, 1f);
}

public ItemListComponent(List<ItemStack> items, int maxHeight, float scale) {
this.items = items;
this.maxHeight = maxHeight;
this.scale = scale;
}

private final List<ItemStack> items;
private final int maxHeight;
private final float scale;

private int gridWidth;
private int gridHeight;
private int maxIndex;

@Override
public int getMinimalWidth() {
return Math.min(items.size(), 9) * 18;
return (int) (Math.min(items.size(), 9) * 18 * scale);
}

@Override
public void setGrownWidth(int grownWidth) {
gridWidth = grownWidth / 18;
gridWidth = Mth.ceil(grownWidth / (18 * scale));
gridHeight = items.isEmpty() ? 0 : Math.min(Mth.positiveCeilDiv(items.size(), gridWidth), maxHeight);
maxIndex = gridWidth * gridHeight - 1;
}

@Override
public int getHeight() {
return gridHeight * 18;
return Mth.ceil(gridHeight * 18 * scale);
}

@Override
public void render(GuiGraphics ctx, int x, int y, DeltaTracker delta) {
var pose = ctx.pose();
pose.pushPose();
pose.translate(x, y, 0);
pose.scale(scale, scale, 0f);

for (var i = 0; i < items.size(); i++) {
var item = items.get(i);
var ix = x + (18 * (i % gridWidth)) + 1;
var iy = y + (18 * (i / gridWidth)) + 1;
var ix = (18 * (i % gridWidth)) + 1;
var iy = (18 * (i / gridWidth)) + 1;
ctx.renderItem(item, ix, iy);
ItemComponent.renderItemDecorations(ctx, item, ix, iy);

if (i == maxIndex) break;
}

pose.popPose();
}

}
1 change: 1 addition & 0 deletions src/api/java/mcp/mobius/waila/api/data/ItemData.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public abstract class ItemData implements IData {
public static final ResourceLocation CONFIG_DISPLAY_MODE = BuiltinDataUtil.rl("item.display_mode");
public static final ResourceLocation CONFIG_MAX_HEIGHT = BuiltinDataUtil.rl("item.max_height");
public static final ResourceLocation CONFIG_SORT_BY_COUNT = BuiltinDataUtil.rl("item.sort_by_count");
public static final ResourceLocation CONFIG_GRID_MODE_SCALE = BuiltinDataUtil.rl("item.grid_mode_scale");

public enum ItemDisplayMode {
GRID, LIST, DYNAMIC
Expand Down
26 changes: 25 additions & 1 deletion src/main/java/mcp/mobius/waila/command/ServerCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,26 @@
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.commands.arguments.coordinates.BlockPosArgument;
import net.minecraft.core.BlockPos;
import net.minecraft.core.component.DataComponentPredicate;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.ClickEvent;
import net.minecraft.network.chat.Component;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.LockCode;
import org.jetbrains.annotations.Nullable;

public class ServerCommand extends CommonCommand<CommandSourceStack, MinecraftServer> {
public abstract class ServerCommand extends CommonCommand<CommandSourceStack, MinecraftServer> {

public ServerCommand() {
super(WailaConstants.NAMESPACE);
}

protected abstract @Nullable String fillContainer(ServerLevel world, BlockPos pos, ServerPlayer player);

@Override
protected boolean pluginCommandRequirement(CommandSourceStack source) {
return source.hasPermission(Commands.LEVEL_ADMINS);
Expand Down Expand Up @@ -138,6 +143,25 @@ protected void register(ArgumentBuilderBuilder<CommandSourceStack> command) {
})
.pop("pos", "lockContainer")

.then(Commands.literal("fillContainer"))
.then(Commands.argument("pos", BlockPosArgument.blockPos()))
.executes(context -> {
var source = context.getSource();
var world = source.getLevel();
var player = source.getPlayer();
var pos = BlockPosArgument.getLoadedBlockPos(context, "pos");

var err = fillContainer(world, pos, player);
if (err != null) {
source.sendFailure(Component.literal(err));
return 0;
} else {
source.sendSuccess(() -> Component.literal("Filled " + pos.toShortString()), false);
return 1;
}
})
.pop("pos", "fillContainer")

.pop("debug");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ protected void registerAdditions(ICommonRegistrar registrar, int priority) {
registrar.localConfig(ItemData.CONFIG_DISPLAY_MODE, ItemData.ItemDisplayMode.DYNAMIC);
registrar.localConfig(ItemData.CONFIG_MAX_HEIGHT, 3);
registrar.localConfig(ItemData.CONFIG_SORT_BY_COUNT, true);
registrar.localConfig(ItemData.CONFIG_GRID_MODE_SCALE, 1f);
}

@Override
Expand Down Expand Up @@ -96,12 +97,13 @@ protected void appendBody(ITooltip tooltip, ItemDataImpl data, IPluginConfig con

var list = stream.toList();
var maxHeight = config.getInt(ItemData.CONFIG_MAX_HEIGHT);
var scale = (float) config.getDouble(ItemData.CONFIG_GRID_MODE_SCALE);

lastItemsComponent = switch (config.<ItemData.ItemDisplayMode>getEnum(ItemData.CONFIG_DISPLAY_MODE)) {
case DYNAMIC -> list.size() <= maxHeight
? new NamedItemListComponent(list, maxHeight)
: new ItemListComponent(list, maxHeight);
case GRID -> new ItemListComponent(list, maxHeight);
: new ItemListComponent(list, maxHeight, scale);
case GRID -> new ItemListComponent(list, maxHeight, scale);
case LIST -> new NamedItemListComponent(list, maxHeight);
};

Expand Down
1 change: 1 addition & 0 deletions src/resources/resources/assets/waila/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@
"config.waila.plugin_wailax.item.display_mode_dynamic" : "Dynamic",
"config.waila.plugin_wailax.item.max_height" : "Max Height",
"config.waila.plugin_wailax.item.sort_by_count" : "Sort by Count",
"config.waila.plugin_wailax.item.grid_mode_scale" : "Grid Mode Scale",
"config.waila.plugin_wailax.item.blacklist" : "Item Contents Blacklist",
"config.waila.plugin_wailax.fluid" : "Fluid",
"config.waila.plugin_wailax.fluid.enabled_block" : "Show Block Fluid Contents",
Expand Down

0 comments on commit 1fa30f1

Please sign in to comment.