Interesting, so that's why a zombie pigman managed to come into my overworld two days ago
Anyway, I'm almost done. The mod is running and System.out.println("Spawn Denied"); is printing, so theoretically it should work.
However, there are still monsters that spawn next to these blocks, and I tried to debug it for several hours without success...
EventHandler
package com.monsterBlocker;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.world.World;
import net.minecraftforge.event.Event.Result;
import net.minecraftforge.event.ForgeSubscribe;
import net.minecraftforge.event.entity.living.LivingSpawnEvent;
import net.minecraftforge.event.world.WorldEvent;
public class MBEventHandler {
//public List<Position> mbBlockList = new ArrayList();
public static final String DATA_NAME = MonsterBlocker.MODID + "_BlockList";
private static final int RANGE_SIDE = 50;
private static final int RANGE_UP = 50;
private static final int RANGE_DOWN = 10;
public static MBSave get(World world) {
MBSave handler = (MBSave) world.loadItemData(MBSave.class, DATA_NAME);
if(handler==null) {
handler = new MBSave();
world.setItemData(DATA_NAME, handler);
}
return handler;
}
@ForgeSubscribe //1.6.4
public void LSECheckSpawn(LivingSpawnEvent.CheckSpawn event)
{
List<Position> mbBlockList = new ArrayList();
MBSave mbdata = get(event.world);
mbBlockList = mbdata.getList();
for (int i=0; i < mbBlockList.size(); ++i)
{
Position posBlock = mbBlockList.get(i);
//Position posMonster = new Position((int) event.x, (int) event.y, (int) event.z);
if ( (posBlock.x - RANGE_SIDE < event.x) && (event.x < posBlock.x + RANGE_SIDE) &&
(posBlock.y - RANGE_SIDE < event.y) && (event.y < posBlock.y + RANGE_SIDE) &&
(posBlock.z - RANGE_DOWN < event.z) && (event.z < posBlock.z + RANGE_UP) )
{
event.setResult(Result.DENY);
System.out.println("Spawn Denied");
return;
}
}
}
}
SavedData
package com.monsterBlocker;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.World;
import net.minecraft.world.WorldSavedData;
public class MBSave extends WorldSavedData {
private static final String DATA_NAME = MonsterBlocker.MODID + "_BlockList";
public List<Position> mbBlockList = new ArrayList();
public MBSave() {
super(DATA_NAME);
}
public MBSave(String s) {
super(s);
}
public List<Position> getList() {
return mbBlockList;
}
/*public static List<Position> get(World world) {
List<Position> handler = (List<Position>) world.loadItemData(List.class, "MonsterBlocker_BlockList");
if(handler==null) {
handler = new ArrayList();
world.setItemData("MonsterBlocker_BlockList", handler);
}
return handler;
}*/
public void onMBBlockChange(List<Position> newMBBlockList) {
mbBlockList = newMBBlockList;
markDirty();
}
public void addBlock(Position pos)
{
mbBlockList.add(pos);
}
@Override
public void readFromNBT(NBTTagCompound nbt) {
mbBlockList.clear();
NBTTagList nbttaglist = nbt.getTagList("mbBlockList");
for (int i = 0; i < nbttaglist.tagCount(); ++i) {
NBTTagCompound posCompound = (NBTTagCompound) nbttaglist.tagAt(i);
Position pos = new Position(posCompound.getInteger("x"), posCompound.getInteger("y"), posCompound.getInteger("z"));
mbBlockList.add(pos);
}
}
@Override
public void writeToNBT(NBTTagCompound nbt) {
NBTTagList nbttaglist = new NBTTagList();
nbt.setTag("mbBlockList", nbttaglist);
for(int i = 0; i < mbBlockList.size(); i++) {
NBTTagCompound compound = new NBTTagCompound();
Position pos = mbBlockList.get(i);
compound.setInteger("x", pos.x);
compound.setInteger("y", pos.y);
compound.setInteger("z", pos.z);
nbttaglist.appendTag(compound);
}
}
}
Block
package com.monsterBlocker;
import java.util.ArrayList;
import java.util.List;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
public class MBBlock extends Block
{
public MBBlock (int id)
{
super(id, Material.iron);
this.setUnlocalizedName(MonsterBlocker.mbBlock_unlocalizedName);
this.setCreativeTab(CreativeTabs.tabBlock);
this.setHardness(5F);
this.setResistance(15F);
this.setStepSound(Block.soundMetalFootstep);
this.setLightValue(2F);
}
@Override
@SideOnly(Side.CLIENT)
public void registerIcons(IconRegister icon) {
blockIcon = icon.registerIcon(MonsterBlocker.AID + ":" + "monsterBlocker");
}
@Override
public void onBlockAdded(World world, int x, int y, int z)
{
Position pos = new Position(x, y, z);
List<Position> mbBlockList = new ArrayList();
MBSave mbdata = MBEventHandler.get(world);
mbBlockList = mbdata.getList();
mbBlockList.add(pos);
mbdata.onMBBlockChange(mbBlockList);
//System.out.println("I Added");
}
@Override
public void breakBlock(World world, int x, int y, int z, int oldBlockID, int oldMetadata)
{
Position pos = new Position(x, y, z);
List<Position> mbBlockList = new ArrayList();
MBSave mbdata = MBEventHandler.get(world);
mbBlockList = mbdata.getList();
mbBlockList.remove(pos);
mbdata.onMBBlockChange(mbBlockList);
}
}
And main file
package com.monsterBlocker;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.Mod.Instance;
import cpw.mods.fml.common.SidedProxy;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.network.NetworkMod;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.common.registry.LanguageRegistry;
@Mod(modid = MonsterBlocker.MODID, name = MonsterBlocker.NAME, version = MonsterBlocker.VERSION)
@NetworkMod( channels = {"MB"}, clientSideRequired = true, serverSideRequired = true )
public class MonsterBlocker
{
public static final String MODID = "MonsterBlocker";
public final static String AID = MODID.toLowerCase();
public final static String NAME = "Monster Blocker";
public static final String VERSION = "0.1";
public static final String DATA_NAME = MonsterBlocker.MODID + "_BlockList";
public static final String mbBlock_unlocalizedName = "monsterBlocker";
public static final String mbBlock_name = "Monster Blocker";
@SidedProxy( clientSide = "com.monsterBlocker.ClientProxy", serverSide = "com.monsterBlocker.CommonProxy" )
public static CommonProxy proxy;
public static Block monsterBlocker;
// The instance of your mod that Forge uses.
@Instance(value = MonsterBlocker.MODID)
public static MonsterBlocker instance;
@EventHandler
public static void preInit(FMLPreInitializationEvent event) {
proxy.initRenderers();
proxy.initSounds();
monsterBlocker = new MBBlock(3009);
GameRegistry.registerBlock(monsterBlocker, MonsterBlocker.mbBlock_name);
LanguageRegistry.addName(monsterBlocker, MonsterBlocker.mbBlock_name);
GameRegistry.addRecipe(new ItemStack(MonsterBlocker.monsterBlocker, 1),
new Object[] {
"BIB",
"IGI",
"BIB",
'G', Block.glowStone, 'I', Item.ingotGold, 'B', Item.blazePowder
});
}
@EventHandler
public static void init ( FMLInitializationEvent event ) {
MinecraftForge.EVENT_BUS.register(new MBEventHandler());
}
}
So if you could please help me one last time and get this mod running how it is supposed to, I'll be really grateful!