Shmeeb

Untitled

Mar 10th, 2018
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.80 KB | None | 0 0
  1. package net.shmeeb.adminshopec.data;
  2.  
  3. import java.util.Optional;
  4. import org.spongepowered.api.Sponge;
  5. import org.spongepowered.api.data.DataContainer;
  6. import org.spongepowered.api.data.DataHolder;
  7. import org.spongepowered.api.data.DataView;
  8. import org.spongepowered.api.data.manipulator.DataManipulatorBuilder;
  9. import org.spongepowered.api.data.manipulator.immutable.common.AbstractImmutableData;
  10. import org.spongepowered.api.data.manipulator.mutable.common.AbstractData;
  11. import org.spongepowered.api.data.merge.MergeFunction;
  12. import org.spongepowered.api.data.persistence.AbstractDataBuilder;
  13. import org.spongepowered.api.data.persistence.InvalidDataException;
  14. import org.spongepowered.api.data.value.immutable.ImmutableValue;
  15. import org.spongepowered.api.data.value.mutable.Value;
  16. import org.spongepowered.api.item.inventory.ItemStack;
  17.  
  18. public class Data extends AbstractData<Data, Data.Immutable> {
  19. private ItemStack item;
  20. private Integer cost;
  21. private Integer amount;
  22. private boolean buy;
  23.  
  24. {
  25. registerGettersAndSetters();
  26. }
  27.  
  28. Data() {
  29. buy = false;
  30. }
  31.  
  32. public Data(ItemStack item, Integer cost, Integer amount, boolean buy) {
  33. this.item = item;
  34. this.cost = cost;
  35. this.amount = amount;
  36. this.buy = buy;
  37. }
  38.  
  39. @Override
  40. protected void registerGettersAndSetters() {
  41. registerFieldGetter(Keys.ITEM, this::getItem);
  42. registerFieldSetter(Keys.ITEM, this::setItem);
  43. registerKeyValue(Keys.ITEM, this::item);
  44. registerFieldGetter(Keys.COST, this::getCost);
  45. registerFieldSetter(Keys.COST, this::setCost);
  46. registerKeyValue(Keys.COST, this::cost);
  47. registerFieldGetter(Keys.AMOUNT, this::getAmount);
  48. registerFieldSetter(Keys.AMOUNT, this::setAmount);
  49. registerKeyValue(Keys.AMOUNT, this::amount);
  50. registerFieldGetter(Keys.BUY, this::isBuy);
  51. registerFieldSetter(Keys.BUY, this::setBuy);
  52. registerKeyValue(Keys.BUY, this::buy);
  53. }
  54.  
  55. public ItemStack getItem() {
  56. return item;
  57. }
  58.  
  59. public void setItem(ItemStack item) {
  60. this.item = item;
  61. }
  62.  
  63. public Value<ItemStack> item() {
  64. return Sponge.getRegistry().getValueFactory().createValue(Keys.ITEM, item);
  65. }
  66.  
  67. public Integer getCost() {
  68. return cost;
  69. }
  70.  
  71. public void setCost(Integer cost) {
  72. this.cost = cost;
  73. }
  74.  
  75. public Value<Integer> cost() {
  76. return Sponge.getRegistry().getValueFactory().createValue(Keys.COST, cost);
  77. }
  78.  
  79. public Integer getAmount() {
  80. return amount;
  81. }
  82.  
  83. public void setAmount(Integer amount) {
  84. this.amount = amount;
  85. }
  86.  
  87. public Value<Integer> amount() {
  88. return Sponge.getRegistry().getValueFactory().createValue(Keys.AMOUNT, amount);
  89. }
  90.  
  91. public boolean isBuy() {
  92. return buy;
  93. }
  94.  
  95. public void setBuy(boolean buy) {
  96. this.buy = buy;
  97. }
  98.  
  99. public Value<Boolean> buy() {
  100. return Sponge.getRegistry().getValueFactory().createValue(Keys.BUY, buy);
  101. }
  102.  
  103. @Override
  104. public Optional<Data> fill(DataHolder dataHolder, MergeFunction overlap) {
  105. dataHolder.get(Data.class).ifPresent(that -> {
  106. Data data = overlap.merge(this, that);
  107. this.item = data.item;
  108. this.cost = data.cost;
  109. this.amount = data.amount;
  110. this.buy = data.buy;
  111. });
  112. return Optional.of(this);
  113. }
  114.  
  115. @Override
  116. public Optional<Data> from(DataContainer container) {
  117. return from((DataView) container);
  118. }
  119.  
  120. public Optional<Data> from(DataView container) {
  121. container.getObject(Keys.ITEM.getQuery(), ItemStack.class).ifPresent(v -> item = v);
  122. container.getObject(Keys.COST.getQuery(), Integer.class).ifPresent(v -> cost = v);
  123. container.getObject(Keys.AMOUNT.getQuery(), Integer.class).ifPresent(v -> amount = v);
  124. container.getBoolean(Keys.BUY.getQuery()).ifPresent(v -> buy = v);
  125. return Optional.of(this);
  126. }
  127.  
  128. @Override
  129. public Data copy() {
  130. return new Data(item, cost, amount, buy);
  131. }
  132.  
  133. @Override
  134. public Immutable asImmutable() {
  135. return new Immutable(item, cost, amount, buy);
  136. }
  137.  
  138. @Override
  139. public int getContentVersion() {
  140. return 1;
  141. }
  142.  
  143. @Override
  144. public DataContainer toContainer() {
  145. return super.toContainer()
  146. .set(Keys.ITEM.getQuery(), item)
  147. .set(Keys.COST.getQuery(), cost)
  148. .set(Keys.AMOUNT.getQuery(), amount)
  149. .set(Keys.BUY.getQuery(), buy);
  150. }
  151.  
  152. public static class Immutable extends AbstractImmutableData<Immutable, Data> {
  153.  
  154. private ItemStack item;
  155. private Integer cost;
  156. private Integer amount;
  157. private boolean buy;
  158. {
  159. registerGetters();
  160. }
  161.  
  162. Immutable() {
  163. buy = false;
  164. }
  165.  
  166. Immutable(ItemStack item, Integer cost, Integer amount, boolean buy) {
  167. this.item = item;
  168. this.cost = cost;
  169. this.amount = amount;
  170. this.buy = buy;
  171. }
  172.  
  173. @Override
  174. protected void registerGetters() {
  175. registerFieldGetter(Keys.ITEM, this::getItem);
  176. registerKeyValue(Keys.ITEM, this::item);
  177. registerFieldGetter(Keys.COST, this::getCost);
  178. registerKeyValue(Keys.COST, this::cost);
  179. registerFieldGetter(Keys.AMOUNT, this::getAmount);
  180. registerKeyValue(Keys.AMOUNT, this::amount);
  181. registerFieldGetter(Keys.BUY, this::isBuy);
  182. registerKeyValue(Keys.BUY, this::buy);
  183. }
  184.  
  185. public ItemStack getItem() {
  186. return item;
  187. }
  188.  
  189. public ImmutableValue<ItemStack> item() {
  190. return Sponge.getRegistry().getValueFactory().createValue(Keys.ITEM, item).asImmutable();
  191. }
  192.  
  193. public Integer getCost() {
  194. return cost;
  195. }
  196.  
  197. public ImmutableValue<Integer> cost() {
  198. return Sponge.getRegistry().getValueFactory().createValue(Keys.COST, cost).asImmutable();
  199. }
  200.  
  201. public Integer getAmount() {
  202. return amount;
  203. }
  204.  
  205. public ImmutableValue<Integer> amount() {
  206. return Sponge.getRegistry().getValueFactory().createValue(Keys.AMOUNT, amount).asImmutable();
  207. }
  208.  
  209. public boolean isBuy() {
  210. return buy;
  211. }
  212.  
  213. public ImmutableValue<Boolean> buy() {
  214. return Sponge.getRegistry().getValueFactory().createValue(Keys.BUY, buy).asImmutable();
  215. }
  216.  
  217. @Override
  218. public Data asMutable() {
  219. return new Data(item, cost, amount, buy);
  220. }
  221.  
  222. @Override
  223. public int getContentVersion() {
  224. return 1;
  225. }
  226.  
  227. @Override
  228. public DataContainer toContainer() {
  229. return super.toContainer()
  230. .set(Keys.ITEM.getQuery(), item)
  231. .set(Keys.COST.getQuery(), cost)
  232. .set(Keys.AMOUNT.getQuery(), amount)
  233. .set(Keys.BUY.getQuery(), buy);
  234. }
  235.  
  236. }
  237.  
  238. public static class Builder extends AbstractDataBuilder<Data> implements DataManipulatorBuilder<Data, Immutable> {
  239.  
  240. public Builder() {
  241. super(Data.class, 1);
  242. }
  243.  
  244. @Override
  245. public Data create() {
  246. return new Data();
  247. }
  248.  
  249. @Override
  250. public Optional<Data> createFrom(DataHolder dataHolder) {
  251. return create().fill(dataHolder);
  252. }
  253.  
  254. @Override
  255. protected Optional<Data> buildContent(DataView container) throws InvalidDataException {
  256. return create().from(container);
  257. }
  258.  
  259. }
  260. }
Advertisement
Add Comment
Please, Sign In to add comment