Skip to content

Commit da4523c

Browse files
committed
needs testing
general code cleanup, separate overlay rendering to a new class and softcode out some hardcoded checks
1 parent 45acc25 commit da4523c

File tree

8 files changed

+292
-295
lines changed

8 files changed

+292
-295
lines changed

src/main/java/dev/lycanea/mwonmod/Mwonmod.java

Lines changed: 2 additions & 234 deletions
Large diffs are not rendered by default.

src/main/java/dev/lycanea/mwonmod/events/AttackEvent.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import dev.lycanea.mwonmod.util.ItemUtils;
88
import org.jetbrains.annotations.Nullable;
99

10+
import java.util.List;
1011
import java.util.Objects;
1112
import net.minecraft.world.InteractionHand;
1213
import net.minecraft.world.InteractionResult;
@@ -16,12 +17,17 @@
1617
import net.minecraft.world.phys.EntityHitResult;
1718

1819
public class AttackEvent {
20+
static List<String> hoes = List.of(
21+
"hoe",
22+
"perfect_hoe",
23+
"divine_hoe",
24+
"royal_scythe");
25+
1926
public static InteractionResult entityAttack(Player playerEntity, Level world, InteractionHand hand, Entity entity, @Nullable EntityHitResult entityHitResult) {
2027
if (!Mwonmod.onMelonKing()) return InteractionResult.PASS;
21-
if (!Config.HANDLER.instance().preventAttackingWithHoe) return InteractionResult.PASS;
22-
if (!Objects.equals(GameState.playerLocation, "main_farm")) return InteractionResult.PASS;
23-
String itemId = ItemUtils.getItemID(playerEntity.getMainHandItem());
24-
if (Objects.equals(itemId, "hoe") || Objects.equals(itemId, "perfect_hoe") || Objects.equals(itemId, "divine_hoe") || Objects.equals(itemId, "royal_scythe")) {
28+
if (Config.HANDLER.instance().preventAttackingWithHoe
29+
&& Objects.equals(GameState.playerLocation, "main_farm")
30+
&& hoes.contains(ItemUtils.getItemID(playerEntity.getMainHandItem()))) {
2531
return InteractionResult.FAIL;
2632
}
2733
return InteractionResult.PASS;

src/main/java/dev/lycanea/mwonmod/events/SellEvent.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
import dev.lycanea.mwonmod.Mwonmod;
44
import dev.lycanea.mwonmod.Config;
55

6-
import net.kyori.adventure.key.Key;
7-
import net.kyori.adventure.sound.Sound;
86
import net.kyori.adventure.title.Title;
97
import net.minecraft.client.Minecraft;
8+
import net.minecraft.sounds.SoundEvents;
109
import net.minecraft.world.InteractionHand;
1110
import net.minecraft.world.InteractionResult;
1211
import net.minecraft.world.entity.Entity;
@@ -18,18 +17,22 @@
1817
import java.time.Duration;
1918
import java.time.temporal.ChronoUnit;
2019
import java.util.List;
21-
import java.util.Objects;
2220

2321
public class SellEvent {
22+
23+
static List<String> merchants = List.of(
24+
"empty[siblings=[empty[style={color=white}], literal{Merchant}[style={color=green}]]]",
25+
"empty[siblings=[empty[style={color=white}], literal{Salesman}[style={color=red}]]]",
26+
"empty[siblings=[empty[style={color=white}], literal{Salesman}[style={color=dark_aqua}]]]",
27+
"empty[siblings=[empty[style={color=white}], literal{Salesman}[style={color=gray}]]]");
28+
2429
public static InteractionResult entityInteract(Player playerEntity, Level world, InteractionHand hand, Entity entity, @Nullable EntityHitResult entityHitResult) {
25-
if (!Mwonmod.onMelonKing()) return InteractionResult.PASS;
26-
if (!Config.HANDLER.instance().preventFullInventorySelling) return InteractionResult.PASS;
30+
if (!Mwonmod.onMelonKing() || Minecraft.getInstance().player == null) return InteractionResult.PASS;
2731

2832
// if (Config.HANDLER.instance().debugMode) Mwonmod.LOGGER.info(String.valueOf(entity.getCustomName()));
29-
if (Objects.equals(String.valueOf(entity.getCustomName()), "empty[siblings=[empty[style={color=white}], literal{Merchant}[style={color=green}]]]") || Objects.equals(String.valueOf(entity.getCustomName()), "empty[siblings=[empty[style={color=white}], literal{Salesman}[style={color=red}]]]") || Objects.equals(String.valueOf(entity.getCustomName()), "empty[siblings=[empty[style={color=white}], literal{Salesman}[style={color=dark_aqua}]]]") || Objects.equals(String.valueOf(entity.getCustomName()), "empty[siblings=[empty[style={color=white}], literal{Salesman}[style={color=gray}]]]")) {
33+
if (Config.HANDLER.instance().preventFullInventorySelling && merchants.contains(String.valueOf(entity.getCustomName()))) {
3034
if (Mwonmod.scanInventory(playerEntity, List.of()).emptySlots() <= Config.HANDLER.instance().fullInvEmptySlots) {
31-
assert Minecraft.getInstance().player != null;
32-
Minecraft.getInstance().player.playSound(Sound.sound(Key.key("minecraft:entity.shulker.hurt_closed"), Sound.Source.MASTER, 1.0F, 1.0F));
35+
Minecraft.getInstance().player.makeSound(SoundEvents.SHULKER_HURT_CLOSED);
3336
Minecraft.getInstance().player.showTitle(Title.title(net.kyori.adventure.text.Component.text(""), net.kyori.adventure.text.Component.text("Inventory Full"), Title.Times.times(Duration.ZERO,Duration.of(500, ChronoUnit.MILLIS),Duration.ZERO)));
3437
return InteractionResult.FAIL;
3538
}

src/main/java/dev/lycanea/mwonmod/mixin/ItemDropMixin.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@
1818
public class ItemDropMixin {
1919
@Inject(method = "drop", at = @At("HEAD"), cancellable = true)
2020
public void dropSelectedItem(boolean entireStack, CallbackInfoReturnable<Boolean> cir) {
21-
if (Mwonmod.onMelonKing()) {
22-
LocalPlayer player = Minecraft.getInstance().player;
21+
if (!Mwonmod.onMelonKing()) return;
22+
LocalPlayer player = Minecraft.getInstance().player;
2323

24-
assert player != null;
25-
ItemStack handItem = player.getMainHandItem();
26-
handItem.getTooltipLines(Item.TooltipContext.EMPTY, player, TooltipFlag.NORMAL)
27-
.stream()
28-
.map(Component::getString)
29-
.forEach(str -> {
30-
if (Config.HANDLER.instance().preventDroppingReflectives && "Reflection".equals(str)) {
31-
player.makeSound(SoundEvents.SHIELD_BLOCK.value());
32-
cir.setReturnValue(false);
33-
cir.cancel();
34-
}
35-
});
36-
}
24+
if (player == null) return;
25+
26+
ItemStack handItem = player.getMainHandItem();
27+
handItem.getTooltipLines(Item.TooltipContext.EMPTY, player, TooltipFlag.NORMAL)
28+
.stream()
29+
.map(Component::getString)
30+
.forEach(str -> {
31+
if (Config.HANDLER.instance().preventDroppingReflectives && "Reflection".equals(str)) {
32+
player.makeSound(SoundEvents.SHIELD_BLOCK.value());
33+
cir.setReturnValue(false);
34+
cir.cancel();
35+
}
36+
});
3737
}
3838
}

src/main/java/dev/lycanea/mwonmod/mixin/OnPacketMixin.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
@Mixin(Connection.class)
3030
public class OnPacketMixin {
3131
@Inject(method = "genericsFtw", at = @At("HEAD"), cancellable = true)
32-
private static <T extends PacketListener> void onGameMessage(Packet<T> packet, PacketListener listener, CallbackInfo ci) {
32+
private static <T extends PacketListener> void onGameMessage(Packet<@org.jetbrains.annotations.NotNull T> packet, PacketListener listener, CallbackInfo ci) {
3333
if (packet instanceof ClientboundSystemChatPacket(Component content, boolean overlay)) {
3434
handleChatPacket(content, overlay, ci);
3535
}
@@ -77,7 +77,7 @@ private static void handleChatPacket(Component content, boolean overlay, Callbac
7777
GameState.melonJoin = LocalDateTime.now();
7878
}
7979

80-
if (!Mwonmod.onMelonKing()) return;
80+
if (!Mwonmod.onMelonKing() || Minecraft.getInstance().player == null) return;
8181
if (message.equals("» All combatants perished, so the battle was lost.")) {
8282
BossState.updateBoss(null);
8383
}
@@ -89,22 +89,21 @@ private static void handleChatPacket(Component content, boolean overlay, Callbac
8989
if (Config.HANDLER.instance().hideSellFailMessage && Objects.equals(message, "> You don't have any Super Enchanted Melons. Get them by cooking four Enchanted Melon Slices, which are gotten by cooking four Melon Slices.")) {
9090
ci.cancel();
9191
}
92-
if (Config.HANDLER.instance().what && Objects.equals(message, "> What?") && Minecraft.getInstance().player != null) {
92+
if (Config.HANDLER.instance().what && Objects.equals(message, "> What?")) {
9393
Mwonmod.notification("> What?", "> What?");
9494
Minecraft.getInstance().execute(() -> Minecraft.getInstance().player.connection.sendChat("> What?"));
9595
}
96-
if (Config.HANDLER.instance().down && Objects.equals(message, "> Down.") && Minecraft.getInstance().player != null) {
96+
if (Config.HANDLER.instance().down && Objects.equals(message, "> Down.")) {
9797
Mwonmod.notification("> Down.", "> Down.");
9898
Minecraft.getInstance().execute(() -> Minecraft.getInstance().player.connection.sendChat("> Down."));
9999
}
100100
Pattern auctionPattern = Pattern.compile("^>\\s*(?:First up,|And next,|Next,|And now,|And lastly,|Now,|Up next,)?\\s*(?:a|an|some)\\s+(.+?)!$");
101-
Pattern newKingPattern = Pattern.compile("^>\\s*([\\w]+)\\s+is\\s+the\\s+new\\s+(?:king|queen|monarch)!$");
101+
Pattern newKingPattern = Pattern.compile("^>\\s*(\\w+)\\s+is\\s+the\\s+new\\s+(?:king|queen|monarch)!$");
102102

103103
Matcher auctionMatcher = auctionPattern.matcher(message);
104104
Matcher kingMatcher = newKingPattern.matcher(message);
105105

106106
if (auctionMatcher.find()) {
107-
assert Minecraft.getInstance().player != null;
108107
if (Config.HANDLER.instance().debugMode)
109108
Minecraft.getInstance().player.displayClientMessage(net.minecraft.network.chat.Component.literal("Auction Item: " + auctionMatcher.group(1)), false);
110109
try {
@@ -122,7 +121,6 @@ private static void handleChatPacket(Component content, boolean overlay, Callbac
122121
Minecraft.getInstance().player.displayClientMessage(net.minecraft.network.chat.Component.literal("Error accessing item data: " + e.getMessage()), false);
123122
}
124123
} else if (kingMatcher.find()) {
125-
assert Minecraft.getInstance().player != null;
126124
if (Config.HANDLER.instance().kingChangeNotification) {
127125
GameState.bank_gold = 0;
128126
GameState.coins = 0;

src/main/java/dev/lycanea/mwonmod/mixin/ScoreboardRendering.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818

1919
@Mixin(Gui.class)
2020
public class ScoreboardRendering {
21+
2122
@Shadow
2223
@Final
2324
private Minecraft minecraft;
25+
2426
@Unique
2527
java.text.NumberFormat formatter = java.text.NumberFormat.getNumberInstance();
28+
2629
@Inject(method = "renderScoreboardSidebar(Lnet/minecraft/client/gui/GuiGraphics;Lnet/minecraft/client/DeltaTracker;)V", at = @At("HEAD"))
2730
private void onRenderScoreboard(GuiGraphics guiGraphics, DeltaTracker deltaTracker, CallbackInfo ci) {
2831
if (this.minecraft.level == null || !Mwonmod.onMelonKing()) return;

0 commit comments

Comments
 (0)