2020-04-15 14:22:03 +02:00
|
|
|
package com.minelittlepony.unicopia.client;
|
2020-01-16 12:35:46 +01:00
|
|
|
|
2020-04-25 15:37:17 +02:00
|
|
|
import java.util.HashMap;
|
2020-04-15 15:45:57 +02:00
|
|
|
import java.util.HashSet;
|
2020-04-25 15:37:17 +02:00
|
|
|
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-09-22 15:11:20 +02:00
|
|
|
import com.minelittlepony.unicopia.entity.player.Pony;
|
2020-01-16 12:35:46 +01:00
|
|
|
|
2020-06-26 11:44:47 +02:00
|
|
|
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
|
2020-01-16 12:35:46 +01:00
|
|
|
import net.minecraft.client.MinecraftClient;
|
|
|
|
import net.minecraft.client.options.KeyBinding;
|
|
|
|
|
2020-09-22 18:13:20 +02:00
|
|
|
public class KeyBindingsHandler {
|
2020-04-25 15:37:17 +02:00
|
|
|
private final String KEY_CATEGORY = "unicopia.category.name";
|
|
|
|
|
2020-09-22 18:13:20 +02:00
|
|
|
public static final KeyBindingsHandler INSTANCE = new KeyBindingsHandler();
|
|
|
|
|
2020-09-25 13:06:41 +02:00
|
|
|
static void bootstrap() {}
|
|
|
|
|
2020-05-06 15:55:25 +02:00
|
|
|
private final Map<KeyBinding, AbilitySlot> keys = new HashMap<>();
|
2020-09-22 18:13:20 +02:00
|
|
|
private final Map<AbilitySlot, KeyBinding> reverse = 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() {
|
2020-09-25 13:06:41 +02:00
|
|
|
addKeybind(GLFW.GLFW_KEY_R, AbilitySlot.PRIMARY);
|
2020-10-02 21:44:33 +02:00
|
|
|
addKeybind(GLFW.GLFW_KEY_G, AbilitySlot.SECONDARY);
|
|
|
|
addKeybind(GLFW.GLFW_KEY_V, AbilitySlot.TERTIARY);
|
2020-04-25 15:37:17 +02:00
|
|
|
}
|
2020-01-16 12:35:46 +01:00
|
|
|
|
2020-09-22 18:13:20 +02:00
|
|
|
public KeyBinding getBinding(AbilitySlot slot) {
|
|
|
|
return reverse.get(slot);
|
|
|
|
}
|
|
|
|
|
2020-05-06 15:55:25 +02:00
|
|
|
public void addKeybind(int code, AbilitySlot slot) {
|
2020-09-25 13:06:41 +02:00
|
|
|
KeyBinding binding = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.unicopia." + slot.name().toLowerCase(), code, KEY_CATEGORY));
|
2020-09-22 18:13:20 +02:00
|
|
|
reverse.put(slot, binding);
|
|
|
|
keys.put(binding, slot);
|
2020-01-16 12:35:46 +01:00
|
|
|
}
|
|
|
|
|
2020-04-25 15:37:17 +02: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
|
|
|
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-10 20:45:07 +02:00
|
|
|
iplayer.getAbilities().clear(slot);
|
2020-01-16 12:35:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|