Jump to content

Random mob spawn onItemUse


AzureusNation123
 Share

Recommended Posts

Is there a way to make a random mob spawn with onItemUse?

 

This is what i have so far, but not sure how to finish it.

 

Item Class

package com.azureusnation.mod.cards.unknown;

import java.util.List;

import net.minecraft.block.Block;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.entity.monster.EntitySkeleton;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;

import com.azureusnation.mod.Main;
import com.azureusnation.mod.ModInfo;
import com.azureusnation.mod.cards.main.CardDescriptions;
import com.azureusnation.mod.cards.main.Name;
import com.azureusnation.mod.cards.main.logic.RandomMob;
import com.azureusnation.mod.cards.main.misc.Icon;
import com.azureusnation.mod.cards.main.misc.Rarity;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class UnknownMob extends Item {

public UnknownMob(int id) {
	super(id);
	this.setUnlocalizedName(Name.unknownMob_unlocalizedName);
	this.setMaxStackSize(16);
	this.setCreativeTab(Main.unknownTab);
}

public boolean onItemUse(ItemStack par1ItemStack, EntityPlayer par3EntityPlayer, World par2World, int x, int y, int z, int side, float xOffset, float yOffset, float zOffSet)
{
	if (!par2World.isRemote)
	{
		if (!par3EntityPlayer.capabilities.isCreativeMode)
		{
			--par1ItemStack.stackSize;
		}

		if (par2World.getBlockId(x, y, z) == 0 && (par2World.getBlockId(x, y, z) == Block.grass.blockID || par2World.getBlockId(x, y, z) == Block.stone.blockID || par2World.getBlockId(x, y, z) == Block.dirt.blockID || par2World.getBlockId(x, y, z) == Block.sand.blockID))
		{
			EntitySkeleton ent = new EntitySkeleton(par2World);
			ent.setLocationAndAngles(x, y + 1, z, MathHelper.wrapAngleTo180_float(par2World.rand.nextFloat() * 360.0F), 0.0F);
			par2World.spawnEntityInWorld(ent);
		}

		par2World.playSoundAtEntity(par3EntityPlayer, ModInfo.ID.toLowerCase()+":card", 1.0F, 1.0F);

	}
	return true;
}

@SideOnly(Side.CLIENT)
public boolean hasEffect(ItemStack par1ItemStack)
{
	return true;
}

public void addInformation(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, List par3List, boolean par4)
{
	par3List.add(Rarity.Unknown);
	par3List.add(CardDescriptions.UnknownHoe);
}

@Override
@SideOnly(Side.CLIENT)
public void registerIcons(IconRegister icon)
{    	
	itemIcon = icon.registerIcon(Icon.Unknown);
}
}

 

Random Selector Class

package com.azureusnation.mod.cards.main.logic;

import java.util.Random;

import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.monster.EntityBlaze;
import net.minecraft.entity.monster.EntityCaveSpider;
import net.minecraft.entity.monster.EntityCreeper;
import net.minecraft.entity.monster.EntityEnderman;
import net.minecraft.entity.monster.EntityGhast;
import net.minecraft.entity.monster.EntityGiantZombie;
import net.minecraft.entity.monster.EntityGolem;
import net.minecraft.entity.monster.EntityIronGolem;
import net.minecraft.entity.monster.EntityMagmaCube;
import net.minecraft.entity.monster.EntityPigZombie;
import net.minecraft.entity.monster.EntitySilverfish;
import net.minecraft.entity.monster.EntitySkeleton;
import net.minecraft.entity.monster.EntitySlime;
import net.minecraft.entity.monster.EntitySnowman;
import net.minecraft.entity.monster.EntitySpider;
import net.minecraft.entity.monster.EntityWitch;

public class RandomMob
{
private static Random random = new Random();

private static final EntityLiving EntityBlaze = new EntityBlaze(null);
private static final EntityLiving EntityCaveSpider = new EntityCaveSpider(null);
private static final EntityLiving EntityCreeper = new EntityCreeper(null);
private static final EntityLiving EntityEnderman = new EntityEnderman(null);
private static final EntityLiving EntityGhast = new EntityGhast(null);
private static final EntityLiving EntityGiantZombie = new EntityGiantZombie(null);
private static final EntityLiving EntityIronGolem = new EntityIronGolem(null);
private static final EntityLiving EntityMagmaCube = new EntityMagmaCube(null);
private static final EntityLiving EntityPigZombie = new EntityPigZombie(null);
private static final EntityLiving EntitySilverfish = new EntitySilverfish(null);
private static final EntityLiving EntitySkeleton = new EntitySkeleton(null);
private static final EntityLiving EntitySlime = new EntitySlime(null);
private static final EntityLiving EntitySnowman = new EntitySnowman(null);
private static final EntityLiving EntitySpider = new EntitySpider(null);
private static final EntityLiving EntityWitch = new EntityWitch(null);

private static EntityLiving Mob [] =
	{
	EntityBlaze,
	EntityCaveSpider,
	EntityCreeper,
	EntityEnderman,
	EntityGhast,
	EntityGiantZombie,
	EntityIronGolem,
	EntityMagmaCube,
	EntityPigZombie,
	EntitySilverfish,
	EntitySkeleton,
	EntitySlime,
	EntitySnowman,
	EntitySpider,
	EntityWitch,
	};

public static void Mob()
{

}
}

Link to comment
Share on other sites

Without actually reading threw your code it should be as simple as making a list, shuffling the list, and then pulling out a random item from the list. You'll want to store either new Entity instances or classes in the list. Then do the rest after getting a valid Entity from the list.

Link to comment
Share on other sites

Replying with the code I gave you on IRC so people seeing this topic in the future can see

 

/**
* Item Author: Katrina S
* nekosune
**/


package com.nekokittygames.modjam.UnDeath;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import net.minecraft.block.Block;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityEggInfo;
import net.minecraft.entity.EntityList;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.EntityLivingData;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Facing;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;

public class TestItem extends Item {

        public TestItem(int par1) {
                super(par1);
                setMaxStackSize(64);
                setCreativeTab(CreativeTabs.tabMisc);
                setUnlocalizedName("testSpawner");
                func_111206_d("undeath:DebugItem");
        }
        public boolean onItemUse(ItemStack par1ItemStack, EntityPlayer par3EntityPlayer, World par2World, int x, int y, int z, int side, float xOffset, float yOffset, float zOffSet)
        {
                if (!par2World.isRemote)
                {
                        int i1 = par2World.getBlockId(x, y, z);
            x += Facing.offsetsXForSide[side];
            y += Facing.offsetsYForSide[side];
            z += Facing.offsetsZForSide[side];
            double d0 = 0.0D;

            if (side == 1 && Block.blocksList[i1] != null && Block.blocksList[i1].getRenderType() == 11)
            {
                d0 = 0.5D;
            }
                        Iterator iterator = EntityList.entityEggs.values().iterator();
                        List<Integer> ids=new ArrayList<Integer>();
                        while (iterator.hasNext())
                        {
                            EntityEggInfo entityegginfo = (EntityEggInfo)iterator.next();
                            ids.add(entityegginfo.spawnedID);
                        }
                        // shuffle and pick from ids
                        Collections.shuffle(ids);
                        int id=ids.get(0);
                        Entity entity = null;
                        entity = EntityList.createEntityByID(id,par2World);
                         
                        if (entity != null && entity instanceof EntityLivingBase)
                        {
                                EntityLiving entityliving = (EntityLiving)entity;
                        entity.setLocationAndAngles(x+0.5d, y+0.5d, z+0.5d, MathHelper.wrapAngleTo180_float(par2World.rand.nextFloat() * 360.0F), 0.0F);
                                            entityliving.rotationYawHead = entityliving.rotationYaw;
                                            entityliving.renderYawOffset = entityliving.rotationYaw;
                                            entityliving.func_110161_a((EntityLivingData)null);
                                           
                                            par2World.spawnEntityInWorld(entity);
                                            entityliving.playLivingSound();
                        }
                       
                }
                return true;
        }

}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I am using a modified MeleeAttackGoal to make an entity pick a random button, go to it and press it. But after a while it just stops and stares at the current button target. I found out that it's just staring at it because it's too far away to press it.  And its's not moving because the current path in the mob navigation is "done". If I move it further away it recalculates a non weird path and fixes itself. If I push it towards the button so it can press it, it sets the target to null, so it once again calculates a new path and fixes itself. What is causing the path to be "done"? And how can I fix this? This is the tick method in the custom goal class: public void tick() { if (mob.buttonTarget != null) { float x = mob.buttonTarget.getX(); float y = mob.buttonTarget.getY(); float z = mob.buttonTarget.getZ(); float hx = x + 0.5f; float hy = y + 0.5f; float hz = z + 0.5f; mob.getLookControl().setLookAt(hx, hy, hz, 30.0F, 30.0F); double distSqr = mob.distanceToSqr(hx, hy, hz); ticksUntilNextPathRecalculation = Math.max(ticksUntilNextPathRecalculation - 1, 0); if ((followingTargetEvenIfNotSeen || hasLineOfSight(mob, mob.buttonTarget)) && ticksUntilNextPathRecalculation <= 0 && ((pathedTargetX == 0.0D && pathedTargetY == 0.0D && pathedTargetZ == 0.0D) || mob.buttonTarget.distSqr(new Vec3i(pathedTargetX, pathedTargetY, pathedTargetZ)) >= 1.0D || mob.getRandom().nextFloat() < 0.05F)) { pathedTargetX = hx; pathedTargetY = hy; pathedTargetZ = hz; ticksUntilNextPathRecalculation = 4 + mob.getRandom().nextInt(7); if (distSqr > 1024.0D) ticksUntilNextPathRecalculation += 10; else if (distSqr > 256.0D) ticksUntilNextPathRecalculation += 5; Path newPath = mob.getNavigation().createPath(mob.buttonTarget, 0); if (newPath != null && !newPath.canReach()) { mob.buttonTarget = null; return; } if (!mob.getNavigation().moveTo(newPath, speedModifier)) ticksUntilNextPathRecalculation += 15; ticksUntilNextPathRecalculation = adjustedTickDelay(ticksUntilNextPathRecalculation); } ticksUntilNextAttack = Math.max(getTicksUntilNextAttack() - 1, 0); checkAndPerformAttack(mob.buttonTarget, distSqr); } }  
    • As of Forge version 44.1.5 and above, Minecraft has started crashing whenever I played it with the Create mod. Without any mods, it loads normally, but when I put in a mod, like MrCrayfish's furniture mod for example, it fails to read the mod properly. Basically I press play and it loads. When the launcher closes, it opens back up again with an Exit Code 1 error, stating that the game crashed due to an unexpected error. Here's my latest.log for more information. https://pastebin.com/pUPBjpbr  
    • The files are harmed by system flaws and glitches. Additionally, the installation procedure might cause files to be lost. The game is so terminated, and an exit code 1 error is displayed. Therefore, attempt a game reinstall.
    • Hello there!  I'm hosting a Minecraft Tekxit PI server on Minecraft 1.12.2 on my computer and I have a little issue. Everything was going great until one day my light in my house went down while a friend was inside the server. The server crashed and when I came back 2 chunks swapped but everything seemed normal. Fastforward in time my friend who was inside the server tried to come back in it but everti-me he tries to join he cannot do it and there is a crash in the server logs. If any other friend tries to join they can, but, that friend that was inside the server when that occurred cannot join. I tried rebooting, rebooting my modem, deleting his player data to start over and nothing works. I'm leaving the crash logs here. Thank you!!! ----------------------------------------------- CRASH LOGS   [10:27:10] [Netty Server IO #1/INFO]: Transforming class 'gu', alias 'net.minecraft.network.NettyCompressionDecoder' [10:27:10] [Netty Server IO #1/INFO]: Scanning method 'decode(Lio/netty/channel/ChannelHandlerContext;Lio/netty/buffer/ByteBuf;Ljava/util/List;)V' [10:27:10] [Netty Server IO #1/INFO]: Patching constant (java.lang.Integer) 2097152 [10:27:10] [Netty Server IO #1/INFO]: Patching constant (java.lang.Integer) 2097152 [10:27:10] [Netty Server IO #1/INFO]: [com.creativemd.creativecore.transformer.CreativeTransformer:transform:49]: [littletiles] Patched net.minecraft.network.NettyCompressionDecoder ... [10:27:13] [Server thread/ERROR]: HandshakeMessageHandler exception java.lang.OutOfMemoryError: Java heap space     at java.util.ArrayList.<init>(Unknown Source) ~[?:1.8.0_361]     at com.google.common.collect.Lists.newArrayListWithCapacity(Lists.java:190) ~[minecraft_server.1.12.2.jar:?]     at net.minecraft.nbt.NBTTagList.read(NBTTagList.java:58) ~[ge.class:?]     at net.minecraft.nbt.NBTTagCompound.readNBT(NBTTagCompound.java:497) ~[fy.class:?]     at net.minecraft.nbt.NBTTagCompound.read(NBTTagCompound.java:58) ~[fy.class:?]     at net.minecraft.nbt.NBTTagCompound.readNBT(NBTTagCompound.java:497) ~[fy.class:?]     at net.minecraft.nbt.NBTTagCompound.read(NBTTagCompound.java:58) ~[fy.class:?]     at net.minecraft.nbt.CompressedStreamTools.read(CompressedStreamTools.java:133) ~[gi.class:?]     at net.minecraft.nbt.CompressedStreamTools.read(CompressedStreamTools.java:88) ~[gi.class:?]     at net.minecraft.nbt.CompressedStreamTools.readCompressed(CompressedStreamTools.java:31) ~[gi.class:?]     at net.minecraft.world.storage.SaveHandler.getPlayerNBT(SaveHandler.java:278) ~[bfb.class:?]     at net.minecraft.server.management.PlayerList.getPlayerNBT(PlayerList.java:340) ~[pl.class:?]     at net.minecraftforge.fml.common.network.handshake.NetworkDispatcher.serverInitiateHandshake(NetworkDispatcher.java:220) ~[NetworkDispatcher.class:?]     at net.minecraftforge.fml.common.network.handshake.FMLHandshakeServerState$1.accept(FMLHandshakeServerState.java:49) ~[FMLHandshakeServerState$1.class:?]     at net.minecraftforge.fml.common.network.handshake.HandshakeMessageHandler.userEventTriggered(HandshakeMessageHandler.java:65) ~[HandshakeMessageHandler.class:?]     at io.netty.channel.AbstractChannelHandlerContext.invokeUserEventTriggered(AbstractChannelHandlerContext.java:329) ~[minecraft_server.1.12.2.jar:?]     at io.netty.channel.AbstractChannelHandlerContext.invokeUserEventTriggered(AbstractChannelHandlerContext.java:315) ~[minecraft_server.1.12.2.jar:?]     at io.netty.channel.AbstractChannelHandlerContext.fireUserEventTriggered(AbstractChannelHandlerContext.java:307) ~[minecraft_server.1.12.2.jar:?]     at io.netty.channel.ChannelInboundHandlerAdapter.userEventTriggered(ChannelInboundHandlerAdapter.java:108) ~[minecraft_server.1.12.2.jar:?]     at io.netty.channel.AbstractChannelHandlerContext.invokeUserEventTriggered(AbstractChannelHandlerContext.java:329) ~[minecraft_server.1.12.2.jar:?]     at io.netty.channel.AbstractChannelHandlerContext.invokeUserEventTriggered(AbstractChannelHandlerContext.java:315) ~[minecraft_server.1.12.2.jar:?]     at io.netty.channel.AbstractChannelHandlerContext.fireUserEventTriggered(AbstractChannelHandlerContext.java:307) ~[minecraft_server.1.12.2.jar:?]     at io.netty.channel.ChannelInboundHandlerAdapter.userEventTriggered(ChannelInboundHandlerAdapter.java:108) ~[minecraft_server.1.12.2.jar:?]     at io.netty.channel.AbstractChannelHandlerContext.invokeUserEventTriggered(AbstractChannelHandlerContext.java:329) ~[minecraft_server.1.12.2.jar:?]     at io.netty.channel.AbstractChannelHandlerContext.invokeUserEventTriggered(AbstractChannelHandlerContext.java:315) ~[minecraft_server.1.12.2.jar:?]     at io.netty.channel.AbstractChannelHandlerContext.fireUserEventTriggered(AbstractChannelHandlerContext.java:307) ~[minecraft_server.1.12.2.jar:?]     at io.netty.channel.DefaultChannelPipeline$HeadContext.userEventTriggered(DefaultChannelPipeline.java:1352) ~[minecraft_server.1.12.2.jar:?]     at io.netty.channel.AbstractChannelHandlerContext.invokeUserEventTriggered(AbstractChannelHandlerContext.java:329) ~[minecraft_server.1.12.2.jar:?]     at io.netty.channel.AbstractChannelHandlerContext.invokeUserEventTriggered(AbstractChannelHandlerContext.java:315) ~[minecraft_server.1.12.2.jar:?]     at io.netty.channel.DefaultChannelPipeline.fireUserEventTriggered(DefaultChannelPipeline.java:920) ~[minecraft_server.1.12.2.jar:?]     at net.minecraftforge.fml.common.network.handshake.NetworkDispatcher.insertIntoChannel(NetworkDispatcher.java:183) ~[NetworkDispatcher.class:?]     at net.minecraftforge.fml.common.network.handshake.NetworkDispatcher.serverToClientHandshake(NetworkDispatcher.java:154) ~[NetworkDispatcher.class:?]
    • (Mod name is Custom Npcs)
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.