A silly plugin "Zombie Miner"

Discussion in 'Plugin Development' started by Splated, Feb 12, 2013.

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

    Splated

    I started making a silly plugin called "Zombie Miner"

    The idea is you have a zombie with a pickaxe strapped to a powered mine cart. He digs and places track as he goes. Until he runs out of track then brings back his loot.

    The problems I'm having:
    1. If a powered mine cart has a passenger it wont propel its self, but it makes smoke puffs.
    2. I Can't give my Zombie an Inventory, that can be opened with right click?
    3. I can't change the mine cart speed.
    Code:java
    1.  
    2. package info.nothingspecial.Zombie_Miner;
    3. import java.util.ArrayList;
    4. import java.util.List;
    5. import org.bukkit.Material;
    6. import org.bukkit.block.Block;
    7. import org.bukkit.entity.EntityType;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.entity.PoweredMinecart;
    10. import org.bukkit.entity.Zombie;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.EventPriority;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.event.block.Action;
    15. import org.bukkit.event.player.PlayerInteractEvent;
    16. import org.bukkit.event.vehicle.VehicleBlockCollisionEvent;
    17. import org.bukkit.event.vehicle.VehicleEntityCollisionEvent;
    18. import org.bukkit.event.vehicle.VehicleExitEvent;
    19. import org.bukkit.inventory.EntityEquipment;
    20. import org.bukkit.inventory.ItemStack;
    21. import org.bukkit.inventory.ShapedRecipe;
    22. import org.bukkit.inventory.meta.ItemMeta;
    23. import org.bukkit.plugin.java.JavaPlugin;
    24. public class Zombie_Miner extends JavaPlugin implements Listener{
    25. void info(String string) {
    26. this.getLogger().info(string);
    27. }
    28. @Override
    29. public void onEnable(){
    30. ItemStack ZomMineItm = this.ZombieCart();
    31. ShapedRecipe ZomMiner = new ShapedRecipe(ZomMineItm);
    32. ZomMiner.shape(" H ", "PZ ", " C ");
    33. ZomMiner.setIngredient('H', Material.GOLD_HELMET);
    34. ZomMiner.setIngredient('Z', Material.SKULL_ITEM, 2);
    35. ZomMiner.setIngredient('C', Material.POWERED_MINECART);
    36. ZomMiner.setIngredient('P', Material.GOLD_PICKAXE);
    37. getServer().addRecipe(ZomMiner);
    38. this.getServer().getPluginManager().registerEvents(this, this);
    39. }
    40. @Override
    41. public void onDisable() {
    42. }
    43. @EventHandler(priority = EventPriority.NORMAL)
    44. public void onMinecartsMove(VehicleEntityCollisionEvent event){
    45. if (!(event.getVehicle() instanceof PoweredMinecart)) return;
    46. if (event.getVehicle().getPassenger() instanceof Zombie){
    47. //Zombie ZomMiner= (Zombie) event.getVehicle().getPassenger();
    48. //PoweredMinecart pmc = (PoweredMinecart) event.getVehicle();
    49.  
    50.  
    51. info("event.getEntity()" + event.getEntity().getType());
    52. }
    53.  
    54.  
    55.  
    56. }
    57. @EventHandler(priority = EventPriority.NORMAL)
    58. public void b(VehicleBlockCollisionEvent event){
    59. if (!(event.getVehicle() instanceof PoweredMinecart)) return;
    60. if (event.getVehicle().getPassenger() instanceof Zombie){
    61. //Zombie ZomMiner= (Zombie) event.getVehicle().getPassenger();
    62. //PoweredMinecart pmc = (PoweredMinecart) event.getVehicle();
    63.  
    64.  
    65.  
    66. event.getBlock().getRelative(0, 1, 0).breakNaturally();
    67. event.getBlock().breakNaturally();
    68. event.getBlock().setType(Material.RAILS);
    69.  
    70.  
    71. }
    72. }
    73. @EventHandler(priority = EventPriority.NORMAL)
    74. public voidonVehicleUpdateEvent(org.bukkit.event.vehicle.VehicleUpdateEvent event){
    75. if (!(event.getVehicle() instanceof PoweredMinecart)) return;
    76. if (event.getVehicle().getPassenger() instanceof Zombie){
    77. //Zombie ZomMiner= (Zombie) event.getVehicle().getPassenger();
    78. PoweredMinecart pmc = (PoweredMinecart) event.getVehicle();
    79.  
    80.  
    81.  
    82. Block block = pmc.getWorld().getBlockAt(pmc.getLocation());
    83.  
    84.  
    85.  
    86. //info("block.getType()block.toString()" + block.getType().toString());
    87.  
    88. if (block.getRelative(0, 1, 0).getType().isSolid() )
    89. block.getRelative(0, 1, 0).breakNaturally();
    90.  
    91.  
    92.  
    93.  
    94. if (block.getLightLevel() < 6)
    95. block.getRelative(0, 1, 0).setType(Material.TORCH);
    96.  
    97. }
    98. }
    99. @EventHandler(priority = EventPriority.LOW)
    100. public void onPlayerInteract(PlayerInteractEvent event) {
    101. Player player = event.getPlayer();
    102. ItemStack inHand = player.getItemInHand();
    103. if (inHand.getType() == Material.POWERED_MINECART && event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    104. if (event.getClickedBlock().getType() == Material.RAILS ||
    105. event.getClickedBlock().getType() == Material.POWERED_RAIL ||
    106. event.getClickedBlock().getType() == Material.DETECTOR_RAIL ){
    107. if (! inHand.hasItemMeta()) return;
    108. if (! inHand.getItemMeta().getDisplayName().equalsIgnoreCase("Zombie Miner Cart") ) return;
    109. event.setCancelled(true);
    110. //player.getInventory().removeItem(player.getInventory().getItemInHand());
    111. PoweredMinecart pmc = event.getClickedBlock().getWorld().spawn(event.getClickedBlock().getLocation(), PoweredMinecart.class);
    112. Zombie ZomMiner = (Zombie) pmc.getWorld().spawnEntity(pmc.getLocation(),EntityType.ZOMBIE);
    113. EntityEquipment ee = ZomMiner.getEquipment();
    114. ee.setItemInHand(new ItemStack(Material.GOLD_PICKAXE ,1));
    115. ee.setHelmet(new ItemStack(Material.GOLD_HELMET ,1));
    116. ZomMiner.setBaby(true);
    117. pmc.setPassenger(ZomMiner);
    118. pmc.setSlowWhenEmpty(false);
    119. pmc.setMaxSpeed(0.1D);
    120.  
    121. }
    122. }
    123. }
    124. @EventHandler(priority = EventPriority.NORMAL)
    125. public voidonVehicleExitEvent(VehicleExitEvent event){
    126. if (!(event.getVehicle() instanceof PoweredMinecart)) return;
    127. if (event.getExited() instanceof Zombie){
    128. Zombie ZomMiner= (Zombie) event.getVehicle().getPassenger();
    129. ItemStack item = new ItemStack(Material.SKULL_ITEM, 1, (byte) 2);
    130. ZomMiner.getWorld().dropItemNaturally(ZomMiner.getLocation(), item);
    131. ZomMiner.remove();
    132. }
    133. }
    134. private ItemStack ZombieCart() {
    135. ItemStack Zcart = new ItemStack(Material.POWERED_MINECART,1);
    136. ItemMeta im = Zcart.getItemMeta();
    137. im.setDisplayName("Zombie Miner Cart");
    138. List<String> lore = new ArrayList<String>();
    139. lore.add("He Bites!");
    140. im.setLore(lore);
    141. Zcart.setItemMeta(im);
    142. return Zcart;
    143. }
    144. }
    145.  
     
  2. Offline

    bleachisback

    Q 1&3: You can set the speed of any entity by using ent.setVelocity(velocity). For you it would be:
    Code:java
    1. pmc.setVelocity(new Vector(x,0,z))


    Q 2: You can use openInventory() to open a specific inventory to a player, and then use InventoryCloseEvent to tell when the player has closed the inventory, as well as what they put in/took out.
     
  3. Offline

    RainoBoy97

    in onDisable put getServer().clearRecipes(); or else it will keep adding the recipe every reload/restart
     
  4. Offline

    Splated

    I got the minecart to Move! now I just need to figure out what direction. thank you!

    I switched the mine cart to a storage cart so I can use the storage carts inventory now.


    done :)
     
  5. Offline

    polo2673

    This looks like a really good plugin are you going to post it :D?
     
  6. Offline

    Splated

    If I can make it work lol

    This vector stuff makes my brain explode!
     
  7. Offline

    ZeusAllMighty11

    How do you know this?

    Thanks I guess
     
  8. Offline

    bleachisback

    Haha, vectors are pretty easy to understand, they're just basic trigonometry/physics. They basically work by adding the vector onto the location.
    ex: if you have a new Vector(0,5,0), it would be sent flying upwards at 5 m/s
    ex: if you have a new Vector(3,0,0), it would be sent moving at 3 m/s on the x axis
    ex: if you have a new Vector(0,0,-5), it would be sent moving at 5 m/s on the z axis, but backwards

    An easy way to get the correct vector is just to do this:
    Code:java
    1.  
    2. @EventHandler(priority = EventPriority.NORMAL)
    3. public void onVehicleMoveEvent(VehicleMoveEvent event)
    4. {
    5. Location current=event.getVehicle().getLocation();
    6. Location destination=//put where you want the minecart to go here
    7. Vector velocity=destination.subtract(current).toVector().normalize().multiply(4);//change the 4 to something else to make it faster or slower
    8. }
    9.  
     
    jorisk322 and microgeek like this.
  9. Offline

    ZachBora

    I wouldn't clear all recipes, what if only your plugin gets disabled? Then other plugins will lose their recipes right? Just remove yours.
     
  10. Offline

    RainoBoy97

    How I know that?
    Well, learning from the community ;)

    All plugins must addRecipe onEnable, so i dont see how this would affect anything :p

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

    bleachisback

    He is saying "what if this one plugin gets disabled, and no other plugin does", which wouldn't trigger onEnable for all of the other plugins.
     
    ZachBora likes this.
  12. Offline

    RainoBoy97

    Oh, sorry, I see. Thats right
     
  13. Offline

    RealDope

    Quick tip: Careful using display names to identify your objects, players can now set names with anvils.
     
  14. Offline

    Splated


    Well if they want to name a mine cart "Zombie Miner Cart" then they can cheat the system lol.
     
  15. Offline

    Splated

    ok I made a project, its awaiting approval.
    http://dev.bukkit.org/server-mods/zombie-miner/

    I got a lot of the bugs out, by checking the block behind the cart for track before digging, this stops the digging diagonal problem. But the zombie will dig ANY block Bedrock included.

    Is there a way to check a block vs a tool to see if that block can be broken by it?

    i tried block.breakNaturally(new ItemStack(Material.WOOD_PICKAXE ,1)); but it still breaks bedrock.

    I dont really want to make a list of every block it cant break.
     
  16. Offline

    RainoBoy97

    A tips to this, I renamed a block to "§-§aTurtle", the "§" will be there, but invisible, so you can check for that name + noone can rename to that ;)
     
  17. Offline

    stirante

    Yes, but it is from NMS code:
    Code:
    nsmItem.canDestroySpecialBlock(nmsBlock);
    
    Isn't it better if you just add §r which will delete default italics?
     
  18. I just wanted to fix some bad habbits from starting...
    No it will not, if you use /reload the recipes are reset by the server, the plugin must re-add them or they'll removed.
    If someone uses a plugin manager and choses to disable this specific plugin he will lose all his recipes because of that line.
    So basically NOT a good idea.
    If you have an internal reloading mechanism and you need to reconfigure the recipe you must remove that specific recipe.

    Apart from what everybody else already said, there are the Minecraft recipes and eventual MOD recipes (like Tekkit) to consider, so don't throw stuff like that without research next time :p
     
    ZeusAllMighty11 likes this.
  19. Offline

    RainoBoy97

     
  20. Offline

    Splated

    Is there a way to check for the recipe before adding it?
     
  21. why u think this is silly? this is just an awesome idea! i hope u can finish this plugin cus le me wants le it
     
  22. Offline

    Splated


    I added §r to my cart name.

    can we use nsmitem? i thought the last bukkit update blocked that sorta code.

    I have a new big problem the zombie disappears when he gets 127 blocks away from the player.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  23. isnt that becus the chunk doesnt load? idk but that what i think...
     
  24. Offline

    Splated

    Im testing this now so far its working but i may need to add some sorta of chunk load if a miner is in an unloaded chunk.

    Zombie ZomMiner = (Zombie) Cart.getPassenger();
    ZomMiner.setTicksLived(1);
    ZomMiner.setRemoveWhenFarAway(false);
     
  25. isnt there any way with what u can get the miners location, then the chunk and load it? like miner.getLocation().getWorld().getChunk().load(); or somthin, im just guessing..
     
  26. Offline

    Splated

    I dont think the server adds duplicate recipes every time I /reload "08:22:00 [INFO] 210 recipes" always same count.

    Chunk load was never triggered i think i need to load the chunk before he enters it.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  27. maybe get the miner Y location and add 16 to it, get that chunk and load ...

    edit: silly me, get x,z loc...

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

    Splated

    Y is up down,

    But thats what I'm thinking onVehicleMoveEvent get a block 2-3 blocks ahead of the cart and do my chunk "is loaded" test. The problem is figuring out what direction its moving I could test block in all 4 directions.


    But another problem is will the server just unload the chunk again?
     
  29. for the y stuff see my edit, for the server chunk unload tsuff idk,...
     
  30. Offline

    Splated

    I was thinking about adding something with Power Tracks and redstone torches

    Maybe a counter every 7 rails placed put a powered rail down for the 8th

    testing the chunk idea now

    cartblock.getRelative(2, 0, 0).getChunk().load();
    cartblock.getRelative(-2, 0, 0).getChunk().load();
    cartblock.getRelative(0, 0, 2).getChunk().load();
    cartblock.getRelative(0, 0, -2).getChunk().load();
     
Thread Status:
Not open for further replies.

Share This Page