Guest User

Untitled

a guest
May 14th, 2011
900
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package org.grimcraft.modules.itemshop;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileReader;
  6. import java.io.PrintWriter;
  7. import java.lang.reflect.Constructor;
  8. import java.util.ArrayList;
  9.  
  10. import org.bukkit.ChatColor;
  11. import org.bukkit.entity.Player;
  12. import org.grimcraft.ResourceLoader;
  13. import org.grimcraft.actor.Actor;
  14. import org.grimcraft.event.EventTrigger;
  15. import org.grimcraft.event.player.PlayerCommandEvent;
  16. import org.grimcraft.event.player.PlayerEventListener;
  17. import org.grimcraft.event.player.PlayerRightClickEvent;
  18. import org.grimcraft.item.Item;
  19. import org.grimcraft.module.Module;
  20.  
  21. import com.nijiko.coelho.iConomy.iConomy;
  22. import com.nijiko.coelho.iConomy.system.Account;
  23.  
  24. public class ItemShopModule extends Module implements PlayerEventListener {
  25. private ArrayList< ItemEntry > entries = new ArrayList< ItemEntry >();
  26.  
  27. public ItemShopModule() {
  28. setName( "Item Shop Module" );
  29. setDescription( "Adds an item shop module to grimcraft." );
  30. }
  31. @Override
  32. public void onEnable() {
  33. if ( !getDataFolder().exists() )
  34. getDataFolder().mkdir();
  35.  
  36. try {
  37. File saveFile = new File( getDataFolder(), "ItemShop.txt" );
  38.  
  39. if ( !saveFile.exists() )
  40. return;
  41.  
  42. FileReader freader = new FileReader( saveFile );
  43. BufferedReader reader = new BufferedReader( freader );
  44.  
  45. String line = null;
  46. while ( ( line = reader.readLine() ) != null ) {
  47. String[] pieces = line.split( "\u001E" );
  48.  
  49. String path = pieces[ 0 ];
  50. Double cost = Double.parseDouble( pieces[ 1 ] );
  51. String name = pieces[ 2 ];
  52. String desc = pieces[ 3 ];
  53.  
  54. ItemEntry entry = new ItemEntry( path, name, desc, cost );
  55.  
  56. entries.add( entry );
  57. }
  58. } catch ( Exception e ) {
  59. e.printStackTrace();
  60. }
  61. }
  62.  
  63. @Override
  64. public void onDisable() {
  65. try {
  66. File saveFile = new File( getDataFolder(), "ItemShop.txt" );
  67.  
  68. if ( !saveFile.exists() || saveFile.delete() )
  69. saveFile.createNewFile();
  70.  
  71. PrintWriter writer = new PrintWriter( saveFile );
  72.  
  73. for ( ItemEntry entry : entries ) {
  74. StringBuffer buffer = new StringBuffer();
  75.  
  76. buffer.append( entry.getItemClass() );
  77. buffer.append( "\u001E" + entry.getCost() );
  78. buffer.append( "\u001E" + entry.getAlias() );
  79. buffer.append( "\u001E" + entry.getDesc() );
  80.  
  81. writer.println( buffer.toString() );
  82. }
  83.  
  84. writer.flush();
  85. writer.close();
  86. } catch ( Exception e ) {
  87. e.printStackTrace();
  88. }
  89. }
  90.  
  91. @Override
  92. public EventTrigger[] getTriggeredEvents() {
  93. return new EventTrigger[] { EventTrigger.PLAYER_COMMAND };
  94. }
  95.  
  96. @Override
  97. public void onPlayerCommand( PlayerCommandEvent event, String[] args ) {
  98. if ( event.getCommand().equalsIgnoreCase( "/itemshop" ) ) {
  99. Player player = event.getPlayer();
  100. event.setParsed();
  101.  
  102. if ( args.length == 0 ) {
  103.  
  104. } else if ( args[ 0 ].equalsIgnoreCase( "list" ) ) {
  105. player.sendMessage( ChatColor.GOLD + "--- Item Shop" );
  106.  
  107. for ( ItemEntry entry : entries ) {
  108. player.sendMessage( entry.toString() );
  109. }
  110. } else if ( args[ 0 ].equalsIgnoreCase( "buy" ) ) {
  111. if ( args.length == 1 ) {
  112. player.sendMessage( "SYNTAX: " + ChatColor.GOLD + "/itemshop buy ( item name )" + ChatColor.WHITE + " - No parenthesis" );
  113.  
  114. return;
  115. }
  116.  
  117. StringBuffer buffer = new StringBuffer();
  118.  
  119. for ( int i = 1; i < args.length; i++ ) {
  120. if ( i != 1 )
  121. buffer.append( " " );
  122.  
  123. buffer.append( args[ i ] );
  124. }
  125.  
  126. ItemEntry entry = null;
  127.  
  128. for ( ItemEntry existing : entries ) {
  129. if ( existing.getAlias().equalsIgnoreCase( buffer.toString() ) ) {
  130. entry = existing;
  131.  
  132. break;
  133. }
  134. }
  135.  
  136. if ( entry == null ) {
  137. player.sendMessage( "That item is not for sale." );
  138.  
  139. return;
  140. }
  141.  
  142. Account account = iConomy.getBank().getAccount( player.getName() );
  143.  
  144. if ( account == null ) {
  145. player.sendMessage( "You do not have an iConomy account to buy the item with." );
  146.  
  147. return;
  148. }
  149.  
  150. if ( !account.hasEnough( entry.getCost() ) ) {
  151. player.sendMessage( "You do not have enough to afford " + ChatColor.GOLD + entry.getAlias() );
  152.  
  153. return;
  154. }
  155.  
  156. try {
  157. Class< ? > clazz = ResourceLoader.getLoader().getClass( entry.getItemClass() );
  158.  
  159. if ( clazz == null || !Item.class.isAssignableFrom( clazz ) )
  160. throw new Exception( "ItemShop: Class for '" + entry.getAlias() + "' is null or is not a subclass of Item" );
  161.  
  162. Constructor< ? extends Item > cons = clazz.asSubclass( Item.class ).getConstructor();
  163.  
  164. if ( cons == null )
  165. throw new Exception( "Could not obtain the empty constructor for '" + entry.getAlias() + "'" );
  166.  
  167. Item item = cons.newInstance();
  168.  
  169. if ( item == null )
  170. throw new Exception( "Could not create an instance of the class for '" + entry.getAlias() + "'" );
  171.  
  172. Actor.getActor( event.getPlayer() ).getInventory().addItem( item );
  173.  
  174. account.subtract( entry.getCost() );
  175.  
  176. player.sendMessage( "You have bought " + ChatColor.GOLD + entry.getAlias() + ChatColor.WHITE + " for " + ChatColor.GREEN + "$" + entry.getCost() );
  177. } catch ( Exception e ) {
  178. e.printStackTrace();
  179.  
  180. player.sendMessage( "There was an exception when purchasing the item, no funds have been deducted." );
  181.  
  182. return;
  183. }
  184. }
  185. } else if ( event.getCommand().equalsIgnoreCase( "/ais" ) ) {
  186. Player player = event.getPlayer();
  187.  
  188. if ( !player.isOp() )
  189. return;
  190. else
  191. event.setParsed();
  192.  
  193. if ( args.length == 0 ) {
  194.  
  195. } else if ( args[ 0 ].equalsIgnoreCase( "add" ) ) {
  196. // /ais add {path} {cost} {alias}
  197. if ( args.length != 5 ) {
  198. player.sendMessage( "SYNTAX: " + ChatColor.GOLD + "/ais add path cost display_name_here item_desc_here" );
  199.  
  200. return;
  201. }
  202.  
  203. Class< ? > clazz = ResourceLoader.getLoader().getClass( args[ 1 ] );
  204.  
  205. if ( clazz == null ) {
  206. player.sendMessage( "The path for the item's class is incorrect." );
  207.  
  208. return;
  209. }
  210.  
  211. Double cost = null;
  212.  
  213. try {
  214. cost = Double.parseDouble( args[ 2 ] );
  215. } catch ( NumberFormatException nfe ) {
  216. player.sendMessage( "The item's cost is malformed." );
  217.  
  218. return;
  219. }
  220.  
  221. if ( cost == null ) {
  222. player.sendMessage( "The item's cost is malformed." );
  223.  
  224. return;
  225. }
  226.  
  227. String alias = args[ 3 ].replaceAll( "_" , " " );
  228.  
  229. if ( entries.contains( alias ) ) {
  230. player.sendMessage( "An entry with that name already exists." );
  231.  
  232. return;
  233. }
  234.  
  235. ItemEntry entry = new ItemEntry( args[ 1 ], alias, args[ 4 ].replaceAll( "_" , " " ), cost );
  236.  
  237. entries.add( entry );
  238.  
  239. player.sendMessage( "The item has been added." );
  240. } else if ( args[ 0 ].equalsIgnoreCase( "remove" ) ) {
  241. if ( args.length != 2 ) {
  242. player.sendMessage( "SYNTAX: " + ChatColor.GOLD + "/ais remove item_name_here" );
  243.  
  244. return;
  245. }
  246.  
  247. int index = entries.indexOf( args[ 1 ].replaceAll( "_", " " ) );
  248.  
  249. if ( index == -1 ) {
  250. player.sendMessage( "The item has not been found." );
  251.  
  252. return;
  253. }
  254.  
  255. entries.remove( index );
  256.  
  257. player.sendMessage( "The item has been removed." );
  258. }
  259. }
  260. }
  261. @Override
  262. public void onPlayerRightClick(PlayerRightClickEvent event) {
  263. }
  264.  
  265. }
RAW Paste Data