crwdns157007:0crwdne157007:0
crwdns157009:0crwdne157009:0
crwdns157011:0:javadoc:crwdne157011:0
crwdns157013:0crwdne157013:0
import java.util.UUID;
public class LastAttackerDataSerilizable {
private UUID lastAttackerId;
}
Note
crwdns157015:0crwdne157015:0
crwdns157017:0:javadoc:crwdne157017:0
import org.spongepowered.api.data.persistence.DataSerializable;
import org.spongepowered.api.data.persistence.DataContainer;
import org.spongepowered.api.data.persistence.DataQuery;
public class LastAttackerDataSerilizable implements DataSerializable {
private UUID lastAttackerId;
public static final DataQuery UUID_PATH = DataQuery.of("attack", "last");
@Override
public int contentVersion(){
return 1;
}
@Override
public DataContainer toContainer(){
return DataContainer.createNew()
.set(LastAttackerDataSerilizable.UUID_PATH, lastAttackerId.toString());
}
}
crwdns157019:0:javadoc:crwdne157019:0
import org.spongepowered.api.data.persistence.InvalidDataException;
public class LastAttackerDataBuilder implements DataBuilder<LastAttackerDataSerilizable> {
@Override
public Optional<LastAttackerDataSerilizable> build(DataView container) throws InvalidDataException {
Optional<String> lastAttackerAsStringId container.getString(LastAttackerDataSerilizable.UUID_PATH);
if(lastAttackerAsStringId.isPresent()){
UUID lastAttacker = UUID.fromString(lastAttackerAsStringId.get());
return Optional.of(new LastAttackerDataSerilizable(lastAttacker));
}
return Optional.empty();
}
}
crwdns157021:0crwdne157021:0
crwdns157023:0crwdne157023:0
crwdns157025:0:javadoc:crwdne157025:0
crwdns157027:0crwdne157027:0
crwdns157029:0crwdne157029:0
Key<? extends Value<String>> key = Key.from(pluginContainer, "my_simple_data", String.class);
DataRegistration myData = DataRegistration.of(key, ServerPlayer.class);
event.register(myData);
crwdns157031:0crwdne157031:0
crwdns157033:0:javadoc:crwdne157033:0
import org.spongepowered.api.ResourceKey;
import org.spongepowered.api.data.Key;
import org.spongepowered.api.data.value.Value;
ResourceKey resourceKey = ResourceKey(pluginContainer, "last_attacker_manipulator");
Key<? extends Value<LastAttackerDataSerilizable>> key = Key
.builder()
.key(resourceKey)
.elementType(LastAttackerDataSerilizable.class)
.build();
Warning
crwdns157035:0crwdne157035:0
Tip
crwdns157037:0crwdne157037:0
crwdns157039:0crwdne157039:0
crwdns157041:0:javadoc:crwdnd157041:0:javadoc:crwdne157041:0
import org.spongepowered.api.data.persistence.DataStore;
DataStore datastore = DataStore
.builder()
.pluginData(resourceKey)
.holder(Entity.class)
.key(key)
.build();
crwdns157043:0crwdne157043:0
crwdns157045:0crwdne157045:0
DataStore datastore = DataStore.of(key, DataQuery.of(), Entity.class);
crwdns157047:0crwdne157047:0
crwdns157049:0crwdne157049:0
import org.spongepowered.api.entity.Entity;
DataStore datastore = DataStore
.builder()
.pluginData(resourceKey)
.holder(Entity.class)
.key(key)
.key(innerKey, DataQuery.of("inner_data"))
.build();
crwdns157051:0crwdne157051:0
crwdns157053:0crwdne157053:0
crwdns157055:0crwdne157055:0
import org.spongepowered.api.data.DataProvider;
DataProvider<Value<UUID>, UUID> provider = DataProvider.mutableBuilder()
.dataKey(innerKey)
.dataHolder(ServerPlayer.class)
.get(this::myCustomGetter)
.build();
public UUID myCustomGetter(ServerPlayer player){
return player.get(key).orElse(player.uniqueId());
}
Note
crwdns157057:0crwdne157057:0
Tip
crwdns157059:0crwdne157059:0
crwdns157061:0crwdne157061:0
crwdns157063:0:javadoc:crwdne157063:0
import org.spongepowered.api.data.DataRegistration;
DataRegistration myData = DataRegistration.builder()
.key(key)
.store(datastore)
.provider(provider)
.build();
event.register(myData);
crwdns157065:0crwdne157065:0
crwdns157067:0:javadoc:crwdne157067:0
Sponge.dataManager().registerBuilder(LastAttackerDataSerilizable.class, new LastAttackerDataBuilder());
crwdns157069:0crwdne157069:0
crwdns157071:0:javadoc:crwdne157071:0
import org.spongepowered.api.data.persistence.DataContentUpdater;
public class LastAttackerUpdater implements DataContentUpdater {
@Override
public int inputVersion(){
return 1;
}
@Override
public int outputVersion(){
return 2;
}
@Override
public DataView update(DataView view){
view.set(DataQuery.of("attack", "occurred"), LocalDateTime.MIN.getNano());
return view;
}
}
crwdns157073:0crwdne157073:0
DataStore.builder()
.pluginData(resourceKey, 1)
.updater(new LastAttackerUpdater())
//continue with the normal registeration