| 1 | package nicotine.util;
|
| 2 |
|
| 3 | import net.minecraft.client.multiplayer.PlayerInfo;
|
| 4 | import net.minecraft.client.player.AbstractClientPlayer;
|
| 5 | import net.minecraft.client.player.RemotePlayer;
|
| 6 | import net.minecraft.commands.arguments.EntityAnchorArgument;
|
| 7 | import net.minecraft.core.BlockPos;
|
| 8 | import net.minecraft.core.component.DataComponents;
|
| 9 | import net.minecraft.network.protocol.game.ServerboundPlayerCommandPacket;
|
| 10 | import net.minecraft.network.protocol.game.ServerboundPlayerInputPacket;
|
| 11 | import net.minecraft.network.protocol.game.ServerboundPunchPacket;
|
| 12 | import net.minecraft.tags.ItemTags;
|
| 13 | import net.minecraft.util.Mth;
|
| 14 | import net.minecraft.world.InteractionHand;
|
| 15 | import net.minecraft.world.entity.Entity;
|
| 16 | import net.minecraft.world.entity.EquipmentSlot;
|
| 17 | import net.minecraft.world.entity.player.Input;
|
| 18 | import net.minecraft.world.item.Item;
|
| 19 | import net.minecraft.world.item.ItemStack;
|
| 20 | import net.minecraft.world.item.Items;
|
| 21 | import net.minecraft.world.item.component.SwingAnimation;
|
| 22 | import net.minecraft.world.phys.BlockHitResult;
|
| 23 | import net.minecraft.world.phys.Vec2;
|
| 24 | import net.minecraft.world.phys.Vec3;
|
| 25 | import nicotine.events.SendMovementPacketAfterEvent;
|
| 26 | import nicotine.events.SendMovementPacketBeforeEvent;
|
| 27 |
|
| 28 | import java.util.ArrayList;
|
| 29 | import java.util.List;
|
| 30 |
|
| 31 | import static nicotine.util.Common.friendList;
|
| 32 | import static nicotine.util.Common.mc;
|
| 33 |
|
| 34 | public class Player {
|
| 35 | private static boolean packetSneak = true;
|
| 36 |
|
| 37 | private enum ActionType {
|
| 38 | ATTACK, PLACE
|
| 39 | }
|
| 40 |
|
| 41 | private static class Action {
|
| 42 | public ActionType actionType;
|
| 43 |
|
| 44 | public Entity entity;
|
| 45 |
|
| 46 | public BlockHitResult blockHitResult;
|
| 47 |
|
| 48 | public boolean revertRotation;
|
| 49 |
|
| 50 | public int slot;
|
| 51 |
|
| 52 | public boolean sneak;
|
| 53 | public boolean selfCenter;
|
| 54 |
|
| 55 | public boolean delayAction = false;
|
| 56 | public boolean rotated = false;
|
| 57 |
|
| 58 | public Vec3 lookAtPos = null;
|
| 59 |
|
| 60 | public Action(ActionType actionType, Entity entity, boolean revertRotation) {
|
| 61 | this.actionType = actionType;
|
| 62 | this.entity = entity;
|
| 63 | this.revertRotation = revertRotation;
|
| 64 | }
|
| 65 |
|
| 66 | public Action(ActionType actionType, BlockHitResult blockHitResult, boolean revertRotation, int slot, boolean sneak, boolean selfCenter) {
|
| 67 | this.actionType = actionType;
|
| 68 | this.blockHitResult = blockHitResult;
|
| 69 | this.revertRotation = revertRotation;
|
| 70 | this.slot = slot;
|
| 71 | this.sneak = sneak;
|
| 72 | this.selfCenter = selfCenter;
|
| 73 | }
|
| 74 | }
|
| 75 |
|
| 76 | final private static List<Action> actions = new ArrayList<>();
|
| 77 |
|
| 78 | public static void init() {
|
| 79 | EventBus.register(SendMovementPacketBeforeEvent.class, event -> {
|
| 80 | if (mc.gameMode.isDestroying() || mc.player.isUsingItem())
|
| 81 | actions.clear();
|
| 82 |
|
| 83 | if (actions.isEmpty())
|
| 84 | return true;
|
| 85 |
|
| 86 | Action action = actions.getFirst();
|
| 87 |
|
| 88 | if (!action.rotated) {
|
| 89 |
|
| 90 | Vec2 rotation = getRotation(action);
|
| 91 |
|
| 92 | mc.player.setXRot(rotation.x);
|
| 93 | mc.player.setYRot(rotation.y);
|
| 94 |
|
| 95 | action.rotated = true;
|
| 96 | }
|
| 97 |
|
| 98 | if (action.delayAction)
|
| 99 | return true;
|
| 100 |
|
| 101 | if (action.actionType == ActionType.ATTACK) {
|
| 102 | if (!mc.player.isWithinEntityInteractionRange(action.entity, 0)) {
|
| 103 | return true;
|
| 104 | }
|
| 105 |
|
| 106 | attack(action.entity);
|
| 107 | swingHand();
|
| 108 | mc.player.connection.send(ServerboundPunchPacket.INSTANCE);
|
| 109 | } else {
|
| 110 | if (!mc.player.isWithinBlockInteractionRange(action.blockHitResult.getBlockPos(), 0)) {
|
| 111 | return true;
|
| 112 | }
|
| 113 |
|
| 114 | int originalSlot = mc.player.getInventory().getSelectedSlot();
|
| 115 |
|
| 116 | Inventory.selectSlot(action.slot);
|
| 117 |
|
| 118 | if (action.sneak)
|
| 119 | toggleSneak();
|
| 120 |
|
| 121 | place(action.blockHitResult);
|
| 122 | swingHand();
|
| 123 |
|
| 124 | if (action.sneak)
|
| 125 | toggleSneak();
|
| 126 |
|
| 127 | if (originalSlot != action.slot)
|
| 128 | Inventory.selectSlot(originalSlot);
|
| 129 | }
|
| 130 |
|
| 131 | return true;
|
| 132 | });
|
| 133 |
|
| 134 | EventBus.register(SendMovementPacketAfterEvent.class, event -> {
|
| 135 | if (mc.gameMode.isDestroying() || mc.player.isUsingItem())
|
| 136 | actions.clear();
|
| 137 |
|
| 138 | if (actions.isEmpty())
|
| 139 | return true;
|
| 140 |
|
| 141 | Action action = actions.getFirst();
|
| 142 |
|
| 143 | if (action.revertRotation) {
|
| 144 | mc.player.setXRot(mc.player.xRotO);
|
| 145 | mc.player.setYRot(mc.player.yRotO);
|
| 146 | action.revertRotation = false;
|
| 147 | }
|
| 148 |
|
| 149 | if (!action.delayAction) {
|
| 150 | actions.removeFirst();
|
| 151 | }
|
| 152 |
|
| 153 | action.delayAction = false;
|
| 154 |
|
| 155 | return true;
|
| 156 | });
|
| 157 | }
|
| 158 |
|
| 159 | public static Vec2 getRotation(Action action) {
|
| 160 |
|
| 161 | Vec3 target;
|
| 162 |
|
| 163 | if (action.actionType == ActionType.ATTACK) {
|
| 164 | target = action.entity.getBoundingBox().getCenter();
|
| 165 | } else {
|
| 166 | if (action.lookAtPos != null)
|
| 167 | target = action.lookAtPos;
|
| 168 | else
|
| 169 | target = Vec3.atCenterOf(action.blockHitResult.getBlockPos());
|
| 170 |
|
| 171 | if (action.selfCenter) {
|
| 172 | selfCenter();
|
| 173 | }
|
| 174 | }
|
| 175 |
|
| 176 | Vec3 vec3d = EntityAnchorArgument.Anchor.EYES.apply(mc.player);
|
| 177 |
|
| 178 | double d = target.x - vec3d.x;
|
| 179 | double e = target.y - vec3d.y;
|
| 180 | double f = target.z - vec3d.z;
|
| 181 | double g = Math.sqrt(d * d + f * f);
|
| 182 |
|
| 183 | float pitch = Mth.wrapDegrees((float)(-(Mth.atan2(e, g) * 180.0F / (float)Math.PI)));
|
| 184 | float yaw = Mth.wrapDegrees((float)(Mth.atan2(f, d) * 180.0F / (float)Math.PI) - 90.0F);
|
| 185 |
|
| 186 | yaw = Mth.wrapDegrees(yaw - mc.player.getYRot()) + mc.player.getYRot();
|
| 187 |
|
| 188 | Vec2 rotation = new Vec2(pitch, yaw);
|
| 189 |
|
| 190 | return rotation;
|
| 191 | }
|
| 192 |
|
| 193 | public static void lookAndPlace(BlockHitResult blockHitResult, int targetSlot, boolean sneak, boolean center) {
|
| 194 | Action action = new Action(ActionType.PLACE, blockHitResult, true, targetSlot, sneak, center);
|
| 195 | actions.add(action);
|
| 196 | }
|
| 197 |
|
| 198 | public static void lookAndPlace(Vec3 lookAtPos, BlockHitResult blockHitResult, int targetSlot, boolean sneak, boolean center) {
|
| 199 | Action action = new Action(ActionType.PLACE, blockHitResult, true, targetSlot, sneak, center);
|
| 200 | action.lookAtPos = lookAtPos;
|
| 201 | actions.add(action);
|
| 202 | }
|
| 203 |
|
| 204 | public static void lookAndAttack(Entity entity, boolean revertRotation) {
|
| 205 | Action action = new Action(ActionType.ATTACK, entity, revertRotation);
|
| 206 | action.delayAction = true;
|
| 207 | actions.add(action);
|
| 208 | }
|
| 209 |
|
| 210 | public static void lookAndAttack(Entity entity) {
|
| 211 | Action action = new Action(ActionType.ATTACK, entity, true);
|
| 212 | action.delayAction = true;
|
| 213 | actions.add(action);
|
| 214 | }
|
| 215 |
|
| 216 | public static void place(BlockHitResult blockHitResult) {
|
| 217 | mc.gameMode.useItemOn(mc.player, mc.player.getUsedItemHand(), blockHitResult);
|
| 218 | }
|
| 219 |
|
| 220 | public static void attack(Entity entity) {
|
| 221 | ItemStack selectedItem = mc.player.getInventory().getSelectedItem();
|
| 222 |
|
| 223 | if (selectedItem.is(ItemTags.SPEARS))
|
| 224 | mc.gameMode.piercingAttack(mc.player.getMainHandItem().getAttackAnimation(), selectedItem.getComponents().get(DataComponents.PIERCING_WEAPON));
|
| 225 | else
|
| 226 | mc.gameMode.attack(mc.player, entity);
|
| 227 | }
|
| 228 |
|
| 229 | public static void swingHand() {
|
| 230 | mc.player.swing(InteractionHand.MAIN_HAND, mc.player.getItemInHand(InteractionHand.MAIN_HAND).getAttackAnimation(), false);
|
| 231 | }
|
| 232 |
|
| 233 | public static void toggleSneak() {
|
| 234 | Input playerInput = mc.player.input.keyPresses;
|
| 235 | Input newPlayerInput = new Input(playerInput.forward(), playerInput.backward(), playerInput.left(), playerInput.right(), playerInput.jump(), packetSneak, playerInput.sprint());
|
| 236 | ServerboundPlayerInputPacket playerInputC2SPacket = new ServerboundPlayerInputPacket(newPlayerInput);
|
| 237 | mc.getConnection().send(playerInputC2SPacket);
|
| 238 | packetSneak = !packetSneak;
|
| 239 | }
|
| 240 |
|
| 241 | public static void startFlying() {
|
| 242 | mc.getConnection().send(new ServerboundPlayerCommandPacket(mc.player, ServerboundPlayerCommandPacket.Action.START_FALL_FLYING));
|
| 243 | }
|
| 244 |
|
| 245 | public static void elytraSwap() {
|
| 246 | for (int i = 0; i <= 35; i++) {
|
| 247 | ItemStack curStack = mc.player.getInventory().getItem(i);
|
| 248 | Item curItem = curStack.getItem();
|
| 249 | if (curItem.components().has(DataComponents.EQUIPPABLE)) {
|
| 250 | EquipmentSlot equipment = curItem.components().get(DataComponents.EQUIPPABLE).slot();
|
| 251 |
|
| 252 | if (equipment.getType() != EquipmentSlot.Type.HUMANOID_ARMOR)
|
| 253 | continue;
|
| 254 |
|
| 255 | int equipmentSlot = equipment.getIndex() + 1;
|
| 256 | int armorSlot = 35 + equipmentSlot;
|
| 257 |
|
| 258 | ItemStack armorStack = mc.player.getInventory().getItem(armorSlot);
|
| 259 |
|
| 260 | if ((armorStack.getItem() == Items.ELYTRA && curItem != Items.ELYTRA) || (armorStack.getItem() != Items.ELYTRA && curItem == Items.ELYTRA)) {
|
| 261 | Inventory.swap(i < 9 ? 36 + i : i, 9 - equipmentSlot);
|
| 262 | return;
|
| 263 | }
|
| 264 | }
|
| 265 | }
|
| 266 | }
|
| 267 |
|
| 268 | public static void selfCenter() {
|
| 269 | Vec3 centerPos = Vec3.atCenterOf(mc.player.blockPosition());
|
| 270 |
|
| 271 | if (centerPos.x == mc.player.getX() && centerPos.z == mc.player.getZ())
|
| 272 | return;
|
| 273 |
|
| 274 | mc.player.setPos(centerPos.x, mc.player.getY(), centerPos.z);
|
| 275 | }
|
| 276 |
|
| 277 | public static boolean isBusy() {
|
| 278 | return !actions.isEmpty() && !mc.gameMode.isDestroying() && !mc.player.isUsingItem();
|
| 279 | }
|
| 280 |
|
| 281 | public static int getPing(AbstractClientPlayer player) {
|
| 282 | int ping = -1;
|
| 283 |
|
| 284 | for (PlayerInfo playerListEntry : mc.getConnection().getListedOnlinePlayers()) {
|
| 285 | if (playerListEntry.getProfile().name().equals(player.getGameProfile().name())) {
|
| 286 | ping = playerListEntry.getLatency();
|
| 287 | break;
|
| 288 | }
|
| 289 | }
|
| 290 |
|
| 291 | return ping;
|
| 292 | }
|
| 293 |
|
| 294 | public static List<ItemStack> getArmorItems() {
|
| 295 | List<ItemStack> armorItems = new ArrayList<>();
|
| 296 |
|
| 297 | for (int i = 36; i < 40; i++) {
|
| 298 | armorItems.add(mc.player.getInventory().getItem(i));
|
| 299 | }
|
| 300 |
|
| 301 | return armorItems;
|
| 302 | }
|
| 303 |
|
| 304 |
|
| 305 | public static AbstractClientPlayer findNearestPlayer(boolean ignoreFriends) {
|
| 306 | AbstractClientPlayer nearestPlayer = null;
|
| 307 | float nearestDistance = Float.MAX_VALUE;
|
| 308 |
|
| 309 | for (AbstractClientPlayer player : mc.level.players()) {
|
| 310 | if (!(player instanceof RemotePlayer))
|
| 311 | continue;
|
| 312 |
|
| 313 | if (player.isDeadOrDying())
|
| 314 | continue;
|
| 315 |
|
| 316 | if (ignoreFriends && friendList.contains(player.getUUID()))
|
| 317 | continue;
|
| 318 |
|
| 319 | float distance = player.distanceTo(mc.player);
|
| 320 | if (nearestDistance > distance) {
|
| 321 | nearestPlayer = player;
|
| 322 | nearestDistance = distance;
|
| 323 | }
|
| 324 | }
|
| 325 |
|
| 326 | return nearestPlayer;
|
| 327 | }
|
| 328 |
|
| 329 | public static boolean isPositionInRenderDistance(Vec3 position) {
|
| 330 | if (position.distanceTo(mc.player.position()) <= mc.options.renderDistance().get() * 16) {
|
| 331 | return true;
|
| 332 | }
|
| 333 |
|
| 334 | return false;
|
| 335 | }
|
| 336 |
|
| 337 | public static List<BlockPos> getSurroundBlocks(BlockPos initPos) {
|
| 338 | return getSurroundBlocks(initPos, -1);
|
| 339 | }
|
| 340 |
|
| 341 | public static List<BlockPos> getSurroundBlocks(BlockPos initPos, int y) {
|
| 342 | List<BlockPos> surroundBlocks = new ArrayList<>();
|
| 343 |
|
| 344 | surroundBlocks.add(initPos.offset(1, y, 0));
|
| 345 | surroundBlocks.add(initPos.offset(0, y, 1));
|
| 346 | surroundBlocks.add(initPos.offset( -1, y ,0));
|
| 347 | surroundBlocks.add(initPos.offset(0, y, -1));
|
| 348 |
|
| 349 | return surroundBlocks;
|
| 350 | }
|
| 351 | }
|
| 352 |
|