Compare commits

...

2 Commits

Author SHA1 Message Date
Shadowfacts c32eaeedb7
Add Grate model 2020-03-29 14:21:28 -04:00
Shadowfacts dd92515c2f
Grate fluid interaction improvements 2020-03-29 12:17:29 -04:00
14 changed files with 273 additions and 72 deletions

View File

@ -6,14 +6,17 @@ import net.minecraft.fluid.FluidState;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.BlockView;
import net.minecraft.world.IWorld;
import net.minecraft.world.World;
import net.minecraft.world.WorldView;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import net.shadowfacts.extrahoppers.util.FluidFlowControllable;
import net.shadowfacts.extrahoppers.util.PositionedFluidStateProvider;
import net.shadowfacts.extrahoppers.util.DynamicFluidStateProvider;
/**
* @author shadowfacts
@ -31,8 +34,8 @@ public abstract class MixinBaseFluid {
private FluidState getUpdatedState(FluidState initial, WorldView world, BlockPos pos, BlockState state) {
BlockPos up = pos.up();
BlockState upState = world.getBlockState(up);
if (upState.getBlock() instanceof PositionedFluidStateProvider) {
return ((PositionedFluidStateProvider)upState.getBlock()).getFluidState(upState, world, up);
if (upState.getBlock() instanceof DynamicFluidStateProvider) {
return ((DynamicFluidStateProvider)upState.getBlock()).getFluidState(upState, world, up);
}
return initial;
}
@ -43,16 +46,33 @@ public abstract class MixinBaseFluid {
cancellable = true
)
private void receivesFlow(Direction direction, BlockView world, BlockPos pos, BlockState state, BlockPos fromPos, BlockState fromState, CallbackInfoReturnable<Boolean> cir) {
FluidFlowControllable.Result result = FluidFlowControllable.Result.DEFAULT;
if (state.getBlock() instanceof FluidFlowControllable) {
result = ((FluidFlowControllable)state.getBlock()).allowsFlow(direction, state, world, pos);
DynamicFluidStateProvider.FlowResult result = DynamicFluidStateProvider.FlowResult.DEFAULT;
if (state.getBlock() instanceof DynamicFluidStateProvider) {
result = ((DynamicFluidStateProvider)state.getBlock()).allowsFlow(direction, state, world, pos);
}
if (result != FluidFlowControllable.Result.DENY && fromState.getBlock() instanceof FluidFlowControllable) {
result = ((FluidFlowControllable)fromState.getBlock()).allowsFlow(direction, fromState, world, fromPos);
if (result != DynamicFluidStateProvider.FlowResult.DENY && fromState.getBlock() instanceof DynamicFluidStateProvider) {
result = ((DynamicFluidStateProvider)fromState.getBlock()).allowsFlow(direction, fromState, world, fromPos);
}
if (result != FluidFlowControllable.Result.DEFAULT) {
cir.setReturnValue(result == FluidFlowControllable.Result.ALLOW);
if (result != DynamicFluidStateProvider.FlowResult.DEFAULT) {
cir.setReturnValue(result == DynamicFluidStateProvider.FlowResult.ALLOW);
}
}
@Inject(
method = "onScheduledTick(Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/fluid/FluidState;)V",
at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;setBlockState(Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/BlockState;I)Z"),
cancellable = true
)
private void onScheduledTick(World world, BlockPos pos, FluidState fluidState, CallbackInfo ci) {
BlockState state = world.getBlockState(pos);
if (state.getBlock() instanceof DynamicFluidStateProvider) {
((DynamicFluidStateProvider)state.getBlock()).setEmptyFluid(world, pos);
this.tryFlow(world, pos, fluidState);
ci.cancel();
}
}
@Shadow
protected abstract void tryFlow(IWorld world, BlockPos pos, FluidState state);
}

View File

@ -0,0 +1,37 @@
package net.shadowfacts.extrahoppers.mixin;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.fluid.Fluid;
import net.minecraft.fluid.Fluids;
import net.minecraft.item.BucketItem;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.shadowfacts.extrahoppers.util.DynamicFluidStateProvider;
import org.objectweb.asm.Opcodes;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
/**
* @author shadowfacts
*/
@Mixin(BucketItem.class)
public abstract class MixinBucketItem {
@Shadow
private Fluid fluid;
@Redirect(
method = "placeFluid(Lnet/minecraft/entity/player/PlayerEntity;Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/util/hit/BlockHitResult;)Z",
at = @At(value = "FIELD", opcode = Opcodes.GETFIELD, target = "Lnet/minecraft/item/BucketItem;fluid:Lnet/minecraft/fluid/Fluid;", ordinal = 4)
)
private Fluid getFluid(BucketItem item, PlayerEntity player, World world, BlockPos pos, BlockHitResult result) {
if (world.getBlockState(pos).getBlock() instanceof DynamicFluidStateProvider) {
return Fluids.WATER;
} else {
return this.fluid;
}
}
}

View File

@ -11,7 +11,7 @@ import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import net.shadowfacts.extrahoppers.util.PositionedFluidStateProvider;
import net.shadowfacts.extrahoppers.util.DynamicFluidStateProvider;
/**
* @author shadowfacts
@ -26,8 +26,8 @@ public abstract class MixinWorldChunk implements Chunk {
public void getFluidState(int x, int y, int z, CallbackInfoReturnable<FluidState> cir) {
BlockPos pos = new BlockPos(x, y, z);
BlockState state = getBlockState(pos);
if (state.getBlock() instanceof PositionedFluidStateProvider) {
FluidState fluidState = ((PositionedFluidStateProvider)state.getBlock()).getFluidState(state, world, pos);
if (state.getBlock() instanceof DynamicFluidStateProvider) {
FluidState fluidState = ((DynamicFluidStateProvider)state.getBlock()).getFluidState(state, world, pos);
cir.setReturnValue(fluidState);
cir.cancel();
}

View File

@ -0,0 +1,27 @@
package net.shadowfacts.extrahoppers.util;
import net.minecraft.block.BlockState;
import net.minecraft.fluid.FluidState;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.BlockView;
import net.minecraft.world.World;
import org.jetbrains.annotations.NotNull;
/**
* @author shadowfacts
*/
public interface DynamicFluidStateProvider {
FluidState getFluidState(@NotNull BlockState state, @NotNull BlockView world, @NotNull BlockPos pos);
@NotNull
FlowResult allowsFlow(@NotNull Direction toSide, @NotNull BlockState state, @NotNull BlockView world, @NotNull BlockPos pos);
void setEmptyFluid(@NotNull World world, @NotNull BlockPos pos);
enum FlowResult {
ALLOW,
DENY,
DEFAULT
}
}

View File

@ -1,23 +0,0 @@
package net.shadowfacts.extrahoppers.util;
import net.minecraft.block.BlockState;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.BlockView;
import org.jetbrains.annotations.NotNull;
/**
* @author shadowfacts
*/
public interface FluidFlowControllable {
@NotNull
Result allowsFlow(@NotNull Direction toSide, @NotNull BlockState state, @NotNull BlockView world, @NotNull BlockPos pos);
enum Result {
ALLOW,
DENY,
DEFAULT
}
}

View File

@ -1,14 +0,0 @@
package net.shadowfacts.extrahoppers.util;
import net.minecraft.block.BlockState;
import net.minecraft.fluid.FluidState;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.BlockView;
import org.jetbrains.annotations.NotNull;
/**
* @author shadowfacts
*/
public interface PositionedFluidStateProvider {
FluidState getFluidState(@NotNull BlockState state, @NotNull BlockView world, @NotNull BlockPos pos);
}

View File

@ -1,18 +1,23 @@
package net.shadowfacts.extrahoppers
import net.fabricmc.api.ClientModInitializer
import net.fabricmc.fabric.api.blockrenderlayer.v1.BlockRenderLayerMap
import net.fabricmc.fabric.api.client.screen.ContainerScreenFactory
import net.fabricmc.fabric.api.client.screen.ScreenProviderRegistry
import net.minecraft.client.render.RenderLayer
import net.shadowfacts.extrahoppers.block.gold.GoldHopperContainer
import net.shadowfacts.extrahoppers.block.gold.GoldHopperScreen
import net.shadowfacts.extrahoppers.block.wood.WoodHopperContainer
import net.shadowfacts.extrahoppers.block.wood.WoodHopperScreen
import net.shadowfacts.extrahoppers.init.EHBlocks
object ExtraHoppersClient: ClientModInitializer {
override fun onInitializeClient() {
ScreenProviderRegistry.INSTANCE.registerFactory(WoodHopperContainer.ID, ContainerScreenFactory(WoodHopperScreen.Companion::create))
ScreenProviderRegistry.INSTANCE.registerFactory(GoldHopperContainer.ID, ContainerScreenFactory(GoldHopperScreen.Companion::create))
BlockRenderLayerMap.INSTANCE.putBlock(EHBlocks.GRATE, RenderLayer.getCutout())
}
}

View File

@ -4,6 +4,7 @@ import net.fabricmc.fabric.api.block.FabricBlockSettings
import net.minecraft.block.*
import net.minecraft.block.enums.BlockHalf
import net.minecraft.entity.EntityContext
import net.minecraft.entity.EntityType
import net.minecraft.fluid.Fluid
import net.minecraft.fluid.FluidState
import net.minecraft.fluid.Fluids
@ -17,9 +18,9 @@ import net.minecraft.util.math.Direction
import net.minecraft.util.shape.VoxelShape
import net.minecraft.world.BlockView
import net.minecraft.world.IWorld
import net.minecraft.world.World
import net.shadowfacts.extrahoppers.block.base.BlockWithEntity
import net.shadowfacts.extrahoppers.util.FluidFlowControllable
import net.shadowfacts.extrahoppers.util.PositionedFluidStateProvider
import net.shadowfacts.extrahoppers.util.DynamicFluidStateProvider
/**
* @author shadowfacts
@ -30,15 +31,15 @@ class GrateBlock: BlockWithEntity<GrateBlockEntity>(
.sounds(BlockSoundGroup.METAL)
.nonOpaque()
.build()
), PositionedFluidStateProvider, FluidFlowControllable, FluidFillable, FluidDrainable {
), DynamicFluidStateProvider, FluidFillable, FluidDrainable {
companion object {
val ID = Identifier("extrahoppers", "grate")
val HALF = Properties.BLOCK_HALF
val TOP_SHAPE = createCuboidShape(0.0, 15.0, 0.0, 16.0, 16.0, 16.0)
val BOTTOM_SHAPE = createCuboidShape(0.0, 0.0, 0.0, 16.0, 1.0, 16.0)
val TOP_SHAPE = createCuboidShape(0.0, 14.0, 0.0, 16.0, 16.0, 16.0)
val BOTTOM_SHAPE = createCuboidShape(0.0, 0.0, 0.0, 16.0, 2.0, 16.0)
}
init {
@ -75,14 +76,46 @@ class GrateBlock: BlockWithEntity<GrateBlockEntity>(
override fun createBlockEntity(world: BlockView) = GrateBlockEntity()
override fun isTranslucent(state: BlockState, world: BlockView, pos: BlockPos): Boolean {
return true
}
override fun isSimpleFullBlock(state: BlockState, world: BlockView, pos: BlockPos): Boolean {
return false
}
override fun allowsSpawning(state: BlockState, world: BlockView, pos: BlockPos, entityType: EntityType<*>): Boolean {
return false
}
override fun getFluidState(state: BlockState, world: BlockView, pos: BlockPos): FluidState {
return getBlockEntity(world, pos)?.fluidState ?: Fluids.EMPTY.defaultState
}
override fun allowsFlow(toSide: Direction, state: BlockState, world: BlockView, pos: BlockPos): FluidFlowControllable.Result {
return FluidFlowControllable.Result.ALLOW
override fun setEmptyFluid(world: World, pos: BlockPos) {
val be = getBlockEntity(world, pos) ?: return
be.fluidState = null
if (!world.isClient) {
be.sync()
}
triggerFluidUpdate(world, pos, world.getBlockState(pos))
}
override fun allowsFlow(toSide: Direction, state: BlockState, world: BlockView, pos: BlockPos): DynamicFluidStateProvider.FlowResult {
return DynamicFluidStateProvider.FlowResult.ALLOW
}
override fun neighborUpdate(state: BlockState, world: World, pos: BlockPos, block: Block, neighborPos: BlockPos, bl: Boolean) {
val fluidState = getFluidState(state, world, pos)
if (!fluidState.isEmpty) {
world.fluidTickScheduler.schedule(pos, fluidState.fluid, fluidState.fluid.getTickRate(world))
}
}
private fun triggerFluidUpdate(world: World, pos: BlockPos, state: BlockState) {
world.updateListeners(pos, state, state, 3)
world.updateNeighbors(pos, state.block)
}
// Fluid Fillable
@ -94,11 +127,14 @@ class GrateBlock: BlockWithEntity<GrateBlockEntity>(
override fun tryFillWithFluid(world: IWorld, pos: BlockPos, state: BlockState, fluidState: FluidState): Boolean {
val be = getBlockEntity(world, pos)
return if (be != null) {
be.fluidState = fluidState
if (!world.isClient) {
be.fluidState = fluidState
world.fluidTickScheduler.schedule(pos, fluidState.fluid, fluidState.fluid.getTickRate(world))
be.sync()
}
if (world is World) {
triggerFluidUpdate(world, pos, state)
}
true
} else {
false
@ -109,10 +145,13 @@ class GrateBlock: BlockWithEntity<GrateBlockEntity>(
val be = getBlockEntity(world, pos) ?: return Fluids.EMPTY
val fluidState = be.fluidState ?: return Fluids.EMPTY
return if (fluidState.isStill) {
be.fluidState = null
if (!world.isClient) {
be.fluidState = null
be.sync()
}
if (world is World) {
triggerFluidUpdate(world, pos, state)
}
fluidState.fluid
} else {
Fluids.EMPTY

View File

@ -24,8 +24,10 @@ class GrateBlockEntity: BlockEntity(EHBlockEntities.GRATE), BlockEntityClientSer
override fun fromTag(tag: CompoundTag) {
super.fromTag(tag)
if (tag.contains("fluid", NbtType.COMPOUND)) {
fluidState = NbtHelper.toBlockState(tag.getCompound("fluid")).fluidState
fluidState = if (tag.contains("fluid", NbtType.COMPOUND)) {
NbtHelper.toBlockState(tag.getCompound("fluid")).fluidState
} else {
null
}
}
@ -37,9 +39,12 @@ class GrateBlockEntity: BlockEntity(EHBlockEntities.GRATE), BlockEntityClientSer
}
override fun fromClientTag(tag: CompoundTag) {
if (tag.contains("fluid", NbtType.COMPOUND)) {
fluidState = NbtHelper.toBlockState(tag.getCompound("fluid")).fluidState
fluidState = if (tag.contains("fluid", NbtType.COMPOUND)) {
NbtHelper.toBlockState(tag.getCompound("fluid")).fluidState
} else {
null
}
world?.updateListeners(pos, cachedState, cachedState, 3)
}
}

View File

@ -1,20 +1,124 @@
{
"ambientocclusion": false,
"textures": {
"face": "extrahoppers:block/grate/face",
"edge": "extrahoppers:block/grate/edge"
"face": "extrahoppers:block/grate",
"edge": "extrahoppers:block/grate_edge",
"internal": "extrahoppers:block/grate_internal",
"particle": "#face"
},
"elements": [
{
"from": [0, 15, 0],
"from": [0, 14, 0],
"to": [1, 16, 16],
"faces": {
"down": {"texture": "#face"},
"up": {"texture": "#face", "cullface": "up"},
"west": {"texture": "#edge", "cullface": "west"},
"east": {"texture": "#edge"},
"north": {"texture": "#edge", "cullface": "north"},
"south": {"texture": "#edge", "cullface": "south"}
}
},
{
"from": [15, 14, 0],
"to": [16, 16, 16],
"faces": {
"down": {"texture": "#face"},
"up": {"texture": "#face", "cullface": "up"},
"west": {"texture": "#edge"},
"east": {"texture": "#edge", "cullface": "east"},
"north": {"texture": "#edge", "cullface": "north"},
"south": {"texture": "#edge", "cullface": "south"},
"west": {"texture": "#edge", "cullface": "west"},
"east": {"texture": "#edge", "cullface": "east"}
"south": {"texture": "#edge", "cullface": "south"}
}
},
{
"from": [1, 14, 0],
"to": [15, 16, 1],
"faces": {
"down": {"texture": "#face", "uv": [1, 0, 15, 1]},
"up": {"texture": "#face", "cullface": "up", "uv": [1, 0, 15, 1]},
"north": {"texture": "#edge"},
"south": {"texture": "#edge", "cullface": "south"}
}
},
{
"from": [1, 14, 15],
"to": [15, 16, 16],
"faces": {
"down": {"texture": "#face", "uv": [1, 0, 15, 1]},
"up": {"texture": "#face", "cullface": "up", "uv": [1, 0, 15, 1]},
"north": {"texture": "#edge", "cullface": "north"},
"south": {"texture": "#edge"}
}
},
{
"from": [1, 14, 7],
"to": [2, 16, 9],
"faces": {
"down": {"texture": "#face", "uv": [1, 7, 2, 9]},
"up": {"texture": "#face", "cullface": "up", "uv": [1, 7, 2, 9]},
"north": {"texture": "#face", "uv": [1, 7, 2, 9]},
"south": {"texture": "#face", "uv": [1, 7, 2, 9]}
}
},
{
"from": [2, 14, 1],
"to": [4, 16, 15],
"faces": {
"down": {"texture": "#face", "uv": [2, 1, 4, 15]},
"up": {"texture": "#face", "cullface": "up", "uv": [2, 1, 4, 15]},
"west": {"texture": "#internal", "uv": [1, 2, 15, 4]},
"east": {"texture": "#internal", "uv": [1, 2, 15, 4]}
}
},
{
"from": [4, 14, 2],
"to": [7, 16, 4],
"faces": {
"down": {"texture": "#face", "uv": [4, 2, 7, 4]},
"up": {"texture": "#face", "cullface": "up", "uv": [4, 2, 7, 4]},
"north": {"texture": "#face", "uv": [4, 2, 7, 4]},
"south": {"texture": "#face", "uv": [4, 2, 7, 4]}
}
},
{
"from": [7, 14, 1],
"to": [9, 16, 15],
"faces": {
"down": {"texture": "#face", "uv": [7, 1, 9, 15]},
"up": {"texture": "#face", "cullface": "up", "uv": [7, 1, 9, 15]},
"west": {"texture": "#internal", "uv": [1, 7, 15, 9]},
"east": {"texture": "#internal", "uv": [1, 7, 15, 9]}
}
},
{
"from": [9, 14, 12],
"to": [12, 16, 14],
"faces": {
"down": {"texture": "#face", "uv": [9, 12, 12, 14]},
"up": {"texture": "#face", "cullface": "up", "uv": [9, 12, 12, 14]},
"north": {"texture": "#face", "uv": [9, 12, 12, 14]},
"south": {"texture": "#face", "uv": [9, 12, 12, 14]}
}
},
{
"from": [12, 14, 1],
"to": [14, 16, 15],
"faces": {
"down": {"texture": "#face", "uv": [12, 1, 14, 15]},
"up": {"texture": "#face", "cullface": "up", "uv": [12, 1, 14, 15]},
"west": {"texture": "#internal", "uv": [1, 12, 15, 14]},
"east": {"texture": "#internal", "uv": [1, 12, 15, 14]}
}
},
{
"from": [14, 14, 7],
"to": [15, 16, 9],
"faces": {
"down": {"texture": "#face", "uv": [14, 7, 15, 9]},
"up": {"texture": "#face", "cullface": "up", "uv": [14, 7, 15, 9]},
"north": {"texture": "#face", "uv": [14, 7, 15, 9]},
"south": {"texture": "#face", "uv": [14, 7, 15, 9]}
}
}
],

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

View File

@ -3,8 +3,9 @@
"package": "net.shadowfacts.extrahoppers.mixin",
"compatibilityLevel": "JAVA_8",
"mixins": [
"MixinWorldChunk",
"MixinBaseFluid"
"MixinBaseFluid",
"MixinBucketItem",
"MixinWorldChunk"
],
"client": [
],