smooth sailing on conveyor belts

This commit is contained in:
Bob 2022-06-01 20:47:35 +02:00
parent baff99e481
commit ed15803a42
7 changed files with 171 additions and 110 deletions

View File

@ -0,0 +1,8 @@
package api.hbm.conveyor;
import net.minecraft.item.ItemStack;
public interface IConveyorItem {
public ItemStack getItemStack();
}

View File

@ -0,0 +1,9 @@
package api.hbm.conveyor;
import net.minecraftforge.common.util.ForgeDirection;
public interface IEnterableBlock {
public boolean canEnter(IConveyorItem entity, ForgeDirection dir);
public void onEnter(IConveyorItem entity, ForgeDirection dir);
}

View File

@ -2,6 +2,9 @@ package com.hbm.entity.item;
import com.hbm.blocks.ModBlocks; import com.hbm.blocks.ModBlocks;
import api.hbm.conveyor.IConveyorItem;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
@ -12,138 +15,179 @@ import net.minecraft.util.DamageSource;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection; import net.minecraftforge.common.util.ForgeDirection;
public class EntityMovingItem extends Entity { public class EntityMovingItem extends Entity implements IConveyorItem {
private int turnProgress;
private double syncPosX;
private double syncPosY;
private double syncPosZ;
@SideOnly(Side.CLIENT)
private double velocityX;
@SideOnly(Side.CLIENT)
private double velocityY;
@SideOnly(Side.CLIENT)
private double velocityZ;
public EntityMovingItem(World p_i1582_1_) { public EntityMovingItem(World p_i1582_1_) {
super(p_i1582_1_); super(p_i1582_1_);
this.setSize(0.5F, 0.25F); this.setSize(0.5F, 0.5F);
this.noClip = true; this.noClip = true;
} }
public void setItemStack(ItemStack stack) { public void setItemStack(ItemStack stack) {
this.getDataWatcher().updateObject(10, stack); this.getDataWatcher().updateObject(10, stack);
this.getDataWatcher().setObjectWatched(10); this.getDataWatcher().setObjectWatched(10);
} }
public ItemStack getItemStack() { public ItemStack getItemStack() {
ItemStack stack = this.getDataWatcher().getWatchableObjectItemStack(10); ItemStack stack = this.getDataWatcher().getWatchableObjectItemStack(10);
return stack == null ? new ItemStack(Blocks.stone) : stack; return stack == null ? new ItemStack(Blocks.stone) : stack;
} }
public boolean canBeCollidedWith() { public boolean canBeCollidedWith() {
return true; return true;
} }
public boolean interactFirst(EntityPlayer player) { public boolean interactFirst(EntityPlayer player) {
if(!worldObj.isRemote && player.inventory.addItemStackToInventory(this.getItemStack().copy())) { if(!worldObj.isRemote && player.inventory.addItemStackToInventory(this.getItemStack().copy())) {
this.setDead(); this.setDead();
} }
return false; return false;
} }
public boolean attackEntityFrom(DamageSource source, float amount) { public boolean attackEntityFrom(DamageSource source, float amount) {
if(!worldObj.isRemote) { if(!worldObj.isRemote) {
this.setDead(); this.setDead();
worldObj.spawnEntityInWorld(new EntityItem(worldObj, posX, posY, posZ, this.getItemStack())); worldObj.spawnEntityInWorld(new EntityItem(worldObj, posX, posY, posZ, this.getItemStack()));
} }
return true; return true;
} }
public boolean canAttackWithItem() { public boolean canAttackWithItem() {
return true; return true;
} }
public boolean hitByEntity(Entity attacker) { public boolean hitByEntity(Entity attacker) {
if(attacker instanceof EntityPlayer) { if(attacker instanceof EntityPlayer) {
} }
this.setDead(); this.setDead();
return false; return false;
} }
protected boolean canTriggerWalking() { protected boolean canTriggerWalking() {
return true; return true;
} }
private int schedule = 0; private int schedule = 0;
public void onUpdate() { public void onUpdate() {
if(!worldObj.isRemote) { if(worldObj.isRemote) {
if(this.turnProgress > 0) {
if(worldObj.getBlock((int)Math.floor(posX), (int)Math.floor(posY), (int)Math.floor(posZ)) != ModBlocks.conveyor) { double interpX = this.posX + (this.syncPosX - this.posX) / (double) this.turnProgress;
this.setDead(); double interpY = this.posY + (this.syncPosY - this.posY) / (double) this.turnProgress;
EntityItem item = new EntityItem(worldObj, posX, posY, posZ, this.getItemStack()); double interpZ = this.posZ + (this.syncPosZ - this.posZ) / (double) this.turnProgress;
item.motionX = this.motionX * 3; --this.turnProgress;
item.motionY = 0.1; this.setPosition(interpX, interpY, interpZ);
item.motionZ = this.motionZ * 3; } else {
item.velocityChanged = true; this.setPosition(this.posX, this.posY, this.posZ);
worldObj.spawnEntityInWorld(item); }
return; }
}
if(!worldObj.isRemote) {
if(worldObj.getBlock((int)Math.floor(posX), (int)Math.floor(posY), (int)Math.floor(posZ)) == ModBlocks.conveyor) {
if(worldObj.getBlock((int) Math.floor(posX), (int) Math.floor(posY), (int) Math.floor(posZ)) != ModBlocks.conveyor) {
if(schedule <= 0) { this.setDead();
ForgeDirection dir = ForgeDirection.getOrientation(worldObj.getBlockMetadata((int)Math.floor(posX), (int)Math.floor(posY), (int)Math.floor(posZ))); EntityItem item = new EntityItem(worldObj, posX, posY, posZ, this.getItemStack());
item.motionX = this.motionX * 3;
if(worldObj.getBlock((int)Math.floor(posX), (int)Math.floor(posY) + 1, (int)Math.floor(posZ)) == ModBlocks.conveyor && motionY >= 0) { item.motionY = 0.1;
dir = ForgeDirection.DOWN; item.motionZ = this.motionZ * 3;
} item.velocityChanged = true;
worldObj.spawnEntityInWorld(item);
if(worldObj.getBlock((int)Math.floor(posX), (int)Math.floor(posY) - 1, (int)Math.floor(posZ)) == ModBlocks.conveyor && motionY <= 0) { return;
dir = ForgeDirection.UP; }
}
if(worldObj.getBlock((int) Math.floor(posX), (int) Math.floor(posY), (int) Math.floor(posZ)) == ModBlocks.conveyor) {
double speed = 0.0625;
if(schedule <= 0) {
schedule = (int) (1 / speed); ForgeDirection dir = ForgeDirection.getOrientation(worldObj.getBlockMetadata((int) Math.floor(posX), (int) Math.floor(posY), (int) Math.floor(posZ)));
motionX = -speed * dir.offsetX;
motionY = -speed * dir.offsetY; if(worldObj.getBlock((int) Math.floor(posX), (int) Math.floor(posY) + 1, (int) Math.floor(posZ)) == ModBlocks.conveyor && motionY >= 0) {
motionZ = -speed * dir.offsetZ; dir = ForgeDirection.DOWN;
}
this.velocityChanged = true;
} if(worldObj.getBlock((int) Math.floor(posX), (int) Math.floor(posY) - 1, (int) Math.floor(posZ)) == ModBlocks.conveyor && motionY <= 0) {
dir = ForgeDirection.UP;
schedule--; }
}
} double speed = 0.0625;
this.moveEntity(motionX, motionY, motionZ);
} schedule = (int) (1 / speed);
motionX = -speed * dir.offsetX;
motionY = -speed * dir.offsetY;
motionZ = -speed * dir.offsetZ;
this.velocityChanged = true;
}
schedule--;
}
this.moveEntity(motionX, motionY, motionZ);
}
}
@SideOnly(Side.CLIENT)
public void setVelocity(double p_70016_1_, double p_70016_3_, double p_70016_5_) {
this.velocityX = this.motionX = p_70016_1_;
this.velocityY = this.motionY = p_70016_3_;
this.velocityZ = this.motionZ = p_70016_5_;
}
@SideOnly(Side.CLIENT)
public void setPositionAndRotation2(double x, double y, double z, float yaw, float pitch, int theNumberThree) {
this.syncPosX = x;
this.syncPosY = y;
this.syncPosZ = z;
this.turnProgress = theNumberThree + 7; //use 4-ply for extra smoothness
this.motionX = this.velocityX;
this.motionY = this.velocityY;
this.motionZ = this.velocityZ;
}
@Override @Override
protected void entityInit() { protected void entityInit() {
this.getDataWatcher().addObjectByDataType(10, 5); this.getDataWatcher().addObjectByDataType(10, 5);
} }
@Override @Override
protected void readEntityFromNBT(NBTTagCompound nbt) { protected void readEntityFromNBT(NBTTagCompound nbt) {
NBTTagCompound compound = nbt.getCompoundTag("Item"); NBTTagCompound compound = nbt.getCompoundTag("Item");
this.setItemStack(ItemStack.loadItemStackFromNBT(compound)); this.setItemStack(ItemStack.loadItemStackFromNBT(compound));
ItemStack stack = getDataWatcher().getWatchableObjectItemStack(10); ItemStack stack = getDataWatcher().getWatchableObjectItemStack(10);
schedule = nbt.getInteger("schedule");
if (stack == null || stack.stackSize <= 0) schedule = nbt.getInteger("schedule");
this.setDead();
if(stack == null || stack.stackSize <= 0)
this.setDead();
} }
@Override @Override
protected void writeEntityToNBT(NBTTagCompound nbt) { protected void writeEntityToNBT(NBTTagCompound nbt) {
if (this.getItemStack() != null) if(this.getItemStack() != null)
nbt.setTag("Item", this.getItemStack().writeToNBT(new NBTTagCompound())); nbt.setTag("Item", this.getItemStack().writeToNBT(new NBTTagCompound()));
nbt.setInteger("schedule", schedule);
}
nbt.setInteger("schedule", schedule);
}
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 246 B

After

Width:  |  Height:  |  Size: 239 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 256 B

After

Width:  |  Height:  |  Size: 199 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 177 B

After

Width:  |  Height:  |  Size: 176 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 134 B

After

Width:  |  Height:  |  Size: 162 B