From e089d8ff12724d6c2608a4fad187747cd166f6e3 Mon Sep 17 00:00:00 2001 From: alisolarflare Date: Sat, 12 Nov 2016 11:17:38 -0500 Subject: [PATCH] Completely rewrote AliLink code --- .../components/alilinks/AliLinkComponent.java | 28 ++-- .../alilinks/commands/PressAliLink.java | 17 +-- .../alilinks/commands/SetAliLink.java | 128 +++++------------- .../components/alilinks/entities/Link.java | 34 ++++- 4 files changed, 85 insertions(+), 122 deletions(-) diff --git a/src/alisolarflare/components/alilinks/AliLinkComponent.java b/src/alisolarflare/components/alilinks/AliLinkComponent.java index 9e6741e..3879f63 100644 --- a/src/alisolarflare/components/alilinks/AliLinkComponent.java +++ b/src/alisolarflare/components/alilinks/AliLinkComponent.java @@ -1,6 +1,8 @@ package alisolarflare.components.alilinks; +import java.util.ArrayList; import java.util.List; +import java.util.Map; import org.bukkit.plugin.java.JavaPlugin; @@ -10,24 +12,26 @@ import alisolarflare.components.alilinks.commands.SetAliLink; import alisolarflare.components.alilinks.entities.Link; public class AliLinkComponent extends Component { - private SetAliLink setAliLink; + private List linkList; + private List> linkData; @Override public void register(JavaPlugin plugin) { - setAliLink = new SetAliLink(plugin); + this.linkList = MapToLinkList(plugin.getConfig().getMapList("aliLinkList")); + for (Link link: linkList){ + linkData.add(link.toMap()); + } - registerCommand(plugin, new PressAliLink(plugin, setAliLink)); - registerCommand(plugin, setAliLink); + registerCommand(plugin, new PressAliLink(plugin, linkList)); + registerCommand(plugin, new SetAliLink(plugin.getConfig(), linkList, linkData)); } - - public void saveLinkList(JavaPlugin plugin) { - plugin.getConfig().set("aliLinkList", setAliLink.linkList); - } - @SuppressWarnings("unchecked") - public List loadLinkList(JavaPlugin plugin) { - return (List) plugin.getConfig().getList("aliLinkList"); + private List MapToLinkList(List> mapList) { + List linkList = new ArrayList(); + for (Map MapWithLinkData : mapList){ + linkList.add(new Link((Map) MapWithLinkData)); + } + return linkList; } - } diff --git a/src/alisolarflare/components/alilinks/commands/PressAliLink.java b/src/alisolarflare/components/alilinks/commands/PressAliLink.java index 9ba8e68..eeeee96 100644 --- a/src/alisolarflare/components/alilinks/commands/PressAliLink.java +++ b/src/alisolarflare/components/alilinks/commands/PressAliLink.java @@ -1,5 +1,7 @@ package alisolarflare.components.alilinks.commands; +import java.util.List; + import org.bukkit.command.CommandSender; import org.bukkit.plugin.java.JavaPlugin; @@ -8,23 +10,23 @@ import alisolarflare.components.alilinks.entities.Link; public class PressAliLink extends PlayerCommand { private JavaPlugin plugin; - private SetAliLink setAliLink; + private List linkList; - public PressAliLink(JavaPlugin plugin, SetAliLink setAliLink) { + public PressAliLink(JavaPlugin plugin, List linkList) { this.plugin = plugin; - this.setAliLink = setAliLink; + this.linkList = linkList; } @Override public boolean OnCommand(CommandSender sender, String label, String[] args) { - sender.getServer().broadcastMessage(setAliLink.linkList.toString() + "over."); + sender.getServer().broadcastMessage(linkList.toString() + "over."); if (args.length < 1) { sender.sendMessage("You must specify a link frequency"); sender.sendMessage("/pressalilink [frequency]"); } - for (Link link : setAliLink.linkList) { + for (Link link : linkList) { for (String inputlink : args) { if (inputlink.equals(link.frequency)) { link.press(plugin); @@ -33,9 +35,4 @@ public class PressAliLink extends PlayerCommand { } return false; } - - @Override - public String[] GetHelpText(String alias) { - return null; - } } diff --git a/src/alisolarflare/components/alilinks/commands/SetAliLink.java b/src/alisolarflare/components/alilinks/commands/SetAliLink.java index b98ff5b..c68aa18 100644 --- a/src/alisolarflare/components/alilinks/commands/SetAliLink.java +++ b/src/alisolarflare/components/alilinks/commands/SetAliLink.java @@ -2,122 +2,62 @@ package alisolarflare.components.alilinks.commands; import java.util.ArrayList; import java.util.List; +import java.util.Map; import org.apache.commons.lang3.StringUtils; -import org.bukkit.Location; +import org.bukkit.World; import org.bukkit.command.CommandSender; +import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; import alisolarflare.components.PlayerCommand; -import alisolarflare.components.alilinks.AliLinkComponent; import alisolarflare.components.alilinks.entities.Link; -/** - * This class manages the command /SetAliLink [x] [y] [z]. This command creates an Ali-Link, a location in a world that when called on by {@link PressAliLink}, creates a temporary redstone - * block, enabling wireless redstone - * - * @see PressAliLink - * @author Alisolarflare - * - */ public class SetAliLink extends PlayerCommand { - public List linkList = new ArrayList(); - AliLinkComponent subplugin; - private JavaPlugin plugin; - - /** - * Constructs the SetAliLink class - * - * @param plugin - * The plugin that contains the configuration file of SetAliLink - */ - public SetAliLink(JavaPlugin plugin) { - this.plugin = plugin; - load(); + public List linkList; + public List> linkData; + private FileConfiguration config; + + public SetAliLink(FileConfiguration config, List linkList, List> linkData) { + this.linkList = linkList; + this.linkData = linkData; + this.config = config; } - /** - * This command creates an Ali-Link which, when activated by the command /PressAliLink, creates a temporary redstone block if and only if /PressAliLink [frequency] matches the frequency saved when - * /SetAliLink [frequency] is called. - * - * @see Class#PressAliLink - * @param sender - * Player who sent the command - * @param command - * Command Object created - * @param label - * Name of the command - * @param args - * Arguments: [frequency] [x-coordinate] [y-coordinate] [z-coordinate], where the coordinates point to the intended location of the Ali-Link - */ @Override public boolean OnCommand(CommandSender sender, String label, String[] args) { - sender.sendMessage("you pressed shit"); + Player player = (Player) sender; + player.sendMessage("you pressed"); if (args == null || args.length < 1) { sender.sendMessage("You must specify a link frequency"); sender.sendMessage("/pressalilink [name]"); return false; } - Player player = (Player) sender; - if (args.length < 4) { - player.sendMessage("short"); - linkList.add(new Link(args[0], player.getLocation())); - save(player); - player.sendMessage("end"); - return false; - } - if (StringUtils.isNumericSpace(args[1]) && StringUtils.isNumericSpace(args[2]) - && StringUtils.isNumericSpace(args[3])) { - player.sendMessage("CUUUSTOM"); - linkList.add(new Link(args[0], new Location(player.getWorld(), Double.parseDouble(args[1]), - Double.parseDouble(args[2]), Double.parseDouble(args[3])))); - save(player); - player.sendMessage("FINISHED"); - } else { - player.sendMessage("UNCUSTOOM"); - linkList.add(new Link(args[0], player.getLocation())); - save(player); - player.sendMessage("UNFINISHED"); - return false; - } - return false; - } - /** - * Tries to save the entire SetAliLink class into memory, which includes all of the current Ali-links saved and in use. - * - * @param player - */ - private void save(Player player) { - player.sendMessage("SAAAVING"); - player.sendMessage("SAVE FAILED: TELL ALI TO FIX THE SAVE AND UN-COMMENT THE PARAGRAPH SHE COMMENTED"); - player.sendMessage("Link will only last until next server restart"); - return; - /* - * subplugin.plugin.getConfig().set("aliLinkList", subplugin.linkList); try { player.sendMessage("SAVIN"); subplugin.plugin.saveConfig(); player.sendMessage("GOOD SAVE"); } catch (Exception e) - * { player.sendMessage("YOU FUCKED STUFF UP"); // TODO Auto-generated catch block e.printStackTrace(); } - */ - } - - /** - * Attempts to load the previous saved state of AliLinks, from the plugin configuration file - */ - @SuppressWarnings("unchecked") - private void load() { - try { - linkList = (List) plugin.getConfig().getList("aliLinkList"); - if (linkList == null || linkList.isEmpty()) { - linkList = new ArrayList(); + String frequency = args[0]; + World world = player.getWorld(); + Double x = player.getLocation().getX(); + Double y = player.getLocation().getY(); + Double z = player.getLocation().getZ(); + + if (args.length > 4) { + boolean arg1isNumber = StringUtils.isNumericSpace(args[1]); + boolean arg2isNumber = StringUtils.isNumericSpace(args[2]); + boolean arg3isNumber = StringUtils.isNumericSpace(args[3]); + if (arg1isNumber && arg2isNumber && arg3isNumber) { + x = Double.parseDouble(args[1]); + y = Double.parseDouble(args[2]); + z = Double.parseDouble(args[3]); } - } catch (Exception e) { - e.printStackTrace(); } + Link link = new Link(frequency, world, x, y, z); + linkList.add(link); + linkData.add(link.toMap()); + saveLinkList(); + return true; } - - @Override - public String[] GetHelpText(String alias) { - // TODO Auto-generated method stub - return null; + private void saveLinkList(){ + config.set("aliLinkList", linkData); } } diff --git a/src/alisolarflare/components/alilinks/entities/Link.java b/src/alisolarflare/components/alilinks/entities/Link.java index 55199d9..b27b61b 100644 --- a/src/alisolarflare/components/alilinks/entities/Link.java +++ b/src/alisolarflare/components/alilinks/entities/Link.java @@ -1,24 +1,29 @@ package alisolarflare.components.alilinks.entities; -import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; import org.bukkit.Location; import org.bukkit.Material; +import org.bukkit.World; import org.bukkit.plugin.java.JavaPlugin; import alisolarflare.components.alilinks.tasks.UnpressTask; -public class Link implements Serializable{ - /** - * - */ - private static final long serialVersionUID = 1L; +public class Link{ public String frequency; public String world; public String x; public String y; public String z; + public Link(Map linkFromMap){ + this.frequency = linkFromMap.get("frequency"); + this.world = linkFromMap.get("world"); + this.x = linkFromMap.get("x"); + this.y = linkFromMap.get("y"); + this.z = linkFromMap.get("z"); + } public Link(String frequency, Location location){ this.frequency = frequency; @@ -28,6 +33,13 @@ public class Link implements Serializable{ this.z = "" + location.getBlockZ(); //plugin.plugin.getConfig().set("frequency", 10); } + public Link (String frequency, World world, Double x, Double y, Double z){ + this.frequency = frequency; + this.world = world.getName(); + this.x = "" + x; + this.y = "" + y; + this.z = "" + z; + } /** * Activates the Ali-Link * @param plugin @@ -38,4 +50,14 @@ public class Link implements Serializable{ UnpressTask unPressTask = new UnpressTask(location); unPressTask.runTaskTimer(plugin, 2, 1); } + + public Map toMap(){ + Map linkAsMap = new HashMap(); + linkAsMap.put("frequency", frequency); + linkAsMap.put("world", world); + linkAsMap.put("x", x); + linkAsMap.put("y", y); + linkAsMap.put("z", z); + return linkAsMap; + } }