Skip to content

Commit

Permalink
Allow blocks to reset "opened" status by sneak-right-clicking.
Browse files Browse the repository at this point in the history
  • Loading branch information
noobanidus committed Sep 5, 2021
1 parent 6a6bba4 commit 582d90f
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/main/java/noobanidus/mods/lootr/api/ILootTile.java
Expand Up @@ -16,4 +16,5 @@ public interface ILootTile {
Set<UUID> getOpeners ();
UUID getTileId ();

void updatePacketViaState();
}
Expand Up @@ -37,7 +37,11 @@ public void onReplaced(BlockState oldState, World world, BlockPos pos, BlockStat

@Override
public ActionResultType onBlockActivated(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult trace) {
ChestUtil.handleLootChest(this, world, pos, player);
if (player.isSneaking()) {
ChestUtil.handleLootSneak(this, world, pos, player);
} else {
ChestUtil.handleLootChest(this, world, pos, player);
}
return ActionResultType.SUCCESS;
}

Expand Down
Expand Up @@ -44,7 +44,9 @@ public LootrChestBlock(Properties builder, Supplier<TileEntityType<? extends Che

@Override
public ActionResultType onBlockActivated(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult trace) {
if (!ChestBlock.isBlocked(world, pos)) {
if (player.isSneaking()) {
ChestUtil.handleLootSneak(this, world, pos, player);
} else if (!ChestBlock.isBlocked(world, pos)) {
ChestUtil.handleLootChest(this, world, pos, player);
}
return ActionResultType.SUCCESS;
Expand Down
Expand Up @@ -44,7 +44,9 @@ public LootrInventoryBlock(Properties builder, Supplier<TileEntityType<? extends

@Override
public ActionResultType onBlockActivated(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult trace) {
if (!ChestBlock.isBlocked(world, pos)) {
if (player.isSneaking()) {
ChestUtil.handleLootSneak(this, world, pos, player);
} else if (!ChestBlock.isBlocked(world, pos)) {
ChestUtil.handleLootInventory(this, world, pos, player);
}
return ActionResultType.SUCCESS;
Expand Down
Expand Up @@ -49,7 +49,9 @@ public void onReplaced(BlockState oldState, World world, BlockPos pos, BlockStat

@Override
public ActionResultType onBlockActivated(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult trace) {
if (!ChestBlock.isBlocked(world, pos)) {
if (player.isSneaking()) {
ChestUtil.handleLootSneak(this, world, pos, player);
} else if (!ChestBlock.isBlocked(world, pos)) {
ChestUtil.handleLootChest(this, world, pos, player);
}
return ActionResultType.SUCCESS;
Expand Down
Expand Up @@ -239,6 +239,7 @@ public void closeInventory(PlayerEntity player) {
}
}

@Override
public void updatePacketViaState() {
if (world != null && !world.isRemote) {
BlockState state = world.getBlockState(getPos());
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/noobanidus/mods/lootr/util/ChestUtil.java
Expand Up @@ -23,12 +23,32 @@
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.UUID;

@SuppressWarnings("unused")
public class ChestUtil {
public static Random random = new Random();
public static Set<Class<?>> tileClasses = new HashSet<>();

public static boolean handleLootSneak (Block block, World world, BlockPos pos, PlayerEntity player) {
if (world.isRemote()) {
return false;
}
if (player.isSpectator()) {
return false;
}

TileEntity te = world.getTileEntity(pos);
if (te instanceof ILootTile) {
Set<UUID> openers = ((ILootTile)te).getOpeners();
openers.remove(player.getUniqueID());
((ILootTile)te).updatePacketViaState();
return true;
}

return false;
}

public static boolean handleLootChest(Block block, World world, BlockPos pos, PlayerEntity player) {
if (world.isRemote()) {
return false;
Expand Down

0 comments on commit 582d90f

Please sign in to comment.