Fixed: Closed file streams after reading

This commit is contained in:
Grafe 2012-12-27 16:51:43 +01:00
parent e8fa5b2ee7
commit 3cf96c5f25
3 changed files with 193 additions and 188 deletions

View File

@ -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<Player> chatSpyer=new CopyOnWriteArrayList<Player>();
@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);
}
}

View File

@ -20,7 +20,7 @@ import org.bukkit.entity.Player;
public class EditWorld {
private static DungeonsXL p=DungeonsXL.p;
public static CopyOnWriteArrayList<EditWorld> eworlds=new CopyOnWriteArrayList<EditWorld>();
//Variables
public World world;
public String owner;
@ -30,10 +30,10 @@ public class EditWorld {
public Location lobby;
public CopyOnWriteArrayList<String> invitedPlayers=new CopyOnWriteArrayList<String>();
public CopyOnWriteArrayList<Block> sign=new CopyOnWriteArrayList<Block>();
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;
}
}

View File

@ -31,10 +31,10 @@ import com.dre.dungeonsxl.DungeonsXL;
public class GameWorld {
private static DungeonsXL p=DungeonsXL.p;
public static CopyOnWriteArrayList<GameWorld> gworlds=new CopyOnWriteArrayList<GameWorld>();
//Variables placeable
public boolean isTutorial;
public CopyOnWriteArrayList<Block> placeableBlocks=new CopyOnWriteArrayList<Block>();
public World world;
public String dungeonname;
@ -46,15 +46,15 @@ public class GameWorld {
public boolean isPlaying=false;
public int id;
public CopyOnWriteArrayList<Material> secureobjects = new CopyOnWriteArrayList<Material>();
public CopyOnWriteArrayList<Sign> signClass=new CopyOnWriteArrayList<Sign>();
public CopyOnWriteArrayList<DMob> dmobs = new CopyOnWriteArrayList<DMob>();
public CopyOnWriteArrayList<GameChest> gchests = new CopyOnWriteArrayList<GameChest>();
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; i<length; i++){
int x=os.readInt();
@ -234,7 +234,7 @@ public class GameWorld {
e.printStackTrace();
}
}
public boolean canBuild(Block block){
for(Block placeableBlock:placeableBlocks){
if(placeableBlock.getLocation().distance(block.getLocation())<1){
@ -243,13 +243,13 @@ public class GameWorld {
}
return false;
}
public void msg(String msg) {
for(DPlayer dplayer:DPlayer.get(this.world)){
p.msg(dplayer.player, msg);
}
}
//Static
public static GameWorld get(World world){
for(GameWorld gworld:gworlds){
@ -257,34 +257,34 @@ public class GameWorld {
return gworld;
}
}
return null;
}
public static void deleteAll(){
for(GameWorld gworld:gworlds){
gworlds.remove(gworld);
p.getServer().unloadWorld(gworld.world,true);
File dir = new File("DXL_Game_"+gworld.id);
p.removeDirectory(dir);
}
}
public static boolean canPlayDungeon(String dungeon, Player player){
if(p.permission.has(player, "dungeonsxl.ignoretimelimit")||player.isOp()){
return true;
}
File dungeonFolder=new File(p.getDataFolder()+"/dungeons/"+dungeon);
if(dungeonFolder.isDirectory()){
ConfigReader confReader=new ConfigReader(new File(p.getDataFolder()+"/dungeons/"+dungeon, "config.yml"));
if(confReader.timeToNextPlay!=0){
//read PlayerConfig
File file=new File(p.getDataFolder()+"/dungeons/"+dungeon, "players.yml");
if(!file.exists()){
try {
file.createNewFile();
@ -293,9 +293,9 @@ public class GameWorld {
e.printStackTrace();
}
}
FileConfiguration playerConfig = YamlConfiguration.loadConfiguration(file);
if(playerConfig.contains(player.getName())){
Long time=playerConfig.getLong(player.getName());
if(time+(confReader.timeToNextPlay*1000*60*60)>System.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<length; i++){
int x=os.readInt();
@ -351,8 +351,10 @@ public class GameWorld {
int z=os.readInt();
Block block=gworld.world.getBlockAt(x, y, z);
gworld.checkSign(block);
}
os.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
@ -360,15 +362,15 @@ public class GameWorld {
// TODO Auto-generated catch block
e.printStackTrace();
}
//if(gworld.confReader.isLobbyDisabled){
// gworld.startGame();
//}
return gworld;
}
return null;
}
@ -392,6 +394,6 @@ public class GameWorld {
}
}
}
}