Get players name in CreatureSpawnEvent

Discussion in 'Plugin Development' started by slater96, Oct 24, 2012.

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


    Hello, I need to be able to get the players name so that I can check if there in a hashset in creature spawn event but i'm not sure how to get it as it only gives me entity and if I try to cast it to a player then it errors alot. Anyone know how I can get it please?
        public void mobSpawns(CreatureSpawnEvent event) {
            SpawnReason spawn = event.getSpawnReason();
            if (plugin.TDM.contains(?????) {
                if (spawn.equals(SpawnReason.BUILD_IRONGOLEM) || spawn.equals(SpawnReason.BUILD_SNOWMAN) || spawn.equals(SpawnReason.CUSTOM) || spawn.equals(SpawnReason.DEFAULT) || spawn.equals(SpawnReason.EGG) || spawn.equals(SpawnReason.SPAWNER_EGG)) {
                    Entity e = event.getEntity();
  2. Offline


    There is no player in the spawn event. Use the PlayerInteractEvent or BlockPlaceEvent if you need a player.
  3. Offline


    How would I get the spawn reason and entity though if I do that?
  4. Offline

    Ewe Loon

    reading you code, it appears you are trying to detect when a player is building a irongolum ?
    if you watch the placing of blocks , and what is around them you can do this
    if you are trying to stop them from creating irongolums you can cancel the placement of the last block
    A little more information for the reason might help find a better solution
  5. Offline


    I'm checking for any mobs created by a player via spawn eggs or building a golem as well as plugins which spawn mobs and villagers spawned by spawn eggs which fall under the default spawn reason. This is so I can clear the mobs after a zombie horde has finished but I need to check if there in a hashset first so it only targets the mobs I want to clear later which is why i'm storing there uuid.

    Can anyone help me please? Really need to get this sorted tonight.

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


    You'll have to improvise.
    For the iron golem/snow golem, i suggest manually checking the blocks themselves on a block place event. when they place the head, check the two blocks under for snow (snowman) or check the two blocks for iron, and the arms (iron golem).

    As for spawning mobs, listen for a player interact event. If they interact with a block, and theyre holding a spawn egg, then you know theyre trying to spawn a mob.
  7. Offline


        public void onMobSpawn(CreatureSpawnEvent event) {
            Entity ent = event.getEntity();
            if(ent instanceof /*what ever here*/) {
                //Do what ever
  8. Offline


    Another solution is to simply keep a record of the location of every player placed block, and then look that up once a creature is spawned. Then you don't have to tie your plugin to the specific rules for spawning mobs. It will also be easier to update if Mojang adds more constructable mobs.
    1. public class ExampleMod extends JavaPlugin implements Listener {
    3. // Don't keep a reference to either the Player nor the Location
    4. private ConcurrentMap<Player, Location> lastPlaced = new MapMaker().
    5. weakKeys().
    6. weakValues().
    7. makeMap();
    9. @Override
    10. public void onEnable() {
    11. PluginManager manager = getServer().getPluginManager();
    12. manager.registerEvents(this, this);
    13. }
    15. @EventHandler
    16. public void onBlockPace(BlockPlaceEvent event) {
    17. Player player = event.getPlayer();
    18. Location location = event.getBlock().getLocation();
    20. // Save the location of the last placed block
    21. lastPlaced.put(player, location);
    22. }
    24. @EventHandler
    25. public void onCreatureSpawn(CreatureSpawnEvent event) {
    27. SpawnReason reason = event.getSpawnReason();
    29. // Make sure this creature was "built"
    30. if (reason == SpawnReason.BUILD_IRONGOLEM || reason == SpawnReason.BUILD_SNOWMAN) {
    31. Player responsible = findNearestPlayerPlacedBlock(event.getLocation(), 10);
    32. String creatureName = event.getEntityType().getName();
    34. if (responsible != null) {
    35. getServer().broadcastMessage("Creature " + creatureName + " was spawned by " + responsible.getName());
    36. } else {
    37. getServer().broadcastMessage("Could not find creator of " + creatureName);
    38. }
    39. }
    40. }
    42. private Player findNearestPlayerPlacedBlock(Location location, int maximumDistance) {
    43. Player best = null;
    44. double bestDistance = Double.MAX_VALUE;
    46. // Simple linear search
    47. for (Player player : location.getWorld().getPlayers()) {
    48. Location lastPlacedBlock = lastPlaced.get(player);
    50. if (lastPlacedBlock != null) {
    51. double distance = location.distanceSquared(lastPlacedBlock);
    53. if (distance < bestDistance && distance < maximumDistance) {
    54. best = player;
    55. distance = bestDistance;
    56. }
    57. }
    58. }
    59. return best;
    60. }
    61. }

    Notice that I'm not using a typical HashMap to store the last placed block. This is because you should never keep a reference to a Player or a Location directly, as it would prevent the garbage collector from properly cleaning up a world or a player. Instead, I use Guava's MapMaker to construct a special map that only references the key and the value "weakly".
Thread Status:
Not open for further replies.

Share This Page