Skip to content

Commit

Permalink
Fix .give and add a test for it
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander01998 committed Nov 25, 2024
1 parent c4770a5 commit 2b35c69
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/main/java/net/wurstclient/commands/GiveCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ private boolean placeStackInHotbar(ItemStack stack)
if(!MC.player.getInventory().getStack(i).isEmpty())
continue;

MC.player.getInventory().main.set(i, stack);
MC.player.networkHandler.sendPacket(
new CreativeInventoryActionC2SPacket(36 + i, stack));
return true;
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/net/wurstclient/test/GiveCmdTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2014-2024 Wurst-Imperium and contributors.
*
* This source code is subject to the terms of the GNU General Public
* License, version 3. If a copy of the GPL was not distributed with this
* file, You can obtain one at: https://www.gnu.org/licenses/gpl-3.0.txt
*/
package net.wurstclient.test;

import static net.wurstclient.test.WurstClientTestHelper.*;

import net.minecraft.item.Items;

public enum GiveCmdTest
{
;

public static void testGiveCmd()
{
System.out.println("Testing .give command");
runChatCommand("clear");
runWurstCommand("give diamond");
assertOneItemInSlot(0, Items.DIAMOND);
runChatCommand("clear");
}
}
30 changes: 30 additions & 0 deletions src/main/java/net/wurstclient/test/WurstClientTestHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
import net.minecraft.client.resource.language.I18n;
import net.minecraft.client.tutorial.TutorialStep;
import net.minecraft.client.util.ScreenshotRecorder;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.wurstclient.WurstClient;

public enum WurstClientTestHelper
{
Expand Down Expand Up @@ -343,6 +346,7 @@ public static void clearChat()
*/
public static void runChatCommand(String command)
{
System.out.println("Running command: /" + command);
submitAndWait(mc -> {
ClientPlayNetworkHandler netHandler = mc.getNetworkHandler();

Expand All @@ -365,4 +369,30 @@ public static void runChatCommand(String command)
netHandler.sendChatCommand(command);
});
}

/**
* Runs the given Wurst command, or fails after 10 seconds.
*
* <p>
* Do not put a . at the start of the command.
*/
public static void runWurstCommand(String command)
{
System.out.println("Running command: ." + command);
submitAndWait(mc -> {
WurstClient.INSTANCE.getCmdProcessor().process(command);
});
}

public static void assertOneItemInSlot(int slot, Item item)
{
submitAndWait(mc -> {
ItemStack stack = mc.player.getInventory().getStack(slot);
if(!stack.isOf(item) || stack.getCount() != 1)
throw new RuntimeException(
"Expected 1 " + item.getName().getString() + " at slot "
+ slot + ", found " + stack.getCount() + " "
+ stack.getItem().getName().getString() + " instead");
});
}
}
3 changes: 2 additions & 1 deletion src/main/java/net/wurstclient/test/WurstE2ETestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ private void runTests()
System.out.println("Checking for broken mixins");
MixinEnvironment.getCurrentEnvironment().audit();

// TODO: Test some Wurst hacks
GiveCmdTest.testGiveCmd();
// TODO: Test more Wurst hacks

System.out.println("Opening inventory");
openInventory();
Expand Down

0 comments on commit 2b35c69

Please sign in to comment.