123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- package me.km.blockprotections;
- import me.km.KajetansMod;
- import me.km.module.Module;
- import me.km.utils.Location;
- import me.km.utils.Utils;
- import net.minecraft.block.Block;
- import net.minecraft.block.BlockButton;
- import net.minecraft.block.BlockContainer;
- import net.minecraft.block.BlockDoor;
- import net.minecraft.block.BlockPistonMoving;
- import net.minecraft.block.BlockTrapDoor;
- import net.minecraft.block.state.IBlockState;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.init.Blocks;
- import net.minecraft.util.EnumHand;
- import net.minecraft.util.math.BlockPos;
- import net.minecraft.util.text.TextFormatting;
- import net.minecraft.world.World;
- import net.minecraftforge.event.entity.player.PlayerInteractEvent;
- import net.minecraftforge.event.world.BlockEvent;
- import net.minecraftforge.fml.common.eventhandler.EventPriority;
- import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
- public class BlockProtection extends Module
- {
- private final static String BLOCK_BYPASS = "block.bypass";
-
- private final BlockProtectionBank bank;
-
- public BlockProtection()
- {
- super("Blocks", TextFormatting.BLUE);
- bank = new BlockProtectionBank();
- registerEvent(this);
- }
-
- public BlockProtectionBank getDatabank()
- {
- return bank;
- }
-
- private boolean shouldBeProtected(Block b)
- {
- return (b instanceof BlockDoor ||
- b instanceof BlockContainer ||
- b == Blocks.LEVER ||
- b instanceof BlockButton ||
- b instanceof BlockTrapDoor) && !(b instanceof BlockPistonMoving);
- }
-
- private BlockPos getSameNearbyBlock(World w, BlockPos pos, Block b)
- {
- Location l = new Location(w, pos);
- if(l.getRelativeBlockState(1, 0, 0).getBlock() == b)
- {
- return pos.add(1, 0, 0);
- }
- else if(l.getRelativeBlockState(-1, 0, 0).getBlock() == b)
- {
- return pos.add(-1, 0, 0);
- }
- else if(l.getRelativeBlockState(0, 0, 1).getBlock() == b)
- {
- return pos.add(0, 0, 1);
- }
- else if(l.getRelativeBlockState(0, 0, -1).getBlock() == b)
- {
- return pos.add(0, 0, -1);
- }
- return null;
- }
-
- @SubscribeEvent(priority = EventPriority.HIGHEST)
- public void onBlockPlace(BlockEvent.PlaceEvent e)
- {
- Block b = e.getPlacedBlock().getBlock();
- EntityPlayer p = e.getPlayer();
- World w = e.getWorld();
-
- if(b == Blocks.CHEST || b == Blocks.TRAPPED_CHEST) // Deny placing chests near other protected chests
- {
- BlockPos otherChest = getSameNearbyBlock(w, e.getPos(), b);
- if(otherChest != null && !bank.hasAccess(otherChest, w, p) && !KajetansMod.perms.hasPermission(p, BLOCK_BYPASS))
- {
- e.setCanceled(true);
- }
- }
- else if(b == Blocks.HOPPER) // Deny placing of hoppers under blocks
- {
- BlockPos pos = e.getPos().add(0, 1, 0);
- if(w.getBlockState(pos).getBlock() instanceof BlockContainer)
- {
- if(!bank.hasAccess(pos, w, p) && !KajetansMod.perms.hasPermission(p, BLOCK_BYPASS))
- {
- e.setCanceled(true);
- }
- }
- }
- }
-
- @SubscribeEvent(priority = EventPriority.HIGHEST)
- public void onBlockBreak(BlockEvent.BreakEvent e)
- {
- EntityPlayer p = e.getPlayer();
- IBlockState state = e.getState();
- if(shouldBeProtected(state.getBlock()))
- {
- BlockPos pos = e.getPos();
- if(Utils.getStateValue(state, BlockDoor.HALF) == BlockDoor.EnumDoorHalf.UPPER)
- {
- pos = pos.add(0, -1, 0);
- }
- World w = e.getWorld();
- if(bank.hasAccess(pos, w, p) || KajetansMod.perms.hasPermission(p, BLOCK_BYPASS))
- {
- bank.remove(pos, w);
- return;
- }
- e.setCanceled(true);
- }
- }
-
- @SubscribeEvent(priority = EventPriority.HIGHEST)
- public void onContainerOpen(PlayerInteractEvent.RightClickBlock e)
- {
- if(e.getHand() != EnumHand.MAIN_HAND)
- {
- return;
- }
- IBlockState state = e.getWorld().getBlockState(e.getPos());
- if(!shouldBeProtected(state.getBlock()))
- {
- return;
- }
- EntityPlayer p = e.getEntityPlayer();
- BlockPos pos = e.getPos();
- if(Utils.getStateValue(state, BlockDoor.HALF) == BlockDoor.EnumDoorHalf.UPPER)
- {
- pos = pos.add(0, -1, 0);
- }
- if(!bank.hasAccess(pos, e.getWorld(), p) && !KajetansMod.perms.hasPermission(p, BLOCK_BYPASS))
- {
- e.setCanceled(true);
- }
- }
- }
|