Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.shmeeb.adminshopec.data;
- 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.DataView;
- import org.spongepowered.api.data.manipulator.DataManipulatorBuilder;
- import org.spongepowered.api.data.manipulator.immutable.common.AbstractImmutableData;
- import org.spongepowered.api.data.manipulator.mutable.common.AbstractData;
- import org.spongepowered.api.data.merge.MergeFunction;
- import org.spongepowered.api.data.persistence.AbstractDataBuilder;
- import org.spongepowered.api.data.persistence.InvalidDataException;
- import org.spongepowered.api.data.value.immutable.ImmutableValue;
- import org.spongepowered.api.data.value.mutable.Value;
- import org.spongepowered.api.item.inventory.ItemStack;
- public class Data extends AbstractData<Data, Data.Immutable> {
- private ItemStack item;
- private Integer cost;
- private Integer amount;
- private boolean buy;
- {
- registerGettersAndSetters();
- }
- Data() {
- buy = false;
- }
- public Data(ItemStack item, Integer cost, Integer amount, boolean buy) {
- this.item = item;
- this.cost = cost;
- this.amount = amount;
- this.buy = buy;
- }
- @Override
- protected void registerGettersAndSetters() {
- registerFieldGetter(Keys.ITEM, this::getItem);
- registerFieldSetter(Keys.ITEM, this::setItem);
- registerKeyValue(Keys.ITEM, this::item);
- registerFieldGetter(Keys.COST, this::getCost);
- registerFieldSetter(Keys.COST, this::setCost);
- registerKeyValue(Keys.COST, this::cost);
- registerFieldGetter(Keys.AMOUNT, this::getAmount);
- registerFieldSetter(Keys.AMOUNT, this::setAmount);
- registerKeyValue(Keys.AMOUNT, this::amount);
- registerFieldGetter(Keys.BUY, this::isBuy);
- registerFieldSetter(Keys.BUY, this::setBuy);
- registerKeyValue(Keys.BUY, this::buy);
- }
- public ItemStack getItem() {
- return item;
- }
- public void setItem(ItemStack item) {
- this.item = item;
- }
- public Value<ItemStack> item() {
- return Sponge.getRegistry().getValueFactory().createValue(Keys.ITEM, item);
- }
- public Integer getCost() {
- return cost;
- }
- public void setCost(Integer cost) {
- this.cost = cost;
- }
- public Value<Integer> cost() {
- return Sponge.getRegistry().getValueFactory().createValue(Keys.COST, cost);
- }
- public Integer getAmount() {
- return amount;
- }
- public void setAmount(Integer amount) {
- this.amount = amount;
- }
- public Value<Integer> amount() {
- return Sponge.getRegistry().getValueFactory().createValue(Keys.AMOUNT, amount);
- }
- public boolean isBuy() {
- return buy;
- }
- public void setBuy(boolean buy) {
- this.buy = buy;
- }
- public Value<Boolean> buy() {
- return Sponge.getRegistry().getValueFactory().createValue(Keys.BUY, buy);
- }
- @Override
- public Optional<Data> fill(DataHolder dataHolder, MergeFunction overlap) {
- dataHolder.get(Data.class).ifPresent(that -> {
- Data data = overlap.merge(this, that);
- this.item = data.item;
- this.cost = data.cost;
- this.amount = data.amount;
- this.buy = data.buy;
- });
- return Optional.of(this);
- }
- @Override
- public Optional<Data> from(DataContainer container) {
- return from((DataView) container);
- }
- public Optional<Data> from(DataView container) {
- container.getObject(Keys.ITEM.getQuery(), ItemStack.class).ifPresent(v -> item = v);
- container.getObject(Keys.COST.getQuery(), Integer.class).ifPresent(v -> cost = v);
- container.getObject(Keys.AMOUNT.getQuery(), Integer.class).ifPresent(v -> amount = v);
- container.getBoolean(Keys.BUY.getQuery()).ifPresent(v -> buy = v);
- return Optional.of(this);
- }
- @Override
- public Data copy() {
- return new Data(item, cost, amount, buy);
- }
- @Override
- public Immutable asImmutable() {
- return new Immutable(item, cost, amount, buy);
- }
- @Override
- public int getContentVersion() {
- return 1;
- }
- @Override
- public DataContainer toContainer() {
- return super.toContainer()
- .set(Keys.ITEM.getQuery(), item)
- .set(Keys.COST.getQuery(), cost)
- .set(Keys.AMOUNT.getQuery(), amount)
- .set(Keys.BUY.getQuery(), buy);
- }
- public static class Immutable extends AbstractImmutableData<Immutable, Data> {
- private ItemStack item;
- private Integer cost;
- private Integer amount;
- private boolean buy;
- {
- registerGetters();
- }
- Immutable() {
- buy = false;
- }
- Immutable(ItemStack item, Integer cost, Integer amount, boolean buy) {
- this.item = item;
- this.cost = cost;
- this.amount = amount;
- this.buy = buy;
- }
- @Override
- protected void registerGetters() {
- registerFieldGetter(Keys.ITEM, this::getItem);
- registerKeyValue(Keys.ITEM, this::item);
- registerFieldGetter(Keys.COST, this::getCost);
- registerKeyValue(Keys.COST, this::cost);
- registerFieldGetter(Keys.AMOUNT, this::getAmount);
- registerKeyValue(Keys.AMOUNT, this::amount);
- registerFieldGetter(Keys.BUY, this::isBuy);
- registerKeyValue(Keys.BUY, this::buy);
- }
- public ItemStack getItem() {
- return item;
- }
- public ImmutableValue<ItemStack> item() {
- return Sponge.getRegistry().getValueFactory().createValue(Keys.ITEM, item).asImmutable();
- }
- public Integer getCost() {
- return cost;
- }
- public ImmutableValue<Integer> cost() {
- return Sponge.getRegistry().getValueFactory().createValue(Keys.COST, cost).asImmutable();
- }
- public Integer getAmount() {
- return amount;
- }
- public ImmutableValue<Integer> amount() {
- return Sponge.getRegistry().getValueFactory().createValue(Keys.AMOUNT, amount).asImmutable();
- }
- public boolean isBuy() {
- return buy;
- }
- public ImmutableValue<Boolean> buy() {
- return Sponge.getRegistry().getValueFactory().createValue(Keys.BUY, buy).asImmutable();
- }
- @Override
- public Data asMutable() {
- return new Data(item, cost, amount, buy);
- }
- @Override
- public int getContentVersion() {
- return 1;
- }
- @Override
- public DataContainer toContainer() {
- return super.toContainer()
- .set(Keys.ITEM.getQuery(), item)
- .set(Keys.COST.getQuery(), cost)
- .set(Keys.AMOUNT.getQuery(), amount)
- .set(Keys.BUY.getQuery(), buy);
- }
- }
- public static class Builder extends AbstractDataBuilder<Data> implements DataManipulatorBuilder<Data, Immutable> {
- public Builder() {
- super(Data.class, 1);
- }
- @Override
- public Data create() {
- return new Data();
- }
- @Override
- public Optional<Data> createFrom(DataHolder dataHolder) {
- return create().fill(dataHolder);
- }
- @Override
- protected Optional<Data> buildContent(DataView container) throws InvalidDataException {
- return create().from(container);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment