diff --git a/sponge/pom.xml b/sponge/pom.xml index 8a26895..42bed31 100644 --- a/sponge/pom.xml +++ b/sponge/pom.xml @@ -94,12 +94,15 @@ - war-repo - http://ci.tommytony.com/plugin/repository/everything/ - - - sponge-repo - http://repo.spongepowered.org/Sponge/maven/ + sponge-maven-repo + Sponge maven repo + http://repo.spongepowered.org/maven + + true + + + true + @@ -113,7 +116,8 @@ org.spongepowered spongeapi - 1.0 + 2.1-SNAPSHOT + provided org.xerial diff --git a/sponge/src/main/java/com/tommytony/war/WarConfig.java b/sponge/src/main/java/com/tommytony/war/WarConfig.java index 7e35b9a..f92979f 100644 --- a/sponge/src/main/java/com/tommytony/war/WarConfig.java +++ b/sponge/src/main/java/com/tommytony/war/WarConfig.java @@ -105,7 +105,7 @@ public class WarConfig implements Closeable { UUID playerId = UUID.fromString(result.getString(1)); if (playerId == null) continue; - Optional player = plugin.getGame().getPlayer(playerId); + Optional player = plugin.getGame().getServer().getPlayer(playerId); if (player.isPresent()) makers.add(player.get()); } @@ -137,7 +137,7 @@ public class WarConfig implements Closeable { private final Class dataType; private final Object defaultValue; - private WarSetting(Class dataType, Object defaultValue) { + WarSetting(Class dataType, Object defaultValue) { this.dataType = dataType; this.defaultValue = defaultValue; } diff --git a/sponge/src/main/java/com/tommytony/war/WarPlugin.java b/sponge/src/main/java/com/tommytony/war/WarPlugin.java index 4da4edf..d95e0a0 100644 --- a/sponge/src/main/java/com/tommytony/war/WarPlugin.java +++ b/sponge/src/main/java/com/tommytony/war/WarPlugin.java @@ -1,39 +1,48 @@ package com.tommytony.war; import com.google.common.base.Optional; +import com.google.inject.Inject; +import com.tommytony.war.command.WarConfigCommand; import com.tommytony.war.command.WarzoneCommand; import com.tommytony.war.zone.Warzone; import org.slf4j.Logger; import org.spongepowered.api.Game; +import org.spongepowered.api.event.Subscribe; import org.spongepowered.api.event.state.PreInitializationEvent; import org.spongepowered.api.event.state.ServerStartedEvent; import org.spongepowered.api.event.state.ServerStartingEvent; import org.spongepowered.api.plugin.Plugin; -import org.spongepowered.api.util.event.Subscribe; +import org.spongepowered.api.service.config.DefaultConfig; import java.io.File; import java.io.FileNotFoundException; import java.sql.SQLException; +import java.util.HashMap; import java.util.Map; @Plugin(id = "war", name = "War", version = "2.0-SNAPSHOT") public class WarPlugin { private Game game; + + @Inject private Logger logger; + + @Inject + @DefaultConfig(sharedRoot = false) private File dataDir; + private WarConfig config; private Map zones; @Subscribe public void onConstruction(PreInitializationEvent event) throws InstantiationException { game = event.getGame(); - logger = event.getPluginLog(); - dataDir = event.getConfigurationDirectory(); try { Class.forName("com.tommytony.war.sqlite.JDBC").newInstance(); } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { throw new InstantiationException("Failed to load SQLite database"); } + zones = new HashMap<>(); } @Subscribe @@ -51,6 +60,7 @@ public class WarPlugin { public void onStart(ServerStartedEvent event) { // register commands game.getCommandDispatcher().register(this, new WarzoneCommand(this), "warzone", "zone"); + game.getCommandDispatcher().register(this, new WarConfigCommand(this), "warcfg", "warconfig"); } public Game getGame() { diff --git a/sponge/src/main/java/com/tommytony/war/command/WarConfigCommand.java b/sponge/src/main/java/com/tommytony/war/command/WarConfigCommand.java index 12ddf4f..e4627ad 100644 --- a/sponge/src/main/java/com/tommytony/war/command/WarConfigCommand.java +++ b/sponge/src/main/java/com/tommytony/war/command/WarConfigCommand.java @@ -1,133 +1,74 @@ package com.tommytony.war.command; import com.google.common.base.Optional; +import com.google.common.collect.ImmutableList; import com.tommytony.war.WarConfig; import com.tommytony.war.WarPlugin; import org.spongepowered.api.entity.player.Player; -import org.spongepowered.api.service.permission.Subject; +import org.spongepowered.api.text.Text; +import org.spongepowered.api.text.Texts; import org.spongepowered.api.util.command.CommandCallable; import org.spongepowered.api.util.command.CommandException; +import org.spongepowered.api.util.command.CommandResult; import org.spongepowered.api.util.command.CommandSource; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; public class WarConfigCommand implements CommandCallable { + + private final Optional desc = Optional.of((Text) Texts.of("View/modify War config")); + private final Optional help = Optional.of((Text) Texts.of("Allows viewing of the server config or changing various settings.")); + private final Text usage = (Text) Texts.of("[-p] setting value"); + private final WarPlugin plugin; public WarConfigCommand(WarPlugin plugin) { this.plugin = plugin; } - /** - * Execute the command based on input arguments. - *

- *

The implementing class must perform the necessary permission - * checks.

- * - * @param source The caller of the command - * @param arguments The raw arguments for this command - * @param parents A stack of parent commands, where the first entry is - * the root command - * @return Whether a command was processed - * @throws org.spongepowered.api.util.command.CommandException Thrown on a command error - */ @Override - public boolean call(CommandSource source, String arguments, List parents) throws CommandException { - if (!testPermission(source)) { - source.sendMessage("You do not have permission for this command."); - return true; - } - source.sendMessage("you do have permission"); - return false; + public CommandResult process(CommandSource source, String arguments) throws CommandException { + throw new UnsupportedOperationException("Not implemented yet"); } - /** - * Test whether this command can probably be executed by the given source. - *

- *

If implementations are unsure if the command can be executed by - * the source, {@code true} should be returned. Return values of this method - * may be used to determine whether this command is listed in command - * listings.

- * - * @param source The caller of the command - * @return Whether permission is (probably) granted - */ - @Override - public boolean testPermission(CommandSource source) { - if (source instanceof Player) { - try { - if (plugin.getConfig().getZoneMakers().contains(source)) { - source.sendMessage("You are a zone maker."); - return true; - } - } catch (SQLException e) { - plugin.getLogger().error("Loading zone makers for testing permission", e); - } - } - if (source instanceof Subject && ((Subject) source).isPermitted("war.admin.config")) { - source.sendMessage("You are a war admin."); - return true; - } - if (!(source instanceof Player) && !(source instanceof Subject)) { - source.sendMessage("You are console or something."); - return true; - } - return false; - } - - /** - * Get a short one-line description of this command. - * - * @return A description, if available - */ - @Override - public Optional getShortDescription() { - return Optional.of("View/modify war config"); - } - - /** - * Get a longer help text about this command. - * - * @return A help text, if available - */ - @Override - public Optional getHelp() { - return Optional.of("Allows viewing of the war server config or changing various settings."); - } - - /** - * Get the usage string of this command. - *

- *

A usage string may look like - * {@code [-w <world>] <var1> <var2>}.

- * - * @return A usage string - */ - @Override - public String getUsage() { - return "[-p] setting:value..."; - } - - /** - * Get a list of suggestions based on input. - *

- *

If a suggestion is chosen by the user, it will replace the last - * word.

- * - * @param source The command source - * @param arguments The arguments entered up to this point - * @return A list of suggestions - * @throws org.spongepowered.api.util.command.CommandException Thrown if there was a parsing error - */ @Override public List getSuggestions(CommandSource source, String arguments) throws CommandException { - ArrayList suggestions = new ArrayList<>(); + ImmutableList.Builder list = ImmutableList.builder(); for (WarConfig.WarSetting setting : WarConfig.WarSetting.values()) { if (setting.name().toLowerCase().startsWith(arguments.toLowerCase())) - suggestions.add(setting.name().toLowerCase() + ":"); + list.add(setting.name().toLowerCase() + ":"); } - return suggestions; + return list.build(); + } + + @Override + public boolean testPermission(CommandSource source) { + try { + if (source instanceof Player && plugin.getConfig().getZoneMakers().contains(source)) { + return true; + } + } catch (SQLException e) { + plugin.getLogger().error("Loading zone makers for testing permission", e); + } + if (source.hasPermission("war.config")) { + return true; + } + return false; + } + + @Override + public Optional getShortDescription(CommandSource source) { + return desc; + } + + @Override + public Optional getHelp(CommandSource source) { + return help; + } + + @Override + public Text getUsage(CommandSource source) { + return usage; } } diff --git a/sponge/src/main/java/com/tommytony/war/command/WarzoneCommand.java b/sponge/src/main/java/com/tommytony/war/command/WarzoneCommand.java index a4bbbef..ecfbd2f 100644 --- a/sponge/src/main/java/com/tommytony/war/command/WarzoneCommand.java +++ b/sponge/src/main/java/com/tommytony/war/command/WarzoneCommand.java @@ -1,126 +1,82 @@ package com.tommytony.war.command; import com.google.common.base.Optional; +import com.google.common.collect.ImmutableList; import com.tommytony.war.WarPlugin; import com.tommytony.war.zone.Warzone; import org.spongepowered.api.entity.player.Player; +import org.spongepowered.api.text.Text; +import org.spongepowered.api.text.Texts; import org.spongepowered.api.util.command.CommandCallable; import org.spongepowered.api.util.command.CommandException; +import org.spongepowered.api.util.command.CommandResult; import org.spongepowered.api.util.command.CommandSource; -import java.util.ArrayList; import java.util.List; /** * Teleport to warzone. */ public class WarzoneCommand implements CommandCallable { + + private final Optional desc = Optional.of((Text) Texts.of("Teleport to a zone")); + private final Optional help = Optional.of((Text) Texts.of("Teleport to a warzone, or join automatically.")); + private final Text usage = (Text) Texts.of(""); + private WarPlugin plugin; public WarzoneCommand(WarPlugin plugin) { this.plugin = plugin; } - /** - * Execute the command based on input arguments. - *

- *

The implementing class must perform the necessary permission - * checks.

- * - * @param source The caller of the command - * @param arguments The raw arguments for this command - * @param parents A stack of parent commands, where the first entry is - * the root command - * @return Whether a command was processed - * @throws org.spongepowered.api.util.command.CommandException Thrown on a command error - */ @Override - public boolean call(CommandSource source, String arguments, List parents) throws CommandException { - if (!(source instanceof Player)) { - return false; + public CommandResult process(CommandSource commandSource, String s) throws CommandException { + if (!(commandSource instanceof Player)) { + return CommandResult.empty(); } - String[] argv = arguments.split(" "); + String[] argv = s.split(" "); if (argv.length < 1) { - return false; + return CommandResult.empty(); } String zoneName = argv[0]; Optional zone = plugin.getZone(zoneName); if (!zone.isPresent()) { - return false; + return CommandResult.empty(); } - Player player = (Player) source; - player.teleport(zone.get().getTeleport()); + Player player = (Player) commandSource; + player.setLocation(zone.get().getTeleport()); - return true; + return CommandResult.success(); } - /** - * Test whether this command can probably be executed by the given source. - *

- *

If implementations are unsure if the command can be executed by - * the source, {@code true} should be returned. Return values of this method - * may be used to determine whether this command is listed in command - * listings.

- * - * @param source The caller of the command - * @return Whether permission is (probably) granted - */ @Override - public boolean testPermission(CommandSource source) { - return true; - } - - /** - * Get a short one-line description of this command. - * - * @return A description, if available - */ - @Override - public Optional getShortDescription() { - return Optional.of("Teleport to a zone"); - } - - /** - * Get a longer help text about this command. - * - * @return A help text, if available - */ - @Override - public Optional getHelp() { - return Optional.of("Use this command to teleport to a zone lobby"); - } - - /** - * Get the usage string of this command. - *

- *

A usage string may look like - * {@code [-w <world>] <var1> <var2>}.

- * - * @return A usage string - */ - @Override - public String getUsage() { - return ""; - } - - /** - * Get a list of suggestions based on input. - *

- *

If a suggestion is chosen by the user, it will replace the last - * word.

- * - * @param source The command source - * @param arguments The arguments entered up to this point - * @return A list of suggestions - * @throws org.spongepowered.api.util.command.CommandException Thrown if there was a parsing error - */ - @Override - public List getSuggestions(CommandSource source, String arguments) throws CommandException { - ArrayList suggestions = new ArrayList<>(); - for (String zone : plugin.getZones().keySet()) { - if (zone.toLowerCase().startsWith(arguments.toLowerCase())) - suggestions.add(zone); + public List getSuggestions(CommandSource commandSource, String s) throws CommandException { + ImmutableList.Builder list = ImmutableList.builder(); + for (Warzone zone : plugin.getZones().values()) { + if (zone.getName().toLowerCase().startsWith(s.toLowerCase())) { + list.add(zone.getName()); + } } - return suggestions; + return list.build(); + } + + @Override + public boolean testPermission(CommandSource commandSource) { + return commandSource.hasPermission("war.teleport"); + } + + @Override + public Optional getShortDescription(CommandSource commandSource) { + return desc; + } + + @Override + public Optional getHelp(CommandSource commandSource) { + return help; + } + + @Override + public Text getUsage(CommandSource commandSource) { + return usage; } } diff --git a/sponge/src/main/java/com/tommytony/war/struct/Region.java b/sponge/src/main/java/com/tommytony/war/struct/Region.java deleted file mode 100644 index ecbf06f..0000000 --- a/sponge/src/main/java/com/tommytony/war/struct/Region.java +++ /dev/null @@ -1,103 +0,0 @@ -package com.tommytony.war.struct; - -import org.spongepowered.api.block.BlockLoc; -import org.spongepowered.api.math.Vector3d; -import org.spongepowered.api.world.Location; -import org.spongepowered.api.world.extent.BlockVolume; - -/** - * A selection of blocks in the world. Identified by two corners. - */ -public class Region implements BlockVolume { - /** - * One corner of the selection. - */ - private Location first; - /** - * The second corner of the selection. - */ - private Location second; - - public Region(Location first, Location second) { - this.first = first; - this.second = second; - } - - /** - * Calculate the minimum value of the selection. - * - * @return the minimum value. - */ - public Location getMin() { - return new Location(first.getExtent(), first.getPosition().min(second.getPosition())); - } - - /** - * Calculate the maximum value of the selection. - * - * @return the maximum value. - */ - public Location getMax() { - return new Location(first.getExtent(), first.getPosition().max(second.getPosition())); - } - - /** - * Get the size of the region in the X dimension. - * - * @return X dimension length. - */ - public int getSizeX() { - return getMax().getBlock().getX() - getMin().getBlock().getX(); - } - - /** - * Get the size of the region in the Y dimension. - * - * @return Y dimension length. - */ - public int getSizeY() { - return getMax().getBlock().getY() - getMin().getBlock().getY(); - } - - /** - * Get the size of the region in the Z dimension. - * - * @return Z dimension length. - */ - public int getSizeZ() { - return getMax().getBlock().getZ() - getMin().getBlock().getZ(); - } - - /** - * Get the total area of the region. - * - * @return region total area. - */ - public int getSize() { - return getSizeX() * getSizeY() * getSizeZ(); - } - - /** - * Get a representation of the block at the given position. - * - * @param position The position - * @return The block - */ - @Override - public BlockLoc getBlock(Vector3d position) { - return first.getExtent().getBlock(position); - } - - /** - * Get a representation of the block at the given position. - * - * @param x The X position - * @param y The Y position - * @param z The Z position - * @return The block - */ - @Override - public BlockLoc getBlock(int x, int y, int z) { - return first.getExtent().getBlock(x, y, z); - } -} diff --git a/sponge/src/main/java/com/tommytony/war/zone/Warzone.java b/sponge/src/main/java/com/tommytony/war/zone/Warzone.java index 85cab9f..e332d88 100644 --- a/sponge/src/main/java/com/tommytony/war/zone/Warzone.java +++ b/sponge/src/main/java/com/tommytony/war/zone/Warzone.java @@ -19,7 +19,7 @@ public class Warzone { private final String name; private final ZoneStorage db; private final ZoneConfig config; - private Location teleport; + private Location teleport; /** * Load or create a war zone from the war settings store. @@ -56,11 +56,11 @@ public class Warzone { return name; } - public Location getTeleport() { + public Location getTeleport() { try { Optional lobby = db.getPosition("lobby", Optional.absent()); if (lobby.isPresent()) - return lobby.get(); + return lobby.get(); else throw new RuntimeException("No teleport location found for zone " + name); } catch (SQLException e) { plugin.getLogger().error("Retrieving teleport", e); diff --git a/sponge/src/main/java/com/tommytony/war/zone/ZoneStorage.java b/sponge/src/main/java/com/tommytony/war/zone/ZoneStorage.java index 028514a..39fc80c 100644 --- a/sponge/src/main/java/com/tommytony/war/zone/ZoneStorage.java +++ b/sponge/src/main/java/com/tommytony/war/zone/ZoneStorage.java @@ -2,7 +2,6 @@ package com.tommytony.war.zone; import com.google.common.base.Optional; import com.tommytony.war.WarPlugin; -import org.spongepowered.api.math.Vectors; import org.spongepowered.api.world.Location; import org.spongepowered.api.world.World; @@ -86,10 +85,18 @@ public class ZoneStorage implements AutoCloseable { try (ResultSet resultSet = stmt.executeQuery()) { if (resultSet.next()) { World resultWorld; - if (world.isPresent()) resultWorld = world.get(); - else resultWorld = plugin.getGame().getWorld(resultSet.getString("world")); - return Optional.of(new Location(resultWorld, Vectors.create3d( - resultSet.getDouble("x"), resultSet.getDouble("y"), resultSet.getDouble("z")))); + if (world.isPresent()) { + resultWorld = world.get(); + } else { + Optional optwld = plugin.getGame().getServer().getWorld(resultSet.getString("world")); + if (optwld.isPresent()) { + resultWorld = optwld.get(); + } else { + return Optional.absent(); + } + } + return Optional.of(new Location(resultWorld, + resultSet.getDouble("x"), resultSet.getDouble("y"), resultSet.getDouble("z"))); } } }