Sure. sorry the question got pushed bellow the stack
Just noticed it had a reply.
I'll post the entire class. I think the problem is related to onMinecartPass, because I don't know how to make it stop from being invoked by the minecart when it's stopped (off course I'd have to change some things in the implementation of my block and maybe in my implementation of ExtendedProperties if disabling onMinecartPass was possible).
Or maybe something related to telling the minecart stop updating entirely (don't know how to do that either. [digression]Entity is way too complex. The way minecraft codes Entity and all its inheritance hierarchy, is just awful in my opinion. Whenever I see code that needs lots of
aInstance instanceof aClass
instead of just using polymorphism, I instantly assume the author of the code isn't so smart. Minecraft force us to code this way because we're stuck with its own code).[/digression]
Enough rambling, here's my code (Stuff is the name of my mod, because I'm that creative):
package stuff.blocks;
import java.util.List;
import java.util.Random;
import pedrinholib.minecraft.DirectionXProp;
import net.minecraft.block.BlockRailBase;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.command.IEntitySelector;
import net.minecraft.entity.item.EntityMinecart;
import net.minecraft.entity.item.EntityMinecartContainer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.Icon;
import net.minecraft.world.World;
import stuff.Stuff;
public class MetaDetectorRailBlock extends BlockRailBase
{
Icon theIcon;
String assetsCommonName = Stuff.ModPrefix() + MetaDetectorRailBlock.class.getSimpleName();
public MetaDetectorRailBlock(int id)
{
super(id, true);//true here says the rail is power related
setUnlocalizedName(assetsCommonName);
}
@Override
public void registerIcons(IconRegister par1IconRegister)
{
blockIcon = par1IconRegister.registerIcon(assetsCommonName);
this.theIcon = par1IconRegister.registerIcon(assetsCommonName + "_powered");
}
@Override
public void updateTick(World world, int x, int y, int z, Random useless)
{
List list = world.selectEntitiesWithinAABB(EntityMinecartContainer.class, AxisAlignedBB.getAABBPool().getAABB(x, y, z, x + 1.0D, y + 1.0D, z + 1.0D), IEntitySelector.selectAnything);
int metadata = world.getBlockMetadata(x, y, z);
if(list.size() > 0)//at least one entity
{
int powerFromNeighbours = world.getStrongestIndirectPower(x, y, z);
int redstoneSignalFromCart = 0;
for(Object cart : list)//hopefully there will be only one
{
int temp = Container.calcRedstoneFromInventory((IInventory)cart);
if(temp > redstoneSignalFromCart)
redstoneSignalFromCart = temp;
}
//if the cart over the tracks has a stronger signal than the input from neighbour blocks, turn
//the track on
if(redstoneSignalFromCart > powerFromNeighbours)
{
turnOn(world, x, y, z, metadata);
}
else
{
turnOff(world, x, y, z, metadata);
}
}
else //there are no minecart over this track, turn it off
{
turnOff(world, x, y, z, metadata);
}
ScheduleUpdate(world, x, y, z);
}
@Override
public void onMinecartPass(World world, EntityMinecart cart, int x, int y, int z)
{
if(cart instanceof IInventory)
{
int currentMetadata = world.getBlockMetadata(x, y, z);
if(isOn(currentMetadata))
{
addVelocity(cart, currentMetadata);
}
else
{
if(registerDirection(cart))
stopCart(cart);
}
}
}
public void ScheduleUpdate(World world, int x, int y, int z)
{
world.scheduleBlockUpdate(x, y, z, this.blockID, 100);
}
/**
* Called when a block is placed using its ItemBlock. Args: World, X, Y, Z, side, hitX, hitY, hitZ, block metadata
*/
@Override
public int onBlockPlaced(World world, int x, int y, int z, int side, float hitX, float hitY, float hitZ, int metadata)
{
ScheduleUpdate(world, x, y, z);
return metadata;
}
/**
* Register the directions for the cart before it stops. It returns true if the cart wasn't already stopped,
* and false otherwise. The result can be used to avoid calling stopCart over and over again.
* @param cart
* @return
*/
protected boolean registerDirection(EntityMinecart cart)
{
DirectionXProp directions = (DirectionXProp)cart.getExtendedProperties(DirectionXProp.Identifier);
if(directions.IsStopped)
{
return false;
}
else
{
directions.IsStopped = true;
if(cart.prevPosX > cart.posX)
{
//System.out.println("This cart is moving towards -X");
directions.XOrientation = -1;
}
else if(cart.prevPosX == cart.posX)
{
//System.out.println("this cart is not moving along the X axis");
directions.XOrientation = 0;
}
else
{
//System.out.println("this cart is moving towards +X");
directions.XOrientation = +1;
}
if(cart.prevPosZ > cart.posZ)
{
//System.out.println("This cart is moving towards -Z");
directions.ZOrientation = -1;
}
else if(cart.prevPosZ == cart.posZ)
{
//System.out.println("this cart is not moving along the Z axis");
directions.ZOrientation = 0;
}
else
{
//System.out.println("this cart is moving towards +Z");
directions.ZOrientation = +1;
}
return true;
}
}
protected void turnOn(World world, int x, int y, int z, int metadata)
{
if((metadata & == 0)//equivalent to !isOn
{
int onStateMetadata = metadata ^ 8;
world.setBlockMetadataWithNotify(x, y, z, onStateMetadata, 2);
world.notifyBlocksOfNeighborChange(x, y, z, this.blockID);
}
}
protected void turnOff(World world, int x, int y, int z, int metadata)
{
if((metadata & != 0)//equivalent to isOn
{
int offStateMetadata = metadata ^ 8;
world.setBlockMetadataWithNotify(x, y, z, offStateMetadata, 2);
world.notifyBlocksOfNeighborChange(x, y, z, this.blockID);
}
}
protected boolean isOn(int metadata)
{
return (metadata & != 0;
}
protected void addVelocity(EntityMinecart cart, int metadata)
{
//otherwise this will never work, since it's only called when the 8 bit is on
metadata = metadata & 7;
double velocityAdjustment;
DirectionXProp directions = (DirectionXProp)cart.getExtendedProperties(DirectionXProp.Identifier);
switch(metadata)
{
case 0:
case 4:
case 5:
if(directions.ZOrientation > 0)
{
//System.out.println("Resuming movement towards +Z");
velocityAdjustment = 0.4;
}
else
{
//System.out.println("Resuming movement towards -Z");
velocityAdjustment = -0.4;
}
velocityAdjustment = cart.motionZ == 0 ? velocityAdjustment : cart.motionZ * 0.4;
directions.IsStopped = false;
cart.addVelocity(0, 0, velocityAdjustment);
break;
case 1:
case 2:
case 3:
if(directions.XOrientation > 0)
{
//System.out.println("Resuming movement towards +X");
velocityAdjustment = 0.4;
}
else
{
//System.out.println("Resuming movement towards -X");
velocityAdjustment = -0.4;
}
velocityAdjustment = cart.motionX == 0 ? velocityAdjustment : cart.motionX * 0.4;
directions.IsStopped = false;
cart.addVelocity(velocityAdjustment, 0, 0);
break;
}
}
protected void stopCart(EntityMinecart cart)
{
cart.setVelocity(0, 0, 0);
}
}