Search in sources :

Example 6 with Chunk

use of org.bukkit.Chunk in project BKCommonLib by bergerhealer.

the class BlockStateConversion method tileEntityToBlockState.

public static BlockState tileEntityToBlockState(Block block, Object nmsTileEntity) {
    // Store and restore old state in case of recursive calls to this function
    // This could happen if inside BlockState construction a chunk is loaded anyway
    // Would be bad, but its best to assume the worst
    TileState old_state = input_state;
    try {
        input_state = new TileState(block, nmsTileEntity);
        BlockState result = proxy_block.getState();
        // Internal BlockState needs to have all proxy field instances replaced with what it should be
        BlockStateCache cache = BlockStateCache.get(result.getClass());
        for (SafeField<World> worldField : cache.worldFields) {
            worldField.set(result, input_state.block.getWorld());
        }
        for (SafeField<Chunk> chunkField : cache.chunkFields) {
            chunkField.set(result, input_state.block.getChunk());
        }
        // All done!
        return result;
    } catch (Throwable t) {
        Logging.LOGGER_CONVERSION.once(Level.SEVERE, "Failed to convert " + nmsTileEntity.getClass().getName() + " to CraftBlockState", t);
        return CraftBlockStateHandle.createNew(input_state.block);
    } finally {
        input_state = old_state;
    }
}
Also used : BlockState(org.bukkit.block.BlockState) World(org.bukkit.World) Chunk(org.bukkit.Chunk)

Example 7 with Chunk

use of org.bukkit.Chunk in project Essentials by drtshock.

the class Commandremove method removeHandler.

private void removeHandler(CommandSource sender, List<String> types, List<String> customTypes, World world, int radius) {
    int removed = 0;
    if (radius > 0) {
        radius *= radius;
    }
    ArrayList<ToRemove> removeTypes = new ArrayList<>();
    ArrayList<Mob> customRemoveTypes = new ArrayList<>();
    for (String s : types) {
        removeTypes.add(ToRemove.valueOf(s));
    }
    boolean warnUser = false;
    for (String s : customTypes) {
        Mob mobType = Mob.fromName(s);
        if (mobType == null) {
            warnUser = true;
        } else {
            customRemoveTypes.add(mobType);
        }
    }
    if (warnUser) {
        sender.sendMessage(tl("invalidMob"));
    }
    for (Chunk chunk : world.getLoadedChunks()) {
        for (Entity e : chunk.getEntities()) {
            if (radius > 0) {
                if (sender.getPlayer().getLocation().distanceSquared(e.getLocation()) > radius) {
                    continue;
                }
            }
            if (e instanceof HumanEntity) {
                continue;
            }
            for (ToRemove toRemove : removeTypes) {
                // We should skip any TAMED animals unless we are specifially targetting them.
                if (e instanceof Tameable && ((Tameable) e).isTamed() && !removeTypes.contains(ToRemove.TAMED)) {
                    continue;
                }
                // We should skip any NAMED animals unless we are specifially targetting them.
                if (e instanceof LivingEntity && e.getCustomName() != null && !removeTypes.contains(ToRemove.NAMED)) {
                    continue;
                }
                switch(toRemove) {
                    case TAMED:
                        if (e instanceof Tameable && ((Tameable) e).isTamed()) {
                            e.remove();
                            removed++;
                        }
                        break;
                    case NAMED:
                        if (e instanceof LivingEntity && e.getCustomName() != null) {
                            e.remove();
                            removed++;
                        }
                        break;
                    case DROPS:
                        if (e instanceof Item) {
                            e.remove();
                            removed++;
                        }
                        break;
                    case ARROWS:
                        if (e instanceof Projectile) {
                            e.remove();
                            removed++;
                        }
                        break;
                    case BOATS:
                        if (e instanceof Boat) {
                            e.remove();
                            removed++;
                        }
                        break;
                    case MINECARTS:
                        if (e instanceof Minecart) {
                            e.remove();
                            removed++;
                        }
                        break;
                    case XP:
                        if (e instanceof ExperienceOrb) {
                            e.remove();
                            removed++;
                        }
                        break;
                    case PAINTINGS:
                        if (e instanceof Painting) {
                            e.remove();
                            removed++;
                        }
                        break;
                    case ITEMFRAMES:
                        if (e instanceof ItemFrame) {
                            e.remove();
                            removed++;
                        }
                        break;
                    case ENDERCRYSTALS:
                        if (e instanceof EnderCrystal) {
                            e.remove();
                            removed++;
                        }
                        break;
                    case AMBIENT:
                        if (e instanceof Flying) {
                            e.remove();
                            removed++;
                        }
                        break;
                    case HOSTILE:
                    case MONSTERS:
                        if (e instanceof Monster || e instanceof ComplexLivingEntity || e instanceof Flying || e instanceof Slime) {
                            e.remove();
                            removed++;
                        }
                        break;
                    case PASSIVE:
                    case ANIMALS:
                        if (e instanceof Animals || e instanceof NPC || e instanceof Snowman || e instanceof WaterMob || e instanceof Ambient) {
                            e.remove();
                            removed++;
                        }
                        break;
                    case MOBS:
                        if (e instanceof Animals || e instanceof NPC || e instanceof Snowman || e instanceof WaterMob || e instanceof Monster || e instanceof ComplexLivingEntity || e instanceof Flying || e instanceof Slime || e instanceof Ambient) {
                            e.remove();
                            removed++;
                        }
                        break;
                    case ENTITIES:
                    case ALL:
                        if (e instanceof Entity) {
                            e.remove();
                            removed++;
                        }
                        break;
                    case CUSTOM:
                        for (Mob type : customRemoveTypes) {
                            if (e.getType() == type.getType()) {
                                e.remove();
                                removed++;
                            }
                        }
                        break;
                }
            }
        }
    }
    sender.sendMessage(tl("removed", removed));
}
Also used : ArrayList(java.util.ArrayList) Mob(com.earth2me.essentials.Mob) Chunk(org.bukkit.Chunk)

Example 8 with Chunk

use of org.bukkit.Chunk in project Towny by ElgarL.

the class ResidentCommand method residentSpawn.

/**
	 * Attempt to send player to bed spawn.
	 * 
	 * @param player
	 */
public void residentSpawn(Player player) {
    boolean isTownyAdmin = TownyUniverse.getPermissionSource().isTownyAdmin(player);
    Resident resident;
    try {
        resident = TownyUniverse.getDataSource().getResident(player.getName());
        Town town;
        Location spawnLoc;
        String notAffordMSG;
        TownSpawnLevel townSpawnPermission;
        // Set target town and affiliated messages.
        town = resident.getTown();
        notAffordMSG = TownySettings.getLangString("msg_err_cant_afford_tp");
        if (TownySettings.getBedUse() && player.getBedSpawnLocation() != null) {
            spawnLoc = player.getBedSpawnLocation();
        } else {
            spawnLoc = town.getSpawn();
        }
        if (isTownyAdmin) {
            townSpawnPermission = TownSpawnLevel.ADMIN;
        } else {
            townSpawnPermission = TownSpawnLevel.TOWN_RESIDENT;
        }
        if (!isTownyAdmin) {
            // Prevent spawn travel while in disallowed zones (if
            // configured)
            List<String> disallowedZones = TownySettings.getDisallowedTownSpawnZones();
            if (!disallowedZones.isEmpty()) {
                String inTown = null;
                try {
                    Location loc = plugin.getCache(player).getLastLocation();
                    inTown = TownyUniverse.getTownName(loc);
                } catch (NullPointerException e) {
                    inTown = TownyUniverse.getTownName(player.getLocation());
                }
                if (inTown == null && disallowedZones.contains("unclaimed"))
                    throw new TownyException(String.format(TownySettings.getLangString("msg_err_town_spawn_disallowed_from"), "the Wilderness"));
                if (inTown != null && resident.hasNation() && TownyUniverse.getDataSource().getTown(inTown).hasNation()) {
                    Nation inNation = TownyUniverse.getDataSource().getTown(inTown).getNation();
                    Nation playerNation = resident.getTown().getNation();
                    if (inNation.hasEnemy(playerNation) && disallowedZones.contains("enemy"))
                        throw new TownyException(String.format(TownySettings.getLangString("msg_err_town_spawn_disallowed_from"), "Enemy areas"));
                    if (!inNation.hasAlly(playerNation) && !inNation.hasEnemy(playerNation) && disallowedZones.contains("neutral"))
                        throw new TownyException(String.format(TownySettings.getLangString("msg_err_town_spawn_disallowed_from"), "Neutral towns"));
                }
            }
        }
        double travelCost = townSpawnPermission.getCost();
        // Check if need/can pay
        if (travelCost > 0 && TownySettings.isUsingEconomy() && (resident.getHoldingBalance() < travelCost))
            throw new TownyException(notAffordMSG);
        // Used later to make sure the chunk we teleport to is loaded.
        Chunk chunk = spawnLoc.getChunk();
        // Essentials tests
        boolean UsingESS = plugin.isEssentials();
        if (UsingESS && !isTownyAdmin) {
            try {
                User user = plugin.getEssentials().getUser(player);
                if (!user.isJailed()) {
                    Teleport teleport = user.getTeleport();
                    if (!chunk.isLoaded())
                        chunk.load();
                    // Cause an essentials exception if in cooldown.
                    teleport.cooldown(true);
                    teleport.teleport(spawnLoc, null);
                }
            } catch (Exception e) {
                TownyMessaging.sendErrorMsg(player, "Error: " + e.getMessage());
                // cooldown?
                return;
            }
        }
        // travel.
        if (travelCost > 0 && TownySettings.isUsingEconomy() && resident.payTo(travelCost, town, String.format("Resident Spawn (%s)", townSpawnPermission))) {
            // +
            TownyMessaging.sendMsg(player, String.format(TownySettings.getLangString("msg_cost_spawn"), TownyEconomyHandler.getFormattedBalance(travelCost)));
        // TownyEconomyObject.getEconomyCurrency()));
        }
        // If an Admin or Essentials teleport isn't being used, use our own.
        if (isTownyAdmin) {
            if (player.getVehicle() != null)
                player.getVehicle().eject();
            if (!chunk.isLoaded())
                chunk.load();
            player.teleport(spawnLoc, TeleportCause.COMMAND);
            return;
        }
        if (!UsingESS) {
            if (TownyTimerHandler.isTeleportWarmupRunning()) {
                // Use teleport warmup
                player.sendMessage(String.format(TownySettings.getLangString("msg_town_spawn_warmup"), TownySettings.getTeleportWarmupTime()));
                plugin.getTownyUniverse().requestTeleport(player, spawnLoc, travelCost);
            } else {
                // Don't use teleport warmup
                if (player.getVehicle() != null)
                    player.getVehicle().eject();
                if (!chunk.isLoaded())
                    chunk.load();
                player.teleport(spawnLoc, TeleportCause.COMMAND);
            }
        }
    } catch (TownyException e) {
        TownyMessaging.sendErrorMsg(player, e.getMessage());
    } catch (EconomyException e) {
        TownyMessaging.sendErrorMsg(player, e.getMessage());
    }
}
Also used : Nation(com.palmergames.bukkit.towny.object.Nation) TownSpawnLevel(com.palmergames.bukkit.towny.object.TownSpawnLevel) User(com.earth2me.essentials.User) Chunk(org.bukkit.Chunk) Teleport(com.earth2me.essentials.Teleport) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) EconomyException(com.palmergames.bukkit.towny.exceptions.EconomyException) AlreadyRegisteredException(com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException) EconomyException(com.palmergames.bukkit.towny.exceptions.EconomyException) Town(com.palmergames.bukkit.towny.object.Town) Resident(com.palmergames.bukkit.towny.object.Resident) Location(org.bukkit.Location)

Example 9 with Chunk

use of org.bukkit.Chunk in project Towny by ElgarL.

the class TownyRegenAPI method regenChunk.

/**
	 * Regenerate the chunk the player is stood in and store the block data so it can be undone later.
	 * 
	 * @param player
	 */
public static void regenChunk(Player player) {
    try {
        Coord coord = Coord.parseCoord(player);
        World world = player.getWorld();
        Chunk chunk = world.getChunkAt(player.getLocation());
        int maxHeight = world.getMaxHeight();
        Object[][][] snapshot = new Object[16][maxHeight][16];
        for (int x = 0; x < 16; x++) {
            for (int z = 0; z < 16; z++) {
                for (int y = 0; y < maxHeight; y++) {
                    //Current block to save
                    BlockState state = chunk.getBlock(x, y, z).getState();
                    if (state instanceof org.bukkit.block.Sign) {
                        BlockSign sign = new BlockSign(BukkitTools.getTypeId(state), BukkitTools.getDataData(state), ((org.bukkit.block.Sign) state).getLines());
                        sign.setLocation(state.getLocation());
                        snapshot[x][y][z] = sign;
                    } else if (state instanceof CreatureSpawner) {
                        BlockMobSpawner spawner = new BlockMobSpawner(((CreatureSpawner) state).getSpawnedType());
                        spawner.setLocation(state.getLocation());
                        spawner.setDelay(((CreatureSpawner) state).getDelay());
                        snapshot[x][y][z] = spawner;
                    } else if ((state instanceof InventoryHolder) && !(state instanceof Player)) {
                        BlockInventoryHolder holder = new BlockInventoryHolder(BukkitTools.getTypeId(state), BukkitTools.getDataData(state), ((InventoryHolder) state).getInventory().getContents());
                        holder.setLocation(state.getLocation());
                        snapshot[x][y][z] = holder;
                    } else {
                        snapshot[x][y][z] = new BlockObject(BukkitTools.getTypeId(state), BukkitTools.getDataData(state), state.getLocation());
                    }
                }
            }
        }
        TownyUniverse.getDataSource().getResident(player.getName()).addUndo(snapshot);
        Bukkit.getWorld(player.getWorld().getName()).regenerateChunk(coord.getX(), coord.getZ());
    } catch (NotRegisteredException e) {
    // Failed to get resident
    }
}
Also used : Player(org.bukkit.entity.Player) BlockInventoryHolder(com.palmergames.bukkit.towny.regen.block.BlockInventoryHolder) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) World(org.bukkit.World) Chunk(org.bukkit.Chunk) CreatureSpawner(org.bukkit.block.CreatureSpawner) WorldCoord(com.palmergames.bukkit.towny.object.WorldCoord) Coord(com.palmergames.bukkit.towny.object.Coord) BlockMobSpawner(com.palmergames.bukkit.towny.regen.block.BlockMobSpawner) BlockState(org.bukkit.block.BlockState) BlockSign(com.palmergames.bukkit.towny.regen.block.BlockSign) BlockObject(com.palmergames.bukkit.towny.regen.block.BlockObject) Sign(org.bukkit.block.Sign) BlockSign(com.palmergames.bukkit.towny.regen.block.BlockSign) InventoryHolder(org.bukkit.inventory.InventoryHolder) BlockInventoryHolder(com.palmergames.bukkit.towny.regen.block.BlockInventoryHolder) BlockObject(com.palmergames.bukkit.towny.regen.block.BlockObject)

Example 10 with Chunk

use of org.bukkit.Chunk in project Towny by ElgarL.

the class TownyRegenAPI method regenUndo.

/**
	 * Restore the relevant chunk using the snapshot data stored in the resident
	 * object.
	 * 
	 * @param snapshot
	 * @param resident
	 */
public static void regenUndo(Object[][][] snapshot, Resident resident) {
    BlockObject key = ((BlockObject) snapshot[0][0][0]);
    World world = key.getLocation().getWorld();
    Chunk chunk = key.getLocation().getChunk();
    int maxHeight = world.getMaxHeight();
    for (int x = 0; x < 16; x++) {
        for (int z = 0; z < 16; z++) {
            for (int y = 0; y < maxHeight; y++) {
                // Snapshot data we need to update the world.
                Object state = snapshot[x][y][z];
                // The block we will be updating
                Block block = chunk.getBlock(x, y, z);
                if (state instanceof BlockSign) {
                    BlockSign signData = (BlockSign) state;
                    BukkitTools.setTypeIdAndData(block, signData.getTypeId(), signData.getData(), false);
                    Sign sign = (Sign) block.getState();
                    int i = 0;
                    for (String line : signData.getLines()) sign.setLine(i++, line);
                    sign.update(true);
                } else if (state instanceof BlockMobSpawner) {
                    BlockMobSpawner spawnerData = (BlockMobSpawner) state;
                    BukkitTools.setTypeIdAndData(block, spawnerData.getTypeId(), spawnerData.getData(), false);
                    ((CreatureSpawner) block.getState()).setSpawnedType(spawnerData.getSpawnedType());
                    ((CreatureSpawner) block.getState()).setDelay(spawnerData.getDelay());
                } else if ((state instanceof BlockInventoryHolder) && !(state instanceof Player)) {
                    BlockInventoryHolder containerData = (BlockInventoryHolder) state;
                    BukkitTools.setTypeIdAndData(block, containerData.getTypeId(), containerData.getData(), false);
                    // Container to receive the inventory
                    InventoryHolder container = (InventoryHolder) block.getState();
                    // Contents we are respawning.						
                    if (containerData.getItems().length > 0)
                        container.getInventory().setContents(containerData.getItems());
                } else {
                    BlockObject blockData = (BlockObject) state;
                    BukkitTools.setTypeIdAndData(block, blockData.getTypeId(), blockData.getData(), false);
                }
            }
        }
    }
    TownyMessaging.sendMessage(BukkitTools.getPlayerExact(resident.getName()), TownySettings.getLangString("msg_undo_complete"));
}
Also used : BlockInventoryHolder(com.palmergames.bukkit.towny.regen.block.BlockInventoryHolder) Player(org.bukkit.entity.Player) World(org.bukkit.World) Chunk(org.bukkit.Chunk) BlockMobSpawner(com.palmergames.bukkit.towny.regen.block.BlockMobSpawner) BlockSign(com.palmergames.bukkit.towny.regen.block.BlockSign) Block(org.bukkit.block.Block) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) BlockObject(com.palmergames.bukkit.towny.regen.block.BlockObject) Sign(org.bukkit.block.Sign) BlockSign(com.palmergames.bukkit.towny.regen.block.BlockSign) InventoryHolder(org.bukkit.inventory.InventoryHolder) BlockInventoryHolder(com.palmergames.bukkit.towny.regen.block.BlockInventoryHolder) BlockObject(com.palmergames.bukkit.towny.regen.block.BlockObject)

Aggregations

Chunk (org.bukkit.Chunk)12 Location (org.bukkit.Location)4 World (org.bukkit.World)3 Player (org.bukkit.entity.Player)3 Teleport (com.earth2me.essentials.Teleport)2 User (com.earth2me.essentials.User)2 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)2 TownyException (com.palmergames.bukkit.towny.exceptions.TownyException)2 Resident (com.palmergames.bukkit.towny.object.Resident)2 BlockInventoryHolder (com.palmergames.bukkit.towny.regen.block.BlockInventoryHolder)2 BlockMobSpawner (com.palmergames.bukkit.towny.regen.block.BlockMobSpawner)2 BlockObject (com.palmergames.bukkit.towny.regen.block.BlockObject)2 BlockSign (com.palmergames.bukkit.towny.regen.block.BlockSign)2 ArrayList (java.util.ArrayList)2 BlockState (org.bukkit.block.BlockState)2 Sign (org.bukkit.block.Sign)2 InventoryHolder (org.bukkit.inventory.InventoryHolder)2 TreasureHuntModule (au.com.mineauz.minigames.minigame.modules.TreasureHuntModule)1 Mob (com.earth2me.essentials.Mob)1 AlreadyRegisteredException (com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException)1