Skip to content

Commit

Permalink
Re-implement Block Entity Collision interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
cam72cam committed Dec 22, 2023
1 parent f6dbe65 commit 1fc86ca
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
7 changes: 6 additions & 1 deletion src/main/java/cam72cam/mod/block/BlockType.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import cam72cam.mod.util.Facing;
import cam72cam.mod.util.SingleCache;
import cam72cam.mod.world.World;
import net.minecraft.block.Block;
import net.minecraft.block.state.BlockFaceShape;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.Entity;
Expand Down Expand Up @@ -140,7 +141,11 @@ public int getWeakPower(World world, Vec3i vec3i, Facing from) {
* BlockInternal is an internal class that should only be extended when you need to implement
* an interface.
*/
protected class BlockInternal extends net.minecraft.block.Block {
protected class BlockInternal extends net.minecraft.block.Block implements IBlockTypeBlock {
public BlockType getType() {
return BlockType.this;
}

public BlockInternal() {
super(BlockType.this.getMaterial().internal);
BlockType type = BlockType.this;
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/cam72cam/mod/block/IBlockTypeBlock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package cam72cam.mod.block;

public interface IBlockTypeBlock {
BlockType getType();
}
8 changes: 8 additions & 0 deletions src/main/java/cam72cam/mod/world/IBlockEntityCollision.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package cam72cam.mod.world;

import cam72cam.mod.entity.Entity;
import cam72cam.mod.math.Vec3i;

public interface IBlockEntityCollision {
boolean canCollide(World world, Vec3i pos, Entity entity);
}
3 changes: 2 additions & 1 deletion src/main/java/cam72cam/mod/world/IConditionalCollision.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cam72cam.mod.world;

import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.Entity;
import net.minecraft.util.DamageSource;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
Expand All @@ -21,6 +22,6 @@ public interface IConditionalCollision {
* @param damageSource Damage source that would be used to collide with the block.
* @return Whether or not to calculate actual collision.
*/
boolean canCollide(World world, BlockPos pos, IBlockState state, DamageSource damageSource);
boolean canCollide(World world, BlockPos pos, IBlockState state, Entity entity);

}
19 changes: 15 additions & 4 deletions src/main/java/cam72cam/mod/world/World.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cam72cam.mod.ModCore;
import cam72cam.mod.block.BlockEntity;
import cam72cam.mod.block.BlockType;
import cam72cam.mod.block.IBlockTypeBlock;
import cam72cam.mod.block.tile.TileEntity;
import cam72cam.mod.entity.*;
import cam72cam.mod.entity.boundingbox.BoundingBox;
Expand Down Expand Up @@ -628,10 +629,20 @@ public void setBlock(Vec3i pos, BlockInfo info) {
}

/** Opt in collision overriding */
public boolean canEntityCollideWith(Vec3i bp, String damageType) {
Block block = internal.getBlockState(bp.internal()).getBlock();
return ! (block instanceof IConditionalCollision) ||
((IConditionalCollision) block).canCollide(internal, bp.internal(), internal.getBlockState(bp.internal()), new DamageSource(damageType));
public boolean canEntityCollideWith(Vec3i bp, Entity entity) {
IBlockState state = internal.getBlockState(bp.internal());
Block block = state.getBlock();

if (block instanceof IConditionalCollision && ((IConditionalCollision) block).canCollide(internal, bp.internal(), state, entity.internal))
return true;

if (block instanceof IBlockTypeBlock) {
BlockType type = ((IBlockTypeBlock) block).getType();
if (type instanceof IBlockEntityCollision) {
return ((IBlockEntityCollision) type).canCollide(this, bp, entity);
}
}
return false;
}

/** Spawn a particle */
Expand Down

0 comments on commit 1fc86ca

Please sign in to comment.