Guest User

Untitled

a guest
May 28th, 2011
671
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.80 KB | None | 0 0
  1. Java: Minecraft Plugin
  2.  
  3. Things you need
  4. -Eclipse, free java programming tool
  5. -Bukkit, the modding API that loads and handles the plugins
  6. -Minecraft
  7.  
  8. The setup
  9. Start Eclipse, which will be the program we're working with.
  10. On the left hand side should be the Package Explorer. Right click and go to New>Java Project. Name your project “TorchPlacement”.
  11. Expand your project folder and right click "src" (source), then create a new package called yourname.tp (as in the initials of the plugin).
  12. Now we'll have to create two classes inside or yourname.tp. Call the first one "tp" and the second one "tpBlockListener". Tp will be the main class, from which every method is called and from which the plugin is enabled. tpBlockListener will "listen" to the blocks.
  13.  
  14. Now you’ll want to import the bukkit .jar file so we can activate the plugin for use and have all the functions we need to make everything work properly.
  15. To do this, right-click your project folder and click “Properties…”.
  16. Then go to “Java build path” and import the jar file through “Add External JARs…”.
  17.  
  18. While writing the plugin we’re going to create, you will have to import certain parts of the bukkit jar file.
  19. Eclipse will tell you when there’s an error in the code with a red line underneath the part of the faulty code. Just hover the mouse over that piece of code and it will suggest some things which might fix the code. This includes importing classes from the bukkit jar. You will want to always import classes instead of changing the original code.
  20.  
  21. Double click tp.java and tpBlockListener.java in the package explorer to open them.
  22. Now we have to go to our main class "tp" and set the plugin up.
  23. First you'll want to edit the third line to:
  24.  
  25. public class tp extends JavaPlugin{
  26.  
  27. }
  28.  
  29. This will start the plugin when loaded into bukkit.
  30. You'll have to place all of the following to come inside the previous method. To do that, just write your code in between "{" and "}".
  31. Now we have to create 2 variables:
  32.  
  33. private final HashMap<Player, Boolean> debugees = new HashMap<Player, Boolean>();
  34. private final tpBlockListener playerListen = new tpBlockListener
  35.  
  36. The first variable will debug the players, the second one creates a listener for the blocks.
  37. After this, we'll want to create the "onEnable" method and make it say "Plugin Enabled" in the server console.
  38.  
  39. public void onEnable(){
  40. System.out.printIn("[TorchPlacement]: Plugin enabled.");
  41. }
  42.  
  43. "public" means that other classes can use this method. It can be set to private as well.
  44. "void" means that it's a new function.
  45. "onEnable()" is the indicator on when the method is called.
  46. "System.out.printIn();" prints a message to the console.
  47.  
  48. It’s the same with the plugin being disabled.
  49.  
  50. public void onDisable(){
  51. System.out.printIn("[TorchPlacement]: Plugin disabled");
  52. }
  53.  
  54. The following will debug the players if needed.
  55.  
  56. public boolean isDebugging(final Player player){
  57. if(debugees.containsKey(player)){
  58. return debugees.get(player);
  59. }else{
  60. return false;
  61. }
  62. }
  63.  
  64. The last thing we have to do in the main class is to activate the player listener at a certain action. Our action will be once somebody places a block or object.
  65. In the “onEnable” method, insert this piece of code:
  66.  
  67. getServer().getPluginManager().registerEvent(Event.Type.BLOCK_PLACE, (Listener) blockListener, Priority.Normal, this);
  68.  
  69. This is one line, but the document is too short for it to place them all in one line here.
  70. Your "tp" class should look like this now.
  71.  
  72. package yourname.tp;
  73.  
  74. import java.util.HashMap;
  75.  
  76. import org.bukkit.entity.Player;
  77. import org.bukkit.event.Event;
  78. import org.bukkit.event.Listener;
  79. import org.bukkit.event.Event.Priority;
  80. import org.bukkit.plugin.java.JavaPlugin;
  81.  
  82. import yourname.tp.tpBlockListener;
  83.  
  84. public class tp extends JavaPlugin{
  85.  
  86. private final HashMap<Player, Boolean> debugees = new HashMap<Player, Boolean>();
  87. private final tpBlockListener playerListen = new tpBlockListener
  88.  
  89. public void onEnable(){
  90. System.out.printIn("[TorchPlacement]: Plugin enabled.");
  91. getServer().getPluginManager().registerEvent(Event.Type.BLOCK_PLACE, (Listener) blockListener, Priority.Normal, this);
  92.  
  93. }
  94.  
  95. public void onDisable(){
  96. System.out.printIn("[TorchPlacement]: Plugin disabled");
  97. }
  98.  
  99. public boolean isDebugging(final Player player){
  100. if(debugees.containsKey(player)){
  101. return debugees.get(player);
  102. }else{
  103. return false;
  104. }
  105. }
  106. }
  107.  
  108.  
  109.  
  110.  
  111. The Player Listener:
  112.  
  113. First of all, you’ll want to make the class a player listener.
  114. Do this by adding “extends BlockListener” to the “public class”.
  115.  
  116. public class tpBlockListener extends BlockListener{
  117.  
  118. }
  119.  
  120. Now what we want to do, is make a method that is called when a player places a block.
  121. In the reference libraries tab on the left hand side, you’ll find the plugin you imported. There will be all ways to call a method or listeners. For this, we’ll want to scroll down to “org.bukkit.event.block”. Then expand “BlockListener.class”. All the listeners for blocks are in that folder. For the our project, we’ll pick “onBlockPlace(BlockPlaceEvent)”.
  122.  
  123. onBlockPlace(BlockPlaceEvent event){
  124.  
  125. }
  126.  
  127. “event” is the name of the event. You can change this to anything you want, but I always like to use event for these things.
  128.  
  129. Let’s send the player a message now.
  130.  
  131. Player player = event.getPlayer();
  132. player.sendMessage(“You placed a “ + event.getBlock ().getType());
  133.  
  134. First, we made the variable player.
  135. The first word “Player” is the type of variable.
  136. The second player is the name of the variable.
  137. Then we declare that player is the one who placed the block.
  138.  
  139. The second line sends “player” a message.
  140. Everything in “ ” is printed directly.
  141. After that we are going to get the block placed.
  142. ‘Event.getBlock()’ will get the block placed in the event.
  143. ‘getType()’ will then get the type of the block from the variable on the left which is the block placed.
  144.  
  145. Now if you’ll test it, it’ll send you a message of every single block you placed.
  146. We only want to send a message when a torch is placed though.
  147.  
  148. We’re going to use a “if()” function for that.
  149. We’re going to add in the message right away with it.
  150.  
  151. If(event.getBlock.getType().equals(Material.TORCH)){
  152. player.sendMessage(“You placed a torch!”);
  153. }
  154.  
  155. As the function gets the block type/material, we’ll want to compare it to the torch material.
  156. ‘Material.’ Is a class that is called when you need a material.
  157. If you wait for a split second after typing the ‘.’ you’ll get a list of materials. The material “TORCH” should be there.
  158.  
  159. We’ll also want to remove the message of any block placed.
  160.  
  161. Here’s the full code for the player listener:
  162.  
  163. package randy.tp;
  164.  
  165. import org.bukkit.Material;
  166. import org.bukkit.entity.Player;
  167. import org.bukkit.event.block.BlockListener;
  168. import org.bukkit.event.block.BlockPlaceEvent;
  169.  
  170. public class tpBlockListener extends BlockListener{
  171.  
  172. public void onBlockPlace(BlockPlaceEvent event){
  173. Player player = event.getPlayer();
  174. if(event.getBlock().getType().equals(Material.TORCH)){
  175. player.sendMessage("You placed a torch!");
  176. }
  177. }
  178. }
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191. Exporting:
  192.  
  193. Before exporting, we have to create a file that indicates the main class, version and creator.
  194.  
  195. Right click the “src” folder, and create > File…
  196. Name it ‘config.yml’.
  197. Open it and paste this in it:
  198.  
  199. name: TorchPlacement
  200. main: [yourname].tp.tp
  201. version: 1.0
  202.  
  203. Now save it, click the filebrowser and hit F5 to refresh the file browser and make it see the changes in the yml file.
  204. Your now set to export it.
  205.  
  206. In the tab ‘File’, go to ‘Export…’.
  207. Select ‘JAR file’ and hit ‘Next’.
  208. Tick your project and select the destination of the .jar file.
  209. Hit finish and copy paste the plugin to your ‘plugins’ directory in the server files.
Advertisement
Add Comment
Please, Sign In to add comment