| 1 | package net.minecraft.client.multiplayer;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.google.common.primitives.Shorts;
|
| 5 | import com.google.common.primitives.SignedBytes;
|
| 6 | import com.mojang.logging.LogUtils;
|
| 7 | import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
| 8 | import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
| 9 | import java.util.List;
|
| 10 | import java.util.Objects;
|
| 11 | import java.util.OptionalInt;
|
| 12 | import net.fabricmc.api.EnvType;
|
| 13 | import net.fabricmc.api.Environment;
|
| 14 | import net.minecraft.SharedConstants;
|
| 15 | import net.minecraft.client.ClientRecipeBook;
|
| 16 | import net.minecraft.client.Minecraft;
|
| 17 | import net.minecraft.client.gui.screens.WorldOptionsScreen;
|
| 18 | import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
|
| 19 | import net.minecraft.client.gui.screens.inventory.CreativeModeInventoryScreen;
|
| 20 | import net.minecraft.client.multiplayer.prediction.BlockStatePredictionHandler;
|
| 21 | import net.minecraft.client.multiplayer.prediction.PredictiveAction;
|
| 22 | import net.minecraft.client.player.ItemActivation;
|
| 23 | import net.minecraft.client.player.LocalPlayer;
|
| 24 | import net.minecraft.core.BlockPos;
|
| 25 | import net.minecraft.core.Direction;
|
| 26 | import net.minecraft.core.NonNullList;
|
| 27 | import net.minecraft.network.HashedStack;
|
| 28 | import net.minecraft.network.protocol.Packet;
|
| 29 | import net.minecraft.network.protocol.game.ServerGamePacketListener;
|
| 30 | import net.minecraft.network.protocol.game.ServerboundAttackPacket;
|
| 31 | import net.minecraft.network.protocol.game.ServerboundContainerButtonClickPacket;
|
| 32 | import net.minecraft.network.protocol.game.ServerboundContainerClickPacket;
|
| 33 | import net.minecraft.network.protocol.game.ServerboundContainerSlotStateChangedPacket;
|
| 34 | import net.minecraft.network.protocol.game.ServerboundInteractPacket;
|
| 35 | import net.minecraft.network.protocol.game.ServerboundPickItemFromBlockPacket;
|
| 36 | import net.minecraft.network.protocol.game.ServerboundPickItemFromEntityPacket;
|
| 37 | import net.minecraft.network.protocol.game.ServerboundPlaceRecipePacket;
|
| 38 | import net.minecraft.network.protocol.game.ServerboundPlayerActionPacket;
|
| 39 | import net.minecraft.network.protocol.game.ServerboundSetCarriedItemPacket;
|
| 40 | import net.minecraft.network.protocol.game.ServerboundSetCreativeModeSlotPacket;
|
| 41 | import net.minecraft.network.protocol.game.ServerboundSpectatorActionPacket;
|
| 42 | import net.minecraft.network.protocol.game.ServerboundUseItemOnPacket;
|
| 43 | import net.minecraft.network.protocol.game.ServerboundUseItemPacket;
|
| 44 | import net.minecraft.network.protocol.game.ServerboundPlayerActionPacket.Action;
|
| 45 | import net.minecraft.stats.StatsCounter;
|
| 46 | import net.minecraft.util.Mth;
|
| 47 | import net.minecraft.world.InteractionHand;
|
| 48 | import net.minecraft.world.InteractionResult;
|
| 49 | import net.minecraft.world.InteractionResult.Success;
|
| 50 | import net.minecraft.world.InteractionResult.TryEmptyHandInteraction;
|
| 51 | import net.minecraft.world.entity.Entity;
|
| 52 | import net.minecraft.world.entity.HasCustomInventoryScreen;
|
| 53 | import net.minecraft.world.entity.player.Input;
|
| 54 | import net.minecraft.world.entity.player.Player;
|
| 55 | import net.minecraft.world.inventory.AbstractContainerMenu;
|
| 56 | import net.minecraft.world.inventory.ContainerInput;
|
| 57 | import net.minecraft.world.inventory.Slot;
|
| 58 | import net.minecraft.world.item.ItemStack;
|
| 59 | import net.minecraft.world.item.component.PiercingWeapon;
|
| 60 | import net.minecraft.world.item.component.SwingAnimation;
|
| 61 | import net.minecraft.world.item.context.UseOnContext;
|
| 62 | import net.minecraft.world.item.crafting.display.RecipeDisplayId;
|
| 63 | import net.minecraft.world.level.GameType;
|
| 64 | import net.minecraft.world.level.Level;
|
| 65 | import net.minecraft.world.level.block.Block;
|
| 66 | import net.minecraft.world.level.block.GameMasterBlock;
|
| 67 | import net.minecraft.world.level.block.state.BlockState;
|
| 68 | import net.minecraft.world.level.material.FluidState;
|
| 69 | import net.minecraft.world.phys.BlockHitResult;
|
| 70 | import net.minecraft.world.phys.EntityHitResult;
|
| 71 | import net.minecraft.world.phys.Vec3;
|
| 72 | import org.apache.commons.lang3.mutable.MutableObject;
|
| 73 | import org.jspecify.annotations.Nullable;
|
| 74 | import org.slf4j.Logger;
|
| 75 |
|
| 76 | @Environment(EnvType.CLIENT)
|
| 77 | public class MultiPlayerGameMode {
|
| 78 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 79 | private static final int DEFAULT_DESTROY_COOLDOWN_TICKS = 5;
|
| 80 | private final Minecraft minecraft;
|
| 81 | private final ClientPacketListener connection;
|
| 82 | private BlockPos destroyBlockPos = new BlockPos(-1, -1, -1);
|
| 83 | private ItemStack destroyingItem = ItemStack.EMPTY;
|
| 84 | private float destroyProgress;
|
| 85 | private float destroyTicks;
|
| 86 | private int destroyDelay;
|
| 87 | private @Nullable Direction destroyDirection;
|
| 88 | private boolean isDestroying;
|
| 89 | private GameType localPlayerMode = GameType.DEFAULT_MODE;
|
| 90 | private @Nullable GameType previousLocalPlayerMode;
|
| 91 | private int carriedIndex;
|
| 92 |
|
| 93 | public MultiPlayerGameMode(final Minecraft minecraft, final ClientPacketListener connection) {
|
| 94 | this.minecraft = minecraft;
|
| 95 | this.connection = connection;
|
| 96 | }
|
| 97 |
|
| 98 | public void adjustPlayer(final Player player) {
|
| 99 | this.localPlayerMode.updatePlayerAbilities(player.getAbilities());
|
| 100 | }
|
| 101 |
|
| 102 | public void setLocalMode(final GameType mode, final @Nullable GameType previousMode) {
|
| 103 | this.localPlayerMode = mode;
|
| 104 | this.previousLocalPlayerMode = previousMode;
|
| 105 | this.localPlayerMode.updatePlayerAbilities(this.minecraft.player.getAbilities());
|
| 106 | }
|
| 107 |
|
| 108 | public void setLocalMode(final GameType mode) {
|
| 109 | if (mode != this.localPlayerMode) {
|
| 110 | this.previousLocalPlayerMode = this.localPlayerMode;
|
| 111 | }
|
| 112 |
|
| 113 | this.localPlayerMode = mode;
|
| 114 | if (this.minecraft.gui.screen() instanceof WorldOptionsScreen worldOptionsScreen) {
|
| 115 | worldOptionsScreen.updatePersonalGameModeButton(mode);
|
| 116 | }
|
| 117 |
|
| 118 | this.localPlayerMode.updatePlayerAbilities(this.minecraft.player.getAbilities());
|
| 119 | }
|
| 120 |
|
| 121 | public boolean canHurtPlayer() {
|
| 122 | return this.localPlayerMode.isSurvival();
|
| 123 | }
|
| 124 |
|
| 125 | public boolean destroyBlock(final BlockPos pos) {
|
| 126 | if (this.minecraft.player.blockActionRestricted(this.minecraft.level, pos, this.localPlayerMode)) {
|
| 127 | return false;
|
| 128 | }
|
| 129 |
|
| 130 | Level level = this.minecraft.level;
|
| 131 | BlockState oldState = level.getBlockState(pos);
|
| 132 | if (!this.minecraft.player.getMainHandItem().canDestroyBlock(oldState, level, pos, this.minecraft.player)) {
|
| 133 | return false;
|
| 134 | }
|
| 135 |
|
| 136 | Block oldBlock = oldState.getBlock();
|
| 137 | if (oldBlock instanceof GameMasterBlock && !this.minecraft.player.canUseGameMasterBlocks()) {
|
| 138 | return false;
|
| 139 | }
|
| 140 |
|
| 141 | if (oldState.isAir()) {
|
| 142 | return false;
|
| 143 | }
|
| 144 |
|
| 145 | oldBlock.playerWillDestroy(level, pos, oldState, this.minecraft.player);
|
| 146 | FluidState fluidState = level.getFluidState(pos);
|
| 147 | boolean changed = level.setBlock(pos, fluidState.createLegacyBlock(), 11);
|
| 148 | if (changed) {
|
| 149 | oldBlock.destroy(level, pos, oldState);
|
| 150 | }
|
| 151 |
|
| 152 | if (SharedConstants.DEBUG_BLOCK_BREAK) {
|
| 153 | LOGGER.error("client broke {} {} -> {}", pos, oldState, level.getBlockState(pos));
|
| 154 | }
|
| 155 |
|
| 156 | return changed;
|
| 157 | }
|
| 158 |
|
| 159 | public boolean startDestroyBlock(final BlockPos pos, final Direction direction) {
|
| 160 | if (this.minecraft.player.blockActionRestricted(this.minecraft.level, pos, this.localPlayerMode)) {
|
| 161 | return false;
|
| 162 | }
|
| 163 |
|
| 164 | if (!this.minecraft.level.getWorldBorder().isWithinBounds(pos)) {
|
| 165 | return false;
|
| 166 | }
|
| 167 |
|
| 168 | if (this.minecraft.player.getAbilities().instabuild) {
|
| 169 | BlockState state = this.minecraft.level.getBlockState(pos);
|
| 170 | this.minecraft.getTutorial().onDestroyBlock(this.minecraft.level, pos, state, 1.0F);
|
| 171 | if (SharedConstants.DEBUG_BLOCK_BREAK) {
|
| 172 | LOGGER.info("Creative start {} {}", pos, state);
|
| 173 | }
|
| 174 |
|
| 175 | this.startPrediction(this.minecraft.level, sequence -> {
|
| 176 | this.destroyBlock(pos);
|
| 177 | return new ServerboundPlayerActionPacket(Action.START_DESTROY_BLOCK, pos, direction, sequence);
|
| 178 | });
|
| 179 | this.destroyDelay = 5;
|
| 180 | } else if (!this.isDestroying || !this.sameDestroyTarget(pos)) {
|
| 181 | if (this.isDestroying) {
|
| 182 | if (SharedConstants.DEBUG_BLOCK_BREAK) {
|
| 183 | LOGGER.info("Abort old break {} {}", pos, this.minecraft.level.getBlockState(pos));
|
| 184 | }
|
| 185 |
|
| 186 | this.connection.send(new ServerboundPlayerActionPacket(Action.ABORT_DESTROY_BLOCK, this.destroyBlockPos, direction));
|
| 187 | }
|
| 188 |
|
| 189 | BlockState state = this.minecraft.level.getBlockState(pos);
|
| 190 | this.minecraft.getTutorial().onDestroyBlock(this.minecraft.level, pos, state, 0.0F);
|
| 191 | if (SharedConstants.DEBUG_BLOCK_BREAK) {
|
| 192 | LOGGER.info("Start break {} {}", pos, state);
|
| 193 | }
|
| 194 |
|
| 195 | this.startPrediction(this.minecraft.level, sequence -> {
|
| 196 | boolean notAir = !state.isAir();
|
| 197 | if (notAir && this.destroyProgress == 0.0F) {
|
| 198 | state.attack(this.minecraft.level, pos, this.minecraft.player);
|
| 199 | }
|
| 200 |
|
| 201 | if (notAir && state.getDestroyProgress(this.minecraft.player, this.minecraft.player.level(), pos) >= 1.0F) {
|
| 202 | this.destroyBlock(pos);
|
| 203 | } else {
|
| 204 | this.isDestroying = true;
|
| 205 | this.destroyBlockPos = pos;
|
| 206 | this.destroyDirection = direction;
|
| 207 | this.destroyingItem = this.minecraft.player.getMainHandItem();
|
| 208 | this.destroyProgress = 0.0F;
|
| 209 | this.destroyTicks = 0.0F;
|
| 210 | this.minecraft.level.destroyBlockProgress(this.minecraft.player.getId(), this.destroyBlockPos, this.getDestroyStage());
|
| 211 | this.minecraft.level.addBreakingBlockEffects(this.destroyBlockPos, this.destroyDirection, true);
|
| 212 | }
|
| 213 |
|
| 214 | return new ServerboundPlayerActionPacket(Action.START_DESTROY_BLOCK, pos, direction, sequence);
|
| 215 | });
|
| 216 | }
|
| 217 |
|
| 218 | return true;
|
| 219 | }
|
| 220 |
|
| 221 | public void stopDestroyBlock() {
|
| 222 | if (this.isDestroying) {
|
| 223 | BlockState state = this.minecraft.level.getBlockState(this.destroyBlockPos);
|
| 224 | this.minecraft.getTutorial().onDestroyBlock(this.minecraft.level, this.destroyBlockPos, state, -1.0F);
|
| 225 | if (SharedConstants.DEBUG_BLOCK_BREAK) {
|
| 226 | LOGGER.info("Stop dest {} {}", this.destroyBlockPos, state);
|
| 227 | }
|
| 228 |
|
| 229 | this.connection.send(new ServerboundPlayerActionPacket(Action.ABORT_DESTROY_BLOCK, this.destroyBlockPos, Direction.DOWN));
|
| 230 | this.isDestroying = false;
|
| 231 | this.destroyProgress = 0.0F;
|
| 232 | this.minecraft.level.destroyBlockProgress(this.minecraft.player.getId(), this.destroyBlockPos, -1);
|
| 233 | this.minecraft.player.resetAttackStrengthTicker();
|
| 234 | }
|
| 235 | }
|
| 236 |
|
| 237 | public boolean continueDestroyBlock(final BlockPos pos, final Direction direction) {
|
| 238 | this.ensureHasSentCarriedItem();
|
| 239 | if (this.destroyDelay > 0) {
|
| 240 | this.destroyDelay--;
|
| 241 | return true;
|
| 242 | }
|
| 243 |
|
| 244 | if (this.minecraft.player.getAbilities().instabuild && this.minecraft.level.getWorldBorder().isWithinBounds(pos)) {
|
| 245 | this.destroyDelay = 5;
|
| 246 | BlockState state = this.minecraft.level.getBlockState(pos);
|
| 247 | this.minecraft.getTutorial().onDestroyBlock(this.minecraft.level, pos, state, 1.0F);
|
| 248 | if (SharedConstants.DEBUG_BLOCK_BREAK) {
|
| 249 | LOGGER.info("Creative cont {} {}", pos, state);
|
| 250 | }
|
| 251 |
|
| 252 | this.startPrediction(this.minecraft.level, sequence -> {
|
| 253 | this.destroyBlock(pos);
|
| 254 | return new ServerboundPlayerActionPacket(Action.START_DESTROY_BLOCK, pos, direction, sequence);
|
| 255 | });
|
| 256 | return true;
|
| 257 | } else if (this.sameDestroyTarget(pos)) {
|
| 258 | BlockState state = this.minecraft.level.getBlockState(pos);
|
| 259 | if (state.isAir()) {
|
| 260 | this.isDestroying = false;
|
| 261 | return false;
|
| 262 | }
|
| 263 |
|
| 264 | this.destroyProgress = this.destroyProgress + state.getDestroyProgress(this.minecraft.player, this.minecraft.player.level(), pos);
|
| 265 | this.destroyTicks++;
|
| 266 | this.minecraft.getTutorial().onDestroyBlock(this.minecraft.level, pos, state, Mth.clamp(this.destroyProgress, 0.0F, 1.0F));
|
| 267 | if (this.destroyProgress >= 1.0F) {
|
| 268 | this.isDestroying = false;
|
| 269 | if (SharedConstants.DEBUG_BLOCK_BREAK) {
|
| 270 | LOGGER.info("Finished breaking {} {}", pos, state);
|
| 271 | }
|
| 272 |
|
| 273 | this.startPrediction(this.minecraft.level, sequence -> {
|
| 274 | this.destroyBlock(pos);
|
| 275 | return new ServerboundPlayerActionPacket(Action.STOP_DESTROY_BLOCK, pos, direction, sequence);
|
| 276 | });
|
| 277 | this.destroyDirection = null;
|
| 278 | this.destroyProgress = 0.0F;
|
| 279 | this.destroyTicks = 0.0F;
|
| 280 | this.destroyDelay = 5;
|
| 281 | } else if (this.destroyDirection != direction) {
|
| 282 | this.destroyDirection = direction;
|
| 283 | this.connection.send(new ServerboundPlayerActionPacket(Action.CHANGE_DESTROY_DIRECTION, pos, direction));
|
| 284 | }
|
| 285 |
|
| 286 | this.minecraft.level.destroyBlockProgress(this.minecraft.player.getId(), this.destroyBlockPos, this.getDestroyStage());
|
| 287 | return true;
|
| 288 | } else {
|
| 289 | return this.startDestroyBlock(pos, direction);
|
| 290 | }
|
| 291 | }
|
| 292 |
|
| 293 | private void startPrediction(final ClientLevel level, final PredictiveAction predictiveAction) {
|
| 294 | try (BlockStatePredictionHandler prediction = level.getBlockStatePredictionHandler().startPredicting()) {
|
| 295 | int sequence = prediction.currentSequence();
|
| 296 | Packet<ServerGamePacketListener> packetConcludingPrediction = predictiveAction.predict(sequence);
|
| 297 | this.connection.send(packetConcludingPrediction);
|
| 298 | }
|
| 299 | }
|
| 300 |
|
| 301 | public void tick() {
|
| 302 | this.ensureHasSentCarriedItem();
|
| 303 | if (this.connection.getConnection().isConnected()) {
|
| 304 | this.connection.getConnection().tick();
|
| 305 | } else {
|
| 306 | this.connection.getConnection().handleDisconnection();
|
| 307 | }
|
| 308 | }
|
| 309 |
|
| 310 | private boolean sameDestroyTarget(final BlockPos pos) {
|
| 311 | ItemStack selected = this.minecraft.player.getMainHandItem();
|
| 312 | return pos.equals(this.destroyBlockPos) && ItemStack.isSameItemSameComponents(selected, this.destroyingItem);
|
| 313 | }
|
| 314 |
|
| 315 | private void ensureHasSentCarriedItem() {
|
| 316 | int index = this.minecraft.player.getInventory().getSelectedSlot();
|
| 317 | if (index != this.carriedIndex) {
|
| 318 | this.carriedIndex = index;
|
| 319 | this.connection.send(new ServerboundSetCarriedItemPacket(this.carriedIndex));
|
| 320 | }
|
| 321 | }
|
| 322 |
|
| 323 | public InteractionResult useItemOn(final LocalPlayer player, final InteractionHand hand, final BlockHitResult blockHit) {
|
| 324 | this.ensureHasSentCarriedItem();
|
| 325 | if (!this.minecraft.level.getWorldBorder().isWithinBounds(blockHit.getBlockPos())) {
|
| 326 | return InteractionResult.FAIL;
|
| 327 | }
|
| 328 |
|
| 329 | MutableObject<InteractionResult> result = new MutableObject<>();
|
| 330 | this.startPrediction(this.minecraft.level, sequence -> {
|
| 331 | result.setValue(this.performUseItemOn(player, hand, blockHit));
|
| 332 | return new ServerboundUseItemOnPacket(hand, blockHit, sequence);
|
| 333 | });
|
| 334 | return result.get();
|
| 335 | }
|
| 336 |
|
| 337 | private InteractionResult performUseItemOn(final LocalPlayer player, final InteractionHand hand, final BlockHitResult blockHit) {
|
| 338 | BlockPos pos = blockHit.getBlockPos();
|
| 339 | ItemStack itemStack = player.getItemInHand(hand);
|
| 340 | if (this.localPlayerMode == GameType.SPECTATOR) {
|
| 341 | return InteractionResult.CONSUME;
|
| 342 | }
|
| 343 |
|
| 344 | boolean haveSomethingInOurHands = !player.getMainHandItem().isEmpty() || !player.getOffhandItem().isEmpty();
|
| 345 | boolean suppressUsingBlock = player.isSecondaryUseActive() && haveSomethingInOurHands;
|
| 346 | if (!suppressUsingBlock) {
|
| 347 | BlockState blockState = this.minecraft.level.getBlockState(pos);
|
| 348 | if (!this.connection.isFeatureEnabled(blockState.getBlock().requiredFeatures())) {
|
| 349 | return InteractionResult.FAIL;
|
| 350 | }
|
| 351 |
|
| 352 | InteractionResult itemUse = blockState.useItemOn(player.getItemInHand(hand), this.minecraft.level, player, hand, blockHit);
|
| 353 | if (itemUse.consumesAction()) {
|
| 354 | return itemUse;
|
| 355 | }
|
| 356 |
|
| 357 | if (itemUse instanceof TryEmptyHandInteraction && hand == InteractionHand.MAIN_HAND) {
|
| 358 | InteractionResult use = blockState.useWithoutItem(this.minecraft.level, player, blockHit);
|
| 359 | if (use.consumesAction()) {
|
| 360 | return use;
|
| 361 | }
|
| 362 | }
|
| 363 | }
|
| 364 |
|
| 365 | if (!itemStack.isEmpty() && !player.getCooldowns().isOnCooldown(itemStack)) {
|
| 366 | UseOnContext context = new UseOnContext(player, hand, blockHit);
|
| 367 | InteractionResult result;
|
| 368 | if (player.hasInfiniteMaterials()) {
|
| 369 | int count = itemStack.getCount();
|
| 370 | result = itemStack.useOn(context);
|
| 371 | itemStack.setCount(count);
|
| 372 | } else {
|
| 373 | result = itemStack.useOn(context);
|
| 374 | if (result instanceof Success success) {
|
| 375 | ItemStack resultItemStack = Objects.requireNonNullElseGet(success.heldItemTransformedTo(), () -> player.getItemInHand(hand));
|
| 376 | if (resultItemStack != itemStack) {
|
| 377 | player.setItemInHand(hand, resultItemStack);
|
| 378 | }
|
| 379 | }
|
| 380 | }
|
| 381 |
|
| 382 | return result;
|
| 383 | } else {
|
| 384 | return InteractionResult.PASS;
|
| 385 | }
|
| 386 | }
|
| 387 |
|
| 388 | public InteractionResult useItem(final Player player, final InteractionHand hand) {
|
| 389 | if (this.localPlayerMode == GameType.SPECTATOR) {
|
| 390 | return InteractionResult.PASS;
|
| 391 | }
|
| 392 |
|
| 393 | this.ensureHasSentCarriedItem();
|
| 394 | MutableObject<InteractionResult> interactionResult = new MutableObject<>();
|
| 395 | this.startPrediction(this.minecraft.level, sequence -> {
|
| 396 | ServerboundUseItemPacket packet = new ServerboundUseItemPacket(hand, sequence, player.getYRot(), player.getXRot());
|
| 397 | ItemStack itemStack = player.getItemInHand(hand);
|
| 398 | if (player.getCooldowns().isOnCooldown(itemStack)) {
|
| 399 | interactionResult.setValue(InteractionResult.PASS);
|
| 400 | return packet;
|
| 401 | }
|
| 402 |
|
| 403 | InteractionResult resultHolder = itemStack.use(this.minecraft.level, player, hand);
|
| 404 | ItemStack result;
|
| 405 | if (resultHolder instanceof Success success) {
|
| 406 | result = Objects.requireNonNullElseGet(success.heldItemTransformedTo(), () -> player.getItemInHand(hand));
|
| 407 | } else {
|
| 408 | result = player.getItemInHand(hand);
|
| 409 | }
|
| 410 |
|
| 411 | if (result != itemStack) {
|
| 412 | player.setItemInHand(hand, result);
|
| 413 | }
|
| 414 |
|
| 415 | interactionResult.setValue(resultHolder);
|
| 416 | return packet;
|
| 417 | });
|
| 418 | return interactionResult.get();
|
| 419 | }
|
| 420 |
|
| 421 | public LocalPlayer createPlayer(final ClientLevel level, final StatsCounter stats, final ClientRecipeBook recipeBook, final ItemActivation itemActivation) {
|
| 422 | return this.createPlayer(level, stats, recipeBook, Input.EMPTY, false, itemActivation);
|
| 423 | }
|
| 424 |
|
| 425 | public LocalPlayer createPlayer(
|
| 426 | final ClientLevel level,
|
| 427 | final StatsCounter stats,
|
| 428 | final ClientRecipeBook recipeBook,
|
| 429 | final Input lastSentInput,
|
| 430 | final boolean wasSprinting,
|
| 431 | final ItemActivation itemActivation
|
| 432 | ) {
|
| 433 | return new LocalPlayer(
|
| 434 | this.minecraft, level, this.connection, stats, recipeBook, lastSentInput, wasSprinting, this.minecraft.computeChatAbilities(), itemActivation
|
| 435 | );
|
| 436 | }
|
| 437 |
|
| 438 | public void attack(final Player player, final Entity entity) {
|
| 439 | this.ensureHasSentCarriedItem();
|
| 440 | this.connection.send(new ServerboundAttackPacket(entity.getId()));
|
| 441 | player.attack(entity);
|
| 442 | player.resetAttackStrengthTicker();
|
| 443 | if (player.getAbilities().instabuild) {
|
| 444 | this.destroyDelay = 5;
|
| 445 | }
|
| 446 | }
|
| 447 |
|
| 448 | public void spectate(final Entity entity) {
|
| 449 | this.connection.send(new ServerboundSpectatorActionPacket(OptionalInt.of(entity.getId())));
|
| 450 | }
|
| 451 |
|
| 452 | public void spectatorNoAction() {
|
| 453 | this.connection.send(new ServerboundSpectatorActionPacket(OptionalInt.empty()));
|
| 454 | }
|
| 455 |
|
| 456 | public InteractionResult interact(final Player player, final Entity entity, final EntityHitResult hitResult, final InteractionHand hand) {
|
| 457 | this.ensureHasSentCarriedItem();
|
| 458 | Vec3 location = hitResult.getLocation().subtract(entity.getX(), entity.getY(), entity.getZ());
|
| 459 | this.connection.send(new ServerboundInteractPacket(entity.getId(), hand, location, player.isShiftKeyDown()));
|
| 460 | return this.localPlayerMode == GameType.SPECTATOR ? InteractionResult.PASS : player.interactOn(entity, hand, location);
|
| 461 | }
|
| 462 |
|
| 463 | public void handleContainerInput(final int containerId, final int slotNum, final int buttonNum, final ContainerInput containerInput, final Player player) {
|
| 464 | AbstractContainerMenu containerMenu = player.containerMenu;
|
| 465 | if (containerId != containerMenu.containerId) {
|
| 466 | LOGGER.warn("Ignoring click in mismatching container. Click in {}, player has {}.", containerId, containerMenu.containerId);
|
| 467 | } else {
|
| 468 | NonNullList<Slot> slots = containerMenu.slots;
|
| 469 | int slotCount = slots.size();
|
| 470 | List<ItemStack> itemsBeforeClick = Lists.newArrayListWithCapacity(slotCount);
|
| 471 |
|
| 472 | for (Slot slot : slots) {
|
| 473 | itemsBeforeClick.add(slot.getItem().copy());
|
| 474 | }
|
| 475 |
|
| 476 | containerMenu.clicked(slotNum, buttonNum, containerInput, player);
|
| 477 | Int2ObjectMap<HashedStack> changedSlots = new Int2ObjectOpenHashMap<>();
|
| 478 |
|
| 479 | for (int i = 0; i < slotCount; i++) {
|
| 480 | ItemStack before = itemsBeforeClick.get(i);
|
| 481 | ItemStack after = slots.get(i).getItem();
|
| 482 | if (!ItemStack.matches(before, after)) {
|
| 483 | changedSlots.put(i, HashedStack.create(after, this.connection.decoratedHashOpsGenenerator()));
|
| 484 | }
|
| 485 | }
|
| 486 |
|
| 487 | HashedStack carriedItem = HashedStack.create(containerMenu.getCarried(), this.connection.decoratedHashOpsGenenerator());
|
| 488 | this.connection
|
| 489 | .send(
|
| 490 | new ServerboundContainerClickPacket(
|
| 491 | containerId, containerMenu.getStateId(), Shorts.checkedCast(slotNum), SignedBytes.checkedCast(buttonNum), containerInput, changedSlots, carriedItem
|
| 492 | )
|
| 493 | );
|
| 494 | }
|
| 495 | }
|
| 496 |
|
| 497 | public void handlePlaceRecipe(final int containerId, final RecipeDisplayId recipe, final boolean useMaxItems) {
|
| 498 | this.connection.send(new ServerboundPlaceRecipePacket(containerId, recipe, useMaxItems));
|
| 499 | }
|
| 500 |
|
| 501 | public void handleInventoryButtonClick(final int containerId, final int buttonId) {
|
| 502 | this.connection.send(new ServerboundContainerButtonClickPacket(containerId, buttonId));
|
| 503 | }
|
| 504 |
|
| 505 | public void handleCreativeModeItemAdd(final ItemStack clicked, final int slot) {
|
| 506 | if (this.minecraft.player.hasInfiniteMaterials() && this.connection.isFeatureEnabled(clicked.getItem().requiredFeatures())) {
|
| 507 | this.connection.send(new ServerboundSetCreativeModeSlotPacket(slot, clicked));
|
| 508 | }
|
| 509 | }
|
| 510 |
|
| 511 | public void handleCreativeModeItemDrop(final ItemStack clicked) {
|
| 512 | boolean hasOtherInventoryOpen = this.minecraft.gui.screen() instanceof AbstractContainerScreen
|
| 513 | && !(this.minecraft.gui.screen() instanceof CreativeModeInventoryScreen);
|
| 514 | if (this.minecraft.player.hasInfiniteMaterials()
|
| 515 | && !hasOtherInventoryOpen
|
| 516 | && !clicked.isEmpty()
|
| 517 | && this.connection.isFeatureEnabled(clicked.getItem().requiredFeatures())) {
|
| 518 | this.connection.send(new ServerboundSetCreativeModeSlotPacket(-1, clicked));
|
| 519 | this.minecraft.player.getDropSpamThrottler().increment();
|
| 520 | }
|
| 521 | }
|
| 522 |
|
| 523 | public void releaseUsingItem(final Player player) {
|
| 524 | this.ensureHasSentCarriedItem();
|
| 525 | this.connection.send(new ServerboundPlayerActionPacket(Action.RELEASE_USE_ITEM, BlockPos.ZERO, Direction.DOWN));
|
| 526 | player.releaseUsingItem();
|
| 527 | }
|
| 528 |
|
| 529 | public void piercingAttack(final SwingAnimation swingAnimation, final PiercingWeapon weapon) {
|
| 530 | this.ensureHasSentCarriedItem();
|
| 531 | this.connection.send(new ServerboundPlayerActionPacket(Action.STAB, BlockPos.ZERO, Direction.DOWN));
|
| 532 | this.minecraft.player.onAttack();
|
| 533 | this.minecraft.player.postPiercingAttack();
|
| 534 | this.minecraft.player.swing(InteractionHand.MAIN_HAND, swingAnimation, false);
|
| 535 | weapon.makeSound(this.minecraft.player);
|
| 536 | }
|
| 537 |
|
| 538 | public boolean hasExperience() {
|
| 539 | return this.localPlayerMode.isSurvival();
|
| 540 | }
|
| 541 |
|
| 542 | public boolean hasMissTime() {
|
| 543 | return !this.localPlayerMode.isCreative();
|
| 544 | }
|
| 545 |
|
| 546 | public boolean isServerControlledInventory() {
|
| 547 | return this.minecraft.player.isPassenger() && this.minecraft.player.getVehicle() instanceof HasCustomInventoryScreen;
|
| 548 | }
|
| 549 |
|
| 550 | public boolean isSpectator() {
|
| 551 | return this.localPlayerMode == GameType.SPECTATOR;
|
| 552 | }
|
| 553 |
|
| 554 | public @Nullable GameType getPreviousPlayerMode() {
|
| 555 | return this.previousLocalPlayerMode;
|
| 556 | }
|
| 557 |
|
| 558 | public GameType getPlayerMode() {
|
| 559 | return this.localPlayerMode;
|
| 560 | }
|
| 561 |
|
| 562 | public boolean isDestroying() {
|
| 563 | return this.isDestroying;
|
| 564 | }
|
| 565 |
|
| 566 | public int getDestroyStage() {
|
| 567 | return this.destroyProgress > 0.0F ? (int)(this.destroyProgress * 10.0F) : -1;
|
| 568 | }
|
| 569 |
|
| 570 | public void handlePickItemFromBlock(final BlockPos pos, final boolean includeData) {
|
| 571 | this.connection.send(new ServerboundPickItemFromBlockPacket(pos, includeData));
|
| 572 | }
|
| 573 |
|
| 574 | public void handlePickItemFromEntity(final Entity entity, final boolean includeData) {
|
| 575 | this.connection.send(new ServerboundPickItemFromEntityPacket(entity.getId(), includeData));
|
| 576 | }
|
| 577 |
|
| 578 | public void handleSlotStateChanged(final int slotId, final int containerId, final boolean newState) {
|
| 579 | this.connection.send(new ServerboundContainerSlotStateChangedPacket(slotId, containerId, newState));
|
| 580 | }
|
| 581 |
|
| 582 | public void dropItem(final LocalPlayer player, final boolean all) {
|
| 583 | Action action = all ? Action.DROP_ALL_ITEMS : Action.DROP_ITEM;
|
| 584 | ItemStack prediction = player.getInventory().removeFromSelected(all);
|
| 585 | this.ensureHasSentCarriedItem();
|
| 586 | this.connection.send(new ServerboundPlayerActionPacket(action, BlockPos.ZERO, Direction.DOWN));
|
| 587 | if (!prediction.isEmpty()) {
|
| 588 | player.swing(InteractionHand.MAIN_HAND, SwingAnimation.DEFAULT, false);
|
| 589 | }
|
| 590 | }
|
| 591 | }
|
| 592 |
|