Slight change to how world edit is loaded + make locate commands like vanilla (#38)

* make getWorldEdit() never null.

Signed-off-by: solonovamax <solonovamax@12oclockpoint.com>

* Locate commands work like vanilla

Signed-off-by: solonovamax <solonovamax@12oclockpoint.com>
This commit is contained in:
solonovamax
2020-12-10 16:57:05 -05:00
committed by GitHub
parent abd29c1cb4
commit 31361286b5
6 changed files with 79 additions and 22 deletions
+1
View File
@@ -10,6 +10,7 @@ import java.nio.file.StandardCopyOption
plugins { plugins {
java java
maven maven
idea
id("com.github.johnrengelman.shadow").version("6.1.0") id("com.github.johnrengelman.shadow").version("6.1.0")
} }
@@ -5,12 +5,19 @@ import com.dfsek.terra.api.gaea.command.WorldCommand;
import com.dfsek.terra.async.AsyncBiomeFinder; import com.dfsek.terra.async.AsyncBiomeFinder;
import com.dfsek.terra.biome.UserDefinedBiome; import com.dfsek.terra.biome.UserDefinedBiome;
import com.dfsek.terra.config.lang.LangUtil; import com.dfsek.terra.config.lang.LangUtil;
import com.dfsek.terra.generation.TerraChunkGenerator;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Collections; import java.util.Collections;
@@ -25,6 +32,7 @@ public class BiomeLocateCommand extends WorldCommand {
this.tp = teleport; this.tp = teleport;
} }
@SuppressWarnings("DuplicatedCode")
@Override @Override
public boolean execute(@NotNull Player sender, @NotNull Command command, @NotNull String label, @NotNull String[] args, World world) { public boolean execute(@NotNull Player sender, @NotNull Command command, @NotNull String label, @NotNull String[] args, World world) {
String id = args[0]; String id = args[0];
@@ -44,11 +52,14 @@ public class BiomeLocateCommand extends WorldCommand {
} }
Bukkit.getScheduler().runTaskAsynchronously(getMain(), new AsyncBiomeFinder(((Terra) getMain()).getWorld(world).getGrid(), b, sender.getLocation().clone().multiply((1D / ((Terra) getMain()).getTerraConfig().getBiomeSearchResolution())), 0, maxRadius, location -> { Bukkit.getScheduler().runTaskAsynchronously(getMain(), new AsyncBiomeFinder(((Terra) getMain()).getWorld(world).getGrid(), b, sender.getLocation().clone().multiply((1D / ((Terra) getMain()).getTerraConfig().getBiomeSearchResolution())), 0, maxRadius, location -> {
if(location != null) { if(location != null) {
LangUtil.send("command.biome.biome-found", sender, String.valueOf(location.getBlockX()), String.valueOf(location.getBlockZ())); ComponentBuilder cm = new ComponentBuilder(String.format("The nearest %s is at ", id.toLowerCase()))
if(tp) { .append(String.format("[%d, ~, %d]", location.getBlockX(), location.getBlockZ()), ComponentBuilder.FormatRetention.NONE)
Location l = new Location(sender.getWorld(), location.getX(), sender.getLocation().getY(), location.getZ()); .event(new ClickEvent(ClickEvent.Action.RUN_COMMAND, String.format("/minecraft:tp %s %d.0 %.2f %d.0", sender.getName(), location.getBlockX(), sender.getLocation().getY(), location.getBlockZ())))
Bukkit.getScheduler().runTask(getMain(), () -> sender.teleport(l)); .event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new BaseComponent[] {new TextComponent("Click to teleport")}))
} .color(ChatColor.GREEN)
.append(String.format(" (%.1f blocks away)", location.add(new Vector(0, sender.getLocation().getY(), 0)).distance(sender.getLocation().toVector())), ComponentBuilder.FormatRetention.NONE);
sender.spigot().sendMessage(cm.create());
// LangUtil.send("command.biome.biome-found", sender, String.valueOf(location.getBlockX()), String.valueOf(location.getBlockZ()));
} else LangUtil.send("command.biome.unable-to-locate", sender); } else LangUtil.send("command.biome.unable-to-locate", sender);
}, (Terra) getMain())); }, (Terra) getMain()));
return true; return true;
@@ -5,12 +5,18 @@ import com.dfsek.terra.api.gaea.command.WorldCommand;
import com.dfsek.terra.async.AsyncStructureFinder; import com.dfsek.terra.async.AsyncStructureFinder;
import com.dfsek.terra.config.lang.LangUtil; import com.dfsek.terra.config.lang.LangUtil;
import com.dfsek.terra.generation.items.TerraStructure; import com.dfsek.terra.generation.items.TerraStructure;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Collections; import java.util.Collections;
@@ -26,10 +32,12 @@ public class LocateCommand extends WorldCommand {
this.tp = tp; this.tp = tp;
} }
@SuppressWarnings("DuplicatedCode")
@Override @Override
public boolean execute(@NotNull Player sender, @NotNull Command command, @NotNull String label, @NotNull String[] args, World world) { public boolean execute(@NotNull Player sender, @NotNull Command command, @NotNull String label, @NotNull String[] args, World world) {
String id = args[0]; String id = args[0];
int maxRadius; int maxRadius;
try { try {
maxRadius = Integer.parseInt(args[1]); maxRadius = Integer.parseInt(args[1]);
} catch(NumberFormatException e) { } catch(NumberFormatException e) {
@@ -46,13 +54,15 @@ public class LocateCommand extends WorldCommand {
Bukkit.getScheduler().runTaskAsynchronously(getMain(), new AsyncStructureFinder(((Terra) getMain()).getWorld(world).getGrid(), s, sender.getLocation(), 0, maxRadius, (location) -> { Bukkit.getScheduler().runTaskAsynchronously(getMain(), new AsyncStructureFinder(((Terra) getMain()).getWorld(world).getGrid(), s, sender.getLocation(), 0, maxRadius, (location) -> {
if(sender.isOnline()) { if(sender.isOnline()) {
if(location != null) { if(location != null) {
sender.sendMessage("Located structure at (" + location.getBlockX() + ", " + location.getBlockZ() + ")."); ComponentBuilder cm = new ComponentBuilder(String.format("The nearest %s is at ", id.toLowerCase()))
if(tp) { .append(String.format("[%d, ~, %d]", location.getBlockX(), location.getBlockZ()), ComponentBuilder.FormatRetention.NONE)
int finalX = location.getBlockX(); .event(new ClickEvent(ClickEvent.Action.RUN_COMMAND, String.format("/minecraft:tp %s %d.0 %.2f %d.0", sender.getName(), location.getBlockX(), sender.getLocation().getY(), location.getBlockZ())))
int finalZ = location.getBlockZ(); .event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new BaseComponent[] {new TextComponent("Click to teleport")}))
Bukkit.getScheduler().runTask(getMain(), () -> sender.teleport(new Location(sender.getWorld(), finalX, sender.getLocation().getY(), finalZ))); .color(ChatColor.GREEN)
} .append(String.format(" (%.1f blocks away)", location.add(new Vector(0, sender.getLocation().getY(), 0)).distance(sender.getLocation().toVector())), ComponentBuilder.FormatRetention.NONE);
} else sender.sendMessage("Unable to locate structure. "); sender.spigot().sendMessage(cm.create());
} else
sender.sendMessage("Unable to locate structure. ");
} }
}, (Terra) getMain())); }, (Terra) getMain()));
return true; return true;
@@ -48,7 +48,8 @@ public class StructurePopulator extends BlockPopulator {
structure: structure:
for(TerraStructure conf : config.getStructures()) { for(TerraStructure conf : config.getStructures()) {
Location spawn = conf.getSpawn().getNearestSpawn(cx + 8, cz + 8, world.getSeed()).toLocation(world); Location spawn = conf.getSpawn().getNearestSpawn(cx + 8, cz + 8, world.getSeed()).toLocation(world);
if(!((UserDefinedBiome) grid.getBiome(spawn)).getConfig().getStructures().contains(conf)) continue; if(!((UserDefinedBiome) grid.getBiome(spawn)).getConfig().getStructures().contains(conf))
continue;
Random r2 = new FastRandom(spawn.hashCode()); Random r2 = new FastRandom(spawn.hashCode());
Structure struc = conf.getStructures().get(r2); Structure struc = conf.getStructures().get(r2);
Rotation rotation = Rotation.fromDegrees(r2.nextInt(4) * 90); Rotation rotation = Rotation.fromDegrees(r2.nextInt(4) * 90);
@@ -0,0 +1,22 @@
package com.dfsek.terra.util.structure;
public class WorldEditNotFoundException extends RuntimeException {
public WorldEditNotFoundException() {
}
public WorldEditNotFoundException(String message) {
super(message);
}
public WorldEditNotFoundException(String message, Throwable cause) {
super(message, cause);
}
public WorldEditNotFoundException(Throwable cause) {
super(cause);
}
public WorldEditNotFoundException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
@@ -10,13 +10,16 @@ import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
public final class WorldEditUtil { public final class WorldEditUtil {
public static Location[] getSelectionLocations(Player sender) { public static Location[] getSelectionLocations(Player sender) {
WorldEditPlugin we = WorldEditUtil.getWorldEdit(); WorldEditPlugin we;
if(we == null) { try {
we = WorldEditUtil.getWorldEdit();
} catch(WorldEditNotFoundException e) {
sender.sendMessage("WorldEdit is not installed! Please install WorldEdit before attempting to export structures."); sender.sendMessage("WorldEdit is not installed! Please install WorldEdit before attempting to export structures.");
return null; throw e;
} }
Region selection; Region selection;
try { try {
@@ -36,18 +39,27 @@ public final class WorldEditUtil {
return new Location[] {l1, l2}; return new Location[] {l1, l2};
} }
/**
* Gets an instance of the WorldEditPlugin class.
*
* @return The world edit plugin instance.
* @throws WorldEditNotFoundException Thrown when worldedit cannot be found.
*/
@NotNull
public static WorldEditPlugin getWorldEdit() { public static WorldEditPlugin getWorldEdit() {
Plugin p = Bukkit.getServer().getPluginManager().getPlugin("WorldEdit"); Plugin p = Bukkit.getServer().getPluginManager().getPlugin("WorldEdit");
if(p instanceof WorldEditPlugin) return (WorldEditPlugin) p; if(p instanceof WorldEditPlugin) return (WorldEditPlugin) p;
Bukkit.getLogger().severe("[Terra] a command requiring WorldEdit was executed, but WorldEdit was not detected!"); Bukkit.getLogger().severe("[Terra] a command requiring WorldEdit was executed, but WorldEdit was not detected!");
return null; throw new WorldEditNotFoundException("Could not find World Edit!");
} }
public static Location[] getSelectionPositions(Player sender) { public static Location[] getSelectionPositions(Player sender) {
WorldEditPlugin we = WorldEditUtil.getWorldEdit(); WorldEditPlugin we;
if(we == null) { try {
we = WorldEditUtil.getWorldEdit();
} catch(WorldEditNotFoundException e) {
sender.sendMessage("WorldEdit is not installed! Please install WorldEdit before attempting to export structures."); sender.sendMessage("WorldEdit is not installed! Please install WorldEdit before attempting to export structures.");
return null; throw e;
} }
CuboidRegion selection; CuboidRegion selection;
try { try {