package me.km.blockprotections; import me.km.KajetansMod; import me.km.api.Utils; import me.km.api.Module; import me.km.api.ModuleListener; import me.km.permissions.Permissions; import net.minecraft.block.Block; import net.minecraft.block.BlockContainer; import net.minecraft.block.BlockDoor; 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.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 ModuleListener { private final BlockProtectionBank bank; public BlockProtection(Module m) { super(m); bank = KajetansMod.blocks.getDataBank(BlockProtectionBank.class); } @SubscribeEvent(priority = EventPriority.HIGHEST) public void onBlockPlace(BlockEvent.PlaceEvent e) { if(!KajetansMod.worldManager.getWorldPreferences(e.getWorld()).blockProtection) { return; } Block b = e.getPlacedBlock().getBlock(); if(!Utils.shouldBeProtected(b)) { return; } EntityPlayer p = e.getPlayer(); World w = e.getWorld(); // Deny placing chests near other protected chests if(b == Blocks.CHEST || b == Blocks.TRAPPED_CHEST) { BlockPos otherChest = Utils.getSameNearbyBlock(w, e.getPos(), b); if(otherChest != null && !bank.hasAccess(otherChest, w, p.getGameProfile(), true) && !KajetansMod.perms.has(p, Permissions.BLOCK_BYPASS)) { this.getModule().send(p, "Du darfst diesen Block hier nicht setzen"); e.setCanceled(true); return; } } // Deny placing of hoppers under blocks if(b == Blocks.HOPPER) { BlockPos pos = e.getPos().add(0, 1, 0); if(w.getBlockState(pos).getBlock() instanceof BlockContainer) { if(!bank.hasAccess(pos, w, p, true) && !KajetansMod.perms.has(p, Permissions.BLOCK_BYPASS)) { this.getModule().send(p, "Du darfst diesen Block hier nicht setzen"); e.setCanceled(true); } } } } @SubscribeEvent(priority = EventPriority.HIGHEST) public void onBlockBreak(BlockEvent.BreakEvent e) { if(!KajetansMod.worldManager.getWorldPreferences(e.getWorld()).blockProtection) { return; } EntityPlayer p = e.getPlayer(); IBlockState state = e.getState(); if(Utils.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.doesExist(pos, w)) { return; } if(bank.hasAccess(pos, w, p, false) || KajetansMod.perms.has(p, Permissions.BLOCK_BYPASS)) { bank.removeBlock(pos, w, p); return; } e.setCanceled(true); this.getModule().send(p, "Du hast keinen Zugriff auf diesen Block."); } } @SubscribeEvent(priority = EventPriority.HIGHEST) public void onContainerOpen(PlayerInteractEvent.RightClickBlock e) { if(e.getHand() != EnumHand.MAIN_HAND || !KajetansMod.worldManager.getWorldPreferences(e.getWorld()).blockProtection) { return; } IBlockState state = e.getWorld().getBlockState(e.getPos()); if(!Utils.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, true) || KajetansMod.perms.has(p, Permissions.BLOCK_BYPASS)) { return; } this.getModule().send(p, "Du hast keinen Zugriff auf diesen Block."); e.setCanceled(true); } }