Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.prennet.boomod.boosautolock.containerlockdata;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Optional;
- import org.spongepowered.api.Sponge;
- import org.spongepowered.api.data.DataContainer;
- import org.spongepowered.api.data.DataHolder;
- import org.spongepowered.api.data.DataSerializable;
- import org.spongepowered.api.data.MemoryDataContainer;
- import org.spongepowered.api.data.manipulator.mutable.common.AbstractData;
- import org.spongepowered.api.data.merge.MergeFunction;
- import org.spongepowered.api.data.value.mutable.Value;
- import com.google.common.base.Objects;
- //the impliments DataSerializable - is not in the fakedata example, but is in other codes
- // doesn't do squat for the warning in logs, but may be required for writing more complex nbt?
- public class ContainerLockData extends AbstractData<ContainerLockData, ImmutableContainerLockData> implements DataSerializable {
- private boolean cboolServerOwned;
- private List<String> csetUUIDsAllowed;
- private boolean cboolAllowAnyone;
- public ContainerLockData() {
- this(new ArrayList<String>(), false, false); // defaulting through to the parameterized constructor below
- }
- public ContainerLockData( List<String> setuserids, boolean serverOwned, boolean allowAnyone ) {
- this.cboolServerOwned = serverOwned;
- this.csetUUIDsAllowed = setuserids;
- this.cboolAllowAnyone = allowAnyone;
- this.registerGettersAndSetters();
- }
- @Override
- public int getContentVersion() {
- return 1; // Set to 1 initially. Change this when the data content version changes and renders old data storage
- // method/manipulators broken
- }
- @Override
- public String toString() {
- return Objects.toStringHelper(this)
- .add("serverOwner", this.cboolServerOwned)
- .add("UUIDS", this.csetUUIDsAllowed)
- .add("allowAnyone", cboolAllowAnyone)
- .toString();
- }
- public Value<List<String>> userUUIDList() {
- return Sponge.getRegistry().getValueFactory()
- .createListValue(KeysContainerlock.UUIDLIST, this.csetUUIDsAllowed);
- }
- public Value<Boolean> serverowned() {
- return Sponge.getRegistry().getValueFactory().createValue(KeysContainerlock.OWNEDBYSERVER, this.cboolServerOwned);
- }
- public Value<Boolean> allowanyone() {
- return Sponge.getRegistry().getValueFactory().createValue(KeysContainerlock.ALLOWANYONE, this.cboolAllowAnyone);
- }
- @Override
- protected void registerGettersAndSetters() {
- registerFieldGetter(KeysContainerlock.UUIDLIST, () -> this.csetUUIDsAllowed);
- registerFieldSetter(KeysContainerlock.UUIDLIST, value -> this.csetUUIDsAllowed = value);
- registerKeyValue(KeysContainerlock.UUIDLIST, this::userUUIDList);
- registerFieldGetter(KeysContainerlock.OWNEDBYSERVER, () -> this.cboolServerOwned);
- registerFieldSetter(KeysContainerlock.OWNEDBYSERVER, value -> this.cboolServerOwned = value);
- registerKeyValue(KeysContainerlock.OWNEDBYSERVER, this::serverowned);
- registerFieldGetter(KeysContainerlock.ALLOWANYONE, () -> this.cboolAllowAnyone);
- registerFieldSetter(KeysContainerlock.ALLOWANYONE, value -> this.cboolAllowAnyone = value);
- registerKeyValue(KeysContainerlock.ALLOWANYONE, this::allowanyone);
- }
- @Override
- public DataContainer toContainer() {
- return new MemoryDataContainer()
- // return super.toContainer()
- .set(KeysContainerlock.UUIDLIST, this.csetUUIDsAllowed)
- .set(KeysContainerlock.ALLOWANYONE, this.cboolAllowAnyone)
- .set(KeysContainerlock.OWNEDBYSERVER, this.cboolServerOwned);
- }
- @Override
- public Optional<ContainerLockData> fill(DataHolder dataholder, MergeFunction overlap) {
- return Optional.empty(); // Not included in the FakeData example.
- }
- @SuppressWarnings("unchecked")
- @Override
- public Optional<ContainerLockData> from(DataContainer container) {
- System.out.println(">>>>>> optiona from datacontainer!!!");
- if (!container.contains(KeysContainerlock.OWNEDBYSERVER.getQuery()) ||
- !container.contains(KeysContainerlock.UUIDLIST.getQuery())
- || !container.contains(KeysContainerlock.ALLOWANYONE.getQuery())) {
- return Optional.empty();
- }
- final boolean bserverowned = container.getBoolean(KeysContainerlock.OWNEDBYSERVER.getQuery()).get();
- final List<String> setusers = (List<String>) container.get(KeysContainerlock.UUIDLIST.getQuery()).get();
- final boolean allowanyone = container.getBoolean(KeysContainerlock.ALLOWANYONE.getQuery()).get();
- return Optional.of(this);
- // Why the fudge create final locals and return THIS?? No idea, was fakedata example..
- // I dont even think this code gets called, cause the final set<> analog threw errors in this form in the other
- }
- @Override
- public ContainerLockData copy() { // new Constructor(this.p, this.q...)
- return new ContainerLockData(this.csetUUIDsAllowed, this.cboolServerOwned, this.cboolAllowAnyone);
- }
- @Override
- public ImmutableContainerLockData asImmutable() { // new immutableconstructor(this.p, this.q..)
- return new ImmutableContainerLockData(this.csetUUIDsAllowed, this.cboolServerOwned, this.cboolAllowAnyone);
- }
- // Why this doesn't get used i dont know, maybe it is supposed to, maybe
- @Override
- public int compareTo(ContainerLockData o) {
- return 0;
- }
- }
- ===================
- package com.prennet.boomod.boosautolock.containerlockdata;
- import java.util.List;
- import java.util.Optional;
- import org.spongepowered.api.data.DataHolder;
- import org.spongepowered.api.data.DataView;
- import org.spongepowered.api.data.manipulator.DataManipulatorBuilder;
- import org.spongepowered.api.data.persistence.InvalidDataException;
- public class ContainerLockDataManipulatorBuilder implements DataManipulatorBuilder<ContainerLockData, ImmutableContainerLockData> {
- @Override
- public ContainerLockData create() {
- return new ContainerLockData();
- }
- @Override // orElse(new Constructor( form of choice
- public Optional<ContainerLockData> createFrom(DataHolder dataholder) {
- return Optional.of(dataholder.get(ContainerLockData.class).orElse(new ContainerLockData()));
- }
- @Override
- public Optional<ContainerLockData> build(DataView container) throws InvalidDataException {
- // This is where Queries.CONTENT_VERSION should be checked in real implimentation for changes
- if (container.contains(KeysContainerlock.UUIDLIST, KeysContainerlock.OWNEDBYSERVER)) {
- final boolean bserverowned = container.getBoolean(KeysContainerlock.OWNEDBYSERVER.getQuery()).get();
- final List<String> setuserids =
- container.getStringList(KeysContainerlock.UUIDLIST.getQuery()).get();
- final boolean anyoneallowed = container.getBoolean(KeysContainerlock.ALLOWANYONE.getQuery()).get();
- return Optional.of(new ContainerLockData(setuserids, bserverowned, anyoneallowed));
- }
- return Optional.empty();
- }
- }
- ==============================================================
- package com.prennet.boomod.boosautolock.containerlockdata;
- import org.spongepowered.api.data.DataQuery;
- public class ContainerLockDataQueries {
- public static final DataQuery USERSET = DataQuery.of("UUIDS");
- public static final DataQuery ALLOWANYONE = DataQuery.of("AllowAll");
- public static final DataQuery SERVEROWNED = DataQuery.of("ServerOwn");
- }
- ================================================
- package com.prennet.boomod.boosautolock.containerlockdata;
- import java.util.List;
- import java.util.Optional;
- import org.spongepowered.api.Sponge;
- import org.spongepowered.api.data.DataContainer;
- import org.spongepowered.api.data.key.Key;
- import org.spongepowered.api.data.manipulator.immutable.common.AbstractImmutableData;
- import org.spongepowered.api.data.value.BaseValue;
- import org.spongepowered.api.data.value.immutable.ImmutableValue;
- import com.google.common.collect.ComparisonChain;
- public class ImmutableContainerLockData extends AbstractImmutableData<ImmutableContainerLockData, ContainerLockData> {
- private boolean cboolServerOwned;
- private List<String> csetUUIDsAllowed;
- private boolean cboolAllowAnyone;
- public ImmutableContainerLockData( List<String> csetUUIDsAllowed, boolean cboolServerOwned, boolean cboolAllowAnyone ) {
- this.cboolServerOwned = cboolServerOwned;
- this.csetUUIDsAllowed = csetUUIDsAllowed;
- this.cboolAllowAnyone = cboolAllowAnyone;
- this.registerGetters();
- }
- @Override
- public int getContentVersion() {
- return 1; // Update this in parallel with the mutable data, etc
- }
- // ImmutableValue<> ...asImmutable(); parallel-version of the mutable data code.
- public ImmutableValue<List<String>> userUUIDList() {
- return Sponge.getRegistry().getValueFactory()
- .createListValue(KeysContainerlock.UUIDLIST, this.csetUUIDsAllowed).asImmutable();
- }
- public ImmutableValue<Boolean> serverowned() {
- return Sponge.getRegistry().getValueFactory().createValue(KeysContainerlock.OWNEDBYSERVER, this.cboolServerOwned)
- .asImmutable();
- }
- public ImmutableValue<Boolean> anyoneallowed() {
- return Sponge.getRegistry().getValueFactory().createValue(KeysContainerlock.ALLOWANYONE, this.cboolAllowAnyone)
- .asImmutable();
- }
- // Same as mutable data, except a memory-container instead of super...
- @Override
- public DataContainer toContainer() {
- return super.toContainer()
- .set(KeysContainerlock.UUIDLIST, this.csetUUIDsAllowed)
- .set(KeysContainerlock.ALLOWANYONE, this.cboolAllowAnyone)
- .set(KeysContainerlock.OWNEDBYSERVER, this.cboolServerOwned);
- }
- private boolean isCboolServerOwned() {
- return this.cboolServerOwned;
- }
- private List<String> getCsetUUIDsAllowed() {
- return this.csetUUIDsAllowed;
- }
- // no setters because, duh, its immutable
- @Override
- protected void registerGetters() {
- // need to manually import the checknotnull import if using this function
- registerFieldGetter(KeysContainerlock.OWNEDBYSERVER, () -> this.cboolServerOwned);
- registerKeyValue(KeysContainerlock.OWNEDBYSERVER, this::serverowned);
- registerFieldGetter(KeysContainerlock.ALLOWANYONE, () -> this.cboolAllowAnyone);
- registerKeyValue(KeysContainerlock.ALLOWANYONE, this::anyoneallowed);
- registerFieldGetter(KeysContainerlock.UUIDLIST, () -> this.csetUUIDsAllowed);
- registerKeyValue(KeysContainerlock.UUIDLIST, this::userUUIDList);
- }
- @Override
- public <E> Optional<ImmutableContainerLockData> with(Key<? extends BaseValue<E>> key, E value) {
- return Optional.empty();
- }
- @Override
- public ContainerLockData asMutable() {// new Constructor(this.p, this.q...)
- return new ContainerLockData(this.csetUUIDsAllowed, this.cboolServerOwned, this.cboolAllowAnyone);
- }
- @Override
- public int compareTo(ImmutableContainerLockData o) {
- // Supposedly done this way because even though object o is a different instance, its being called within the
- // class it is so it can see the private data variables. But how to compare SETS? ..
- // can I cheat by comparing set hashcodes? Same set must same code; very very rare same code from diff set.
- return ComparisonChain.start()
- .compare(o.csetUUIDsAllowed.hashCode(), this.csetUUIDsAllowed.hashCode())
- .compare(o.cboolServerOwned, this.cboolServerOwned)
- .compare(o.cboolAllowAnyone, this.cboolAllowAnyone)
- .result();
- }
- }
- ===============================================
- package com.prennet.boomod.boosautolock.containerlockdata;
- import org.spongepowered.api.data.key.Key;
- import org.spongepowered.api.data.key.KeyFactory;
- import org.spongepowered.api.data.value.mutable.ListValue;
- import org.spongepowered.api.data.value.mutable.Value;
- public class KeysContainerlock {
- public static final Key<ListValue<String>> UUIDLIST = KeyFactory.makeListKey(String.class, ContainerLockDataQueries.USERSET);
- public static final Key<Value<Boolean>> OWNEDBYSERVER =
- KeyFactory
- .makeSingleKey(Boolean.class, Value.class, ContainerLockDataQueries.SERVEROWNED);
- public static final Key<Value<Boolean>> ALLOWANYONE =
- KeyFactory
- .makeSingleKey(Boolean.class, Value.class, ContainerLockDataQueries.ALLOWANYONE);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement