Skip to content

Commit

Permalink
Fixes #83 - Updated WE API usage according to latest WE
Browse files Browse the repository at this point in the history
  • Loading branch information
rlf committed Dec 21, 2014
1 parent b207514 commit 1f1f502
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 52 deletions.
60 changes: 36 additions & 24 deletions src/main/java/us/talabrek/ultimateskyblock/WorldEditHandler.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
package us.talabrek.ultimateskyblock;

import com.sk89q.worldedit.*;
import com.sk89q.worldedit.bukkit.BukkitPlayer;
import com.sk89q.worldedit.bukkit.BukkitWorld;
import com.sk89q.worldedit.bukkit.WorldEditPlugin;
import com.sk89q.worldedit.data.DataException;
import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardReader;
import com.sk89q.worldedit.function.operation.Operation;
import com.sk89q.worldedit.function.operation.Operations;
import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.schematic.SchematicFormat;
import com.sk89q.worldedit.session.ClipboardHolder;
import com.sk89q.worldedit.world.registry.WorldData;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

import java.io.File;
import java.io.IOException;
import java.io.*;
import java.util.logging.Level;

public class WorldEditHandler {
Expand All @@ -27,27 +31,35 @@ public static WorldEditPlugin getWorldEdit() {
return (WorldEditPlugin) plugin;
}

public static boolean loadIslandSchematic(final World world, final File file, final Location origin) throws DataException, IOException, MaxChangedBlocksException {
final Vector v = new Vector(origin.getBlockX(), origin.getBlockY(), origin.getBlockZ());
final SchematicFormat format = SchematicFormat.getFormat(file);
if (format == null) {
return false;
}
final EditSession es = new EditSession(new BukkitWorld(world), 999999999);
final CuboidClipboard cc = format.load(file);
cc.paste(es, v, false);
return true;
}
public static boolean loadIslandSchematic(Player player, final World world, final File file, final Location origin) {
WorldEdit worldEdit = getWorldEdit().getWorldEdit();
BukkitPlayer wePlayer = getWorldEdit().wrapPlayer(player);
LocalSession session = worldEdit.getSession(wePlayer);
try (InputStream in = new BufferedInputStream(new FileInputStream(file))) {
BukkitWorld bukkitWorld = new BukkitWorld(world);
ClipboardReader reader = ClipboardFormat.SCHEMATIC.getReader(in);

public static void reloadIsland(final World skyWorld, final ProtectedRegion region) {
long t = System.currentTimeMillis();
final Region cube = getRegion(skyWorld, region);
for (Vector2D chunk : cube.getChunks()) {
skyWorld.loadChunk(chunk.getBlockX()/16, chunk.getBlockZ()/16, true);
skyWorld.refreshChunk(chunk.getBlockX()/16, chunk.getBlockZ()/16);
WorldData worldData = bukkitWorld.getWorldData();
Clipboard clipboard = reader.read(worldData);
ClipboardHolder holder = new ClipboardHolder(clipboard, worldData);
session.setClipboard(holder);

EditSession editSession = new EditSession(bukkitWorld, 255 * Settings.island_protectionRange * Settings.island_protectionRange);
editSession.enableQueue();
Vector to = new Vector(origin.getBlockX(), origin.getBlockY(), origin.getBlockZ());
Operation operation = holder
.createPaste(editSession, worldData)
.to(to)
.ignoreAirBlocks(false)
.build();
Operations.completeLegacy(operation);
editSession.flushQueue();
editSession.commit();
return true;
} catch (IOException|WorldEditException e) {
uSkyBlock.log(Level.WARNING, "Unable to load schematic " + file, e);
}
long diff = System.currentTimeMillis() - t;
uSkyBlock.log(Level.INFO, String.format("Reloaded island in %d.%03d seconds", (diff / 1000), (diff % 1000)));
return false;
}

public static void clearIsland(final World skyWorld, final ProtectedRegion region) {
Expand Down
62 changes: 34 additions & 28 deletions src/main/java/us/talabrek/ultimateskyblock/uSkyBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
import org.bukkit.block.BlockFace;
import org.bukkit.block.Chest;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Monster;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.bukkit.generator.ChunkGenerator;
Expand All @@ -34,7 +34,6 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.text.DateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
Expand Down Expand Up @@ -404,7 +403,6 @@ public boolean restartPlayerIsland(final Player player, final Location next) {
islandLogic.clearIsland(next);
createIsland(player, next);
changePlayerBiome(player, "OCEAN");
islandLogic.reloadIsland(next);
next.setY((double) Settings.island_height);
setNewPlayerIsland(player, next);
clearPlayerInventory(player);
Expand Down Expand Up @@ -444,13 +442,18 @@ public void clearPlayerInventory(Player player) {
}

private void clearEntitiesNearPlayer(Player player) {
for (final Entity tempent : player.getNearbyEntities((double) (Settings.island_protectionRange / 2), 250.0, (double) (Settings.island_protectionRange / 2))) {
if (!(tempent instanceof Player)) {
tempent.remove();
for (final Entity entity : player.getNearbyEntities((double) (Settings.island_radius), 255.0, (double) (Settings.island_radius))) {
if (!validEntity(entity)) {
entity.remove();
}
}
}

private boolean validEntity(Entity entity) {
return (entity instanceof Player) ||
(entity.getFallDistance() == 0 && !(entity instanceof Monster));
}

public boolean devSetPlayerIsland(final Player sender, final Location l, final String player) {
if (!this.getActivePlayers().containsKey(player)) {
final PlayerInfo pi = new PlayerInfo(player);
Expand Down Expand Up @@ -1059,30 +1062,31 @@ private void protectWithWorldGuard(CommandSender sender, Player player, PlayerIn
private void createIsland(Player player, Location next) throws DataException, IOException, MaxChangedBlocksException {
boolean hasIslandNow = false;
if (getInstance().getSchemFile().length > 0 && Bukkit.getServer().getPluginManager().isPluginEnabled("WorldEdit")) {
String cSchem = "";
for (int i = 0; i < getSchemFile().length; ++i) {
if (!hasIslandNow) {
if (getInstance().getSchemFile()[i].getName().lastIndexOf(46) > 0) {
cSchem = getSchemFile()[i].getName().substring(0, getSchemFile()[i].getName().lastIndexOf(46));
} else {
cSchem = getSchemFile()[i].getName();
}
if (VaultHandler.checkPerk(player.getName(), "usb.schematic." + cSchem, skyBlockWorld) && WorldEditHandler.loadIslandSchematic(skyBlockWorld, getSchemFile()[i], next)) {
this.setChest(next, player);
hasIslandNow = true;
}
for (File schemFile : getSchemFile()) {
// First run-through - try to set the island the player has permission for.
String cSchem = schemFile.getName();
if (cSchem.lastIndexOf('.') > 0) {
cSchem = cSchem.substring(0, cSchem.lastIndexOf('.'));
}
if (VaultHandler.checkPerk(player.getName(), "usb.schematic." + cSchem, skyBlockWorld)
&& WorldEditHandler.loadIslandSchematic(player, skyBlockWorld, schemFile, next)) {
setChest(next, player);
hasIslandNow = true;
break;
}
}
if (!hasIslandNow) {
for (int i = 0; i < getSchemFile().length; ++i) {
if (getInstance().getSchemFile()[i].getName().lastIndexOf(46) > 0) {
cSchem = getSchemFile()[i].getName().substring(0, getSchemFile()[i].getName().lastIndexOf(46));
} else {
cSchem = getSchemFile()[i].getName();
for (File schemFile : getSchemFile()) {
// 2nd Run through, set the default set schematic (if found).
String cSchem = schemFile.getName();
if (cSchem.lastIndexOf('.') > 0) {
cSchem = cSchem.substring(0, cSchem.lastIndexOf('.'));
}
if (cSchem.equalsIgnoreCase(Settings.island_schematicName) && WorldEditHandler.loadIslandSchematic(skyBlockWorld, getSchemFile()[i], next)) {
if (cSchem.equalsIgnoreCase(Settings.island_schematicName)
&& WorldEditHandler.loadIslandSchematic(player, skyBlockWorld, schemFile, next)) {
this.setChest(next, player);
hasIslandNow = true;
break;
}
}
}
Expand Down Expand Up @@ -1430,22 +1434,24 @@ public Location findChestLocation(final Location loc) {
}
}
}
return null;
return loc;
}

private Location findNearestSpawnLocation(Location loc) {
World world = loc.getWorld();
int px = loc.getBlockX();
int pz = loc.getBlockZ();
int py = loc.getBlockY();
for (int dy = 0; dy <= 15; dy++) {
for (int dy = 1; dy <= 30; dy++) {
for (int dx = 1; dx <= 30; dx++) {
for (int dz = 1; dz <= 30; dz++) {
// Scans from the center and out
int x = px + (dx % 2 == 0 ? dx/2 : -dx/2);
int z = pz + (dz % 2 == 0 ? dz/2 : -dz/2);
int y = py + dy;
if (world.getBlockAt(x, y, z).getType() == Material.AIR && world.getBlockAt(x, y+1, z).getType() == Material.AIR) {
int y = py + (dy % 2 == 0 ? dy/2 : -dy/2);
if (world.getBlockAt(x, y, z).getType() == Material.AIR
&& world.getBlockAt(x, y+1, z).getType() == Material.AIR
&& world.getBlockAt(x, y-1, z).getType().isSolid()) {
// look at the old location
Location spawnLocation = new Location(world, x, y, z);
Location d = loc.clone().subtract(spawnLocation);
Expand Down

0 comments on commit 1f1f502

Please sign in to comment.