Package com.bergerkiller.bukkit.common.nbt

Examples of com.bergerkiller.bukkit.common.nbt.CommonTagCompound


          }
          message(ChatColor.YELLOW + "Creating world '" + worldname + "' (this can take a while) ...");
          if (cgenName.indexOf(':') == 0) {
            String args = cgenName.substring(1);
            // Write a level.dat with the options changed
            CommonTagCompound data = wc.createData(seedval);
            data.putValue("generatorName", wc.worldmode.getTypeName());
            data.putValue("generatorVersion", 0);
            data.putValue("generatorOptions", args);
            wc.setData(data);
            message(ChatColor.WHITE + "World options: " + ChatColor.YELLOW + args);
            message(ChatColor.WHITE + "World generator: " + ChatColor.YELLOW + "Default (Vanilla)");
          } else {
            wc.setChunkGeneratorName(cgenName);
View Full Code Here


   * @param human to generate information about
   * @return empty data
   */
  public static CommonTagCompound createEmptyData(HumanEntity human) {
    final Vector velocity = human.getVelocity();
    CommonTagCompound empty = new CommonTagCompound();
    CommonLivingEntity<?> livingEntity = CommonEntity.get(human);
    empty.putUUID("", human.getUniqueId());
    empty.putValue("Health", (short) livingEntity.getMaxHealth());
    empty.putValue("HealF", (float) livingEntity.getMaxHealth()); // since 1.6.1 health is a float
    empty.putValue("HurtTime", (short) 0);
    empty.putValue("DeathTime", (short) 0);
    empty.putValue("AttackTime", (short) 0);
    empty.putListValues("Motion", velocity.getX(), velocity.getY(), velocity.getZ());
    setLocation(empty, WorldManager.getSpawnLocation(MyWorlds.getMainWorld()));
    final Object humanHandle = livingEntity.getHandle();
    IntVector3 coord = EntityHumanRef.spawnCoord.get(humanHandle);
    if (coord != null) {
      empty.putValue("SpawnWorld", EntityHumanRef.spawnWorld.get(humanHandle));
      empty.putValue("SpawnX", coord.x);
      empty.putValue("SpawnY", coord.y);
      empty.putValue("SpawnZ", coord.z);
    }
    return empty;
  }
View Full Code Here

  public static Location readLastLocation(Player player, World world) {
    File posFile = WorldConfig.get(world).getPlayerData(player.getName());
    if (!posFile.exists()) {
      return null;
    }
    CommonTagCompound data = read(posFile, player);
    CommonTagList posInfo = data.getValue(DATA_TAG_LASTPOS, CommonTagList.class);
    if (posInfo != null && posInfo.size() == 3) {
      // Apply position
      Location location = new Location(world, posInfo.getValue(0, 0.0), posInfo.getValue(1, 0.0), posInfo.getValue(2, 0.0));
      CommonTagList rotInfo = data.getValue(DATA_TAG_LASTROT, CommonTagList.class);
      if (rotInfo != null && rotInfo.size() == 2) {
        location.setYaw(rotInfo.getValue(0, 0.0f));
        location.setPitch(rotInfo.getValue(1, 0.0f));
      }
      return location;
View Full Code Here

    }
    try {
      CommonPlayer commonPlayer = CommonEntity.get(player);
      Object playerHandle = Conversion.toEntityHandle.convert(player);
      File source = getSaveFile(player);
      CommonTagCompound data = read(source, player);

      // First, clear previous player information when loading involves adding new elements
      clearEffects(player);

      // Refresh attributes
      if (data.containsKey("Attributes")) {
        NBTUtil.loadAttributes(player, data.get("Attributes", CommonTagList.class));
      }

      // Load the data
      NBTUtil.loadInventory(player.getInventory(), data.createList("Inventory"));
      EntityHumanRef.exp.set(playerHandle, data.getValue("XpP", 0.0f));
      EntityHumanRef.expLevel.set(playerHandle, data.getValue("XpLevel", 0));
      EntityHumanRef.expTotal.set(playerHandle, data.getValue("XpTotal", 0));

      if (Common.MC_VERSION.equals("1.5.2")) {
        commonPlayer.setHealth(data.getValue("Health", (int) commonPlayer.getMaxHealth()));
      } else {
        commonPlayer.setHealth(data.getValue("HealF", (float) commonPlayer.getMaxHealth()));
      }
     
      // Respawn position
      String spawnWorld = data.getValue("SpawnWorld", "");
      IntVector3 spawn = null;
      if (!spawnWorld.isEmpty()) {
        Integer x = data.getValue("SpawnX", Integer.class);
        Integer y = data.getValue("SpawnY", Integer.class);
        Integer z = data.getValue("SpawnZ", Integer.class);
        if (x != null && y != null && z != null) {
          spawn = new IntVector3(x, y, z);
        } else {
          spawnWorld = ""; //reset, invalid coordinates
        }
      }
      EntityHumanRef.spawnCoord.set(playerHandle, spawn);
      EntityHumanRef.spawnWorld.set(playerHandle, spawnWorld);
      EntityHumanRef.spawnForced.set(playerHandle, data.getValue("SpawnForced", false));

      // Other data
      NBTUtil.loadFoodMetaData(EntityHumanRef.foodData.get(playerHandle), data);
      NBTUtil.loadInventory(player.getEnderChest(), data.createList("EnderItems"));
     
      // Load Mob Effects
      HashMap<Integer, Object> effects = EntityHumanRef.mobEffects.get(playerHandle);
      if (data.containsKey("ActiveEffects")) {
        CommonTagList taglist = data.createList("ActiveEffects");
        for (int i = 0; i < taglist.size(); ++i) {
          Object mobEffect = NBTUtil.loadMobEffect((CommonTagCompound) taglist.get(i));
          effects.put(MobEffectRef.effectId.get(mobEffect), mobEffect);
        }
      }
View Full Code Here

  @Override
  public CommonTagCompound onLoad(HumanEntity human) {
    try {
      File main;
      CommonTagCompound tagcompound;
      boolean hasPlayedBefore = false;
      // Get the source file to use for loading
      main = getMainFile(human.getUniqueId());
      hasPlayedBefore = main.exists();

      // Find out where to find the save file
      // No need to check for this if not using world inventories - it is always the main file then
      if (MyWorlds.useWorldInventories && hasPlayedBefore && !MyWorlds.forceMainWorldSpawn) {
        try {
          // Allow switching worlds and positions
          tagcompound = CommonTagCompound.readFrom(main);
          World world = Bukkit.getWorld(tagcompound.getUUID("World"));
          if (world != null) {
            // Switch to the save file of the loaded world
            String saveWorld = WorldConfig.get(world).inventory.getSharedWorldName();
            main = WorldConfig.get(saveWorld).getPlayerData(human.getName());
          }
View Full Code Here

   * @param respawnLocation where the player respawns at
   */
  public void onRespawnSave(Player player, Location respawnLocation) {
    try {
      // Generate player saved information - used in favour of accessing NMS fields
      CommonTagCompound savedInfo = NBTUtil.saveEntity(player, null);
      // Generate a new tag compound with information
      CommonTagCompound tagcompound = createEmptyData(player);

      // We store this entire Bukkit tag + experience information!
      CommonTagCompound bukkitTag = savedInfo.get("bukkit", CommonTagCompound.class);
      if (bukkitTag != null) {
        // But, we do need to wipe information as specified
        if (bukkitTag.getValue("keepLevel", false)) {
          // Preserve experience
          bukkitTag.putValue("newTotalExp", savedInfo.getValue("XpTotal", 0));
          bukkitTag.putValue("newLevel", savedInfo.getValue("XpLevel", 0));
          tagcompound.putValue("XpP", savedInfo.getValue("XpP", 0.0f));
        }
        // Store experience (if not preserved, uses newTotal/newLevel) and the tag
        tagcompound.putValue("XpTotal", bukkitTag.getValue("newTotalExp", 0));
        tagcompound.putValue("XpLevel", bukkitTag.getValue("newLevel", 0));
        tagcompound.put("bukkit", bukkitTag);
      }

      // Ender inventory should not end up wiped!
      CommonTagList enderItems = savedInfo.get("EnderItems", CommonTagList.class);
View Full Code Here

  }

  @Override
  public void onSave(HumanEntity human) {
    try {
      CommonTagCompound tagcompound = NBTUtil.saveEntity(human, null);

      // Request several locations where player data is stored
      // Main file: the Main World folder where only the current World is stored
      // Pos file: the folder of the World the player is on where the position is stored     
      // Dest file: the inventory-merged folder where player info is stored
      File mainFile = getMainFile(human.getUniqueId());
      File posFile = WorldConfig.get(human).getPlayerData(human.getName());
      File destFile = getSaveFile(human);

      Location loc = human.getLocation();
      if (posFile.equals(destFile)) {
        // Append the Last Pos/Rot to the data
        tagcompound.putListValues(DATA_TAG_LASTPOS, loc.getX(), loc.getY(), loc.getZ());
        tagcompound.putListValues(DATA_TAG_LASTROT, loc.getYaw(), loc.getPitch());
      } else {
        // Append original last position (if available) to the data
        if (destFile.exists()) {
          CommonTagCompound data = read(destFile, human);
          if (data.containsKey(DATA_TAG_LASTPOS)) {
            tagcompound.put(DATA_TAG_LASTPOS, data.get(DATA_TAG_LASTPOS));
          }
          if (data.containsKey(DATA_TAG_LASTROT)) {
            tagcompound.put(DATA_TAG_LASTROT, data.get(DATA_TAG_LASTROT));
          }
        }

        // Write the Last Pos/Rot to the official world file instead
        CommonTagCompound data = read(posFile, human);
        data.putListValues(DATA_TAG_LASTPOS, loc.getX(), loc.getY(), loc.getZ());
        data.putListValues(DATA_TAG_LASTROT, loc.getYaw(), loc.getPitch());
        data.writeTo(posFile);
      }

      // Save data to the destination file
      tagcompound.writeTo(destFile);

      // Write the current world name of the player to the save file of the main world
      if (!mainFile.equals(destFile)) {
        // Update the world in the main file
        CommonTagCompound maincompound = read(mainFile, human);
        maincompound.put("Pos", tagcompound.get("Pos"));
        maincompound.put("Rotation", tagcompound.get("Rotation"));
        maincompound.putUUID("World", human.getWorld().getUID());
        maincompound.writeTo(mainFile);
      }
    } catch (Exception exception) {
      Bukkit.getLogger().warning("Failed to save player data for " + human.getName());
      exception.printStackTrace();
    }
View Full Code Here

    World world = worldC.getWorld();
    if (world != null) {
      return new Position(world.getSpawnLocation());
    }
    // Read from level.dat (if available)
    CommonTagCompound data = worldC.getData();
    if (data != null) {
      double[] pos = data.getValue("Pos", double[].class);
      float[] rot = data.getValue("Rotation", float[].class);
      return new Position(onWorldName, pos[0], pos[1], pos[2], rot[0], rot[1]);
    }
    // Absolutely NO idea, return a generic position
    return new Position(onWorldName, 0, 64, 0);
  }
View Full Code Here

            }
            if (stream != null) {
             
              //Validate the stream and close
              try {
                CommonTagCompound comp = CommonTagCompound.readFromUncompressed(stream);
                if (comp == null) {
                  editcount++;
                  locations[i] = 0;
                  MyWorlds.plugin.log(Level.WARNING, "Invalid tag compound at chunk " + chunkX + "/" + chunkZ);
                } else {
                  //correct location?
                  if (comp.containsKey("Level")) {
                    CommonTagCompound level = comp.createCompound("Level");
                    int xPos = level.getValue("xPos", Integer.MIN_VALUE);
                    int zPos = level.getValue("zPos", Integer.MIN_VALUE);
                    //valid coordinates?
                    if (xPos != chunkX || zPos != chunkZ) {
                      MyWorlds.plugin.log(Level.WARNING, "Chunk [" + xPos + "/" + zPos + "] was stored at [" + chunkX + "/" + chunkZ + "], moving...");
                      level.putValue("xPos", chunkX);
                      level.putValue("zPos", chunkZ);
                      //rewrite to stream
                      ByteArrayOutputStream baos = new ByteArrayOutputStream(8096);
                          OutputStream outputstream = new DeflaterOutputStream(baos);
                          level.writeToUncompressed(outputstream);
                          outputstream.close();
                          //write to region file
                          raf.seek(seekindex);
                          byte[] newdata = baos.toByteArray();
                          raf.writeInt(newdata.length + 1);
View Full Code Here

    } else {
      this.worldmode = WorldMode.get(worldname);
      this.spawnPoint = new Position(worldname, 0, 128, 0);
      if (WorldManager.worldExists(this.worldname)) {
        // Open up the level.dat of the World and read the settings from it
        CommonTagCompound data = this.getData();
        if (data != null) {
          // Read the settings from it
          this.spawnPoint.setX(data.getValue("SpawnX", this.spawnPoint.getBlockX()));
          this.spawnPoint.setY(data.getValue("SpawnY", this.spawnPoint.getBlockY()));
          this.spawnPoint.setZ(data.getValue("SpawnZ", this.spawnPoint.getBlockZ()));
        }
        // Figure out the world mode by inspecting the region files in the world
        // On failure, it will resort to using the world name to figure it out
        File regions = this.getRegionFolder();
        if (regions != null && regions.isDirectory()) {
View Full Code Here

TOP

Related Classes of com.bergerkiller.bukkit.common.nbt.CommonTagCompound

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.