From 7886c052e060b28a40ba8e8d8ca3d4c85ed64d40 Mon Sep 17 00:00:00 2001 From: alisolarflare Date: Mon, 14 Nov 2016 21:57:50 -0500 Subject: [PATCH] Cleaned up AliLink Code --- .../components/alilinks/AliLinkComponent.java | 7 ++- .../alilinks/commands/PressAliLink.java | 9 +-- .../alilinks/commands/SetAliLink.java | 12 ++-- .../components/alilinks/entities/Link.java | 60 ++++++++++--------- 4 files changed, 47 insertions(+), 41 deletions(-) diff --git a/src/alisolarflare/components/alilinks/AliLinkComponent.java b/src/alisolarflare/components/alilinks/AliLinkComponent.java index 3879f63..85e1fac 100644 --- a/src/alisolarflare/components/alilinks/AliLinkComponent.java +++ b/src/alisolarflare/components/alilinks/AliLinkComponent.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import org.bukkit.Server; import org.bukkit.plugin.java.JavaPlugin; import alisolarflare.components.Component; @@ -17,7 +18,7 @@ public class AliLinkComponent extends Component { private List> linkData; @Override public void register(JavaPlugin plugin) { - this.linkList = MapToLinkList(plugin.getConfig().getMapList("aliLinkList")); + this.linkList = MapToLinkList(plugin.getConfig().getMapList("aliLinkList"), plugin.getServer()); for (Link link: linkList){ linkData.add(link.toMap()); } @@ -27,10 +28,10 @@ public class AliLinkComponent extends Component { } @SuppressWarnings("unchecked") - private List MapToLinkList(List> mapList) { + private List MapToLinkList(List> mapList, Server server) { List linkList = new ArrayList(); for (Map MapWithLinkData : mapList){ - linkList.add(new Link((Map) MapWithLinkData)); + linkList.add(new Link((Map) MapWithLinkData, server)); } return linkList; } diff --git a/src/alisolarflare/components/alilinks/commands/PressAliLink.java b/src/alisolarflare/components/alilinks/commands/PressAliLink.java index 0f2b0ba..e68fdae 100644 --- a/src/alisolarflare/components/alilinks/commands/PressAliLink.java +++ b/src/alisolarflare/components/alilinks/commands/PressAliLink.java @@ -18,14 +18,15 @@ public class PressAliLink extends PlayerCommand { } @Override - public boolean OnCommand(CommandSender sender, String label, String[] args) { - if (args.length < 1) { + public boolean OnCommand(CommandSender sender, String label, String[] inputFrequencies) { + if (inputFrequencies.length < 1) { sender.sendMessage("You must specify a link frequency"); return false; } + for (Link link : linkList) { - for (String inputlink : args) { - if (inputlink.equals(link.frequency)) { + for (String frequency : inputFrequencies) { + if (frequency.equals(link.frequency)) { link.press(plugin); } } diff --git a/src/alisolarflare/components/alilinks/commands/SetAliLink.java b/src/alisolarflare/components/alilinks/commands/SetAliLink.java index 11ffb83..56e9e02 100644 --- a/src/alisolarflare/components/alilinks/commands/SetAliLink.java +++ b/src/alisolarflare/components/alilinks/commands/SetAliLink.java @@ -35,18 +35,18 @@ public class SetAliLink extends PlayerCommand { String frequency = args[0]; World world = player.getWorld(); - Double x = player.getLocation().getX(); - Double y = player.getLocation().getY(); - Double z = player.getLocation().getZ(); + int x = player.getLocation().getBlockX(); + int y = player.getLocation().getBlockY(); + int z = player.getLocation().getBlockZ(); 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]); + x = Integer.parseInt(args[1]); + y = Integer.parseInt(args[2]); + z = Integer.parseInt(args[3]); } } diff --git a/src/alisolarflare/components/alilinks/entities/Link.java b/src/alisolarflare/components/alilinks/entities/Link.java index b27b61b..546f116 100644 --- a/src/alisolarflare/components/alilinks/entities/Link.java +++ b/src/alisolarflare/components/alilinks/entities/Link.java @@ -5,6 +5,7 @@ import java.util.Map; import org.bukkit.Location; import org.bukkit.Material; +import org.bukkit.Server; import org.bukkit.World; import org.bukkit.plugin.java.JavaPlugin; @@ -12,52 +13,55 @@ import alisolarflare.components.alilinks.tasks.UnpressTask; public class Link{ public String frequency; - public String world; - public String x; - public String y; - public String z; + public World world; + public int x; + public int y; + public int 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, World world, int x, int y, int z){ + this.frequency = frequency; + this.world = world; + this.x = x; + this.y = y; + this.z = z; } - public Link(String frequency, Location location){ + public Link(String frequency, Location location, Server server){ this.frequency = frequency; - this.world = location.getWorld().getName(); - this.x = "" + location.getBlockX(); - this.y = "" + location.getBlockY(); - this.z = "" + location.getBlockZ(); + this.world = server.getWorld(location.getWorld().getName()); + this.x = location.getBlockX(); + this.y = location.getBlockY(); + 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; + + public Link(Map linkFromMap, Server server){ + this.frequency = linkFromMap.get("frequency"); + this.world = server.getWorld(linkFromMap.get("world")); + this.x = Integer.parseInt(linkFromMap.get("x")); + this.y = Integer.parseInt(linkFromMap.get("y")); + this.z = Integer.parseInt(linkFromMap.get("z")); } + /** * Activates the Ali-Link * @param plugin */ public void press(JavaPlugin plugin) { - Location location = new Location(plugin.getServer().getWorld(world), Integer.parseInt(x), Integer.parseInt(y), Integer.parseInt(z)); + Location location = new Location(this.world, this.x, this.y, this.z); location.getBlock().setType(Material.REDSTONE_BLOCK); - UnpressTask unPressTask = new UnpressTask(location); - unPressTask.runTaskTimer(plugin, 2, 1); + new UnpressTask(location).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); + linkAsMap.put("world", world.getName()); + linkAsMap.put("x", Integer.toString(this.x)); + linkAsMap.put("y", Integer.toString(this.y)); + linkAsMap.put("z", Integer.toString(this.z)); + return linkAsMap; } }