Thread: Keyword Spawning "::item ags" instead of "::item 11694"

Page 1 of 4 123 ... LastLast
Results 1 to 10 of 35
  1. #1 Keyword Spawning "::item ags" instead of "::item 11694" 
    Super Donator

    Jack Daniels's Avatar
    Join Date
    Oct 2010
    Posts
    1,846
    Thanks given
    790
    Thanks received
    508
    Rep Power
    387
    Since alot of people don't like memorizing item id's it would be much easier to spawn items on a spawn server doing :pawn ags 100, item ::fury 4 than :pawn 11694 100
    therefore I created this very simple system...

    Basicly, all u need to do is add some keywords (which is done by moderators on my server) and then ur ready to go!


    I'm not converting this to retard-code style instead im giving u guys my commands system together with this command :3

    First of all, the Command class:

    Code:
    package org.hyperion.rs2.commands;
    
    import org.hyperion.rs2.model.Player;
    
    /**
     * 
     * @author Jack Daniels.
     *
     */
    public abstract class Command {
    	
    	/**
    	 * The type of minimum rights.
    	 */
    	public static final int 
    		NORMAL_RIGHTS = 0,
    		DONATOR_RIGHTS = 1,
    		MOD_RIGHTS = 2,
    		ADMIN_RIGHTS = 3,
    		OWNER_RIGHTS = 4;
    	
    	/**
    	 * Name of the command which would be used as ::<startsWith>.
    	 */
    	public final String startsWith;
    	
    	/**
    	 * The rights.
    	 */
    	private final int rights;
    	
    	/**
    	 * The method which executes the command.
    	 * @param player
    	 * @param input
    	 */
    	public abstract void execute(Player player, String input);
    	
    	/**
    	 * Gets the rights of the command.
    	 * @return the rights
    	 */
    	public int getRights() {
    		return rights;
    	}
    	
    	/**
    	 * Use this method to create a new command, this command can be added to the server 
    	 * by submitting this command in the CommandHandler class.
    	 */
    	public Command(String startsWith, int rights) {
    		this.startsWith = startsWith.toLowerCase();
    		this.rights = rights;
    	}
    	
    	/**
    	 * Removes the command name with space from the input
    	 * e.g. converts "update 30" to "30"
    	 * @param input
    	 * @returns the actual input of the command
    	 */
    	public String filterInput(String input) {
    		return input.replace(startsWith + " ", "");
    	}
    	
    	/**
    	 * Removes the command name with space from the input
    	 * and splits the remaining String into integer parts
    	 * e.g. converts "update 30" to {30}
    	 * @param input
    	 * @returns Integer Array of the input
    	 */
    	public int[] getIntArray(String input) {
    		input = filterInput(input);
    		String[] parts = input.split(" ");
    		int[] intArray = new int[parts.length];
    		for(int i = 0; i < intArray.length; i++) {
    			try {
    				intArray[i] = Integer.parseInt(parts[i]);
    			} catch(Exception e) {}
    		}
    		return intArray;
    	}
    }

    Now the CommandHandler class

    Code:
    package org.hyperion.rs2.commands;
    
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Iterator;
    
    import org.hyperion.Server;
    import org.hyperion.rs2.commands.impl.AdminsLogger;
    import org.hyperion.rs2.commands.impl.DemoteCommand;
    import org.hyperion.rs2.commands.impl.EpicRapeCommand;
    import org.hyperion.rs2.commands.impl.GiveDonatorPointsCommand;
    import org.hyperion.rs2.commands.impl.KeywordCommand;
    import org.hyperion.rs2.commands.impl.LvlCommand;
    import org.hyperion.rs2.commands.impl.ModCommand;
    import org.hyperion.rs2.commands.impl.RapeCommand;
    import org.hyperion.rs2.commands.impl.RecordingCommand;
    import org.hyperion.rs2.commands.impl.RestartServerCommand;
    import org.hyperion.rs2.commands.impl.ScreenshotCommand;
    import org.hyperion.rs2.commands.impl.SendiCommand;
    import org.hyperion.rs2.commands.impl.SkillCommand;
    import org.hyperion.rs2.commands.impl.SpawnCommand;
    import org.hyperion.rs2.commands.impl.YellCommand;
    import org.hyperion.rs2.model.GameObjectDefinition;
    import org.hyperion.rs2.model.Item;
    import org.hyperion.rs2.model.NPC;
    import org.hyperion.rs2.model.NPCDefinition;
    import org.hyperion.rs2.model.Player;
    import org.hyperion.rs2.model.Player.Rights;
    import org.hyperion.rs2.model.SpecialBar;
    import org.hyperion.rs2.model.SpellBook;
    import org.hyperion.rs2.model.World;
    import org.hyperion.rs2.model.container.Bank;
    import org.hyperion.rs2.model.container.ShopManager;
    import org.hyperion.rs2.model.content.ContentEntity;
    import org.hyperion.rs2.model.content.misc.RandomSpamming;
    import org.hyperion.rs2.model.content.misc.SpawnServerCommands;
    import org.hyperion.rs2.model.content.skill.Prayer;
    import org.hyperion.rs2.net.ActionSender;
    import org.hyperion.rs2.pf.Tile;
    import org.hyperion.rs2.pf.TileMap;
    import org.hyperion.rs2.pf.TileMapBuilder;
    import org.hyperion.rs2.util.TextUtils;
    
    /**
     * 
     * @author Jack Daniels.
     *
     */
    
    public class CommandHandler {
     
    	/**
    	 * HashMap to hold all the commands.
    	 */
    	private static HashMap<String,Command> commands = new HashMap<String,Command>();
    	
    	/**
    	 * Use this method to add commands to the server.
    	 * @param startsWith
    	 * @param command
    	 */
    	public static void submit(Command... cmds) {
    		for(Command cmd: cmds) {
    			commands.put(cmd.startsWith, cmd);
    		}
    	}
    	
    	/**
    	 * Use this method to check whether your command input has been processed.
    	 * @param startsWith
    	 * @param player
    	 * @param input
    	 * @returns true if the command was found in the commands hashmap and had the rights to execute.
    	 */
    	public static boolean processed(String startsWith, Player player, String input){
    		Command command = commands.get(startsWith);
    		if(command != null) {
    			switch(command.getRights()){
    			case Command.DONATOR_RIGHTS:
    				if(player.playerStatus == 0 && player.getRights().toInteger() == 0){
    					return false;
    				}
    				break;
    			case Command.MOD_RIGHTS:
    				if(player.getRights().toInteger() < 1){
    					return false;
    				}
    				break;
    			case Command.ADMIN_RIGHTS:
    				if(player.getRights().toInteger() < 2){
    					return false;
    				}
    				if(player.getRights() == Rights.ADMINISTRATOR && !player.isOwner())
    					AdminsLogger.getLogger().log(new Date() + "\t" + player.getName() + "\t" + input);
    				break;
    			case Command.OWNER_RIGHTS:
    				if(!player.isOwner()) {
    					return false;
    				}
    				break;
    			}
    			command.execute(player,input);
    			return true;
    		}
    		if(player.isOwner())
    			player.getActionSender().sendMessage("Command is null.");
    		return false;
    	}
    }
    Now the SpawnCommand class

    Code:
    package org.hyperion.rs2.commands.impl;
    
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.util.HashMap;
    import java.util.Map;
    
    import org.hyperion.rs2.commands.Command;
    import org.hyperion.rs2.model.Player;
    import org.hyperion.rs2.model.content.misc.ItemSpawning;
    
    /**
     * 
     * @author Jack Daniels.
     *
     */
    public class SpawnCommand extends Command {
    	
    	/**
    	 * A Map to hold the keywords.
    	 */
    	private static final Map<String,Integer> keywords = new HashMap<String,Integer>();
    	
    	/**
    	 * Gets the keywords Map.
    	 * @return the keywords map.
    	 */
    	public static Map<String,Integer> getMap() {
    		return keywords;
    	}
    	
    	/**
    	 * Constructs a new spawn command.
    	 * @param name
    	 */
    	public SpawnCommand(String name) {
    		super(name, Command.NORMAL_RIGHTS);
    	}
    
    	@Override
    	public void execute(Player player, String input) {
    		String keywordInput = filterInput(input);
    		String[] parts = keywordInput.split(" ");
    		String keyword = parts[0];
    		int amount = 1;
    		if(keywords.get(keyword) != null) {
    			try {
    				if(parts.length > 1)
    					amount = Integer.parseInt(parts[1]);
    				int id = keywords.get(keyword);
    				ItemSpawning.spawnItem(player, id, amount);
    			} catch(Exception e) {
    				player.getActionSender().sendMessage("Your command could not be parsed.");
    			}
    		} else {
    			int[] params = getIntArray(input);
    			int id = params[0];
    			if(params.length > 1)
    				amount = params[1];
    			ItemSpawning.spawnItem(player, id, amount);
    		}
    	}
    	
    	static {
    		/*
    		 * Saved commands loading.
    		 */
    		try {
    			BufferedReader in = new BufferedReader(new FileReader(KeywordCommand.SAVE_FILE));
    			String line;
    			while((line = in.readLine()) != null) {
    				String[] parts = line.split(",");
    				String keyword = parts[0];
    				try {
    					int id = Integer.parseInt(parts[1]);
    					keywords.put(keyword, id);
    				} catch(Exception e) {
    					continue;
    				}
    			}
    			in.close();
    		} catch(Exception e) {
    			e.printStackTrace();
    		}
    	}
    }
    and now the Keywordscommand class

    Code:
    package org.hyperion.rs2.commands.impl;
    
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    
    import org.hyperion.rs2.commands.Command;
    import org.hyperion.rs2.model.Player;
    import org.hyperion.rs2.model.Player.Rights;
    
    /**
     * 
     * @author Jack Daniels.
     *
     */
    public class KeywordCommand extends Command {
    
    	/**
    	 * The save file.
    	 */
    	public static final File SAVE_FILE = new File("./data/keywords.cfg");
    	
    	/**
    	 * Constructs a new Keyword Command.
    	 * @param startsWith
    	 */
    	public KeywordCommand(String startsWith) {
    		super(startsWith, Command.MOD_RIGHTS);
    	}
    
    	@Override
    	public void execute(Player player, String input) {
    		input = filterInput(input);
    		String[] parts = input.split(" ");
    		String keyword = parts[0];
    		if(SpawnCommand.getMap().get(keyword) != null) {
    			player.getActionSender().sendMessage("Keyword was already set before..");
    			if(player.getRights() == Rights.MODERATOR)
    				return;
    			if(player.getRights().toInteger() >= Rights.ADMINISTRATOR.toInteger()) {
    				try {
    					int id = Integer.parseInt(parts[1]);
    					int oldId = SpawnCommand.getMap().get(keyword);
    					if(id == oldId) {
    						player.getActionSender().sendMessage("That id was already set.");
    						return;
    					}
    					SpawnCommand.getMap().put(keyword, id);
    					save(keyword,id);
    					return;
    				} catch(Exception e) {
    					player.getActionSender().sendMessage("Command could not be parsed.");
    				}
    			}
    		}
    		try {
    			int id = Integer.parseInt(parts[1]);
    			SpawnCommand.getMap().put(keyword, id);
    			save(keyword,id);
    		} catch(Exception e) {
    			player.getActionSender().sendMessage("Command could not be parsed.");
    		}
    	}
    
    	/**
    	 * Saves the command to the <code>SAVE_FILE</code>.
    	 * @param keyword
    	 * @param id
    	 * @throws Exception
    	 */
    	private void save(String keyword, int id) throws Exception {
    		BufferedWriter out = new BufferedWriter(new FileWriter(SAVE_FILE, true));
    		out.write(keyword + "," + id);
    		out.newLine();
    		out.close();
    	}
    	
    
    }
    What u gotta now is only add these few lines somewhere

    Code:
    	CommandHandler.submit(new SpawnCommand("item"), new SpawnCommand("pickup"), new SpawnCommand("spawn"));
    	CommandHandler.submit(new KeywordCommand("setkeyword"));
    K then ur done :3
    Reply With Quote  
     

  2. Thankful users:


  3. #2  
    Ray
    Ray is offline
    Registered Member

    Join Date
    Dec 2011
    Posts
    1,100
    Thanks given
    139
    Thanks received
    564
    Discord
    View profile
    Rep Power
    2884
    Doesn't work for PI does it?
    Reply With Quote  
     

  4. #3  
    Banned

    Join Date
    Jun 2012
    Posts
    653
    Thanks given
    233
    Thanks received
    128
    Rep Power
    0
    Would if you learned to code and convert stuff.
    OT: Nice job Jack. I have to stand by my beliefs though. There is no need to create a new class for every command.
    Reply With Quote  
     

  5. Thankful users:


  6. #4  
    Super Donator

    Jack Daniels's Avatar
    Join Date
    Oct 2010
    Posts
    1,846
    Thanks given
    790
    Thanks received
    508
    Rep Power
    387
    Quote Originally Posted by God Eclipse View Post
    Would if you learned to code and convert stuff.
    OT: Nice job Jack. I have to stand by my beliefs though. There is no need to create a new class for every command.
    I'm not creating classes for every command....since these commands are like 30 lines they obviously shouldnt be put together
    in a huge class together with 300 other commands...

    Most of my commands are like this:

    Code:
    
    CommandHandler.submit(new Command("listonlinestaff", Command.MOD_RIGHTS) {
    			@Override
    			public void execute(Player player, String input) {
    				List<Player> onlineStaff = World.getWorld().getStaffManager().getOnlineStaff();
    				player.getActionSender().sendMessage("Staff online : " + onlineStaff.size());
    				for(Player staffMember: onlineStaff) {
    					player.getActionSender().sendMessage(staffMember.getName());
    				}
    			}
    		});
    I just submit commands from whatever class I want to submit them, this way my sql commands are stored in my sql-system related classes and not
    in one big class..
    Reply With Quote  
     

  7. #5  
    Super Donator

    Jack Daniels's Avatar
    Join Date
    Oct 2010
    Posts
    1,846
    Thanks given
    790
    Thanks received
    508
    Rep Power
    387
    Quote Originally Posted by Boss View Post
    Doesn't work for PI does it?
    Why wouldn't it work for PI? Do PI programmers use a different kind of Java or something?
    Reply With Quote  
     

  8. Thankful users:


  9. #6  
    Ray
    Ray is offline
    Registered Member

    Join Date
    Dec 2011
    Posts
    1,100
    Thanks given
    139
    Thanks received
    564
    Discord
    View profile
    Rep Power
    2884
    Quote Originally Posted by Jack Daniels View Post
    Why wouldn't it work for PI? Do PI programmers use a different kind of Java or something?
    You're an idiot. You ripped this from a mopar tutorial. Learn to give credits, I'd give proof to back up my statement but I don't see why I should waste my time on you for any longer.
    Reply With Quote  
     

  10. #7  
    Banned

    Join Date
    Jun 2012
    Posts
    653
    Thanks given
    233
    Thanks received
    128
    Rep Power
    0
    This coming from the one asking if it did or did not work for pi. Stfu kid.
    Reply With Quote  
     

  11. Thankful user:


  12. #8  
    Kneel before me.

    Join Date
    Sep 2012
    Posts
    202
    Thanks given
    202
    Thanks received
    91
    Rep Power
    125
    All that for one command??
    Reply With Quote  
     

  13. #9  
    Super Donator

    Jack Daniels's Avatar
    Join Date
    Oct 2010
    Posts
    1,846
    Thanks given
    790
    Thanks received
    508
    Rep Power
    387
    Quote Originally Posted by Awviks View Post
    All that for one command??
    Perhaps you should read the thread better, smartass, I said that I'm giving my Command System together with this command....
    Reply With Quote  
     

  14. #10  
    Super Donator

    Jack Daniels's Avatar
    Join Date
    Oct 2010
    Posts
    1,846
    Thanks given
    790
    Thanks received
    508
    Rep Power
    387
    Quote Originally Posted by Boss View Post
    You're an idiot. You ripped this from a mopar tutorial. Learn to give credits, I'd give proof to back up my statement but I don't see why I should waste my time on you for any longer.
    I don't even use mopar lol, someone either copied this from me or ur imagination is going wild
    Reply With Quote  
     

  15. Thankful user:


Page 1 of 4 123 ... LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Replies: 5
    Last Post: 04-14-2011, 08:14 AM
  2. Replies: 12
    Last Post: 11-26-2010, 01:53 AM
  3. "hed","head","headicon" command like ::emote Rep++
    By «I Gf I» ©£ in forum Help
    Replies: 12
    Last Post: 10-06-2009, 05:30 AM
  4. Replies: 23
    Last Post: 09-10-2009, 03:29 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •