Files
src
build.gradle
gradle.properties
settings.gradle
Code
9 316
src/client/java/com/kodari/autorefill/AutoRefillModClient.java
package com.kodari.autorefill;
import com.kodari.autorefill.config.AutoRefillConfig;
import com.kodari.autorefill.config.ConfigManager;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.slot.SlotActionType;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
public class AutoRefillModClient implements ClientModInitializer {
private static final KeyBinding.Category KEY_CATEGORY =
KeyBinding.Category.create(Identifier.of(AutoRefillMod.MOD_ID, "keybinds"));
private static KeyBinding toggleKey;
private static PlayerEntity lastPlayer;
@Override
public void onInitializeClient() {
AutoRefillConfig config = ConfigManager.getConfig();
toggleKey = KeyBindingHelper.registerKeyBinding(
new KeyBinding("key.autorefill.toggle", config.toggleKeyCode, KEY_CATEGORY));
ClientTickEvents.END_CLIENT_TICK.register(AutoRefillModClient::onClientTick);
}
private static void onClientTick(MinecraftClient client) {
if (client.player == null) {
lastPlayer = null;
return;
}
if (client.player != lastPlayer) {
lastPlayer = client.player;
showStatus(client, ConfigManager.getConfig().enabled);
}
while (toggleKey.wasPressed()) {
AutoRefillConfig config = ConfigManager.getConfig();
config.enabled = !config.enabled;
ConfigManager.save();
showStatus(client, config.enabled);
}
AutoRefillConfig config = ConfigManager.getConfig();
if (!config.enabled || client.currentScreen != null || client.interactionManager == null) {
return;
}
refillOneSlot(client, ConfigManager.getRefillItem());
}
private static void refillOneSlot(MinecraftClient client, Item refillItem) {
PlayerEntity player = client.player;
ItemStack[] hotbar = new ItemStack[9];
for (int hotbarSlot = 0; hotbarSlot < hotbar.length; hotbarSlot++) {
hotbar[hotbarSlot] = player.getInventory().getStack(hotbarSlot);
}
for (int hotbarSlot = 0; hotbarSlot < hotbar.length; hotbarSlot++) {
if (!hotbar[hotbarSlot].isEmpty()) {
continue;
}
for (int inventorySlot = 9; inventorySlot < 36; inventorySlot++) {
if (!player.getInventory().getStack(inventorySlot).isOf(refillItem)) {
continue;
}
int hotbarScreenSlot = 36 + hotbarSlot;
client.interactionManager.clickSlot(
player.currentScreenHandler.syncId,
inventorySlot,
0,
SlotActionType.PICKUP,
player);
client.interactionManager.clickSlot(
player.currentScreenHandler.syncId,
hotbarScreenSlot,
0,
SlotActionType.PICKUP,
player);
return;
}
}
}
private static void showStatus(MinecraftClient client, boolean enabled) {
Text state = Text.translatable(enabled ? "message.autorefill.on" : "message.autorefill.off");
client.player.sendMessage(Text.translatable("message.autorefill.status", state), true);
}
}