Search in sources :

Example 91 with BlockSnapshot

use of org.spongepowered.api.block.BlockSnapshot in project SpongeCommon by SpongePowered.

the class InstrumentTestPlugin method onUseItem.

@Listener
public void onUseItem(InteractBlockEvent.Secondary.MainHand event, @First Player player) {
    if (!enabled || !player.get(Keys.IS_SNEAKING).get()) {
        return;
    }
    final BlockSnapshot snapshot = event.getTargetBlock();
    if (snapshot.getState().getType().equals(BlockTypes.NOTEBLOCK)) {
        final InstrumentProperty instrumentProperty = player.getWorld().getBlock(snapshot.getPosition().sub(0, 1, 0)).getProperty(InstrumentProperty.class).orElse(null);
        if (instrumentProperty != null) {
            final InstrumentType instrument = instrumentProperty.getValue();
            player.sendMessage(Text.of(TextColors.DARK_GREEN, "Clicked on a note block with instrument: ", TextColors.GREEN, instrument.getName()));
        } else {
            player.sendMessage(Text.of(TextColors.DARK_GREEN, "Clicked on a note block that strangely did not have any instrument type."));
        }
    } else {
        final InstrumentProperty instrumentProperty = snapshot.getProperty(InstrumentProperty.class).orElse(null);
        if (instrumentProperty != null) {
            final InstrumentType instrument = instrumentProperty.getValue();
            player.sendMessage(Text.of(TextColors.DARK_GREEN, "Clicked on a block with instrument type: ", TextColors.GREEN, instrument.getName()));
        } else {
            player.sendMessage(Text.of(TextColors.DARK_GREEN, "Clicked on a block which had no instrument type."));
        }
    }
}
Also used : BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) InstrumentType(org.spongepowered.api.data.type.InstrumentType) InstrumentProperty(org.spongepowered.api.data.property.block.InstrumentProperty) Listener(org.spongepowered.api.event.Listener)

Example 92 with BlockSnapshot

use of org.spongepowered.api.block.BlockSnapshot in project RedProtect by FabioZumbi12.

the class RPBlockListener method onBlockPlace.

@Listener(order = Order.FIRST, beforeModifications = true)
public void onBlockPlace(ChangeBlockEvent.Place e, @First Player p) {
    RedProtect.get().logger.debug("blocks", "BlockListener - Is BlockPlaceEvent event! Cancelled? " + e.isCancelled());
    BlockSnapshot b = e.getTransactions().get(0).getOriginal();
    Location<World> bloc = b.getLocation().get();
    World w = bloc.getExtent();
    ItemType m = ItemTypes.NONE;
    if (p.getItemInHand(HandTypes.MAIN_HAND).isPresent()) {
        m = p.getItemInHand(HandTypes.MAIN_HAND).get().getItem();
    } else if (p.getItemInHand(HandTypes.OFF_HAND).isPresent()) {
        m = p.getItemInHand(HandTypes.OFF_HAND).get().getItem();
    }
    Boolean antih = RedProtect.get().cfgs.getBool("region-settings.anti-hopper");
    Region r = RedProtect.get().rm.getTopRegion(b.getLocation().get());
    if (r == null && RedProtect.get().cfgs.getGlobalFlagList(w.getName(), "if-build-false", "place-blocks").contains(b.getState().getType().getName())) {
        return;
    }
    if (r != null) {
        if (!r.canMinecart(p) && m.getName().contains("minecart")) {
            RPLang.sendMessage(p, "blocklistener.region.cantplace");
            e.setCancelled(true);
            return;
        }
        if (b.getState().getType().equals(BlockTypes.MOB_SPAWNER) && r.allowSpawner(p)) {
            return;
        }
        try {
            if (!r.canBuild(p) && !r.canPlace(b)) {
                RPLang.sendMessage(p, "blocklistener.region.cantbuild");
                e.setCancelled(true);
            } else {
                if (!RedProtect.get().ph.hasPerm(p, "redprotect.bypass") && antih && (m.equals(ItemTypes.HOPPER) || m.getName().contains("rail"))) {
                    int x = bloc.getBlockX();
                    int y = bloc.getBlockY();
                    int z = bloc.getBlockZ();
                    BlockSnapshot ib = w.createSnapshot(x, y + 1, z);
                    if (!cont.canBreak(p, ib) || !cont.canBreak(p, b)) {
                        RPLang.sendMessage(p, "blocklistener.container.chestinside");
                        e.setCancelled(true);
                    }
                }
            }
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
}
Also used : ItemType(org.spongepowered.api.item.ItemType) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) Region(br.net.fabiozumbi12.RedProtect.Sponge.Region) World(org.spongepowered.api.world.World) Listener(org.spongepowered.api.event.Listener)

Example 93 with BlockSnapshot

use of org.spongepowered.api.block.BlockSnapshot in project RedProtect by FabioZumbi12.

the class RPBlockListener method onBlockGrow.

@Listener(order = Order.FIRST, beforeModifications = true)
public void onBlockGrow(ChangeBlockEvent.Grow e) {
    RedProtect.get().logger.debug("blocks", "RPBlockListener - Is ChangeBlockEvent.Grow event");
    BlockSnapshot b = e.getTransactions().get(0).getOriginal();
    Region r = RedProtect.get().rm.getTopRegion(b.getLocation().get());
    if (r != null && !r.canGrow()) {
        e.setCancelled(true);
        RedProtect.get().logger.debug("blocks", "Cancel grow " + b.getState().getName());
    }
}
Also used : BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) Region(br.net.fabiozumbi12.RedProtect.Sponge.Region) Listener(org.spongepowered.api.event.Listener)

Example 94 with BlockSnapshot

use of org.spongepowered.api.block.BlockSnapshot in project RedProtect by FabioZumbi12.

the class RPBlockListener method onSignPlace.

@Listener(order = Order.FIRST, beforeModifications = true)
public void onSignPlace(ChangeSignEvent e, @First Player p) {
    RedProtect.get().logger.debug("blocks", "BlockListener - Is SignChangeEvent event! Cancelled? " + e.isCancelled());
    Sign s = e.getTargetTile();
    List<Text> lines = e.getText().asList();
    Location<World> loc = s.getLocation();
    World w = p.getWorld();
    BlockSnapshot b = w.createSnapshot(loc.getBlockPosition());
    if (b == null) {
        this.setErrorSign(e, p, RPLang.get("blocklistener.block.null"));
        return;
    }
    Region signr = RedProtect.get().rm.getTopRegion(loc);
    if (signr != null && !signr.canSign(p)) {
        RPLang.sendMessage(p, "playerlistener.region.cantinteract");
        e.setCancelled(true);
        return;
    }
    Text line1 = lines.get(0);
    if (lines.size() != 4) {
        this.setErrorSign(e, p, RPLang.get("blocklistener.sign.wronglines"));
        return;
    }
    if (RedProtect.get().cfgs.getBool("server-protection.sign-spy.enabled")) {
        Sponge.getServer().getConsole().sendMessage(RPUtil.toText(RPLang.get("blocklistener.signspy.location").replace("{x}", "" + loc.getX()).replace("{y}", "" + loc.getY()).replace("{z}", "" + loc.getZ()).replace("{world}", w.getName())));
        Sponge.getServer().getConsole().sendMessage(RPUtil.toText(RPLang.get("blocklistener.signspy.player").replace("{player}", p.getName())));
        Sponge.getServer().getConsole().sendMessage(RPUtil.toText(RPLang.get("blocklistener.signspy.lines12").replace("{line1}", lines.get(0).toPlain()).replace("{line2}", lines.get(1).toPlain())));
        Sponge.getServer().getConsole().sendMessage(RPUtil.toText(RPLang.get("blocklistener.signspy.lines34").replace("{line3}", lines.get(2).toPlain()).replace("{line4}", lines.get(3).toPlain())));
        if (!RedProtect.get().cfgs.getBool("server-protection.sign-spy.only-console")) {
            for (Player play : Sponge.getServer().getOnlinePlayers()) {
                if (RedProtect.get().ph.hasGenPerm(play, "redprotect.signspy")) /* && !play.equals(p)*/
                {
                    play.sendMessage(RPUtil.toText(RPLang.get("blocklistener.signspy.location").replace("{x}", "" + loc.getX()).replace("{y}", "" + loc.getY()).replace("{z}", "" + loc.getZ()).replace("{world}", w.getName())));
                    play.sendMessage(RPUtil.toText(RPLang.get("blocklistener.signspy.player").replace("{player}", p.getName())));
                    play.sendMessage(RPUtil.toText(RPLang.get("blocklistener.signspy.lines12").replace("{line1}", lines.get(0).toPlain()).replace("{line2}", lines.get(1).toPlain())));
                    play.sendMessage(RPUtil.toText(RPLang.get("blocklistener.signspy.lines34").replace("{line3}", lines.get(2).toPlain()).replace("{line4}", lines.get(3).toPlain())));
                }
            }
        }
    }
    if ((RedProtect.get().cfgs.getBool("private.use") && s.getType().equals(TileEntityTypes.SIGN)) && (line1.toPlain().equalsIgnoreCase("private") || line1.toPlain().equalsIgnoreCase("[private]") || line1.toPlain().equalsIgnoreCase(RPLang.get("blocklistener.container.signline")) || line1.toPlain().equalsIgnoreCase("[" + RPLang.get("blocklistener.container.signline") + "]"))) {
        Region r = RedProtect.get().rm.getTopRegion(loc);
        Boolean out = RedProtect.get().cfgs.getBool("private.allow-outside");
        if (out || r != null) {
            if (cont.isContainer(b)) {
                int length = p.getName().length();
                if (length > 16) {
                    length = 16;
                }
                lines.set(1, RPUtil.toText(p.getName().substring(0, length)));
                e.getText().setElements(lines);
                RPLang.sendMessage(p, "blocklistener.container.protected");
                return;
            } else {
                RPLang.sendMessage(p, "blocklistener.container.notprotected");
                // w.digBlock(loc.getBlockPosition(), Cause.of(NamedCause.simulated(p)));
                return;
            }
        } else {
            RPLang.sendMessage(p, "blocklistener.container.notregion");
            // w.digBlock(loc.getBlockPosition(), Cause.of(NamedCause.simulated(p)));
            return;
        }
    }
    if (line1.toPlain().equalsIgnoreCase("[rp]")) {
        String claimmode = RedProtect.get().cfgs.getWorldClaimType(p.getWorld().getName());
        if ((!claimmode.equalsIgnoreCase("BLOCK") && !claimmode.equalsIgnoreCase("BOTH")) && !RedProtect.get().ph.hasPerm(p, "redprotect.admin.create")) {
            this.setErrorSign(e, p, RPLang.get("blocklistener.region.claimmode"));
            return;
        }
        RegionBuilder rb = new EncompassRegionBuilder(e);
        if (rb.ready()) {
            Region r = rb.build();
            lines.set(0, RPUtil.toText(RPLang.get("blocklistener.region.signcreated")));
            lines.set(1, RPUtil.toText(r.getName()));
            e.getText().setElements(lines);
            // RPLang.sendMessage(p, RPLang.get("blocklistener.region.created").replace("{region}",  r.getName()));
            RedProtect.get().rm.add(r, RedProtect.get().serv.getWorld(r.getWorld()).get());
        }
    }
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) EncompassRegionBuilder(br.net.fabiozumbi12.RedProtect.Sponge.actions.EncompassRegionBuilder) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) Text(org.spongepowered.api.text.Text) World(org.spongepowered.api.world.World) Region(br.net.fabiozumbi12.RedProtect.Sponge.Region) Sign(org.spongepowered.api.block.tileentity.Sign) RegionBuilder(br.net.fabiozumbi12.RedProtect.Sponge.RegionBuilder) EncompassRegionBuilder(br.net.fabiozumbi12.RedProtect.Sponge.actions.EncompassRegionBuilder) Listener(org.spongepowered.api.event.Listener)

Example 95 with BlockSnapshot

use of org.spongepowered.api.block.BlockSnapshot in project RedProtect by FabioZumbi12.

the class RPBlockListener method onDecay.

@Listener(order = Order.FIRST, beforeModifications = true)
public void onDecay(ChangeBlockEvent.Decay e, @First BlockSnapshot source) {
    BlockSnapshot bfrom = e.getTransactions().get(0).getOriginal();
    RedProtect.get().logger.debug("blocks", "Is BlockFromToEvent.Decay event is to " + source.getState().getType().getName() + " from " + bfrom.getState().getType().getName());
    Region r = RedProtect.get().rm.getTopRegion(bfrom.getLocation().get());
    if (r != null && !r.leavesDecay() && source.getState().getType().getName().contains("leaves")) {
        e.setCancelled(true);
    }
}
Also used : BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) Region(br.net.fabiozumbi12.RedProtect.Sponge.Region) Listener(org.spongepowered.api.event.Listener)

Aggregations

BlockSnapshot (org.spongepowered.api.block.BlockSnapshot)150 World (org.spongepowered.api.world.World)67 Listener (org.spongepowered.api.event.Listener)66 Location (org.spongepowered.api.world.Location)26 BlockState (org.spongepowered.api.block.BlockState)22 Entity (org.spongepowered.api.entity.Entity)21 BlockPos (net.minecraft.util.math.BlockPos)20 CauseStackManager (org.spongepowered.api.event.CauseStackManager)19 Region (br.net.fabiozumbi12.RedProtect.Sponge.Region)18 IBlockState (net.minecraft.block.state.IBlockState)18 Transaction (org.spongepowered.api.data.Transaction)18 BlockType (org.spongepowered.api.block.BlockType)15 Player (org.spongepowered.api.entity.living.player.Player)15 Direction (org.spongepowered.api.util.Direction)15 ChangeBlockEvent (org.spongepowered.api.event.block.ChangeBlockEvent)14 SpongeBlockSnapshot (org.spongepowered.common.block.SpongeBlockSnapshot)14 Vector3d (com.flowpowered.math.vector.Vector3d)13 ArrayList (java.util.ArrayList)13 User (org.spongepowered.api.entity.living.player.User)11 ItemStack (org.spongepowered.api.item.inventory.ItemStack)11