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

66 lines
2.4 KiB
Java
Raw Normal View History

package com.minelittlepony.unicopia.client;
2020-01-16 12:35:46 +01:00
2020-04-15 15:45:57 +02:00
import java.util.HashSet;
import java.util.Set;
2020-01-16 12:35:46 +01:00
2020-04-15 19:06:45 +02:00
import com.minelittlepony.unicopia.KeyBind;
2020-04-16 00:44:58 +02:00
import com.minelittlepony.unicopia.Unicopia;
2020-04-15 17:22:29 +02:00
import com.minelittlepony.unicopia.ability.Abilities;
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 {
2020-01-16 12:35:46 +01:00
private final MinecraftClient client = MinecraftClient.getInstance();
2020-04-15 15:45:57 +02:00
private final Set<KeyBinding> bindings = new HashSet<>();
private final Set<KeyBinding> removed = new HashSet<>();
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-04-15 19:06:45 +02:00
public void addKeybind(KeyBind p) {
2020-01-16 12:35:46 +01:00
KeyBindingRegistry.INSTANCE.addCategory(p.getKeyCategory());
2020-04-16 00:44:58 +02:00
FabricKeyBinding b = FabricKeyBinding.Builder.create(new Identifier(Unicopia.MODID, p.getKeyName()), InputUtil.Type.KEYSYM, p.getKeyCode(), p.getKeyCategory()).build();
2020-01-16 12:35:46 +01:00
KeyBindingRegistry.INSTANCE.register(b);
bindings.add(b);
}
public void onKeyInput() {
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
for (KeyBinding i : bindings) {
if (i.isPressed()) {
2020-04-15 15:45:57 +02:00
if (pressed.add(i)) {
2020-04-15 17:22:29 +02:00
if (!Abilities.getInstance().hasRegisteredPower(i.getDefaultKeyCode().getKeyCode())) {
2020-01-16 12:35:46 +01:00
removed.add(i);
System.out.println("Error: Keybinding(" + i.getLocalizedName() + ") does not have a registered pony power. Keybinding will be removed from event.");
} else {
2020-04-15 17:22:29 +02:00
Abilities.getInstance()
2020-01-16 12:35:46 +01:00
.getCapablePowerFromKey(i.getDefaultKeyCode().getKeyCode(), iplayer.getSpecies())
.ifPresent(iplayer.getAbilities()::tryUseAbility);
}
}
2020-04-15 15:45:57 +02:00
} else if (pressed.remove(i)) {
iplayer.getAbilities().tryClearAbility();
2020-01-16 12:35:46 +01:00
}
}
2020-04-15 15:45:57 +02:00
bindings.removeAll(removed);
pressed.removeAll(removed);
removed.clear();
2020-01-16 12:35:46 +01:00
}
}