|
| 1 | +/* |
| 2 | + * Copyright (c) 2014-2026 Wurst-Imperium and contributors. |
| 3 | + * |
| 4 | + * This source code is subject to the terms of the GNU General Public |
| 5 | + * License, version 3. If a copy of the GPL was not distributed with this |
| 6 | + * file, You can obtain one at: https://www.gnu.org/licenses/gpl-3.0.txt |
| 7 | + */ |
| 8 | +package net.wurstclient.chestsearch; |
| 9 | + |
| 10 | +import net.minecraft.core.BlockPos; |
| 11 | + |
| 12 | +public final class SlotHighlighter |
| 13 | +{ |
| 14 | + public static final SlotHighlighter INSTANCE = new SlotHighlighter(); |
| 15 | + |
| 16 | + private int enchantmentHandlerSlot = -1; |
| 17 | + private int chestSearchSlot = -1; |
| 18 | + private String chestSearchDimension = ""; |
| 19 | + private BlockPos chestSearchPos; |
| 20 | + private boolean stickyUntilHovered; |
| 21 | + |
| 22 | + private String pendingDimension = ""; |
| 23 | + private BlockPos pendingPos; |
| 24 | + private int pendingSlot = -1; |
| 25 | + |
| 26 | + private SlotHighlighter() |
| 27 | + {} |
| 28 | + |
| 29 | + public synchronized void setPending(String dimension, BlockPos pos, |
| 30 | + int slot) |
| 31 | + { |
| 32 | + if(pos == null || slot < 0) |
| 33 | + { |
| 34 | + clearPending(); |
| 35 | + return; |
| 36 | + } |
| 37 | + pendingDimension = normalize(dimension); |
| 38 | + pendingPos = pos.immutable(); |
| 39 | + pendingSlot = slot; |
| 40 | + } |
| 41 | + |
| 42 | + public synchronized void clearPending() |
| 43 | + { |
| 44 | + pendingDimension = ""; |
| 45 | + pendingPos = null; |
| 46 | + pendingSlot = -1; |
| 47 | + } |
| 48 | + |
| 49 | + public synchronized int tryActivate(String dimension, BlockPos pos) |
| 50 | + { |
| 51 | + String dim = normalize(dimension); |
| 52 | + if(pendingPos == null || pendingSlot < 0 || pos == null) |
| 53 | + return -1; |
| 54 | + if(!dim.equals(pendingDimension) || !pos.equals(pendingPos)) |
| 55 | + return -1; |
| 56 | + chestSearchSlot = pendingSlot; |
| 57 | + chestSearchDimension = dim; |
| 58 | + chestSearchPos = pos.immutable(); |
| 59 | + return chestSearchSlot; |
| 60 | + } |
| 61 | + |
| 62 | + public synchronized int tryActivateForDimension(String dimension) |
| 63 | + { |
| 64 | + String dim = normalize(dimension); |
| 65 | + if(pendingPos == null || pendingSlot < 0) |
| 66 | + return -1; |
| 67 | + if(!dim.equals(pendingDimension)) |
| 68 | + return -1; |
| 69 | + chestSearchSlot = pendingSlot; |
| 70 | + chestSearchDimension = dim; |
| 71 | + chestSearchPos = pendingPos.immutable(); |
| 72 | + return chestSearchSlot; |
| 73 | + } |
| 74 | + |
| 75 | + public synchronized int getActiveSlot() |
| 76 | + { |
| 77 | + return chestSearchSlot >= 0 ? chestSearchSlot : enchantmentHandlerSlot; |
| 78 | + } |
| 79 | + |
| 80 | + public synchronized int getEnchantmentHandlerSlot() |
| 81 | + { |
| 82 | + return enchantmentHandlerSlot; |
| 83 | + } |
| 84 | + |
| 85 | + public synchronized int getChestSearchSlot() |
| 86 | + { |
| 87 | + return chestSearchSlot; |
| 88 | + } |
| 89 | + |
| 90 | + public synchronized String getChestSearchDimension() |
| 91 | + { |
| 92 | + return chestSearchDimension; |
| 93 | + } |
| 94 | + |
| 95 | + public synchronized BlockPos getChestSearchPos() |
| 96 | + { |
| 97 | + return chestSearchPos; |
| 98 | + } |
| 99 | + |
| 100 | + public synchronized void setActiveSlot(int slot) |
| 101 | + { |
| 102 | + if(slot < 0) |
| 103 | + { |
| 104 | + clearEnchantmentHandlerActive(); |
| 105 | + return; |
| 106 | + } |
| 107 | + enchantmentHandlerSlot = slot; |
| 108 | + stickyUntilHovered = false; |
| 109 | + } |
| 110 | + |
| 111 | + public synchronized void setStickySlot(int slot) |
| 112 | + { |
| 113 | + if(slot < 0) |
| 114 | + { |
| 115 | + clearEnchantmentHandlerActive(); |
| 116 | + return; |
| 117 | + } |
| 118 | + enchantmentHandlerSlot = slot; |
| 119 | + stickyUntilHovered = true; |
| 120 | + } |
| 121 | + |
| 122 | + public synchronized boolean isStickyActive() |
| 123 | + { |
| 124 | + return enchantmentHandlerSlot >= 0 && stickyUntilHovered; |
| 125 | + } |
| 126 | + |
| 127 | + public synchronized boolean isEnchantmentHandlerActive() |
| 128 | + { |
| 129 | + return enchantmentHandlerSlot >= 0; |
| 130 | + } |
| 131 | + |
| 132 | + public synchronized boolean isChestSearchActive() |
| 133 | + { |
| 134 | + return chestSearchSlot >= 0; |
| 135 | + } |
| 136 | + |
| 137 | + public synchronized void clearEnchantmentHandlerActive() |
| 138 | + { |
| 139 | + enchantmentHandlerSlot = -1; |
| 140 | + stickyUntilHovered = false; |
| 141 | + } |
| 142 | + |
| 143 | + public synchronized void clearChestSearchActive() |
| 144 | + { |
| 145 | + chestSearchSlot = -1; |
| 146 | + chestSearchDimension = ""; |
| 147 | + chestSearchPos = null; |
| 148 | + } |
| 149 | + |
| 150 | + public synchronized void forgetChestSearchSelection() |
| 151 | + { |
| 152 | + clearPending(); |
| 153 | + clearChestSearchActive(); |
| 154 | + } |
| 155 | + |
| 156 | + public synchronized void clearActive() |
| 157 | + { |
| 158 | + clearEnchantmentHandlerActive(); |
| 159 | + clearChestSearchActive(); |
| 160 | + } |
| 161 | + |
| 162 | + public synchronized void clearAll() |
| 163 | + { |
| 164 | + clearPending(); |
| 165 | + clearActive(); |
| 166 | + } |
| 167 | + |
| 168 | + private static String normalize(String dimension) |
| 169 | + { |
| 170 | + if(dimension == null) |
| 171 | + return ""; |
| 172 | + String dim = dimension.trim().toLowerCase(java.util.Locale.ROOT); |
| 173 | + int colon = dim.indexOf(':'); |
| 174 | + if(colon >= 0 && colon < dim.length() - 1) |
| 175 | + return dim.substring(colon + 1); |
| 176 | + return dim; |
| 177 | + } |
| 178 | + |
| 179 | +} |
0 commit comments