Skip to content

Commit

Permalink
Merge pull request #72 from Kiroto/powerGloveBlockFix
Browse files Browse the repository at this point in the history
Power glove block fix
  • Loading branch information
TechPizzaDev authored May 26, 2024
2 parents 68c1e83 + c5d9707 commit 315fbd4
Showing 1 changed file with 63 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,77 @@ protected AC_ItemPowerGlove(int id) {
super(id);
}

private FallingBlockEntity currentFallingBlock = null;

/**
* Uses the current item to push a given block.
*
* @param stack The current power glove item stack.
* @param player The player that is trying to move the block
* @param world The block in which the block is being moved
* @param x The x position of the block
* @param y The y position of the block
* @param z The z position of the block
* @param side The side from which the block was moved.
* @return false if the move operation was unsuccessful. Otherwise, true.
*/
@Override
public boolean useOnBlock(ItemStack stack, PlayerEntity player, World world, int x, int y, int z, int side) {
boolean currentBlockExists = currentFallingBlock != null && !currentFallingBlock.removed;
if (currentBlockExists) {
// Saving and exiting while the block is falling keeps the falling block entity saved to the item.
// In this case, the glove thinks there is a falling block, but it isn't on the current world.
// This would mean that the player would not be able to use the power glove until the game is reset.
// So we check if the currently tracked falling block is on this world:
// If it is, do nothing (fail to push), if not, unset it and proceed with the push.
if (currentFallingBlock.world == world) return false;
else currentFallingBlock = null;
}
int xDir = 0;
int zDir = 0;
if (side == 2) {
zDir = 1;
} else if (side == 3) {
zDir = -1;
} else if (side == 4) {
xDir = 1;
} else {
if (side != 5) {

switch (side) {
case 2:
zDir = 1;
break;
case 3:
zDir = -1;
break;
case 4:
xDir = 1;
break;
case 5:
xDir = -1;
break;
default:
return false;
}
}

xDir = -1;
int currentBlockId = world.getBlockId(x, y, z);
boolean isPushableBlock = currentBlockId != AC_Blocks.pushableBlock.id;

if (!isPushableBlock) return false;

boolean isPulling = Keyboard.isKeyDown(Keyboard.KEY_LCONTROL) || Keyboard.isKeyDown(Keyboard.KEY_RCONTROL);

if (isPulling) {
xDir *= -1;
zDir *= -1;
}

if (world.getBlockId(x, y, z) != AC_Blocks.pushableBlock.id) {
return false;
} else {
if (Keyboard.isKeyDown(Keyboard.KEY_LCONTROL) || Keyboard.isKeyDown(Keyboard.KEY_RCONTROL)) {
xDir *= -1;
zDir *= -1;
}

int id = world.getBlockId(x + xDir, y, z + zDir);
if (Block.BY_ID[id] == null || Block.BY_ID[id].material.isLiquid() || id == Block.FIRE.id) {
int var11 = world.getBlockId(x, y, z);
int var12 = world.getBlockMeta(x, y, z);
world.placeBlockWithMetaData(x, y, z, 0, 0);
var var13 = new FallingBlockEntity(world, (double) x + 0.5D, (double) y + 0.5D, (double) z + 0.5D, var11);
var13.xVelocity = 0.3D * (double) xDir;
var13.zVelocity = 0.3D * (double) zDir;
((ExFallingBlockEntity) var13).setMetadata(var12);
world.spawnEntity(var13);
}

return true;
int destinationBlockId = world.getBlockId(x + xDir, y, z + zDir);
Block destinationBlock = Block.BY_ID[destinationBlockId];
boolean isValidDestination = destinationBlock == null || destinationBlock.material.isLiquid() || destinationBlockId == Block.FIRE.id;

if (isValidDestination) {
int blockMetadata = world.getBlockMeta(x, y, z);
world.placeBlockWithMetaData(x, y, z, 0, 0);
currentFallingBlock = new FallingBlockEntity(world, (double) x + 0.5D, (double) y + 0.5D, (double) z + 0.5D, currentBlockId);
currentFallingBlock.xVelocity = 0.3D * (double) xDir;
currentFallingBlock.zVelocity = 0.3D * (double) zDir;
((ExFallingBlockEntity) currentFallingBlock).setMetadata(blockMetadata);
world.spawnEntity(currentFallingBlock);
}
return true;
}
}

0 comments on commit 315fbd4

Please sign in to comment.