Skip to content

Instantly share code, notes, and snippets.

@clienthax
Created September 25, 2017 14:25
Show Gist options
  • Save clienthax/f48d3194dd43160c33babfba4e511989 to your computer and use it in GitHub Desktop.
Save clienthax/f48d3194dd43160c33babfba4e511989 to your computer and use it in GitHub Desktop.
Yunowork
package uk.co.haxyshideout.bountyheads.data.sponge.interfaces.sponge;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.data.DataContainer;
import org.spongepowered.api.data.DataHolder;
import org.spongepowered.api.data.manipulator.mutable.common.AbstractData;
import org.spongepowered.api.data.merge.MergeFunction;
import org.spongepowered.api.data.value.ValueFactory;
import org.spongepowered.api.data.value.mutable.Value;
import uk.co.haxyshideout.bountyheads.data.BountyHeadKeys;
import uk.co.haxyshideout.bountyheads.data.sponge.interfaces.IBountyHeadData;
import java.time.Instant;
import java.util.Optional;
import static com.google.common.base.Preconditions.checkNotNull;
import static uk.co.haxyshideout.bountyheads.data.BountyHeadKeys.*;
/**
* Created by clienthax on 24/09/2017.
*/
public class BountyHeadData extends AbstractData<BountyHeadData, ImmutableBountyHeadData> implements IBountyHeadData {
public static final ValueFactory VALUE_FACTORY = Sponge.getRegistry().getValueFactory();
private int bountyAmount;
private long killedInstantEpochSeconds;
public BountyHeadData() {
this(0, Instant.now().getEpochSecond());
}
public BountyHeadData(int bountyAmount, long killedInstantEpochSeconds) {
this.bountyAmount = bountyAmount;
this.killedInstantEpochSeconds = killedInstantEpochSeconds;
registerGettersAndSetters();
}
@Override
protected void registerGettersAndSetters() {
registerFieldGetter(BOUNTY_AMOUNT, () -> this.bountyAmount);
registerFieldSetter(BOUNTY_AMOUNT, this::setBountyAmount);
registerKeyValue(BOUNTY_AMOUNT, this::bountyAmount);
registerFieldGetter(KILLED_INSTANT_EPOCH_SECONDS, () -> this.killedInstantEpochSeconds);
registerFieldSetter(KILLED_INSTANT_EPOCH_SECONDS, this::setKilledInstantEpochSeconds);
registerKeyValue(KILLED_INSTANT_EPOCH_SECONDS, this::timeInstantKilledEpochSeconds);
}
public void setBountyAmount(int bountyAmount) {
this.bountyAmount = bountyAmount;
}
public void setKilledInstantEpochSeconds(Long epochSeconds) {
this.killedInstantEpochSeconds = epochSeconds;
}
@Override
public Optional<BountyHeadData> fill(DataHolder dataHolder, MergeFunction overlap) {
BountyHeadData bountyHeadData = checkNotNull(checkNotNull(overlap).merge(copy(), from(dataHolder.toContainer()).orElse(null)));
return Optional.of(bountyHeadData.set(BOUNTY_AMOUNT, this.bountyAmount).set(KILLED_INSTANT_EPOCH_SECONDS, this.killedInstantEpochSeconds));
}
@Override
public Optional<BountyHeadData> from(DataContainer container) {
if(container.contains(BOUNTY_AMOUNT) && container.contains(KILLED_INSTANT_EPOCH_SECONDS)) {
int bountyAmount = container.getObject(BOUNTY_AMOUNT.getQuery(), Integer.class).get();
long killedInstantEpochSeconds = container.getObject(KILLED_INSTANT_EPOCH_SECONDS.getQuery(), Long.class).get();
return Optional.of(new BountyHeadData(bountyAmount, killedInstantEpochSeconds));
}
return Optional.empty();
}
@Override
public BountyHeadData copy() {
return new BountyHeadData(this.bountyAmount, this.killedInstantEpochSeconds);
}
@Override
public ImmutableBountyHeadData asImmutable() {
return new ImmutableBountyHeadData(this.bountyAmount, this.killedInstantEpochSeconds);
}
@Override
public int getContentVersion() {
return 0;
}
@Override
public Value<Long> timeInstantKilledEpochSeconds() {
return VALUE_FACTORY.createValue(KILLED_INSTANT_EPOCH_SECONDS, this.killedInstantEpochSeconds);
}
@Override
public Value<Integer> bountyAmount() {
return VALUE_FACTORY.createValue(BOUNTY_AMOUNT, this.bountyAmount);
}
}
package uk.co.haxyshideout.bountyheads.data.sponge.interfaces.sponge;
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 static uk.co.haxyshideout.bountyheads.data.BountyHeadKeys.BOUNTY_AMOUNT;
import static uk.co.haxyshideout.bountyheads.data.BountyHeadKeys.KILLED_INSTANT_EPOCH_SECONDS;
/**
* Created by clienthax on 24/09/2017.
*/
public class BountyHeadDataBuilder implements DataManipulatorBuilder<BountyHeadData, ImmutableBountyHeadData> {
@Override
public BountyHeadData create() {
return new BountyHeadData();
}
@Override
public Optional<BountyHeadData> createFrom(DataHolder dataHolder) {
return create().fill(dataHolder);
}
@Override
public Optional<BountyHeadData> build(DataView container) throws InvalidDataException {
BountyHeadData bountyHeadData = new BountyHeadData();
if(container.contains(BOUNTY_AMOUNT.getQuery())) {
bountyHeadData = bountyHeadData.set(BOUNTY_AMOUNT, container.getObject(BOUNTY_AMOUNT.getQuery(), Integer.class).get());
}
if(container.contains(KILLED_INSTANT_EPOCH_SECONDS.getQuery())) {
bountyHeadData = bountyHeadData.set(KILLED_INSTANT_EPOCH_SECONDS, container.getObject(KILLED_INSTANT_EPOCH_SECONDS.getQuery(), Long.class).get());
}
if(!container.contains(BOUNTY_AMOUNT.getQuery(), KILLED_INSTANT_EPOCH_SECONDS.getQuery()))
{
return Optional.empty();
}
return Optional.of(bountyHeadData);
}
}
package uk.co.haxyshideout.bountyheads.data;
import org.spongepowered.api.data.key.Key;
import org.spongepowered.api.data.value.mutable.Value;
import static org.spongepowered.api.data.DataQuery.of;
import static org.spongepowered.api.data.key.KeyFactory.makeSingleKey;
import static org.spongepowered.api.util.TypeTokens.*;
/**
* Created by clienthax on 19/09/2017.
*/
public class BountyHeadKeys {
public static Key<Value<Integer>> BOUNTY_AMOUNT = makeSingleKey(INTEGER_TOKEN, INTEGER_VALUE_TOKEN, of("bountyAmount"), "bountyheads:bountyamount", "Bounty Amount");
public static Key<Value<Long>> KILLED_INSTANT_EPOCH_SECONDS = makeSingleKey(LONG_TOKEN, LONG_VALUE_TOKEN, of("killedInstantEpochSeconds"), "bountyheads:killedinstantepochseconds", "Killed Instant Epoch Seconds");
}
package uk.co.haxyshideout.bountyheads.data.sponge.interfaces;
import org.spongepowered.api.data.value.mutable.Value;
/**
* Created by clienthax on 24/09/2017.
*/
public interface IBountyHeadData {
Value<Long> timeInstantKilledEpochSeconds();
Value<Integer> bountyAmount();
}
package uk.co.haxyshideout.bountyheads.data.sponge.interfaces;
import com.google.common.collect.ImmutableTable;
import org.spongepowered.api.data.value.immutable.ImmutableValue;
/**
* Created by clienthax on 24/09/2017.
*/
public interface IImmutableBountyHeadData {
ImmutableValue<Long> timeInstantKilledEpochSeconds();
ImmutableValue<Integer> bountyAmount();
}
package uk.co.haxyshideout.bountyheads.data.sponge.interfaces.sponge;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.data.manipulator.immutable.common.AbstractImmutableData;
import org.spongepowered.api.data.value.ValueFactory;
import org.spongepowered.api.data.value.immutable.ImmutableValue;
import uk.co.haxyshideout.bountyheads.data.sponge.interfaces.IImmutableBountyHeadData;
import static uk.co.haxyshideout.bountyheads.data.BountyHeadKeys.BOUNTY_AMOUNT;
import static uk.co.haxyshideout.bountyheads.data.BountyHeadKeys.KILLED_INSTANT_EPOCH_SECONDS;
/**
* Created by clienthax on 24/09/2017.
*/
public class ImmutableBountyHeadData extends AbstractImmutableData<ImmutableBountyHeadData, BountyHeadData> implements IImmutableBountyHeadData {
public static final ValueFactory VALUE_FACTORY = Sponge.getRegistry().getValueFactory();
private int bountyAmount;
private long killedInstantEpochSeconds;
final ImmutableValue<Integer> bountyAmountValue;
final ImmutableValue<Long> killedInstantEpochSecondsValue;
public ImmutableBountyHeadData(int bountyAmount, long killedInstantEpochSeconds) {
this.bountyAmount = bountyAmount;
this.killedInstantEpochSeconds = killedInstantEpochSeconds;
this.bountyAmountValue = VALUE_FACTORY.createValue(BOUNTY_AMOUNT, bountyAmount).asImmutable();
this.killedInstantEpochSecondsValue = VALUE_FACTORY.createValue(KILLED_INSTANT_EPOCH_SECONDS, killedInstantEpochSeconds).asImmutable();
registerGetters();
}
@Override
protected void registerGetters() {
registerFieldGetter(BOUNTY_AMOUNT, () -> this.bountyAmount);
registerKeyValue(BOUNTY_AMOUNT, this::bountyAmount);
registerFieldGetter(KILLED_INSTANT_EPOCH_SECONDS, () -> this.killedInstantEpochSeconds);
registerKeyValue(KILLED_INSTANT_EPOCH_SECONDS, this::timeInstantKilledEpochSeconds);
}
@Override
public BountyHeadData asMutable() {
return new BountyHeadData(this.bountyAmount, this.killedInstantEpochSeconds);
}
@Override
public int getContentVersion() {
return 0;
}
@Override
public ImmutableValue<Long> timeInstantKilledEpochSeconds() {
return killedInstantEpochSecondsValue;
}
@Override
public ImmutableValue<Integer> bountyAmount() {
return bountyAmountValue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment