Added info command

+ Added objects for location models that have unique info strings
+ Fixed add_resident
pull/2/head
Joey Hines 2020-01-07 17:48:48 -06:00
parent 6cf0cf5976
commit e7bddf51cb
10 changed files with 228 additions and 9 deletions

View File

@ -50,21 +50,22 @@ public class AddResidentCommand extends GeoffreyCommand {
}
private class CommandCallback extends GeoffreyAPICallback {
private String newResidentName;
CommandCallback(CommandSender sender, String new_resident_name) {
CommandCallback(CommandSender sender, String newResidentName) {
super(sender);
errors.put("ResidentNotFoundError", "Ain't no one in this darn database named" + new_resident_name + "you goob");
errors.put("IsResidentError", new_resident_name + " is already a resident, stop having amosia.");
errors.put("ResidentNotFoundError", "Ain't no one in this darn database named" + newResidentName + "you goob");
errors.put("IsResidentError", newResidentName + " is already a resident, stop having amosia.");
errors.put("LocationLookUpError", "You do not have a town by that name you ding dong goober.");
this.newResidentName = newResidentName;
}
@Override
public void invoke(String s) {
try {
JsonElement json = parseJSON(s);
GeoffreyPlayer player = new GeoffreyPlayer(json.getAsJsonObject());
String msg = ChatColor.GREEN + player.getUsername() + "has been added to your town";
String msg = ChatColor.GREEN + newResidentName + "has been added to your town";
commandSender.sendMessage(msg);
}
catch (GeoffreyCommandError e) {

View File

@ -0,0 +1,103 @@
package com.zerohighdef.geoffrey.Commands;
import com.google.gson.JsonElement;
import com.zerohighdef.geoffrey.GeoffreyMC;
import com.zerohighdef.geoffrey.Models.GeoffreyLocation;
import com.zerohighdef.geoffrey.Models.GeoffreyMarket;
import com.zerohighdef.geoffrey.Models.GeoffreyShop;
import com.zerohighdef.geoffrey.Models.GeoffreyTown;
import com.zerohighdef.geoffrey.Objects.GeoffreyAPICallback;
import com.zerohighdef.geoffrey.Objects.GeoffreyCommand;
import com.zerohighdef.geoffrey.Objects.GeoffreyCommandError;
import com.zerohighdef.geoffrey.Objects.GeoffreyUtil;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;
public class InfoCommand extends GeoffreyCommand {
public InfoCommand(GeoffreyMC plugin) {
super(plugin);
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (args.length > 0) {
try {
String locationName = GeoffreyUtil.parseArgs(args).get(0);
Map<String, String> params = new HashMap<String, String>();
params.put("location_name", locationName);
RunCommand("info", params, Method.GET, new CommandCallback(sender, locationName));
}
catch (ParseException e) {
sender.sendMessage(ChatColor.RED + e.getMessage());
}
}
else {
sender.sendMessage(ChatColor.RED + "You must specify a location name.");
return false;
}
return true;
}
private class CommandCallback extends GeoffreyAPICallback {
private String locationName;
CommandCallback(CommandSender sender, String locationName) {
super(sender);
errors.put("LocationLookUpError", "There are no locations that match " + locationName);
this.locationName = locationName;
}
@Override
public void invoke(String s) {
try {
JsonElement json = parseJSON(s);
String locationType = json.getAsJsonObject().get("type").getAsString();
String infoString;
String url = plugin.getBaseURL();
if (locationType.compareToIgnoreCase("shop") == 0) {
GeoffreyShop shop = new GeoffreyShop(json.getAsJsonObject());
infoString = shop.getInfoString();
url += shop.getLink();
}
else if (locationType.compareToIgnoreCase("town") == 0) {
GeoffreyTown town = new GeoffreyTown(json.getAsJsonObject());
infoString = town.getInfoString();
url += town.getLink();
}
else if (locationType.compareToIgnoreCase("market") == 0) {
GeoffreyMarket market = new GeoffreyMarket(json.getAsJsonObject());
infoString = market.getInfoString();
url += market.getLink();
}
else {
GeoffreyLocation location = new GeoffreyLocation(json.getAsJsonObject());
infoString = location.getInfoString();
url += location.getLink();
}
TextComponent message = new TextComponent();
message.setText(infoString);
message.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, url));
message.setColor(net.md_5.bungee.api.ChatColor.GREEN);
commandSender.spigot().sendMessage(message);
}
catch (GeoffreyCommandError e) {
return;
}
}
}
}

View File

@ -1,8 +1,6 @@
package com.zerohighdef.geoffrey;
import com.zerohighdef.geoffrey.Commands.*;
import com.zerohighdef.geoffrey.Models.GeoffreyItemListing;
import com.zerohighdef.geoffrey.Models.GeoffreyLocation;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
@ -11,6 +9,7 @@ import java.util.logging.Logger;
public final class GeoffreyMC extends JavaPlugin {
private String APIToken;
private String baseAPIURL;
private String baseURL;
private static FileConfiguration config;
private Logger log = Logger.getLogger( "Minecraft" );
@ -23,7 +22,9 @@ public final class GeoffreyMC extends JavaPlugin {
if (isEnabled()) {
APIToken = config.getString("geoffrey_api.key");
baseAPIURL = config.getString("geoffrey_api.api_base_url");
baseURL = config.getString("geoffrey_api.base_url");
FindCommand findCommand = new FindCommand(this);
this.getCommand("geoffrey_find").setExecutor(findCommand);
@ -79,6 +80,9 @@ public final class GeoffreyMC extends JavaPlugin {
FindFarmCommand findFarmCommand = new FindFarmCommand(this);
this.getCommand("geoffrey_find_farm").setExecutor(findFarmCommand);
InfoCommand infoCommand = new InfoCommand(this);
this.getCommand("geoffrey_info").setExecutor(infoCommand);
}
}
@ -103,6 +107,10 @@ public final class GeoffreyMC extends JavaPlugin {
return APIToken;
}
public String getBaseAPIURL() {
return baseAPIURL;
}
public String getBaseURL() {
return baseURL;
}

View File

@ -2,6 +2,7 @@ package com.zerohighdef.geoffrey.Models;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.bukkit.ChatColor;
public class GeoffreyLocation {
private int xCoord;
@ -10,6 +11,7 @@ public class GeoffreyLocation {
private GeoffreyPlayer owner;
private String dimension;
private String tunnelString = null;
private String link = null;
public GeoffreyLocation(JsonObject locationJSON) {
this.xCoord = locationJSON.get("x_coord").getAsInt();
@ -17,6 +19,7 @@ public class GeoffreyLocation {
this.locationName = locationJSON.get("name").getAsString();
this.owner = new GeoffreyPlayer(locationJSON.getAsJsonArray("owner").getAsJsonArray().get(0).getAsJsonObject());
this.dimension = locationJSON.get("dimension").getAsString();
this.link = locationJSON.get("link").getAsString();
JsonElement tunnelElement = locationJSON.get("tunnel");
@ -47,6 +50,10 @@ public class GeoffreyLocation {
return locationString;
}
public String getInfoString() {
return ChatColor.BOLD + getFormattedLocationString() + ChatColor.RESET;
}
public int getxCoord() {
return xCoord;
}
@ -70,4 +77,8 @@ public class GeoffreyLocation {
public String getTunnel() {
return tunnelString;
}
public String getLink() {
return link;
}
}

View File

@ -0,0 +1,37 @@
package com.zerohighdef.geoffrey.Models;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import java.util.LinkedList;
public class GeoffreyMarket extends GeoffreyLocation{
private LinkedList<GeoffreyLocation> shops;
public GeoffreyMarket(JsonObject locationJSON) {
super(locationJSON);
this.shops = new LinkedList<>();
for (JsonElement json : locationJSON.get("shops").getAsJsonArray()) {
this.shops.add(new GeoffreyLocation(json.getAsJsonObject()));
}
}
public GeoffreyMarket(int xCoord, int zCoord, String locationName, GeoffreyPlayer owner, String dimension, LinkedList<GeoffreyLocation> shops) {
super(xCoord, zCoord, locationName, owner, dimension);
this.shops = shops;
}
@Override
public String getInfoString() {
StringBuilder infoString = new StringBuilder();
infoString.append(super.getInfoString());
infoString.append("\nShops:");
for (GeoffreyLocation shop: shops) {
infoString.append("\n");
infoString.append(shop.getFormattedLocationString());
}
return infoString.toString();
}
}

View File

@ -1,9 +1,11 @@
package com.zerohighdef.geoffrey.Models;
import com.google.common.collect.Iterables;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import java.awt.event.ItemListener;
import java.util.LinkedList;
import java.util.List;
@ -35,6 +37,20 @@ public class GeoffreyShop extends GeoffreyLocation{
return items;
}
@Override
public String getInfoString() {
StringBuilder infoString = new StringBuilder();
infoString.append(super.getInfoString());
infoString.append("\nItems:");
for (GeoffreyItemListing item: Iterables.limit(items, 5)) {
infoString.append("\n");
infoString.append(item.getFormattedItemListing());
}
return infoString.toString();
}
public GeoffreyItemListing getFirstItem() {
return items.get(0);
}

View File

@ -0,0 +1,38 @@
package com.zerohighdef.geoffrey.Models;
import com.google.common.collect.Iterables;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import java.util.LinkedList;
public class GeoffreyTown extends GeoffreyLocation {
private LinkedList<GeoffreyPlayer> residents;
public GeoffreyTown(JsonObject locationJSON) {
super(locationJSON);
this.residents = new LinkedList<>();
for (JsonElement json : locationJSON.get("residents").getAsJsonArray()) {
this.residents.add(new GeoffreyPlayer(json.getAsJsonObject()));
}
}
public GeoffreyTown(int xCoord, int zCoord, String locationName, GeoffreyPlayer owner, String dimension, LinkedList <GeoffreyPlayer> residents) {
super(xCoord, zCoord, locationName, owner, dimension);
this.residents = residents;
}
@Override
public String getInfoString() {
StringBuilder locationString = new StringBuilder();
locationString.append(super.getInfoString());
locationString.append("\nResidents:");
for (GeoffreyPlayer resident: Iterables.limit(residents, 5)) {
locationString.append("\n");
locationString.append(resident.getUsername());
}
return locationString.toString();
}
}

View File

@ -10,7 +10,7 @@ import xyz.etztech.core.web.ICallback;
import java.util.Map;
public class GeoffreyCommand implements CommandExecutor {
private GeoffreyMC plugin;
protected GeoffreyMC plugin;
protected enum Method {
GET, POST
@ -21,7 +21,7 @@ public class GeoffreyCommand implements CommandExecutor {
}
protected void RunCommand(String commandName, Map<String, String> params, Method requestType, ICallback callback) {
String url = plugin.getBaseURL() + "/command/" + commandName + "/";
String url = plugin.getBaseAPIURL() + "/command/" + commandName + "/";
params.put("api", plugin.getAPIToken());
if (requestType == Method.GET) {

View File

@ -1,4 +1,5 @@
# GeoffreyAPI Settings
geoffrey_api:
key: ''
api_base_url: ''
base_url: ''

View File

@ -114,6 +114,10 @@ commands:
description: Finds a farm producing a resource
aliases: [find_farm]
usage: /find_farm [resource name]
geoffrey_info:
description: Gives info on a location
aliases: [info]
usage: /info [location_name]
permissions:
add: