|
| 1 | +package com.coflnet.mixin; |
| 2 | + |
| 3 | +import com.coflnet.config.AngryCoopProtectionManager; |
| 4 | +import net.minecraft.client.MinecraftClient; |
| 5 | +import net.minecraft.client.gui.screen.ingame.HandledScreen; |
| 6 | +import net.minecraft.component.DataComponentTypes; |
| 7 | +import net.minecraft.entity.player.PlayerInventory; |
| 8 | +import net.minecraft.item.ItemStack; |
| 9 | +import net.minecraft.screen.slot.Slot; |
| 10 | +import net.minecraft.text.Text; |
| 11 | +import org.jetbrains.annotations.Nullable; |
| 12 | +import org.lwjgl.glfw.GLFW; |
| 13 | +import org.spongepowered.asm.mixin.Mixin; |
| 14 | +import org.spongepowered.asm.mixin.Shadow; |
| 15 | +import org.spongepowered.asm.mixin.injection.At; |
| 16 | +import org.spongepowered.asm.mixin.injection.Inject; |
| 17 | +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; |
| 18 | + |
| 19 | +import java.util.Locale; |
| 20 | +import java.util.Optional; |
| 21 | + |
| 22 | +@Mixin(HandledScreen.class) |
| 23 | +public abstract class AuctionProtectionMixin { |
| 24 | + |
| 25 | + private enum ScreenMode { |
| 26 | + SELLER, |
| 27 | + BIDDER |
| 28 | + } |
| 29 | + |
| 30 | + @Shadow @Nullable protected Slot focusedSlot; |
| 31 | + @Shadow public abstract @Nullable Slot getSlotAt(double x, double y); |
| 32 | + |
| 33 | + @Inject(method = "mouseClicked", at = @At("HEAD"), cancellable = true) |
| 34 | + private void onAngryCoopMouseClicked(double mouseX, double mouseY, int button, CallbackInfoReturnable<Boolean> cir) { |
| 35 | + try { |
| 36 | + if (!AngryCoopProtectionManager.isEnabled()) { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + if (button != 0 && button != 1) { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + MinecraftClient client = MinecraftClient.getInstance(); |
| 45 | + if (client.player == null) { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + HandledScreen<?> screen = (HandledScreen<?>) (Object) this; |
| 50 | + String screenTitle = stripFormatting(screen.getTitle().getString()); |
| 51 | + ScreenMode mode = determineScreenMode(screenTitle); |
| 52 | + if (mode == null) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + Slot clickedSlot = getSlotAt(mouseX, mouseY); |
| 57 | + if (clickedSlot == null || !clickedSlot.hasStack()) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + ItemStack clickedStack = clickedSlot.getStack(); |
| 62 | + if (clickedStack.isEmpty()) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + boolean ctrlPressed = GLFW.glfwGetKey(client.getWindow().getHandle(), GLFW.GLFW_KEY_LEFT_CONTROL) == GLFW.GLFW_PRESS |
| 67 | + || GLFW.glfwGetKey(client.getWindow().getHandle(), GLFW.GLFW_KEY_RIGHT_CONTROL) == GLFW.GLFW_PRESS; |
| 68 | + |
| 69 | + String playerNameLower = client.player.getGameProfile().getName().toLowerCase(Locale.ROOT); |
| 70 | + String clickedName = stripFormatting(clickedStack.getName().getString()).trim(); |
| 71 | + |
| 72 | + if (isClaimAll(clickedName)) { |
| 73 | + if (!ctrlPressed && hasForeignEntry(screen, playerNameLower, client.player.getInventory(), mode)) { |
| 74 | + client.player.sendMessage(Text.literal(getClaimAllMessage(mode)), false); |
| 75 | + cir.setReturnValue(true); |
| 76 | + } |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + Optional<String> foreignActor = getForeignActor(clickedStack, playerNameLower, mode); |
| 81 | + if (foreignActor.isEmpty()) { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + if (ctrlPressed) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + client.player.sendMessage(Text.literal(getBlockedClickMessage(mode, foreignActor.get())), false); |
| 90 | + cir.setReturnValue(true); |
| 91 | + } catch (Exception e) { |
| 92 | + System.out.println("[AuctionProtectionMixin] mouseClicked failed: " + e.getMessage()); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + private boolean isClaimAll(String name) { |
| 97 | + return name.trim().equalsIgnoreCase("Claim All"); |
| 98 | + } |
| 99 | + |
| 100 | + private ScreenMode determineScreenMode(String title) { |
| 101 | + String lower = title.toLowerCase(Locale.ROOT); |
| 102 | + if (lower.contains("manage auctions")) { |
| 103 | + return ScreenMode.SELLER; |
| 104 | + } |
| 105 | + if (lower.contains("your bids")) { |
| 106 | + return ScreenMode.BIDDER; |
| 107 | + } |
| 108 | + return null; |
| 109 | + } |
| 110 | + |
| 111 | + private Optional<String> getForeignActor(ItemStack stack, String playerNameLower, ScreenMode mode) { |
| 112 | + var loreComponent = stack.get(DataComponentTypes.LORE); |
| 113 | + if (loreComponent == null) { |
| 114 | + return Optional.empty(); |
| 115 | + } |
| 116 | + |
| 117 | + for (Text line : loreComponent.lines()) { |
| 118 | + String raw = stripFormatting(line.getString()); |
| 119 | + String lower = raw.toLowerCase(Locale.ROOT); |
| 120 | + |
| 121 | + if (mode == ScreenMode.SELLER) { |
| 122 | + if (lower.contains("this is your own auction")) { |
| 123 | + return Optional.empty(); |
| 124 | + } |
| 125 | + |
| 126 | + if (lower.startsWith("seller:")) { |
| 127 | + if (!lower.contains(playerNameLower)) { |
| 128 | + String seller = raw.substring("Seller:".length()).trim(); |
| 129 | + return Optional.of(seller.isEmpty() ? "Unknown" : seller); |
| 130 | + } else { |
| 131 | + return Optional.empty(); |
| 132 | + } |
| 133 | + } |
| 134 | + } else { |
| 135 | + if (lower.startsWith("bidder:")) { |
| 136 | + if (lower.startsWith("bidder: you")) { |
| 137 | + return Optional.empty(); |
| 138 | + } |
| 139 | + if (!lower.contains(playerNameLower)) { |
| 140 | + String bidder = raw.substring("Bidder:".length()).trim(); |
| 141 | + return Optional.of(bidder.isEmpty() ? "Unknown" : bidder); |
| 142 | + } else { |
| 143 | + return Optional.empty(); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + if (lower.contains("you are the highest bidder")) { |
| 148 | + return Optional.empty(); |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + return Optional.empty(); |
| 154 | + } |
| 155 | + |
| 156 | + private boolean hasForeignEntry(HandledScreen<?> screen, String playerNameLower, PlayerInventory playerInventory, ScreenMode mode) { |
| 157 | + for (Slot slot : screen.getScreenHandler().slots) { |
| 158 | + if (slot.inventory == playerInventory) { |
| 159 | + continue; |
| 160 | + } |
| 161 | + |
| 162 | + if (!slot.hasStack()) { |
| 163 | + continue; |
| 164 | + } |
| 165 | + |
| 166 | + if (getForeignActor(slot.getStack(), playerNameLower, mode).isPresent()) { |
| 167 | + return true; |
| 168 | + } |
| 169 | + } |
| 170 | + return false; |
| 171 | + } |
| 172 | + |
| 173 | + private String getClaimAllMessage(ScreenMode mode) { |
| 174 | + String reason = mode == ScreenMode.SELLER |
| 175 | + ? "auctions listed by co-op members" |
| 176 | + : "bids placed by co-op members"; |
| 177 | + return "§c[SkyCofl Angry Coop] §fBlocked Claim All because " + reason + " were detected. Hold §bCtrl§f to override."; |
| 178 | + } |
| 179 | + |
| 180 | + private String getBlockedClickMessage(ScreenMode mode, String foreignName) { |
| 181 | + String action = mode == ScreenMode.SELLER ? "listed by" : "bid on by"; |
| 182 | + return "§c[SkyCofl Angry Coop] §fBlocked claiming auction " + action + " §e" + foreignName + "§f. Hold §bCtrl§f to override."; |
| 183 | + } |
| 184 | + |
| 185 | + private String stripFormatting(String text) { |
| 186 | + if (text == null) { |
| 187 | + return ""; |
| 188 | + } |
| 189 | + |
| 190 | + StringBuilder builder = new StringBuilder(text.length()); |
| 191 | + boolean skip = false; |
| 192 | + for (int i = 0; i < text.length(); i++) { |
| 193 | + char c = text.charAt(i); |
| 194 | + if (c == '§') { |
| 195 | + skip = true; |
| 196 | + continue; |
| 197 | + } |
| 198 | + if (skip) { |
| 199 | + skip = false; |
| 200 | + continue; |
| 201 | + } |
| 202 | + builder.append(c); |
| 203 | + } |
| 204 | + return builder.toString(); |
| 205 | + } |
| 206 | +} |
0 commit comments