Search in sources :

Example 71 with BlockSnapshot

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

the class WildernessWorldWrapper method onEntitySpawn.

@Listener
public void onEntitySpawn(SpawnEntityEvent event) {
    List<Entity> entities = event.getEntities();
    Optional<BlockSnapshot> optBlockCause = event.getCause().first(BlockSnapshot.class);
    for (Entity entity : entities) {
        Location<World> loc = entity.getLocation();
        Optional<Integer> optLevel = getLevel(loc);
        if (!optLevel.isPresent()) {
            continue;
        }
        int level = optLevel.get();
        if (entity instanceof Egg && optBlockCause.isPresent()) {
            PrimedTNT explosive = (PrimedTNT) entity.getLocation().getExtent().createEntity(EntityTypes.PRIMED_TNT, entity.getLocation().getPosition());
            explosive.setVelocity(entity.getVelocity());
            explosive.offer(Keys.FUSE_DURATION, 20 * 4);
            // TODO used to have a 1/4 chance of creating fire
            entity.getLocation().getExtent().spawnEntity(explosive);
            event.setCancelled(true);
            return;
        }
        if (level > 1) {
            // TODO move damage modification
            if (entity instanceof Monster) {
                HealthData healthData = ((Monster) entity).getHealthData();
                double curMax = healthData.maxHealth().get();
                if (curMax <= 80) {
                    // TODO do this a better way, but for now it prevents super mobs
                    double newMax = curMax * getHealthMod(level);
                    healthData.set(Keys.MAX_HEALTH, newMax);
                    healthData.set(Keys.HEALTH, newMax);
                    entity.offer(healthData);
                }
                // Wandering Bosses
                Collection<String> wanderers = wanderingMobManager.getSupportedWanderersOfType(entity.getType());
                for (String wanderer : wanderers) {
                    if (wanderingMobManager.chanceBind(wanderer, level, entity)) {
                        break;
                    }
                }
            }
        }
        Optional<Value<Integer>> optExplosiveRadius = Optional.empty();
        if (optExplosiveRadius.isPresent()) {
            Value<Integer> explosiveRadius = optExplosiveRadius.get();
            int min = explosiveRadius.get();
            entity.offer(Keys.EXPLOSION_RADIUS, Optional.of(MathExt.bound((min + level) / 2, min, entity instanceof Fireball ? 4 : 9)));
        }
    }
}
Also used : PrimedTNT(org.spongepowered.api.entity.explosive.PrimedTNT) HealthData(org.spongepowered.api.data.manipulator.mutable.entity.HealthData) Egg(org.spongepowered.api.entity.projectile.Egg) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) Fireball(org.spongepowered.api.entity.projectile.explosive.fireball.Fireball) Monster(org.spongepowered.api.entity.living.monster.Monster) Value(org.spongepowered.api.data.value.mutable.Value) ContextValue(org.spongepowered.api.event.filter.cause.ContextValue) Listener(org.spongepowered.api.event.Listener)

Example 72 with BlockSnapshot

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

the class CustomTerragu method process.

public void process(ChangeBlockEvent.Break event) {
    if (event.getTransactions().size() > 1) {
        return;
    }
    Optional<Player> optPlayer = event.getCause().first(Player.class);
    if (optPlayer.isPresent()) {
        Player player = optPlayer.get();
        Optional<Direction> optClickedDir = Optional.ofNullable(clickMap.get(player));
        Optional<org.spongepowered.api.item.inventory.ItemStack> optStack = player.getItemInHand(HandTypes.MAIN_HAND);
        if (optStack.isPresent() && optClickedDir.isPresent()) {
            if (optStack.get().getItem() == this) {
                ItemStack stack = tf(optStack.get());
                for (Transaction<BlockSnapshot> snapshot : event.getTransactions()) {
                    if (!snapshot.getOriginal().getLocation().isPresent()) {
                        return;
                    }
                    int maxDist = getMaxEditDist(stack);
                    int dmg = destroyLine(player, optClickedDir.get(), maxDist - 1, snapshot.getOriginal());
                    stack.damageItem(dmg, tf(player));
                    player.setItemInHand(HandTypes.MAIN_HAND, tf(stack));
                }
            }
        }
    }
}
Also used : EntityPlayer(net.minecraft.entity.player.EntityPlayer) Player(org.spongepowered.api.entity.living.player.Player) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) ItemStack(net.minecraft.item.ItemStack) Direction(org.spongepowered.api.util.Direction)

Example 73 with BlockSnapshot

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

the class RegionMarker method onBlockBreak.

@Listener
public void onBlockBreak(ChangeBlockEvent.Break event, @First Player player) {
    Optional<RegionService> optService = Sponge.getServiceManager().provide(RegionService.class);
    if (!optService.isPresent()) {
        return;
    }
    RegionService service = optService.get();
    for (Transaction<BlockSnapshot> block : event.getTransactions()) {
        if (!block.isValid()) {
            continue;
        }
        if (block.getOriginal().getState().getType() != this) {
            continue;
        }
        Optional<Location<World>> optLoc = block.getOriginal().getLocation();
        if (optLoc.isPresent()) {
            Optional<Region> optRef = service.getMarkedRegion(optLoc.get());
            if (optRef.isPresent()) {
                Region ref = optRef.get();
                if (ref.isMember(player)) {
                    ref.remPoint(new RegionPoint(optLoc.get().getPosition()));
                    player.sendMessage(Text.of(TextColors.YELLOW, "Region marker deleted!"));
                }
            }
        }
    }
}
Also used : BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) Region(com.skelril.skree.service.internal.region.Region) RegionService(com.skelril.skree.service.RegionService) Location(org.spongepowered.api.world.Location) RegionPoint(com.skelril.skree.service.internal.region.RegionPoint) Listener(org.spongepowered.api.event.Listener)

Example 74 with BlockSnapshot

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

the class RegionMaster method onBlockBreak.

@Listener
public void onBlockBreak(ChangeBlockEvent.Break event, @First Player player) {
    Optional<RegionService> optService = Sponge.getServiceManager().provide(RegionService.class);
    if (!optService.isPresent()) {
        return;
    }
    RegionService service = optService.get();
    for (Transaction<BlockSnapshot> block : event.getTransactions()) {
        if (!block.isValid()) {
            continue;
        }
        if (block.getOriginal().getState().getType() != this) {
            continue;
        }
        Optional<Location<World>> optLoc = block.getOriginal().getLocation();
        if (!optLoc.isPresent()) {
            continue;
        }
        Optional<Region> optRef = service.getMarkedRegion(optLoc.get());
        if (!optRef.isPresent()) {
            continue;
        }
        Region ref = optRef.get();
        if (!ref.getFullPoints().isEmpty()) {
            block.setValid(false);
            player.sendMessage(Text.of(TextColors.RED, "You must first delete all markers!"));
        } else {
            service.rem(optLoc.get());
            player.sendMessage(Text.of(TextColors.YELLOW, "Region deleted!"));
        }
    }
}
Also used : BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) Region(com.skelril.skree.service.internal.region.Region) RegionService(com.skelril.skree.service.RegionService) Location(org.spongepowered.api.world.Location) Listener(org.spongepowered.api.event.Listener)

Example 75 with BlockSnapshot

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

the class TrackingUtil method updateTickBlock.

public static void updateTickBlock(IMixinWorldServer mixinWorld, Block block, BlockPos pos, IBlockState state, Random random) {
    final WorldServer minecraftWorld = mixinWorld.asMinecraftWorld();
    try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
        Sponge.getCauseStackManager().pushCause(minecraftWorld);
        if (ShouldFire.TICK_BLOCK_EVENT) {
            BlockSnapshot snapshot = mixinWorld.createSpongeBlockSnapshot(state, state, pos, BlockChangeFlags.NONE);
            final TickBlockEvent event = SpongeEventFactory.createTickBlockEventScheduled(Sponge.getCauseStackManager().getCurrentCause(), snapshot);
            SpongeImpl.postEvent(event);
            if (event.isCancelled()) {
                return;
            }
        }
        final LocatableBlock locatable = LocatableBlock.builder().location(new Location<>(mixinWorld.asSpongeWorld(), pos.getX(), pos.getY(), pos.getZ())).state((BlockState) state).build();
        Sponge.getCauseStackManager().pushCause(locatable);
        IPhaseState<BlockTickContext> phase = ((IMixinBlock) block).requiresBlockCapture() ? TickPhase.Tick.BLOCK : TickPhase.Tick.NO_CAPTURE_BLOCK;
        final BlockTickContext phaseContext = phase.createPhaseContext().source(locatable);
        checkAndAssignBlockTickConfig(block, minecraftWorld, phaseContext);
        final PhaseTracker phaseTracker = PhaseTracker.getInstance();
        // We have to associate any notifiers in case of scheduled block updates from other sources
        final PhaseData current = phaseTracker.getCurrentPhaseData();
        final IPhaseState<?> currentState = current.state;
        ((IPhaseState) currentState).appendNotifierPreBlockTick(mixinWorld, pos, current.context, phaseContext);
        try (PhaseContext<?> context = phaseContext.buildAndSwitch()) {
            block.updateTick(minecraftWorld, pos, state, random);
        } catch (Exception | NoClassDefFoundError e) {
            phaseTracker.printExceptionFromPhase(e, phaseContext);
        }
    }
}
Also used : StackFrame(org.spongepowered.api.event.CauseStackManager.StackFrame) SpongeBlockSnapshot(org.spongepowered.common.block.SpongeBlockSnapshot) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) WorldServer(net.minecraft.world.WorldServer) TickBlockEvent(org.spongepowered.api.event.block.TickBlockEvent) BlockState(org.spongepowered.api.block.BlockState) IBlockState(net.minecraft.block.state.IBlockState) BlockTickContext(org.spongepowered.common.event.tracking.phase.tick.BlockTickContext) CauseStackManager(org.spongepowered.api.event.CauseStackManager) LocatableBlock(org.spongepowered.api.world.LocatableBlock)

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