-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It's not about the money money money.:dollar:
- Loading branch information
Hugo Beaucamps
committed
Jan 18, 2020
1 parent
b310dab
commit d5325cb
Showing
6 changed files
with
252 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.onaple.jessietags; | ||
|
||
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; | ||
|
||
public class JessieKeys { | ||
|
||
public static Key<Value<Integer>> PRICE_TAG; | ||
public JessieKeys(){ | ||
PRICE_TAG = Key.builder() | ||
.id("price.tag") | ||
.name("Price tag") | ||
.query(DataQuery.of(".","price")) | ||
.type(TypeTokens.INTEGER_VALUE_TOKEN) | ||
.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.onaple.jessietags; | ||
|
||
import org.spongepowered.api.command.CommandException; | ||
import org.spongepowered.api.command.CommandResult; | ||
import org.spongepowered.api.command.CommandSource; | ||
import org.spongepowered.api.command.args.CommandContext; | ||
import org.spongepowered.api.command.spec.CommandExecutor; | ||
import org.spongepowered.api.data.type.HandTypes; | ||
import org.spongepowered.api.entity.living.player.Player; | ||
import org.spongepowered.api.item.inventory.ItemStack; | ||
import org.spongepowered.api.text.Text; | ||
|
||
import java.util.Optional; | ||
|
||
public class PriceGetCommand implements CommandExecutor { | ||
|
||
@Override | ||
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException { | ||
ItemStack item = ((Player) src).getItemInHand(HandTypes.MAIN_HAND).get(); | ||
|
||
Optional<Integer> price = item.get(JessieKeys.PRICE_TAG); | ||
if(price.isPresent()){ | ||
src.sendMessage(Text.builder("item price is "+ price.get()).toText()); | ||
} else { | ||
src.sendMessage(Text.builder("no item price found").toText()); | ||
} | ||
return CommandResult.success(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.onaple.jessietags; | ||
|
||
import org.slf4j.Logger; | ||
import org.spongepowered.api.command.CommandException; | ||
import org.spongepowered.api.command.CommandResult; | ||
import org.spongepowered.api.command.CommandSource; | ||
import org.spongepowered.api.command.args.CommandContext; | ||
import org.spongepowered.api.command.spec.CommandExecutor; | ||
import org.spongepowered.api.data.type.HandType; | ||
import org.spongepowered.api.data.type.HandTypes; | ||
import org.spongepowered.api.entity.living.player.Player; | ||
import org.spongepowered.api.item.inventory.ItemStack; | ||
import org.spongepowered.api.text.Text; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Singleton; | ||
|
||
@Singleton | ||
public class PriceSetCommand implements CommandExecutor { | ||
|
||
@Inject | ||
Logger logger; | ||
|
||
@Override | ||
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException { | ||
int price = args.<Integer>getOne("price").get(); | ||
ItemStack item = ((Player) src).getItemInHand(HandTypes.MAIN_HAND).get(); | ||
item.offer(item.getOrCreate(TagDataManipulator.class).get()); | ||
item.offer(JessieKeys.PRICE_TAG,price); | ||
logger.info("support = {}, itemStack = {}",item.supports(JessieKeys.PRICE_TAG),item); | ||
((Player) src).setItemInHand(HandTypes.MAIN_HAND,item); | ||
src.sendMessage(Text.of("price", price,"set to", item)); | ||
return CommandResult.success(); | ||
} | ||
} |
124 changes: 124 additions & 0 deletions
124
src/main/java/com/onaple/jessietags/TagDataManipulator.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package com.onaple.jessietags; | ||
|
||
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.key.Key; | ||
import org.spongepowered.api.data.manipulator.DataManipulatorBuilder; | ||
import org.spongepowered.api.data.manipulator.ImmutableDataManipulator; | ||
import org.spongepowered.api.data.manipulator.immutable.common.AbstractImmutableSingleData; | ||
import org.spongepowered.api.data.manipulator.mutable.common.AbstractSingleData; | ||
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.BaseValue; | ||
import org.spongepowered.api.data.value.immutable.ImmutableValue; | ||
import org.spongepowered.api.data.value.mutable.Value; | ||
|
||
import java.util.Optional; | ||
|
||
public class TagDataManipulator extends AbstractSingleData<Integer,TagDataManipulator, TagDataManipulator.Immutable> { | ||
|
||
protected TagDataManipulator(Integer value) { | ||
super(value, JessieKeys.PRICE_TAG); | ||
} | ||
|
||
@Override | ||
protected Value<?> getValueGetter() { | ||
return Sponge.getRegistry().getValueFactory().createValue(JessieKeys.PRICE_TAG,getValue()); | ||
} | ||
|
||
@Override | ||
public Optional<TagDataManipulator> fill(DataHolder dataHolder, MergeFunction overlap) { | ||
Optional<TagDataManipulator> data_ = dataHolder.get(TagDataManipulator.class); | ||
if (data_.isPresent()) { | ||
TagDataManipulator data = data_.get(); | ||
TagDataManipulator finalData = overlap.merge(this, data); | ||
setValue(finalData.getValue()); | ||
} | ||
return Optional.of(this); | ||
} | ||
|
||
@Override | ||
public Optional<TagDataManipulator> from(DataContainer container) { | ||
return from((DataView) container); | ||
} | ||
|
||
public Optional<TagDataManipulator> from(DataView view) { | ||
if (view.contains(JessieKeys.PRICE_TAG.getQuery())) { | ||
setValue(view.getInt(JessieKeys.PRICE_TAG.getQuery()).get()); | ||
return Optional.of(this); | ||
} else { | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
@Override | ||
public TagDataManipulator copy() { | ||
return new TagDataManipulator(getValue()); | ||
} | ||
|
||
@Override | ||
public Immutable asImmutable() { | ||
return new Immutable(getValue()); | ||
} | ||
|
||
@Override | ||
public int getContentVersion() { | ||
return 1; | ||
} | ||
|
||
@Override | ||
public DataContainer toContainer() { | ||
return super.toContainer().set(JessieKeys.PRICE_TAG.getQuery(),getValue()); | ||
} | ||
|
||
|
||
public static class Immutable extends AbstractImmutableSingleData<Integer,Immutable,TagDataManipulator>{ | ||
|
||
protected Immutable(Integer value) { | ||
super(value, JessieKeys.PRICE_TAG); | ||
} | ||
|
||
@Override | ||
protected ImmutableValue<?> getValueGetter() { | ||
return Sponge.getRegistry().getValueFactory().createValue(JessieKeys.PRICE_TAG, getValue()).asImmutable(); | ||
} | ||
|
||
@Override | ||
public TagDataManipulator asMutable() { | ||
return new TagDataManipulator(getValue()); | ||
} | ||
|
||
@Override | ||
public int getContentVersion() { | ||
return 1; | ||
} | ||
|
||
@Override | ||
public DataContainer toContainer() { | ||
return super.toContainer().set(JessieKeys.PRICE_TAG.getQuery(),getValue()); | ||
} | ||
} | ||
public static class Builder extends AbstractDataBuilder<TagDataManipulator> implements DataManipulatorBuilder<TagDataManipulator, Immutable> { | ||
Builder() { | ||
super(TagDataManipulator.class, 1); | ||
} | ||
|
||
@Override | ||
public TagDataManipulator create() { | ||
return new TagDataManipulator(0); | ||
} | ||
|
||
@Override | ||
public Optional<TagDataManipulator> createFrom(DataHolder dataHolder) { | ||
return create().fill(dataHolder); | ||
} | ||
|
||
@Override | ||
protected Optional<TagDataManipulator> 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.onaple.jessietags; | ||
|
||
import com.google.inject.internal.cglib.proxy.$MethodProxy; | ||
import com.onaple.itemizer.data.beans.ItemNbtFactory; | ||
import ninja.leaping.configurate.objectmapping.Setting; | ||
import org.spongepowered.api.data.key.Key; | ||
import org.spongepowered.api.data.manipulator.DataManipulator; | ||
|
||
import java.util.List; | ||
|
||
public class TagFactory implements ItemNbtFactory { | ||
|
||
@Setting("price") | ||
private Integer price; | ||
|
||
@Override | ||
public Key getKey() { | ||
return JessieKeys.PRICE_TAG; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "Jessie's tags"; | ||
} | ||
|
||
@Override | ||
public DataManipulator<?, ?> constructDataManipulator() { | ||
return new TagDataManipulator(price); | ||
} | ||
|
||
@Override | ||
public int compareTo(ItemNbtFactory itemNbtFactory) { | ||
return 0; | ||
} | ||
} |