feat: toggle system & improved locking mechanism

This commit is contained in:
Erica Marigold 2024-05-20 20:05:06 +05:30
parent 172aa5b876
commit 597ffaf4b1
No known key found for this signature in database
GPG key ID: 2768CC0C23D245D1
7 changed files with 72 additions and 41 deletions

View file

@ -19,12 +19,9 @@ repositories {
}
loom {
splitEnvironmentSourceSets()
mods {
"elytra-lock" {
sourceSet sourceSets.main
sourceSet sourceSets.client
}
}

View file

@ -1,21 +1,46 @@
package xyz.devcomp.elytralock;
import org.lwjgl.glfw.GLFW;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.impl.networking.client.ClientPlayNetworkAddon;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayNetworkHandler;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.minecraft.network.message.MessageType;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.network.packet.c2s.play.ClickSlotC2SPacket;
import net.minecraft.network.packet.c2s.play.SlotChangedStateC2SPacket;
public class ElytraLock implements ClientModInitializer {
public static final Logger LOGGER = LoggerFactory.getLogger("Elytra Lock");
private static KeyBinding lockKeybind;
private static boolean locked = false;
private static MinecraftClient client;
@Override
public void onInitializeClient() {
// This entrypoint is suitable for setting up client-specific logic, such as rendering.
lockKeybind = KeyBindingHelper.registerKeyBinding(
new KeyBinding("key.elytralock.lock", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_J, "category.elytralock"));
client = MinecraftClient.getInstance();
LOGGER.info("Elytra lock initializing!");
}
public static boolean isLocked() {
// TODO: Load from config
return true;
if (lockKeybind.wasPressed()) {
locked = !locked;
}
client.inGameHud.getChatHud().addMessage(Text.translatable("elytralock.chat.lockedMessage"));
return locked;
}
}

View file

@ -0,0 +1,34 @@
package xyz.devcomp.elytralock.mixin;
import xyz.devcomp.elytralock.ElytraLock;
import org.apache.commons.lang3.mutable.MutableObject;
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;
import com.llamalad7.mixinextras.sugar.Local;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.network.ClientPlayerInteractionManager;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
@Environment(EnvType.CLIENT)
@Mixin(ClientPlayerInteractionManager.class)
public class ClientPlayerInteractionManagerMixin {
@Inject(method = "interactItem(Lnet/minecraft/entity/player/PlayerEntity;Lnet/minecraft/util/Hand;)Lnet/minecraft/util/ActionResult;", at = @At(value = "INVOKE", target = "net/minecraft/client/network/ClientPlayerInteractionManager.sendSequencedPacket (Lnet/minecraft/client/world/ClientWorld;Lnet/minecraft/client/network/SequencedPacketCreator;)V"), cancellable = true)
private void skipElytra(PlayerEntity player, Hand hand, CallbackInfoReturnable<ActionResult> info, @Local MutableObject<ActionResult> mutableObject) {
ItemStack itemStack = player.getStackInHand(hand);
if (itemStack.isOf(Items.ELYTRA) && ElytraLock.isLocked()) {
mutableObject.setValue(ActionResult.FAIL);
info.setReturnValue((ActionResult) mutableObject.getValue());
}
}
}

View file

@ -1,30 +0,0 @@
package xyz.devcomp.elytralock.mixin;
import xyz.devcomp.elytralock.ElytraLock;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.item.Equipment;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.util.TypedActionResult;
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;
import com.llamalad7.mixinextras.sugar.Local;
@Mixin(Equipment.class)
@Environment(EnvType.CLIENT)
public interface EquipmentMixin {
@Inject(method = "equipAndSwap(Lnet/minecraft/item/Item;Lnet/minecraft/world/World;Lnet/minecraft/entity/player/PlayerEntity;Lnet/minecraft/util/Hand;)Lnet/minecraft/util/TypedActionResult;", at = @At(value = "INVOKE_ASSIGN", target = "net/minecraft/entity/player/PlayerEntity.getStackInHand (Lnet/minecraft/util/Hand;)Lnet/minecraft/item/ItemStack;"), cancellable = true)
private void injected(CallbackInfoReturnable<TypedActionResult<ItemStack>> info,
@Local(ordinal = 0) ItemStack itemStack) {
if (itemStack.isOf(Items.ELYTRA) && ElytraLock.isLocked()) {
info.setReturnValue(TypedActionResult.fail(itemStack));
}
}
}

View file

@ -0,0 +1,5 @@
{
"category.elytralock": "Elytra Lock Keybinds",
"key.elytralock.lock": "Lock elytra",
"elytralock.chat.lockedMessage": "[elytra-lock] Elytra is locked!"
}

View file

@ -3,7 +3,7 @@
"package": "xyz.devcomp.elytralock.mixin",
"compatibilityLevel": "JAVA_17",
"mixins": [
"EquipmentMixin"
"ClientPlayerInteractionManagerMixin"
],
"injectors": {
"defaultRequire": 1

View file

@ -3,13 +3,13 @@
"id": "elytra-lock",
"version": "${version}",
"name": "elytra-lock",
"description": "This is an example description! Tell everyone what your mod is about!",
"description": "A simple mod which registers a keybind to lock your elytra from usage when not required.",
"authors": [
"Me!"
"CompeyDev <hi@devcomp.xyz>"
],
"contact": {
"homepage": "https://fabricmc.net/",
"sources": "https://github.com/FabricMC/fabric-example-mod"
"sources": "https://github.com/CompeyDev/elytra-lock"
},
"license": "CC0-1.0",
"icon": "assets/elytra-lock/icon.png",