Engineer Class help CTF

Discussion in 'Plugin Development' started by renaud444, Feb 26, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    renaud444

    Hey, i was wondering if anyone could help me with this Custom class for the CTF plugin. I have already started it, and have the turret to where it will work, then destroy itself when the owner dies or when it runs out of ammo. Please let me know if i have developed any bad habits here or if anything would make this cause memory leaks. Here is the source:
    Show Spoiler

    Code:java
    1. import java.util.ArrayList;
    2. import java.util.HashMap;
    3. import java.util.List;
    4.  
    5. import net.askarian.MisterErwin.CTF.API.CtFClass;
    6.  
    7. import org.bukkit.Location;
    8. import org.bukkit.Material;
    9. import org.bukkit.block.BlockFace;
    10. import org.bukkit.enchantments.Enchantment;
    11. import org.bukkit.entity.Arrow;
    12. import org.bukkit.entity.Bat;
    13. import org.bukkit.entity.Entity;
    14. import org.bukkit.entity.EntityType;
    15. import org.bukkit.entity.Player;
    16. import org.bukkit.event.EventHandler;
    17. import org.bukkit.event.EventPriority;
    18. import org.bukkit.event.HandlerList;
    19. import org.bukkit.event.Listener;
    20. import org.bukkit.event.block.Action;
    21. import org.bukkit.event.block.BlockPlaceEvent;
    22. import org.bukkit.event.entity.PlayerDeathEvent;
    23. import org.bukkit.event.player.PlayerInteractEvent;
    24. import org.bukkit.inventory.ItemStack;
    25. import org.bukkit.inventory.PlayerInventory;
    26. import org.bukkit.util.Vector;
    27.  
    28. public class Engineer extends CtFClass implements Listener {
    29. private String version = "1.0";
    30. private String name = "Engineer";
    31. private String description = "Engineer class";
    32. private String command = "engineer";
    33. int ThreadID;
    34. Turret turret;
    35. Arrow arrow;
    36. int threaded;
    37. int nplayerleg;
    38. Location loc;
    39. private List<String> authors = new ArrayList();
    40. private HashMap<Integer, String> turrets = new HashMap();
    41.  
    42. public void disable() {
    43. BlockPlaceEvent.getHandlerList();
    44. HandlerList.unregisterAll(this);
    45. }
    46.  
    47. public void enable() {
    48. this.authors.add("RedstoneEditor");
    49.  
    50. this.plugin.getServer().getPluginManager()
    51. .registerEvents(this, this.plugin);
    52. }
    53.  
    54. public List<String> getAuthors() {
    55. return this.authors;
    56. }
    57.  
    58. public String getCommand() {
    59. return this.command;
    60. }
    61.  
    62. public String getDescription() {
    63. return this.description;
    64. }
    65.  
    66. public String getName() {
    67. return this.name;
    68. }
    69.  
    70. public String getVersion() {
    71. return this.version;
    72. }
    73.  
    74. public boolean givekit(Player p) {
    75. PlayerInventory inv = p.getInventory();
    76.  
    77. ItemStack item = new ItemStack(302, 1);
    78. inv.setHelmet(item);
    79.  
    80. item = new ItemStack(311, 1);
    81. inv.setChestplate(item);
    82.  
    83. item = new ItemStack(312, 1);
    84. inv.setLeggings(item);
    85.  
    86. item = new ItemStack(305, 1);
    87. inv.setBoots(item);
    88.  
    89. item = new ItemStack(Material.DISPENSER, 1);
    90. inv.addItem(new ItemStack[] { item });
    91. item.removeEnchantment(Enchantment.DAMAGE_ALL);
    92. item.removeEnchantment(Enchantment.KNOCKBACK);
    93.  
    94. item = new ItemStack(364, 4);
    95. inv.addItem(new ItemStack[] { item });
    96.  
    97. return true;
    98. }
    99.  
    100. public boolean saytime(Player p, int time) {
    101. p.setLevel(time);
    102. return true;
    103. }
    104.  
    105. @EventHandler
    106. public void playerDeath(PlayerDeathEvent event) {
    107. if (event.getEntity() instanceof Player) {
    108. Player p = (Player) event.getEntity();
    109. if (plugin.tm.getPClass(p) == getCommand()) {
    110. turret.destroy(turret.getLocation());
    111. }
    112. }
    113. }
    114.  
    115. @EventHandler(priority = EventPriority.HIGH)
    116. public void onPlayerInteract(PlayerInteractEvent event) {
    117. if ((event.getAction() != Action.LEFT_CLICK_BLOCK)
    118. && (event.getAction() != Action.LEFT_CLICK_AIR)) {
    119. return;
    120. }
    121. if (event.getPlayer().getInventory().getItemInHand().getType() == Material.DISPENSER) {
    122. Turret turret = new Turret(this, event.getPlayer().getName(),
    123. this.turrets.size() + 1, event.getClickedBlock()
    124. .getLocation());
    125. this.turret = turret;
    126. turret.build(turret.getLocation());
    127. this.turrets.put(turret.getId(), event.getPlayer().getName());
    128. turret.fire();
    129. }
    130. }
    131.  
    132. class Turret {
    133. String owner;
    134. int ammo = 64;
    135. int id;
    136. Location loc;
    137.  
    138. public Turret(Engineer paramEngineer, String o, int id, Location loc) {
    139. this.owner = o;
    140. this.id = id;
    141. this.loc = loc;
    142. }
    143.  
    144. public void build(Location loc) {
    145. loc.getBlock().getRelative(BlockFace.UP).setType(Material.FENCE);
    146. loc.getBlock().getRelative(BlockFace.UP, 2)
    147. .setType(Material.DISPENSER);
    148. }
    149.  
    150. public void destroy(Location loc) {
    151. loc.getBlock().getRelative(BlockFace.UP).setType(Material.AIR);
    152. loc.getBlock().getRelative(BlockFace.UP, 2).setType(Material.AIR);
    153. turrets.remove(getId());
    154. plugin.getServer().getScheduler().cancelTask(ThreadID);
    155. ThreadID = 0;
    156. }
    157.  
    158. private void fire() {
    159. if (turrets.containsKey(getId())) {
    160.  
    161. final Location l = getNextBlockUp();
    162.  
    163. final Bat bat = (Bat) l.getWorld().spawnEntity(
    164. l.add(0.0D, 1.0D, 0.0D), EntityType.BAT);
    165.  
    166. plugin.getServer().getScheduler()
    167. .scheduleSyncRepeatingTask(plugin, new Runnable() {
    168.  
    169. public void run() {
    170. if (turret.ammo > 0) {
    171. int id = getId();
    172. List<Entity> lEnt = bat.getNearbyEntities(
    173. 10.0D, 10.0D, 10.0D);
    174. for (Entity ent : lEnt) {
    175. if ((ent instanceof Player)) {
    176. Player p = (Player) ent;
    177.  
    178. int h = 1;
    179.  
    180. if (p.getLocation().distance(
    181. bat.getLocation()) >= 8.0D) {
    182. h = 2;
    183. }
    184. if (p.isFlying()) {
    185. h = 1;
    186. }
    187.  
    188. Vector v1 = l.toVector();
    189. Vector v2 = p.getLocation()
    190. .add(0.0D, h, 0.0D)
    191. .toVector();
    192.  
    193. Vector st = v1.subtract(v2)
    194. .normalize();
    195. st.setX(st.getX() * -1.0D);
    196. st.setY(st.getY() * -1.0D);
    197. st.setZ(st.getZ() * -1.0D);
    198. if (plugin.tm.getTeam((Player) ent) != plugin.tm
    199. .getTeam(getOwner())) {
    200. Arrow sb = (Arrow) bat
    201. .launchProjectile(Arrow.class);
    202. sb.setVelocity(st);
    203. ammo--;
    204. System.out.println("Ammo: "
    205. + ammo);
    206. }
    207. bat.remove();
    208. }
    209. }
    210. } else if (ammo <= 0) {
    211. plugin.getServer().getScheduler()
    212. .cancelTask(ThreadID);
    213. ThreadID = 0;
    214. destroy(getLocation());
    215. }
    216. }
    217. }, 1L, 12L);
    218. }
    219. }
    220.  
    221. public int getId() {
    222. return this.id;
    223. }
    224.  
    225. public String getOwner() {
    226. return this.owner;
    227. }
    228.  
    229. public Location getLocation() {
    230. return this.loc;
    231. }
    232.  
    233. public Location getNextBlockUp() {
    234. return getLocation().getBlock().getRelative(BlockFace.UP, 3)
    235. .getLocation();
    236. }
    237. }
    238. }

     
  2. Offline

    Nitnelave

    Maybe put the Turret in its own class, and create a class for the runnable? It's getting quite difficult to read...
     
  3. Offline

    renaud444

    The plugin won't allow multiple classes per "class".
     
  4. Offline

    Rprrr

    renaud444
    You don't have to put a class in a class, you can create a new class file and put it there.
     
  5. Offline

    renaud444

    The plugin api WONT let you. It causes errors

    It said something about "If you have more than one class file, Multiply them." I am wondering what that means.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  6. Offline

    Nitnelave

    Create a file for each class.
     
  7. Offline

    renaud444

    I know that the pluginn requires that each java class be in one file!
     
  8. Offline

    Nitnelave

    Well, I don't know that plugin, and I don't know the API, so I can't help you there. Maybe ask on that plugin's forum?
     
  9. Offline

    renaud444

    The plugin creator is on vacation with school
     
  10. Offline

    tyzoid

    renaud444
    Plugin issues aside,
    Code:java
    1.  
    2. private String version = "1.0";
    3. private String name = "Engineer";
    4. private String description = "Engineer class";
    5. private String command = "engineer";
    6. // snip
    7. private List<String> authors = new ArrayList();
    8. private HashMap<Integer, String> turrets = new HashMap();
    9.  

    In your class header, you should consider making some of these variables final. For example, Your "version" variable won't be changing at runtime, and neither is your name, description, or possibly command. It makes your code much easier to maintain because you will prevent access to variables that shouldn't be granted access to.

    Again, make authors and turrets final as well. These have methods that allow you to modify the method, and there should be no need to redeclare either of them.
     
  11. Offline

    renaud444

    Thanks :3
     
  12. Offline

    MisterErwin

    And multiply classes worked also before ;) - (e.g. ninja)
     
Thread Status:
Not open for further replies.

Share This Page