Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.07 KB | None | 0 0
  1. package com.prennet.boomod.boosautolock.containerlockdata;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Optional;
  6.  
  7. import org.spongepowered.api.Sponge;
  8. import org.spongepowered.api.data.DataContainer;
  9. import org.spongepowered.api.data.DataHolder;
  10. import org.spongepowered.api.data.DataSerializable;
  11. import org.spongepowered.api.data.MemoryDataContainer;
  12. import org.spongepowered.api.data.manipulator.mutable.common.AbstractData;
  13. import org.spongepowered.api.data.merge.MergeFunction;
  14. import org.spongepowered.api.data.value.mutable.Value;
  15.  
  16. import com.google.common.base.Objects;
  17.  
  18. //the impliments DataSerializable - is not in the fakedata example, but is in other codes
  19. // doesn't do squat for the warning in logs, but may be required for writing more complex nbt?
  20. public class ContainerLockData extends AbstractData<ContainerLockData, ImmutableContainerLockData> implements DataSerializable {
  21.  
  22. private boolean cboolServerOwned;
  23. private List<String> csetUUIDsAllowed;
  24. private boolean cboolAllowAnyone;
  25.  
  26. public ContainerLockData() {
  27. this(new ArrayList<String>(), false, false); // defaulting through to the parameterized constructor below
  28. }
  29.  
  30. public ContainerLockData( List<String> setuserids, boolean serverOwned, boolean allowAnyone ) {
  31. this.cboolServerOwned = serverOwned;
  32. this.csetUUIDsAllowed = setuserids;
  33. this.cboolAllowAnyone = allowAnyone;
  34. this.registerGettersAndSetters();
  35. }
  36.  
  37. @Override
  38. public int getContentVersion() {
  39. return 1; // Set to 1 initially. Change this when the data content version changes and renders old data storage
  40. // method/manipulators broken
  41. }
  42.  
  43. @Override
  44. public String toString() {
  45. return Objects.toStringHelper(this)
  46. .add("serverOwner", this.cboolServerOwned)
  47. .add("UUIDS", this.csetUUIDsAllowed)
  48. .add("allowAnyone", cboolAllowAnyone)
  49. .toString();
  50. }
  51.  
  52. public Value<List<String>> userUUIDList() {
  53. return Sponge.getRegistry().getValueFactory()
  54. .createListValue(KeysContainerlock.UUIDLIST, this.csetUUIDsAllowed);
  55. }
  56.  
  57. public Value<Boolean> serverowned() {
  58. return Sponge.getRegistry().getValueFactory().createValue(KeysContainerlock.OWNEDBYSERVER, this.cboolServerOwned);
  59. }
  60.  
  61. public Value<Boolean> allowanyone() {
  62. return Sponge.getRegistry().getValueFactory().createValue(KeysContainerlock.ALLOWANYONE, this.cboolAllowAnyone);
  63. }
  64.  
  65. @Override
  66. protected void registerGettersAndSetters() {
  67. registerFieldGetter(KeysContainerlock.UUIDLIST, () -> this.csetUUIDsAllowed);
  68. registerFieldSetter(KeysContainerlock.UUIDLIST, value -> this.csetUUIDsAllowed = value);
  69. registerKeyValue(KeysContainerlock.UUIDLIST, this::userUUIDList);
  70.  
  71. registerFieldGetter(KeysContainerlock.OWNEDBYSERVER, () -> this.cboolServerOwned);
  72. registerFieldSetter(KeysContainerlock.OWNEDBYSERVER, value -> this.cboolServerOwned = value);
  73. registerKeyValue(KeysContainerlock.OWNEDBYSERVER, this::serverowned);
  74.  
  75. registerFieldGetter(KeysContainerlock.ALLOWANYONE, () -> this.cboolAllowAnyone);
  76. registerFieldSetter(KeysContainerlock.ALLOWANYONE, value -> this.cboolAllowAnyone = value);
  77. registerKeyValue(KeysContainerlock.ALLOWANYONE, this::allowanyone);
  78. }
  79.  
  80. @Override
  81. public DataContainer toContainer() {
  82. return new MemoryDataContainer()
  83. // return super.toContainer()
  84. .set(KeysContainerlock.UUIDLIST, this.csetUUIDsAllowed)
  85. .set(KeysContainerlock.ALLOWANYONE, this.cboolAllowAnyone)
  86. .set(KeysContainerlock.OWNEDBYSERVER, this.cboolServerOwned);
  87. }
  88.  
  89. @Override
  90. public Optional<ContainerLockData> fill(DataHolder dataholder, MergeFunction overlap) {
  91. return Optional.empty(); // Not included in the FakeData example.
  92. }
  93.  
  94. @SuppressWarnings("unchecked")
  95. @Override
  96.  
  97. public Optional<ContainerLockData> from(DataContainer container) {
  98. System.out.println(">>>>>> optiona from datacontainer!!!");
  99. if (!container.contains(KeysContainerlock.OWNEDBYSERVER.getQuery()) ||
  100. !container.contains(KeysContainerlock.UUIDLIST.getQuery())
  101. || !container.contains(KeysContainerlock.ALLOWANYONE.getQuery())) {
  102. return Optional.empty();
  103. }
  104. final boolean bserverowned = container.getBoolean(KeysContainerlock.OWNEDBYSERVER.getQuery()).get();
  105. final List<String> setusers = (List<String>) container.get(KeysContainerlock.UUIDLIST.getQuery()).get();
  106. final boolean allowanyone = container.getBoolean(KeysContainerlock.ALLOWANYONE.getQuery()).get();
  107. return Optional.of(this);
  108. // Why the fudge create final locals and return THIS?? No idea, was fakedata example..
  109. // I dont even think this code gets called, cause the final set<> analog threw errors in this form in the other
  110. }
  111.  
  112. @Override
  113. public ContainerLockData copy() { // new Constructor(this.p, this.q...)
  114. return new ContainerLockData(this.csetUUIDsAllowed, this.cboolServerOwned, this.cboolAllowAnyone);
  115. }
  116.  
  117. @Override
  118. public ImmutableContainerLockData asImmutable() { // new immutableconstructor(this.p, this.q..)
  119. return new ImmutableContainerLockData(this.csetUUIDsAllowed, this.cboolServerOwned, this.cboolAllowAnyone);
  120. }
  121.  
  122. // Why this doesn't get used i dont know, maybe it is supposed to, maybe
  123. @Override
  124. public int compareTo(ContainerLockData o) {
  125. return 0;
  126. }
  127.  
  128. }
  129.  
  130.  
  131. ===================
  132.  
  133. package com.prennet.boomod.boosautolock.containerlockdata;
  134.  
  135. import java.util.List;
  136. import java.util.Optional;
  137.  
  138. import org.spongepowered.api.data.DataHolder;
  139. import org.spongepowered.api.data.DataView;
  140. import org.spongepowered.api.data.manipulator.DataManipulatorBuilder;
  141. import org.spongepowered.api.data.persistence.InvalidDataException;
  142.  
  143. public class ContainerLockDataManipulatorBuilder implements DataManipulatorBuilder<ContainerLockData, ImmutableContainerLockData> {
  144.  
  145. @Override
  146. public ContainerLockData create() {
  147. return new ContainerLockData();
  148. }
  149.  
  150. @Override // orElse(new Constructor( form of choice
  151. public Optional<ContainerLockData> createFrom(DataHolder dataholder) {
  152. return Optional.of(dataholder.get(ContainerLockData.class).orElse(new ContainerLockData()));
  153. }
  154.  
  155. @Override
  156. public Optional<ContainerLockData> build(DataView container) throws InvalidDataException {
  157. // This is where Queries.CONTENT_VERSION should be checked in real implimentation for changes
  158. if (container.contains(KeysContainerlock.UUIDLIST, KeysContainerlock.OWNEDBYSERVER)) {
  159. final boolean bserverowned = container.getBoolean(KeysContainerlock.OWNEDBYSERVER.getQuery()).get();
  160. final List<String> setuserids =
  161. container.getStringList(KeysContainerlock.UUIDLIST.getQuery()).get();
  162. final boolean anyoneallowed = container.getBoolean(KeysContainerlock.ALLOWANYONE.getQuery()).get();
  163. return Optional.of(new ContainerLockData(setuserids, bserverowned, anyoneallowed));
  164. }
  165. return Optional.empty();
  166. }
  167. }
  168.  
  169. ==============================================================
  170.  
  171. package com.prennet.boomod.boosautolock.containerlockdata;
  172.  
  173. import org.spongepowered.api.data.DataQuery;
  174.  
  175. public class ContainerLockDataQueries {
  176.  
  177. public static final DataQuery USERSET = DataQuery.of("UUIDS");
  178. public static final DataQuery ALLOWANYONE = DataQuery.of("AllowAll");
  179. public static final DataQuery SERVEROWNED = DataQuery.of("ServerOwn");
  180.  
  181. }
  182.  
  183. ================================================
  184. package com.prennet.boomod.boosautolock.containerlockdata;
  185.  
  186. import java.util.List;
  187. import java.util.Optional;
  188.  
  189. import org.spongepowered.api.Sponge;
  190. import org.spongepowered.api.data.DataContainer;
  191. import org.spongepowered.api.data.key.Key;
  192. import org.spongepowered.api.data.manipulator.immutable.common.AbstractImmutableData;
  193. import org.spongepowered.api.data.value.BaseValue;
  194. import org.spongepowered.api.data.value.immutable.ImmutableValue;
  195.  
  196. import com.google.common.collect.ComparisonChain;
  197.  
  198. public class ImmutableContainerLockData extends AbstractImmutableData<ImmutableContainerLockData, ContainerLockData> {
  199. private boolean cboolServerOwned;
  200. private List<String> csetUUIDsAllowed;
  201. private boolean cboolAllowAnyone;
  202.  
  203. public ImmutableContainerLockData( List<String> csetUUIDsAllowed, boolean cboolServerOwned, boolean cboolAllowAnyone ) {
  204. this.cboolServerOwned = cboolServerOwned;
  205. this.csetUUIDsAllowed = csetUUIDsAllowed;
  206. this.cboolAllowAnyone = cboolAllowAnyone;
  207. this.registerGetters();
  208. }
  209.  
  210. @Override
  211. public int getContentVersion() {
  212. return 1; // Update this in parallel with the mutable data, etc
  213. }
  214.  
  215. // ImmutableValue<> ...asImmutable(); parallel-version of the mutable data code.
  216. public ImmutableValue<List<String>> userUUIDList() {
  217. return Sponge.getRegistry().getValueFactory()
  218. .createListValue(KeysContainerlock.UUIDLIST, this.csetUUIDsAllowed).asImmutable();
  219. }
  220.  
  221. public ImmutableValue<Boolean> serverowned() {
  222. return Sponge.getRegistry().getValueFactory().createValue(KeysContainerlock.OWNEDBYSERVER, this.cboolServerOwned)
  223. .asImmutable();
  224. }
  225.  
  226. public ImmutableValue<Boolean> anyoneallowed() {
  227. return Sponge.getRegistry().getValueFactory().createValue(KeysContainerlock.ALLOWANYONE, this.cboolAllowAnyone)
  228. .asImmutable();
  229. }
  230.  
  231. // Same as mutable data, except a memory-container instead of super...
  232. @Override
  233. public DataContainer toContainer() {
  234. return super.toContainer()
  235. .set(KeysContainerlock.UUIDLIST, this.csetUUIDsAllowed)
  236. .set(KeysContainerlock.ALLOWANYONE, this.cboolAllowAnyone)
  237. .set(KeysContainerlock.OWNEDBYSERVER, this.cboolServerOwned);
  238. }
  239.  
  240. private boolean isCboolServerOwned() {
  241. return this.cboolServerOwned;
  242. }
  243.  
  244. private List<String> getCsetUUIDsAllowed() {
  245. return this.csetUUIDsAllowed;
  246. }
  247.  
  248. // no setters because, duh, its immutable
  249. @Override
  250. protected void registerGetters() {
  251. // need to manually import the checknotnull import if using this function
  252. registerFieldGetter(KeysContainerlock.OWNEDBYSERVER, () -> this.cboolServerOwned);
  253. registerKeyValue(KeysContainerlock.OWNEDBYSERVER, this::serverowned);
  254.  
  255. registerFieldGetter(KeysContainerlock.ALLOWANYONE, () -> this.cboolAllowAnyone);
  256. registerKeyValue(KeysContainerlock.ALLOWANYONE, this::anyoneallowed);
  257.  
  258. registerFieldGetter(KeysContainerlock.UUIDLIST, () -> this.csetUUIDsAllowed);
  259. registerKeyValue(KeysContainerlock.UUIDLIST, this::userUUIDList);
  260. }
  261.  
  262. @Override
  263. public <E> Optional<ImmutableContainerLockData> with(Key<? extends BaseValue<E>> key, E value) {
  264. return Optional.empty();
  265. }
  266.  
  267. @Override
  268. public ContainerLockData asMutable() {// new Constructor(this.p, this.q...)
  269. return new ContainerLockData(this.csetUUIDsAllowed, this.cboolServerOwned, this.cboolAllowAnyone);
  270. }
  271.  
  272. @Override
  273. public int compareTo(ImmutableContainerLockData o) {
  274. // Supposedly done this way because even though object o is a different instance, its being called within the
  275. // class it is so it can see the private data variables. But how to compare SETS? ..
  276. // can I cheat by comparing set hashcodes? Same set must same code; very very rare same code from diff set.
  277. return ComparisonChain.start()
  278. .compare(o.csetUUIDsAllowed.hashCode(), this.csetUUIDsAllowed.hashCode())
  279. .compare(o.cboolServerOwned, this.cboolServerOwned)
  280. .compare(o.cboolAllowAnyone, this.cboolAllowAnyone)
  281. .result();
  282. }
  283.  
  284. }
  285.  
  286. ===============================================
  287.  
  288. package com.prennet.boomod.boosautolock.containerlockdata;
  289.  
  290. import org.spongepowered.api.data.key.Key;
  291. import org.spongepowered.api.data.key.KeyFactory;
  292. import org.spongepowered.api.data.value.mutable.ListValue;
  293. import org.spongepowered.api.data.value.mutable.Value;
  294.  
  295. public class KeysContainerlock {
  296. public static final Key<ListValue<String>> UUIDLIST = KeyFactory.makeListKey(String.class, ContainerLockDataQueries.USERSET);
  297. public static final Key<Value<Boolean>> OWNEDBYSERVER =
  298. KeyFactory
  299. .makeSingleKey(Boolean.class, Value.class, ContainerLockDataQueries.SERVEROWNED);
  300. public static final Key<Value<Boolean>> ALLOWANYONE =
  301. KeyFactory
  302. .makeSingleKey(Boolean.class, Value.class, ContainerLockDataQueries.ALLOWANYONE);
  303. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement