Skip to content

Commit

Permalink
Add tests for fix
Browse files Browse the repository at this point in the history
  • Loading branch information
senseiwells committed Sep 24, 2024
1 parent 9a7885b commit 04261ae
Showing 1 changed file with 59 additions and 1 deletion.
60 changes: 59 additions & 1 deletion src/testmod/java/eu/pb4/sgui/testmod/SGuiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.minecraft.block.Blocks;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.enchantment.Enchantments;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.item.WrittenBookItem;
Expand Down Expand Up @@ -648,6 +649,57 @@ private static int test11(CommandContext<ServerCommandSource> objectCommandConte
return 0;
}

private static int test12(CommandContext<ServerCommandSource> objectCommandContext) {
try {
var player = objectCommandContext.getSource().getPlayerOrThrow();
player.sendMessage(
Text.literal("Pickaxe should *only* be able to be swapped only to offhand, both in and out of inventory gui")
);

var hotbar = new HotbarGui(player);
var elements = new GuiElement[1];
elements[0] = new GuiElement(new ItemStack(Items.GOLDEN_PICKAXE), (a, type, c, gui) -> {
if (type != ClickType.OFFHAND_SWAP) {
return;
}
var offhand = gui.getSlot(9);
if (offhand == null || offhand.getItemStack().isEmpty()) {
gui.setSlot(9, elements[0].getItemStack());
elements[0].setItemStack(ItemStack.EMPTY);
} else if (elements[0].getItemStack().isEmpty()) {
elements[0].setItemStack(offhand.getItemStack());
gui.setSlot(9, ItemStack.EMPTY);
}
});
hotbar.setSlot(0, elements[0]);
hotbar.open();
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}

private static int test13(CommandContext<ServerCommandSource> objectCommandContext) {
try {
var player = objectCommandContext.getSource().getPlayerOrThrow();
player.getInventory().setStack(PlayerInventory.OFF_HAND_SLOT, new ItemStack(Items.DIAMOND));

var stack = new ItemStack(Items.GOLDEN_PICKAXE);
stack.set(
DataComponentTypes.CUSTOM_NAME,
Text.literal("Can't swap to offhand")
);

var gui = new SimpleGui(ScreenHandlerType.GENERIC_9X3, player, true);
gui.setTitle(Text.literal("Offhand item should be invisible in gui"));
gui.setSlot(0, stack);
gui.open();
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}

private static int snake(CommandContext<ServerCommandSource> objectCommandContext) {
try {
ServerPlayerEntity player = objectCommandContext.getSource().getPlayer();
Expand All @@ -660,7 +712,7 @@ private static int snake(CommandContext<ServerCommandSource> objectCommandContex
}


public void onInitialize() {
public void onInitialize() {
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
dispatcher.register(
literal("test").executes(SGuiTest::test)
Expand Down Expand Up @@ -695,6 +747,12 @@ public void onInitialize() {
dispatcher.register(
literal("test11").executes(SGuiTest::test11)
);
dispatcher.register(
literal("test12").executes(SGuiTest::test12)
);
dispatcher.register(
literal("test13").executes(SGuiTest::test13)
);
dispatcher.register(
literal("snake").executes(SGuiTest::snake)
);
Expand Down

0 comments on commit 04261ae

Please sign in to comment.