Skip to content

Commit

Permalink
Fixed Toggle Areas for 1.13, now properly uses WorldEdit Clipboards r…
Browse files Browse the repository at this point in the history
…ather than reimplementing it.
  • Loading branch information
me4502 committed Aug 16, 2018
1 parent be26156 commit 52f2ff9
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 700 deletions.
90 changes: 45 additions & 45 deletions src/main/java/com/sk89q/craftbook/mechanics/area/Area.java
Expand Up @@ -15,12 +15,12 @@
import com.sk89q.squirrelid.resolver.HttpRepositoryService;
import com.sk89q.squirrelid.resolver.ProfileService;
import com.sk89q.util.yaml.YAMLProcessor;
import com.sk89q.worldedit.world.DataException;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.Action;
Expand Down Expand Up @@ -60,9 +60,9 @@ public void onSignChange(SignChangeEvent event) {

if (event.getLine(0).trim().isEmpty()) {
if (shortenNames && cbid.length() > 14)
event.setLine(0, ("~" + cbid).substring(0, 14));
event.setLine(0, ('~' + cbid).substring(0, 14));
else
event.setLine(0, "~" + cbid);
event.setLine(0, '~' + cbid);
}

if (event.getLine(1).equalsIgnoreCase("[Area]")) {
Expand Down Expand Up @@ -99,7 +99,7 @@ public void onRightClick(SignClickEvent event) {

if(event.getAction() != Action.RIGHT_CLICK_BLOCK) return;
CraftBookPlayer player = CraftBookPlugin.inst().wrapPlayer(event.getPlayer());
boolean save = false;
boolean save;

ChangedSign sign = event.getSign();

Expand All @@ -125,7 +125,7 @@ public void onRightClick(SignClickEvent event) {
save = sign.getLine(1).equals("[SaveArea]");

// toggle the area on or off
toggle(event.getClickedBlock(), sign, save);
toggle(sign, save);

event.setCancelled(true);
}
Expand All @@ -134,7 +134,7 @@ private static boolean isValidArea(String namespace, String areaOn, String areaO

if (CopyManager.isExistingArea(CraftBookPlugin.inst().getDataFolder(), namespace, areaOn)) {
if (areaOff == null || areaOff.isEmpty() || areaOff.equals("--")) return true;
if (CopyManager.isExistingArea(CraftBookPlugin.inst().getDataFolder(), namespace, areaOff)) return true;
return CopyManager.isExistingArea(CraftBookPlugin.inst().getDataFolder(), namespace, areaOff);
}
return false;
}
Expand All @@ -153,7 +153,7 @@ private static boolean isValidArea(ChangedSign sign) {
ProfileService resolver = HttpRepositoryService.forMinecraft();
Profile profile = resolver.findByName("player.getName()"); // May be null

namespace = "~" + CraftBookPlugin.inst().getUUIDMappings().getCBID(profile.getUniqueId());
namespace = '~' + CraftBookPlugin.inst().getUUIDMappings().getCBID(profile.getUniqueId());
CopyManager.renameNamespace(CraftBookPlugin.inst().getDataFolder(), originalNamespace, namespace);
sign.setLine(0, namespace);
} catch (Exception e) {
Expand Down Expand Up @@ -185,7 +185,7 @@ public void onBlockRedstoneChange(SourcedBlockRedstoneEvent event) {
if (!allowRedstone) return;
if (!SignUtil.isSign(event.getBlock())) return;

boolean save = false;
boolean save;

ChangedSign sign = CraftBookBukkitUtil.toChangedSign(event.getBlock());

Expand All @@ -197,54 +197,54 @@ public void onBlockRedstoneChange(SourcedBlockRedstoneEvent event) {
save = sign.getLine(1).equals("[SaveArea]");

// toggle the area
toggle(event.getBlock(), sign, save);
toggle(sign, save);
}

private static boolean toggle(Block signBlock, ChangedSign sign, boolean save) {
private static boolean toggle(ChangedSign sign, boolean save) {

if (!checkSign(sign)) return false;

try {
World world = CraftBookBukkitUtil.toSign(sign).getWorld();
String namespace = sign.getLine(0);
String id = StringUtils.replace(sign.getLine(2), "-", "").toLowerCase(Locale.ENGLISH);
String inactiveID = StringUtils.replace(sign.getLine(3), "-", "").toLowerCase(Locale.ENGLISH);

CuboidCopy copy;
BlockArrayClipboard copy;

if (checkToggleState(sign)) {

copy = CopyManager.getInstance().load(world, namespace, id);
copy = CopyManager.getInstance().load(namespace, id);
copy.getRegion().setWorld(BukkitAdapter.adapt(sign.getBlock().getWorld()));

// if this is a save area save it before toggling off
if (save) {
copy.copy();
CopyManager.getInstance().save(world, namespace, id, copy);
copy = CopyManager.getInstance().copy(copy.getRegion());
CopyManager.getInstance().save(namespace, id, copy);
}
// if we are toggling to the second area we dont clear the old area
if (!inactiveID.isEmpty() && !inactiveID.equals("--")) {
copy = CopyManager.getInstance().load(world, namespace, inactiveID);
copy.paste();
copy = CopyManager.getInstance().load(namespace, inactiveID);
CopyManager.getInstance().paste(copy);
} else {
copy.clear();
CopyManager.getInstance().clear(copy);
}
setToggledState(sign, false);
} else {

// toggle the area on
// if this is a save area save it before toggling off
if (save && !inactiveID.isEmpty() && !inactiveID.equals("--")) {
copy = CopyManager.getInstance().load(world, namespace, inactiveID);
copy.copy();
CopyManager.getInstance().save(world, namespace, inactiveID, copy);
copy = CopyManager.getInstance().load(namespace, inactiveID);
copy.getRegion().setWorld(BukkitAdapter.adapt(sign.getBlock().getWorld()));
copy = CopyManager.getInstance().copy(copy.getRegion());
CopyManager.getInstance().save(namespace, inactiveID, copy);
}

copy = CopyManager.getInstance().load(world, namespace, id);
copy.paste();
copy = CopyManager.getInstance().load(namespace, id);
CopyManager.getInstance().paste(copy);
setToggledState(sign, true);
}
return true;
} catch (CuboidCopyException | IOException | DataException e) {
} catch (IOException | WorldEditException e) {
CraftBookPlugin.logger().log(Level.SEVERE, "Failed to toggle Area: " + e.getMessage());
}
return false;
Expand All @@ -258,45 +258,45 @@ public static boolean toggleCold(ChangedSign sign) {
boolean save = sign.getLine(1).equalsIgnoreCase("[SaveArea]");

try {
World world = CraftBookBukkitUtil.toSign(sign).getWorld();
String namespace = sign.getLine(0);
String id = StringUtils.replace(sign.getLine(2), "-", "").toLowerCase(Locale.ENGLISH);
String inactiveID = StringUtils.replace(sign.getLine(3), "-", "").toLowerCase(Locale.ENGLISH);

CuboidCopy copy;
BlockArrayClipboard copy;

if (toggleOn) {

copy = CopyManager.getInstance().load(world, namespace, id);
copy = CopyManager.getInstance().load(namespace, id);
copy.getRegion().setWorld(BukkitAdapter.adapt(sign.getBlock().getWorld()));

// if this is a save area save it before toggling off
if (save) {
copy.copy();
CopyManager.getInstance().save(world, namespace, id, copy);
copy = CopyManager.getInstance().copy(copy.getRegion());
CopyManager.getInstance().save(namespace, id, copy);
}
// if we are toggling to the second area we dont clear the old area
if (!inactiveID.isEmpty() && !inactiveID.equals("--")) {
copy = CopyManager.getInstance().load(world, namespace, inactiveID);
copy.paste();
copy = CopyManager.getInstance().load(namespace, inactiveID);
CopyManager.getInstance().paste(copy);
} else {
copy.clear();
CopyManager.getInstance().clear(copy);
}
setToggledState(sign, false);
} else {

// toggle the area on
// if this is a save area save it before toggling off
if (save && !inactiveID.isEmpty() && !inactiveID.equals("--")) {
copy = CopyManager.getInstance().load(world, namespace, inactiveID);
copy.copy();
CopyManager.getInstance().save(world, namespace, inactiveID, copy);
} else
copy = CopyManager.getInstance().load(world, namespace, id);
copy.paste();
copy = CopyManager.getInstance().load(namespace, inactiveID);
copy.getRegion().setWorld(BukkitAdapter.adapt(sign.getBlock().getWorld()));
copy = CopyManager.getInstance().copy(copy.getRegion());
CopyManager.getInstance().save(namespace, inactiveID, copy);
} else {
copy = CopyManager.getInstance().load(namespace, id);
}
CopyManager.getInstance().paste(copy);
setToggledState(sign, true);
}
return true;
} catch (CuboidCopyException | IOException | DataException e) {
} catch (IOException | WorldEditException e) {
CraftBookPlugin.logger().log(Level.SEVERE, "Failed to cold toggle Area: " + e.getMessage());
}
return false;
Expand Down Expand Up @@ -330,7 +330,7 @@ private static void setToggledState(ChangedSign sign, boolean state) {
int toToggleOn = state ? 2 : 3;
int toToggleOff = state ? 3 : 2;
sign.setLine(toToggleOff, StringUtils.replace(sign.getLine(toToggleOff), "-", ""));
sign.setLine(toToggleOn, "-" + sign.getLine(toToggleOn) + "-");
sign.setLine(toToggleOn, '-' + sign.getLine(toToggleOn) + '-');
sign.update(false);
}

Expand Down
30 changes: 10 additions & 20 deletions src/main/java/com/sk89q/craftbook/mechanics/area/AreaCommands.java
Expand Up @@ -13,10 +13,10 @@
import com.sk89q.worldedit.IncompleteRegionException;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.bukkit.WorldEditPlugin;
import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.world.DataException;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.World;
Expand Down Expand Up @@ -67,7 +67,7 @@ public void saveArea(CommandContext context, CommandSender sender) throws Comman
throw new CommandException("Invalid namespace. Needs to be between 1 and 14 letters long.");

if (personal) {
namespace = "~" + namespace;
namespace = '~' + namespace;
}

id = context.getString(0);
Expand All @@ -76,10 +76,8 @@ public void saveArea(CommandContext context, CommandSender sender) throws Comman
throw new CommandException("Invalid area name. Needs to be between 1 and 13 letters long.");

try {
WorldEditPlugin worldEdit = CraftBookPlugin.plugins.getWorldEdit();

World world = ((Player) sender).getWorld();
Region sel = WorldEdit.getInstance().getSessionManager().findByName(sender.getName()).getSelection(BukkitAdapter.adapt(world));
com.sk89q.worldedit.world.World world = BukkitAdapter.adapt(((Player) sender).getWorld());
Region sel = WorldEdit.getInstance().getSessionManager().findByName(sender.getName()).getSelection(world);
if(sel == null) {
sender.sendMessage(ChatColor.RED + "You have not made a selection!");
return;
Expand All @@ -97,7 +95,7 @@ public void saveArea(CommandContext context, CommandSender sender) throws Comman
// Check to make sure that a user doesn't have too many toggle
// areas (to prevent flooding the server with files)
if (Area.instance.maxAreasPerUser >= 0 && !namespace.equals("global") && !player.hasPermission("craftbook.mech.area.bypass-limit")) {
int count = CopyManager.meetsQuota(world, namespace, id,
int count = CopyManager.meetsQuota(namespace, id,
Area.instance.maxAreasPerUser);

if (count > -1) {
Expand All @@ -107,32 +105,24 @@ public void saveArea(CommandContext context, CommandSender sender) throws Comman
}

// Copy
CuboidCopy copy;

if (Area.instance.useSchematics) {
copy = new MCEditCuboidCopy(min, size, world);
} else {
copy = new FlatCuboidCopy(min, size, world);
}

copy.copy();
BlockArrayClipboard copy = CopyManager.getInstance().copy(sel);

plugin.getServer().getLogger().info(player.getName() + " saving toggle area with folder '" + namespace +
"' and ID '" + id + "'.");

// Save
try {
CopyManager.getInstance().save(world, namespace, id.toLowerCase(Locale.ENGLISH), copy);
CopyManager.getInstance().save(namespace, id.toLowerCase(Locale.ENGLISH), copy);
player.print("Area saved as '" + id + "' under the '" + namespace + "' namespace.");
} catch (IOException e) {
player.printError("Could not save area: " + e.getMessage());
} catch (DataException e) {
player.print(e.getMessage());
}
} catch (NoClassDefFoundError e) {
throw new CommandException("WorldEdit.jar does not exist in plugins/, or is outdated. (Or you are using an outdated version of CraftBook)");
} catch (IncompleteRegionException e) {
throw new CommandException("Invalid selection");
} catch (WorldEditException e) {
player.printError(e.getMessage());
}
}

Expand Down

0 comments on commit 52f2ff9

Please sign in to comment.