Skip to content

Commit b43ec3d

Browse files
committed
See changelog
1 parent a0e63bd commit b43ec3d

File tree

11 files changed

+93
-12
lines changed

11 files changed

+93
-12
lines changed

Changelog.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# Version v1.0.0
2+
* Bugfix:
3+
* Creative-Mode prevents restocking of items which could sometime be faulty due to creative inventory
4+
* Trading with an invalid villager no longer crashes instead logs error and stops replay
5+
* New Feature:
6+
* Enchanting now works in replays. **This however changes recording-file which makes old one unusable!**
7+
*
8+
19
# Version v0.5.3
210
### Only supported for minecraft versions 1.21.5 and upwards
311
* Bugfix:

src/main/java/net/jasper/mod/automation/InventoryAutomation.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import net.minecraft.screen.slot.SlotActionType;
1313
import net.minecraft.util.ActionResult;
1414
import net.minecraft.util.Hand;
15+
import net.minecraft.world.GameMode;
1516

1617
/**
1718
* This class is responsible for re-stacking items in the inventory when replaying
@@ -23,6 +24,10 @@ public class InventoryAutomation {
2324
public static void register() {
2425
inventoryTasks.register("inventoryTasks");
2526
UseBlockCallback.EVENT.register((player, world, hand, hitResult) -> {
27+
if (GameMode.CREATIVE.equals(player.getGameMode())) {
28+
return ActionResult.PASS;
29+
}
30+
2631
// If is disabled in settings do nothing
2732
if (!PlayerAutomaOptionsScreen.restackBlocksOption.getValue()) {
2833
return ActionResult.PASS;

src/main/java/net/jasper/mod/automation/PlayerRecorder.java

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@
1111
import net.jasper.mod.util.IOHelpers;
1212
import net.jasper.mod.util.Textures;
1313
import net.jasper.mod.util.ThumbnailHelpers;
14+
import net.jasper.mod.util.data.LookingDirection;
15+
import net.jasper.mod.util.data.Recording;
16+
import net.jasper.mod.util.data.SlotClick;
1417
import net.jasper.mod.util.data.TaskQueue;
15-
import net.jasper.mod.util.data.*;
1618
import net.jasper.mod.util.keybinds.Constants;
1719
import net.minecraft.client.MinecraftClient;
1820
import net.minecraft.client.gui.screen.Screen;
21+
import net.minecraft.client.gui.screen.ingame.EnchantmentScreen;
1922
import net.minecraft.client.gui.screen.ingame.MerchantScreen;
2023
import net.minecraft.client.option.KeyBinding;
2124
import net.minecraft.client.sound.PositionedSoundInstance;
@@ -63,6 +66,9 @@ public class PlayerRecorder {
6366
// Click on Trade in Villager Trading Screen (MerchantScreen)
6467
public static final Queue<Integer> lastVillagerTradeMade = new ConcurrentLinkedDeque<>();
6568

69+
// Click on Enchantment (EnchantmentScreen)
70+
public static final Queue<Integer> lastEnchantmentMade = new ConcurrentLinkedDeque<>();
71+
6672
public static void register() {
6773
// Always reset the PlayerRecorder if a Join event happens could be breaking the mod. Therefor leave it out now.
6874
// ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> PlayerRecorder.reset());
@@ -110,7 +116,8 @@ public static void register() {
110116
lastSlotClicked.poll(),
111117
client.currentScreen == null ? null : client.currentScreen.getClass(),
112118
lastCommandUsed.poll(),
113-
lastVillagerTradeMade.poll()
119+
lastVillagerTradeMade.poll(),
120+
lastEnchantmentMade.poll()
114121
);
115122
record.add(newEntry);
116123
});
@@ -124,6 +131,7 @@ public static void reset() {
124131
lastSlotClicked.clear();
125132
lastCommandUsed.clear();pressedModifiers.clear();
126133
lastVillagerTradeMade.clear();
134+
lastEnchantmentMade.clear();
127135
state = IDLE;
128136
}
129137

@@ -268,6 +276,7 @@ private static void startReplay(boolean looped, int loopCount) {
268276
Class<?> currentScreen = entry.currentScreen();
269277
String command = entry.command();
270278
Integer villagerTrade = entry.villagerTrade();
279+
Integer enchantmentMade = entry.enchantment();
271280

272281
// Replay Ticks
273282
tasks.add(() -> {
@@ -332,14 +341,29 @@ private static void startReplay(boolean looped, int loopCount) {
332341
try {
333342
MerchantScreen tradeScreen = (MerchantScreen) client.currentScreen;
334343
MerchantScreenAccessor accessor = (MerchantScreenAccessor) tradeScreen;
335-
accessor.setSelectedIndex(villagerTrade);
336-
accessor.setSyncRecipeIndexInvoker();
337-
// Button Sound not happening as button not clicked but immediate it
338-
client.getSoundManager().play(PositionedSoundInstance.master(SoundEvents.UI_BUTTON_CLICK, 1.0f));
344+
if (villagerTrade < tradeScreen.getScreenHandler().getRecipes().size()) {
345+
accessor.setSelectedIndex(villagerTrade);
346+
accessor.setSyncRecipeIndexInvoker();
347+
// Button Sound not happening as button not clicked but immediate it
348+
client.getSoundManager().play(PositionedSoundInstance.master(SoundEvents.UI_BUTTON_CLICK, 1.0f));
349+
} else {
350+
client.currentScreen = null;
351+
PlayerRecorder.stopReplay();
352+
ClientHelpers.writeToActionBar(Text.translatable("playerautoma.messages.error.clickSlotError"));
353+
}
339354
} catch (Exception e) {
340355
PlayerAutomaClient.LOGGER.warn("Villager Trade Click resulted in unexpected exception", e);
341356
}
342357
}
358+
359+
// Click enchantment if possible
360+
if (enchantmentMade != null && client.currentScreen instanceof EnchantmentScreen) {
361+
try {
362+
client.interactionManager.clickButton(((EnchantmentScreen) client.currentScreen).getScreenHandler().syncId, enchantmentMade);
363+
} catch (Exception e) {
364+
PlayerAutomaClient.LOGGER.warn("Enchantment Click resulted in unexpected exception", e);
365+
}
366+
}
343367
});
344368
}
345369

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package net.jasper.mod.mixins;
2+
3+
import net.jasper.mod.automation.PlayerRecorder;
4+
import net.minecraft.client.MinecraftClient;
5+
import net.minecraft.client.gui.screen.ingame.EnchantmentScreen;
6+
import net.minecraft.client.network.ClientPlayerInteractionManager;
7+
import org.spongepowered.asm.mixin.Mixin;
8+
import org.spongepowered.asm.mixin.injection.At;
9+
import org.spongepowered.asm.mixin.injection.Inject;
10+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
11+
12+
@Mixin(ClientPlayerInteractionManager.class)
13+
public class ClientPlayerInteractionManagerMixin {
14+
15+
@Inject(method = "clickButton", at = @At("HEAD"))
16+
private void mouseClicked(int syncId, int buttonId, CallbackInfo ci) {
17+
MinecraftClient client = MinecraftClient.getInstance();
18+
if (client.currentScreen instanceof EnchantmentScreen) {
19+
PlayerRecorder.lastEnchantmentMade.add(buttonId);
20+
}
21+
}
22+
}

src/main/java/net/jasper/mod/mixins/accessors/ScreenAccessor.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package net.jasper.mod.mixins.accessors;
22

3-
import com.google.common.collect.Lists;
43
import net.minecraft.client.gui.Drawable;
54
import net.minecraft.client.gui.screen.Screen;
65
import org.spongepowered.asm.mixin.Mixin;

src/main/java/net/jasper/mod/util/ClientHelpers.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.jasper.mod.util;
22

3+
import net.jasper.mod.automation.PlayerRecorder;
34
import net.jasper.mod.gui.option.PlayerAutomaOptionsScreen;
45
import net.jasper.mod.util.data.LookingDirection;
56
import net.jasper.mod.util.data.SlotClick;
@@ -60,7 +61,13 @@ public static void clickSlot(SlotClick click) {
6061
MinecraftClient client = MinecraftClient.getInstance();
6162
assert client.player != null;
6263
assert client.interactionManager != null;
63-
client.interactionManager.clickSlot(client.player.currentScreenHandler.syncId, click.slotId(), click.button(), click.actionType(), client.player);
64+
try {
65+
client.interactionManager.clickSlot(client.player.currentScreenHandler.syncId, click.slotId(), click.button(), click.actionType(), client.player);
66+
} catch(Exception e) {
67+
client.currentScreen = null;
68+
PlayerRecorder.stopReplay();
69+
writeToActionBar(Text.translatable("playerautoma.messages.error.clickSlotError"));
70+
}
6471
}
6572

6673
public static void writeToChat(Text message) {

src/main/java/net/jasper/mod/util/JsonHelpers.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public class JsonHelpers {
4141
private static final String THUMBNAIL_WIDTH = "width";
4242
private static final String THUMBNAIL_HEIGHT = "height";
4343
private static final String VILLAGER_TRADE = "villagerTrade";
44+
private static final String ENCHANTMENT_MADE = "enchantmentMade";
4445

4546

4647
public static String serialize(Recording r) {
@@ -124,6 +125,11 @@ public static String serialize(Recording r) {
124125
jsonEntry.addProperty(VILLAGER_TRADE, entry.villagerTrade());
125126
}
126127

128+
// Enchantment made
129+
if (entry.enchantment() != null) {
130+
jsonEntry.addProperty(ENCHANTMENT_MADE, entry.enchantment());
131+
}
132+
127133
}
128134
entries.add(jsonEntry);
129135
}
@@ -220,6 +226,11 @@ public static Recording deserialize(String s) {
220226
villagerTrade = jsonEntry.get(VILLAGER_TRADE).getAsInt();
221227
}
222228

229+
Integer enchantmentMade = null;
230+
if (jsonEntry.has(ENCHANTMENT_MADE)) {
231+
enchantmentMade = jsonEntry.get(ENCHANTMENT_MADE).getAsInt();
232+
}
233+
223234
Recording.RecordEntry entry = new Recording.RecordEntry(
224235
keysPressed,
225236
timesPressed,
@@ -229,7 +240,8 @@ public static Recording deserialize(String s) {
229240
slotclick,
230241
currentScreen,
231242
command,
232-
villagerTrade
243+
villagerTrade,
244+
enchantmentMade
233245
);
234246
result.add(entry);
235247
}

src/main/java/net/jasper/mod/util/data/Recording.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public record RecordEntry(
1818
SlotClick slotClicked,
1919
Class<?> currentScreen,
2020
String command,
21-
Integer villagerTrade
21+
Integer villagerTrade,
22+
Integer enchantment
2223
) implements Serializable {}
2324

2425
public final List<RecordEntry> entries;

src/main/resources/assets/playerautoma/lang/de_de.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,5 +148,6 @@
148148
"playerautoma.messages.error.cannotStopNotStartedReplay": "Kann keine Wiedergabe stoppen die nicht gestartet wurde",
149149
"playerautoma.messages.error.cannotStopNotStartedRecording": "Kann keine Aufnahme stoppen die nicht gestartet wurde",
150150
"playerautoma.messages.error.cannotNameRecordingThatWay": "Aufnahmename darf nur [a-z0-9/._-] Zeichen enthalten",
151-
"playerautoma.messages.error.unknownException": "Ein unbekannter Fehler ist aufgetreten. Bitte prüfe %s für zusätzliche Informationen"
151+
"playerautoma.messages.error.unknownException": "Ein unbekannter Fehler ist aufgetreten. Bitte prüfe %s für zusätzliche Informationen",
152+
"playerautoma.messages.error.clickSlotError": "Wiedergabe gestoppt, da es zu einem Fehler beim klicken eines Inventar- oder Menüslot kam",
152153
}

src/main/resources/assets/playerautoma/lang/en_us.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,5 +148,6 @@
148148
"playerautoma.messages.error.cannotStopNotStartedReplay": "Cannot stop replay that has not been started yet",
149149
"playerautoma.messages.error.cannotStopNotStartedRecording": "Cannot stop recording that has not been started yet",
150150
"playerautoma.messages.error.cannotNameRecordingThatWay": "Recording names can only contain [a-z0-9/._-] characters",
151-
"playerautoma.messages.error.unknownException": "An unknown Exception occurred. Please check %s for additional Information"
151+
"playerautoma.messages.error.unknownException": "An unknown Exception occurred. Please check %s for additional Information",
152+
"playerautoma.messages.error.clickSlotError": "Stopped replay due to an error while trying to click a slot"
152153
}

0 commit comments

Comments
 (0)