|
| 1 | +/* |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2023-2025 Fox2Code |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | +package com.fox2code.foxloader.entity; |
| 25 | + |
| 26 | +import com.fox2code.foxloader.launcher.FoxLauncher; |
| 27 | +import com.mojang.nbt.CompoundTag; |
| 28 | +import net.minecraft.client.Minecraft; |
| 29 | +import net.minecraft.client.player.skin.SkinHelper; |
| 30 | +import net.minecraft.client.renderer.particle.EntityFXPickup; |
| 31 | +import net.minecraft.common.entity.Entity; |
| 32 | +import net.minecraft.common.entity.other.EntityItem; |
| 33 | +import net.minecraft.common.entity.player.EntityPlayer; |
| 34 | +import net.minecraft.common.entity.projectile.EntityThrownArrow; |
| 35 | +import net.minecraft.common.networking.Packet18Animation; |
| 36 | +import net.minecraft.common.networking.Packet22Collect; |
| 37 | +import net.minecraft.common.networking.Packet41UpdateFishingRod; |
| 38 | +import net.minecraft.common.world.World; |
| 39 | +import net.minecraft.server.MinecraftServer; |
| 40 | +import net.minecraft.server.entity.EntityTracker; |
| 41 | + |
| 42 | +/** |
| 43 | + * Fake player to use player specific methods from mods. |
| 44 | + */ |
| 45 | +public abstract class EntityFakePlayer extends EntityPlayer { |
| 46 | + public int gamemode = 0; |
| 47 | + |
| 48 | + public EntityFakePlayer(World world) { |
| 49 | + this(world, null); |
| 50 | + } |
| 51 | + |
| 52 | + public EntityFakePlayer(World world, String username) { |
| 53 | + super(world); |
| 54 | + this.username = username == null ? "[FakePlayer]" : username; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void updateSkin() { |
| 59 | + String skinUsername; |
| 60 | + if (FoxLauncher.isClient() && (skinUsername = this.getSkinUsername()) != null) { |
| 61 | + String originalUsername = this.username; |
| 62 | + this.username = skinUsername; |
| 63 | + SkinHelper.applySkin(this); |
| 64 | + this.username = originalUsername; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void readEntityFromNBT(CompoundTag nbt) { |
| 70 | + super.readEntityFromNBT(nbt); |
| 71 | + this.gamemode = nbt.getInteger("Gamemode"); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void writeEntityToNBT(CompoundTag bt) { |
| 76 | + super.writeEntityToNBT(bt); |
| 77 | + bt.setInteger("Gamemode", this.gamemode); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void onItemPickup(Entity entity, int arg2) { |
| 82 | + if (this.worldObj.isRemote) { |
| 83 | + // Replicate EntityOtherPlayerMP behaviour |
| 84 | + super.onItemPickup(entity, arg2); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + if (FoxLauncher.isClient()) { |
| 89 | + // Replicate EntityPlayerSP behaviour |
| 90 | + Minecraft mc = Minecraft.getInstance(); |
| 91 | + mc.effectRenderer.addEffect(new EntityFXPickup(mc.theWorld, entity, this, -0.5F)); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + // Replicate EntityPlayerMP behaviour |
| 96 | + if (!entity.isDead) { |
| 97 | + EntityTracker tracker = MinecraftServer.getInstance().getEntityTracker(this.dimension); |
| 98 | + if (entity instanceof EntityItem || entity instanceof EntityThrownArrow) { |
| 99 | + tracker.sendPacketToTrackedPlayers(entity, new Packet22Collect(entity.entityId, this.entityId)); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + super.onItemPickup(entity, arg2); |
| 104 | + this.currentContainer.updateInventory(); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public void swingItem() { |
| 109 | + this.swingProgressInt = -1; |
| 110 | + this.isSwinging = true; |
| 111 | + if (FoxLauncher.isServer()) { |
| 112 | + MinecraftServer.getInstance().getEntityTracker(this.dimension) |
| 113 | + .sendPacketToTrackedPlayers(this, new Packet18Animation(this, 1)); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public void func_6420_o() {} |
| 119 | + |
| 120 | + @Override |
| 121 | + public boolean teleportToDim(int dimension, double x, double y, double z, float yaw, float pitch) { |
| 122 | + // Cross dimension teleport not supported. |
| 123 | + if (this.dimension != dimension) { |
| 124 | + return false; |
| 125 | + } |
| 126 | + this.setPositionAndRotation(x, y, z, yaw, pitch); |
| 127 | + return true; |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public boolean changeGamemode(int gamemode) { |
| 132 | + if (this.worldObj.isRemote) { |
| 133 | + return super.changeGamemode(gamemode); |
| 134 | + } |
| 135 | + if (this.gamemode == gamemode) { |
| 136 | + return false; |
| 137 | + } |
| 138 | + if (gamemode == 0) { |
| 139 | + this.capabilities.allowFlying = false; |
| 140 | + this.capabilities.isFlying = false; |
| 141 | + this.capabilities.isCreativeMode = false; |
| 142 | + this.capabilities.disableDamage = false; |
| 143 | + } else { |
| 144 | + this.capabilities.allowFlying = true; |
| 145 | + this.capabilities.isCreativeMode = true; |
| 146 | + this.capabilities.disableDamage = true; |
| 147 | + } |
| 148 | + this.gamemode = gamemode; |
| 149 | + return true; |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public void updateFishingRod() { |
| 154 | + super.updateFishingRod(); |
| 155 | + if (FoxLauncher.isServer()) { |
| 156 | + MinecraftServer.getInstance().configManager.sendPacketToAllPlayers( |
| 157 | + new Packet41UpdateFishingRod(this.entityId)); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public int getPlayerDataGamemode() { |
| 163 | + return this.gamemode; |
| 164 | + } |
| 165 | + |
| 166 | + public String getSkinUsername() { |
| 167 | + return null; |
| 168 | + } |
| 169 | +} |
0 commit comments