Commit 1f070732d7074a46b84b880f839cee86ea1fda18

Authored by Mumfrey
1 parent f7c2ee1f

Fix registration of keybindings with custom categories

src/client/java/com/mumfrey/liteloader/client/GameEngineClient.java
... ... @@ -8,7 +8,9 @@ package com.mumfrey.liteloader.client;
8 8 import java.util.ArrayList;
9 9 import java.util.Arrays;
10 10 import java.util.List;
  11 +import java.util.Map;
11 12  
  13 +import com.mumfrey.liteloader.client.mixin.IKeyBinding;
12 14 import com.mumfrey.liteloader.client.overlays.IMinecraft;
13 15 import com.mumfrey.liteloader.common.GameEngine;
14 16 import com.mumfrey.liteloader.common.Resources;
... ... @@ -29,6 +31,11 @@ import net.minecraft.server.integrated.IntegratedServer;
29 31 */
30 32 public class GameEngineClient implements GameEngine<Minecraft, IntegratedServer>
31 33 {
  34 + /**
  35 + * Base index for custom keybind categories added by mods
  36 + */
  37 + private static final int CATEGORY_SORT_INDEX_START = 1000;
  38 +
32 39 private final Minecraft engine = Minecraft.getMinecraft();
33 40  
34 41 private final Resources<?, ?> resources = new ResourcesClient();
... ... @@ -159,6 +166,49 @@ public class GameEngineClient implements GameEngine&lt;Minecraft, IntegratedServer&gt;
159 166 @Override
160 167 public void setKeyBindings(List<KeyBinding> keyBindings)
161 168 {
  169 + this.addMissingCategories(keyBindings);
162 170 this.engine.gameSettings.keyBindings = keyBindings.toArray(new KeyBinding[0]);
163 171 }
  172 +
  173 + /**
  174 + * Add categories specified in the supplied keybindings list to the list of
  175 + * category sort orders in {@link KeyBinding}.
  176 + *
  177 + * @param keyBindings keybinding list
  178 + */
  179 + private void addMissingCategories(List<KeyBinding> keyBindings)
  180 + {
  181 + Map<String, Integer> categorySort = IKeyBinding.getCategorySort();
  182 +
  183 + for (KeyBinding binding : keyBindings)
  184 + {
  185 + String category = binding.getKeyCategory();
  186 + if (!categorySort.containsKey(category))
  187 + {
  188 + categorySort.put(category, this.getAvailableSortIndex(categorySort));
  189 + }
  190 + }
  191 + }
  192 +
  193 + /**
  194 + * Finds an available sorting index (starting at CATEGORY_SORT_INDEX_START)
  195 + * which exists at the end of the sorting space. This ensures that mod
  196 + * categories are always sorted after vanilla and other preexisting
  197 + * categories.
  198 + *
  199 + * @param categorySort sorting map
  200 + * @return next available sorting index
  201 + */
  202 + private Integer getAvailableSortIndex(Map<String, Integer> categorySort)
  203 + {
  204 + int index = GameEngineClient.CATEGORY_SORT_INDEX_START;
  205 + for (Integer value : categorySort.values())
  206 + {
  207 + if (value.intValue() >= index) {
  208 + index = value.intValue() + 1;
  209 + }
  210 + }
  211 +
  212 + return Integer.valueOf(index);
  213 + }
164 214 }
... ...
src/client/java/com/mumfrey/liteloader/client/mixin/IKeyBinding.java 0 → 100644
  1 +/*
  2 + * This file is part of LiteLoader.
  3 + * Copyright (C) 2012-16 Adam Mummery-Smith
  4 + * All Rights Reserved.
  5 + */
  6 +package com.mumfrey.liteloader.client.mixin;
  7 +
  8 +import java.util.Map;
  9 +
  10 +import org.apache.commons.lang3.NotImplementedException;
  11 +import org.spongepowered.asm.mixin.Mixin;
  12 +import org.spongepowered.asm.mixin.gen.Accessor;
  13 +
  14 +import net.minecraft.client.settings.KeyBinding;
  15 +
  16 +@Mixin(KeyBinding.class)
  17 +public interface IKeyBinding
  18 +{
  19 + @Accessor(value = "field_193627_d")
  20 + public static Map<String, Integer> getCategorySort()
  21 + {
  22 + throw new NotImplementedException("IKeyBinding mixin failed to apply");
  23 + }
  24 +}
... ...
src/client/resources/mixins.liteloader.client.json
... ... @@ -24,7 +24,8 @@
24 24 "MixinGuiTextField",
25 25 "MixinIntIdentityHashBiMap",
26 26 "MixinGuiOverlayDebug",
27   - "IGuiButton"
  27 + "IGuiButton",
  28 + "IKeyBinding"
28 29 ],
29 30 "injectors": {
30 31 "defaultRequire": 1
... ...