format PlayerController

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

View file

@ -23,8 +23,7 @@ import common.util.Vec3;
import common.world.State;
import common.world.World;
public class PlayerController
{
public class PlayerController {
private final Client gm;
private final ClientPlayer netClientHandler;
private BlockPos currentBlock = new BlockPos(-1, -1, -1);
@ -37,29 +36,24 @@ public class PlayerController
private int currentPlayerItem;
private boolean interacting;
public PlayerController(Client gmIn, ClientPlayer netHandler)
{
public PlayerController(Client gmIn, ClientPlayer netHandler) {
this.gm = gmIn;
this.netClientHandler = netHandler;
}
public boolean onPlayerDestroyBlock(BlockPos pos, Facing side)
{
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)
{
if(block1 == Blocks.air) {
return false;
}
else
{
else {
world.playAuxSFX(2001, pos, BlockRegistry.getStateId(iblockstate));
boolean flag = world.setBlockToAir(pos);
if (flag)
{
if(flag) {
block1.onBlockDestroyedByPlayer(world, pos, iblockstate);
}
@ -67,12 +61,10 @@ public class PlayerController
ItemStack itemstack1 = this.gm.player.getCurrentEquippedItem();
if (itemstack1 != null)
{
if(itemstack1 != null) {
itemstack1.onBlockDestroyed(world, block1, pos, this.gm.player);
if (itemstack1.stackSize == 0)
{
if(itemstack1.stackSize == 0) {
this.gm.player.destroyCurrentEquippedItem();
}
}
@ -81,24 +73,19 @@ public class PlayerController
}
}
public boolean clickBlock(BlockPos loc, Facing face)
{
if (!World.isValidXZ(loc))
{
public boolean clickBlock(BlockPos loc, Facing face) {
if(!World.isValidXZ(loc)) {
return false;
}
else
{
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)
{
if(!this.isHittingBlock || !this.isHittingPosition(loc)) {
if(this.isHittingBlock) {
this.netClientHandler.addToSendQueue(new CPacketBreak(CPacketBreak.Action.ABORT_DESTROY_BLOCK, this.currentBlock, face));
}
@ -106,18 +93,15 @@ public class PlayerController
Block block1 = this.gm.world.getState(loc).getBlock();
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);
}
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.blockHitDelay = 3;
}
else
{
else {
this.isHittingBlock = true;
this.currentBlock = loc;
this.currentItemHittingBlock = this.gm.player.getHeldItem();
@ -131,10 +115,8 @@ public class PlayerController
}
}
public void resetBlockRemoving()
{
if (this.isHittingBlock)
{
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;
@ -142,44 +124,39 @@ public class PlayerController
}
}
public void resetInteraction()
{
public void resetInteraction() {
this.interacting = false;
}
public boolean onPlayerDamageBlock(BlockPos posBlock, Facing directionFacing)
{
public boolean onPlayerDamageBlock(BlockPos posBlock, Facing directionFacing) {
if(this.interacting)
return false;
this.syncCurrentPlayItem();
if (this.blockHitDelay > 0)
{
if(this.blockHitDelay > 0) {
--this.blockHitDelay;
return true;
}
else if (this.isHittingPosition(posBlock))
{
else if(this.isHittingPosition(posBlock)) {
Block block = this.gm.world.getState(posBlock).getBlock();
if (block == Blocks.air)
{
if(block == Blocks.air) {
this.isHittingBlock = false;
return false;
}
else
{
else {
this.curBlockDamageMP += block.getPlayerRelativeBlockHardness(this.gm.player, this.gm.player.worldObj, posBlock);
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.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));
}
++this.stepSoundTickCounter;
if (this.curBlockDamageMP >= 1.0F)
{
if(this.curBlockDamageMP >= 1.0F) {
this.isHittingBlock = false;
this.netClientHandler.addToSendQueue(new CPacketBreak(CPacketBreak.Action.STOP_DESTROY_BLOCK, posBlock, directionFacing));
this.onPlayerDestroyBlock(posBlock, directionFacing);
@ -192,79 +169,67 @@ public class PlayerController
return true;
}
}
else
{
else {
return this.clickBlock(posBlock, directionFacing);
}
}
public void updateController()
{
public void updateController() {
this.syncCurrentPlayItem();
if (this.netClientHandler.getNetworkManager().isChannelOpen())
{
if(this.netClientHandler.getNetworkManager().isChannelOpen()) {
this.netClientHandler.getNetworkManager().processReceivedPackets();
}
else
{
else {
this.netClientHandler.getNetworkManager().checkDisconnected();
}
}
private boolean isHittingPosition(BlockPos pos)
{
private boolean isHittingPosition(BlockPos pos) {
ItemStack itemstack = this.gm.player.getHeldItem();
boolean flag = this.currentItemHittingBlock == null && itemstack == null;
if (this.currentItemHittingBlock != null && itemstack != null)
{
flag = itemstack.getItem() == this.currentItemHittingBlock.getItem() && ItemStack.areItemStackTagsEqual(itemstack, this.currentItemHittingBlock) && (itemstack.isItemStackDamageable() || itemstack.getMetadata() == this.currentItemHittingBlock.getMetadata());
if(this.currentItemHittingBlock != null && itemstack != null) {
flag = itemstack.getItem() == this.currentItemHittingBlock.getItem()
&& ItemStack.areItemStackTagsEqual(itemstack, this.currentItemHittingBlock)
&& (itemstack.isItemStackDamageable() || itemstack.getMetadata() == this.currentItemHittingBlock.getMetadata());
}
return pos.equals(this.currentBlock) && flag;
}
private void syncCurrentPlayItem()
{
private void syncCurrentPlayItem() {
int i = this.gm.player.inventory.currentItem;
if (i != this.currentPlayerItem)
{
if(i != this.currentPlayerItem) {
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)
{
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 (!World.isValidXZ(hitPos))
{
if(!World.isValidXZ(hitPos)) {
return false;
}
else
{
else {
if(heldStack == null || !heldStack.getItem().onAction(heldStack, player, worldIn, ItemControl.SECONDARY, hitPos)) {
State iblockstate = worldIn.getState(hitPos);
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;
}
if (!flag && heldStack != null && heldStack.getItem() instanceof ItemBlock)
{
if(!flag && heldStack != null && heldStack.getItem() instanceof ItemBlock) {
ItemBlock itemblock = (ItemBlock)heldStack.getItem();
if (!itemblock.canPlaceBlockOnSide(worldIn, hitPos, side, player, heldStack))
{
if(!itemblock.canPlaceBlockOnSide(worldIn, hitPos, side, player, heldStack)) {
return false;
}
}
@ -276,91 +241,76 @@ public class PlayerController
this.netClientHandler.addToSendQueue(new CPacketPlace(hitPos, side.getIndex(), player.inventory.getCurrentItem(), f, f1, f2));
if (!flag)
{
if (heldStack == null)
{
if(!flag) {
if(heldStack == null) {
return false;
}
else
{
else {
return heldStack.onItemUse(player, worldIn, hitPos, side, f, f1, f2);
}
}
else
{
else {
return true;
}
}
}
public boolean sendUseItem(EntityNPC playerIn, World worldIn, ItemStack itemStackIn)
{
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 != itemStackIn || itemstack != null && itemstack.stackSize != i)
{
if(itemstack != itemStackIn || itemstack != null && itemstack.stackSize != i) {
playerIn.inventory.mainInventory[playerIn.inventory.currentItem] = itemstack;
if (itemstack.stackSize == 0)
{
if(itemstack.stackSize == 0) {
playerIn.inventory.mainInventory[playerIn.inventory.currentItem] = null;
}
return true;
}
else
{
else {
return false;
}
}
public EntityNPC createPlayerEntity(WorldClient worldIn, int type)
{
public EntityNPC createPlayerEntity(WorldClient worldIn, int type) {
EntityNPC player = (EntityNPC)EntityRegistry.createEntityByID(type, worldIn);
player.setClientPlayer(this.netClientHandler);
return player;
}
public void attackEntity(EntityNPC playerIn, Entity targetEntity)
{
public void attackEntity(EntityNPC playerIn, Entity targetEntity) {
this.syncCurrentPlayItem();
this.netClientHandler.addToSendQueue(new CPacketAction(CPacketAction.Action.ATTACK, targetEntity.getId()));
playerIn.attackTargetEntityWithCurrentItem(targetEntity);
}
public boolean interactWithEntitySendPacket(EntityNPC playerIn, Entity targetEntity)
{
public boolean interactWithEntitySendPacket(EntityNPC playerIn, Entity targetEntity) {
this.syncCurrentPlayItem();
this.netClientHandler.addToSendQueue(new CPacketAction(CPacketAction.Action.INTERACT, targetEntity.getId()));
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);
ItemStack itemstack = playerIn.openContainer.slotClick(slotId, mouseButtonClicked, mode, playerIn);
this.netClientHandler.addToSendQueue(new CPacketClick(windowId, slotId, mouseButtonClicked, mode, itemstack, short1));
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)));
}
public void onStoppedUsingItem(EntityNPC playerIn)
{
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()
{
public boolean getIsHittingBlock() {
return this.isHittingBlock;
}
}