Skip to content

Commit

Permalink
Cleared up and finished Hotbar Slot Locking
Browse files Browse the repository at this point in the history
  • Loading branch information
Tripp1e committed Feb 7, 2024
1 parent b75b3a3 commit fdc4996
Show file tree
Hide file tree
Showing 15 changed files with 162 additions and 66 deletions.
26 changes: 13 additions & 13 deletions src/main/java/com/tripp1e/isleshelper/Events.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@
import com.tripp1e.isleshelper.features.bossrush.Frog;
import com.tripp1e.isleshelper.features.bossrush.General;
import com.tripp1e.isleshelper.config.ConfigManager;
import com.tripp1e.isleshelper.mixin.accessor.TextDisplayEntityAccessor;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientEntityEvents;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerEntityEvents;
import net.minecraft.entity.decoration.DisplayEntity;

public class Events {

public static void init() {

ClientTickEvents.START_CLIENT_TICK.register(client -> {
if (Utils.getWorld() == null) return;
if (Utils.getNull()) return;

//General
if (ConfigManager.get().bossRush.onlyPartyChatsEnabled) General.onlyPartyMessages();
if (ConfigManager.get().bossRush.timerEnabled) General.timer(null);
if (ConfigManager.get().bossRush.timerEnabled) General.timer(Utils.drawContext);

//Frog
if (Utils.getBoss().equals("frog")) {
Expand All @@ -26,23 +28,21 @@ public static void init() {
});

HudRenderCallback.EVENT.register((drawContext, tickDelta) -> {
if (Utils.getWorld() == null) return;
if (Utils.getNull()) return;
Utils.drawContext = drawContext;
if (ConfigManager.get().bossRush.timerEnabled) General.timer(drawContext);
});

ServerEntityEvents.ENTITY_LOAD.register((entity, world) -> {
if (Utils.getWorld() == null) return;
IslesHelperClient.LOGGER.info("Something loaded: " + entity);
ClientEntityEvents.ENTITY_LOAD.register(((entity, world) -> {
if (Utils.getNull()) return;
if (entity instanceof DisplayEntity.TextDisplayEntity textEntity) {
//IslesHelperClient.LOGGER.info("TextLines: " + (TextDisplayEntityAccessor)textEntity.invokeGetText());
}

if (ConfigManager.get().bossRush.teammateDeathMessageEnabled) General.teamDeathNotify(entity);
General.timer(Utils.drawContext);

});
}));

ClientTickEvents.END_CLIENT_TICK.register(client -> {
if (client.player == null) return;

if (Utils.getNull()) return;


});
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/tripp1e/isleshelper/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public class Utils{

public static PlayerEntity getPlayer() {return MinecraftClient.getInstance().player;}
public static World getWorld() {return MinecraftClient.getInstance().world;}
public static boolean getNull() {
return getPlayer() == null && getWorld() == null;
}
public static InGameHud getHUD() {return MinecraftClient.getInstance().inGameHud;}
public static Vec3d normalVector = new Vec3d(1.0D,1.0D,1.0D);
public static Box boxAroundEntity(Entity entity, int radius) {return new Box(entity.getBlockPos().multiply(radius));}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/tripp1e/isleshelper/config/Config.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.tripp1e.isleshelper.config;

import dev.isxander.yacl3.config.v2.api.SerialEntry;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -20,6 +21,9 @@ public static class General {
@SerialEntry
public List<Integer> lockedSlots = new ArrayList<>();

@SerialEntry
public ObjectOpenHashSet<String> protectedItems = new ObjectOpenHashSet<>();

}

public static class BossRush {
Expand Down
15 changes: 0 additions & 15 deletions src/main/java/com/tripp1e/isleshelper/config/ConfigManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,13 @@
import com.tripp1e.isleshelper.IslesHelperClient;
import com.tripp1e.isleshelper.config.categories.BossRushCategory;
import com.tripp1e.isleshelper.config.categories.GeneralCategory;
import com.tripp1e.isleshelper.mixin.HandledScreenAccessor;
import dev.isxander.yacl3.api.YetAnotherConfigLib;
import dev.isxander.yacl3.config.v2.api.ConfigClassHandler;
import dev.isxander.yacl3.config.v2.api.serializer.GsonConfigSerializerBuilder;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents;
import net.fabricmc.fabric.api.client.screen.v1.Screens;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.ingame.GenericContainerScreen;
import net.minecraft.client.gui.tooltip.Tooltip;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

Expand All @@ -44,15 +38,6 @@ public static Screen createGUI(Screen parent) {
public static void init() {
HANDLER.load();
ClientCommandRegistrationCallback.EVENT.register(((dispatcher, registryAccess) -> dispatcher.register(ClientCommandManager.literal(IslesHelperClient.MOD_ID))));
ScreenEvents.AFTER_INIT.register((client, screen, scaledWidth, scaledHeight) -> {
if (screen instanceof GenericContainerScreen genericContainerScreen && screen.getTitle().getString().equals("IslesHelper Config")) {
Screens.getButtons(screen).add(ButtonWidget
.builder(Text.literal("\uD83D\uDD27"), buttonWidget -> client.setScreen(createGUI(screen)))
.dimensions(((HandledScreenAccessor) genericContainerScreen).getX() + ((HandledScreenAccessor) genericContainerScreen).getBackgroundWidth() - 16, ((HandledScreenAccessor) genericContainerScreen).getY() + 4, 12, 12)
.tooltip(Tooltip.of(Text.of("IslesHelper Config")))
.build());
}
});
}

// Make Handler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import net.fabricmc.fabric.api.client.message.v1.ClientReceiveMessageEvents;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.decoration.ArmorStandEntity;
import net.minecraft.entity.decoration.DisplayEntity;

public class General {

Expand All @@ -24,23 +24,20 @@ public static void onlyPartyMessages() {
ClientReceiveMessageEvents.ALLOW_CHAT.register((message, a, b, c, d) -> message.toString().toLowerCase().startsWith("party"));
}

private static double currentTime = System.currentTimeMillis();
public static DisplayEntity.TextDisplayEntity displayEntity = null;
private static double startTime = System.currentTimeMillis();
public static String deltaTime = "0";
private static double currentTime = System.currentTimeMillis();
public static void timer(DrawContext context) {
startTime = Utils.isInBoss() ? startTime : currentTime;
String deltaTime = (currentTime - startTime)/1000D + "";

int x = ConfigManager.get().bossRush.timerX;
int y = ConfigManager.get().bossRush.timerY;

if (!Utils.isInBoss()) startTime = currentTime;
deltaTime = (currentTime - startTime)/1000D + "";


if (Utils.getWorld().getEntitiesByType(EntityType.INTERACTION, Utils.boxAroundEntity(10), entity -> entity.getCustomName() != null && entity.getCustomName().toString().toLowerCase().contains("rank")).isEmpty()) {
currentTime = System.currentTimeMillis();
} else {
if (displayEntity != null && Utils.isInBoss()) {
Utils.sendTitle(deltaTime, 0, 2, 0);
}
} else currentTime = System.currentTimeMillis();

if (context != null && Utils.isInBoss())
Renderer.renderer.drawString(context.getMatrices(), deltaTime, x, y, 1, 1, 1, 1);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.tripp1e.isleshelper.features.general;

import com.tripp1e.isleshelper.config.ConfigManager;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.option.KeyBinding;
import org.lwjgl.glfw.GLFW;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.List;

//From https://github.com/SkyblockerMod/Skyblocker/blob/master/src/main/java/de/hysky/skyblocker/skyblock/item/HotbarSlotLock.java
public class HotbarSlotLock {
public static KeyBinding hotbarSlotLock;
public static void init() {
hotbarSlotLock = KeyBindingHelper.registerKeyBinding( new KeyBinding(
"key.hotbarSlotLock",
GLFW.GLFW_KEY_H,
"key.categories.isleshelper"
));
}

public static boolean isLocked(int slot) {
return ConfigManager.get().general.lockedSlots.contains(slot);
}

public static void handleDropSelectedItem(int slot, CallbackInfoReturnable<Boolean> cir) {
if (isLocked(slot)) cir.setReturnValue(false);
}

public static void handleInputEvents(ClientPlayerEntity player) {
while (hotbarSlotLock.wasPressed()) {
List<Integer> lockedSlots = ConfigManager.get().general.lockedSlots;
int selected = player.getInventory().selectedSlot;
if (!isLocked(player.getInventory().selectedSlot)) lockedSlots.add(selected);
else lockedSlots.remove(Integer.valueOf(selected));
ConfigManager.save();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.tripp1e.isleshelper.mixin;

import com.mojang.authlib.GameProfile;
import com.tripp1e.isleshelper.features.general.HotbarSlotLock;
import net.minecraft.client.network.AbstractClientPlayerEntity;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.world.ClientWorld;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

// From https://github.com/SkyblockerMod/Skyblocker/blob/master/src/main/java/de/hysky/skyblocker/mixin/ClientPlayerEntityMixin.java
@Mixin(ClientPlayerEntity.class)
public abstract class ClientPlayerEntityMixin extends AbstractClientPlayerEntity {

public ClientPlayerEntityMixin(ClientWorld world, GameProfile profile) {super(world, profile);}
@Inject(method = "dropSelectedItem", at = @At("HEAD"), cancellable = true)
public void skyblockisles$dropSelectedItem(CallbackInfoReturnable<Boolean> cir) {
HotbarSlotLock.handleDropSelectedItem(this.getInventory().selectedSlot, cir);
}

}

This file was deleted.

37 changes: 37 additions & 0 deletions src/main/java/com/tripp1e/isleshelper/mixin/InGameHudMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.tripp1e.isleshelper.mixin;

import com.llamalad7.mixinextras.sugar.Local;
import com.tripp1e.isleshelper.IslesHelperClient;
import com.tripp1e.isleshelper.features.general.HotbarSlotLock;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.hud.InGameHud;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.Identifier;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import com.mojang.blaze3d.systems.RenderSystem;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.function.Supplier;

// From https://github.com/SkyblockerMod/Skyblocker/blob/master/src/main/java/de/hysky/skyblocker/mixin/InGameHudMixin.java
@Environment(EnvType.CLIENT)
@Mixin(InGameHud.class)
public abstract class InGameHudMixin {
@Unique
private static final Supplier<Identifier> SLOT_LOCK_ICON = () -> new Identifier(IslesHelperClient.MOD_ID, "textures/slotlock.png");

@Inject(method = "renderHotbar", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/hud/InGameHud;renderHotbarItem(Lnet/minecraft/client/gui/DrawContext;IIFLnet/minecraft/entity/player/PlayerEntity;Lnet/minecraft/item/ItemStack;I)V", ordinal = 0))
public void skyblocker$renderHotbarItemLockOrRarityBg(float tickDelta, DrawContext context, CallbackInfo ci, @Local(ordinal = 4, name = "m") int index, @Local(ordinal = 5, name = "n") int x, @Local(ordinal = 6, name = "o") int y, @Local PlayerEntity player) {
if (HotbarSlotLock.isLocked(index)) {
RenderSystem.enableBlend();
context.drawTexture(SLOT_LOCK_ICON.get(), x, y, 0, 0, 16, 16, 16, 16);
RenderSystem.disableBlend();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.tripp1e.isleshelper.mixin;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayerEntity;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import com.tripp1e.isleshelper.features.general.HotbarSlotLock;


// From https://github.com/SkyblockerMod/Skyblocker/blob/master/src/main/java/de/hysky/skyblocker/mixin/MinecraftClientMixin.java
@Mixin(MinecraftClient.class)
public abstract class MinecraftClientMixin {

@Shadow
@Nullable
public ClientPlayerEntity player;

@Inject(method = "handleInputEvents", at = @At("HEAD"))
public void skyblocker$handleInputEvents(CallbackInfo ci) {
HotbarSlotLock.handleInputEvents(player);
}

}
4 changes: 2 additions & 2 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"sources": "https://github.com/Tripp1e/IslesHelper"
},
"license": "LGPL-3.0-or-later",
"icon": "assets/isleshelper/icon.png",
"icon": "icon.png",
"environment": "client",
"entrypoints": {
"client": [
Expand Down Expand Up @@ -40,7 +40,7 @@
"id": "isleshelper",
"name": "Isles Helper",
"description": "QoL Mod for Skyblock Isles",
"icon": "assets/isleshelper/icon.png"
"icon": "icon.png"
},
"update_checker": true
}
Expand Down
10 changes: 6 additions & 4 deletions src/main/resources/isleshelper.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
"required": true,
"package": "com.tripp1e.isleshelper.mixin",
"compatibilityLevel": "JAVA_17",
"mixins": [
"HandledScreenAccessor"
],
"injectors": {
"defaultRequire": 1
},
"client": [
"ClientPlayerEntityMixin"
"ClientPlayerEntityMixin",
"InGameHudMixin",
"MinecraftClientMixin"
],
"mixins": [
"accessor.TextDisplayEntityAccessor"
]
}

0 comments on commit fdc4996

Please sign in to comment.