Skip to content

Commit 191819f

Browse files
committed
Clean up felling direction code a bit Fixes #11 hopefully
1 parent 4ef4d4c commit 191819f

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

src/main/java/mods/timberjack/common/entity/EntityTimber.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class EntityTimber extends Entity implements IEntityAdditionalSpawnData {
5151
private float fallHurtAmount = 2.0F;
5252
private NBTTagCompound tileEntityData;
5353
private List<ItemStack> drops = new ArrayList<>();
54-
private EnumFacing fellingDirection;
54+
private EnumFacing fellingDirection = EnumFacing.UP;
5555

5656
public EntityTimber(World worldIn) {
5757
super(worldIn);
@@ -123,7 +123,7 @@ public void onUpdate() {
123123

124124
IBlockState state = this.worldObj.getBlockState(currentPos);
125125
if (state.getBlock() == block) {
126-
if (!log) {
126+
if (!this.worldObj.isRemote && !log) {
127127
drops.addAll(block.getDrops(worldObj, currentPos, state, 0));
128128
}
129129
this.worldObj.setBlockToAir(currentPos);
@@ -260,7 +260,7 @@ private boolean doTileDrops() {
260260
return worldObj.getGameRules().getBoolean("doEntityDrops");
261261
}
262262

263-
public void rotateLog(IBlockState state, BlockPos pos) {
263+
private void rotateLog(IBlockState state, BlockPos pos) {
264264
if (state.getBlock() instanceof BlockLog && state.getProperties().containsKey(BlockLog.LOG_AXIS)) {
265265
BlockLog.EnumAxis axis;
266266
switch (fellingDirection.getAxis()) {
@@ -273,8 +273,10 @@ public void rotateLog(IBlockState state, BlockPos pos) {
273273
default:
274274
axis = BlockLog.EnumAxis.Y;
275275
}
276-
IBlockState newState = state.withProperty(BlockLog.LOG_AXIS, axis);
277-
worldObj.setBlockState(pos, newState);
276+
if (axis != BlockLog.EnumAxis.Y) {
277+
IBlockState newState = state.withProperty(BlockLog.LOG_AXIS, axis);
278+
worldObj.setBlockState(pos, newState);
279+
}
278280
}
279281
}
280282

@@ -291,7 +293,7 @@ protected void writeEntityToNBT(NBTTagCompound compound) {
291293
compound.setBoolean("HurtEntities", this.hurtEntities);
292294
compound.setFloat("FallHurtAmount", this.fallHurtAmount);
293295
compound.setInteger("FallHurtMax", this.fallHurtMax);
294-
compound.setInteger("FallingDirection", this.fellingDirection.ordinal());
296+
compound.setInteger("FellingDirection", this.fellingDirection.ordinal());
295297

296298
if (this.tileEntityData != null) {
297299
compound.setTag("TileEntityData", this.tileEntityData);
@@ -329,7 +331,7 @@ protected void readEntityFromNBT(NBTTagCompound compound) {
329331
this.tileEntityData = compound.getCompoundTag("TileEntityData");
330332
}
331333

332-
this.fellingDirection = EnumFacing.VALUES[compound.getInteger("FallingDirection")];
334+
this.fellingDirection = EnumFacing.VALUES[compound.getInteger("FellingDirection")];
333335

334336
if (block == null || block.getDefaultState().getMaterial() == Material.AIR) {
335337
this.fallingBlock = Blocks.LOG.getDefaultState();
@@ -370,10 +372,14 @@ public boolean ignoreItemEntityData() {
370372
@Override
371373
public void writeSpawnData(ByteBuf buffer) {
372374
buffer.writeInt(Block.getStateId(fallingBlock));
375+
buffer.writeBoolean(log);
376+
buffer.writeByte(fellingDirection.ordinal());
373377
}
374378

375379
@Override
376380
public void readSpawnData(ByteBuf additionalData) {
377-
fallingBlock = Block.getStateById(additionalData.getInt(additionalData.readerIndex()) & 65535);
381+
fallingBlock = Block.getStateById(additionalData.readInt() & 65535);
382+
log = additionalData.readBoolean();
383+
fellingDirection = EnumFacing.VALUES[additionalData.readByte()];
378384
}
379385
}

src/main/java/mods/timberjack/common/felling/TimberjackEventHandler.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,17 @@ public void chopEvent(BlockEvent.BreakEvent event) {
7272
if ((!event.getPlayer().isSneaking() || !TimberjackConfig.sneakingPreventsFelling()) && TimberjackUtils.isWood(event.getState(), world, event.getPos())) {
7373
EnumFacing fellingDirection;
7474
if (world.rand.nextFloat() < 0.1) {
75-
fellingDirection = EnumFacing.HORIZONTALS[new Random().nextInt(EnumFacing.HORIZONTALS.length)];
75+
fellingDirection = getRandomHorizontalFacing();
7676
} else {
7777
fellingDirection = BlockPistonBase.getFacingFromEntity(event.getPos(), event.getPlayer()).getOpposite();
78+
if (fellingDirection.getAxis() == EnumFacing.Axis.Y)
79+
fellingDirection = getRandomHorizontalFacing();
7880
}
7981
FellingManager.fellingManagers.computeIfAbsent(world, FellingManager::new).onChop(event.getPos(), fellingDirection);
8082
}
8183
}
84+
85+
private EnumFacing getRandomHorizontalFacing() {
86+
return EnumFacing.HORIZONTALS[new Random().nextInt(EnumFacing.HORIZONTALS.length)];
87+
}
8288
}

src/main/java/mods/timberjack/common/felling/TimberjackUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ private static void spawnFalling(World world, BlockPos pos, Vec3d centroid, IBlo
113113
Vec3d vector = new Vec3d(pos.getX(), 0, pos.getZ());
114114
vector = vector.subtract(centroid.xCoord, 0, centroid.zCoord);
115115
vector = vector.normalize().scale(0.5);
116-
vector = vector.add(new Vec3d(fellingDirection.getDirectionVec()));
116+
if (fellingDirection.getAxis() != EnumFacing.Axis.Y)
117+
vector = vector.add(new Vec3d(fellingDirection.getDirectionVec()));
117118
vector = vector.normalize();
118119
entity.motionX = vector.xCoord * 0.3 + (world.rand.nextFloat() - 0.5) * 0.15;
119120
entity.motionZ = vector.zCoord * 0.3 + (world.rand.nextFloat() - 0.5) * 0.15;

0 commit comments

Comments
 (0)