crwdns148757:0crwdne148757:0

crwdns148759:0:javadoc:crwdne148759:0

crwdns148761:0:javadoc:crwdnd148761:0:javadoc:crwdnd148761:0:javadoc:crwdnd148761:0:javadoc:crwdnd148761:0:javadoc:crwdnd148761:0:javadoc:crwdne148761:0

crwdns148763:0:javadoc:crwdnd148763:0:javadoc:crwdnd148763:0:ref:crwdnd148763:0:ref:crwdne148763:0

crwdns148765:0:javadoc:crwdnd148765:0:javadoc:crwdne148765:0

Note

crwdns148767:0crwdne148767:0

crwdns148769:0:javadoc:crwdnd148769:0:javadoc:crwdne148769:0

crwdns148771:0crwdne148771:0

crwdns148773:0:javadoc:crwdne148773:0

crwdns148775:0crwdne148775:0

import org.spongepowered.api.Sponge;
import org.spongepowered.api.data.value.ValueFactory;
import org.spongepowered.api.data.value.mutable.Value;

import org.spongepowered.cookbook.myhomes.data.home.Home;
import org.spongepowered.cookbook.myhomes.data.Keys;

@Override
protected Value<Home> defaultHome() {
    return Sponge.getRegistry().getValueFactory()
            .createValue(Keys.DEFAULT_HOME, getValue(), null);
}

crwdns148777:0crwdne148777:0

crwdns148779:0:javadoc:crwdnd148779:0:javadoc:crwdne148779:0

crwdns148781:0:javadoc:crwdne148781:0

crwdns148783:0crwdne148783:0

import org.spongepowered.api.data.DataQuery;
import org.spongepowered.api.data.key.Key;
import org.spongepowered.api.data.key.KeyFactory;
import org.spongepowered.api.data.value.mutable.Value;
import org.spongepowered.api.data.value.mutable.Value;

import com.google.common.reflect.TypeToken;

import org.spongepowered.cookbook.myhomes.data.home.Home;

public static final Key<Value<Home>> DEFAULT_HOME = KeyFactory.makeSingleKey(
        TypeToken.of(Home.class),
        new TypeToken<Value<Home>>() {},
        DataQuery.of("DefaultHome"), "myhomes:default_home", "Default Home");

Note

crwdns148785:0:javadoc:crwdnd148785:0:javadoc:crwdne148785:0

crwdns148787:0crwdne148787:0

  • crwdns148789:0crwdne148789:0

  • crwdns148791:0crwdne148791:0

crwdns148793:0crwdne148793:0

crwdns148795:0:doc:crwdnd148795:0:javadoc:crwdnd148795:0:javadoc:crwdnd148795:0:javadoc:crwdnd148795:0:ref:crwdne148795:0

Note

crwdns148797:0crwdne148797:0

crwdns148799:0crwdne148799:0

import org.spongepowered.api.data.DataContainer;

import org.spongepowered.cookbook.myhomes.data.Keys;

@Override
public DataContainer toContainer() {
    DataContainer container = super.toContainer();
    // This is the simplest, but use whatever structure you want!
    container.set(Keys.DEFAULT_HOME.getQuery(), this.defaultHome);
    container.set(Keys.HOMES, this.homes);

    return container;
}

crwdns148801:0crwdne148801:0

crwdns148803:0crwdne148803:0

crwdns148805:0:javadoc:crwdnd148805:0:javadoc:crwdne148805:0

Note

crwdns148807:0:javadoc:crwdne148807:0

import org.spongepowered.api.event.game.state.GameInitializationEvent;
import org.spongepowered.api.data.DataRegistration;

import org.example.MyCustomData;
import org.example.ImmutableCustomData;
import org.example.CustomDataBuilder;

@Listener
public void onInit(GameInitializationEvent event) {
  DataRegistration.builder()
      .dataClass(MyCustomData.class)
      .immutableClass(ImmutableCustomData.class)
      .builder(new CustomDataBuilder())
      .manipulatorId("my-custom")
      .dataName("My Custom")
      .buildAndRegister(myPluginContainer);
}

Warning

crwdns148809:0:javadoc:crwdne148809:0

crwdns148811:0crwdne148811:0

crwdns148813:0crwdne148813:0

crwdns148815:0crwdne148815:0

  • crwdns148817:0crwdne148817:0

  • crwdns148819:0crwdne148819:0

  • crwdns148821:0crwdne148821:0

  • crwdns148823:0crwdne148823:0

  • crwdns148825:0crwdne148825:0

  • crwdns148827:0crwdne148827:0

  • crwdns148829:0crwdne148829:0

crwdns148831:0crwdne148831:0

Tip

crwdns148833:0crwdne148833:0

crwdns148835:0crwdne148835:0

crwdns148837:0crwdne148837:0

  • crwdns148839:0crwdne148839:0

  • crwdns148841:0crwdne148841:0

crwdns148843:0:javadoc:crwdnd148843:0:javadoc:crwdne148843:0

Note

crwdns148845:0crwdne148845:0

crwdns148847:0crwdne148847:0

crwdns148849:0crwdne148849:0

import org.spongepowered.api.data.DataHolder;
import org.spongepowered.api.data.merge.MergeFunction;

import org.spongepowered.cookbook.myhomes.data.friends.FriendsData;

import java.util.Optional;

@Override
public Optional<FriendsData> fill(DataHolder dataHolder, MergeFunction overlap) {
    FriendsData merged = overlap.merge(this, dataHolder.get(FriendsData.class).orElse(null));
    setValue(merged.friends().get());

    return Optional.of(this);
}

crwdns148851:0crwdne148851:0

import org.spongepowered.api.data.DataContainer;
import org.spongepowered.api.data.DataQuery;

import org.spongepowered.cookbook.myhomes.data.Keys;
import org.spongepowered.cookbook.myhomes.data.friends.FriendsData;
import org.spongepowered.cookbook.myhomes.data.friends.ImmutableFriendsData;

import com.google.common.collect.Maps;

import java.util.Optional;
import java.util.UUID;

@Override
public Optional<FriendsData> from(DataContainer container) {
    if(container.contains(Keys.FRIENDS)) {
        List<UUID> friends = container.getObjectList(Keys.FRIENDS.getQuery(), UUID.class).get();
        return Optional.of(setValue(friends));
    }

    return Optional.empty();
}

crwdns148853:0crwdne148853:0

import org.spongepowered.cookbook.myhomes.data.friends.FriendsData;

@Override
public FriendsData copy() {
    return new FriendsDataImpl(getValue());
}

crwdns148855:0crwdne148855:0

crwdns148857:0crwdne148857:0

crwdns148859:0crwdne148859:0

crwdns148861:0crwdne148861:0

crwdns148863:0crwdne148863:0

crwdns148865:0:javadoc:crwdne148865:0

crwdns148867:0crwdne148867:0

crwdns148869:0crwdne148869:0

crwdns148871:0:doc:crwdnd148871:0:javadoc:crwdnd148871:0:javadoc:crwdne148871:0

crwdns148873:0crwdne148873:0

  • crwdns148875:0crwdne148875:0

  • crwdns148877:0crwdne148877:0

  • crwdns148879:0crwdne148879:0

crwdns148881:0crwdne148881:0

crwdns148883:0crwdne148883:0

import org.spongepowered.cookbook.myhomes.data.Keys

// registerGetters() for immutable implementation
@Override
protected void registerGettersAndSetters() {
    registerKeyValue(Keys.DEFAULT_HOME, this::defaultHome);
    registerKeyValue(Keys.HOMES, this::homes);

    registerFieldGetter(Keys.DEFAULT_HOME, this::getDefaultHome);
    registerFieldGetter(Keys.HOMES, this::getHomes);

    // Only on mutable implementation
    registerFieldSetter(Keys.DEFAULT_HOME, this::setDefaultHome);
    registerFieldSetter(Keys.HOMES, this::setHomes);
}

crwdns148885:0crwdne148885:0