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.ArrayList;
|
|
|
|
import java.util.Collection;
|
|
|
|
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.List;
|
|
|
|
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-04-15 17:22:29 +02:00
|
|
|
import com.minelittlepony.unicopia.ability.Abilities;
|
2020-04-25 15:37:17 +02:00
|
|
|
import com.minelittlepony.unicopia.ability.Ability;
|
|
|
|
import com.minelittlepony.unicopia.ability.data.Hit;
|
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-04-25 15:37:17 +02:00
|
|
|
private final String KEY_CATEGORY = "unicopia.category.name";
|
|
|
|
|
|
|
|
private final Map<KeyBinding, List<Ability<? extends Hit>>> keyPools = new HashMap<>();
|
2020-01-16 12:35:46 +01:00
|
|
|
|
2020-04-15 15:45:57 +02:00
|
|
|
private final Set<KeyBinding> bindings = 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-25 15:37:17 +02:00
|
|
|
private Collection<Ability<?>> getKeyCodePool(KeyBinding keyCode) {
|
|
|
|
return keyPools.computeIfAbsent(keyCode, i -> new ArrayList<>());
|
|
|
|
}
|
2020-01-16 12:35:46 +01:00
|
|
|
|
2020-04-25 15:37:17 +02:00
|
|
|
public void addKeybind(Ability<?> p) {
|
|
|
|
KeyBindingRegistry.INSTANCE.addCategory(KEY_CATEGORY);
|
2020-01-16 12:35:46 +01:00
|
|
|
|
2020-04-25 15:37:17 +02:00
|
|
|
Identifier id = Abilities.REGISTRY.getId(p);
|
|
|
|
int code = Abilities.KEYS_CODES.get(id);
|
|
|
|
|
|
|
|
FabricKeyBinding b = FabricKeyBinding.Builder.create(id, InputUtil.Type.KEYSYM, code, KEY_CATEGORY).build();
|
|
|
|
KeyBindingRegistry.INSTANCE.register(b);
|
|
|
|
getKeyCodePool(b).add(p);
|
2020-01-16 12:35:46 +01:00
|
|
|
bindings.add(b);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
for (KeyBinding i : bindings) {
|
|
|
|
if (i.isPressed()) {
|
|
|
|
|
2020-04-15 15:45:57 +02:00
|
|
|
if (pressed.add(i)) {
|
2020-04-25 15:37:17 +02:00
|
|
|
getKeyCodePool(i)
|
|
|
|
.stream()
|
|
|
|
.filter(power -> power.canUse(iplayer.getSpecies()))
|
|
|
|
.findFirst()
|
|
|
|
.ifPresent(iplayer.getAbilities()::tryUseAbility);
|
2020-01-16 12:35:46 +01:00
|
|
|
}
|
2020-04-15 15:45:57 +02:00
|
|
|
} else if (pressed.remove(i)) {
|
|
|
|
iplayer.getAbilities().tryClearAbility();
|
2020-01-16 12:35:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|