CustomXPPerkSerializer.java 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package com.gmail.nossr50.config.hocon;
  2. import com.gmail.nossr50.api.exceptions.InvalidSkillException;
  3. import com.gmail.nossr50.datatypes.experience.CustomXPPerk;
  4. import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
  5. import com.gmail.nossr50.mcMMO;
  6. import com.google.common.reflect.TypeResolver;
  7. import com.google.common.reflect.TypeToken;
  8. import ninja.leaping.configurate.ConfigurationNode;
  9. import ninja.leaping.configurate.objectmapping.ObjectMappingException;
  10. import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer;
  11. import org.checkerframework.checker.nullness.qual.NonNull;
  12. import org.checkerframework.checker.nullness.qual.Nullable;
  13. public class CustomXPPerkSerializer implements TypeSerializer<CustomXPPerk> {
  14. @Nullable
  15. @Override
  16. public CustomXPPerk deserialize(@NonNull TypeToken<?> type, @NonNull ConfigurationNode value) throws ObjectMappingException {
  17. String perkName = value.getNode("name").getValue(TypeToken.of(String.class));
  18. CustomXPPerk customXPPerk = new CustomXPPerk(perkName);
  19. //See if any children nodes match skills by name
  20. for(ConfigurationNode configurationNode : value.getChildrenList())
  21. {
  22. try {
  23. PrimarySkillType primarySkillType = matchIgnoreCase(configurationNode.getValue(TypeToken.of(String.class)));
  24. float boostValue = configurationNode.getNode("XP-Multiplier").getValue(TypeToken.of(Float.class));
  25. customXPPerk.setCustomXPValue(primarySkillType, boostValue);
  26. } catch (InvalidSkillException e) {
  27. mcMMO.p.getLogger().info("Custom XP perk has a skill defined that was not found, did you misspell it?");
  28. e.printStackTrace();
  29. } catch (ObjectMappingException e) {
  30. e.printStackTrace();
  31. }
  32. }
  33. return customXPPerk;
  34. }
  35. @Override
  36. public void serialize(@NonNull TypeToken<?> type, @Nullable CustomXPPerk obj, @NonNull ConfigurationNode value) throws ObjectMappingException {
  37. }
  38. private PrimarySkillType matchIgnoreCase(String string) throws InvalidSkillException
  39. {
  40. for(PrimarySkillType primarySkillType : PrimarySkillType.values())
  41. {
  42. if(string.equalsIgnoreCase(primarySkillType.toString()))
  43. return primarySkillType;
  44. }
  45. throw new InvalidSkillException();
  46. }
  47. /*
  48. CustomXPPerk customXPPerk = new CustomXPPerk("examplecustomxpperk");
  49. customXPPerk.setCustomXPValue(PrimarySkillType.MINING, 13.37f);
  50. customXPPerk.setCustomXPValue(PrimarySkillType.WOODCUTTING, 4.0f);
  51. */
  52. }