From 8967e39084fce3623ad88853f7e3785449306e5b Mon Sep 17 00:00:00 2001 From: alisolarflare Date: Sun, 21 Aug 2016 13:23:59 -0400 Subject: [PATCH] Coded File IO --- .classpath | 1 + plugin.yml | 10 +++--- src/alisolarflare/AliPresents.java | 32 +++++++++++++++++ src/alisolarflare/MainPlugin.java | 20 ----------- src/alisolarflare/links/AliLinkSubPlug.java | 31 +++++++++++++++++ src/alisolarflare/links/AliLinkSubPlugin.java | 20 ----------- src/alisolarflare/links/Deserializer.java | 34 +++++++++++++++++++ src/alisolarflare/links/Link.java | 16 ++++++--- src/alisolarflare/links/PressAliLink.java | 4 +-- src/alisolarflare/links/Serializer.java | 31 +++++++++++++++++ src/alisolarflare/links/SetAliLink.java | 10 ++++-- src/alisolarflare/links/UnpressTask.java | 1 + 12 files changed, 156 insertions(+), 54 deletions(-) create mode 100644 src/alisolarflare/AliPresents.java delete mode 100644 src/alisolarflare/MainPlugin.java create mode 100644 src/alisolarflare/links/AliLinkSubPlug.java delete mode 100644 src/alisolarflare/links/AliLinkSubPlugin.java create mode 100644 src/alisolarflare/links/Deserializer.java create mode 100644 src/alisolarflare/links/Serializer.java diff --git a/.classpath b/.classpath index cefbd63..a8e4705 100644 --- a/.classpath +++ b/.classpath @@ -4,5 +4,6 @@ + diff --git a/plugin.yml b/plugin.yml index d48f733..e2d472c 100644 --- a/plugin.yml +++ b/plugin.yml @@ -1,11 +1,11 @@ -main: alisolarflare.MainPlugin +main: alisolarflare.AliPresents name: AliPresents -version: 1.0.0 +version: 2.0.0 commands: alishulker: - description: Spawns a shulker at player location. /Alishulker + description: Spawns a shulker at player location Alishulker health withereffect invisibility true experiencedrops pressalilink: - description: Presses an Ali Link. /pressalilink |frequency| + description: Presses an Ali Link pressalilink frequency setalilink: - description: Creates an Ali Link. /setalilink |frequency| \ No newline at end of file + description: Creates an Ali Link setalilink frequency \ No newline at end of file diff --git a/src/alisolarflare/AliPresents.java b/src/alisolarflare/AliPresents.java new file mode 100644 index 0000000..e8764f5 --- /dev/null +++ b/src/alisolarflare/AliPresents.java @@ -0,0 +1,32 @@ +package alisolarflare; + +import java.util.logging.Logger; + +import org.bukkit.plugin.PluginDescriptionFile; +import alisolarflare.links.AliLinkSubPlug; +import org.bukkit.plugin.java.JavaPlugin; +import alisolarflare.shulker.AliShulker; + +public class AliPresents extends JavaPlugin{ + public void onEnable(){ + PluginDescriptionFile pdfFile = getDescription(); + Logger logger = getLogger(); + + + logger.info(pdfFile.getName() + " has been started (V." + pdfFile.getVersion()+ ")."); + + registerSubPlugins(); + registerCommands(); + + logger.info(pdfFile.getName() + " has fully registered (V." + pdfFile.getVersion()+ ")."); + + + } + public void registerSubPlugins(){ + AliLinkSubPlug alilinksubplugin = new AliLinkSubPlug(this); + alilinksubplugin.register(); + } + public void registerCommands(){ + getCommand("alishulker").setExecutor(new AliShulker()); + } +} diff --git a/src/alisolarflare/MainPlugin.java b/src/alisolarflare/MainPlugin.java deleted file mode 100644 index e6b0f8a..0000000 --- a/src/alisolarflare/MainPlugin.java +++ /dev/null @@ -1,20 +0,0 @@ -package alisolarflare; - -import org.bukkit.plugin.java.JavaPlugin; - -import alisolarflare.links.AliLinkSubPlugin; -import alisolarflare.shulker.AliShulker; - -public class MainPlugin extends JavaPlugin{ - public void onEnable(){ - registerSubPlugins(); - registerCommands(); - } - public void registerSubPlugins(){ - AliLinkSubPlugin alilinksubplugin = new AliLinkSubPlugin(this); - alilinksubplugin.register(); - } - public void registerCommands(){ - getCommand("alishulker").setExecutor(new AliShulker()); - } -} diff --git a/src/alisolarflare/links/AliLinkSubPlug.java b/src/alisolarflare/links/AliLinkSubPlug.java new file mode 100644 index 0000000..1b8c533 --- /dev/null +++ b/src/alisolarflare/links/AliLinkSubPlug.java @@ -0,0 +1,31 @@ +package alisolarflare.links; + +import java.util.ArrayList; +import java.util.List; + +import alisolarflare.AliPresents; + +public class AliLinkSubPlug{ + public List linkList; + public AliPresents plugin; + public Serializer serializer; + public Deserializer deserializer; + + public AliLinkSubPlug(AliPresents plugin){ + this.plugin = plugin; + this.serializer = new Serializer(); + this.deserializer = new Deserializer(); + try{ + this.linkList = deserializer.loadLinkList(); + }catch(Exception e){ + linkList = new ArrayList(); + } + + } + public void register(){ + plugin.getCommand("pressalilink").setExecutor(new PressAliLink(this)); + plugin.getCommand("setalilink").setExecutor(new SetAliLink(this)); + + } + +} diff --git a/src/alisolarflare/links/AliLinkSubPlugin.java b/src/alisolarflare/links/AliLinkSubPlugin.java deleted file mode 100644 index cf38cf9..0000000 --- a/src/alisolarflare/links/AliLinkSubPlugin.java +++ /dev/null @@ -1,20 +0,0 @@ -package alisolarflare.links; - -import java.util.ArrayList; -import java.util.List; - -import alisolarflare.MainPlugin; - -public class AliLinkSubPlugin{ - public List linkList = new ArrayList(); - public MainPlugin plugin; - public AliLinkSubPlugin(MainPlugin plugin){ - this.plugin = plugin; - } - public void register(){ - plugin.getCommand("pressalilink").setExecutor(new PressAliLink(this)); - plugin.getCommand("setalilink").setExecutor(new SetAliLink(this)); - - } - -} diff --git a/src/alisolarflare/links/Deserializer.java b/src/alisolarflare/links/Deserializer.java new file mode 100644 index 0000000..437b16e --- /dev/null +++ b/src/alisolarflare/links/Deserializer.java @@ -0,0 +1,34 @@ +package alisolarflare.links; + + +import java.io.FileInputStream; +import java.io.ObjectInputStream; +import java.util.List; + +public class Deserializer{ + + public static void main (String args[]) { + + //Deserializer deserializer = new Deserializer(); + //List aliLink = deserializer.loadLink(aliLink); + //System.out.println(aliLink); + } + + @SuppressWarnings("unchecked") + public List loadLinkList(){ + List aliLinkList; + try{ + + FileInputStream fin = new FileInputStream("AliLinkList.ser"); + ObjectInputStream ois = new ObjectInputStream(fin); + aliLinkList = (List) ois.readObject(); + ois.close(); + + return aliLinkList; + + }catch(Exception ex){ + ex.printStackTrace(); + return null; + } + } +} diff --git a/src/alisolarflare/links/Link.java b/src/alisolarflare/links/Link.java index 16ae533..3f24142 100644 --- a/src/alisolarflare/links/Link.java +++ b/src/alisolarflare/links/Link.java @@ -1,20 +1,28 @@ package alisolarflare.links; +import java.io.Serializable; + import org.bukkit.Location; import org.bukkit.Material; -public class Link { - static AliLinkSubPlugin plugin; +public class Link implements Serializable{ + /** + * + */ + private static final long serialVersionUID = 1L; + AliLinkSubPlug subplugin; String frequency; Location location; - public Link(AliLinkSubPlugin plugin, String frequency, Location location){ + public Link(AliLinkSubPlug plugin, String frequency, Location location){ this.frequency = frequency; this.location = location; + this.subplugin = plugin; + plugin.plugin.getConfig().set("frequency", 10); } public void press() { location.getBlock().setType(Material.REDSTONE_BLOCK); UnpressTask unPressTask = new UnpressTask(location); - unPressTask.runTaskTimer(plugin.plugin, 2, 1); + unPressTask.runTaskTimer(subplugin.plugin, 2, 1); } } diff --git a/src/alisolarflare/links/PressAliLink.java b/src/alisolarflare/links/PressAliLink.java index 40261dd..994857d 100644 --- a/src/alisolarflare/links/PressAliLink.java +++ b/src/alisolarflare/links/PressAliLink.java @@ -5,8 +5,8 @@ import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; public class PressAliLink implements CommandExecutor{ - private AliLinkSubPlugin subplugin; - public PressAliLink(AliLinkSubPlugin subplugin){ + private AliLinkSubPlug subplugin; + public PressAliLink(AliLinkSubPlug subplugin){ this.subplugin = subplugin; } @Override diff --git a/src/alisolarflare/links/Serializer.java b/src/alisolarflare/links/Serializer.java new file mode 100644 index 0000000..1b1172e --- /dev/null +++ b/src/alisolarflare/links/Serializer.java @@ -0,0 +1,31 @@ +package alisolarflare.links; + + +import java.io.FileOutputStream; +import java.io.ObjectOutputStream; +import java.util.List; + +public class Serializer { + + public static void main (String args[]) { + + //Serializer serializer = new Serializer(); + //serializer.saveLink(plugin, frequency, location) + } + + public void saveLinkList(List aliLinkList){ + //saves link + + try{ + + FileOutputStream fout = new FileOutputStream("AliLinkList.ser"); + ObjectOutputStream oos = new ObjectOutputStream(fout); + oos.writeObject(aliLinkList); + oos.close(); + System.out.println("Done"); + + }catch(Exception ex){ + ex.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/alisolarflare/links/SetAliLink.java b/src/alisolarflare/links/SetAliLink.java index cb59fb0..4c535e1 100644 --- a/src/alisolarflare/links/SetAliLink.java +++ b/src/alisolarflare/links/SetAliLink.java @@ -8,8 +8,8 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; public class SetAliLink implements CommandExecutor{ - AliLinkSubPlugin subplugin; - public SetAliLink(AliLinkSubPlugin plugin){ + AliLinkSubPlug subplugin; + public SetAliLink(AliLinkSubPlug plugin){ this.subplugin = plugin; } @Override @@ -22,11 +22,15 @@ public class SetAliLink implements CommandExecutor{ sender.sendMessage("You must be a player to use this command!"); } Player player = (Player) sender; - if (args[1] == null || args[2] == null || args[3] == null){ + if (args.length < 4){ subplugin.linkList.add(new Link(subplugin, args[0], player.getLocation())); + return false; } if (StringUtils.isNumericSpace(args[1]) && StringUtils.isNumericSpace(args[2]) && StringUtils.isNumericSpace(args[3])){ subplugin.linkList.add(new Link(subplugin, args[0],new Location(player.getWorld(), Double.parseDouble(args[1]), Double.parseDouble(args[2]), Double.parseDouble(args[3])))); + }else{ + subplugin.linkList.add(new Link(subplugin, args[0], player.getLocation())); + return false; } return false; } diff --git a/src/alisolarflare/links/UnpressTask.java b/src/alisolarflare/links/UnpressTask.java index 4794ef6..946c2c7 100644 --- a/src/alisolarflare/links/UnpressTask.java +++ b/src/alisolarflare/links/UnpressTask.java @@ -15,6 +15,7 @@ public class UnpressTask extends BukkitRunnable { public void run() { location.getBlock().setType(Material.STAINED_CLAY); location.getBlock().setData((byte) 14); + this.cancel(); } }