Search in sources :

Example 6 with BlockType

use of org.spongepowered.api.block.BlockType in project Skree by Skelril.

the class WildernessWorldWrapper method onBlockPlace.

@Listener
public void onBlockPlace(ChangeBlockEvent.Place event, @Named(NamedCause.SOURCE) Player player) {
    for (Transaction<BlockSnapshot> block : event.getTransactions()) {
        Optional<Location<World>> optLoc = block.getFinal().getLocation();
        if (!optLoc.isPresent() || !isApplicable(optLoc.get())) {
            continue;
        }
        Location<World> loc = optLoc.get();
        BlockState finalState = block.getFinal().getState();
        if (config.getDropAmplificationConfig().amplifies(finalState)) {
            // Allow creative mode players to still place blocks
            if (player.getGameModeData().type().get().equals(GameModes.CREATIVE)) {
                continue;
            }
            BlockType originalType = block.getOriginal().getState().getType();
            if (ore().contains(originalType)) {
                continue;
            }
            try {
                Vector3d origin = loc.getPosition();
                World world = loc.getExtent();
                for (int i = 0; i < 40; ++i) {
                    ParticleEffect effect = ParticleEffect.builder().type(ParticleTypes.MAGIC_CRITICAL_HIT).velocity(new Vector3d(Probability.getRangedRandom(-1, 1), Probability.getRangedRandom(-.7, .7), Probability.getRangedRandom(-1, 1))).quantity(1).build();
                    world.spawnParticles(effect, origin.add(.5, .5, .5));
                }
            } catch (Exception ex) {
                player.sendMessage(/* ChatTypes.SYSTEM, */
                Text.of(TextColors.RED, "You find yourself unable to place that block."));
            }
            block.setValid(false);
        }
    }
}
Also used : ParticleEffect(org.spongepowered.api.effect.particle.ParticleEffect) BlockState(org.spongepowered.api.block.BlockState) BlockType(org.spongepowered.api.block.BlockType) Vector3d(com.flowpowered.math.vector.Vector3d) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) World(org.spongepowered.api.world.World) Location(org.spongepowered.api.world.Location) Listener(org.spongepowered.api.event.Listener)

Example 7 with BlockType

use of org.spongepowered.api.block.BlockType in project HuskyCrates-Sponge by codeHusky.

the class HuskyCrates method placeBlock.

@Listener
public void placeBlock(ChangeBlockEvent event) {
    if (event.getCause().root() instanceof Player) {
        Player plr = (Player) event.getCause().root();
        if (event instanceof ChangeBlockEvent.Place || event instanceof ChangeBlockEvent.Break) {
            BlockType t = event.getTransactions().get(0).getOriginal().getLocation().get().getBlock().getType();
            Location<World> location = event.getTransactions().get(0).getOriginal().getLocation().get();
            location.getBlock().toContainer().set(DataQuery.of("rock"), 1);
            //location.getBlock().with()
            if (validCrateBlocks.contains(t)) {
                //crateUtilities.recognizeChest(event.getTransactions().get(0).getOriginal().getLocation().get());
                if (event instanceof ChangeBlockEvent.Place) {
                    if (plr.getItemInHand(HandTypes.MAIN_HAND).isPresent()) {
                        Optional<Object> tt = plr.getItemInHand(HandTypes.MAIN_HAND).get().toContainer().get(DataQuery.of("UnsafeData", "crateID"));
                        if (tt.isPresent()) {
                            String crateID = tt.get().toString();
                            if (!plr.hasPermission("huskycrates.tester")) {
                                event.setCancelled(true);
                                return;
                            }
                            if (!crateUtilities.physicalCrates.containsKey(location))
                                crateUtilities.physicalCrates.put(location, new PhysicalCrate(location, crateID, this));
                            crateUtilities.physicalCrates.get(location).createHologram();
                            updatePhysicalCrates();
                        }
                    }
                }
            } else {
                if (crateUtilities.physicalCrates.containsKey(location)) {
                    if (!plr.hasPermission("huskycrates.tester")) {
                        event.setCancelled(true);
                        return;
                    }
                    crateUtilities.flag = true;
                    crateUtilities.physicalCrates.get(location).as.remove();
                    crateUtilities.physicalCrates.remove(location);
                    updatePhysicalCrates();
                }
            }
        }
    }
}
Also used : ChangeBlockEvent(org.spongepowered.api.event.block.ChangeBlockEvent) Player(org.spongepowered.api.entity.living.player.Player) BlockType(org.spongepowered.api.block.BlockType) JSONObject(org.json.JSONObject) PhysicalCrate(pw.codehusky.huskycrates.crate.PhysicalCrate) World(org.spongepowered.api.world.World) Listener(org.spongepowered.api.event.Listener)

Example 8 with BlockType

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

the class SpongeParticleHelper method getBlockState.

@SuppressWarnings("deprecation")
private static int getBlockState(SpongeParticleEffect effect, Optional<BlockState> defaultBlockState) {
    Optional<BlockState> blockState = effect.getOption(ParticleOptions.BLOCK_STATE);
    if (blockState.isPresent()) {
        return Block.getStateId((IBlockState) blockState.get());
    }
    Optional<ItemStackSnapshot> optSnapshot = effect.getOption(ParticleOptions.ITEM_STACK_SNAPSHOT);
    if (optSnapshot.isPresent()) {
        ItemStackSnapshot snapshot = optSnapshot.get();
        Optional<BlockType> blockType = snapshot.getType().getBlock();
        if (blockType.isPresent()) {
            return Block.getStateId(((Block) blockType.get()).getStateFromMeta(((SpongeItemStackSnapshot) snapshot).getDamageValue()));
        }
        return 0;
    }
    return Block.getStateId((IBlockState) defaultBlockState.get());
}
Also used : SpongeItemStackSnapshot(org.spongepowered.common.item.inventory.SpongeItemStackSnapshot) BlockState(org.spongepowered.api.block.BlockState) IBlockState(net.minecraft.block.state.IBlockState) BlockType(org.spongepowered.api.block.BlockType) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) SpongeItemStackSnapshot(org.spongepowered.common.item.inventory.SpongeItemStackSnapshot)

Example 9 with BlockType

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

the class BreakablePlaceableUtils method get.

public static Optional<Set<BlockType>> get(ItemStack stack, String nbtKey) {
    NBTTagCompound tag = stack.getTagCompound();
    if (tag == null) {
        return Optional.empty();
    }
    NBTTagList blockIds = tag.getTagList(nbtKey, NbtDataUtil.TAG_STRING);
    if (blockIds.hasNoTags()) {
        return Optional.empty();
    }
    Set<BlockType> blockTypes = Sets.newHashSetWithExpectedSize(blockIds.tagCount());
    for (int i = 0; i < blockIds.tagCount(); i++) {
        Optional<BlockType> blockType = SpongeImpl.getGame().getRegistry().getType(BlockType.class, blockIds.getStringTagAt(i));
        if (blockType.isPresent()) {
            blockTypes.add(blockType.get());
        }
    }
    return Optional.of(blockTypes);
}
Also used : NBTTagList(net.minecraft.nbt.NBTTagList) BlockType(org.spongepowered.api.block.BlockType) NBTTagCompound(net.minecraft.nbt.NBTTagCompound)

Example 10 with BlockType

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

the class MixinChunk_Collisions method allowEntityCollision.

private <T extends Entity> boolean allowEntityCollision(List<T> listToFill) {
    if (this.world instanceof IMixinWorldServer) {
        IMixinWorldServer spongeWorld = (IMixinWorldServer) this.world;
        if (spongeWorld.isProcessingExplosion()) {
            // allow explosions
            return true;
        }
        final PhaseContext<?> phaseContext = PhaseTracker.getInstance().getCurrentContext();
        LocatableBlock locatable = phaseContext.getSource(LocatableBlock.class).orElse(null);
        if (locatable != null) {
            BlockType blockType = locatable.getLocation().getBlockType();
            IModData_Collisions spongeBlock = (IModData_Collisions) blockType;
            if (spongeBlock.requiresCollisionsCacheRefresh()) {
                spongeBlock.initializeCollisionState(this.world);
                spongeBlock.requiresCollisionsCacheRefresh(false);
            }
            return !((spongeBlock.getMaxCollisions() >= 0) && (listToFill.size() >= spongeBlock.getMaxCollisions()));
        }
        IModData_Collisions spongeEntity = phaseContext.getSource(IModData_Collisions.class).orElse(null);
        if (spongeEntity != null) {
            if (spongeEntity.requiresCollisionsCacheRefresh()) {
                spongeEntity.initializeCollisionState(this.world);
                spongeEntity.requiresCollisionsCacheRefresh(false);
            }
            return !((spongeEntity.getMaxCollisions() >= 0) && (listToFill.size() >= spongeEntity.getMaxCollisions()));
        }
        return true;
    }
    return true;
}
Also used : IModData_Collisions(org.spongepowered.common.mixin.plugin.entitycollisions.interfaces.IModData_Collisions) BlockType(org.spongepowered.api.block.BlockType) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) LocatableBlock(org.spongepowered.api.world.LocatableBlock)

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