Heyho,
i tried to use the Sponge data api to store addtional informations to an itemstack.
Unfortunately there is the following error i try to fix but nothing worked out…
I created the serializable class, the builder and the keys. I also registered the classes into the data manager. (Classes below)
Dataclass:
public class EquipmentProperty implements DataSerializable{
public static final DataQuery EQP_ID = DataQuery.of("EQP_ID");
public static final DataQuery EQP_NAME = DataQuery.of("EQP_NAME");
private int id;
private String name;
public EquipmentProperty(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public int getContentVersion() {
return EquipmentPropertyBuilder.CONENT_VERSION;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
@Override
public DataContainer toContainer() {
return new MemoryDataContainer()
.set(EQP_ID,this.id)
.set(EQP_NAME, this.name)
.set(Queries.CONTENT_VERSION, EquipmentPropertyBuilder.CONENT_VERSION);
}
}
Builder-Class:
public class EquipmentPropertyBuilder extends AbstractDataBuilder<EquipmentProperty>{
public static final int CONENT_VERSION = 1;
public EquipmentPropertyBuilder() {
super(EquipmentProperty.class, CONENT_VERSION);
}
@Override
protected Optional<EquipmentProperty> buildContent(DataView container) throws InvalidDataException {
if(!container.contains(EquipmentProperty.EQP_ID,EquipmentProperty.EQP_NAME))return Optional.empty();
int id = container.getInt(EquipmentProperty.EQP_ID).get();
String name = container.getString(EquipmentProperty.EQP_NAME).get();
return Optional.of(new EquipmentProperty(id, name));
}
}
Key:
public class Keys {
public static final Key<Value<EquipmentProperty>> EQUIPMENTPROPERTIE = KeyFactory.makeSingleKey(
TypeToken.of(EquipmentProperty.class),
new TypeToken<Value<EquipmentProperty>>() {},
DataQuery.of("EQUIPMENTPROPERTIE"), "EQUIPMENTPROPERTIE:EQUIPMENTPROPERTIE", "EQUIPMENTPROPERTIE");
}
Main:
@Listener
public void init(GameInitializationEvent event){
DataManager dataManager = Sponge.getDataManager();
dataManager.registerBuilder(EquipmentProperty.class,new EquipmentPropertyBuilder());
}
Adding to Itemstack:
try {
this.itemStack.tryOffer(Keys.EQUIPMENTPROPERTIE, new EquipmentProperty(1, "Test"));
}catch(Exception e) {
e.printStackTrace();
}
Maybe you guys know what I am doing wrong
Thanks in advance.
Greetings, Kyraja