From 4a84f9e096774252995f68c8e23bd5b49bde76c3 Mon Sep 17 00:00:00 2001 From: Sen Date: Sun, 25 May 2025 15:26:14 +0200 Subject: [PATCH] format PlayerController --- client/src/client/util/PlayerController.java | 536 +++++++++---------- 1 file changed, 243 insertions(+), 293 deletions(-) diff --git a/client/src/client/util/PlayerController.java b/client/src/client/util/PlayerController.java index a489f4a..215c085 100755 --- a/client/src/client/util/PlayerController.java +++ b/client/src/client/util/PlayerController.java @@ -23,344 +23,294 @@ import common.util.Vec3; import common.world.State; import common.world.World; -public class PlayerController -{ - private final Client gm; - private final ClientPlayer netClientHandler; - private BlockPos currentBlock = new BlockPos(-1, -1, -1); - private ItemStack currentItemHittingBlock; - private float curBlockDamageMP; - private float stepSoundTickCounter; - private int blockHitDelay; - private boolean isHittingBlock; - private boolean noclip; - private int currentPlayerItem; - private boolean interacting; +public class PlayerController { + private final Client gm; + private final ClientPlayer netClientHandler; + private BlockPos currentBlock = new BlockPos(-1, -1, -1); + private ItemStack currentItemHittingBlock; + private float curBlockDamageMP; + private float stepSoundTickCounter; + private int blockHitDelay; + private boolean isHittingBlock; + private boolean noclip; + private int currentPlayerItem; + private boolean interacting; - public PlayerController(Client gmIn, ClientPlayer netHandler) - { - this.gm = gmIn; - this.netClientHandler = netHandler; - } + public PlayerController(Client gmIn, ClientPlayer netHandler) { + this.gm = gmIn; + this.netClientHandler = netHandler; + } - public boolean onPlayerDestroyBlock(BlockPos pos, Facing side) - { - World world = this.gm.world; - State iblockstate = world.getState(pos); - Block block1 = iblockstate.getBlock(); + public boolean onPlayerDestroyBlock(BlockPos pos, Facing side) { + World world = this.gm.world; + State iblockstate = world.getState(pos); + Block block1 = iblockstate.getBlock(); - if (block1 == Blocks.air) - { - return false; - } - else - { - world.playAuxSFX(2001, pos, BlockRegistry.getStateId(iblockstate)); - boolean flag = world.setBlockToAir(pos); + if(block1 == Blocks.air) { + return false; + } + else { + world.playAuxSFX(2001, pos, BlockRegistry.getStateId(iblockstate)); + boolean flag = world.setBlockToAir(pos); - if (flag) - { - block1.onBlockDestroyedByPlayer(world, pos, iblockstate); - } + if(flag) { + block1.onBlockDestroyedByPlayer(world, pos, iblockstate); + } - this.currentBlock = new BlockPos(this.currentBlock.getX(), -1, this.currentBlock.getZ()); + this.currentBlock = new BlockPos(this.currentBlock.getX(), -1, this.currentBlock.getZ()); - ItemStack itemstack1 = this.gm.player.getCurrentEquippedItem(); + ItemStack itemstack1 = this.gm.player.getCurrentEquippedItem(); - if (itemstack1 != null) - { - itemstack1.onBlockDestroyed(world, block1, pos, this.gm.player); + if(itemstack1 != null) { + itemstack1.onBlockDestroyed(world, block1, pos, this.gm.player); - if (itemstack1.stackSize == 0) - { - this.gm.player.destroyCurrentEquippedItem(); - } - } + if(itemstack1.stackSize == 0) { + this.gm.player.destroyCurrentEquippedItem(); + } + } - return flag; - } - } + return flag; + } + } - public boolean clickBlock(BlockPos loc, Facing face) - { - if (!World.isValidXZ(loc)) - { - return false; - } - else - { - ItemStack stack = this.gm.player.getHeldItem(); - if(stack != null && stack.getItem().onAction(stack, this.gm.player, this.gm.world, ItemControl.PRIMARY, loc)) { - this.interacting = true; - this.netClientHandler.addToSendQueue(new CPacketBreak(CPacketBreak.Action.START_DESTROY_BLOCK, loc, face)); - return true; - } - if (!this.isHittingBlock || !this.isHittingPosition(loc)) - { - if (this.isHittingBlock) - { - this.netClientHandler.addToSendQueue(new CPacketBreak(CPacketBreak.Action.ABORT_DESTROY_BLOCK, this.currentBlock, face)); - } + public boolean clickBlock(BlockPos loc, Facing face) { + if(!World.isValidXZ(loc)) { + return false; + } + else { + ItemStack stack = this.gm.player.getHeldItem(); + if(stack != null && stack.getItem().onAction(stack, this.gm.player, this.gm.world, ItemControl.PRIMARY, loc)) { + this.interacting = true; + this.netClientHandler.addToSendQueue(new CPacketBreak(CPacketBreak.Action.START_DESTROY_BLOCK, loc, face)); + return true; + } + if(!this.isHittingBlock || !this.isHittingPosition(loc)) { + if(this.isHittingBlock) { + this.netClientHandler.addToSendQueue(new CPacketBreak(CPacketBreak.Action.ABORT_DESTROY_BLOCK, this.currentBlock, face)); + } - this.netClientHandler.addToSendQueue(new CPacketBreak(CPacketBreak.Action.START_DESTROY_BLOCK, loc, face)); - Block block1 = this.gm.world.getState(loc).getBlock(); - boolean flag = block1 != Blocks.air; + this.netClientHandler.addToSendQueue(new CPacketBreak(CPacketBreak.Action.START_DESTROY_BLOCK, loc, face)); + Block block1 = this.gm.world.getState(loc).getBlock(); + boolean flag = block1 != Blocks.air; - if (flag && this.curBlockDamageMP == 0.0F) - { - block1.onBlockClicked(this.gm.world, loc, this.gm.player); - } + if(flag && this.curBlockDamageMP == 0.0F) { + block1.onBlockClicked(this.gm.world, loc, this.gm.player); + } - if (flag && block1.getPlayerRelativeBlockHardness(this.gm.player, this.gm.player.worldObj, loc) >= 1.0F) - { - this.onPlayerDestroyBlock(loc, face); - this.blockHitDelay = 3; - } - else - { - this.isHittingBlock = true; - this.currentBlock = loc; - this.currentItemHittingBlock = this.gm.player.getHeldItem(); - this.curBlockDamageMP = 0.0F; - this.stepSoundTickCounter = 0.0F; - this.gm.world.sendBlockBreakProgress(this.gm.player.getId(), this.currentBlock, (int)(this.curBlockDamageMP * 10.0F) - 1); - } - } + if(flag && block1.getPlayerRelativeBlockHardness(this.gm.player, this.gm.player.worldObj, loc) >= 1.0F) { + this.onPlayerDestroyBlock(loc, face); + this.blockHitDelay = 3; + } + else { + this.isHittingBlock = true; + this.currentBlock = loc; + this.currentItemHittingBlock = this.gm.player.getHeldItem(); + this.curBlockDamageMP = 0.0F; + this.stepSoundTickCounter = 0.0F; + this.gm.world.sendBlockBreakProgress(this.gm.player.getId(), this.currentBlock, (int)(this.curBlockDamageMP * 10.0F) - 1); + } + } - return true; - } - } + return true; + } + } - public void resetBlockRemoving() - { - if (this.isHittingBlock) - { - this.netClientHandler.addToSendQueue(new CPacketBreak(CPacketBreak.Action.ABORT_DESTROY_BLOCK, this.currentBlock, Facing.DOWN)); - this.isHittingBlock = false; - this.curBlockDamageMP = 0.0F; - this.gm.world.sendBlockBreakProgress(this.gm.player.getId(), this.currentBlock, -1); - } - } - - public void resetInteraction() - { - this.interacting = false; - } + public void resetBlockRemoving() { + if(this.isHittingBlock) { + this.netClientHandler.addToSendQueue(new CPacketBreak(CPacketBreak.Action.ABORT_DESTROY_BLOCK, this.currentBlock, Facing.DOWN)); + this.isHittingBlock = false; + this.curBlockDamageMP = 0.0F; + this.gm.world.sendBlockBreakProgress(this.gm.player.getId(), this.currentBlock, -1); + } + } - public boolean onPlayerDamageBlock(BlockPos posBlock, Facing directionFacing) - { - if(this.interacting) - return false; - this.syncCurrentPlayItem(); + public void resetInteraction() { + this.interacting = false; + } - if (this.blockHitDelay > 0) - { - --this.blockHitDelay; - return true; - } - else if (this.isHittingPosition(posBlock)) - { - Block block = this.gm.world.getState(posBlock).getBlock(); + public boolean onPlayerDamageBlock(BlockPos posBlock, Facing directionFacing) { + if(this.interacting) + return false; + this.syncCurrentPlayItem(); - if (block == Blocks.air) - { - this.isHittingBlock = false; - return false; - } - else - { - this.curBlockDamageMP += block.getPlayerRelativeBlockHardness(this.gm.player, this.gm.player.worldObj, posBlock); + if(this.blockHitDelay > 0) { + --this.blockHitDelay; + return true; + } + else if(this.isHittingPosition(posBlock)) { + Block block = this.gm.world.getState(posBlock).getBlock(); - if (this.stepSoundTickCounter % 4.0F == 0.0F && block.sound.getStepSound() != null) - { - this.gm.getSoundManager().playSound(new PositionedSound(block.sound.getStepSound(), 0.25F, /* block.sound.getFrequency() * 0.5F, */ (float)posBlock.getX() + 0.5F, (float)posBlock.getY() + 0.5F, (float)posBlock.getZ() + 0.5F)); - } + if(block == Blocks.air) { + this.isHittingBlock = false; + return false; + } + else { + this.curBlockDamageMP += block.getPlayerRelativeBlockHardness(this.gm.player, this.gm.player.worldObj, posBlock); - ++this.stepSoundTickCounter; + if(this.stepSoundTickCounter % 4.0F == 0.0F && block.sound.getStepSound() != null) { + this.gm.getSoundManager() + .playSound(new PositionedSound(block.sound.getStepSound(), 0.25F, + /* block.sound.getFrequency() * 0.5F, */ (float)posBlock.getX() + 0.5F, (float)posBlock.getY() + 0.5F, + (float)posBlock.getZ() + 0.5F)); + } - if (this.curBlockDamageMP >= 1.0F) - { - this.isHittingBlock = false; - this.netClientHandler.addToSendQueue(new CPacketBreak(CPacketBreak.Action.STOP_DESTROY_BLOCK, posBlock, directionFacing)); - this.onPlayerDestroyBlock(posBlock, directionFacing); - this.curBlockDamageMP = 0.0F; - this.stepSoundTickCounter = 0.0F; - this.blockHitDelay = 5; - } + ++this.stepSoundTickCounter; - this.gm.world.sendBlockBreakProgress(this.gm.player.getId(), this.currentBlock, (int)(this.curBlockDamageMP * 10.0F) - 1); - return true; - } - } - else - { - return this.clickBlock(posBlock, directionFacing); - } - } + if(this.curBlockDamageMP >= 1.0F) { + this.isHittingBlock = false; + this.netClientHandler.addToSendQueue(new CPacketBreak(CPacketBreak.Action.STOP_DESTROY_BLOCK, posBlock, directionFacing)); + this.onPlayerDestroyBlock(posBlock, directionFacing); + this.curBlockDamageMP = 0.0F; + this.stepSoundTickCounter = 0.0F; + this.blockHitDelay = 5; + } - public void updateController() - { - this.syncCurrentPlayItem(); + this.gm.world.sendBlockBreakProgress(this.gm.player.getId(), this.currentBlock, (int)(this.curBlockDamageMP * 10.0F) - 1); + return true; + } + } + else { + return this.clickBlock(posBlock, directionFacing); + } + } - if (this.netClientHandler.getNetworkManager().isChannelOpen()) - { - this.netClientHandler.getNetworkManager().processReceivedPackets(); - } - else - { - this.netClientHandler.getNetworkManager().checkDisconnected(); - } - } + public void updateController() { + this.syncCurrentPlayItem(); - private boolean isHittingPosition(BlockPos pos) - { - ItemStack itemstack = this.gm.player.getHeldItem(); - boolean flag = this.currentItemHittingBlock == null && itemstack == null; + if(this.netClientHandler.getNetworkManager().isChannelOpen()) { + this.netClientHandler.getNetworkManager().processReceivedPackets(); + } + else { + this.netClientHandler.getNetworkManager().checkDisconnected(); + } + } - if (this.currentItemHittingBlock != null && itemstack != null) - { - flag = itemstack.getItem() == this.currentItemHittingBlock.getItem() && ItemStack.areItemStackTagsEqual(itemstack, this.currentItemHittingBlock) && (itemstack.isItemStackDamageable() || itemstack.getMetadata() == this.currentItemHittingBlock.getMetadata()); - } + private boolean isHittingPosition(BlockPos pos) { + ItemStack itemstack = this.gm.player.getHeldItem(); + boolean flag = this.currentItemHittingBlock == null && itemstack == null; - return pos.equals(this.currentBlock) && flag; - } + if(this.currentItemHittingBlock != null && itemstack != null) { + flag = itemstack.getItem() == this.currentItemHittingBlock.getItem() + && ItemStack.areItemStackTagsEqual(itemstack, this.currentItemHittingBlock) + && (itemstack.isItemStackDamageable() || itemstack.getMetadata() == this.currentItemHittingBlock.getMetadata()); + } - private void syncCurrentPlayItem() - { - int i = this.gm.player.inventory.currentItem; + return pos.equals(this.currentBlock) && flag; + } - if (i != this.currentPlayerItem) - { - this.currentPlayerItem = i; - this.netClientHandler.addToSendQueue(new CPacketAction(CPacketAction.Action.SET_ITEMSLOT, this.currentPlayerItem)); - } - } + private void syncCurrentPlayItem() { + int i = this.gm.player.inventory.currentItem; - public boolean onPlayerRightClick(EntityNPC player, WorldClient worldIn, ItemStack heldStack, BlockPos hitPos, Facing side, Vec3 hitVec) - { - this.syncCurrentPlayItem(); - float f = (float)(hitVec.xCoord - (double)hitPos.getX()); - float f1 = (float)(hitVec.yCoord - (double)hitPos.getY()); - float f2 = (float)(hitVec.zCoord - (double)hitPos.getZ()); - boolean flag = false; + if(i != this.currentPlayerItem) { + this.currentPlayerItem = i; + this.netClientHandler.addToSendQueue(new CPacketAction(CPacketAction.Action.SET_ITEMSLOT, this.currentPlayerItem)); + } + } - if (!World.isValidXZ(hitPos)) - { - return false; - } - else - { - if(heldStack == null || !heldStack.getItem().onAction(heldStack, player, worldIn, ItemControl.SECONDARY, hitPos)) { - State iblockstate = worldIn.getState(hitPos); + public boolean onPlayerRightClick(EntityNPC player, WorldClient worldIn, ItemStack heldStack, BlockPos hitPos, Facing side, Vec3 hitVec) { + this.syncCurrentPlayItem(); + float f = (float)(hitVec.xCoord - (double)hitPos.getX()); + float f1 = (float)(hitVec.yCoord - (double)hitPos.getY()); + float f2 = (float)(hitVec.zCoord - (double)hitPos.getZ()); + boolean flag = false; - if ((!player.isSneaking() || player.getHeldItem() == null) - && iblockstate.getBlock().onBlockActivated(worldIn, hitPos, iblockstate, player, side, f, f1, f2)) - { - flag = true; - } + if(!World.isValidXZ(hitPos)) { + return false; + } + else { + if(heldStack == null || !heldStack.getItem().onAction(heldStack, player, worldIn, ItemControl.SECONDARY, hitPos)) { + State iblockstate = worldIn.getState(hitPos); - if (!flag && heldStack != null && heldStack.getItem() instanceof ItemBlock) - { - ItemBlock itemblock = (ItemBlock)heldStack.getItem(); + if((!player.isSneaking() || player.getHeldItem() == null) + && iblockstate.getBlock().onBlockActivated(worldIn, hitPos, iblockstate, player, side, f, f1, f2)) { + flag = true; + } - if (!itemblock.canPlaceBlockOnSide(worldIn, hitPos, side, player, heldStack)) - { - return false; - } - } - } - else { - heldStack.getItem().onItemUse(heldStack, player, worldIn, hitPos, side, f, f1, f2); - flag = true; - } + if(!flag && heldStack != null && heldStack.getItem() instanceof ItemBlock) { + ItemBlock itemblock = (ItemBlock)heldStack.getItem(); - this.netClientHandler.addToSendQueue(new CPacketPlace(hitPos, side.getIndex(), player.inventory.getCurrentItem(), f, f1, f2)); + if(!itemblock.canPlaceBlockOnSide(worldIn, hitPos, side, player, heldStack)) { + return false; + } + } + } + else { + heldStack.getItem().onItemUse(heldStack, player, worldIn, hitPos, side, f, f1, f2); + flag = true; + } - if (!flag) - { - if (heldStack == null) - { - return false; - } - else - { - return heldStack.onItemUse(player, worldIn, hitPos, side, f, f1, f2); - } - } - else - { - return true; - } - } - } + this.netClientHandler.addToSendQueue(new CPacketPlace(hitPos, side.getIndex(), player.inventory.getCurrentItem(), f, f1, f2)); - public boolean sendUseItem(EntityNPC playerIn, World worldIn, ItemStack itemStackIn) - { - this.syncCurrentPlayItem(); - this.netClientHandler.addToSendQueue(new CPacketPlace(playerIn.inventory.getCurrentItem())); - int i = itemStackIn.stackSize; - ItemStack itemstack = itemStackIn.useItemRightClick(worldIn, playerIn); + if(!flag) { + if(heldStack == null) { + return false; + } + else { + return heldStack.onItemUse(player, worldIn, hitPos, side, f, f1, f2); + } + } + else { + return true; + } + } + } - if (itemstack != itemStackIn || itemstack != null && itemstack.stackSize != i) - { - playerIn.inventory.mainInventory[playerIn.inventory.currentItem] = itemstack; + public boolean sendUseItem(EntityNPC playerIn, World worldIn, ItemStack itemStackIn) { + this.syncCurrentPlayItem(); + this.netClientHandler.addToSendQueue(new CPacketPlace(playerIn.inventory.getCurrentItem())); + int i = itemStackIn.stackSize; + ItemStack itemstack = itemStackIn.useItemRightClick(worldIn, playerIn); - if (itemstack.stackSize == 0) - { - playerIn.inventory.mainInventory[playerIn.inventory.currentItem] = null; - } + if(itemstack != itemStackIn || itemstack != null && itemstack.stackSize != i) { + playerIn.inventory.mainInventory[playerIn.inventory.currentItem] = itemstack; - return true; - } - else - { - return false; - } - } + if(itemstack.stackSize == 0) { + playerIn.inventory.mainInventory[playerIn.inventory.currentItem] = null; + } - public EntityNPC createPlayerEntity(WorldClient worldIn, int type) - { - EntityNPC player = (EntityNPC)EntityRegistry.createEntityByID(type, worldIn); - player.setClientPlayer(this.netClientHandler); - return player; - } + return true; + } + else { + return false; + } + } - public void attackEntity(EntityNPC playerIn, Entity targetEntity) - { - this.syncCurrentPlayItem(); - this.netClientHandler.addToSendQueue(new CPacketAction(CPacketAction.Action.ATTACK, targetEntity.getId())); - playerIn.attackTargetEntityWithCurrentItem(targetEntity); - } + public EntityNPC createPlayerEntity(WorldClient worldIn, int type) { + EntityNPC player = (EntityNPC)EntityRegistry.createEntityByID(type, worldIn); + player.setClientPlayer(this.netClientHandler); + return player; + } - public boolean interactWithEntitySendPacket(EntityNPC playerIn, Entity targetEntity) - { - this.syncCurrentPlayItem(); - this.netClientHandler.addToSendQueue(new CPacketAction(CPacketAction.Action.INTERACT, targetEntity.getId())); - return playerIn.interactWith(targetEntity); - } + public void attackEntity(EntityNPC playerIn, Entity targetEntity) { + this.syncCurrentPlayItem(); + this.netClientHandler.addToSendQueue(new CPacketAction(CPacketAction.Action.ATTACK, targetEntity.getId())); + playerIn.attackTargetEntityWithCurrentItem(targetEntity); + } - public ItemStack windowClick(int windowId, int slotId, int mouseButtonClicked, int mode, EntityNPC playerIn) - { - short short1 = playerIn.openContainer.getNextTransactionID(playerIn.inventory); - ItemStack itemstack = playerIn.openContainer.slotClick(slotId, mouseButtonClicked, mode, playerIn); - this.netClientHandler.addToSendQueue(new CPacketClick(windowId, slotId, mouseButtonClicked, mode, itemstack, short1)); - return itemstack; - } + public boolean interactWithEntitySendPacket(EntityNPC playerIn, Entity targetEntity) { + this.syncCurrentPlayItem(); + this.netClientHandler.addToSendQueue(new CPacketAction(CPacketAction.Action.INTERACT, targetEntity.getId())); + return playerIn.interactWith(targetEntity); + } - public void sendEnchantPacket(int windowID, int button) - { - this.netClientHandler.addToSendQueue(new CPacketAction(CPacketAction.Action.ENCHANT_ITEM, windowID | (button << 8))); - } + public ItemStack windowClick(int windowId, int slotId, int mouseButtonClicked, int mode, EntityNPC playerIn) { + short short1 = playerIn.openContainer.getNextTransactionID(playerIn.inventory); + ItemStack itemstack = playerIn.openContainer.slotClick(slotId, mouseButtonClicked, mode, playerIn); + this.netClientHandler.addToSendQueue(new CPacketClick(windowId, slotId, mouseButtonClicked, mode, itemstack, short1)); + return itemstack; + } - public void onStoppedUsingItem(EntityNPC playerIn) - { - this.syncCurrentPlayItem(); - this.netClientHandler.addToSendQueue(new CPacketBreak(CPacketBreak.Action.RELEASE_USE_ITEM, BlockPos.ORIGIN, Facing.DOWN)); - playerIn.stopUsingItem(); - } + public void sendEnchantPacket(int windowID, int button) { + this.netClientHandler.addToSendQueue(new CPacketAction(CPacketAction.Action.ENCHANT_ITEM, windowID | (button << 8))); + } - public boolean getIsHittingBlock() - { - return this.isHittingBlock; - } + public void onStoppedUsingItem(EntityNPC playerIn) { + this.syncCurrentPlayItem(); + this.netClientHandler.addToSendQueue(new CPacketBreak(CPacketBreak.Action.RELEASE_USE_ITEM, BlockPos.ORIGIN, Facing.DOWN)); + playerIn.stopUsingItem(); + } + + public boolean getIsHittingBlock() { + return this.isHittingBlock; + } }