PDA

View Full Version : [java] help



w_is_awesome
04-06-2012, 11:29 PM
Well I'm making a minecraft mod and I don't know how to make it spawn only ###Seconds/Ticks. Can someone please help me. I'm using eclipse

Zachafer
04-06-2012, 11:40 PM
Why don't you start by posting what code you have and what you have tried.

w_is_awesome
04-06-2012, 11:42 PM
It is a run off of the zombie... one of my first mods... here it is.


package net.minecraft.src;

import java.util.

public class EntityGiantZombie extends EntityMob
{
public EntityGiantZombie(World par1World)
{
super(par1World);
texture = "/mob/zombie.png";
moveSpeed = 0.5F;
attackStrength = 50;
isImmuneToFire = true;
tasks.addTask(1, new EntityAIAttackOnCollide(this, net.minecraft.src.EntityPlayer.class, moveSpeed, false));
tasks.addTask(2, new EntityAIMoveTwardsRestriction(this, moveSpeed));
tasks.addTask(3, new EntityAIWander(this, moveSpeed));
tasks.addTask(4, new EntityAIWatchClosest(this, net.minecraft.src.EntityPlayer.class, 8F));
tasks.addTask(4, new EntityAILookIdle(this));
targetTasks.addTask(1, new EntityAIHurtByTarget(this, false));
targetTasks.addTask(2, new EntityAINearestAttackableTarget(this, net.minecraft.src.EntityPlayer.class, 16F, 0, true));
}

public int getMaxHealth()
{
return 100;
}

protected boolean isAIEnabled()
{
return true;
}


public void onLivingUpdate()
{
if (worldObj.isDaytime() && !worldObj.isRemote)
{
float f = getBrightness(1.0F);

if (f > 0.5F && worldObj.canBlockSeeTheSky(MathHelper.floor_double (posX), MathHelper.floor_double(posY), MathHelper.floor_double(posZ)) && rand.nextFloat() * 30F < (f - 0.4F) * 2.0F)
{
setFire(8);
}
}

super.onLivingUpdate();
}

/**
* Returns the sound this mob makes while it's alive.
*/
protected String getLivingSound()
{
return "mob.zombie";
}

/**
* Returns the sound this mob makes when it is hurt.
*/
protected String getHurtSound()
{
return "mob.zombiehurt";
}

/**
* Returns the sound this mob makes on death.
*/
protected String getDeathSound()
{
return "mob.zombiedeath";
}

/**
* Takes a coordinate in and returns a weight to determine how likely this creature will try to path to the block.
* Args: x, y, z
*/
public float getBlockPathWeight(int par1, int par2, int par3)
{
return worldObj.getLightBrightness(par1, par2, par3) - 0.5F;
}

protected void dropRareDrop(int par1)
{
switch (rand.nextInt(4))
{
case 0:
dropItem(Item.diamond.shiftedIndex, 1);
break;
}
}
}

Zachafer
04-06-2012, 11:49 PM
Can you elaborate on "make it spawn only ###Seconds/Ticks" ?

I'm unfamiliar with Minecraft B)

w_is_awesome
04-06-2012, 11:51 PM
well so the map doesnt overload i only want the entity to spawn/load every (lets say) 60 seconds
ticks is another measurement in minecraft.

Zachafer
04-06-2012, 11:54 PM
well so the map doesnt overload i only want the entity to spawn/load every (lets say) 60 seconds
ticks is another measurement in minecraft.

60 seconds is different than ticks. I would set a variable that holds the last System.currentTimeMillis() and is set when you spawn/load the entity. Then in the code for spawning the entity, ensure that System.currentTimeMillis() - lastRespawn >= 60000 before spawning.

w_is_awesome
04-07-2012, 12:01 AM
ik ticks is like .4 seconds. but would the code look like this.
[CODE]
{
System.currentTimeMillis
{
lastRespawn = 60000
}
}
[CODE]
tab button isn't working srry

But i think it goes after
[Code]
Import java.until.[Something goes here]
[CODE]

Zachafer
04-07-2012, 12:05 AM
Try something like this:
package net.minecraft.src;

import java.util.*;

public class EntityGiantZombie extends EntityMob
{

public static long lastSpawn = -1;

public EntityGiantZombie(World par1World)
{
if(lastSpawn != -1 && System.currentTimeMillis() - EntityGiantZombie.lastSpawn < 60000)
{
return;
}
super(par1World);
texture = "/mob/zombie.png";
moveSpeed = 0.5F;
attackStrength = 50;
isImmuneToFire = true;
tasks.addTask(1, new EntityAIAttackOnCollide(this, net.minecraft.src.EntityPlayer.class, moveSpeed, false));
tasks.addTask(2, new EntityAIMoveTwardsRestriction(this, moveSpeed));
tasks.addTask(3, new EntityAIWander(this, moveSpeed));
tasks.addTask(4, new EntityAIWatchClosest(this, net.minecraft.src.EntityPlayer.class, 8F));
tasks.addTask(4, new EntityAILookIdle(this));
targetTasks.addTask(1, new EntityAIHurtByTarget(this, false));
targetTasks.addTask(2, new EntityAINearestAttackableTarget(this, net.minecraft.src.EntityPlayer.class, 16F, 0, true));
}

public int getMaxHealth()
{
return 100;
}

protected boolean isAIEnabled()
{
return true;
}


public void onLivingUpdate()
{
if (worldObj.isDaytime() && !worldObj.isRemote)
{
float f = getBrightness(1.0F);

if (f > 0.5F && worldObj.canBlockSeeTheSky(MathHelper.floor_double (posX), MathHelper.floor_double(posY), MathHelper.floor_double(posZ)) &&
rand.nextFloat() * 30F < (f - 0.4F) * 2.0F)
{
setFire(8);
}
}

super.onLivingUpdate();
}

/**
* Returns the sound this mob makes while it's alive.
*/
protected String getLivingSound()
{
return "mob.zombie";
}

/**
* Returns the sound this mob makes when it is hurt.
*/
protected String getHurtSound()
{
return "mob.zombiehurt";
}

/**
* Returns the sound this mob makes on death.
*/
protected String getDeathSound()
{
return "mob.zombiedeath";
}

/**
* Takes a coordinate in and returns a weight to determine how likely this creature will try to path to the block.
* Args: x, y, z
*/
public float getBlockPathWeight(int par1, int par2, int par3)
{
return worldObj.getLightBrightness(par1, par2, par3) - 0.5F;
}

protected void dropRareDrop(int par1)
{
switch (rand.nextInt(4))
{
case 0:
dropItem(Item.diamond.shiftedIndex, 1);
break;
}
}

}

w_is_awesome
04-07-2012, 12:26 AM
thank you but i have one issue.

{
if(lastSpawn != -1 && System.currentTimeMillis() - EntityGiantZombie.lastSpawn < 690000)
{
return;
}
}


The thing in red gives me this error: "Cannot return within and intializer"

Zachafer
04-07-2012, 12:29 AM
My bad, try this:

public static long lastSpawn = -1;

public EntityGiantZombie(World par1World)
{
if(EntityGiantZombie.lastSpawn == -1 || System.currentTimeMillis() - EntityGiantZombie.lastSpawn >= 60000)
{
super(par1World);
texture = "/mob/zombie.png";
moveSpeed = 0.5F;
attackStrength = 50;
isImmuneToFire = true;
tasks.addTask(1, new EntityAIAttackOnCollide(this, net.minecraft.src.EntityPlayer.class, moveSpeed, false));
tasks.addTask(2, new EntityAIMoveTwardsRestriction(this, moveSpeed));
tasks.addTask(3, new EntityAIWander(this, moveSpeed));
tasks.addTask(4, new EntityAIWatchClosest(this, net.minecraft.src.EntityPlayer.class, 8F));
tasks.addTask(4, new EntityAILookIdle(this));
targetTasks.addTask(1, new EntityAIHurtByTarget(this, false));
targetTasks.addTask(2, new EntityAINearestAttackableTarget(this, net.minecraft.src.EntityPlayer.class, 16F, 0, true));
EntityGiantZombie.lastSpawn = System.currentTimeMillis();
}
}

w_is_awesome
04-07-2012, 12:32 AM
thank you!

Neoquest
04-08-2012, 12:21 AM
Ticks are 1/20th of a second. It seems like you don't really know anything about Java :/

Ryan~
04-08-2012, 12:48 AM
Ticks are 1/20th of a second. It seems like you don't really know anything about Java :/

Ticks aren't really important, honestly. Zach knows a pretty good bit about Java, do you?

Zachafer bro, he's trying your gangster.

Miguel
04-08-2012, 01:18 AM
Ticks are 1/20th of a second. It seems like you don't really know anything about Java :/

It seems like you don't know that a tick is defined by the program, not a language or any standardized time at all.

[Only registered and activated users can see links]_and_rounds

For example, Runescape ticks are 3/5s of a second

Neoquest
04-08-2012, 01:50 AM
I actually did know that ticks are program specified, in the case of Minecraft they are 1/20th of a second, while w_is_awesome said they were 0.4. Also I was speaking to w_is_awesome, I'm sure Zachafer knows plenty of Java.

Zachafer
04-08-2012, 02:49 AM
And for this reason, I used code that waited 60 seconds, not ticks.