Posted February 23, 201510 yr So i just created a second event handler (for ticks), registered it and now the game always crashes one or two minutes after it has been fired. I need this event handler to create a countdown for my minigame. ... MinecraftForge.EVENT_BUS.register(new BlackGeckoEventHandler()); //Main event Handler FMLCommonHandler.instance().bus().register(new BlackGeckoEventHandler()); FMLCommonHandler.instance().bus().register(new TickHandlerDimensionShift()); ... Tick event handler package com.blackgeckogames.server.mod.events; import java.util.ArrayList; import java.util.List; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.gameevent.TickEvent; import com.blackgeckogames.server.mod.minigames.GameMode; import com.blackgeckogames.server.mod.minigames.GameMode.EnumGameState; public class TickHandlerDimensionShift { public static List<GameMode> games = new ArrayList(); @SubscribeEvent public void onPlayerTick(TickEvent.PlayerTickEvent event) { } //Called when the server ticks. Usually 20 ticks a second. @SubscribeEvent public void onServerTick(TickEvent.ServerTickEvent event) { if(games.size()>0){ for(GameMode game : games){ game.countdown--; if(game.countdown % 20 ==0){ game.setAllPlayerXP(game.countdown/20); } if(game.countdown<=0){ game.countdown=0; game.state=EnumGameState.RUNNING; game.start(); games.remove(game); } } } } //Called when a new frame is displayed (See fps) @SubscribeEvent public void onRenderTick(TickEvent.RenderTickEvent event) { } //Called when the world ticks @SubscribeEvent public void onWorldTick(TickEvent.WorldTickEvent event) { } } My parent minigame class: package com.blackgeckogames.server.mod.minigames; import java.io.File; import java.io.FileFilter; import java.io.IOException; import java.util.ArrayList; import java.util.List; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.server.MinecraftServer; import net.minecraft.util.IChatComponent; import net.minecraftforge.common.DimensionManager; import org.apache.commons.io.FileUtils; import com.blackgeckogames.server.mod.BlackGeckoServer; import com.blackgeckogames.server.mod.events.TickHandlerDimensionShift; public class GameMode{ public enum EnumGameMode{ LOBBY ,SKY_BATTLE, PVP, HUNGERGAMES } public enum EnumGameState{ RESTARTING ,WAITING, STARTING, RUNNING } public int dim; public int countdown; public int countdownMax = 20*60; //one minute in ticks public EnumGameState state; public int map; public List<EntityPlayer> players = new ArrayList<EntityPlayer>(); public void joinPlayer(EntityPlayer player){ players.add(player); System.out.println("JOINED GAME MODE!"); } public void disconnectPlayer(EntityPlayer player){ players.remove(player); } protected void countdown(){ this.state=EnumGameState.STARTING; this.countdown=countdownMax; TickHandlerDimensionShift.games.add(this); } protected void resetWorld(EnumGameMode gamemode, int mapNumber, int dimNumber) { DimensionManager.unloadWorld(dimNumber); File folder = new File(MinecraftServer.getServer().worldServerForDimension(dimNumber).getSaveHandler().getWorldDirectory()+"/BGS/DIMENSIONS/"+gamemode.toString()+"/WORLD_"+dimNumber); File newFolder = new File(BlackGeckoServer.baseFolder+"/MAPS/"+gamemode.toString()+"/MAP_"+mapNumber); if(folder.exists()){ try { FileUtils.deleteDirectory(folder); if(newFolder.exists() && newFolder.isDirectory() && newFolder.list().length >0 ){ FileUtils.copyDirectory(newFolder, folder, new FileFilter() { @Override public boolean accept(File pathname) { return pathname.canRead(); } }); } else { newFolder.mkdir(); BlackGeckoServer.logger.warn("*****************************!!!WARNING!!!*****************************"); BlackGeckoServer.logger.warn("***********************************************************************"); BlackGeckoServer.logger.warn("UNABLE TO LOAD " + gamemode.toString() + " MAP " + mapNumber + ". THE FOLDER IS EITHER EMPTY OR DOES NOT EXIST."); BlackGeckoServer.logger.warn("***********************************************************************"); } } catch (IOException e) { } } this.state=EnumGameState.WAITING; } public void start() { } public void messageToAllPlayers(IChatComponent message){ for (EntityPlayer player : this.players) { player.addChatMessage(message); } } public void setAllPlayerXP(int level){ for (EntityPlayer player : this.players) { player.experienceLevel= level; } } } And here is the minigame that starts the countdown: package com.blackgeckogames.server.mod.minigames.skybattle; import java.util.ArrayList; import java.util.List; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.util.ChatComponentText; import net.minecraft.util.EnumChatFormatting; import net.minecraft.util.IChatComponent; import com.blackgeckogames.server.mod.minigames.GameMode; public class SkyBattle extends GameMode{ public int teamNumber; public int playerPerTeam; public List[] teams ={new ArrayList<EntityPlayer>(),new ArrayList<EntityPlayer>(),new ArrayList<EntityPlayer>(),new ArrayList<EntityPlayer>(),new ArrayList<EntityPlayer>(),new ArrayList<EntityPlayer>(),new ArrayList<EntityPlayer>(),new ArrayList<EntityPlayer>()}; public SkyBattle(int i) { this(i,1, 2,1); } public SkyBattle(int i, int map, int teams, int playersPerTeam) { this.dim=i; this.map=map; this.teamNumber=teams; this.playerPerTeam=playersPerTeam; this.state=EnumGameState.RESTARTING; this.resetWorld(EnumGameMode.SKY_BATTLE, this.map, this.dim); } @Override public void joinPlayer(EntityPlayer player){ players.add(player); System.out.println("joining SKY BATTLE"); if(this.players.size()>=this.playerPerTeam*this.teamNumber){ for (EntityPlayer player1 : this.players) { player1.addChatComponentMessage(new ChatComponentText("The game has started.")); this.countdown(); } } } @Override public void disconnectPlayer(EntityPlayer player){ players.remove(player); for(int i=0;i<this.teams.length;i++){ if(this.teams[i].contains(player)) this.teams[i].remove(player); } } public void joinTeam(EntityPlayer player, EnumTeam team){ if(team.ordinal()<this.teamNumber){ if(teams[team.ordinal()].contains(player)){ teams[team.ordinal()].remove(player); player.addChatComponentMessage(new ChatComponentText("You left Team " + team.toString())); } else { if(teams[team.ordinal()].size()<this.playerPerTeam){ for(int i=0; i<teams.length;i++){ if(teams[i].contains(player)){ teams[i].remove(player); } } teams[team.ordinal()].add(player); player.addChatComponentMessage(new ChatComponentText(team.getChatColor() + "You joined team " + team.toString() + ".")); player.setCustomNameTag(team.getChatColor()+player.getName()); } else { player.addChatComponentMessage(new ChatComponentText("This team is already full.")); } } } else { player.addChatComponentMessage(new ChatComponentText(EnumChatFormatting.DARK_RED+"The team you were about to join does not exist.")); } } public void autojoinTeam(){ for (EntityPlayer player : this.players) { boolean isInTeam=false; for(int i=0;i<this.teams.length;i++){ if(this.teams[i].contains(player)) isInTeam=true; } if(!isInTeam){ for(int i=0;i<this.teams.length;i++){ if(this.teams[i].size()<this.playerPerTeam){ this.teams[i].add(player); player.addChatComponentMessage(new ChatComponentText(EnumTeam.values()[i].getChatColor() + "You joined team " + EnumTeam.values()[i].toString() + ".")); player.setCustomNameTag(EnumTeam.values()[i].getChatColor() +player.getName()); break; } } } } } @Override public void start(){ this.autojoinTeam(); this.messageToAllPlayers(new ChatComponentText(EnumChatFormatting.AQUA +"The game has started!")); this.messageToAllPlayers(new ChatComponentText(EnumChatFormatting.WHITE +"As long as your beacon is not destroyed you can respawn at you base.")); this.messageToAllPlayers(new ChatComponentText(EnumChatFormatting.WHITE +"Try to destroy the enemies beacon.")); } } Oh, nearly forgot the crash: [15:44:15] [server thread/ERROR] [FML]: Exception caught during firing event net.minecraftforge.fml.common.gameevent.TickEvent$ServerTickEvent@1653f459: java.util.ConcurrentModificationException at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:886) ~[?:1.8.0_05] at java.util.ArrayList$Itr.next(ArrayList.java:836) ~[?:1.8.0_05] at com.blackgeckogames.server.mod.events.TickHandlerDimensionShift.onServerTick(TickHandlerDimensionShift.java:30) ~[TickHandlerDimensionShift.class:?] at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_16_TickHandlerDimensionShift_onServerTick_ServerTickEvent.invoke(.dynamic) ~[?:?] at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:55) ~[ASMEventHandler.class:?] at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:138) [EventBus.class:?] at net.minecraftforge.fml.common.FMLCommonHandler.onPreServerTick(FMLCommonHandler.java:268) [FMLCommonHandler.class:?] at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:587) [MinecraftServer.class:?] at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:478) [MinecraftServer.class:?] at java.lang.Thread.run(Thread.java:745) [?:1.8.0_05] [15:44:16] [server thread/ERROR] [FML]: Index: 1 Listeners: [15:44:16] [server thread/ERROR] [FML]: 0: NORMAL [15:44:16] [server thread/ERROR] [FML]: 1: ASM: com.blackgeckogames.server.mod.events.TickHandlerDimensionShift@34ee488f onServerTick(Lnet/minecraftforge/fml/common/gameevent/TickEvent$ServerTickEvent;)V [15:44:16] [server thread/ERROR]: Encountered an unexpected exception java.util.ConcurrentModificationException at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:886) ~[?:1.8.0_05] at java.util.ArrayList$Itr.next(ArrayList.java:836) ~[?:1.8.0_05] at com.blackgeckogames.server.mod.events.TickHandlerDimensionShift.onServerTick(TickHandlerDimensionShift.java:30) ~[TickHandlerDimensionShift.class:?] at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_16_TickHandlerDimensionShift_onServerTick_ServerTickEvent.invoke(.dynamic) ~[?:?] at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:55) ~[ASMEventHandler.class:?] at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:138) ~[EventBus.class:?] at net.minecraftforge.fml.common.FMLCommonHandler.onPreServerTick(FMLCommonHandler.java:268) ~[FMLCommonHandler.class:?] at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:587) ~[MinecraftServer.class:?] at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:478) [MinecraftServer.class:?] at java.lang.Thread.run(Thread.java:745) [?:1.8.0_05] Any ideas why this is not working? EDIT: Just noticed that i called my tick event class *DimensionShift. Pls ignore the strange name. (Its now called BlackGeckoServerTickHandler) Here could be your advertisement!
February 23, 201510 yr ConcurrentModificationException: You are modifying an array while another thread is trying to iterate through it. Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable. If you think this is the case, JUST REPORT ME. Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice. Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked. DO NOT PM ME WITH PROBLEMS. No help will be given.
February 23, 201510 yr Make a copy of the games array list using new ArrayList<>(games). Iterate over that. Then modify the elements in the old array list as you wish. Maker of the Craft++ mod.
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.