Search in sources :

Example 26 with TownyWorld

use of com.palmergames.bukkit.towny.object.TownyWorld in project Towny by ElgarL.

the class TownyFlatFileSource method loadTownBlockList.

/*
	 * Load keys
	 */
@Override
public boolean loadTownBlockList() {
    TownyMessaging.sendDebugMsg("Loading TownBlock List");
    String line;
    BufferedReader fin = null;
    try {
        fin = new BufferedReader(new FileReader(rootFolder + dataFolder + FileMgmt.fileSeparator() + "townblocks.txt"));
        while ((line = fin.readLine()) != null) if (!line.equals("")) {
            String[] tokens = line.split(",");
            if (tokens.length < 3)
                continue;
            TownyWorld world;
            try {
                world = getWorld(tokens[0]);
            } catch (NotRegisteredException ex) {
                /*
						 * The world is not listed.
						 * Allow the creation of new worlds here to account
						 * for mod worlds which are not reported at startup.
						 */
                newWorld(tokens[0]);
                world = getWorld(tokens[0]);
            }
            int x = Integer.parseInt(tokens[1]);
            int z = Integer.parseInt(tokens[2]);
            try {
                world.newTownBlock(x, z);
            } catch (AlreadyRegisteredException e) {
            }
        }
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    } finally {
        if (fin != null) {
            try {
                fin.close();
            } catch (IOException ignore) {
            }
        }
    }
}
Also used : NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) AlreadyRegisteredException(com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) IOException(java.io.IOException) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) IOException(java.io.IOException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) AlreadyRegisteredException(com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException) InvalidNameException(javax.naming.InvalidNameException) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Example 27 with TownyWorld

use of com.palmergames.bukkit.towny.object.TownyWorld in project Towny by ElgarL.

the class TownyFlatFileSource method utilSaveTownBlocks.

@Deprecated
public String utilSaveTownBlocks(List<TownBlock> townBlocks) {
    HashMap<TownyWorld, ArrayList<TownBlock>> worlds = new HashMap<TownyWorld, ArrayList<TownBlock>>();
    String out = "";
    // Sort all town blocks according to what world its in
    for (TownBlock townBlock : townBlocks) {
        TownyWorld world = townBlock.getWorld();
        if (!worlds.containsKey(world))
            worlds.put(world, new ArrayList<TownBlock>());
        worlds.get(world).add(townBlock);
    }
    for (TownyWorld world : worlds.keySet()) {
        out += world.getName() + ":";
        for (TownBlock townBlock : worlds.get(world)) {
            out += "[" + townBlock.getType().getId();
            out += "," + (townBlock.isOutpost() ? "1" : "0");
            out += "]" + townBlock.getX() + "," + townBlock.getZ() + "," + townBlock.getPlotPrice() + ";";
        }
        out += "|";
    }
    return out;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) TownBlock(com.palmergames.bukkit.towny.object.TownBlock)

Example 28 with TownyWorld

use of com.palmergames.bukkit.towny.object.TownyWorld in project Towny by ElgarL.

the class TownyEntityListener method onHangingPlace.

@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onHangingPlace(HangingPlaceEvent event) {
    if (plugin.isError()) {
        event.setCancelled(true);
        return;
    }
    long start = System.currentTimeMillis();
    Player player = event.getPlayer();
    Entity hanging = event.getEntity();
    try {
        TownyWorld townyWorld = TownyUniverse.getDataSource().getWorld(hanging.getWorld().getName());
        if (!townyWorld.isUsingTowny())
            return;
        // Get build permissions (updates if none exist)
        boolean bBuild = PlayerCacheUtil.getCachePermission(player, hanging.getLocation(), 321, (byte) 0, TownyPermission.ActionType.BUILD);
        // Allow placing if we are permitted
        if (bBuild)
            return;
        /*
			 * Fetch the players cache
			 */
        PlayerCache cache = plugin.getCache(player);
        event.setCancelled(true);
        if (cache.hasBlockErrMsg())
            TownyMessaging.sendErrorMsg(player, cache.getBlockErrMsg());
    } catch (NotRegisteredException e1) {
        TownyMessaging.sendErrorMsg(player, TownySettings.getLangString("msg_err_not_configured"));
        event.setCancelled(true);
        return;
    }
    TownyMessaging.sendDebugMsg("onHangingBreak took " + (System.currentTimeMillis() - start) + "ms (" + event.getEventName() + ", " + event.isCancelled() + ")");
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) PlayerCache(com.palmergames.bukkit.towny.object.PlayerCache) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) EventHandler(org.bukkit.event.EventHandler)

Example 29 with TownyWorld

use of com.palmergames.bukkit.towny.object.TownyWorld in project Towny by ElgarL.

the class TownyEntityListener method onHangingBreak.

@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onHangingBreak(HangingBreakEvent event) {
    if (plugin.isError()) {
        event.setCancelled(true);
        return;
    }
    TownyWorld townyWorld = null;
    String worldName = null;
    Entity hanging = event.getEntity();
    try {
        worldName = hanging.getWorld().getName();
        townyWorld = TownyUniverse.getDataSource().getWorld(worldName);
        if (!townyWorld.isUsingTowny())
            return;
    } catch (NotRegisteredException e1) {
        // event.setCancelled(true);
        return;
    }
    if (event instanceof HangingBreakByEntityEvent) {
        HangingBreakByEntityEvent evt = (HangingBreakByEntityEvent) event;
        Object remover = evt.getRemover();
        /*
			 * Check if this has a shooter.
			 */
        if (remover instanceof Projectile) {
            remover = ((Projectile) remover).getShooter();
        }
        if (remover instanceof Player) {
            Player player = (Player) remover;
            // Get destroy permissions (updates if none exist)
            boolean bDestroy = PlayerCacheUtil.getCachePermission(player, hanging.getLocation(), 321, (byte) 0, TownyPermission.ActionType.DESTROY);
            // Allow the removal if we are permitted
            if (bDestroy)
                return;
            /*
				 * Fetch the players cache
				 */
            PlayerCache cache = plugin.getCache(player);
            event.setCancelled(true);
            if (cache.hasBlockErrMsg())
                TownyMessaging.sendErrorMsg(player, cache.getBlockErrMsg());
        } else {
            // Explosions are blocked in this plot
            if (!locationCanExplode(townyWorld, hanging.getLocation()))
                event.setCancelled(true);
        }
    } else {
        switch(event.getCause()) {
            case EXPLOSION:
                if (!locationCanExplode(townyWorld, event.getEntity().getLocation()))
                    event.setCancelled(true);
                break;
            default:
        }
    }
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) HangingBreakByEntityEvent(org.bukkit.event.hanging.HangingBreakByEntityEvent) Player(org.bukkit.entity.Player) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) PlayerCache(com.palmergames.bukkit.towny.object.PlayerCache) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) Projectile(org.bukkit.entity.Projectile) EventHandler(org.bukkit.event.EventHandler)

Example 30 with TownyWorld

use of com.palmergames.bukkit.towny.object.TownyWorld in project Towny by ElgarL.

the class TownyEntityListener method onEntityDeath.

/**
	 * Prevent monsters from dropping blocks if within an arena plot.
	 * 
	 * @param event
	 */
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onEntityDeath(EntityDeathEvent event) {
    if (plugin.isError()) {
        return;
    }
    Entity entity = event.getEntity();
    if (entity instanceof Monster) {
        Location loc = entity.getLocation();
        TownyWorld townyWorld = null;
        try {
            townyWorld = TownyUniverse.getDataSource().getWorld(loc.getWorld().getName());
            // remove drops from monster deaths if in an arena plot
            if (townyWorld.isUsingTowny()) {
                if (townyWorld.getTownBlock(Coord.parseCoord(loc)).getType() == TownBlockType.ARENA)
                    event.getDrops().clear();
            }
        } catch (NotRegisteredException e) {
        // Unknown world or not in a town
        }
    }
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) Monster(org.bukkit.entity.Monster) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) Location(org.bukkit.Location) BlockLocation(com.palmergames.bukkit.towny.regen.block.BlockLocation) EventHandler(org.bukkit.event.EventHandler)

Aggregations

TownyWorld (com.palmergames.bukkit.towny.object.TownyWorld)33 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)27 TownBlock (com.palmergames.bukkit.towny.object.TownBlock)15 TownyException (com.palmergames.bukkit.towny.exceptions.TownyException)14 EventHandler (org.bukkit.event.EventHandler)12 Player (org.bukkit.entity.Player)11 Entity (org.bukkit.entity.Entity)9 AlreadyRegisteredException (com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException)7 PlayerCache (com.palmergames.bukkit.towny.object.PlayerCache)7 BlockLocation (com.palmergames.bukkit.towny.regen.block.BlockLocation)7 Location (org.bukkit.Location)7 LivingEntity (org.bukkit.entity.LivingEntity)7 Coord (com.palmergames.bukkit.towny.object.Coord)6 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 Resident (com.palmergames.bukkit.towny.object.Resident)5 WorldCoord (com.palmergames.bukkit.towny.object.WorldCoord)5 Block (org.bukkit.block.Block)5 Town (com.palmergames.bukkit.towny.object.Town)3 Projectile (org.bukkit.entity.Projectile)3