Skip to content

Commit 9fad665

Browse files
committed
Navigator and EnchantmentHandler Fixes
1 parent c7daf65 commit 9fad665

7 files changed

Lines changed: 130 additions & 24 deletions

File tree

src/main/java/net/wurstclient/clickgui/ClickGui.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,22 @@ public boolean handleNavigatorPopupClick(double mouseX, double mouseY,
318318
return popupClicked;
319319
}
320320

321+
public boolean handleNavigatorMouseScroll(double mouseX, double mouseY,
322+
double delta)
323+
{
324+
boolean popupScrolled = handlePopupMouseScroll(mouseX, mouseY, delta);
325+
if(popupScrolled)
326+
{
327+
for(Popup popup : popups)
328+
if(popup.getOwner().getParent().isClosing())
329+
popup.close();
330+
331+
popups.removeIf(Popup::isClosing);
332+
}
333+
334+
return popupScrolled;
335+
}
336+
321337
public void handleNavigatorMouseClick(double cMouseX, double cMouseY,
322338
int mouseButton, Window window)
323339
{
@@ -405,8 +421,7 @@ private boolean handlePopupMouseScroll(double mouseX, double mouseY,
405421
return false;
406422
}
407423

408-
private void handleWindowMouseClick(int mouseX, int mouseY, int mouseButton,
409-
Click context)
424+
private void handleWindowMouseClick(int mouseX, int mouseY, int mouseButton)
410425
{
411426
for(int i = windows.size() - 1; i >= 0; i--)
412427
{

src/main/java/net/wurstclient/clickgui/Window.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class Window
2020
private int y;
2121
private int width;
2222
private int height;
23+
private boolean clampPosition = true;
2324

2425
private boolean valid;
2526
private final ArrayList<Component> children = new ArrayList<>();
@@ -68,6 +69,9 @@ public final void setTitle(String title)
6869
*/
6970
public final int getX()
7071
{
72+
if(!clampPosition)
73+
return x;
74+
7175
int scaledWidth = WurstClient.MC.getWindow().getScaledWidth();
7276
return MathHelper.clamp(x, -width + 1, scaledWidth - 1);
7377
}
@@ -92,6 +96,9 @@ public final void setX(int x)
9296
*/
9397
public final int getY()
9498
{
99+
if(!clampPosition)
100+
return y;
101+
95102
int scaledHeight = WurstClient.MC.getWindow().getScaledHeight();
96103
return MathHelper.clamp(y, -12, scaledHeight - 1);
97104
}
@@ -111,6 +118,11 @@ public final void setY(int y)
111118
this.y = y;
112119
}
113120

121+
public final void setClampPosition(boolean clampPosition)
122+
{
123+
this.clampPosition = clampPosition;
124+
}
125+
114126
public final int getWidth()
115127
{
116128
return width;

src/main/java/net/wurstclient/clickgui/components/MobWeaponRuleComponent.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import org.lwjgl.glfw.GLFW;
1717

1818
import net.minecraft.client.font.TextRenderer;
19-
import net.minecraft.client.gui.Click;
2019
import net.minecraft.client.gui.DrawContext;
2120
import net.wurstclient.clickgui.ClickGui;
2221
import net.wurstclient.clickgui.ClickGuiIcons;
@@ -49,8 +48,7 @@ public MobWeaponRuleComponent(MobWeaponRuleSetting setting)
4948
}
5049

5150
@Override
52-
public void handleMouseClick(double mouseX, double mouseY, int mouseButton,
53-
Click context)
51+
public void handleMouseClick(double mouseX, double mouseY, int mouseButton)
5452
{
5553
Box mobBox = getMobBox();
5654
Box weaponBox = getWeaponBox();

src/main/java/net/wurstclient/hacks/EnchantmentHandlerHack.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,7 +1391,7 @@ private void takeEntry(GearEntry entry, ScreenHandler handler)
13911391
if(entry.slotId < 0)
13921392
return;
13931393

1394-
Slot slot = handler.getSlot(entry.slotId);
1394+
Slot slot = getSlotSafe(handler, entry.slotId);
13951395
if(slot == null || !slot.hasStack())
13961396
return;
13971397

@@ -1408,7 +1408,7 @@ private void takeEntry(BookEntry entry, ScreenHandler handler)
14081408
if(entry.slotId < 0)
14091409
return;
14101410

1411-
Slot slot = handler.getSlot(entry.slotId);
1411+
Slot slot = getSlotSafe(handler, entry.slotId);
14121412
if(slot == null || !slot.hasStack())
14131413
return;
14141414

@@ -1425,7 +1425,7 @@ private void takeEntry(PotionEntry entry, ScreenHandler handler)
14251425
if(entry.slotId < 0)
14261426
return;
14271427

1428-
Slot slot = handler.getSlot(entry.slotId);
1428+
Slot slot = getSlotSafe(handler, entry.slotId);
14291429
if(slot == null || !slot.hasStack())
14301430
return;
14311431

@@ -1460,7 +1460,7 @@ private void takeGearCategory(GearCategory category, ScreenHandler handler,
14601460
{
14611461
if(entry.slotId < 0)
14621462
continue; // display-only (in shulker)
1463-
Slot slot = handler.getSlot(entry.slotId);
1463+
Slot slot = getSlotSafe(handler, entry.slotId);
14641464
if(slot == null || !slot.hasStack())
14651465
continue;
14661466

@@ -1497,7 +1497,7 @@ private void takeBookCategory(BookCategory category, ScreenHandler handler,
14971497
{
14981498
if(entry.slotId < 0)
14991499
continue;
1500-
Slot slot = handler.getSlot(entry.slotId);
1500+
Slot slot = getSlotSafe(handler, entry.slotId);
15011501
if(slot == null || !slot.hasStack())
15021502
continue;
15031503
MC.interactionManager.clickSlot(handler.syncId, entry.slotId, 0,
@@ -1533,7 +1533,7 @@ private void takePotionCategory(PotionCategory category,
15331533
{
15341534
if(entry.slotId < 0)
15351535
continue;
1536-
Slot slot = handler.getSlot(entry.slotId);
1536+
Slot slot = getSlotSafe(handler, entry.slotId);
15371537
if(slot == null || !slot.hasStack())
15381538
continue;
15391539
MC.interactionManager.clickSlot(handler.syncId, entry.slotId, 0,
@@ -1543,6 +1543,18 @@ private void takePotionCategory(PotionCategory category,
15431543
needsRescan = true;
15441544
}
15451545

1546+
private Slot getSlotSafe(ScreenHandler handler, int slotId)
1547+
{
1548+
if(handler == null || slotId < 0)
1549+
return null;
1550+
1551+
List<Slot> slots = handler.slots;
1552+
if(slots == null || slotId >= slots.size())
1553+
return null;
1554+
1555+
return handler.getSlot(slotId);
1556+
}
1557+
15461558
private int panelInnerHeight()
15471559
{
15481560
float scale = MathHelper.clamp(textScale.getValueF(), 0.5F, 1.25F);

src/main/java/net/wurstclient/navigator/NavigatorFeatureScreen.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public NavigatorFeatureScreen(Feature feature, NavigatorMainScreen parent)
6868
window.add(c);
6969
}
7070

71+
window.setClampPosition(false);
7172
window.setWidth(308);
7273
window.setFixedWidth(true);
7374
window.pack();

src/main/java/net/wurstclient/navigator/NavigatorScreen.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ public final boolean mouseReleased(double x, double y, int button)
107107
public final boolean mouseScrolled(double mouseX, double mouseY,
108108
double horizontalAmount, double verticalAmount)
109109
{
110+
if(WurstClient.INSTANCE.getGui().handleNavigatorMouseScroll(mouseX,
111+
mouseY, verticalAmount))
112+
return true;
113+
110114
// scrollbar
111115
if(!scrollbarLocked)
112116
{

src/main/java/net/wurstclient/settings/MobWeaponRuleSetting.java

Lines changed: 77 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818
import com.google.gson.JsonObject;
1919
import com.google.gson.JsonPrimitive;
2020

21+
import net.minecraft.client.MinecraftClient;
2122
import net.minecraft.client.network.ClientPlayerEntity;
23+
import net.minecraft.client.network.ClientPlayNetworkHandler;
2224
import net.minecraft.entity.Entity;
25+
import net.minecraft.entity.EntityType;
2326
import net.minecraft.entity.SpawnGroup;
2427
import net.minecraft.entity.attribute.EntityAttributes;
2528
import net.minecraft.entity.player.PlayerInventory;
@@ -28,6 +31,9 @@
2831
import net.minecraft.item.MaceItem;
2932
import net.minecraft.item.TridentItem;
3033
import net.minecraft.registry.Registries;
34+
import net.minecraft.registry.Registry;
35+
import net.minecraft.registry.RegistryKeys;
36+
import net.minecraft.registry.DynamicRegistryManager;
3137
import net.minecraft.registry.tag.ItemTags;
3238
import net.minecraft.util.Identifier;
3339
import net.wurstclient.WurstClient;
@@ -44,7 +50,7 @@ public final class MobWeaponRuleSetting extends Setting
4450
{
4551
private static final MobOption ANY_OPTION =
4652
new MobOption("any", "Any mob", null);
47-
private static final List<MobOption> MOB_OPTIONS = buildMobOptions();
53+
private static volatile List<MobOption> mobOptionsCache;
4854

4955
private MobOption selectedMob = ANY_OPTION;
5056
private WeaponCategory selectedWeapon = WeaponCategory.NONE;
@@ -65,7 +71,7 @@ public Component getComponent()
6571

6672
public List<MobOption> getMobOptions()
6773
{
68-
return MOB_OPTIONS;
74+
return getOrCreateMobOptions();
6975
}
7076

7177
public MobOption getSelectedMob()
@@ -171,30 +177,88 @@ public Set<PossibleKeybind> getPossibleKeybinds(String featureName)
171177

172178
private static MobOption findMobOption(String id)
173179
{
174-
for(MobOption option : MOB_OPTIONS)
180+
for(MobOption option : getOrCreateMobOptions())
175181
if(option.id().equals(id))
176182
return option;
177183

178184
return ANY_OPTION;
179185
}
180186

187+
private static List<MobOption> getOrCreateMobOptions()
188+
{
189+
List<MobOption> cached = mobOptionsCache;
190+
int cachedSize = cached == null ? 0 : cached.size();
191+
if(cached == null || cachedSize <= 1)
192+
{
193+
List<MobOption> rebuilt = buildMobOptions();
194+
if(rebuilt.size() > 1)
195+
{
196+
mobOptionsCache = rebuilt;
197+
return rebuilt;
198+
}
199+
200+
return rebuilt;
201+
}
202+
203+
return cached;
204+
}
205+
181206
private static List<MobOption> buildMobOptions()
182207
{
183208
List<MobOption> options = new ArrayList<>();
184209
options.add(ANY_OPTION);
185-
Registries.ENTITY_TYPE.getIds().stream()
186-
.map(Registries.ENTITY_TYPE::get)
187-
.filter(type -> type.getSpawnGroup() != SpawnGroup.MISC)
188-
.map(type -> {
189-
Identifier id = Registries.ENTITY_TYPE.getId(type);
190-
String name = type.getName().getString();
191-
return new MobOption(id.toString(), name, type);
192-
}).sorted(Comparator.comparing(MobOption::displayName,
193-
String.CASE_INSENSITIVE_ORDER))
194-
.forEach(options::add);
210+
211+
Registry<EntityType<?>> registry = resolveEntityRegistry();
212+
for(Identifier id : registry.getIds())
213+
{
214+
EntityType<?> type = registry.get(id);
215+
if(type == null || type.getSpawnGroup() == SpawnGroup.MISC)
216+
continue;
217+
218+
String name = type.getName().getString();
219+
options.add(new MobOption(id.toString(), name, type));
220+
}
221+
222+
options.sort(Comparator.comparing(MobOption::displayName,
223+
String.CASE_INSENSITIVE_ORDER));
195224
return Collections.unmodifiableList(options);
196225
}
197226

227+
private static Registry<EntityType<?>> resolveEntityRegistry()
228+
{
229+
MinecraftClient mc = WurstClient.MC;
230+
if(mc != null)
231+
{
232+
if(mc.world != null)
233+
{
234+
Registry<EntityType<?>> worldRegistry =
235+
getRegistryFromManager(mc.world.getRegistryManager());
236+
if(worldRegistry != null)
237+
return worldRegistry;
238+
}
239+
240+
ClientPlayNetworkHandler handler = mc.getNetworkHandler();
241+
if(handler != null)
242+
{
243+
Registry<EntityType<?>> networkRegistry =
244+
getRegistryFromManager(handler.getRegistryManager());
245+
if(networkRegistry != null)
246+
return networkRegistry;
247+
}
248+
}
249+
250+
return Registries.ENTITY_TYPE;
251+
}
252+
253+
private static Registry<EntityType<?>> getRegistryFromManager(
254+
DynamicRegistryManager manager)
255+
{
256+
if(manager == null)
257+
return null;
258+
259+
return manager.getOptional(RegistryKeys.ENTITY_TYPE).orElse(null);
260+
}
261+
198262
public record MobOption(String id, String displayName,
199263
net.minecraft.entity.EntityType<?> type)
200264
{

0 commit comments

Comments
 (0)