Skip to content

Commit

Permalink
Common Protection API implementations for Building Staffs
Browse files Browse the repository at this point in the history
Signed-off-by: Noaaan <[email protected]>
  • Loading branch information
Noaaan committed Jan 30, 2024
1 parent 5a8ef4b commit 1ab7bce
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import de.dafuqs.spectrum.helpers.*;
import de.dafuqs.spectrum.items.*;
import de.dafuqs.spectrum.registries.*;
import eu.pb4.common.protection.api.CommonProtection;
import net.minecraft.block.*;
import net.minecraft.entity.player.*;
import net.minecraft.item.*;
Expand Down Expand Up @@ -38,7 +39,7 @@ public boolean canInteractWith(BlockState state, BlockView world, BlockPos pos,
}

float hardness = state.getHardness(world, pos);
return hardness >= 0;
return hardness >= 0 && CommonProtection.canInteractBlock(player.getWorld(), pos, player.getGameProfile(), player);
}

/**
Expand All @@ -59,10 +60,15 @@ protected static Triplet<Block, Item, Integer> countSuitableReplacementItems(@No

return BuildingHelper.getBuildingItemCountInInventoryIncludingSimilars(player, targetBlock, blocksToPlace);
}


// TODO - Refactor note - Is PlacedBlocks planned on being used? At the same time, should this specific impl not be in ConstructorStaffItem?
protected static int placeBlocksAndDecrementInventory(PlayerEntity player, World world, Block blockToPlace, Item itemToConsume, Direction side, List<BlockPos> targetPositions, int inkCostPerBlock) {
int placedBlocks = 0;
for (BlockPos position : targetPositions) {
// Only place blocks where you are allowed to do so
if (!CommonProtection.canPlaceBlock(world, position, player.getGameProfile(), player))
continue;

BlockState originalState = world.getBlockState(position);
if (originalState.isAir() || originalState.getBlock() instanceof FluidBlock || (originalState.isReplaceable() && originalState.getCollisionShape(world, position).isEmpty())) {
BlockState stateToPlace = blockToPlace.getPlacementState(new BuildingStaffPlacementContext(world, player, new BlockHitResult(Vec3d.ofBottomCenter(position), side, position, false)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public ActionResult useOnBlock(ItemUsageContext context) {
BlockPos pos = context.getBlockPos();
BlockState targetBlockState = world.getBlockState(pos);

if ((player != null && canInteractWith(targetBlockState, context.getWorld(), context.getBlockPos(), context.getPlayer()))) {
if ((player != null && this.canInteractWith(targetBlockState, context.getWorld(), context.getBlockPos(), context.getPlayer()))) {
Block blockToPlace = targetBlockState.getBlock();
Item itemToConsume;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import de.dafuqs.spectrum.particle.*;
import de.dafuqs.spectrum.recipe.pedestal.*;
import de.dafuqs.spectrum.registries.*;
import eu.pb4.common.protection.api.CommonProtection;
import net.fabricmc.api.*;
import net.minecraft.block.*;
import net.minecraft.client.*;
Expand Down Expand Up @@ -109,6 +110,11 @@ public static boolean exchange(World world, BlockPos pos, @NotNull PlayerEntity
List<ItemStack> stacks = new ArrayList<>();
BlockState stateToPlace;
for (BlockPos targetPosition : targetPositions) {

// Require both place and break permissions in order to swap blocks
if (!CommonProtection.canPlaceBlock(world, targetPosition, player.getGameProfile(), player) || !CommonProtection.canBreakBlock(world, targetPosition, player.getGameProfile(), player))
continue;

if (!player.isCreative()) {
BlockState droppedStacks = world.getBlockState(targetPosition);
stacks.addAll(Block.getDroppedStacks(droppedStacks, (ServerWorld) world, targetPosition, world.getBlockEntity(targetPosition), player, exchangeStaffItemStack));
Expand Down

0 comments on commit 1ab7bce

Please sign in to comment.