From 3cf96c5f25eada06a434db7753ec22a34ffa4d78 Mon Sep 17 00:00:00 2001 From: Grafe Date: Thu, 27 Dec 2012 16:51:43 +0100 Subject: [PATCH] Fixed: Closed file streams after reading --- .../src/com/dre/dungeonsxl/DungeonsXL.java | 182 +++++++++--------- .../src/com/dre/dungeonsxl/EditWorld.java | 95 ++++----- .../com/dre/dungeonsxl/game/GameWorld.java | 104 +++++----- 3 files changed, 193 insertions(+), 188 deletions(-) diff --git a/DungeonsXL/src/com/dre/dungeonsxl/DungeonsXL.java b/DungeonsXL/src/com/dre/dungeonsxl/DungeonsXL.java index b7abc407..e01799be 100644 --- a/DungeonsXL/src/com/dre/dungeonsxl/DungeonsXL.java +++ b/DungeonsXL/src/com/dre/dungeonsxl/DungeonsXL.java @@ -39,65 +39,65 @@ import com.dre.dungeonsxl.listener.PlayerListener; public class DungeonsXL extends JavaPlugin{ public static DungeonsXL p; - + //Listener private static Listener entitylistener; private static Listener playerlistener; private static Listener blocklistener; - + //Main Config Reader public ConfigReader mainConfig; - + //Tutorial public String tutorialDungeon; public String tutorialStartGroup; public String tutorialEndGroup; - + //Chatspyer public CopyOnWriteArrayList chatSpyer=new CopyOnWriteArrayList(); - - + + @Override public void onEnable(){ p=this; - + //Commands getCommand("dungeonsxl").setExecutor(new CommandListener()); - + //MSG this.log(this.getDescription().getName()+" enabled!"); - + //Init Classes new DCommandRoot(); - + //InitFolders this.initFolders(); - + //Setup Permissions this.setupPermissions(); - + //Listener entitylistener=new EntityListener(); playerlistener=new PlayerListener(); blocklistener=new BlockListener(); - + Bukkit.getServer().getPluginManager().registerEvents(entitylistener,this); Bukkit.getServer().getPluginManager().registerEvents(playerlistener,this); Bukkit.getServer().getPluginManager().registerEvents(blocklistener,this); - + //Load All this.loadAll(); - + //Load Config mainConfig=new ConfigReader(new File(p.getDataFolder(), "config.yml")); - + //Load MobTypes DMobType.load(new File(p.getDataFolder(), "mobs.yml")); - + // -------------------------------------------- // // Update Sheduler // -------------------------------------------- // - + p.getServer().getScheduler().scheduleSyncRepeatingTask(p, new Runnable() { public void run() { for(GameWorld gworld:GameWorld.gworlds){ @@ -109,13 +109,13 @@ public class DungeonsXL extends JavaPlugin{ } for(EditWorld eworld:EditWorld.eworlds){ if(eworld.world.getPlayers().isEmpty()){ - + eworld.delete(); } } } }, 0L, 1200L); - + p.getServer().getScheduler().scheduleSyncRepeatingTask(p, new Runnable() { public void run() { MobSpawner.updateAll(); @@ -123,7 +123,7 @@ public class DungeonsXL extends JavaPlugin{ GameMessage.updateAll(); GameCheckpoint.update(); DPlayer.update(true); - + //Tutorial Mode for(Player player:p.getServer().getOnlinePlayers()){ if(DPlayer.get(player)==null){ @@ -151,39 +151,39 @@ public class DungeonsXL extends JavaPlugin{ } } }, 0L, 20L); - + p.getServer().getScheduler().scheduleSyncRepeatingTask(p, new Runnable() { public void run() { DPlayer.update(false); } }, 0L, 2L); } - - + + @Override public void onDisable(){ //Save this.saveAll(); - + //MSG this.log(this.getDescription().getName()+" disabled!"); - + //DPlayer leaves World for(DPlayer dplayer:DPlayer.players){ dplayer.leave(); } - + //Delete all Data DPortal.portals.clear(); DGSign.dgsigns.clear(); LeaveSign.lsigns.clear(); - + //Delete Worlds GameWorld.deleteAll(); EditWorld.deleteAll(); } - - + + //Init. public void initFolders(){ //Check Folder @@ -191,14 +191,14 @@ public class DungeonsXL extends JavaPlugin{ if(!folder.exists()){ folder.mkdir(); } - + folder = new File(this.getDataFolder()+File.separator+"dungeons"); if(!folder.exists()){ folder.mkdir(); } } - - + + //Permissions public Permission permission = null; @@ -210,28 +210,28 @@ public class DungeonsXL extends JavaPlugin{ } return (permission != null); } - + public Boolean GroupEnabled(String group){ - + for(String agroup:permission.getGroups()){ if(agroup.equalsIgnoreCase(group)){ return true; } } - + return false; } - - + + //Save and Load public void saveAll(){ File file = new File(this.getDataFolder(), "data.yml"); FileConfiguration configFile = new YamlConfiguration(); - + DPortal.save(configFile); DGSign.save(configFile); LeaveSign.save(configFile); - + try { configFile.save(file); } catch (IOException e) { @@ -239,18 +239,18 @@ public class DungeonsXL extends JavaPlugin{ e.printStackTrace(); } } - - + + public void loadAll(){ File file = new File(this.getDataFolder(), "data.yml"); FileConfiguration configFile = YamlConfiguration.loadConfiguration(file); - + DPortal.load(configFile); DGSign.load(configFile); LeaveSign.load(configFile); } - - + + //File Control public boolean removeDirectory(File directory) { if (directory.isDirectory()) { @@ -260,32 +260,34 @@ public class DungeonsXL extends JavaPlugin{ } return directory.delete(); } - - public void copyFile(File in, File out) throws IOException { - FileChannel inChannel = new FileInputStream(in).getChannel(); - FileChannel outChannel = new FileOutputStream(out).getChannel(); - try { - inChannel.transferTo(0, inChannel.size(), outChannel); - } catch (IOException e) { - throw e; - } finally { - if (inChannel != null) - inChannel.close(); - if (outChannel != null) - outChannel.close(); - } - } - + + public void copyFile(File in, File out) throws IOException { + FileChannel inChannel=null; + FileChannel outChannel=null; + try { + inChannel = new FileInputStream(in).getChannel(); + outChannel = new FileOutputStream(out).getChannel(); + inChannel.transferTo(0, inChannel.size(), outChannel); + } catch (IOException e) { + throw e; + } finally { + if (inChannel != null) + inChannel.close(); + if (outChannel != null) + outChannel.close(); + } + } + public String[] excludedFiles={"config.yml"}; - + public void copyDirectory(File sourceLocation, File targetLocation) { - + if (sourceLocation.isDirectory()) { - + if (!targetLocation.exists()) { targetLocation.mkdir(); } - + String[] children = sourceLocation.list(); for (int i = 0; i < children.length; i++) { boolean isOk=true; @@ -299,25 +301,25 @@ public class DungeonsXL extends JavaPlugin{ copyDirectory(new File(sourceLocation, children[i]), new File( targetLocation, children[i])); } - + } } else { - + try { - + if (!targetLocation.getParentFile().exists()) { - + createDirectory(targetLocation.getParentFile().getAbsolutePath()); targetLocation.createNewFile(); - + } else if (!targetLocation.exists()) { - + targetLocation.createNewFile(); } - + InputStream in = new FileInputStream(sourceLocation); OutputStream out = new FileOutputStream(targetLocation); - + byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { @@ -325,9 +327,9 @@ public class DungeonsXL extends JavaPlugin{ } in.close(); out.close(); - + } catch (Exception e) { - + if (e.getMessage().contains("Zugriff") || e.getMessage().contains("Access")) DungeonsXL.p.log("Error: " + e.getMessage() + " // Access denied"); @@ -336,17 +338,17 @@ public class DungeonsXL extends JavaPlugin{ } } } - + public void createDirectory(String s) { - + if (!new File(s).getParentFile().exists()) { - + createDirectory(new File(s).getParent()); } - + new File(s).mkdir(); } - + public void deletenotusingfiles(File directory){ File[] files=directory.listFiles(); for(File file:files){ @@ -355,7 +357,7 @@ public class DungeonsXL extends JavaPlugin{ } } } - + //Misc public void updateInventory(Player p) { CraftPlayer c = (CraftPlayer) p; @@ -378,34 +380,34 @@ public class DungeonsXL extends JavaPlugin{ } } } - + //Msg - + public void msg(Player player,String msg){ player.sendMessage(ChatColor.DARK_GREEN+"[DXL] "+ChatColor.WHITE+msg); } - + public void msg(Player player,String msg, boolean zusatz){ if(zusatz){ player.sendMessage(ChatColor.DARK_GREEN+"[DXL]"+ChatColor.WHITE+msg); }else{ player.sendMessage(ChatColor.WHITE+msg); } - + } - + //Misc. - + public EntityType getEntitiyType(String name){ for(EntityType type:EntityType.values()){ if(name.equalsIgnoreCase(type.getName())){ return type; } } - + return null; } - + // -------------------------------------------- // // LOGGING // -------------------------------------------- // @@ -418,5 +420,5 @@ public class DungeonsXL extends JavaPlugin{ { Logger.getLogger("Minecraft").log(level, "["+this.getDescription().getFullName()+"] "+msg); } - + } diff --git a/DungeonsXL/src/com/dre/dungeonsxl/EditWorld.java b/DungeonsXL/src/com/dre/dungeonsxl/EditWorld.java index 5d6ec63b..873bfd3a 100644 --- a/DungeonsXL/src/com/dre/dungeonsxl/EditWorld.java +++ b/DungeonsXL/src/com/dre/dungeonsxl/EditWorld.java @@ -20,7 +20,7 @@ import org.bukkit.entity.Player; public class EditWorld { private static DungeonsXL p=DungeonsXL.p; public static CopyOnWriteArrayList eworlds=new CopyOnWriteArrayList(); - + //Variables public World world; public String owner; @@ -30,10 +30,10 @@ public class EditWorld { public Location lobby; public CopyOnWriteArrayList invitedPlayers=new CopyOnWriteArrayList(); public CopyOnWriteArrayList sign=new CopyOnWriteArrayList(); - + public EditWorld(){ eworlds.add(this); - + //ID this.id=-1; int i=-1; @@ -48,7 +48,7 @@ public class EditWorld { } if(!exist) this.id=i; } - + name="DXL_Edit_"+this.id; } @@ -56,7 +56,7 @@ public class EditWorld { WorldCreator creator=WorldCreator.name(name); creator.type(WorldType.FLAT); creator.generateStructures(false); - + this.world=p.getServer().createWorld(creator); } @@ -73,7 +73,7 @@ public class EditWorld { out.writeInt(sign.getZ()); } out.close(); - + } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -81,7 +81,7 @@ public class EditWorld { // TODO Auto-generated catch block e.printStackTrace(); } - + //Check Configuration /*File file=new File("plugins/DungeonsXL/dungeons/"+this.dungeonname+"/config.yml"); if(!file.exists()){ @@ -100,32 +100,32 @@ public class EditWorld { } } }*/ - + } - + public void checkSign(Block block){ if((block.getState() instanceof Sign)){ Sign sign = (Sign) block.getState(); String[] lines=sign.getLines(); - + if(lines[1].equalsIgnoreCase("lobby")){ this.lobby=block.getLocation(); } } } - + public void delete(){ eworlds.remove(this); for(Player player:this.world.getPlayers()){ DPlayer dplayer=DPlayer.get(player); dplayer.leave(); } - + p.getServer().unloadWorld(this.world,true); File dir = new File("DXL_Edit_"+this.id); p.removeDirectory(dir); } - + //Static public static EditWorld get(World world){ for(EditWorld eworld:eworlds){ @@ -133,20 +133,20 @@ public class EditWorld { return eworld; } } - + return null; } - + public static EditWorld get(String name){ for(EditWorld eworld:eworlds){ if(eworld.name.equalsIgnoreCase(name)){ return eworld; } } - + return null; } - + public static void deleteAll(){ for(EditWorld eworld:eworlds){ eworlds.remove(eworld); @@ -154,31 +154,31 @@ public class EditWorld { DPlayer dplayer=DPlayer.get(player); dplayer.leave(); } - + p.getServer().unloadWorld(eworld.world,true); File dir = new File("DXL_Edit_"+eworld.id); p.removeDirectory(dir); } } - + public static EditWorld load(String name){ for(EditWorld eworld:eworlds){ - + if(eworld.dungeonname.equalsIgnoreCase(name)){ return eworld; } } - + File file=new File("plugins/DungeonsXL/dungeons/"+name); - + if(file.exists()){ EditWorld eworld = new EditWorld(); eworld.dungeonname=name; //World p.copyDirectory(file,new File("DXL_Edit_"+eworld.id)); - + eworld.world=p.getServer().createWorld(WorldCreator.name("DXL_Edit_"+eworld.id)); - + try { ObjectInputStream os=new ObjectInputStream(new FileInputStream(new File("plugins/DungeonsXL/dungeons/"+eworld.dungeonname+"/DXLData.data"))); int length=os.readInt(); @@ -190,7 +190,8 @@ public class EditWorld { eworld.checkSign(block); eworld.sign.add(block); } - + os.close(); + } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -198,13 +199,13 @@ public class EditWorld { // TODO Auto-generated catch block e.printStackTrace(); } - + return eworld; } - + return null; } - + public static boolean exist(String name){ //Cheack Loaded EditWorlds for(EditWorld eworld:eworlds){ @@ -212,14 +213,14 @@ public class EditWorld { return true; } } - + //Cheack Unloaded Worlds File file=new File("plugins/DungeonsXL/dungeons/"+name); - + if(file.exists()){ return true; } - + return false; } @@ -228,14 +229,14 @@ public class EditWorld { p.msg(dplayer.player, msg); } } - + //Invite public static boolean addInvitedPlayer(String eworldname,String player){ - + EditWorld eworld=EditWorld.get(eworldname); - - - + + + if(eworld!=null){ eworld.invitedPlayers.add(player.toLowerCase()); }else{ @@ -246,15 +247,15 @@ public class EditWorld { return true; } } - + return false; - + } - + public static boolean removeInvitedPlayer(String eworldname,String player){ - + EditWorld eworld=EditWorld.get(eworldname); - + if(eworld!=null){ eworld.invitedPlayers.remove(player.toLowerCase()); }else{ @@ -265,15 +266,15 @@ public class EditWorld { return true; } } - + return false; - + } - + public static boolean isInvitedPlayer(String eworldname,String player){ EditWorld eworld=EditWorld.get(eworldname); - + if(eworld!=null){ return eworld.invitedPlayers.contains(player.toLowerCase()); }else{ @@ -282,8 +283,8 @@ public class EditWorld { return confreader.invitedPlayer.contains(player.toLowerCase()); } } - + return false; - + } } diff --git a/DungeonsXL/src/com/dre/dungeonsxl/game/GameWorld.java b/DungeonsXL/src/com/dre/dungeonsxl/game/GameWorld.java index 5072d984..0771ddc9 100644 --- a/DungeonsXL/src/com/dre/dungeonsxl/game/GameWorld.java +++ b/DungeonsXL/src/com/dre/dungeonsxl/game/GameWorld.java @@ -31,10 +31,10 @@ import com.dre.dungeonsxl.DungeonsXL; public class GameWorld { private static DungeonsXL p=DungeonsXL.p; public static CopyOnWriteArrayList gworlds=new CopyOnWriteArrayList(); - + //Variables placeable public boolean isTutorial; - + public CopyOnWriteArrayList placeableBlocks=new CopyOnWriteArrayList(); public World world; public String dungeonname; @@ -46,15 +46,15 @@ public class GameWorld { public boolean isPlaying=false; public int id; public CopyOnWriteArrayList secureobjects = new CopyOnWriteArrayList(); - + public CopyOnWriteArrayList signClass=new CopyOnWriteArrayList(); public CopyOnWriteArrayList dmobs = new CopyOnWriteArrayList(); public CopyOnWriteArrayList gchests = new CopyOnWriteArrayList(); public ConfigReader confReader; - + public GameWorld(){ gworlds.add(this); - + //ID this.id=-1; int i=-1; @@ -70,12 +70,12 @@ public class GameWorld { if(!exist) this.id=i; } } - + public void checkSign(Block block){ if((block.getState() instanceof Sign)){ Sign sign = (Sign) block.getState(); String[] lines=sign.getLines(); - + if(!isPlaying){ if(lines[1].equalsIgnoreCase("lobby")){ this.locLobby=block.getLocation(); @@ -114,10 +114,10 @@ public class GameWorld { int[] direction=DGSign.getDirection(block.getData()); int directionX=direction[0]; int directionZ=direction[1]; - + int xx=0,zz=0; for(DClass dclass:this.confReader.getClasses()){ - + //Check existing signs boolean isContinued=true; for(Sign isusedsign:this.signClass){ @@ -125,13 +125,13 @@ public class GameWorld { isContinued=false; } } - + if(isContinued){ Block classBlock=block.getRelative(xx,0,zz); - + if(classBlock.getData()==sign.getData().getData()&&classBlock.getTypeId()==68&&(classBlock.getState() instanceof Sign)){ Sign classSign = (Sign) classBlock.getState(); - + classSign.setLine(0, ChatColor.DARK_BLUE+"############"); classSign.setLine(1, ChatColor.DARK_GREEN+dclass.name); classSign.setLine(2, ""); @@ -149,9 +149,9 @@ public class GameWorld { else{ block.setTypeId(0); } - + } - + }else{ if(lines[1].equalsIgnoreCase("mob")){ if(lines[2]!=""&&lines[3]!=""){ @@ -180,14 +180,14 @@ public class GameWorld { } if(lines[1].equalsIgnoreCase("checkpoint")){ int radius=0; - - + + if(lines[2]!=null ){ if(lines[2].length()>0){ radius=Integer.parseInt(lines[2]); } } - + new GameCheckpoint(this,block.getLocation(),radius); block.setTypeId(0); } @@ -211,13 +211,13 @@ public class GameWorld { } } } - + public void startGame() { this.isPlaying=true; ObjectInputStream os; try { os = new ObjectInputStream(new FileInputStream(new File("DXL_Game_"+this.id+"/DXLData.data"))); - + int length=os.readInt(); for(int i=0; iSystem.currentTimeMillis()){ @@ -306,44 +306,44 @@ public class GameWorld { }else{ return false; } - + return true; } - + public void delete(){ //for(GameWorld gworld:gworlds){ gworlds.remove(this); - + p.getServer().unloadWorld(this.world,true); File dir = new File("DXL_Game_"+this.id); p.removeDirectory(dir); //} } - + public static GameWorld load(String name){ - + File file=new File("plugins/DungeonsXL/dungeons/"+name); - + if(file.exists()){ GameWorld gworld = new GameWorld(); gworld.dungeonname=name; - - + + //Config einlesen gworld.confReader=new ConfigReader(new File(p.getDataFolder()+"/dungeons/"+gworld.dungeonname, "config.yml")); - + //Secure Objects gworld.secureobjects=gworld.confReader.secureobjects; - + //World p.copyDirectory(file,new File("DXL_Game_"+gworld.id)); - + gworld.world=p.getServer().createWorld(WorldCreator.name("DXL_Game_"+gworld.id)); - + ObjectInputStream os; try { os = new ObjectInputStream(new FileInputStream(new File("DXL_Game_"+gworld.id+"/DXLData.data"))); - + int length=os.readInt(); for(int i=0; i