crwdns118619:0crwdne118619:0

crwdns118621:0:javadoc:crwdne118621:0

crwdns118623:0:javadoc:crwdnd118623:0:javadoc:crwdnd118623:0:javadoc:crwdnd118623:0:javadoc:crwdnd118623:0:javadoc:crwdnd118623:0:javadoc:crwdne118623:0

crwdns118625:0:javadoc:crwdnd118625:0:javadoc:crwdnd118625:0:ref:crwdnd118625:0:ref:crwdne118625:0

crwdns118627:0:javadoc:crwdnd118627:0:javadoc:crwdne118627:0

Note

crwdns118629:0crwdne118629:0

crwdns118631:0:javadoc:crwdnd118631:0:javadoc:crwdne118631:0

crwdns118633:0crwdne118633:0

crwdns118635:0:javadoc:crwdne118635:0

crwdns118637:0crwdne118637: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);
}

crwdns118639:0crwdne118639:0

crwdns118641:0:javadoc:crwdnd118641:0:javadoc:crwdne118641:0

crwdns118643:0:javadoc:crwdne118643:0

crwdns118645:0crwdne118645: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

crwdns118647:0:javadoc:crwdnd118647:0:javadoc:crwdne118647:0

crwdns118649:0crwdne118649:0

  • crwdns118651:0crwdne118651:0

  • crwdns118653:0crwdne118653:0

crwdns118655:0crwdne118655:0

crwdns118657:0:doc:crwdnd118657:0:javadoc:crwdnd118657:0:javadoc:crwdnd118657:0:javadoc:crwdnd118657:0:ref:crwdne118657:0

Note

crwdns118659:0crwdne118659:0

crwdns118661:0crwdne118661: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;
}

crwdns118663:0crwdne118663:0

crwdns118665:0crwdne118665:0

crwdns118667:0:javadoc:crwdnd118667:0:javadoc:crwdne118667:0

Note

crwdns118669:0:javadoc:crwdne118669: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")
      .build();
}

Warning

crwdns118671:0:javadoc:crwdne118671:0

crwdns118673:0crwdne118673:0

crwdns118675:0:javadoc:crwdne118675:0

crwdns118677:0crwdne118677:0

  • crwdns118679:0crwdne118679:0

  • crwdns118681:0:javadoc:crwdne118681:0

  • crwdns118683:0crwdne118683:0

  • crwdns118685:0crwdne118685:0

  • crwdns118687:0crwdne118687:0

  • crwdns118689:0crwdne118689:0

  • crwdns118691:0crwdne118691:0

crwdns118693:0crwdne118693:0

Tip

crwdns118695:0crwdne118695:0

crwdns118697:0crwdne118697:0

crwdns118699:0crwdne118699:0

  • crwdns118701:0crwdne118701:0

  • crwdns118703:0crwdne118703:0

crwdns118705:0:javadoc:crwdnd118705:0:javadoc:crwdne118705:0

Note

crwdns118707:0crwdne118707:0

crwdns118709:0crwdne118709:0

crwdns118711:0crwdne118711: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);
}

crwdns118713:0crwdne118713: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();
}

crwdns118715:0crwdne118715:0

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

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

crwdns118717:0crwdne118717:0

crwdns118719:0crwdne118719:0

crwdns118721:0crwdne118721:0

crwdns118723:0crwdne118723:0

crwdns118725:0crwdne118725:0

crwdns118727:0:javadoc:crwdne118727:0

crwdns118729:0crwdne118729:0

crwdns118731:0crwdne118731:0

crwdns118733:0:doc:crwdnd118733:0:javadoc:crwdnd118733:0:javadoc:crwdne118733:0

crwdns118735:0crwdne118735:0

  • crwdns118737:0crwdne118737:0

  • crwdns118739:0crwdne118739:0

  • crwdns118741:0crwdne118741:0

crwdns118743:0crwdne118743:0

crwdns118745:0crwdne118745: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);
}

crwdns118747:0crwdne118747:0