Advertisement
Guest User

Sample script

a guest
May 11th, 2016
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var blocks = {};
  2.  
  3. function onGameInitializationEvent(event){    
  4.     handleStart();
  5. }
  6.  
  7. function onStart(){    
  8.     handleStart();
  9. }
  10.  
  11. function onGameStoppingServerEvent(event){
  12.     handleStop();
  13. }
  14.  
  15. function onStop(){
  16.     handleStop();
  17. }
  18.  
  19. function handleStart(){
  20.     fileManager.create("blocks.json");
  21.     var fileJSON = JSON.parse(fileManager.read("blocks.json"));
  22.  
  23.     if(fileJSON != null)
  24.         blocks = fileJSON;
  25.  
  26.     console.log("First plugin loaded!");
  27.  
  28.     commandManager.register(createCommand());
  29.     eventManager.register(createEventListenerBlockBreak());
  30. }
  31.  
  32. function handleStop(){
  33.     fileManager.write("blocks.json", JSON.stringify(blocks));
  34. }
  35.  
  36.  
  37. function createEventListenerBlockBreak(){
  38.     var eventListener = {
  39.             listener: function(event, player) {
  40.                 //Name of the block break
  41.                 var name = event.transactions[0].original.state.type.name+"";
  42.  
  43.                 //If the player is defined and the block is defined in the list of blocks, it send a message to the player that inform him how much money he received.
  44.                 if(player && blocks[name]){
  45.                     player.sendMessage(Text.of("You received ", TextColors.GREEN, blocks[name]+" "+economyService.currency.symbol.toPlain));
  46.                     economyService.addMoney(player, parseFloat(blocks[name]));
  47.                 }
  48.             },
  49.             event: org.spongepowered.api.event.block.ChangeBlockEvent.Break.class
  50.         };
  51.  
  52.     return eventListener;
  53. }
  54.  
  55. function createCommand(){
  56.     var command = {
  57.             description: "Set the amount of money to receive when you break this block.",
  58.             permission: "job.set.amount",
  59.             executor: function(commandSource, commandContext) {
  60.                 //If the commandSource is a player, it set the price of the block and informs the player the price has been set.
  61.                 if(commandSource instanceof Player.class){                    
  62.                     commandSource.sendMessage(Text.of("You set the amount to receive for ", TextColors.GREEN, commandContext.one("blockId").get+"", TextColors.WHITE, " to ", TextColors.GREEN, commandContext.one("amount").get+""+economyService.currency.symbol.toPlain));
  63.  
  64.                     blocks[commandContext.one("blockId").get+""] = Javascript.convertJSObjectToObject(commandContext.one("amount").get);
  65.                 }
  66.             },
  67.             commands: ["job", "jsJob"],
  68.             arguments: [
  69.                 {
  70.                     format: "onlyOne",
  71.                     name: "blockId"
  72.                 },
  73.                 {
  74.                     format: "onlyOne",
  75.                     type: "doubleNum",
  76.                     name: "amount"
  77.                 }
  78.             ]
  79.         };
  80.  
  81.     return command;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement