Search in sources :

Example 6 with UndoList

use of com.elmakers.mine.bukkit.api.block.UndoList in project MagicPlugin by elBukkit.

the class EntityController method onItemSpawn.

@EventHandler(priority = EventPriority.LOWEST)
public void onItemSpawn(ItemSpawnEvent event) {
    if (disableItemSpawn || com.elmakers.mine.bukkit.block.BlockData.undoing) {
        event.setCancelled(true);
        return;
    }
    Item itemEntity = event.getEntity();
    ItemStack spawnedItem = itemEntity.getItemStack();
    Block block = itemEntity.getLocation().getBlock();
    BlockData undoData = com.elmakers.mine.bukkit.block.UndoList.getBlockData(block.getLocation());
    boolean isBreaking = block.getType() != Material.AIR;
    if (!isBreaking) {
        MaterialSet doubleAttachables = controller.getMaterialSetManager().getMaterialSetEmpty("attachable_double");
        isBreaking = doubleAttachables.testItem(spawnedItem);
    }
    if (undoData != null && isBreaking) {
        // So we can catch this as a one-time event, for blocks we have recorded.
        if (undoData.getMaterial() != Material.AIR) {
            UndoList undoList = undoData.getUndoList();
            if (undoList != null) {
                undoList.add(block);
            } else {
                controller.getLogger().warning("Block broken into item under undo at " + block + ", but no undo list was assigned");
            }
            event.setCancelled(true);
            return;
        }
        // If this was a block we built magically, don't drop items if the item being dropped
        // matches the block type. This is messy, but avoid players losing all their items
        // when suffocating inside a Blob
        Collection<ItemStack> drops = block.getDrops();
        if (drops != null) {
            for (ItemStack drop : drops) {
                if (drop.getType() == spawnedItem.getType()) {
                    com.elmakers.mine.bukkit.block.UndoList.commit(undoData);
                    event.setCancelled(true);
                    return;
                }
            }
        }
    }
    if (Wand.isSkill(spawnedItem)) {
        event.setCancelled(true);
        return;
    }
    if (Wand.isWand(spawnedItem)) {
        boolean invulnerable = controller.getWandProperty(spawnedItem, "invulnerable", false);
        if (invulnerable) {
            CompatibilityUtils.setInvulnerable(event.getEntity());
        }
        boolean trackWand = controller.getWandProperty(spawnedItem, "track", false);
        if (trackWand) {
            Wand wand = controller.getWand(spawnedItem);
            controller.addLostWand(wand, event.getEntity().getLocation());
        }
    } else {
        // registerEntityForUndo(event.getEntity());
        if (ageDroppedItems > 0) {
            int ticks = ageDroppedItems * 20 / 1000;
            Item item = event.getEntity();
            CompatibilityUtils.ageItem(item, ticks);
        }
    }
}
Also used : Item(org.bukkit.entity.Item) UndoList(com.elmakers.mine.bukkit.api.block.UndoList) MaterialSet(com.elmakers.mine.bukkit.api.magic.MaterialSet) Block(org.bukkit.block.Block) Wand(com.elmakers.mine.bukkit.wand.Wand) ItemStack(org.bukkit.inventory.ItemStack) BlockData(com.elmakers.mine.bukkit.api.block.BlockData) EventHandler(org.bukkit.event.EventHandler)

Example 7 with UndoList

use of com.elmakers.mine.bukkit.api.block.UndoList in project MagicPlugin by elBukkit.

the class ExplosionController method onEntityFinalizeExplode.

@EventHandler(priority = EventPriority.HIGHEST)
public void onEntityFinalizeExplode(EntityExplodeEvent event) {
    Entity explodingEntity = event.getEntity();
    if (explodingEntity == null)
        return;
    UndoList blockList = getExplosionUndo(explodingEntity);
    if (blockList == null)
        return;
    if (event.isCancelled()) {
        blockList.cancelExplosion(explodingEntity);
    } else {
        controller.disableItemSpawn();
        try {
            blockList.finalizeExplosion(explodingEntity, event.blockList());
        } catch (Exception ex) {
            controller.getLogger().log(Level.WARNING, "Error finalizing explosion", ex);
        }
        controller.enableItemSpawn();
    }
}
Also used : Entity(org.bukkit.entity.Entity) UndoList(com.elmakers.mine.bukkit.api.block.UndoList) EventHandler(org.bukkit.event.EventHandler)

Example 8 with UndoList

use of com.elmakers.mine.bukkit.api.block.UndoList in project MagicPlugin by elBukkit.

the class HangingController method onHangingBreak.

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onHangingBreak(HangingBreakEvent event) {
    final Hanging entity = event.getEntity();
    if (!entity.isValid())
        return;
    try {
        final BlockFace attachedFace = entity.getAttachedFace();
        Location location = entity.getLocation();
        UndoList undoList = controller.getPendingUndo(location);
        if (undoList != null) {
            event.setCancelled(true);
            undoList.damage(entity);
        } else {
            location = location.getBlock().getRelative(attachedFace).getLocation();
            undoList = controller.getPendingUndo(location);
            if (undoList != null) {
                event.setCancelled(true);
                undoList.damage(entity);
            }
        }
    } catch (Exception ex) {
        controller.getLogger().log(Level.WARNING, "Failed to handle HangingBreakEvent", ex);
    }
}
Also used : UndoList(com.elmakers.mine.bukkit.api.block.UndoList) Hanging(org.bukkit.entity.Hanging) BlockFace(org.bukkit.block.BlockFace) Location(org.bukkit.Location) EventHandler(org.bukkit.event.EventHandler)

Example 9 with UndoList

use of com.elmakers.mine.bukkit.api.block.UndoList in project MagicPlugin by elBukkit.

the class PlayerController method onPlayerPrePickupItem.

@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onPlayerPrePickupItem(PlayerPickupItemEvent event) {
    Item item = event.getItem();
    ItemStack pickup = item.getItemStack();
    if (NMSUtils.isTemporary(pickup) || item.hasMetadata("temporary")) {
        item.remove();
        event.setCancelled(true);
        return;
    }
    boolean isWand = Wand.isWand(pickup);
    // Creative mode inventory hacky work-around :\
    if (event.getPlayer().getGameMode() == GameMode.CREATIVE && isWand && enableCreativeModeEjecting) {
        event.setCancelled(true);
        return;
    }
    // Check to see if this is an item we might undo, and remove it from undo
    UndoList undoList = controller.getEntityUndo(item);
    if (undoList != null) {
        undoList.remove(item);
    }
    Player player = event.getPlayer();
    Mage mage = controller.getMage(player);
    // Remove lost wands from records
    Messages messages = controller.getMessages();
    if (isWand) {
        Wand wand = controller.getWand(pickup);
        if (!wand.canUse(player)) {
            if (lastDropWarn == 0 || System.currentTimeMillis() - lastDropWarn > 10000) {
                mage.sendMessage(messages.get("wand.bound").replace("$name", wand.getOwner()));
            }
            lastDropWarn = System.currentTimeMillis();
            event.setCancelled(true);
            return;
        }
        controller.removeLostWand(wand.getId());
    }
    // Wands will absorb spells and upgrade items
    Wand activeWand = mage.getActiveWand();
    if (activeWand != null && activeWand.isModifiable() && (Wand.isSpell(pickup) || Wand.isBrush(pickup) || Wand.isUpgrade(pickup) || Wand.isSP(pickup)) && activeWand.addItem(pickup)) {
        event.getItem().remove();
        event.setCancelled(true);
        return;
    }
    if (!mage.hasStoredInventory() && isWand) {
        mage.checkWandNextTick();
    }
}
Also used : Item(org.bukkit.entity.Item) Player(org.bukkit.entity.Player) Messages(com.elmakers.mine.bukkit.api.magic.Messages) UndoList(com.elmakers.mine.bukkit.api.block.UndoList) Mage(com.elmakers.mine.bukkit.magic.Mage) Wand(com.elmakers.mine.bukkit.wand.Wand) ItemStack(org.bukkit.inventory.ItemStack) EventHandler(org.bukkit.event.EventHandler)

Example 10 with UndoList

use of com.elmakers.mine.bukkit.api.block.UndoList in project MagicPlugin by elBukkit.

the class MagicController method undoRecent.

@Nullable
@Override
public UndoList undoRecent(Block target, int timeout) {
    for (Mage mage : mages.values()) {
        com.elmakers.mine.bukkit.api.block.UndoQueue queue = mage.getUndoQueue();
        UndoList undid = queue.undoRecent(target, timeout);
        if (undid != null) {
            return undid;
        }
    }
    return null;
}
Also used : UndoList(com.elmakers.mine.bukkit.api.block.UndoList) Mage(com.elmakers.mine.bukkit.api.magic.Mage) Nullable(javax.annotation.Nullable)

Aggregations

UndoList (com.elmakers.mine.bukkit.api.block.UndoList)36 EventHandler (org.bukkit.event.EventHandler)15 Entity (org.bukkit.entity.Entity)14 Block (org.bukkit.block.Block)13 Location (org.bukkit.Location)10 FallingBlock (org.bukkit.entity.FallingBlock)10 Mage (com.elmakers.mine.bukkit.api.magic.Mage)8 LivingEntity (org.bukkit.entity.LivingEntity)6 Player (org.bukkit.entity.Player)6 ItemStack (org.bukkit.inventory.ItemStack)6 Ageable (org.bukkit.entity.Ageable)4 PigZombie (org.bukkit.entity.PigZombie)4 Slime (org.bukkit.entity.Slime)4 Zombie (org.bukkit.entity.Zombie)4 MageController (com.elmakers.mine.bukkit.api.magic.MageController)3 SpellResult (com.elmakers.mine.bukkit.api.spell.SpellResult)3 Target (com.elmakers.mine.bukkit.utility.Target)3 Nullable (javax.annotation.Nullable)3 BlockFace (org.bukkit.block.BlockFace)3 Skeleton (org.bukkit.entity.Skeleton)3