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