General AliLink Refactoring

This commit is contained in:
alisolarflare 2016-11-30 23:42:04 -05:00
parent 068a353d0d
commit 7d73a18d17
5 changed files with 37 additions and 42 deletions

View file

@ -1,9 +1,12 @@
package alisolarflare.components.alilinks; package alisolarflare.components.alilinks;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.bukkit.Server;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -57,4 +60,12 @@ public class AliLinkAPI {
linkList.add(link); linkList.add(link);
return true; return true;
} }
@SuppressWarnings("unchecked")
private List<Link> MapToLinkList(List<Map<?, ?>> mapList, Server server) {
List<Link> linkList = new ArrayList<Link>();
for (Map<?, ?> MapWithLinkData : mapList){
linkList.add(new Link((Map<String,String>) MapWithLinkData, server));
}
return linkList;
}
} }

View file

@ -1,36 +1,13 @@
package alisolarflare.components.alilinks; package alisolarflare.components.alilinks;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.bukkit.Server;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import alisolarflare.architecture.Component; import alisolarflare.architecture.Component;
import alisolarflare.components.alilinks.commands.AliLink; import alisolarflare.components.alilinks.commands.AliLink;
import alisolarflare.components.alilinks.entities.Link;
public class AliLinkComponent extends Component { public class AliLinkComponent extends Component {
private List<Map<String,String>> linkMap;
private List<Link> linkList;
@Override @Override
public void register(JavaPlugin plugin) { public void register(JavaPlugin plugin) {
this.linkList = MapToLinkList(plugin.getConfig().getMapList("aliLinkList"), plugin.getServer());
for (Link link: linkList){
linkMap.add(link.toMap());
}
registerCommand(plugin, new AliLink(plugin)); registerCommand(plugin, new AliLink(plugin));
}
@SuppressWarnings("unchecked")
private List<Link> MapToLinkList(List<Map<?, ?>> mapList, Server server) {
List<Link> linkList = new ArrayList<Link>();
for (Map<?, ?> MapWithLinkData : mapList){
linkList.add(new Link((Map<String,String>) MapWithLinkData, server));
}
return linkList;
} }
} }

View file

@ -21,7 +21,7 @@ public class AliLink extends PlayerCommand{
case "press": case "press":
return AliLinkAPI.sendPressEvent(player, inputFrequencies, plugin); return AliLinkAPI.sendPressEvent(player, inputFrequencies, plugin);
case "set": case "set":
AliLinkAPI.createAliLink(player, inputFrequencies); return AliLinkAPI.createAliLink(player, inputFrequencies);
default: default:
return false; return false;
} }

View file

@ -19,23 +19,15 @@ public class Link{
public int y; public int y;
public int z; public int z;
public Link (String frequency, World world, int x, int y, int z){ /**Constructor for copying links*/
this.frequency = frequency; public Link(Link link){
this.world = world; this.frequency = link.frequency;
this.x = x; this.world = link.world;
this.y = y; this.x = link.x;
this.z = z; this.y = link.y;
this.z = link.z;
} }
/**Constructor for deserialization*/
public Link(String frequency, Location location, Server server){
this.frequency = frequency;
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(Map<String,String> linkFromMap, Server server){ public Link(Map<String,String> linkFromMap, Server server){
this.frequency = linkFromMap.get("frequency"); this.frequency = linkFromMap.get("frequency");
this.world = server.getWorld(linkFromMap.get("world")); this.world = server.getWorld(linkFromMap.get("world"));
@ -43,6 +35,22 @@ public class Link{
this.y = Integer.parseInt(linkFromMap.get("y")); this.y = Integer.parseInt(linkFromMap.get("y"));
this.z = Integer.parseInt(linkFromMap.get("z")); this.z = Integer.parseInt(linkFromMap.get("z"));
} }
/**Constructor for Location inputs*/
public Link(String frequency, Location location){
this.frequency = frequency;
this.world = location.getWorld();
this.x = location.getBlockX();
this.y = location.getBlockY();
this.z = location.getBlockZ();
}
/**Pure Constructor*/
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;
}
/** /**
* Activates the Ali-Link * Activates the Ali-Link
@ -51,7 +59,7 @@ public class Link{
public void press(JavaPlugin plugin) { public void press(JavaPlugin plugin) {
Location location = new Location(this.world, this.x, this.y, this.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);
new UnpressTask(location).runTaskTimer(plugin, 2, 1); new UnpressTask(location).runTaskLater(plugin, 2);
} }
public Map<String,String> toMap(){ public Map<String,String> toMap(){

View file

@ -17,5 +17,4 @@ public class UnpressTask extends BukkitRunnable {
location.getBlock().setData((byte) 14); location.getBlock().setData((byte) 14);
this.cancel(); this.cancel();
} }
} }