Java net.minecraft.entity.player.EntityPlayerMP 代码实例

・22 分钟阅读

以下是展示如何使用net.minecraft.entity.player.EntityPlayerMP的最佳示例。 我们使用了代码质量辨别算法从开源项目中提取出了最佳的优秀示例。

实例 1


public static void openServerGui(MinerGuiPacket t, int guiType, World world, EntityPlayerMP playerMP, Coord4D obj, int i)
{
        Container container = null;
        playerMP.closeContainer();
        if(guiType == 0)
        {
                container = new ContainerNull(playerMP, (TileEntityContainerBlock)obj.getTileEntity(world));
        }
        else if(guiType == 4)
        {
                container = new ContainerDigitalMiner(playerMP.inventory, (TileEntityDigitalMiner)obj.getTileEntity(world));
        }
        else if(guiType == 5)
        {
                container = new ContainerNull(playerMP, (TileEntityContainerBlock)obj.getTileEntity(world));
        }
        else if(guiType == 1 || guiType == 2 || guiType == 3 || guiType == 6)
        {
                container = new ContainerFilter(playerMP.inventory, (TileEntityContainerBlock)obj.getTileEntity(world));
        }
        playerMP.getNextWindowId();
        int window = playerMP.currentWindowId;
        if(t == MinerGuiPacket.SERVER)
        {
                Mekanism.packetHandler.sendTo(new DigitalMinerGuiMessage(MinerGuiPacket.CLIENT, obj, guiType, window, 0), playerMP);
        }
        else if(t == MinerGuiPacket.SERVER_INDEX)
        {
                Mekanism.packetHandler.sendTo(new DigitalMinerGuiMessage(MinerGuiPacket.CLIENT_INDEX, obj, guiType, window, i), playerMP);
        }
        playerMP.openContainer = container;
        playerMP.openContainer.windowId = window;
        playerMP.openContainer.addCraftingToCrafters(playerMP);
        if(guiType == 0)
        {
                TileEntityDigitalMiner tile = (TileEntityDigitalMiner)obj.getTileEntity(world);
                for(EntityPlayer player : tile.playersUsing)
                {
                        Mekanism.packetHandler.sendTo(new TileEntityMessage(obj, tile.getFilterPacket(new ArrayList())), (EntityPlayerMP)player);
                }
        }
}
 

实例 2


@Override
public void processCommand(ICommandSender sender, String[] params)
{
        MinecraftServer server = MinecraftServer.getServer();
        EntityPlayerMP player = null;
        try {
                player = getCommandSenderAsPlayer(sender);
        } catch (PlayerNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }
        WorldServer world = server.worldServerForDimension(player.getEntityWorld().provider.getDimensionId());
        if(params.length == 3)
        {
                int x = (int)Math.floor(player.posX);
                int y = (int)Math.floor(player.posY);
                int z = (int)Math.floor(player.posZ);
                int mX = x >> 12;
                int mZ = z >> 12;
                int inX = Integer.parseInt(params[0]);
                int inY = Integer.parseInt(params[1]);
                int inZ = Integer.parseInt(params[2]);
                Point p = new Point(x, z);
                if(params[0].contains("+") || params[0].contains("-"))
                        x += inX;
                else
                        x = mX*4096+inX;
                if(params[1].contains("+") || params[1].contains("-"))
                        y += inY;
                else
                        y = inY;
                if(params[2].contains("+") || params[2].contains("-"))
                        z += inZ;
                else
                        z = mZ*4096+inZ;
                player.setPositionAndUpdate(x, y, z);
        }
}
 

实例 3


public void func_72375_a(final EntityPlayerMP playerIn, final WorldServer worldIn)
{
        final WorldServer var3 = playerIn.getServerForPlayer();
        if (worldIn != null)
                worldIn.getPlayerManager().removePlayer(playerIn);
        var3.getPlayerManager().addPlayer(playerIn);
        var3.theChunkProviderServer.loadChunk((int) playerIn.posX >> 4, (int) playerIn.posZ >> 4);
}
 

实例 4


public boolean canPlayerEdit(EntityPlayer p) {
        if (settings.canEdit() && p instanceof EntityPlayerMP) {
                EntityPlayerMP mp = (EntityPlayerMP) p;
                if (mp.mcServer == null || mp.getCommandSenderName() == null)
                        return false;
                // New method of checking whether the player is opped using GameProfile.
                ServerConfigurationManager configManager        = mp.mcServer.getConfigurationManager();
                EntityPlayerMP player                                           = configManager.func_152612_a(mp.getCommandSenderName());
                GameProfile profile                                                     = player.getGameProfile();
                return !mp.mcServer.isDedicatedServer() || configManager.func_152596_g(profile);
        }
        return false;
}
 

实例 5


@Override
public void handleCommand(ICommandSender sender, String[] args, boolean isPlayer) throws WrongUsageException {
    if (isPlayer) {
        EntityPlayerMP player = (EntityPlayerMP)sender;
        boolean CanSend = PermissionsHandler.canUseCommand(CommandsConfig.CommandPermissions.get("Repair"), player);
        if (CanSend) {
            ItemStack HeldItem = player.getHeldItem();
            HeldItem.setMetadata(0);
            sender.addChatMessage(new ChatComponentText("u00A7b" + HeldItem.getDisplayName() + " has been repaired."));
        } else throw new WrongUsageException(StatCollector.translateToLocal("commands.exception.permission"));
    } else throw new WrongUsageException(StatCollector.translateToLocal("commands.exception.playersonly"));
}
 

实例 6


public static void sendMotd (ICommandSender sender) {
        Date now = new Date();
        if (sender instanceof EntityPlayerMP) {
                EntityPlayerMP player = (EntityPlayerMP) sender;
                String onlinePlayers = "" + player.worldObj.playerEntities.size();
                for (int l = 0; l < msgList.size(); l++) {
                        player.sendChatToPlayer(ChatMessageComponent.createFromText(
                                msgList.get(l)
                                        .replaceAll("&", ChatFormatCodes.CODE.toString())
                                        .replaceAll("%playername%", player.username)
                                        .replaceAll("%players%", onlinePlayers)
                                        .replaceAll("%time%", now.toString())));
                }
        } else {
                for (int l = 0; l < msgList.size(); l++) {
                        sender.sendChatToPlayer(ChatMessageComponent.createFromText(msgList.get(l)));
                }
        }
}
 

实例 7


public void procCommand(final ICommandSender sender, final WorldSettings.GameType gameType, final EntityPlayerMP player)
{
        player.setGameType(gameType);
        player.fallDistance = 0.0F;
        if (sender.getEntityWorld().getGameRules().getGameRuleBooleanValue("sendCommandFeedback"))
                player.addChatMessage(new ChatComponentTranslation("gameMode.changed"));
        final ChatComponentTranslation message = new ChatComponentTranslation("gameMode." + gameType.getName());
        if (player != sender)
                CommandUtilities.notifyOperators(sender, 1, "commands.gamemode.success.other", player.getName(), message);
        else
                CommandUtilities.notifyOperators(sender, 1, "commands.gamemode.success.self", message);
}
 

实例 8


@SubscribeEvent
public void onServerPacket(ClientCustomPacketEvent event)
{
        try
        {
                EntityPlayerMP player = ((NetHandlerPlayServer) event.handler).playerEntity;
                ByteBufInputStream bbis = new ByteBufInputStream(event.packet.payload());
                ItemStack currentItem = player.getCurrentEquippedItem();
                byte packetId = bbis.readByte();
        } catch (Exception e)
        {
                e.printStackTrace();
        }
}
 

实例 9


public void updatePlayerInventory(EntityPlayer player) 
{
        if (player instanceof EntityPlayerMP) {
                EntityPlayerMP playerMP = (EntityPlayerMP)player;
                playerMP.sendContainerToPlayer(playerMP.inventoryContainer);
        }
}
 

实例 10


@SubscribeEvent
public void entityDeathEvent(LivingDeathEvent event) {
    if (event.entityLiving != null && !event.entityLiving.worldObj.isRemote && event.source != null) {
        if (event.source.getSourceOfDamage() != null && damageFromPlayer(event.source) && !targets.isEmpty()) {
            KillTarget info = matchInfo(event.entityLiving.getClass());
            EntityPlayerMP player = getPlayerFromSource(event.source);
            if (info != null && player != null && !player.func_147099_x().hasAchievementUnlocked(info.getAchievement()) && player.func_147099_x().canUnlockAchievement(info.getAchievement())) {
                int count = 1 + NBTUtil.getInt(player, info.getAchievement().statId);
                NBTUtil.setInt(player, info.getAchievement().statId, count);
                if (count >= info.getCount()) {
                    player.addStat(info.getAchievement(), 1);
                    NBTUtil.removeTag(player, info.getAchievement().statId);
                }
            }
        }
    }
}
 

实例 11


@Override
public void handleCommand(ICommandSender sender, String[] args, boolean isPlayer) throws PlayerNotFoundException, WrongUsageException {
    switch(args.length){
        case 1:
            if (isPlayer) {
                EntityPlayerMP player = (EntityPlayerMP) sender;
                boolean CanSend = PermissionsHandler.canUseCommand(CommandsConfig.CommandPermissions.get("Kill"), player);
                if (CanSend) player.attackEntityFrom(new DamageSource("command_kill." + player.worldObj.rand.nextInt(18)).setDamageBypassesArmor().setDamageAllowedInCreativeMode(), Float.MAX_VALUE);
                else throw new WrongUsageException(StatCollector.translateToLocal("commands.exception.permission"));
            } else throw new WrongUsageException(StatCollector.translateToLocal("commands.exception.playersonly"));
        break;
        case 2:
            EntityPlayerMP player2 = CommandBase.getPlayer(sender, args[1]);
            player2.attackEntityFrom(new DamageSource("command_kill." + player2.worldObj.rand.nextInt(18)).setDamageBypassesArmor().setDamageAllowedInCreativeMode(), Float.MAX_VALUE);
        break;
        default: throw new WrongUsageException(StatCollector.translateToLocal("commands." + getCommandName().toLowerCase() + ".usage"));
    }
}
 

实例 12


/**
 * Called when a player disconnects from the game. Writes player data to disk and removes them from the world.
 */
public void playerLoggedOut(final EntityPlayerMP playerIn)
{
        playerIn.triggerAchievement(StatList.leaveGameStat);
        this.writePlayerData(playerIn);
        final WorldServer var2 = playerIn.getServerForPlayer();
        if (playerIn.ridingEntity != null)
        {
                var2.removePlayerEntityDangerously(playerIn.ridingEntity);
                logger.debug("removing player mount");
        }
        var2.removeEntity(playerIn);
        var2.getPlayerManager().removePlayer(playerIn);
        this.playerEntityList.remove(playerIn);
        final String name = playerIn.getName();
        for (final Iterator<ITabCompletion> it = this.playerCompletions.iterator(); it.hasNext();)
                if (name.equals(it.next().name))
                {
                        it.remove();
                        break;
                }
        this.field_177454_f.remove(playerIn.getUniqueID());
        this.playerStatFiles.remove(playerIn.getUniqueID());
        this.sendPacketToAllPlayers(new S38PacketPlayerListItem(S38PacketPlayerListItem.Action.REMOVE_PLAYER, new EntityPlayerMP[] { playerIn }));
}
 

实例 13


/**
 * called during player login. reads the player information from disk.
 */
public NBTTagCompound readPlayerDataFromFile(final EntityPlayerMP playerIn)
{
        final NBTTagCompound var2 = this.mcServer.worldServers[0].getWorldInfo().getPlayerNBTTagCompound();
        NBTTagCompound var3;
        if (playerIn.getName().equals(this.mcServer.getServerOwner()) && var2 != null)
        {
                playerIn.readFromNBT(var2);
                var3 = var2;
                logger.debug("loading single player");
        }
        else
                var3 = this.playerNBTManagerObj.readPlayerData(playerIn);
        return var3;
}
 

实例 14


private void tp(EntityPlayerMP player)
{
    World world = player.worldObj;
    if (!world.provider.canRespawnHere())
        world = DimensionManager.getWorld(0);
    ChunkCoordinates spawn = player.getBedLocation(world.provider.dimensionId);
    if (spawn == null && world.provider.dimensionId != 0)
    {
        world = DimensionManager.getWorld(0);
        spawn = player.getBedLocation(world.provider.dimensionId);
    }
    if (spawn == null)
        throw new TranslatedCommandException("No bed found.");
    spawn = EntityPlayer.verifyRespawnCoordinates(player.worldObj, spawn, true);
    if (spawn == null)
        throw new TranslatedCommandException("Your bed has been obstructed.");
    PlayerInfo.get(player.getPersistentID()).setLastTeleportOrigin(new WarpPoint(player));
    WarpPoint spawnPoint = new WarpPoint(world.provider.dimensionId, spawn, player.rotationPitch, player.rotationYaw);
    TeleportHelper.teleport(player, spawnPoint);
}
 

实例 15


public void initializeConnectionToPlayer(final NetworkManager netManager, final EntityPlayerMP playerIn)
{
        final GameProfile var3 = playerIn.getGameProfile();
        final PlayerProfileCache var4 = this.mcServer.getPlayerProfileCache();
        final GameProfile var5 = var4.func_152652_a(var3.getId());
        final String var6 = var5 == null ? var3.getName() : var5.getName();
        var4.func_152649_a(var3);
        final NBTTagCompound var7 = this.readPlayerDataFromFile(playerIn);
        playerIn.setWorld(this.mcServer.worldServerForDimension(playerIn.dimension));
        playerIn.theItemInWorldManager.setWorld((WorldServer) playerIn.worldObj);
        String var8 = "local";
        if (netManager.getRemoteAddress() != null)
                var8 = netManager.getRemoteAddress().toString();
        logger.info(playerIn.getName() + "[" + var8 + "] logged in with entity id " + playerIn.getEntityId() + " at (" + playerIn.posX + ", " + playerIn.posY + ", " + playerIn.posZ + ")");
        final WorldServer var9 = this.mcServer.worldServerForDimension(playerIn.dimension);
        final WorldInfo var10 = var9.getWorldInfo();
        final BlockPos var11 = var9.getSpawnPoint();
        this.func_72381_a(playerIn, (EntityPlayerMP) null, var9);
        final NetHandlerPlayServer var12 = new NetHandlerPlayServer(this.mcServer, netManager, playerIn);
        var12.sendPacket(new S01PacketJoinGame(playerIn.getEntityId(), playerIn.theItemInWorldManager.getGameType(), var10.isHardcoreModeEnabled(), var9.provider.getDimensionId(), var9.getDifficulty(), this.getMaxPlayers(), var10.getTerrainType(), var9.getGameRules().getGameRuleBooleanValue("reducedDebugInfo")));
        var12.sendPacket(new S3FPacketCustomPayload("MC|Brand", (new PacketBuffer(Unpooled.buffer())).writeString(this.getServerInstance().getServerModName())));
        var12.sendPacket(new S41PacketServerDifficulty(var10.getDifficulty(), var10.isDifficultyLocked()));
        var12.sendPacket(new S05PacketSpawnPosition(var11));
        var12.sendPacket(new S39PacketPlayerAbilities(playerIn.capabilities));
        var12.sendPacket(new S09PacketHeldItemChange(playerIn.inventory.currentItem));
        playerIn.getStatFile().func_150877_d();
        playerIn.getStatFile().func_150884_b(playerIn);
        this.func_96456_a((ServerScoreboard) var9.getScoreboard(), playerIn);
        this.mcServer.refreshStatusNextTick();
        ChatComponentTranslation var13;
        if (!playerIn.getName().equalsIgnoreCase(var6))
                var13 = new ChatComponentTranslation("multiplayer.player.joined.renamed", playerIn.getDisplayName(), var6);
        else
                var13 = new ChatComponentTranslation("multiplayer.player.joined", playerIn.getDisplayName());
        var13.getChatStyle().setColor(EnumChatFormatting.YELLOW);
        this.sendChatMsg(var13);
        this.playerLoggedIn(playerIn);
        var12.setPlayerLocation(playerIn.posX, playerIn.posY, playerIn.posZ, playerIn.rotationYaw, playerIn.rotationPitch);
        this.updateTimeAndWeatherForPlayer(playerIn, var9);
        if (this.mcServer.getResourcePackUrl().length() > 0)
                playerIn.func_175397_a(this.mcServer.getResourcePackUrl(), this.mcServer.getResourcePackHash());
        final Iterator var14 = playerIn.getActivePotionEffects().iterator();
        while (var14.hasNext())
        {
                final PotionEffect var15 = (PotionEffect) var14.next();
                var12.sendPacket(new S1DPacketEntityEffect(playerIn.getEntityId(), var15));
        }
        playerIn.addSelfToInternalCraftingInventory();
        if (var7 != null && var7.hasKey("Riding", 10))
        {
                final Entity var16 = EntityList.createEntityFromNBT(var7.getCompoundTag("Riding"), var9);
                if (var16 != null)
                {
                        var16.forceSpawn = true;
                        var9.spawnEntityInWorld(var16);
                        playerIn.mountEntity(var16);
                        var16.forceSpawn = false;
                }
        }
}
 
讨论
淘淘あ西西 profile image