Java Code Examples for org.spongepowered.api.event.block.ChangeBlockEvent#Break

The following examples show how to use org.spongepowered.api.event.block.ChangeBlockEvent#Break . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BlacklistListener.java    From EssentialCmds with MIT License 6 votes vote down vote up
@Listener
public void onBreakBlock(ChangeBlockEvent.Break event, @Root Player player)
{
	if (!player.hasPermission("essentialcmds.blacklist.bypass"))
	{
		for (Transaction<BlockSnapshot> transaction : event.getTransactions())
		{
			if (Utils.getBlacklistItems().contains(transaction.getOriginal().getState().getType().getId()))
			{
				if (Utils.areBlacklistMsgsEnabled())
					player.sendMessage(Text.of(TextColors.RED, "The item ", TextColors.GRAY, transaction.getFinal().getState().getType().getId(), TextColors.RED, " has been confiscated as it is blacklisted."));

				event.setCancelled(true);
			}
		}
	}
}
 
Example 2
Source File: GlobalListener.java    From RedProtect with GNU General Public License v3.0 6 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onBlockBreakGeneric(ChangeBlockEvent.Break e) {
    RedProtect.get().logger.debug(LogLevel.BLOCKS, "GlobalListener - Is onBlockBreakGeneric event");

    LocatableBlock locatable = e.getCause().first(LocatableBlock.class).orElse(null);
    if (locatable != null) {
        BlockState sourceState = locatable.getBlockState();

        //liquid check
        MatterProperty mat = sourceState.getProperty(MatterProperty.class).orElse(null);
        if (mat != null && mat.getValue() == MatterProperty.Matter.LIQUID) {
            boolean allowdamage = RedProtect.get().config.globalFlagsRoot().worlds.get(locatable.getLocation().getExtent().getName()).allow_changes_of.flow_damage;

            Region r = RedProtect.get().rm.getTopRegion(locatable.getLocation(), this.getClass().getName());
            if (r == null && !allowdamage && locatable.getLocation().getBlockType() != BlockTypes.AIR) {
                e.setCancelled(true);
            }
        }
    }
}
 
Example 3
Source File: GlobalListener.java    From RedProtect with GNU General Public License v3.0 6 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onBlockBreakGlobal(ChangeBlockEvent.Break e, @Root Player p) {
    RedProtect.get().logger.debug(LogLevel.DEFAULT, "GlobalListener - Is BlockBreakEvent event! Cancelled? " + false);

    BlockSnapshot bt = e.getTransactions().get(0).getOriginal();
    Region r = RedProtect.get().rm.getTopRegion(bt.getLocation().get(), this.getClass().getName());
    if (r != null) {
        return;
    }

    if (!RedProtect.get().getUtil().canBuildNear(p, bt.getLocation().get())) {
        e.setCancelled(true);
        return;
    }

    if (!bypassBuild(p, bt, 2)) {
        e.setCancelled(true);
        RedProtect.get().logger.debug(LogLevel.DEFAULT, "GlobalListener - Can't Break!");
    }
}
 
Example 4
Source File: GlobalListener.java    From RedProtect with GNU General Public License v3.0 6 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onFireSpread(ChangeBlockEvent.Break e, @First LocatableBlock locatable) {
    RedProtect.get().logger.debug(LogLevel.BLOCKS, "GlobalListener - Is onBlockBreakGeneric event");

    BlockState sourceState = locatable.getBlockState();

    if (sourceState.getType() == BlockTypes.FIRE) {
        BlockSnapshot b = e.getTransactions().get(0).getOriginal();
        boolean fireDamage = RedProtect.get().config.globalFlagsRoot().worlds.get(locatable.getLocation().getExtent().getName()).fire_block_damage;
        if (!fireDamage && b.getState().getType() != BlockTypes.FIRE) {
            Region r = RedProtect.get().rm.getTopRegion(b.getLocation().get(), this.getClass().getName());
            if (r == null) {
                RedProtect.get().logger.debug(LogLevel.BLOCKS, "Tryed to break from FIRE!");
                e.setCancelled(true);
            }
        }
    }
}
 
Example 5
Source File: SignListener.java    From UltimateCore with MIT License 6 votes vote down vote up
@Listener
public void onSignDestroy(ChangeBlockEvent.Break event, @Root Player p) {
    for (Transaction<BlockSnapshot> transaction : event.getTransactions()) {
        BlockSnapshot snapshot = transaction.getOriginal();
        if (snapshot.supports(Keys.SIGN_LINES) && snapshot.getLocation().isPresent()) {
            List<Text> texts = snapshot.get(Keys.SIGN_LINES).get();
            //Checking for sign contents
            for (UCSign usign : UltimateCore.get().getSignService().get().getRegisteredSigns()) {
                if (texts.get(0).toPlain().equalsIgnoreCase("[" + usign.getIdentifier() + "]")) {
                    if (!p.hasPermission(usign.getDestroyPermission().get())) {
                        Messages.send(p, "core.nopermissions");
                    }
                    SignDestroyEvent cevent = new SignDestroyEvent(usign, snapshot.getLocation().get(), Cause.builder().append(UltimateCore.getContainer()).append(p).build(EventContext.builder().build()));
                    Sponge.getEventManager().post(cevent);
                    if (!cevent.isCancelled() && usign.onDestroy(p, event, texts)) {
                        Messages.send(p, "sign.destroy", "%sign%", usign.getIdentifier());
                    }
                }
            }
        }
    }
}
 
Example 6
Source File: BlockprotectionListener.java    From UltimateCore with MIT License 6 votes vote down vote up
@Listener
public void onBreak(ChangeBlockEvent.Break event) {
    ModuleConfig config = Modules.BLOCKPROTECTION.get().getConfig().get();
    if (!config.get().getNode("protections", "allow-interact-primary").getBoolean()) return;
    Player p = event.getCause().first(Player.class).orElse(null);

    boolean modified = false;
    for (Protection prot : GlobalData.get(BlockprotectionKeys.PROTECTIONS).get()) {
        //Ignore protection if the player is allowed to modify it
        if (p != null && prot.getPlayers().contains(p.getUniqueId())) continue;
        //For each location of the protection,
        for (Transaction trans : event.getTransactions().stream().filter(trans -> trans.getFinal().getLocation().isPresent() && prot.getLocations().contains(trans.getFinal().getLocation().get())).collect(Collectors.toList())) {
            modified = true;
            trans.setValid(false);
        }

        //If anything has been cancelled & caused by player, send message
        if (p != null && modified) {
            p.sendMessage(prot.getLocktype().getErrorMessage(p, prot));
        }
    }
}
 
Example 7
Source File: BuildPermListener.java    From Nations with MIT License 5 votes vote down vote up
@Listener(order=Order.FIRST, beforeModifications = true)
public void onPlayerBreaksBlock(ChangeBlockEvent.Break event)
{
	User user = Utils.getUser(event);

	if (user != null && user.hasPermission("nations.admin.bypass.perm.build"))
	{
		return;
	}
	event
	.getTransactions()
	.stream()
	.forEach(trans -> trans.getOriginal().getLocation().ifPresent(loc -> {
		if (!ConfigHandler.isWhitelisted("break", trans.getFinal().getState().getType().getId())
				&& ConfigHandler.getNode("worlds").getNode(trans.getFinal().getLocation().get().getExtent().getName()).getNode("enabled").getBoolean())
		{
			if (user == null || !DataHandler.getPerm("build", user.getUniqueId(), loc))
			{
				trans.setValid(false);
				if (user != null && user instanceof Player) {
					try {
						((Player) user).sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_PERM_BUILD));
					} catch (Exception e) {}
				}
			}
		}
	}));
}
 
Example 8
Source File: EntityListener.java    From RedProtect with GNU General Public License v3.0 5 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void WitherBlockBreak(ChangeBlockEvent.Break event, @First Entity e) {
    if (e instanceof Monster) {
        BlockSnapshot b = event.getTransactions().get(0).getOriginal();
        RedProtect.get().logger.debug(LogLevel.ENTITY, "EntityListener - Is EntityChangeBlockEvent event! Block " + b.getState().getType().getName());
        Region r = RedProtect.get().rm.getTopRegion(b.getLocation().get(), this.getClass().getName());
        if (!cont.canWorldBreak(b)) {
            event.setCancelled(true);
            return;
        }
        if (r != null && !r.canMobLoot()) {
            event.setCancelled(true);
        }
    }
}
 
Example 9
Source File: BlockListener.java    From RedProtect with GNU General Public License v3.0 5 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onBlockBreak(ChangeBlockEvent.Break e, @First Player p) {
    RedProtect.get().logger.debug(LogLevel.BLOCKS, "BlockListener - Is ChangeBlockEvent.Break event!");

    BlockSnapshot b = e.getTransactions().get(0).getOriginal();
    Location<World> bloc = b.getLocation().get();

    boolean antih = RedProtect.get().config.configRoot().region_settings.anti_hopper;
    Region r = RedProtect.get().rm.getTopRegion(bloc, this.getClass().getName());

    if (!RedProtect.get().ph.hasPerm(p, "redprotect.bypass")) {
        BlockSnapshot ib = bloc.getBlockRelative(Direction.UP).createSnapshot();
        if ((antih && !cont.canBreak(p, ib)) || !cont.canBreak(p, b)) {
            RedProtect.get().lang.sendMessage(p, "blocklistener.container.breakinside");
            e.setCancelled(true);
            return;
        }
    }

    if (r == null && canBreakList(p.getWorld(), b.getState().getType().getName())) {
        return;
    }

    if (r != null && b.getState().getType().equals(BlockTypes.MOB_SPAWNER) && r.allowSpawner(p)) {
        return;
    }

    if (r != null && r.canBuild(p) && b.getState().getType().getName().equalsIgnoreCase("sign")) {
        Sign s = (Sign) b.getLocation().get().getTileEntity().get();
        if (s.lines().get(0).toPlain().equalsIgnoreCase("[flag]")) {
            RedProtect.get().config.removeSign(r.getID(), b.getLocation().get());
            return;
        }
    }

    if (r != null && !r.canBuild(p) && !r.canTree(b) && !r.canMining(b) && !r.canCrops(b) && !r.canBreak(b)) {
        RedProtect.get().lang.sendMessage(p, "blocklistener.region.cantbuild");
        e.setCancelled(true);
    }
}
 
Example 10
Source File: BlockListener.java    From RedProtect with GNU General Public License v3.0 5 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onFireSpread(ChangeBlockEvent.Break e, @First LocatableBlock locatable) {
    RedProtect.get().logger.debug(LogLevel.BLOCKS, "BlockListener - Is onBlockBreakGeneric event");

    BlockState sourceState = locatable.getBlockState();

    if (sourceState.getType() == BlockTypes.FIRE) {
        BlockSnapshot b = e.getTransactions().get(0).getOriginal();
        Region r = RedProtect.get().rm.getTopRegion(b.getLocation().get(), this.getClass().getName());
        if (r != null && !r.canFire() && b.getState().getType() != BlockTypes.FIRE) {
            RedProtect.get().logger.debug(LogLevel.BLOCKS, "Tryed to break from FIRE!");
            e.setCancelled(true);
        }
    }
}
 
Example 11
Source File: BlockListener.java    From UltimateCore with MIT License 5 votes vote down vote up
@Listener
public void onBreak(ChangeBlockEvent.Break event) {
    ModuleConfig config = Modules.BLACKLIST.get().getConfig().get();
    CommentedConfigurationNode hnode = config.get();
    for (Transaction<BlockSnapshot> trans : event.getTransactions()) {
        if (!trans.isValid()) continue;
        CommentedConfigurationNode node = hnode.getNode("blocks", trans.getOriginal().getState().getType().getId());
        if (!node.isVirtual()) {
            if (node.getNode("deny-break").getBoolean()) {
                trans.setValid(false);
            }
        }
    }
}
 
Example 12
Source File: ChangeBlockListener.java    From Prism with MIT License 4 votes vote down vote up
/**
 * Listens to the base change block event.
 *
 * @param event ChangeBlockEvent
 */
@Listener(order = Order.POST)
public void onChangeBlock(ChangeBlockEvent event) {

    if (event.getCause().allOf(PluginContainer.class).stream().map(PluginContainer::getId).anyMatch(id ->
            Prism.getInstance().getConfig().getGeneralCategory().getBlacklist().contains(id))) {
        // Don't do anything
        return;
    }

    if (event.getCause().first(Player.class).map(Player::getUniqueId).map(Prism.getInstance().getActiveWands()::contains).orElse(false)) {
        // Cancel and exit event here, not supposed to place/track a block with an active wand.
        event.setCancelled(true);
        return;
    }

    if (event.getTransactions().isEmpty()
            || (!Prism.getInstance().getConfig().getEventCategory().isBlockBreak()
            && !Prism.getInstance().getConfig().getEventCategory().isBlockDecay()
            && !Prism.getInstance().getConfig().getEventCategory().isBlockGrow()
            && !Prism.getInstance().getConfig().getEventCategory().isBlockPlace())) {
        return;
    }

    for (Transaction<BlockSnapshot> transaction : event.getTransactions()) {
        if (!transaction.isValid() || !transaction.getOriginal().getLocation().isPresent()) {
            continue;
        }

        BlockType originalBlockType = transaction.getOriginal().getState().getType();
        BlockType finalBlockType = transaction.getFinal().getState().getType();

        PrismRecord.EventBuilder eventBuilder = PrismRecord.create()
                .source(event.getCause())
                .blockOriginal(transaction.getOriginal())
                .blockReplacement(transaction.getFinal())
                .location(transaction.getOriginal().getLocation().get());

        if (event instanceof ChangeBlockEvent.Break) {
            if (!Prism.getInstance().getConfig().getEventCategory().isBlockBreak()
                    || BlockUtil.rejectBreakCombination(originalBlockType, finalBlockType)
                    || EventUtil.rejectBreakEventIdentity(originalBlockType, finalBlockType, event.getCause())) {
                continue;
            }

            eventBuilder
                    .event(PrismEvents.BLOCK_BREAK)
                    .target(originalBlockType.getId().replace("_", " "))
                    .buildAndSave();
        } else if (event instanceof ChangeBlockEvent.Decay) {
            if (!Prism.getInstance().getConfig().getEventCategory().isBlockDecay()) {
                continue;
            }

            eventBuilder
                    .event(PrismEvents.BLOCK_DECAY)
                    .target(originalBlockType.getId().replace("_", " "))
                    .buildAndSave();
        } else if (event instanceof ChangeBlockEvent.Grow) {
            if (!Prism.getInstance().getConfig().getEventCategory().isBlockGrow()) {
                continue;
            }

            eventBuilder
                    .event(PrismEvents.BLOCK_GROW)
                    .target(finalBlockType.getId().replace("_", " "))
                    .buildAndSave();
        } else if (event instanceof ChangeBlockEvent.Place) {
            if (!Prism.getInstance().getConfig().getEventCategory().isBlockPlace()
                    || BlockUtil.rejectPlaceCombination(originalBlockType, finalBlockType)
                    || EventUtil.rejectPlaceEventIdentity(originalBlockType, finalBlockType, event.getCause())) {
                continue;
            }

            eventBuilder
                    .event(PrismEvents.BLOCK_PLACE)
                    .target(finalBlockType.getId().replace("_", " "))
                    .buildAndSave();
        }
    }
}