Jump to content

CoalOres

Members
  • Posts

    86
  • Joined

  • Last visited

Everything posted by CoalOres

  1. I have discovered the source of the problem, now I am getting an array index out of bounds exception: Command: package com.megaparties; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import net.minecraft.command.CommandBase; import net.minecraft.command.CommandException; import net.minecraft.command.ICommandSender; public class wparty extends CommandBase { public String getCommandName() { return "wparty"; } public String getCommandUsage(ICommandSender sender) { return "wparty"; } public boolean canCommandSenderUseCommand(ICommandSender icommandsender) { return true; } public void processCommand(ICommandSender sender, String[] args) throws CommandException { com.megaparties.Events.cont = true; String path = "config/whitelist.txt"; String line; try { FileReader file = new FileReader(path); BufferedReader buffered = new BufferedReader(file); while ((line = buffered.readLine()) != null) { com.megaparties.Events.list.add(line); } buffered.close(); } catch (IOException e) { e.printStackTrace(); } } } Event: @SubscribeEvent public void onClientTick(TickEvent.ClientTickEvent event) { if (cont){ if(this.count < 5) { this.count++; } else { this.count = 0; String path = "config/whitelist.txt"; String line; if(i < list.size()){ Minecraft.getMinecraft().thePlayer.sendChatMessage("/party invite " + list.get(i)); i ++; System.out.println(i); System.out.println(list.size()); } else{ com.megaparties.Events.cont = false; i = 0; } } }
  2. I don't think that would quite work. You see I also have commands which write to the file and clear it whenever you activate them, so it needs to be re-read. I probably misunderstood that. Isn't there anything simpler I could do? Like if(i <= list.size()){ Minecraft.getMinecraft().thePlayer.sendChatMessage("/party invite " + list.get(i)); i ++; } I tried that and it still didn't work.
  3. Ok, I've done that, it works, save for the fact that the loop never ends... i never equals null and so it doesn't close. I can't put it at 0 because 0 still has a value, I need to detect when it has gone through the whole list.
  4. My New Code: @SubscribeEvent public void onClientTick(TickEvent.ClientTickEvent event) { if (cont == true){ if(this.count < 5) { this.count++; } else { this.count = 0; String path = "config/whitelist.txt"; String line; try { FileReader file = new FileReader(path); BufferedReader buffered = new BufferedReader(file); while ((line = buffered.readLine()) != null) { list.add(line); } int i = 0; if(list.get(i) != null){ Minecraft.getMinecraft().thePlayer.sendChatMessage("/party invite " + line); i ++; } else{ buffered.close(); com.megaparties.Events.cont = false; } } catch (IOException e) { e.printStackTrace(); } } } I can already see that it isn't going to work because i is set back to 0 every time it loops over.
  5. Got it confused with Python, I need to use list.get don't I?
  6. Yes we do, but it's your mod, and we are not going to write your mod for you. I've attempted to copy the data in the txt file to an arrayList but it is just resulting in a "The type of expression must be an array type but it is resolved to List<String>". This is the only way I can think of keeping track: @SubscribeEvent public void onClientTick(TickEvent.ClientTickEvent event) { if (cont == true){ if(this.count < 5) { this.count++; } else { this.count = 0; String path = "config/whitelist.txt"; String line; try { FileReader file = new FileReader(path); BufferedReader buffered = new BufferedReader(file); while ((line = buffered.readLine()) != null) { list.add(line); } int i = 0; if(list[i] != null){ Minecraft.getMinecraft().thePlayer.sendChatMessage("/party invite " + line); i ++; } else{ buffered.close(); com.megaparties.Events.cont = false; } } catch (IOException e) { e.printStackTrace(); } } } } There error is on the if(list != null)
  7. The "Null" was caused by my setting it to "Null" at the start. But now it only reads the first line and keeps reading only the first line, how would I resolve this?
  8. I set it to client and now it spams the chat relentlessly with "Invited null to the party" without stopping.
  9. package com.megaparties; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.FutureTask; import net.minecraft.client.Minecraft; import net.minecraft.command.CommandBase; import net.minecraft.command.CommandException; import net.minecraft.command.ICommandSender; import net.minecraft.util.ChatComponentText; import net.minecraft.util.EnumChatFormatting; import net.minecraft.util.IChatComponent; public class wparty extends CommandBase { private long waitTime; int time = 0; public boolean onTickInGame(float time, Minecraft minecraftInstance){ time++; return true; } public String getCommandName() { return "wparty"; } public String getCommandUsage(ICommandSender sender) { return "wparty"; } public boolean canCommandSenderUseCommand(ICommandSender icommandsender) { return true; } public void processCommand(ICommandSender sender, String[] args) throws CommandException { com.megaparties.Events.cont = true; } } The ontick in game is there for some reason, probably from old code, that the problem? EDIT: Removed it, not the problem. Still gives errors.
  10. Alright, this is my new code: package com.megaparties; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import net.minecraft.client.Minecraft; import net.minecraft.util.ChatComponentText; import net.minecraft.util.EnumChatFormatting; import net.minecraft.util.IChatComponent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.gameevent.TickEvent; public class Events { public static boolean cont = false; private int count = 0; //Called whenever the player is updated or ticked. @SubscribeEvent public void onPlayerTick(TickEvent.PlayerTickEvent event) { } //Called when the client ticks. @SubscribeEvent public void onClientTick(TickEvent.ClientTickEvent event) { } //Called when the server ticks. Usually 20 ticks a second. @SubscribeEvent public void onServerTick(TickEvent.ServerTickEvent event) { if (cont == true){ if(this.count < 2) { this.count++; } else { this.count = 0; String path = "config/whitelist.txt"; String line = null; try { FileReader file = new FileReader(path); BufferedReader buffered = new BufferedReader(file); if(buffered.readLine() != null){ Minecraft.getMinecraft().thePlayer.sendChatMessage("/party invite " + line); } else{ buffered.close(); com.megaparties.Events.cont = false; } } catch (IOException e) { e.printStackTrace(); } } } } //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) { } } When I run it, it does not execute the invite and spams the console with this error: What's up with this?
  11. I see... Do I need to create a boolean which is changed every time I operate the command so it will fire the reading?
  12. I'm not quite understanding you... I just need an integer to increase every tick, so that I can detect when it reaches a certain amount of ticks. How do I "tell the tick handler" to start counting?
  13. How do I register onServerTick if it is included in my command class though?
  14. It starts at 0 and should get bigger each tick... Is that not how it works? And there is a break statement for when 50 ticks have passed...
  15. Given that a go and it crashes my Minecraft when I use it (doesn't respond): public void processCommand(ICommandSender sender, String[] args) throws CommandException { String path = "config/whitelist.txt"; String line = null; try { FileReader file = new FileReader(path); BufferedReader buffered = new BufferedReader(file); sender.addChatMessage((IChatComponent)new ChatComponentText((Object)EnumChatFormatting.DARK_AQUA + "People on your invite list: ")); while ((line = buffered.readLine()) != null) { Minecraft.getMinecraft().thePlayer.sendChatMessage("/party invite " + line); while (true){ if (com.megaparties.Events.count >= 50){ break; } } } Main: package com.megaparties; import com.megaparties.WhitelistAdd; import com.megaparties.WhitelistClear; import com.megaparties.wparty; import net.minecraft.command.ICommand; import net.minecraftforge.client.ClientCommandHandler; import net.minecraftforge.fml.common.FMLCommonHandler; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.event.FMLInitializationEvent; import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; @Mod(modid="megap", name="Mega Parties", version="1.0", clientSideOnly=true, acceptedMinecraftVersions="[1.8.9]") public class FMain { @Mod.Instance(value="simpleafk") public static FMain instance; @Mod.EventHandler public void preInit(FMLPreInitializationEvent event) { } @Mod.EventHandler public void init(FMLInitializationEvent event) { Events event1 = new Events(); ClientCommandHandler.instance.registerCommand((ICommand)new WhitelistAdd()); ClientCommandHandler.instance.registerCommand((ICommand)new WhitelistClear()); ClientCommandHandler.instance.registerCommand((ICommand)new wparty()); FMLCommonHandler.instance().bus().register(event1); } } Events: package com.megaparties; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.gameevent.TickEvent; public class Events { public static int count = 0; //Called whenever the player is updated or ticked. @SubscribeEvent public void onPlayerTick(TickEvent.PlayerTickEvent event) { } //Called when the client ticks. @SubscribeEvent public void onClientTick(TickEvent.ClientTickEvent event) { } //Called when the server ticks. Usually 20 ticks a second. @SubscribeEvent public void onServerTick(TickEvent.ServerTickEvent event) { count ++; if (count > 50) { count = 0; } } //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) { } } Im using server ticks as it is supposedly more accurate.
  16. I've attempted the future task you suggested and this my code now (note I haven't enclosed it in runnable yet): public void processCommand(ICommandSender sender, String[] args) throws CommandException { MyCallable callable1 = new MyCallable(1000); FutureTask<String> futureTask1 = new FutureTask<String>(callable1); ExecutorService executor = Executors.newFixedThreadPool(1); String path = "config/whitelist.txt"; String line = null; try { FileReader file = new FileReader(path); BufferedReader buffered = new BufferedReader(file); sender.addChatMessage((IChatComponent)new ChatComponentText((Object)EnumChatFormatting.DARK_AQUA + "People on your invite list: ")); boolean cont = false; while ((line = buffered.readLine()) != null) { executor.execute(futureTask1); Minecraft.getMinecraft().thePlayer.sendChatMessage("/party invite " + line); while (true){ if (futureTask1.isDone()){ break; } } } import java.util.concurrent.Callable; public class MyCallable implements Callable<String> { private long waitTime; public MyCallable(int timeInMillis){ this.waitTime=timeInMillis; } @Override public String call() throws Exception { Thread.sleep(waitTime); //return the thread name executing this callable task return Thread.currentThread().getName(); } } However the delay seems to not work... What am I doing wrong?
  17. I'm waiting because, as I said, there is an anti spam protection which only allows commands to be entered every tenth of a second or so. And so far, I have not discovered a better method which would work in this situation, if you could recommend one I would appreciate it.
  18. I've asked this question a while ago, but I've become more familiar with modding now and were wondering if anybody has any better things to use than thread.sleep, as it freezes Minecraft when in use and seems to be inaccurate sometimes. This is my current Code: package com.megaparties; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import net.minecraft.client.Minecraft; import net.minecraft.command.CommandBase; import net.minecraft.command.CommandException; import net.minecraft.command.ICommandSender; import net.minecraft.util.ChatComponentText; import net.minecraft.util.EnumChatFormatting; import net.minecraft.util.IChatComponent; public class wparty extends CommandBase { public String getCommandName() { return "wparty"; } public String getCommandUsage(ICommandSender sender) { return "wparty"; } public boolean canCommandSenderUseCommand(ICommandSender icommandsender) { return true; } public void processCommand(ICommandSender sender, String[] args) throws CommandException { String path = "config/whitelist.txt"; String line = null; try { FileReader file = new FileReader(path); BufferedReader buffered = new BufferedReader(file); sender.addChatMessage((IChatComponent)new ChatComponentText((Object)EnumChatFormatting.DARK_AQUA + "People on your invite list: ")); while ((line = buffered.readLine()) != null) { Minecraft.getMinecraft().thePlayer.sendChatMessage("/party invite " + line); Thread.sleep(100); } buffered.close(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException z) { z.printStackTrace(); } } } As you can see I read a list of strings from a txt file and then send a chat message which includes these names. The issue is, thread.sleep freezes the client for however long it takes and sometimes will send them too quickly, resulting in the anti spam protection on the server not allowing it to go through. Does anybody have a better solution?
  19. That seems really complicated, is there any simpler way? I've seen stuff about scheduled executor delays?
  20. But surely that won't work because of the loop? And that still doesn't give me an actual delay in time.
  21. But this stuff about counting ticks? Sounds like it would be better.
  22. There is a command delay, you can't spam commands, so there needs to be a specific delay, you get kicked off the server if you spam too much, which this definitely does.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.