Files
src
build.gradle
gradle.properties
manifest.kod
settings.gradle
Code
8 421
src/client/java/com/kodari/attributeswap/AttributeSwapClient.java
package com.kodari.attributeswap;
import com.mojang.blaze3d.platform.InputConstants;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keymapping.v1.KeyMappingHelper;
import net.fabricmc.fabric.api.event.player.AttackEntityCallback;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientPacketListener;
import net.minecraft.network.protocol.game.ServerboundAttackPacket;
import net.minecraft.network.protocol.game.ServerboundSetCarriedItemPacket;
import net.minecraft.resources.Identifier;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Properties;
public final class AttributeSwapClient implements ClientModInitializer {
public static final String MOD_ID = "attributeswap";
private static final int DEFAULT_ATTRIBUTE_SLOT = 0;
private static final int DEFAULT_EFFECT_SLOT = 1;
private static boolean enabled;
private static int attributeSlot = DEFAULT_ATTRIBUTE_SLOT;
private static int effectSlot = DEFAULT_EFFECT_SLOT;
private static KeyMapping openScreenKey;
private static KeyMapping toggleModuleKey;
@Override
public void onInitializeClient() {
loadConfig();
KeyMapping.Category category = KeyMapping.Category.register(Identifier.fromNamespaceAndPath(MOD_ID, "controls"));
openScreenKey = KeyMappingHelper.registerKeyMapping(new KeyMapping(
"key.attributeswap.open", InputConstants.Type.KEYBOARD, InputConstants.KEY_H, category));
toggleModuleKey = KeyMappingHelper.registerKeyMapping(new KeyMapping(
"key.attributeswap.toggle", InputConstants.Type.KEYBOARD, InputConstants.KEY_J, category));
ClientTickEvents.END_CLIENT_TICK.register(client -> {
while (openScreenKey.consumeClick()) {
if (client.gui.screen() instanceof AttributeSwapScreen) {
client.gui.setScreen(null);
} else if (client.gui.screen() == null) {
client.gui.setScreen(new AttributeSwapScreen());
}
}
while (toggleModuleKey.consumeClick()) {
setEnabled(!enabled);
}
});
AttackEntityCallback.EVENT.register((player, level, hand, entity, hitResult) -> handleAttack(player, entity));
}
public static boolean isEnabled() {
return enabled;
}
public static int getAttributeSlot() {
return attributeSlot;
}
public static int getEffectSlot() {
return effectSlot;
}
public static void toggleEnabled() {
setEnabled(!enabled);
}
public static void setAttributeSlot(int slot) {
if (!isValidSlot(slot))
return;
attributeSlot = slot;
saveConfig();
}
public static void setEffectSlot(int slot) {
if (!isValidSlot(slot))
return;
effectSlot = slot;
saveConfig();
}
private static InteractionResult handleAttack(Player player, Entity entity) {
Minecraft client = Minecraft.getInstance();
if (!enabled || client.player != player || client.gui.screen() != null || client.gameMode == null)
return InteractionResult.PASS;
if (entity == player || !entity.isAlive() || entity.isRemoved() || !entity.isAttackable())
return InteractionResult.PASS;
if (!isValidSlot(attributeSlot) || !isValidSlot(effectSlot) || attributeSlot == effectSlot)
return InteractionResult.PASS;
ClientPacketListener connection = client.getConnection();
if (connection == null)
return InteractionResult.PASS;
Inventory inventory = player.getInventory();
inventory.setSelectedSlot(attributeSlot);
connection.send(new ServerboundSetCarriedItemPacket(attributeSlot));
player.attack(entity);
player.resetAttackStrengthTicker();
connection.send(new ServerboundAttackPacket(entity.getId()));
inventory.setSelectedSlot(effectSlot);
connection.send(new ServerboundSetCarriedItemPacket(effectSlot));
return InteractionResult.FAIL;
}
private static void setEnabled(boolean value) {
enabled = value;
saveConfig();
}
private static boolean isValidSlot(int slot) {
return slot >= 0 && slot < 9;
}
private static Path configPath() {
return Minecraft.getInstance().gameDirectory.toPath().resolve("config").resolve("attributeswap.properties");
}
private static void loadConfig() {
Properties properties = new Properties();
try (InputStream input = Files.newInputStream(configPath())) {
properties.load(input);
enabled = Boolean.parseBoolean(properties.getProperty("enabled", "false"));
attributeSlot = readSlot(properties, "attributeSlot", DEFAULT_ATTRIBUTE_SLOT);
effectSlot = readSlot(properties, "effectSlot", DEFAULT_EFFECT_SLOT);
} catch (IOException | IllegalArgumentException ignored) {
enabled = false;
attributeSlot = DEFAULT_ATTRIBUTE_SLOT;
effectSlot = DEFAULT_EFFECT_SLOT;
}
}
private static int readSlot(Properties properties, String key, int fallback) {
try {
int slot = Integer.parseInt(properties.getProperty(key, Integer.toString(fallback)));
return isValidSlot(slot) ? slot : fallback;
} catch (NumberFormatException ignored) {
return fallback;
}
}
private static void saveConfig() {
Properties properties = new Properties();
properties.setProperty("enabled", Boolean.toString(enabled));
properties.setProperty("attributeSlot", Integer.toString(attributeSlot));
properties.setProperty("effectSlot", Integer.toString(effectSlot));
Path path = configPath();
try {
Files.createDirectories(path.getParent());
try (OutputStream output = Files.newOutputStream(path)) {
properties.store(output, "Attribute Swap client settings");
}
} catch (IOException ignored) {
}
}
}