1

Okay, so I am attempting to make a custom spawners plugin, but I've already hit a bit issue.. I cannot figure out how to change what creature the spawner summons. The code I have currently can be found below (This is a SpawnerSpawnEvent, also everything works other than the spawning of the skeleton, The console gets sent the 'File exists' message, The file does indeed exist (This is done in the block place event, I will also include this below, not sure if it is needed.) so I am very confused on how I could achieve this..) Thank you in advance for your time.

SpawnerSpawnEvent »

package me.askingg.events;

import java.io.File;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.block.CreatureSpawner;
import org.bukkit.entity.EntityType;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.SpawnerSpawnEvent;

import me.askingg.golems.Main;

    public class CreatureSpawn implements Listener {

        Main plugin;

        @EventHandler
        public void coalSpawn(SpawnerSpawnEvent event) {

            CreatureSpawner spawner = (CreatureSpawner) event.getSpawner().getBlock().getState();
            Location location = spawner.getLocation();
            String world = spawner.getWorld().getName().toString();

            File locationFile = new File("plugins/Golems/Locations", world + " - " + location.getBlockX() + "-"
                    + location.getBlockY() + "-" + location.getBlockZ() + ".yml");

            if (locationFile.exists()) {

                Bukkit.getConsoleSender().sendMessage(Main.colorCodes(Main.prefix + "&fThe file exists..."));
                spawner.setSpawnedType(EntityType.SKELETON);
                spawner.update();

            }
        }
    }

BlockPlaceEvent »

package me.askingg.events;

import java.io.File;
import java.io.IOException;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;

import me.askingg.golems.Main;

    public class BlockPlace implements Listener {

        @EventHandler
        public void spawnerPlace(BlockPlaceEvent event) {

            Player player = (Player) event.getPlayer();
            Block block = event.getBlock();
            Location location = block.getLocation();
            String world = block.getWorld().getName().toString();

            if (block.getType().equals(Material.SPAWNER)) {
                if (player.getInventory().getItemInMainHand().getItemMeta().getDisplayName()
                        .equals(Main.colorCodes("&fSkeleton Spawner"))) {

                    File locationFile = new File("plugins/Golems/Locations", world + " - " + location.getBlockX() + "-"
                            + location.getBlockY() + "-" + location.getBlockZ() + ".yml");
                    if (!(locationFile.exists())) {
                        try {

                            locationFile.createNewFile();
                            Bukkit.getConsoleSender()
                                    .sendMessage(Main.colorCodes(Main.prefix
                                            + "&aSuccessfully&f created a new &fSkeleton Spawner&f location &8(&a"
                                            + world + " &8-&a " + location.getBlockX() + "&8-&a" + location.getBlockY() + "&8-&a"
                                            + location.getBlockZ() + "&8)"));

                        } catch (IOException e) {
                        }
                    }
                }
            }
        }
    }
Askingg
  • 107
  • 1
  • 2
  • 11
  • So... exactly which part of this code dump demonstrates your problem? You need to post the shortest sample of code that clearly identifies the issue that's giving you trouble. The first step to fixing a problem is knowing what it is, and if you don't know, why would we? Posting any old code or code that *might* contain a problem isn't good enough. Now is a good time to read the Stack Overflow [help file](http://stackoverflow.com/help) and learn what questions are considered on topic here, and how to ask good questions that will receive good answers. – MarsAtomic Aug 10 '18 at 19:14
  • The issue lies in the SpawnerSpawnEvent: spawner.setSpawnedType(EntityType.SKELETON); It does not change the entitytype of the spawned mob – Askingg Aug 10 '18 at 19:19
  • You've identified a line of code, but that's not the same as knowing what the problem is. What happens when you run the code? Does your code find the file it needs to find? Are there errors? Is something spawning other than the skeleton? These are all questions you're meant to answer before you post a question, because if you knew those answers, you'd at least have a better idea of where the problem is. – MarsAtomic Aug 10 '18 at 19:41
  • There are no console errors what so ever, pigs are spawning (Because pigs are the default mob for spawners for spawn..) The console does get the 'The file exists...' message Basically, Everything works perfectly fine except for the actual spawning of the skeleton... – Askingg Aug 10 '18 at 19:45

1 Answers1

0

Alright, so. Based on my quick read of the documentation it looks like you can cast "block" in this instance to CreatureSpawner and then set the spawnType.

Example:

if (block.getType().equals(Material.SPAWNER)) {
  CreatureSpawner spawner = (CreatureSpawner) block;
  spawner.setSpawnType(EntityType.SKELETON);
}

Be aware some of that maybe pseudo code as I didn't delve that much into the Bukkit API documentation but you should be able to figure it out from there.

Spencer
  • 21
  • 1
  • Okay, so I tried this thinking from the start that it wouldn't work, and unfortunately I was correct. Thank you for attempting though. <3 This is very similar to what I was trying to do in the first place, it is just checking if the spawner is a spawner (It's a SpawnerSpawnEvent aka, the event of a mob spawner spawning an entity, so checking if it is a spawner is pointless) I do understand you're not very familiar with the BukkitAPI, again thank you for atleast trying. – Askingg Aug 10 '18 at 20:03
  • It's not that I'm not familiar, I misinterpreted your question. I used to write plugins all the time back in 2015. This is going to be a very hacky and gross solution but might solve your problem. Cancel the spawn event on certain blocks with a certain metadata or name that you set, then just spawn a skeleton in the location of that entity. – Spencer Aug 10 '18 at 20:30
  • I found a fix, don't worry. Thank you for helping. It was just because I am stupid... I was trying to spawn a skeleton in the day time.. – Askingg Aug 10 '18 at 20:38
  • Ha, that's great. – Spencer Aug 10 '18 at 20:42