Skip to content

Commit

Permalink
hitbox reworks
Browse files Browse the repository at this point in the history
- Hitboxes are now properly registered entities. This will create more bugs than it fixes but it's important I swear.
- Locomotive now properly defines it's minecart canBeAdjusted values, this should help with railcraft stuff maybe.
- TC rail block and gag now have consistent collision heights.
- Game now warns about "towing mode" when linking two trains. This is the closest this feature has come to documentation anyone will read in the past 10 years.
- Lots of new linking bugs! Can you find them all?
  • Loading branch information
EternalBlueFlame committed Jan 14, 2025
1 parent 7828aba commit 5fb27b5
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 25 deletions.
14 changes: 7 additions & 7 deletions src/main/java/train/common/api/EntityRollingStock.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ public GameProfile getOwner() {
public EntityRollingStock(World world, double d, double d1, double d2) {
super(world, d, d1, d2);
if(world==null){return;}
initRollingStock(world);
setPosition(d, d1 + yOffset, d2);
initRollingStock(world);
motionX = 0.0D;
motionY = 0.0D;
motionZ = 0.0D;
Expand Down Expand Up @@ -1011,12 +1011,6 @@ else if (seats.size() != 0 && worldObj.isRemote && Traincraft.proxy.getCurrentSc
handleTrain();
handleOverheating.HandleHeatLevel(this);
linkhandler.handleStake(this);
//update the collision handler's positions
if(collisionHandler!=null){
collisionHandler.position(posX, posY, posZ, rotationPitch, getYaw());
collisionHandler.updateCollidingEntities(this);
collisionHandler.manageCollision(this);
}
this.func_145775_I();
MinecraftForge.EVENT_BUS.post(new MinecartUpdateEvent(this, floor_posX, floor_posY, floor_posZ));
//setBoundingBoxSmall(posX, posY, posZ, 0.98F, 0.7F);
Expand All @@ -1026,6 +1020,12 @@ else if (seats.size() != 0 && worldObj.isRemote && Traincraft.proxy.getCurrentSc
}


//update the collision handler's positions
if(collisionHandler!=null){
collisionHandler.position(posX, posY, posZ, rotationPitch, getYaw());
collisionHandler.updateCollidingEntities(this);
collisionHandler.manageCollision(this);
}
for (EntitySeat seat: seats) { //handle died in train
if (seat.getPassenger() != null && (seat.getPassenger().isDead || seat != seat.getPassenger().ridingEntity)) {
seat.getPassenger().ridingEntity = null;
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/train/common/api/Locomotive.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.netty.buffer.ByteBuf;
import net.minecraft.block.material.Material;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityMinecart;
import net.minecraft.entity.passive.EntityAnimal;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
Expand Down Expand Up @@ -492,6 +493,10 @@ public boolean canBePushed() {
public void setCanBeAdjusted(boolean canBeAdj) {
this.canBeAdjusted = canBeAdj;
}
@Override
public boolean canBeAdjusted(EntityMinecart cart) {
return canBeAdjusted;
}

/**
* gets packet from server and distribute for GUI handles motion
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/train/common/blocks/BlockTCRail.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class BlockTCRail extends Block {
public BlockTCRail() {
super(Material.iron);
setCreativeTab(Traincraft.tcTab);
this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.0F, 1.0F);
this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.025F, 1.0F);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/train/common/blocks/BlockTCRailGag.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class BlockTCRailGag extends Block {
public BlockTCRailGag() {
super(Material.iron);
setCreativeTab(Traincraft.tcTab);
this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.125F, 1.0F);
this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.025F, 1.0F);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/train/common/core/handlers/EntityHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import train.common.api.EntityBogie;
import train.common.api.TrainRecord;
import train.common.core.EntityIds;
import train.common.entity.CollisionBox;
import train.common.entity.zeppelin.EntityZeppelinOneBalloon;
import train.common.entity.zeppelin.EntityZeppelinTwoBalloons;
import train.common.library.EnumTrains;
Expand All @@ -27,6 +28,7 @@ public static void init() {
EntityRegistry.registerModEntity(EntityBogie.class, "Entity Front Bogie", EntityIds.LOCOMOTIVE_BOGIE, Traincraft.instance, 512, 1, true);//front bogie
EntityRegistry.registerModEntity(EntityZeppelinOneBalloon.class, "zeppelin big", EntityIds.ZEPPELIN_BIG, Traincraft.instance, 512, 1, true);//zepplin big
EntityRegistry.registerModEntity(EntitySeat.class, "Seat", 16, Traincraft.instance,80,3,true);//seat
EntityRegistry.registerModEntity(CollisionBox.class, "collisionBox", 17, Traincraft.instance,80,3,false);//hitboxes
for(TrainRecord trains : EnumTrains.trains()){
TraincraftRegistry.registerTransport(trains);
}
Expand Down
20 changes: 14 additions & 6 deletions src/main/java/train/common/entity/CollisionBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import mods.railcraft.api.carts.IMinecart;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.entity.IEntityMultiPart;
import net.minecraft.entity.boss.EntityDragonPart;
import net.minecraft.entity.item.EntityMinecart;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.DamageSource;
import net.minecraft.util.MovingObjectPosition;
Expand All @@ -28,9 +30,15 @@ public class CollisionBox extends EntityDragonPart implements IInventory, IFluid
static String dragonBoxName ="b";
public EntityRollingStock host;

//this isnt actually used, but if we remove it, compiler complains.
public CollisionBox(World w){
super(null,dragonBoxName,1,1);
//client side entity registration shenanagains. this lets us register the hitbox as a real entity
public CollisionBox(final World w){
super(new IEntityMultiPart() {
@Override
public World func_82194_d() {return w;}

@Override
public boolean attackEntityFromPart(EntityDragonPart p, DamageSource d, float i) {return false;}
},dragonBoxName,1,1);
}

public CollisionBox(EntityRollingStock transport) {
Expand All @@ -46,7 +54,7 @@ public boolean canBeCollidedWith() {

@Override
public String getCommandSenderName() {
return host.getCommandSenderName();
return host==null?"collisionBox":host.getCommandSenderName();
}

@Override
Expand All @@ -68,7 +76,7 @@ public void onUpdate() {
}
}
if (ticksExisted % 100 == 0) {
if (!(worldObj.getEntityByID(host.getEntityId()) instanceof EntityRollingStock)) {
if (host ==null || !(worldObj.getEntityByID(host.getEntityId()) instanceof EntityRollingStock)) {
this.setDead();
worldObj.removeEntity(this);
}
Expand All @@ -77,7 +85,7 @@ public void onUpdate() {

@Override
public boolean attackEntityFrom(DamageSource damageSource, float p_70097_2_) {
return this.host.attackEntityFromPart(this, damageSource, p_70097_2_);
return host != null && this.host.attackEntityFromPart(this, damageSource, p_70097_2_);
}

@Override
Expand Down
23 changes: 13 additions & 10 deletions src/main/java/train/common/entity/EntityHitbox.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,21 @@ public void manageCollision(EntityRollingStock host){
}
} else {
if (e instanceof CollisionBox) {
if(((CollisionBox) e).host==null){
return;
}
EntityRollingStock entityOne = (((CollisionBox) e).host);
if (entityOne.isAttaching && host.isAttaching) {
LinkHandler.addStake(host, entityOne, true);
LinkHandler.addStake(entityOne, host, true);
if(entityOne instanceof Locomotive && host instanceof Locomotive && (entityOne.canBeAdjusted(null) || host.canBeAdjusted(null))){
LinkHandler.addStake(host, entityOne, true);
LinkHandler.addStake(entityOne, host, true);
} else {
EntityPlayer p = host.getWorld().getClosestPlayerToEntity(host,32);
if(p!=null){
p.addChatComponentMessage(new ChatComponentText("One or more trains is not in towing mode."));
p.addChatComponentMessage(new ChatComponentText("Use a Stake while sneaking to toggle towing mode."));
}
}
return;
}
double[] motion = CommonUtil.rotatePoint(0.005, 0,
Expand Down Expand Up @@ -227,14 +238,6 @@ public boolean containsEntity(Entity e){
return true;
}
}
//for whatever reason the collision boxes dont seem to exist in the WORLD, but they do exist in their host.
if(e instanceof EntityRollingStock){
for(CollisionBox otherBox : ((EntityRollingStock) e).collisionHandler.interactionBoxes){
if(Math.abs(otherBox.posX-box.posX)<0.5 && Math.abs(otherBox.posZ-box.posZ)<0.5 && Math.abs(otherBox.posY-box.posY)<2){
return true;
}
}
}
//check for X
if (e.boundingBox.intersectsWith(box.boundingBox.expand(0.4D, e instanceof EntityPlayer ?1.2D:0.4D, 0.4D)))
return true;
Expand Down

0 comments on commit 5fb27b5

Please sign in to comment.