public static void teleportPlayerOutOfMachineDimension(EntityPlayerMP player) {
    NBTTagCompound playerNBT = player.getEntityData();

    // Grab the CompactMachines entry from the player NBT data
    NBTTagCompound cmNBT = null;
    if (playerNBT.hasKey(Reference.MOD_ID)) {
      cmNBT = playerNBT.getCompoundTag(Reference.MOD_ID);
    }

    int targetDimension = 0;
    double targetX;
    double targetY;
    double targetZ;

    if (cmNBT != null && cmNBT.hasKey("oldPosX")) {
      // First try to grab the original position by looking at the CompactMachines NBT Tag
      targetDimension = cmNBT.getInteger("oldDimension");
      targetX = cmNBT.getDouble("oldPosX");
      targetY = cmNBT.getDouble("oldPosY");
      targetZ = cmNBT.getDouble("oldPosZ");
    } else if (playerNBT.hasKey("oldDimension")
        && playerNBT.getInteger("oldDimension") != ConfigurationHandler.dimensionId) {
      // Backwards compatibility - but these values are also being set by RandomThings
      // A problem exists in two cases:
      // a) A player entered the SpiritDimension from the CM dimension, RandomThings would set the
      // oldDimension to the CM dimension
      // b) A player entered the CM dimension from the SpectreDimension, CM did previously set the
      // oldDimension to the SpectreDimension
      // In both cases the player gets trapped in a loop between the two dimensions and has no way
      // of getting back to the overworld
      // We want to allow backwards compatibility with our old settings so players in a CM during an
      // update to a version containing this commit
      // would still be trapped in the two dimensions. We can break this cycle by not allowing to
      // get back to another CM using the
      // old system.
      // That's because CM never writes its own dimension into the oldDimension tag, only
      // RandomThings would do that.
      targetDimension = playerNBT.getInteger("oldDimension");
      targetX = playerNBT.getDouble("oldPosX");
      targetY = playerNBT.getDouble("oldPosY");
      targetZ = playerNBT.getDouble("oldPosZ");
    } else {
      ChunkCoordinates cc =
          MinecraftServer.getServer().worldServerForDimension(0).provider.getRandomizedSpawnPoint();
      targetX = cc.posX;
      targetY = cc.posY;
      targetZ = cc.posZ;
    }

    MinecraftServer.getServer()
        .getConfigurationManager()
        .transferPlayerToDimension(
            player,
            targetDimension,
            new TeleporterCM(MinecraftServer.getServer().worldServerForDimension(targetDimension)));
    player.setPositionAndUpdate(targetX, targetY, targetZ);
  }
  @Override
  public boolean teleport(Location location, PlayerTeleportEvent.TeleportCause cause) {
    if (getHandle().playerNetServerHandler == null) return false;

    // From = Players current Location
    Location from = this.getLocation();
    // To = Players new Location if Teleport is Successful
    Location to = location;
    // Create & Call the Teleport Event.
    PlayerTeleportEvent event = new PlayerTeleportEvent((Player) this, from, to, cause);
    server.getPluginManager().callEvent(event);
    // Return False to inform the Plugin that the Teleport was unsuccessful/cancelled.
    if (event.isCancelled() == true) {
      return false;
    }
    // Update the From Location
    from = event.getFrom();
    // Grab the new To Location dependent on whether the event was cancelled.
    to = event.getTo();
    // Grab the To and From World Handles.
    WorldServer fromWorld = ((BukkitWorld) from.getWorld()).getHandle();
    WorldServer toWorld = ((BukkitWorld) to.getWorld()).getHandle();
    // Grab the EntityPlayerMP
    EntityPlayerMP entity = getHandle();

    // Check if the fromWorld and toWorld are the same.
    if (fromWorld == toWorld) {
      entity.setPositionAndUpdate(location.getX(), location.getY(), location.getZ());
    } else {
      // Close any foreign inventory
      if (getHandle().openContainer != getHandle().inventoryContainer) getHandle().closeInventory();
      server
          .getHandle()
          .getConfigurationManager()
          .transferPlayerToDimension(entity, toWorld.getWorldInfo().getDimension());
      entity.setPositionAndUpdate(location.getX(), location.getY(), location.getZ());
    }
    return true;
  }
 public static void travelToDimension(Entity entity, int dimensionId, int x, int y, int z) {
   if (!entity.worldObj.isRemote && !entity.isDead) {
     MinecraftServer minecraftserver = MinecraftServer.getServer();
     WorldServer newWorldServer = minecraftserver.worldServerForDimension(dimensionId);
     if (entity instanceof EntityPlayerMP) {
       minecraftserver
           .getConfigurationManager()
           .transferPlayerToDimension(
               (EntityPlayerMP) entity, dimensionId, new CustomTeleporter(newWorldServer));
       ((EntityPlayerMP) entity).setPositionAndUpdate(x + 0.5, y, z + 0.5);
     } else {
       travelEntityToDimension(entity, dimensionId, x, y, z);
     }
   }
 }
  public static boolean movePlayer(String playername, int dim, ChunkCoordinates dest) {

    EntityPlayerMP player =
        MinecraftServer.getServer().getConfigurationManager().func_152612_a(playername);

    if (MinecraftServer.getServer().getConfigurationManager().func_152612_a(playername) != null) {
      if (player.dimension != dim) {
        MinecraftServer.getServer()
            .getConfigurationManager()
            .transferPlayerToDimension(player, dim);
      }
      player.setPositionAndUpdate(dest.posX, dest.posY, dest.posZ);
      return true;
    } else {
      queuePlayer(playername, dim, dest);
      return false;
    }
  }
  public static void teleportPlayerToCoords(EntityPlayerMP player, int coord, boolean isReturning) {
    // LogHelper.info("Teleporting player to: " + coord);
    NBTTagCompound playerNBT = player.getEntityData();

    // Grab the CompactMachines entry from the player NBT data
    NBTTagCompound cmNBT;
    if (playerNBT.hasKey(Reference.MOD_ID)) {
      cmNBT = playerNBT.getCompoundTag(Reference.MOD_ID);
    } else {
      cmNBT = new NBTTagCompound();
      playerNBT.setTag(Reference.MOD_ID, cmNBT);
    }

    if (player.dimension != ConfigurationHandler.dimensionId) {
      cmNBT.setInteger("oldDimension", player.dimension);
      cmNBT.setDouble("oldPosX", player.posX);
      cmNBT.setDouble("oldPosY", player.posY);
      cmNBT.setDouble("oldPosZ", player.posZ);

      int oldDimension = player.dimension;

      WorldServer machineWorld =
          MinecraftServer.getServer().worldServerForDimension(ConfigurationHandler.dimensionId);
      MinecraftServer.getServer()
          .getConfigurationManager()
          .transferPlayerToDimension(
              player, ConfigurationHandler.dimensionId, new TeleporterCM(machineWorld));

      // If this is not being called teleporting from The End ends up without
      // the client knowing about any blocks, i.e. blank screen, no blocks, but
      // server collisions etc.
      if (oldDimension == 1) {
        machineWorld.spawnEntityInWorld(player);
      }

      // Since the player is currently not in the machine dimension, we want to clear
      // his coord history - in case he exited the machine world not via a shrinking device
      // which automatically clears the last entry in the coord history.
      if (playerNBT.hasKey("coordHistory")) {
        playerNBT.removeTag("coordHistory");
      }
    }

    if (!isReturning) {
      NBTTagList coordHistory;
      if (playerNBT.hasKey("coordHistory")) {
        coordHistory = playerNBT.getTagList("coordHistory", 10);
      } else {
        coordHistory = new NBTTagList();
      }
      NBTTagCompound toAppend = new NBTTagCompound();
      toAppend.setInteger("coord", coord);

      coordHistory.appendTag(toAppend);
      playerNBT.setTag("coordHistory", coordHistory);
    }

    MachineSaveData mHandler = CompactMachines.instance.machineHandler;
    double[] destination = mHandler.getSpawnLocation(coord);

    // Check whether the spawn location is blocked
    WorldServer machineWorld =
        MinecraftServer.getServer().worldServerForDimension(ConfigurationHandler.dimensionId);
    int dstX = (int) Math.floor(destination[0]);
    int dstY = (int) Math.floor(destination[1]);
    int dstZ = (int) Math.floor(destination[2]);

    if (!machineWorld.isAirBlock(dstX, dstY, dstZ)
        || !machineWorld.isAirBlock(dstX, dstY + 1, dstZ)) {
      // If it is blocked, try to find a better position
      double[] bestSpot = findBestSpawnLocation(machineWorld, coord);
      if (bestSpot != null) {
        destination = bestSpot;
      }

      // otherwise teleport to the default location... player will probably die though.
    }

    player.setPositionAndUpdate(destination[0], destination[1], destination[2]);

    if (destination.length == 5) {
      MessagePlayerRotation packet =
          new MessagePlayerRotation((float) destination[3], (float) destination[4]);
      PacketHandler.INSTANCE.sendTo(packet, player);
    }
  }
示例#6
0
  @SubscribeEvent
  public void onServerPacket(ServerCustomPacketEvent event) {

    if (!event.packet.channel().equals(Telepads.channelName)) return;

    EntityPlayerMP p = ((NetHandlerPlayServer) event.handler).playerEntity;
    ByteBufInputStream dis = new ByteBufInputStream(event.packet.payload());
    ByteBuf buf = event.packet.payload();

    World world = p.worldObj;

    int x = (int) p.posX;
    int y = (int) p.posY;
    int z = (int) p.posZ;

    try {

      int packetID = dis.readInt();

      int x2 = dis.readInt();
      int y2 = dis.readInt();
      int z2 = dis.readInt();

      TETelepad pad = (TETelepad) p.worldObj.getTileEntity(x2, y2, z2);

      switch (packetID) {
        case IDENTIFIER_GUI:
          int id = dis.readInt();
          p.openGui(Telepads.instance, id, world, x2, y2, z2);
          break;

        case IDENTIFIER_NAMEPAD:
          String name = dis.readUTF();
          String channel = dis.readUTF();

          pad.telepadname = name;
          pad.TELEPORTCHANNEL = channel;

          TelepadWorldData.get(world).addPad(pad);
          pad.markDirty();
          TelepadWorldData.get(world).markDirty();

          ByteBuf buff = Unpooled.buffer();
          ByteBufOutputStream out = new ByteBufOutputStream(buff);
          try {

            out.writeInt(ServerPacketHandler.IDENTIFIER_NAMEPAD);
            out.writeInt(x2);
            out.writeInt(y2);
            out.writeInt(z2);
            out.writeUTF(name);
            out.writeUTF(channel);
            out.close();

            if (!p.worldObj.isRemote)
              Telepads.Channel.sendTo(new FMLProxyPacket(buf, Telepads.channelName), p);

          } catch (IOException e) {
            e.printStackTrace();
          }
          break;

        case IDENTIFIER_TELEPORTER:
          if (dis.readInt() == GuiTeleport.EXIT_BUTTON) {
            pad.resetTE();
            pad.getDescriptionPacket();
            break;
          } else {
            // reset pad BEFORE the player gets teleported, or else it might be out of range and
            // can't be reset
            pad.resetTE();
            pad.getDescriptionPacket();

            int otherPadX = dis.readInt();
            int otherPadY = dis.readInt();
            int otherPadZ = dis.readInt();

            int dimID = dis.readInt();

            // if the dimension id = the End, play endscreen and teleport to spawn point.
            // this is needed or game will act funny if you don't.
            if (dimID != p.worldObj.provider.dimensionId) {
              if (p.worldObj.provider.dimensionId == 1) {
                p.travelToDimension(1);
              } else {
                p.travelToDimension(dimID);
                p.setPositionAndUpdate(otherPadX + 2, otherPadY + 0.5d, otherPadZ);
              }

            } else {
              p.setPositionAndUpdate(otherPadX + 2, otherPadY + 0.5d, otherPadZ);
            }
          }

          break;

        case IDENTIFIER_PLATFORM:
          boolean b = dis.readBoolean();

          pad.isStandingOnPlatform = b;

          if (b == false) {
            pad.resetTE();
            pad.playerStandingOnPad = null;
          }

          break;

        case IDENTIFIER_RESETnNOTIFY:
          p.addChatMessage(new ChatComponentText(dis.readUTF()));
          pad.resetTE();
          break;
      }

      dis.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  private static Entity sendEntityToWorld(
      Entity entity, int newDimension, Vector3 newPos, Facing3 newLook) {
    MinecraftServer server = MinecraftServer.getServer();
    Entity currentEntity = entity;
    if (entity.dimension != newDimension) {
      if (entity instanceof EntityPlayerMP) {
        EntityPlayerMP player = (EntityPlayerMP) entity;
        ServerConfigurationManager scm = server.getConfigurationManager();
        int oldDimension = player.dimension;
        player.dimension = newDimension;
        WorldServer oldWorld = server.worldServerForDimension(oldDimension);
        WorldServer newWorld = server.worldServerForDimension(newDimension);

        DimensionRegisterMessage packet =
            new DimensionRegisterMessage(
                newDimension, DimensionManager.getProviderType(newDimension));
        LCRuntime.runtime.network().getPreferredPipe().sendForgeMessageTo(packet, player);

        player.closeScreen();
        player.playerNetServerHandler.sendPacket(
            new S07PacketRespawn(
                player.dimension,
                player.worldObj.difficultySetting,
                newWorld.getWorldInfo().getTerrainType(),
                player.theItemInWorldManager.getGameType()));
        oldWorld.removePlayerEntityDangerously(player);
        player.isDead = false;
        player.setLocationAndAngles(
            newPos.x, newPos.y, newPos.z, (float) newLook.yaw, (float) newLook.pitch);
        newWorld.spawnEntityInWorld(player);
        player.setWorld(newWorld);
        scm.func_72375_a(player, oldWorld);
        player.playerNetServerHandler.setPlayerLocation(
            newPos.x, newPos.y, newPos.z, (float) newLook.yaw, (float) newLook.pitch);
        player.theItemInWorldManager.setWorld(newWorld);
        scm.updateTimeAndWeatherForPlayer(player, newWorld);
        scm.syncPlayerInventory(player);
        Iterator<?> var6 = player.getActivePotionEffects().iterator();
        while (var6.hasNext())
          player.playerNetServerHandler.sendPacket(
              new S1DPacketEntityEffect(player.getEntityId(), (PotionEffect) var6.next()));
        player.playerNetServerHandler.sendPacket(
            new S1FPacketSetExperience(
                player.experience, player.experienceTotal, player.experienceLevel));
      } else {
        int oldDimension = entity.dimension;
        WorldServer oldWorld = server.worldServerForDimension(oldDimension);
        WorldServer newWorld = server.worldServerForDimension(newDimension);
        entity.dimension = newDimension;

        entity.worldObj.removeEntity(entity);
        entity.isDead = false;
        server
            .getConfigurationManager()
            .transferEntityToWorld(entity, oldDimension, oldWorld, newWorld);
        currentEntity = EntityList.createEntityByName(EntityList.getEntityString(entity), newWorld);

        if (currentEntity != null) {
          currentEntity.copyDataFrom(entity, true);
          currentEntity.setLocationAndAngles(
              newPos.x, newPos.y, newPos.z, (float) newLook.yaw, (float) newLook.pitch);
          newWorld.spawnEntityInWorld(currentEntity);
        }

        entity.isDead = true;
        oldWorld.resetUpdateEntityTick();
        newWorld.resetUpdateEntityTick();
      }
    } else {
      currentEntity.setLocationAndAngles(
          newPos.x, newPos.y, newPos.z, (float) newLook.yaw, (float) newLook.pitch);
      if (currentEntity instanceof EntityPlayerMP) {
        EntityPlayerMP mpEnt = (EntityPlayerMP) currentEntity;
        mpEnt.rotationYaw = (float) newLook.yaw;
        mpEnt.setPositionAndUpdate(newPos.x, newPos.y, newPos.z);
        mpEnt.worldObj.updateEntityWithOptionalForce(entity, false);
      }
    }
    return currentEntity;
  }