Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. package com.arlania.world.content.upgrade;
  2.  
  3. import com.arlania.model.Item;
  4. import com.arlania.util.JsonLoader;
  5. import com.arlania.world.entity.impl.player.Player;
  6. import com.google.common.collect.ObjectArrays;
  7. import com.google.gson.Gson;
  8. import com.google.gson.GsonBuilder;
  9. import com.google.gson.JsonObject;
  10. import it.unimi.dsi.fastutil.objects.ObjectArrayList;
  11. import it.unimi.dsi.fastutil.objects.ObjectList;
  12.  
  13. import java.util.Arrays;
  14. import java.util.List;
  15.  
  16. /**
  17. * @author Tamatea#0001
  18. */
  19. public class Upgrade {
  20.  
  21. private static final ObjectList<Upgradeable> UPGRADEABLES = new ObjectArrayList<>();
  22.  
  23. /**
  24. * Cache the items so we don't have to make a list of them every time a player views the interface
  25. */
  26. private static final ObjectArrayList<Item> UPGRADEABLES_ITEMS = new ObjectArrayList<>();
  27.  
  28. private static final int SUCCESS_RATE_STRING = 29006;
  29. private static final int REQUIREMENT_ITEM_CONTAINER = 29013;
  30. private static final int END_ITEM_CHILD = 29014;
  31. private static final int LIST_ITEM_CONTAINER = 29021;
  32.  
  33. public static void open(Player player, int index) {
  34. if(!player.getClickDelay().elapsed(600))
  35. return;
  36. player.getClickDelay().reset();
  37. if (index > UPGRADEABLES.size())
  38. return;
  39.  
  40. player.setAttribute("UPGRADEABLE", index);
  41. player.getPacketSender().sendInterface(29000);
  42. Upgradeable upgradeable = UPGRADEABLES.get(index);
  43. player.getPacketSender().sendItemsOnInterface(REQUIREMENT_ITEM_CONTAINER, 4, List.of(upgradeable.getReqItem()), true);
  44. player.getPacketSender().sendFrame126(SUCCESS_RATE_STRING, "@gr1@Success rate: @whi@"+(upgradeable.getChance() * 10)+"%");
  45. player.getPacketSender().sendItemOnInterface(END_ITEM_CHILD, upgradeable.endItem, 0);
  46. player.getPacketSender().sendItemsOnInterface(LIST_ITEM_CONTAINER, 80, UPGRADEABLES_ITEMS, true);
  47. }
  48.  
  49. public static void doUpgrade(Player player) {
  50. int index = player.getAttribute("UPGRADEABLE");
  51. if(index > UPGRADEABLES.size())
  52. return;
  53. Upgradeable upgradeable = UPGRADEABLES.get(index);
  54. if(upgradeable == null)
  55. return;
  56. if(upgradeable.hasRequirement(player)) {
  57. for(Item item : upgradeable.reqItem) {
  58. player.getInventory().delete(item);
  59. }
  60. player.getInventory().add(upgradeable.endItem, 1);
  61. } else {
  62. player.sendMessage("@red@You do not have the required items to make this upgrade!");
  63. }
  64. }
  65.  
  66.  
  67. public static void init() {
  68. Gson GSON = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create();
  69.  
  70. new JsonLoader() {
  71.  
  72. @Override
  73. public void load(JsonObject reader, Gson builder) {
  74. int startItem = reader.get("STARTER_ITEM").getAsInt();
  75. Item[] items, required = GSON.fromJson(reader.getAsJsonArray("REQUIREMENTS"), Item[].class);
  76. if(Arrays.stream(required).noneMatch(i -> i.getId() == startItem))
  77. items = ObjectArrays.concat(new Item(startItem, 1), GSON.fromJson(reader.getAsJsonArray("REQUIREMENTS"), Item[].class));
  78. else items = required;
  79. int chance = reader.get("SUCCESS_CHANCE").getAsInt();
  80. int endItem = reader.get("END_ITEM").getAsInt();
  81. UPGRADEABLES.add(new Upgradeable(startItem, items, chance, endItem));
  82. }
  83.  
  84. @Override
  85. public String filePath() {
  86. return "./data/def/json/upgrading.json";
  87. }
  88. }.load();
  89. System.out.println("loaded "+UPGRADEABLES.size()+" upgradeables");
  90. for(Upgradeable u : UPGRADEABLES) {
  91. UPGRADEABLES_ITEMS.add(new Item(u.startItem));
  92. }
  93. }
  94.  
  95. public static class Upgradeable {
  96.  
  97. private Item[] reqItem;
  98. private int startItem;
  99. private int chance;
  100. private int endItem;
  101.  
  102. Upgradeable(int startItem, Item[] reqItem, int chance, int EndItem) {
  103. this.startItem = startItem;
  104. this.reqItem = reqItem;
  105. this.chance = chance;
  106. this.endItem = EndItem;
  107. }
  108.  
  109. public int getChance() {
  110. return chance;
  111. }
  112.  
  113. public int getEndItem() {
  114. return endItem;
  115. }
  116. public int getStartItem() {
  117. return startItem;
  118. }
  119.  
  120. public Item[] getReqItem() {
  121. return reqItem;
  122. }
  123. public boolean hasRequirement(Player player) {
  124. return player.getInventory().containsAll(reqItem);
  125. }
  126.  
  127. public static Upgradeable of(int id) {
  128. for(Upgradeable upgradeable : UPGRADEABLES) {
  129. if(upgradeable.startItem == id)
  130. return upgradeable;
  131. }
  132. return null;
  133. }
  134. }
  135.  
  136.  
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement