-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
525 additions
and
91 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
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
18 changes: 17 additions & 1 deletion
18
src/main/java/eu/pb4/polyfactory/block/fluids/FluidContainerOwner.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 |
---|---|---|
@@ -1,10 +1,26 @@ | ||
package eu.pb4.polyfactory.block.fluids; | ||
|
||
import eu.pb4.polyfactory.block.other.FilledStateProvider; | ||
import eu.pb4.polyfactory.fluid.FluidContainer; | ||
import eu.pb4.polyfactory.util.FactoryUtil; | ||
import net.minecraft.text.Text; | ||
import net.minecraft.util.Formatting; | ||
import net.minecraft.util.math.Direction; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public interface FluidContainerOwner { | ||
public interface FluidContainerOwner extends FilledStateProvider { | ||
@Nullable | ||
FluidContainer getFluidContainer(Direction direction); | ||
|
||
@Nullable | ||
FluidContainer getMainFluidContainer(); | ||
|
||
|
||
@Override | ||
default Text getFilledStateText() { | ||
var main = getMainFluidContainer(); | ||
return main != null ? | ||
Text.translatable("text.polyfactory.x_out_of_y", FactoryUtil.fluidText(main.stored()), FactoryUtil.fluidText(main.capacity())) | ||
: null; | ||
} | ||
} |
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
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
135 changes: 135 additions & 0 deletions
135
src/main/java/eu/pb4/polyfactory/block/fluids/PortableFluidTankBlock.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,135 @@ | ||
package eu.pb4.polyfactory.block.fluids; | ||
|
||
import eu.pb4.factorytools.api.block.BarrierBasedWaterloggable; | ||
import eu.pb4.factorytools.api.block.FactoryBlock; | ||
import eu.pb4.factorytools.api.virtualentity.BlockModel; | ||
import eu.pb4.factorytools.api.virtualentity.ItemDisplayElementUtil; | ||
import eu.pb4.polyfactory.block.property.ConnectablePart; | ||
import eu.pb4.polyfactory.block.property.FactoryProperties; | ||
import eu.pb4.polyfactory.fluid.FluidContainer; | ||
import eu.pb4.polyfactory.fluid.FluidInstance; | ||
import eu.pb4.polyfactory.models.FactoryModels; | ||
import eu.pb4.polyfactory.models.fluid.MultiFluidViewModel; | ||
import eu.pb4.polymer.virtualentity.api.ElementHolder; | ||
import eu.pb4.polymer.virtualentity.api.attachment.BlockAwareAttachment; | ||
import eu.pb4.polymer.virtualentity.api.attachment.HolderAttachment; | ||
import eu.pb4.polymer.virtualentity.api.elements.ItemDisplayElement; | ||
import net.minecraft.block.Block; | ||
import net.minecraft.block.BlockEntityProvider; | ||
import net.minecraft.block.BlockState; | ||
import net.minecraft.block.Blocks; | ||
import net.minecraft.block.entity.BlockEntity; | ||
import net.minecraft.block.entity.BlockEntityTicker; | ||
import net.minecraft.block.entity.BlockEntityType; | ||
import net.minecraft.item.ItemPlacementContext; | ||
import net.minecraft.server.network.ServerPlayerEntity; | ||
import net.minecraft.server.world.ServerWorld; | ||
import net.minecraft.state.StateManager; | ||
import net.minecraft.state.property.DirectionProperty; | ||
import net.minecraft.state.property.EnumProperty; | ||
import net.minecraft.state.property.Properties; | ||
import net.minecraft.util.math.BlockPos; | ||
import net.minecraft.util.math.Direction; | ||
import net.minecraft.world.World; | ||
import net.minecraft.world.WorldAccess; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.joml.Vector3f; | ||
|
||
public class PortableFluidTankBlock extends Block implements FactoryBlock, PipeConnectable, BlockEntityProvider, BarrierBasedWaterloggable { | ||
public static final DirectionProperty FACING = Properties.FACING; | ||
public PortableFluidTankBlock(Settings settings) { | ||
super(settings); | ||
this.setDefaultState(this.getDefaultState().with(WATERLOGGED, false)); | ||
} | ||
|
||
@Override | ||
public boolean canPipeConnect(WorldAccess world, BlockPos pos, BlockState state, Direction dir) { | ||
return state.get(FACING) == dir; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public BlockState getPlacementState(ItemPlacementContext ctx) { | ||
var sidePos = ctx.getBlockPos().offset(ctx.getSide(), -1); | ||
var state = ctx.getWorld().getBlockState(sidePos); | ||
if (state.getBlock() instanceof PipeConnectable connectable && connectable.canPipeConnect(ctx.getWorld(), sidePos, state, ctx.getSide())) { | ||
return this.getDefaultState().with(FACING, ctx.getSide().getOpposite()); | ||
} | ||
return this.getDefaultState().with(FACING, ctx.getSide()); | ||
} | ||
|
||
@Override | ||
protected BlockState getStateForNeighborUpdate(BlockState state, Direction direction, BlockState neighborState, WorldAccess world, BlockPos pos, BlockPos neighborPos) { | ||
tickWater(state, world, pos); | ||
return state; | ||
} | ||
|
||
@Override | ||
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) { | ||
super.appendProperties(builder); | ||
builder.add(WATERLOGGED, FACING); | ||
} | ||
|
||
@Override | ||
public @Nullable ElementHolder createElementHolder(ServerWorld world, BlockPos pos, BlockState initialBlockState) { | ||
return new Model(initialBlockState); | ||
} | ||
|
||
@Override | ||
public boolean tickElementHolder(ServerWorld world, BlockPos pos, BlockState initialBlockState) { | ||
return true; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public BlockEntity createBlockEntity(BlockPos pos, BlockState state) { | ||
return new PortableFluidTankBlockEntity(pos, state); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(World world, BlockState state, BlockEntityType<T> type) { | ||
return PortableFluidTankBlockEntity::tick; | ||
} | ||
|
||
@Override | ||
public BlockState getPolymerBreakEventBlockState(BlockState state, ServerPlayerEntity player) { | ||
return Blocks.COPPER_BLOCK.getDefaultState(); | ||
} | ||
|
||
public static final class Model extends BlockModel { | ||
private final ItemDisplayElement main; | ||
|
||
private Model(BlockState state) { | ||
this.main = ItemDisplayElementUtil.createSimple(state.getBlock().asItem()); | ||
this.main.setScale(new Vector3f(2f)); | ||
updateStatePos(state); | ||
this.addElement(this.main); | ||
} | ||
|
||
private void updateStatePos(BlockState state) { | ||
var dir = state.get(FACING); | ||
float p = 0; | ||
float y = 0; | ||
|
||
if (dir.getAxis() != Direction.Axis.Y) { | ||
p = 90; | ||
y = dir.asRotation(); | ||
} else if (dir == Direction.DOWN) { | ||
p = 180; | ||
} | ||
|
||
|
||
this.main.setYaw(y); | ||
this.main.setPitch(p); | ||
} | ||
|
||
@Override | ||
public void notifyUpdate(HolderAttachment.UpdateType updateType) { | ||
super.notifyUpdate(updateType); | ||
if (updateType == BlockAwareAttachment.BLOCK_STATE_UPDATE) { | ||
updateStatePos(this.blockState()); | ||
} | ||
} | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
src/main/java/eu/pb4/polyfactory/block/fluids/PortableFluidTankBlockEntity.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,91 @@ | ||
package eu.pb4.polyfactory.block.fluids; | ||
|
||
import eu.pb4.polyfactory.block.BlockHeat; | ||
import eu.pb4.polyfactory.block.FactoryBlockEntities; | ||
import eu.pb4.polyfactory.fluid.FluidContainer; | ||
import eu.pb4.polyfactory.item.FactoryDataComponents; | ||
import eu.pb4.polyfactory.item.component.FluidComponent; | ||
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidConstants; | ||
import net.minecraft.block.BlockState; | ||
import net.minecraft.block.entity.BlockEntity; | ||
import net.minecraft.component.ComponentMap; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.nbt.NbtCompound; | ||
import net.minecraft.registry.RegistryWrapper; | ||
import net.minecraft.server.world.ServerWorld; | ||
import net.minecraft.util.ItemScatterer; | ||
import net.minecraft.util.math.BlockPos; | ||
import net.minecraft.util.math.Direction; | ||
import net.minecraft.world.World; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public class PortableFluidTankBlockEntity extends BlockEntity implements FluidInputOutput.ContainerBased { | ||
public static final long CAPACITY = FluidConstants.BLOCK * 3; | ||
private final FluidContainer container = new FluidContainer(CAPACITY, this::onFluidChanged); | ||
private float blockTemperature = 0; | ||
|
||
public PortableFluidTankBlockEntity(BlockPos pos, BlockState state) { | ||
super(FactoryBlockEntities.PORTABLE_FLUID_TANK, pos, state); | ||
} | ||
|
||
public FluidContainer getFluidContainer() { | ||
return this.container; | ||
} | ||
|
||
@Override | ||
protected void writeNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLookup) { | ||
super.writeNbt(nbt, registryLookup); | ||
nbt.put("fluid", this.container.toNbt(registryLookup)); | ||
} | ||
|
||
@Override | ||
protected void readNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLookup) { | ||
super.readNbt(nbt, registryLookup); | ||
this.container.fromNbt(registryLookup, nbt, "fluid"); | ||
} | ||
@Override | ||
protected void readComponents(ComponentsAccess components) { | ||
super.readComponents(components); | ||
var f = components.get(FactoryDataComponents.FLUID); | ||
if (f != null) { | ||
this.container.clear(); | ||
f.extractTo(this.container); | ||
} | ||
} | ||
|
||
@Override | ||
protected void addComponents(ComponentMap.Builder componentMapBuilder) { | ||
super.addComponents(componentMapBuilder); | ||
componentMapBuilder.add(FactoryDataComponents.FLUID, FluidComponent.copyFrom(this.container)); | ||
} | ||
|
||
@Override | ||
public void removeFromCopiedStackNbt(NbtCompound nbt) { | ||
super.removeFromCopiedStackNbt(nbt); | ||
nbt.remove("fluid"); | ||
} | ||
@Override | ||
public FluidContainer getFluidContainer(Direction direction) { | ||
return this.container; | ||
} | ||
|
||
@Override | ||
public @Nullable FluidContainer getMainFluidContainer() { | ||
return this.container; | ||
} | ||
|
||
private void onFluidChanged() { | ||
this.markDirty(); | ||
} | ||
public static <T extends BlockEntity> void tick(World world, BlockPos pos, BlockState state, T t) { | ||
if (!(t instanceof PortableFluidTankBlockEntity tank)) { | ||
return; | ||
} | ||
tank.blockTemperature = BlockHeat.get(world.getBlockState(pos.down())) + tank.container.fluidTemperature(); | ||
tank.container.tick((ServerWorld) world, pos, tank.blockTemperature, tank::dropItem); | ||
} | ||
|
||
private void dropItem(ItemStack stack) { | ||
ItemScatterer.spawn(world, this.pos.getX() + 0.5, this.pos.getY() + 0.5, this.pos.getZ() + 0.5, stack); | ||
} | ||
} |
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
Oops, something went wrong.