Skip to content

Commit

Permalink
fix: filter drops
Browse files Browse the repository at this point in the history
Closes #202
  • Loading branch information
klikli-dev committed Jul 16, 2024
1 parent 63dedb4 commit 9f7daba
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package com.klikli_dev.theurgy.content.apparatus.logisticsitemconnector;

import com.klikli_dev.theurgy.content.apparatus.DirectionalBlockShape;
import com.klikli_dev.theurgy.content.behaviour.filter.HasFilterBehaviour;
import com.klikli_dev.theurgy.content.behaviour.logistics.HasWireEndPoint;
import com.klikli_dev.theurgy.logistics.Wires;
import com.klikli_dev.theurgy.network.Networking;
Expand Down Expand Up @@ -47,13 +48,13 @@ public LogisticsItemConnectorBlock(Properties properties) {
}

@Override
protected @NotNull ItemInteractionResult useItemOn(@NotNull ItemStack pStack, @NotNull BlockState pState, @NotNull Level pLevel, @NotNull BlockPos pPos, Player pPlayer, @NotNull InteractionHand pHand, @NotNull BlockHitResult pHitResult) {
if(!(pLevel.getBlockEntity(pPos) instanceof LogisticsItemConnectorBlockEntity blockEntity)){
protected @NotNull ItemInteractionResult useItemOn(@NotNull ItemStack pStack, @NotNull BlockState pState, @NotNull Level pLevel, @NotNull BlockPos pPos, @NotNull Player pPlayer, @NotNull InteractionHand pHand, @NotNull BlockHitResult pHitResult) {
if (!(pLevel.getBlockEntity(pPos) instanceof LogisticsItemConnectorBlockEntity blockEntity)) {
return ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION;
}

var result = blockEntity.filter().useItemOn(pStack, pState, pLevel, pPos, pPlayer, pHand, pHitResult);
if(result != ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION)
if (result != ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION)
return result;

if (!pPlayer.getItemInHand(pHand).isEmpty())
Expand All @@ -67,16 +68,21 @@ public LogisticsItemConnectorBlock(Properties properties) {
return ItemInteractionResult.SUCCESS;
}

@SuppressWarnings("deprecation")
@Override
public VoxelShape getShape(BlockState pState, BlockGetter pLevel, BlockPos pPos, CollisionContext pContext) {
public @NotNull VoxelShape getShape(BlockState pState, @NotNull BlockGetter pLevel, @NotNull BlockPos pPos, @NotNull CollisionContext pContext) {
return SHAPE.getShape(pState.getValue(FACING));
}

@Override
public void onRemove(BlockState pState, Level pLevel, BlockPos pPos, BlockState pNewState, boolean pMovedByPiston) {
super.onRemove(pState, pLevel, pPos, pNewState, pMovedByPiston);
public void onRemove(BlockState pState, @NotNull Level pLevel, @NotNull BlockPos pPos, BlockState pNewState, boolean pMovedByPiston) {
//Drop filter
if (!pState.is(pNewState.getBlock())) {
if (pLevel.getBlockEntity(pPos) instanceof HasFilterBehaviour hasFilterBehaviour) {
hasFilterBehaviour.filter().onRemove(pState, pLevel, pPos, pNewState, pMovedByPiston);
}
}

//drop wires
if (pState.hasBlockEntity() && (!pState.is(pNewState.getBlock()) || !pNewState.hasBlockEntity())) {
var removedWires = Wires.get(pLevel).removeWiresFor(pPos);
if (pLevel.isClientSide)
Expand All @@ -88,6 +94,8 @@ public void onRemove(BlockState pState, Level pLevel, BlockPos pPos, BlockState
blockEntity.leafNode().onDestroyed();
}
}

super.onRemove(pState, pLevel, pPos, pNewState, pMovedByPiston);
}

@Override
Expand All @@ -104,7 +112,6 @@ public BlockState getStateForPlacement(BlockPlaceContext pContext) {
: this.defaultBlockState().setValue(FACING, direction);
}

@SuppressWarnings("deprecation")
@Override
public boolean canSurvive(BlockState pState, LevelReader pLevel, BlockPos pPos) {
//The item connector can be connected to any block that is not air.
Expand All @@ -114,7 +121,6 @@ public boolean canSurvive(BlockState pState, LevelReader pLevel, BlockPos pPos)
return !pLevel.getBlockState(blockpos).isAir();
}

@SuppressWarnings("deprecation")
@Override
public @NotNull BlockState updateShape(BlockState pState, Direction pFacing, @NotNull BlockState pFacingState, @NotNull LevelAccessor pLevel, @NotNull BlockPos pCurrentPos, @NotNull BlockPos pFacingPos) {
return pFacing.getOpposite() == pState.getValue(FACING) && !pState.canSurvive(pLevel, pCurrentPos) ? Blocks.AIR.defaultBlockState() : pState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,16 @@
package com.klikli_dev.theurgy.content.apparatus.logisticsitemconnector.extractor;

import com.klikli_dev.theurgy.content.apparatus.logisticsitemconnector.LogisticsItemConnectorBlock;
import com.klikli_dev.theurgy.content.render.outliner.Outliner;
import com.klikli_dev.theurgy.registry.BlockEntityRegistry;
import com.mojang.serialization.MapCodec;
import net.minecraft.core.BlockPos;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.DirectionalBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.Shapes;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class LogisticsItemExtractorBlock extends LogisticsItemConnectorBlock {
Expand All @@ -31,19 +26,19 @@ public LogisticsItemExtractorBlock(Properties properties) {
}

@Override
protected MapCodec<? extends DirectionalBlock> codec() {
protected @NotNull MapCodec<? extends DirectionalBlock> codec() {
return CODEC;
}

@Nullable
@Override
public BlockEntity newBlockEntity(BlockPos pPos, BlockState pState) {
public BlockEntity newBlockEntity(@NotNull BlockPos pPos, @NotNull BlockState pState) {
return BlockEntityRegistry.LOGISTICS_ITEM_EXTRACTOR.get().create(pPos, pState);
}

@Nullable
@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level pLevel, BlockState pState, BlockEntityType<T> pBlockEntityType) {
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level pLevel, @NotNull BlockState pState, @NotNull BlockEntityType<T> pBlockEntityType) {
if (pLevel.isClientSide()) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.minecraft.core.BlockPos;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.Containers;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.ItemInteractionResult;
import net.minecraft.world.entity.player.Player;
Expand Down Expand Up @@ -40,8 +41,14 @@ public Filter filter() {
}

public void filter(Filter filter) {
this.filter(filter, true);
}

public void filter(Filter filter, boolean notify) {
this.filter = filter;
this.callback.accept(filter);
if (notify) {
this.callback.accept(filter);
}
}

public FilterBehaviour withCallback(Consumer<Filter> callback) {
Expand Down Expand Up @@ -94,4 +101,14 @@ public void readNetwork(CompoundTag pTag, HolderLookup.Provider pRegistries) {

return ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION;
}

public void onRemove(BlockState pState, Level pLevel, BlockPos pPos, BlockState pNewState, boolean pIsMoving) {
if (!this.filter.isEmpty()) {
var stack = this.filter().item().copy();

this.filter(Filter.empty(), false);

Containers.dropItemStack(pLevel, pPos.getX(), pPos.getY(), pPos.getZ(), stack);
}
}
}

0 comments on commit 9f7daba

Please sign in to comment.