Skip to content

Commit

Permalink
Allow breaking a pylon if it has no owner
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremiahwinsley committed Dec 11, 2021
1 parent aff8af8 commit a3320a9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import net.permutated.pylons.util.TranslationKey;

import javax.annotation.Nullable;
import java.util.Objects;
import java.util.Optional;

public abstract class AbstractPylonBlock extends Block implements EntityBlock {
Expand Down Expand Up @@ -70,8 +69,8 @@ public VoxelShape getShape(BlockState blockState, BlockGetter reader, BlockPos p
* @param player the player
* @return the result
*/
public static boolean isPylonOwner(@Nullable BlockEntity blockEntity, Player player) {
return (blockEntity instanceof AbstractPylonTile tile && Objects.equals(tile.getOwner(), player.getUUID())) || player.hasPermissions(2);
public static boolean canAccessPylon(@Nullable BlockEntity blockEntity, Player player) {
return (blockEntity instanceof AbstractPylonTile tile && tile.canAccess(player));
}

@Nullable
Expand Down Expand Up @@ -106,7 +105,7 @@ public void destroy(LevelAccessor world, BlockPos blockPos, BlockState blockStat
public static void onBlockBreakEvent(BlockEvent.BreakEvent event) {
BlockEntity tileEntity = event.getWorld().getBlockEntity(event.getPos());

if (!isPylonOwner(tileEntity, event.getPlayer())) {
if (!canAccessPylon(tileEntity, event.getPlayer())) {
event.setCanceled(true);
}
}
Expand All @@ -115,7 +114,7 @@ public static void onBlockBreakEvent(BlockEvent.BreakEvent event) {
@SuppressWarnings("java:S1874") // deprecated method from super class
public float getDestroyProgress(BlockState state, Player player, BlockGetter getter, BlockPos blockPos) {
BlockEntity blockEntity = getter.getBlockEntity(blockPos);
if (isPylonOwner(blockEntity, player)) {
if (canAccessPylon(blockEntity, player)) {
int i = net.minecraftforge.common.ForgeHooks.isCorrectToolForDrops(state, player) ? 30 : 100;
return player.getDigSpeed(state, blockPos) / 2.0F / i;
}
Expand Down Expand Up @@ -160,7 +159,7 @@ public AbstractContainerMenu createMenu(int i, Inventory playerInventory, Player
}
};

if (isPylonOwner(tileEntity, player)) {
if (canAccessPylon(tileEntity, player)) {
NetworkHooks.openGui((ServerPlayer) player, containerProvider, pylonTile::updateContainer);
} else {
return InteractionResult.FAIL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.world.Containers;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
Expand All @@ -23,6 +24,7 @@

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Objects;
import java.util.UUID;

public abstract class AbstractPylonTile extends BlockEntity {
Expand Down Expand Up @@ -76,6 +78,10 @@ public void setOwner(UUID owner) {
this.setChanged();
}

public boolean canAccess(Player player) {
return Objects.equals(player.getUUID(), owner) || owner == null || player.hasPermissions(2);
}

private long lastTicked = 0L;

public boolean canTick(final int every) {
Expand Down Expand Up @@ -108,7 +114,7 @@ protected static void dropItems(@Nullable Level world, BlockPos pos, IItemHandle
}

public String getOwnerName() {
String lastKnown = UsernameCache.getLastKnownUsername(owner);
String lastKnown = owner == null ? null : UsernameCache.getLastKnownUsername(owner);
return StringUtils.defaultString(lastKnown, Constants.UNKNOWN);
}

Expand Down

0 comments on commit a3320a9

Please sign in to comment.