How could I set the fluid amount in a block


#1

Hey all,
I’m wondering how I could set a fluid type and amount in a tank.
The block I’m using is the tank from open blocks and I want to set the fluid type to lava and the amount to 10000mb. How could I go about this?

Thanks,
fireboyev


#2

I could be wrong (im not a forge guy) but it seems like by getting the tile entity of the tank block. You should be able to cast it to this

After that use the getTank() method (seems how the tile entity checks its fluid, so probably can set it)

After you set it. Go back to the tile entity and call “update()”


#3

I would prefer to have it be done with just the sponge API as I would like it to work with other modded tanks too.


#4

I dont understand? Are you expecting the sponge api to have the functionality of every mod (or at least OpenBlock)?

As for allowing other tanks (im guessing you mean you want OpenBlock mod to be a soft dependency. As in only use OpenBlock code if OpenBlock is detected and if its not, your plugin will run without crashing) you could essentially isolate your OpenBlock code into another class and only initiate that OpenBlock code with a if statement checking that the target block is a openBlock block using the id of the target block and checking it compared to a string id.

I doubt it due to the fact TileEntities dont really relate to a full block id. But you maybe able to get a blockstate of the tank with a select fluid without needing anything other then Java and the SpongeAPI by using the SpongeRegister and getting a blockstate of the block with a written string of the full block id.


#5

Sponge has Keys.FLUID_TANK_CONTENTS.


#6

Shows my lack of knowledge of how sponge interacts with forge XD


#7

I managed to do it with for (Location loc : blockLocations) {
if (!loc.supports(Keys.FLUID_TANK_CONTENTS))
continue;
List fluids = new ArrayList();
FluidStack stack = FluidStack.builder().fluid(FluidTypes.LAVA).volume(10000).build();
fluids.add(stack.createSnapshot());
Map<Direction, List> map = loc.get(Keys.FLUID_TANK_CONTENTS).get();
for (Direction dir : map.keySet()) {
map.put(dir, fluids);
}
loc.addScheduledUpdate(1, 1);
}
And it works just fine, however I don’t think looping over the directions is very efficient. Up and none didn’t work. Only looping worked


#8

Depending on the tile’s implementation, some directions may not work, but in short, you hav the right answer.