-
-
Save alimtunc/235256e5f45aa9a43d85a300b8894834 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
plugins { | |
id 'org.spongepowered.plugin' version '0.8.1' | |
} | |
group = pluginGroup | |
version = pluginVersion | |
dependencies { | |
compile 'org.spongepowered:spongeapi:7.1.0' | |
} | |
sponge.plugin.id = pluginId |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.elementia.playermanager; | |
import org.spongepowered.api.Sponge; | |
import org.spongepowered.api.data.DataContainer; | |
import org.spongepowered.api.data.manipulator.immutable.common.AbstractImmutableData; | |
import org.spongepowered.api.data.value.immutable.ImmutableValue; | |
import java.util.UUID; | |
public class MyImmutableStandardData extends AbstractImmutableData<MyImmutableStandardData, MyStandardData> { | |
private UUID id; | |
private String name; | |
private int lvlPvP; | |
private int honors; | |
private String element; | |
public MyImmutableStandardData(UUID id, String name, int lvlPvP, int honors, String element) { | |
this.id = id; | |
this.name = name; | |
this.lvlPvP = lvlPvP; | |
this.honors = honors; | |
this.element = element; | |
registerGetters(); | |
} | |
@Override | |
protected void registerGetters() { | |
registerFieldGetter(MyKeys.STANDARD_ID, () -> this.id); | |
registerFieldGetter(MyKeys.STANDARD_NAME, () -> this.name); | |
registerFieldGetter(MyKeys.STANDARD_LVLPVP, () -> this.lvlPvP); | |
registerFieldGetter(MyKeys.STANDARD_HONORS, () -> this.honors); | |
registerFieldGetter(MyKeys.STANDARD_ELEMENT, () -> this.element); | |
registerKeyValue(MyKeys.STANDARD_ID, this::id); | |
registerKeyValue(MyKeys.STANDARD_NAME, this::name); | |
registerKeyValue(MyKeys.STANDARD_LVLPVP, this::lvlPvP); | |
registerKeyValue(MyKeys.STANDARD_HONORS, this::honors); | |
registerKeyValue(MyKeys.STANDARD_ELEMENT, this::element); | |
} | |
public ImmutableValue<UUID> id() { | |
return Sponge.getRegistry().getValueFactory().createValue(MyKeys.STANDARD_ID, id).asImmutable(); | |
} | |
public ImmutableValue<String> name() { | |
return Sponge.getRegistry().getValueFactory().createValue(MyKeys.STANDARD_NAME, name).asImmutable(); | |
} | |
public ImmutableValue<Integer> lvlPvP() { | |
return Sponge.getRegistry().getValueFactory().createValue(MyKeys.STANDARD_LVLPVP, lvlPvP).asImmutable(); | |
} | |
public ImmutableValue<Integer> honors() { | |
return Sponge.getRegistry().getValueFactory().createValue(MyKeys.STANDARD_HONORS, honors).asImmutable(); | |
} | |
public ImmutableValue<String> element() { | |
return Sponge.getRegistry().getValueFactory().createValue(MyKeys.STANDARD_ELEMENT, element).asImmutable(); | |
} | |
public UUID getId() { return this.id; } | |
public String getName() { return this.name; } | |
public int getLvlPvP() { return this.lvlPvP; } | |
public int getHonors() { return this.honors; } | |
public String getElement() { return this.element; } | |
@Override | |
public MyStandardData asMutable() { | |
return new MyStandardData(id, name, lvlPvP, honors, element); | |
} | |
@Override | |
public int getContentVersion() { | |
return 1; | |
} | |
@Override | |
public DataContainer toContainer() { | |
try { | |
return super.toContainer().set(MyKeys.STANDARD_ID.getQuery(), this.id) | |
.set(MyKeys.STANDARD_NAME.getQuery(), this.name) | |
.set(MyKeys.STANDARD_LVLPVP.getQuery(), this.lvlPvP) | |
.set(MyKeys.STANDARD_HONORS.getQuery(), this.honors) | |
.set(MyKeys.STANDARD_ELEMENT.getQuery(), this.element); | |
} catch (Exception e) | |
{ | |
e.printStackTrace(); | |
return null; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.elementia.playermanager; | |
import org.spongepowered.api.data.DataQuery; | |
import org.spongepowered.api.data.key.Key; | |
import org.spongepowered.api.data.value.mutable.Value; | |
import org.spongepowered.api.util.TypeTokens; | |
import java.util.UUID; | |
public class MyKeys { | |
private MyKeys() {} | |
static void dummy() {} // invoke static constructor | |
public static Key<Value<UUID>> STANDARD_ID; | |
public static Key<Value<String>> STANDARD_NAME; | |
public static Key<Value<Integer>> STANDARD_LVLPVP; | |
public static Key<Value<Integer>> STANDARD_HONORS; | |
public static Key<Value<String>> STANDARD_ELEMENT; | |
static { | |
STANDARD_ID = Key.builder() | |
.type(TypeTokens.UUID_VALUE_TOKEN) | |
.id("playermanager:standard_id") | |
.name("Standard ID") | |
.query(DataQuery.of('.', "standard.name")) | |
.build(); | |
STANDARD_NAME = Key.builder() | |
.type(TypeTokens.STRING_VALUE_TOKEN) | |
.id("playermanager:standard_name") | |
.name("Standard Name") | |
.query(DataQuery.of('.', "standard.name")) | |
.build(); | |
STANDARD_LVLPVP = Key.builder() | |
.type(TypeTokens.INTEGER_VALUE_TOKEN) | |
.name("Standard lvlPvp") | |
.query(DataQuery.of(".", "standard.lvlPvP")) | |
.id("playermanager:standard_lvlPvP") | |
.build(); | |
STANDARD_HONORS = Key.builder() | |
.type(TypeTokens.INTEGER_VALUE_TOKEN) | |
.name("Standard honors") | |
.query(DataQuery.of(".", "standard.honors")) | |
.id("playermanager:standard_honors") | |
.build(); | |
STANDARD_ELEMENT = Key.builder() | |
.type(TypeTokens.STRING_VALUE_TOKEN) | |
.name("Standard element") | |
.query(DataQuery.of(".", "standard.element")) | |
.id("playermanager:standard_element") | |
.build(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.elementia.playermanager; | |
import java.io.IOException; | |
import org.spongepowered.api.Sponge; | |
import org.spongepowered.api.data.DataContainer; | |
import org.spongepowered.api.data.DataHolder; | |
import org.spongepowered.api.data.DataView; | |
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 java.util.Optional; | |
import java.util.UUID; | |
public class MyStandardData extends AbstractData<MyStandardData, MyImmutableStandardData> { | |
private UUID id; | |
private String name; | |
private int lvlPvP; | |
private int honors; | |
private String element; | |
MyStandardData(UUID id, String name, int lvlPvP, int honors, String element) { | |
this.id = id; | |
this.name = name; | |
this.lvlPvP= lvlPvP; | |
this.honors = honors; | |
this.element = element; | |
registerGettersAndSetters(); | |
} | |
@Override | |
protected void registerGettersAndSetters() { | |
registerFieldGetter(MyKeys.STANDARD_ID, () -> this.id); | |
registerFieldGetter(MyKeys.STANDARD_NAME, () -> this.name); | |
registerFieldGetter(MyKeys.STANDARD_LVLPVP, () -> this.lvlPvP); | |
registerFieldGetter(MyKeys.STANDARD_HONORS, () -> this.honors); | |
registerFieldGetter(MyKeys.STANDARD_ELEMENT, () -> this.element); | |
registerFieldSetter(MyKeys.STANDARD_ID, id -> this.id = id); | |
registerFieldSetter(MyKeys.STANDARD_NAME, name -> this.name = name); | |
registerFieldSetter(MyKeys.STANDARD_LVLPVP, lvlPvP -> this.lvlPvP = lvlPvP); | |
registerFieldSetter(MyKeys.STANDARD_HONORS, honors -> this.honors = honors); | |
registerFieldSetter(MyKeys.STANDARD_ELEMENT, element -> this.element = element); | |
registerKeyValue(MyKeys.STANDARD_ID, this::id); | |
registerKeyValue(MyKeys.STANDARD_NAME, this::name); | |
registerKeyValue(MyKeys.STANDARD_LVLPVP, this::lvlPvP); | |
registerKeyValue(MyKeys.STANDARD_HONORS, this::honors); | |
registerKeyValue(MyKeys.STANDARD_ELEMENT, this::element); | |
} | |
public Value<UUID> id() { | |
return Sponge.getRegistry().getValueFactory().createValue(MyKeys.STANDARD_ID, this.id); | |
} | |
public Value<String> name() { | |
return Sponge.getRegistry().getValueFactory().createValue(MyKeys.STANDARD_NAME, this.name); | |
} | |
public Value<Integer> lvlPvP() { | |
return Sponge.getRegistry().getValueFactory().createValue(MyKeys.STANDARD_LVLPVP, this.lvlPvP); | |
} | |
public Value<Integer> honors() { | |
return Sponge.getRegistry().getValueFactory().createValue(MyKeys.STANDARD_HONORS, this.honors); | |
} | |
public Value<String> element() { | |
return Sponge.getRegistry().getValueFactory().createValue(MyKeys.STANDARD_ELEMENT, this.element); | |
} | |
public void setId(UUID id) { this.id = id; } | |
public void setName(String name) { this.name = name; } | |
public void setLvlPvP(int lvlPvP) { this.lvlPvP = lvlPvP; } | |
public void setHonors(int honors) { this.honors = honors; } | |
public void setElement(String element) { this.element = element; } | |
public UUID getId() { return this.id; } | |
public String getName() { return this.name; } | |
public int getLvlPvP() { return this.lvlPvP; } | |
public int getHonors() { return this.honors; } | |
public String getElement() { return this.element; } | |
@Override | |
public Optional<MyStandardData> fill(DataHolder dataHolder, MergeFunction overlap) { | |
Optional<MyStandardData> otherData_ = dataHolder.get(MyStandardData.class); | |
if (otherData_.isPresent()) { | |
MyStandardData otherData = otherData_.get(); | |
MyStandardData finalData = overlap.merge(this, otherData); | |
this.id = finalData.id; | |
this.name = finalData.name; | |
this.lvlPvP = finalData.lvlPvP; | |
this.honors = finalData.honors; | |
this.element = finalData.element; | |
} | |
return Optional.of(this); | |
} | |
// the double method isn't strictly necessary but makes implementing the builder easier | |
@Override | |
public Optional<MyStandardData> from(DataContainer container) { | |
try { | |
return from((DataView) container); | |
} | |
catch (Exception e) | |
{ | |
e.printStackTrace(); | |
return null; | |
} | |
} | |
public Optional<MyStandardData> from(DataView dataView) { | |
if (dataView.contains(MyKeys.STANDARD_ID.getQuery()) && dataView.contains(MyKeys.STANDARD_NAME.getQuery()) && dataView.contains(MyKeys.STANDARD_LVLPVP.getQuery()) && dataView.contains(MyKeys.STANDARD_HONORS.getQuery()) && dataView.contains(MyKeys.STANDARD_ELEMENT.getQuery())) { | |
this.id = dataView.getObject(MyKeys.STANDARD_ID.getQuery(), UUID.class).get(); | |
this.name = dataView.getString(MyKeys.STANDARD_NAME.getQuery()).get(); | |
this.lvlPvP = dataView.getInt(MyKeys.STANDARD_LVLPVP.getQuery()).get(); | |
this.honors = dataView.getInt(MyKeys.STANDARD_HONORS.getQuery()).get(); | |
this.element = dataView.getString(MyKeys.STANDARD_ELEMENT.getQuery()).get(); | |
return Optional.of(this); | |
} else { | |
return Optional.empty(); | |
} | |
} | |
@Override | |
public MyStandardData copy() { | |
return new MyStandardData(this.id, this.name, this.lvlPvP, this.honors, this.element); | |
} | |
@Override | |
public MyImmutableStandardData asImmutable() { | |
return new MyImmutableStandardData(this.id, this.name, this.lvlPvP, this.honors, this.element); | |
} | |
@Override | |
public int getContentVersion() { | |
return 1; | |
} | |
// IMPORTANT this is what causes your data to be written to NBT | |
@Override | |
public DataContainer toContainer() { | |
try { | |
return super.toContainer() | |
.set(MyKeys.STANDARD_ID, this.id) | |
.set(MyKeys.STANDARD_NAME.getQuery(), this.name) | |
.set(MyKeys.STANDARD_LVLPVP.getQuery(), this.lvlPvP) | |
.set(MyKeys.STANDARD_HONORS.getQuery(), this.honors) | |
.set(MyKeys.STANDARD_ELEMENT.getQuery(), this.element); | |
} catch (Exception e) | |
{ | |
e.printStackTrace(); | |
return null; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.elementia.playermanager; | |
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.AbstractDataBuilder; | |
import org.spongepowered.api.data.persistence.InvalidDataException; | |
import java.util.Optional; | |
import java.util.UUID; | |
public class MyStandardDataBuilder extends AbstractDataBuilder<MyStandardData> implements DataManipulatorBuilder<MyStandardData, MyImmutableStandardData> { | |
MyStandardDataBuilder() { | |
super(MyStandardData.class, 1); | |
} | |
@Override | |
public MyStandardData create() { | |
return new MyStandardData(UUID.randomUUID(), "Null", 1, 0, "Null"); | |
} | |
@Override | |
public Optional<MyStandardData> createFrom(DataHolder dataHolder) { | |
return create().fill(dataHolder); | |
} | |
@Override | |
protected Optional<MyStandardData> buildContent(DataView container) throws InvalidDataException { | |
return create().from(container); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.elementia.playermanager; | |
import com.elementia.playermanager.init.Resource; | |
import com.google.inject.Inject; | |
import org.spongepowered.api.data.DataRegistration; | |
import org.spongepowered.api.data.key.Keys; | |
import org.spongepowered.api.entity.living.player.Player; | |
import org.spongepowered.api.event.Listener; | |
import org.spongepowered.api.event.filter.Getter; | |
import org.spongepowered.api.event.filter.data.Has; | |
import org.spongepowered.api.event.game.state.GameInitializationEvent; | |
import org.spongepowered.api.event.game.state.GamePreInitializationEvent; | |
import org.spongepowered.api.event.network.ClientConnectionEvent; | |
import org.spongepowered.api.plugin.Plugin; | |
import org.spongepowered.api.plugin.PluginContainer; | |
@Plugin(id = Resource.ID, name = Resource.NAME, | |
description = Resource.DESCRIPTION, version = Resource.VERSION) | |
public class PlayerManager { | |
@Inject | |
PluginContainer container; | |
@Listener | |
public void preInit(GamePreInitializationEvent e) { | |
MyKeys.dummy(); | |
DataRegistration.builder() | |
.dataName("My Standard Data") | |
.manipulatorId("standard_data") // prefix is added for you and you can't add it yourself | |
.dataClass(MyStandardData.class) | |
.immutableClass(MyImmutableStandardData.class) | |
.builder(new MyStandardDataBuilder()) | |
.buildAndRegister(container); | |
} | |
@Listener | |
public void init(GameInitializationEvent e) { | |
} | |
// keys don't work until you add the manipulator | |
@Listener | |
public void onJoin(ClientConnectionEvent.Join e, | |
@Getter("getTargetEntity") Player player) { | |
if (player.get(Keys.FIRST_DATE_PLAYED).equals(player.get(Keys.LAST_DATE_PLAYED))) | |
{ | |
MyStandardData data = player.getOrCreate(MyStandardData.class).get(); | |
data.setId(player.getUniqueId()); | |
data.setName(player.getName()); | |
player.offer(data); | |
} | |
} | |
public void setLvlPvP(Player player, int lvlPvp) | |
{ | |
MyStandardData data = player.getOrCreate(MyStandardData.class).get(); | |
data.setLvlPvP(lvlPvp); | |
player.offer(data); | |
} | |
public int getLvlPvP(Player player) | |
{ | |
MyStandardData data = player.getOrCreate(MyStandardData.class).get(); | |
return data.lvlPvP().get(); | |
} | |
public void setHonors(Player player, int honors) | |
{ | |
MyStandardData data = player.getOrCreate(MyStandardData.class).get(); | |
data.setHonors(honors); | |
player.offer(data); | |
} | |
public int getHonors(Player player) | |
{ | |
MyStandardData data = player.getOrCreate(MyStandardData.class).get(); | |
return data.honors().get(); | |
} | |
public void setElement(Player player, String element) | |
{ | |
MyStandardData data = player.getOrCreate(MyStandardData.class).get(); | |
data.setElement(element); | |
player.offer(data); | |
} | |
public String getElement(Player player) | |
{ | |
MyStandardData data = player.getOrCreate(MyStandardData.class).get(); | |
return data.element().get(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Maybe that is an issue? https://gist.github.com/alimtunc/235256e5f45aa9a43d85a300b8894834#file-playerdata-java-L110