Cleaned up AliLink Code

This commit is contained in:
alisolarflare 2016-11-14 21:57:50 -05:00
parent 21bb3a03f9
commit 7886c052e0
4 changed files with 47 additions and 41 deletions

View file

@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.bukkit.Server;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import alisolarflare.components.Component; import alisolarflare.components.Component;
@ -17,7 +18,7 @@ public class AliLinkComponent extends Component {
private List<Map<String,String>> linkData; private List<Map<String,String>> linkData;
@Override @Override
public void register(JavaPlugin plugin) { 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){ for (Link link: linkList){
linkData.add(link.toMap()); linkData.add(link.toMap());
} }
@ -27,10 +28,10 @@ public class AliLinkComponent extends Component {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private List<Link> MapToLinkList(List<Map<?, ?>> mapList) { private List<Link> MapToLinkList(List<Map<?, ?>> mapList, Server server) {
List<Link> linkList = new ArrayList<Link>(); List<Link> linkList = new ArrayList<Link>();
for (Map<?, ?> MapWithLinkData : mapList){ for (Map<?, ?> MapWithLinkData : mapList){
linkList.add(new Link((Map<String,String>) MapWithLinkData)); linkList.add(new Link((Map<String,String>) MapWithLinkData, server));
} }
return linkList; return linkList;
} }

View file

@ -18,14 +18,15 @@ public class PressAliLink extends PlayerCommand {
} }
@Override @Override
public boolean OnCommand(CommandSender sender, String label, String[] args) { public boolean OnCommand(CommandSender sender, String label, String[] inputFrequencies) {
if (args.length < 1) { if (inputFrequencies.length < 1) {
sender.sendMessage("You must specify a link frequency"); sender.sendMessage("You must specify a link frequency");
return false; return false;
} }
for (Link link : linkList) { for (Link link : linkList) {
for (String inputlink : args) { for (String frequency : inputFrequencies) {
if (inputlink.equals(link.frequency)) { if (frequency.equals(link.frequency)) {
link.press(plugin); link.press(plugin);
} }
} }

View file

@ -35,18 +35,18 @@ public class SetAliLink extends PlayerCommand {
String frequency = args[0]; String frequency = args[0];
World world = player.getWorld(); World world = player.getWorld();
Double x = player.getLocation().getX(); int x = player.getLocation().getBlockX();
Double y = player.getLocation().getY(); int y = player.getLocation().getBlockY();
Double z = player.getLocation().getZ(); int z = player.getLocation().getBlockZ();
if (args.length > 4) { if (args.length > 4) {
boolean arg1isNumber = StringUtils.isNumericSpace(args[1]); boolean arg1isNumber = StringUtils.isNumericSpace(args[1]);
boolean arg2isNumber = StringUtils.isNumericSpace(args[2]); boolean arg2isNumber = StringUtils.isNumericSpace(args[2]);
boolean arg3isNumber = StringUtils.isNumericSpace(args[3]); boolean arg3isNumber = StringUtils.isNumericSpace(args[3]);
if (arg1isNumber && arg2isNumber && arg3isNumber) { if (arg1isNumber && arg2isNumber && arg3isNumber) {
x = Double.parseDouble(args[1]); x = Integer.parseInt(args[1]);
y = Double.parseDouble(args[2]); y = Integer.parseInt(args[2]);
z = Double.parseDouble(args[3]); z = Integer.parseInt(args[3]);
} }
} }

View file

@ -5,6 +5,7 @@ import java.util.Map;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
@ -12,52 +13,55 @@ import alisolarflare.components.alilinks.tasks.UnpressTask;
public class Link{ public class Link{
public String frequency; public String frequency;
public String world; public World world;
public String x; public int x;
public String y; public int y;
public String z; public int z;
public Link(Map<String,String> linkFromMap){ public Link (String frequency, World world, int x, int y, int z){
this.frequency = linkFromMap.get("frequency"); this.frequency = frequency;
this.world = linkFromMap.get("world"); this.world = world;
this.x = linkFromMap.get("x"); this.x = x;
this.y = linkFromMap.get("y"); this.y = y;
this.z = linkFromMap.get("z"); this.z = z;
} }
public Link(String frequency, Location location){ public Link(String frequency, Location location, Server server){
this.frequency = frequency; this.frequency = frequency;
this.world = location.getWorld().getName(); this.world = server.getWorld(location.getWorld().getName());
this.x = "" + location.getBlockX(); this.x = location.getBlockX();
this.y = "" + location.getBlockY(); this.y = location.getBlockY();
this.z = "" + location.getBlockZ(); this.z = location.getBlockZ();
//plugin.plugin.getConfig().set("frequency", 10); //plugin.plugin.getConfig().set("frequency", 10);
} }
public Link (String frequency, World world, Double x, Double y, Double z){
this.frequency = frequency; public Link(Map<String,String> linkFromMap, Server server){
this.world = world.getName(); this.frequency = linkFromMap.get("frequency");
this.x = "" + x; this.world = server.getWorld(linkFromMap.get("world"));
this.y = "" + y; this.x = Integer.parseInt(linkFromMap.get("x"));
this.z = "" + z; this.y = Integer.parseInt(linkFromMap.get("y"));
this.z = Integer.parseInt(linkFromMap.get("z"));
} }
/** /**
* Activates the Ali-Link * Activates the Ali-Link
* @param plugin * @param plugin
*/ */
public void press(JavaPlugin 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); location.getBlock().setType(Material.REDSTONE_BLOCK);
UnpressTask unPressTask = new UnpressTask(location); new UnpressTask(location).runTaskTimer(plugin, 2, 1);
unPressTask.runTaskTimer(plugin, 2, 1);
} }
public Map<String,String> toMap(){ public Map<String,String> toMap(){
Map<String, String> linkAsMap = new HashMap<String,String>(); Map<String, String> linkAsMap = new HashMap<String,String>();
linkAsMap.put("frequency", frequency); linkAsMap.put("frequency", frequency);
linkAsMap.put("world", world); linkAsMap.put("world", world.getName());
linkAsMap.put("x", x); linkAsMap.put("x", Integer.toString(this.x));
linkAsMap.put("y", y); linkAsMap.put("y", Integer.toString(this.y));
linkAsMap.put("z", z); linkAsMap.put("z", Integer.toString(this.z));
return linkAsMap; return linkAsMap;
} }
} }