Search in sources :

Example 41 with BlockType

use of org.spongepowered.api.block.BlockType in project modules-extra by CubeEngine.

the class ItemDuctListener method isDuctInteraction.

private static boolean isDuctInteraction(InteractBlockEvent event) {
    if (!(event instanceof InteractBlockEvent.Primary.MainHand) && !(event instanceof InteractBlockEvent.Secondary.MainHand)) {
        return false;
    }
    if (!event.getTargetBlock().getLocation().isPresent()) {
        return false;
    }
    Location<World> loc = event.getTargetBlock().getLocation().get();
    BlockType type = loc.getBlockType();
    if (!isEndPointType(type)) {
        return false;
    }
    Direction dir = loc.get(Keys.DIRECTION).orElse(Direction.NONE);
    Optional<TileEntity> te = loc.getRelative(dir).getTileEntity();
    return te.isPresent() && te.get() instanceof Carrier;
}
Also used : TileEntity(org.spongepowered.api.block.tileentity.TileEntity) BlockType(org.spongepowered.api.block.BlockType) Carrier(org.spongepowered.api.item.inventory.Carrier) World(org.spongepowered.api.world.World) Direction(org.spongepowered.api.util.Direction)

Example 42 with BlockType

use of org.spongepowered.api.block.BlockType in project modules-extra by CubeEngine.

the class ItemDuctListener method onBreak.

@Listener
public void onBreak(ChangeBlockEvent.Break event) {
    for (Transaction<BlockSnapshot> trans : event.getTransactions()) {
        if (!trans.getOriginal().getLocation().isPresent()) {
            continue;
        }
        BlockType type = trans.getOriginal().getState().getType();
        Location<World> loc = trans.getOriginal().getLocation().get();
        if (isEndPointType(type)) {
            Direction dir = trans.getOriginal().get(Keys.DIRECTION).orElse(Direction.NONE);
            Optional<DuctData> data = loc.getRelative(dir).get(DuctData.class);
            if (data.isPresent()) {
                data.get().remove(dir.getOpposite());
                if (data.get().getFilters().isEmpty()) {
                    loc.getRelative(dir).remove(DuctData.class);
                } else {
                    loc.getRelative(dir).offer(data.get());
                }
            }
        }
    }
}
Also used : BlockType(org.spongepowered.api.block.BlockType) IDuctData(org.cubeengine.module.itemduct.data.IDuctData) DuctData(org.cubeengine.module.itemduct.data.DuctData) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) World(org.spongepowered.api.world.World) Direction(org.spongepowered.api.util.Direction) Listener(org.spongepowered.api.event.Listener)

Example 43 with BlockType

use of org.spongepowered.api.block.BlockType in project modules-extra by CubeEngine.

the class ItemDuctTransferListener method activate.

private void activate() {
    List<Network> networks = new ArrayList<>();
    for (Iterator<Location<World>> it = this.promptedActivations.keySet().iterator(); it.hasNext(); ) {
        Location<World> loc = it.next();
        if (this.promptedActivations.get(loc) - 1000 > System.currentTimeMillis()) {
            promptedActivations.clear();
            continue;
        }
        it.remove();
        // Check if data is still present
        Optional<DuctData> data = loc.get(DuctData.class);
        if (data.isPresent()) {
            for (Direction dir : Direction.values()) {
                if (dir.isCardinal() || dir.isUpright()) {
                    BlockType type = loc.getRelative(dir).getBlockType();
                    if (STICKY_PISTON.equals(type) || OBSERVER.equals(type)) {
                        Optional<List<ItemStack>> filters = data.get().get(dir);
                        if (filters.isPresent()) {
                            Network network = manager.findNetwork(loc.getRelative(dir));
                            TileEntity te = loc.getTileEntity().get();
                            Inventory inventory = ((Carrier) te).getInventory();
                            if (te instanceof Chest) {
                                inventory = ((Chest) te).getDoubleChestInventory().orElse(inventory);
                            }
                            network.activate(inventory, filters.get());
                            networks.add(network);
                        }
                    }
                }
            }
        }
    }
    for (Network network : networks) {
        for (Location<World> exitLoc : network.exitPoints.keySet()) {
            Direction exitDir = exitLoc.get(Keys.DIRECTION).orElse(Direction.NONE).getOpposite();
            exitLoc = exitLoc.getRelative(exitDir.getOpposite());
            promptActivation(exitLoc.getTileEntity().filter(t -> t instanceof Carrier).map(Carrier.class::cast).orElse(null), true, null);
        }
    }
    if (promptedActivations.isEmpty()) {
        task.cancel();
    }
}
Also used : Inventory(org.spongepowered.api.item.inventory.Inventory) Chest(org.spongepowered.api.block.tileentity.carrier.Chest) Getter(org.spongepowered.api.event.filter.Getter) Keys(org.spongepowered.api.data.key.Keys) HashMap(java.util.HashMap) Itemduct(org.cubeengine.module.itemduct.Itemduct) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Network(org.cubeengine.module.itemduct.Network) OBSERVER(org.spongepowered.api.block.BlockTypes.OBSERVER) ItemStack(org.spongepowered.api.item.inventory.ItemStack) TileEntity(org.spongepowered.api.block.tileentity.TileEntity) BlockCarrier(org.spongepowered.api.item.inventory.BlockCarrier) Map(java.util.Map) Task(org.spongepowered.api.scheduler.Task) InteractBlockEvent(org.spongepowered.api.event.block.InteractBlockEvent) PluginContainer(org.spongepowered.api.plugin.PluginContainer) ItemDuctManager(org.cubeengine.module.itemduct.ItemDuctManager) Location(org.spongepowered.api.world.Location) Iterator(java.util.Iterator) STICKY_PISTON(org.spongepowered.api.block.BlockTypes.STICKY_PISTON) Sponge(org.spongepowered.api.Sponge) InteractInventoryEvent(org.spongepowered.api.event.item.inventory.InteractInventoryEvent) Carrier(org.spongepowered.api.item.inventory.Carrier) Root(org.spongepowered.api.event.filter.cause.Root) Direction(org.spongepowered.api.util.Direction) List(java.util.List) Container(org.spongepowered.api.item.inventory.Container) MultiBlockCarrier(org.spongepowered.api.item.inventory.MultiBlockCarrier) ChangeInventoryEvent(org.spongepowered.api.event.item.inventory.ChangeInventoryEvent) CarriedInventory(org.spongepowered.api.item.inventory.type.CarriedInventory) World(org.spongepowered.api.world.World) BlockType(org.spongepowered.api.block.BlockType) HandTypes(org.spongepowered.api.data.type.HandTypes) Optional(java.util.Optional) Player(org.spongepowered.api.entity.living.player.Player) Listener(org.spongepowered.api.event.Listener) DuctData(org.cubeengine.module.itemduct.data.DuctData) Chest(org.spongepowered.api.block.tileentity.carrier.Chest) ArrayList(java.util.ArrayList) World(org.spongepowered.api.world.World) Direction(org.spongepowered.api.util.Direction) TileEntity(org.spongepowered.api.block.tileentity.TileEntity) BlockType(org.spongepowered.api.block.BlockType) Network(org.cubeengine.module.itemduct.Network) DuctData(org.cubeengine.module.itemduct.data.DuctData) ArrayList(java.util.ArrayList) List(java.util.List) BlockCarrier(org.spongepowered.api.item.inventory.BlockCarrier) Carrier(org.spongepowered.api.item.inventory.Carrier) MultiBlockCarrier(org.spongepowered.api.item.inventory.MultiBlockCarrier) Inventory(org.spongepowered.api.item.inventory.Inventory) CarriedInventory(org.spongepowered.api.item.inventory.type.CarriedInventory) Location(org.spongepowered.api.world.Location)

Example 44 with BlockType

use of org.spongepowered.api.block.BlockType in project modules-extra by CubeEngine.

the class MarketSignListener method onBlockBreak.

@Listener
public void onBlockBreak(ChangeBlockEvent.Break event, @First Player player) {
    for (Transaction<BlockSnapshot> transaction : event.getTransactions()) {
        BlockSnapshot orig = transaction.getOriginal();
        BlockType type = orig.getState().getType();
        if (type == STANDING_SIGN || type == WALL_SIGN) {
            Optional<ImmutableMarketSignData> signData = orig.get(ImmutableMarketSignData.class);
            if (signData.isPresent()) {
                if (signData.get().getSignType() != null) {
                    event.setCancelled(true);
                    return;
                }
            }
        }
        Location<World> origLoc = orig.getLocation().get();
        for (Direction blockFace : BlockUtil.BLOCK_FACES) {
            if (blockFace == DOWN) {
                continue;
            }
            Location<World> relative = origLoc.getRelative(blockFace);
            if (!relative.get(MarketSignData.class).isPresent()) {
                continue;
            }
            if (blockFace == UP) {
                if (relative.getBlockType() == STANDING_SIGN) {
                    event.setCancelled(true);
                    return;
                }
            } else {
                if (relative.getBlockType() == WALL_SIGN && relative.get(Keys.DIRECTION).get() == blockFace) {
                    event.setCancelled(true);
                    return;
                }
            }
        }
    }
}
Also used : ImmutableMarketSignData(org.cubeengine.module.signmarket.data.ImmutableMarketSignData) BlockType(org.spongepowered.api.block.BlockType) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) World(org.spongepowered.api.world.World) Direction(org.spongepowered.api.util.Direction) Listener(org.spongepowered.api.event.Listener)

Example 45 with BlockType

use of org.spongepowered.api.block.BlockType in project modules-extra by CubeEngine.

the class MarketSignListener method onSignPlace.

@Listener(order = Order.POST)
public void onSignPlace(ChangeBlockEvent.Place event, @First Player player) {
    if (event.getTransactions().size() > 1 || !module.getEditModeCommand().hasUser(player)) {
        return;
    }
    for (Transaction<BlockSnapshot> trans : event.getTransactions()) {
        BlockType type = trans.getFinal().getState().getType();
        if (// placed sign
        type == BlockTypes.STANDING_SIGN || type == BlockTypes.WALL_SIGN) {
            if (!player.hasPermission(module.perms().EDIT_USE.getId())) {
                event.setCancelled(true);
                i18n.send(player, NEGATIVE, "You are not allowed to create market signs!");
                return;
            }
            MarketSignData data = new MarketSignData();
            data.setID(UUID.randomUUID());
            data.setOwner(player.getUniqueId());
            Location<World> loc = trans.getFinal().getLocation().get();
            loc.offer(data);
            manager.setSign(loc, player);
            Sponge.getCauseStackManager().pushCause(player);
            player.closeInventory();
        }
    }
}
Also used : BlockType(org.spongepowered.api.block.BlockType) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) MarketSignData(org.cubeengine.module.signmarket.data.MarketSignData) ImmutableMarketSignData(org.cubeengine.module.signmarket.data.ImmutableMarketSignData) World(org.spongepowered.api.world.World) Listener(org.spongepowered.api.event.Listener)

Aggregations

BlockType (org.spongepowered.api.block.BlockType)91 World (org.spongepowered.api.world.World)34 BlockState (org.spongepowered.api.block.BlockState)23 Listener (org.spongepowered.api.event.Listener)21 Location (org.spongepowered.api.world.Location)17 BlockSnapshot (org.spongepowered.api.block.BlockSnapshot)16 ItemStack (org.spongepowered.api.item.inventory.ItemStack)14 Vector3i (com.flowpowered.math.vector.Vector3i)12 Optional (java.util.Optional)12 Player (org.spongepowered.api.entity.living.player.Player)12 List (java.util.List)10 Set (java.util.Set)10 Direction (org.spongepowered.api.util.Direction)10 Vector3d (com.flowpowered.math.vector.Vector3d)9 ArrayList (java.util.ArrayList)9 Keys (org.spongepowered.api.data.key.Keys)9 Sponge (org.spongepowered.api.Sponge)8 LanternBlockType (org.lanternpowered.server.block.LanternBlockType)7 Text (org.spongepowered.api.text.Text)7 HashMap (java.util.HashMap)6