Completely rewrote AliLink code
This commit is contained in:
parent
0df59ba8d4
commit
e089d8ff12
4 changed files with 85 additions and 122 deletions
|
@ -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<Link> linkList;
|
||||
private List<Map<String,String>> 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<Link> loadLinkList(JavaPlugin plugin) {
|
||||
return (List<Link>) plugin.getConfig().getList("aliLinkList");
|
||||
private List<Link> MapToLinkList(List<Map<?, ?>> mapList) {
|
||||
List<Link> linkList = new ArrayList<Link>();
|
||||
for (Map<?, ?> MapWithLinkData : mapList){
|
||||
linkList.add(new Link((Map<String,String>) MapWithLinkData));
|
||||
}
|
||||
return linkList;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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<Link> linkList;
|
||||
|
||||
public PressAliLink(JavaPlugin plugin, SetAliLink setAliLink) {
|
||||
public PressAliLink(JavaPlugin plugin, List<Link> 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 <frequency> [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<Link> linkList = new ArrayList<Link>();
|
||||
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<Link> linkList;
|
||||
public List<Map<String, String>> linkData;
|
||||
private FileConfiguration config;
|
||||
|
||||
public SetAliLink(FileConfiguration config, List<Link> linkList, List<Map<String, String>> 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<Link>) plugin.getConfig().getList("aliLinkList");
|
||||
if (linkList == null || linkList.isEmpty()) {
|
||||
linkList = new ArrayList<Link>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<String,String> 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<String,String> toMap(){
|
||||
Map<String, String> linkAsMap = new HashMap<String,String>();
|
||||
linkAsMap.put("frequency", frequency);
|
||||
linkAsMap.put("world", world);
|
||||
linkAsMap.put("x", x);
|
||||
linkAsMap.put("y", y);
|
||||
linkAsMap.put("z", z);
|
||||
return linkAsMap;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue