This is my first plugin and I am trying to create a plugin that when someone says /Rules or /rules it displays, from a text file, all the rules of the server. Here is my code for my two pages: Code: package com.bukkit.shawn.Basic; import java.io.File; import org.bukkit.Server; import org.bukkit.event.Event; import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginLoader; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.PluginManager; /** * Basic for Bukkit * * @author Saturisk */ public class Basic extends JavaPlugin{ private final BasicPlayerListener playerListener = new BasicPlayerListener(this); public Basic(PluginLoader pluginLoader, Server instance, PluginDescriptionFile desc, File folder, File plugin, ClassLoader cLoader) { super(pluginLoader, instance, desc, folder, plugin, cLoader); } @Override public void onDisable() { } @Override public void onEnable() { PluginManager pm = getServer().getPluginManager(); pm.registerEvent(Event.Type.PLAYER_COMMAND, this.playerListener, Event.Priority.Normal, this); } } that's my basic and this is my player command thing: Code: //The Package package com.bukkit.shawn.Basic; import org.bukkit.entity.Player; import org.bukkit.event.player.PlayerChatEvent; import org.bukkit.event.player.PlayerListener; public class BasicPlayerListener extends PlayerListener{ public static Basic plugin; public BasicPlayerListener(Basic instance) { plugin = instance; } public void onPlayerCommand(PlayerChatEvent event) { String[] split = event.getMessage().split(" "); Player player = event.getPlayer(); if ((split[0].equalsIgnoreCase("/rules")) || (split[0].equalsIgnoreCase("/Rules"))) { System.out.println( txtFile.getName() ; } } } I know the txtFile thing isn't right, is there an easier way to do this? This is as far as i got and i can't find anything about it.
Code: split[0].equalsIgnoreCase("/rules")) ||(split[0].equalsIgnoreCase("/Rules"))) 1) You dont need both /rules and /Rules, IgnoreCase is used to ignore case sensitive letters so you only need split[0].equalsIgnoreCase("/rules")) 2) Make something like this Code: if (chat[0].equalsIgnoreCase("/rules")) { player.sendMessage(ChatColor.DARK_AQUA + "Dont grief !!"); player.sendMessage(ChatColor.DARK_AQUA + "Dont eat my cake!!!");
Okay, do i have to extend it? Or could i fit this into one, how would i do that? AND how could i get it to read from a text file, if possible so i could change the code easily or could i just make a class over it? --- merged: Feb 24, 2011 1:11 AM --- It has enable and disable, i just want it so when someone does it, it goes --- merged: Feb 24, 2011 1:14 AM --- Sorry to post again, but it's having an error with the "chat[0]" how do i fix it? It has the red underliner on chat
Watch samkios tutorials: http://forums.bukkit.org/threads/basic-bukkit-plugin-tutorial.1339/ http://forums.bukkit.org/threads/intermediate-bukkit-plugin-tutorial.2548/ I can't tell you what error is it, may be you forgot to close the statment Code: if (chat[0].equalsIgnoreCase("/rules")) { player.sendMessage(ChatColor.DARK_AQUA + "Dont grief !!"); player.sendMessage(ChatColor.DARK_AQUA + "Dont eat my cake!!!"); }
To get it to read from a textfile: Code: //Put this in your onEnable method, it creates the file (new File(directory)).mkdir(); try { (new File(directory + "rules.txt")).createNewFile(); } catch (IOException ex) { log.info("Could not create rules file."); } //In the PlayerListener, create a method called read, this is what it should look like: public ArrayList<String> read() { String lineString; FileInputStream stream; ArrayList<String> readList = new ArrayList<String>(); try { stream = new FileInputStream(Basic.directory + "rules.txt"); DataInputStream in = new DataInputStream(stream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); lineString = br.readLine(); for(int i=0; lineString != null; i++) { readList.set(i, lineString); lineString = lineString + "\n"; } return readList; } catch (Exception e) { e.printStackTrace(); return null; } } //And in PlayerListener, where you check if the person says /rules, put this ArrayList<String> temp = read(); int size = temp.size(); for(int i=0; i<size; i++) { String msg = temp.get(i); player.sendMessage(msg); //I assume you already have the player as event.getPlayer(); Also, my way is probably overly complicated. Though this is how I do it! EDIT: It's also un-tested. Most of my code that deals with files fails anyway, but it would be worth a try.