Changing A monster spawner

Discussion in 'Plugin Development' started by billofbong, Dec 27, 2011.

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

    billofbong

    I want to be able to change a monster spawner by placing a sign above it saying:
    Code:
    [MS]
    Mobtype
    But that won't work. Here's what I got so far:
    Code:java
    1.  
    2. package org.awesomecraft.plugins.monsterspawners;
    3.  
    4. import org.bukkit.Material;
    5. import org.bukkit.block.Block;
    6. import org.bukkit.block.BlockFace;
    7. import org.bukkit.block.CreatureSpawner;
    8. import org.bukkit.entity.CreatureType;
    9. import org.bukkit.event.block.BlockListener;
    10. import org.bukkit.event.block.SignChangeEvent;
    11.  
    12. /**
    13.  *
    14.  * @author Will
    15.  */
    16. public class MonsterBlockListener extends BlockListener {
    17. @Override
    18. public void onSignChange(SignChangeEvent event) {
    19. Block block = event.getBlock().getRelative(BlockFace.DOWN); //Block underneath the sign
    20. if(block.getType() == Material.MOB_SPAWNER) {
    21. if(event.getLine(1).equalsIgnoreCase("[MS]")) {
    22. if(event.getLine(2) != null ) {
    23. CreatureSpawner spawner = (CreatureSpawner) block.getState();
    24. if(event.getLine(2).equalsIgnoreCase("Chicken")) {
    25. spawner.setCreatureType(CreatureType.CHICKEN);
    26. }
    27. if(event.getLine(2).equalsIgnoreCase("Cow")) {
    28. spawner.setCreatureType(CreatureType.COW);
    29. }
    30. if(event.getLine(2).equalsIgnoreCase("Creeper")) {
    31. spawner.setCreatureType(CreatureType.CREEPER);
    32. }
    33. if(event.getLine(2).equalsIgnoreCase("Ghast")) {
    34. spawner.setCreatureType(CreatureType.GHAST);
    35. }
    36. if(event.getLine(2).equalsIgnoreCase("Giant")) {
    37. spawner.setCreatureType(CreatureType.GIANT);
    38. }
    39. if(event.getLine(2).equalsIgnoreCase("Pig")) {
    40. spawner.setCreatureType(CreatureType.PIG);
    41. }
    42. if(event.getLine(2).equalsIgnoreCase("PigZombie") || event.getLine(2).equalsIgnoreCase("Pig Zombie")) {
    43. spawner.setCreatureType(CreatureType.PIG_ZOMBIE);
    44. }
    45. if(event.getLine(2).equalsIgnoreCase("Sheep")) {
    46. spawner.setCreatureType(CreatureType.SHEEP);
    47. }
    48. if(event.getLine(2).equalsIgnoreCase("Skeleton")) {
    49. spawner.setCreatureType(CreatureType.SKELETON);
    50. }
    51. if(event.getLine(2).equalsIgnoreCase("Slime")) {
    52. spawner.setCreatureType(CreatureType.SLIME);
    53. }
    54. if(event.getLine(2).equalsIgnoreCase("Spider")) {
    55. spawner.setCreatureType(CreatureType.SPIDER);
    56. }
    57. if(event.getLine(2).equalsIgnoreCase("Squid")) {
    58. spawner.setCreatureType(CreatureType.SQUID);
    59. }
    60. if(event.getLine(2).equalsIgnoreCase("Zombie")) {
    61. spawner.setCreatureType(CreatureType.ZOMBIE);
    62. }
    63. if(event.getLine(2).equalsIgnoreCase("Wolf")) {
    64. spawner.setCreatureType(CreatureType.WOLF);
    65. }
    66. if(event.getLine(2).equalsIgnoreCase("Cave Spider")) {
    67. spawner.setCreatureType(CreatureType.CAVE_SPIDER);
    68. }
    69. if(event.getLine(2).equalsIgnoreCase("Enderman")) {
    70. spawner.setCreatureType(CreatureType.ENDERMAN);
    71. }
    72. if(event.getLine(2).equalsIgnoreCase("Silverfish")) {
    73. spawner.setCreatureType(CreatureType.SILVERFISH);
    74. }
    75. if(event.getLine(2).equalsIgnoreCase("Ender Dragon")) {
    76. spawner.setCreatureType(CreatureType.ENDER_DRAGON);
    77. }
    78. if(event.getLine(2).equalsIgnoreCase("Villager")) {
    79. spawner.setCreatureType(CreatureType.VILLAGER);
    80. }
    81.  
    82. }
    83. else {
    84. event.setLine(2, "Error");
    85. }
    86. }
    87.  
    88. }
    89. }
    90.  
    91. }
    92.  


    I receive no errors, nothing. It doesn't actually respond even when I put nothing on the second line:
    Code:java
    1. if(event.getLine(1) != null) {
    2. //bla bla bla...
    3. }
    4. else {
    5. event.setLine(2, "Error");
    6. }


    Thanks,

    Billofbong
     
  2. Offline

    user_43347

    A sign has 4 lines, but they are not 1-4, they are 0-3, 0 being the first, 3 the last. Change those and see what happens. And if you want, I'll provide a "rough" copy of yours with all of that coded.
     
  3. Offline

    Sir Savary

    @steaks4uce
    I hate to say it, but you happen to be one of the biggest dicks on the forums. You're full of yourself, grow up.

    @billofbong
    When I get a chance later today, I can assist you with this code here. I'm just too tired atm to be productive.
     
  4. Offline

    user_43347

    If you want to get mad because I tried to help, suite yourself, but if you're coming out with guns blazing, you better expect return fire. How I say things, it's yours to interpret, but if you take everything like it's against you, you're just paranoid. And just for starting a war, you can't talk, because you need to grow up.

    I would imagine it would be something like this, also, I think you get the CreatureSpawner from the block and not it's state, but if not, it's an easy change. Also, this is just a rough version, some things might need changing.
    Code:
    Block spawner = event.getBlock().getRelative(BlockFace.DOWN);
    if (spawner.getType().equals(Material.MOB_SPAWNER)) { //Make sure it is a spawner
        if (event.getLine(0).equalsIgnoreCase("[MS]")) { //Make sure its the correct sign
             event.setLine(0, ChatColor.GREEN + "[MS]"); //Fancy effects, yay!
             CreatureType ctype = CreatureType.fromName(event.getLine(1)); //Try and get creature from the text
             if (ctype != null) { //Not sure if this is needed
                  CreatureSpawner cspawner = (CreatureSpawner) spawner; //Get the spawner from the block
                  try { //Just in case of error with the creaturetype being null
                      cspawner.setCreatureType(ctype);  //Set it to our type
                  } catch (Exception ex) {}
             }
        }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 22, 2016
Thread Status:
Not open for further replies.

Share This Page