|
22 | 22 | * Represents a generic enum converter.
|
23 | 23 | * @author Kristian
|
24 | 24 | */
|
| 25 | +@SuppressWarnings({"unchecked","rawtypes"}) |
25 | 26 | public abstract class EnumWrappers {
|
26 | 27 | public enum ClientCommand {
|
27 | 28 | PERFORM_RESPAWN,
|
@@ -161,8 +162,8 @@ public enum PlayerDigType {
|
161 | 162 | }
|
162 | 163 |
|
163 | 164 | public enum PlayerAction implements AliasedEnum {
|
164 |
| - PRESS_SHIFT_KEY("START_SNEAKING"), |
165 |
| - RELEASE_SHIFT_KEY("STOP_SNEAKING"), |
| 165 | + START_SNEAKING("PRESS_SHIFT_KEY"), |
| 166 | + STOP_SNEAKING("RELEASE_SHIFT_KEY"), |
166 | 167 | STOP_SLEEPING,
|
167 | 168 | START_SPRINTING,
|
168 | 169 | STOP_SPRINTING,
|
@@ -677,8 +678,9 @@ public static <T extends Enum<T>> EquivalentConverter<T> getGenericConverter(Cla
|
677 | 678 | return new EnumConverter<>(null, specificType);
|
678 | 679 | }
|
679 | 680 |
|
680 |
| - // The common enum converter |
681 |
| - @SuppressWarnings({ "rawtypes", "unchecked" }) |
| 681 | + /** |
| 682 | + * The common Enum converter |
| 683 | + */ |
682 | 684 | public static class EnumConverter<T extends Enum<T>> implements EquivalentConverter<T> {
|
683 | 685 | private Class<?> genericType;
|
684 | 686 | private Class<T> specificType;
|
@@ -712,62 +714,68 @@ public interface AliasedEnum {
|
712 | 714 | String[] getAliases();
|
713 | 715 | }
|
714 | 716 |
|
| 717 | + /** |
| 718 | + * Enums whose name has changed across NMS versions. Enums using this must also implement {@link AliasedEnum} |
| 719 | + */ |
715 | 720 | public static class AliasedEnumConverter<T extends Enum<T> & AliasedEnum> implements EquivalentConverter<T> {
|
716 | 721 | private Class<?> genericType;
|
717 | 722 | private Class<T> specificType;
|
718 | 723 |
|
| 724 | + private Map<T, Object> genericMap = new ConcurrentHashMap<>(); |
| 725 | + private Map<Object, T> specificMap = new ConcurrentHashMap<>(); |
| 726 | + |
719 | 727 | public AliasedEnumConverter(Class<?> genericType, Class<T> specificType) {
|
720 | 728 | this.genericType = genericType;
|
721 | 729 | this.specificType = specificType;
|
722 | 730 | }
|
723 | 731 |
|
724 | 732 | @Override
|
725 | 733 | public T getSpecific(Object generic) {
|
726 |
| - String name = ((Enum) generic).name(); |
727 |
| - |
728 |
| - try { |
729 |
| - return Enum.valueOf(specificType, name); |
730 |
| - } catch (Exception ex) { |
731 |
| - // TODO would caching help much, if at all? |
732 |
| - for (T elem : specificType.getEnumConstants()) { |
733 |
| - for (String alias : elem.getAliases()) { |
734 |
| - if (alias.equals(name)) { |
735 |
| - return elem; |
| 734 | + return specificMap.computeIfAbsent(generic, x -> { |
| 735 | + String name = ((Enum) generic).name(); |
| 736 | + |
| 737 | + try { |
| 738 | + return Enum.valueOf(specificType, name); |
| 739 | + } catch (Exception ex) { |
| 740 | + for (T elem : specificType.getEnumConstants()) { |
| 741 | + for (String alias : elem.getAliases()) { |
| 742 | + if (alias.equals(name)) { |
| 743 | + return elem; |
| 744 | + } |
736 | 745 | }
|
737 | 746 | }
|
738 | 747 | }
|
739 |
| - } |
740 | 748 |
|
741 |
| - throw new IllegalArgumentException("Unknown enum constant " + name); |
| 749 | + throw new IllegalArgumentException("Unknown enum constant " + name); |
| 750 | + }); |
742 | 751 | }
|
743 | 752 |
|
744 | 753 | @Override
|
745 | 754 | public Object getGeneric(T specific) {
|
746 |
| - String name = specific.name(); |
747 |
| - |
748 |
| - try { |
749 |
| - return Enum.valueOf((Class) genericType, specific.name()); |
750 |
| - } catch (Exception ex) { |
751 |
| - for (Object elem : genericType.getEnumConstants()) { |
752 |
| - for (String alias : specific.getAliases()) { |
753 |
| - if (alias.equals(name)) { |
754 |
| - return elem; |
| 755 | + return genericMap.computeIfAbsent(specific, x -> { |
| 756 | + String name = specific.name(); |
| 757 | + |
| 758 | + try { |
| 759 | + return Enum.valueOf((Class) genericType, specific.name()); |
| 760 | + } catch (Exception ex) { |
| 761 | + for (Object rawElem : genericType.getEnumConstants()) { |
| 762 | + Enum elem = (Enum) rawElem; |
| 763 | + for (String alias : specific.getAliases()) { |
| 764 | + if (alias.equals(elem.name())) { |
| 765 | + return elem; |
| 766 | + } |
755 | 767 | }
|
756 | 768 | }
|
757 | 769 | }
|
758 |
| - } |
759 | 770 |
|
760 |
| - throw new IllegalArgumentException("Unknown enum constant " + name); |
| 771 | + throw new IllegalArgumentException("Unknown enum constant " + name); |
| 772 | + }); |
761 | 773 | }
|
762 | 774 |
|
763 | 775 | @Override
|
764 | 776 | public Class<T> getSpecificType() {
|
765 | 777 | return specificType;
|
766 | 778 | }
|
767 |
| - |
768 |
| - void setGenericType(Class<?> genericType) { |
769 |
| - this.genericType = genericType; |
770 |
| - } |
771 | 779 | }
|
772 | 780 |
|
773 | 781 | /**
|
|
0 commit comments