Non-static method problem


#1

Hello,
I would like to store in a ItemStack a BiomeType, but I have this error:

Error:(119, 18) java: non-static method offer(H) cannot be referenced from a static context

Code:

@Listener
public void onPreInitialization(GamePreInitializationEvent event) throws IOException {
    DataRegistration.builder()
            .id("biometype_id")
            .name("biometype_name")
            .dataClass(MyBiomeTypeData.class)
            .immutableClass(MyBiomeTypeData.Immutable.class)
            .builder(new MyBiomeTypeData.Builder())
            .build();

    ItemStack.offer(new MyBiomeTypeData());
}

Class:

public class MyBiomeTypeData extends AbstractSingleData<BiomeType, MyBiomeTypeData, MyBiomeTypeData.Immutable> {

public MyBiomeTypeData() {
    super(BiomeTypes.PLAINS, ToolKeys.BIOME_TYPE);
}

MyBiomeTypeData(BiomeType biometype) {
    super(biometype, ToolKeys.BIOME_TYPE);
}

@Override
public Value<BiomeType> getValueGetter() {
    return Sponge.getRegistry().getValueFactory().createValue(ToolKeys.BIOME_TYPE, getValue());
}

public Value<BiomeType> biometype() {
    return getValueGetter();
}

@Override
public Optional<MyBiomeTypeData> fill(DataHolder dataHolder, MergeFunction overlap) {
    Optional<MyBiomeTypeData> data_ = dataHolder.get(MyBiomeTypeData.class);
    if (data_.isPresent()) {
        MyBiomeTypeData data = data_.get();
        MyBiomeTypeData finalData = overlap.merge(this, data);
        setValue(finalData.getValue());
    }
    return Optional.of(this);
}

@Override
public Optional<MyBiomeTypeData> from(DataContainer container) {
    return Optional.of(this);
}

@Override
public MyBiomeTypeData copy() {
    return new MyBiomeTypeData(getValue());
}

@Override
public Immutable asImmutable() {
    return new Immutable(getValue());
}

@Override
public int getContentVersion() {
    return 1;
}

public class Immutable extends AbstractImmutableSingleData<BiomeType, Immutable, MyBiomeTypeData> {
    Immutable(BiomeType biometype) {
        super(biometype, ToolKeys.BIOME_TYPE);
    }

    @Override
    protected ImmutableValue<?> getValueGetter() {
        return Sponge.getRegistry().getValueFactory().createValue(ToolKeys.BIOME_TYPE, getValue()).asImmutable();
    }

    @Override
    public MyBiomeTypeData asMutable() {
        return new MyBiomeTypeData(getValue());
    }

    @Override
    public int getContentVersion() {
        return 1;
    }
}
public static class Builder extends AbstractDataBuilder<MyBiomeTypeData> implements DataManipulatorBuilder<MyBiomeTypeData, Immutable> {
    public Builder() {
        super(MyBiomeTypeData.class, 1);
    }

    @Override
    public MyBiomeTypeData create() {
        return new MyBiomeTypeData(BiomeTypes.PLAINS);
    }

    @Override
    public Optional<MyBiomeTypeData> createFrom(DataHolder dataHolder) {
        return create().fill(dataHolder);
    }

    @Override
    protected Optional<MyBiomeTypeData> buildContent(DataView container) throws InvalidDataException {
        return Optional.of(create());
    }
}

}


#2

This is a basic java error. You really should learn java before sponge.

The function offer only works if there is a item stack to offer to. By using the ItemStack class without an instance of the class and then using a function that only work if there is a instance of it will throw this error. This is the simplest way I could think of explaining it.

To create an instance of itemstack, use ItemStack.builder()