format PlayerController

This commit is contained in:
Sen 2025-05-25 15:26:14 +02:00
parent 2cf7f0036d
commit 4a84f9e096

View file

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