Unicopia/src/main/java/com/minelittlepony/unicopia/client/KeyBindingsHandler.java

69 lines
2.3 KiB
Java
Raw Normal View History

package com.minelittlepony.unicopia.client;
2020-01-16 12:35:46 +01:00
import java.util.HashMap;
2020-04-15 15:45:57 +02:00
import java.util.HashSet;
import java.util.Map;
2020-04-15 15:45:57 +02:00
import java.util.Set;
2020-01-16 12:35:46 +01:00
2020-05-06 15:55:25 +02:00
import org.lwjgl.glfw.GLFW;
import com.minelittlepony.unicopia.ability.AbilitySlot;
2020-04-15 18:12:00 +02:00
import com.minelittlepony.unicopia.entity.player.Pony;
2020-01-16 12:35:46 +01:00
import net.fabricmc.fabric.api.client.keybinding.FabricKeyBinding;
import net.fabricmc.fabric.api.client.keybinding.KeyBindingRegistry;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.options.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.minecraft.util.Identifier;
2020-04-15 15:55:18 +02:00
class KeyBindingsHandler {
private final String KEY_CATEGORY = "unicopia.category.name";
2020-05-06 15:55:25 +02:00
private final Map<KeyBinding, AbilitySlot> keys = new HashMap<>();
2020-01-16 12:35:46 +01:00
2020-04-15 15:45:57 +02:00
private final Set<KeyBinding> pressed = new HashSet<>();
2020-01-16 12:35:46 +01:00
2020-05-06 15:55:25 +02:00
public KeyBindingsHandler() {
KeyBindingRegistry.INSTANCE.addCategory(KEY_CATEGORY);
addKeybind(GLFW.GLFW_KEY_O, AbilitySlot.PRIMARY);
addKeybind(GLFW.GLFW_KEY_P, AbilitySlot.SECONDARY);
addKeybind(GLFW.GLFW_KEY_L, AbilitySlot.TERTIARY);
}
2020-01-16 12:35:46 +01:00
2020-05-06 15:55:25 +02:00
public void addKeybind(int code, AbilitySlot slot) {
KeyBindingRegistry.INSTANCE.addCategory(KEY_CATEGORY);
2020-01-16 12:35:46 +01:00
2020-05-06 15:55:25 +02:00
FabricKeyBinding b = FabricKeyBinding.Builder.create(new Identifier("unicopia", slot.name().toLowerCase()), InputUtil.Type.KEYSYM, code, KEY_CATEGORY).build();
2020-05-03 19:20:51 +02:00
KeyBindingRegistry.INSTANCE.register(b);
2020-05-06 15:55:25 +02:00
keys.put(b, slot);
2020-01-16 12:35:46 +01:00
}
public void tick(MinecraftClient client) {
2020-01-16 12:35:46 +01:00
if (client.currentScreen != null
|| client.player == null) {
return;
}
2020-04-15 18:12:00 +02:00
Pony iplayer = Pony.of(client.player);
2020-01-16 12:35:46 +01:00
2020-05-06 15:55:25 +02:00
for (KeyBinding i : keys.keySet()) {
AbilitySlot slot = keys.get(i);
if (slot == AbilitySlot.PRIMARY && client.options.keySneak.isPressed()) {
slot = AbilitySlot.PASSIVE;
}
2020-01-16 12:35:46 +01:00
if (i.isPressed()) {
2020-04-15 15:45:57 +02:00
if (pressed.add(i)) {
2020-05-06 15:55:25 +02:00
System.out.println("Key down " + slot);
iplayer.getAbilities().activate(slot);
2020-01-16 12:35:46 +01:00
}
2020-04-15 15:45:57 +02:00
} else if (pressed.remove(i)) {
2020-05-06 15:55:25 +02:00
System.out.println("Key up " + slot);
iplayer.getAbilities().cancelAbility(slot);
2020-01-16 12:35:46 +01:00
}
}
}
}