Chunk failed to save?

Discussion in 'Spigot Plugin Development' started by Bear53, Jun 8, 2016.

  1. Hello. So I have custom villagers spawned with custom trades. For some reason I just got this error randomly
    Code (Text):
    08.06 17:37:42 [Server] INFO ... 10 more
    08.06 17:37:42 [Server] INFO at net.minecraft.server.v1_8_R3.Entity.e(Entity.java:1209) ~[spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
    08.06 17:37:42 [Server] INFO at net.minecraft.server.v1_8_R3.EntityVillager.b(EntityVillager.java:159) ~[spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
    08.06 17:37:42 [Server] INFO at net.minecraft.server.v1_8_R3.MerchantRecipeList.a(SourceFile:134) ~[spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
    08.06 17:37:42 [Server] INFO at net.minecraft.server.v1_8_R3.MerchantRecipe.k(SourceFile:130) ~[spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
    08.06 17:37:42 [Server] INFO Caused by: java.lang.NullPointerException
    08.06 17:37:42 [Server] INFO at java.lang.Thread.run(Unknown Source) [?:1.8.0_77]
    08.06 17:37:42 [Server] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
    08.06 17:37:42 [Server] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:679) [spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
    08.06 17:37:42 [Server] INFO at org.bukkit.craftbukkit.v1_8_R3.CraftWorld.save(CraftWorld.java:753) [spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
    08.06 17:37:42 [Server] INFO at net.minecraft.server.v1_8_R3.WorldServer.save(WorldServer.java:928) [spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
    08.06 17:37:42 [Server] INFO at net.minecraft.server.v1_8_R3.ChunkProviderServer.saveChunks(ChunkProviderServer.java:323) [spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
    08.06 17:37:42 [Server] INFO at net.minecraft.server.v1_8_R3.ChunkProviderServer.saveChunk(ChunkProviderServer.java:251) [spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
    08.06 17:37:42 [Server] INFO at net.minecraft.server.v1_8_R3.ChunkRegionLoader.a(ChunkRegionLoader.java:127) [spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
    08.06 17:37:42 [Server] INFO at net.minecraft.server.v1_8_R3.ChunkRegionLoader.a(ChunkRegionLoader.java:278) ~[spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
    08.06 17:37:42 [Server] INFO at net.minecraft.server.v1_8_R3.Entity.d(Entity.java:1160) ~[spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
    08.06 17:37:42 [Server] INFO at net.minecraft.server.v1_8_R3.Entity.e(Entity.java:1223) ~[spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
    08.06 17:37:42 [Server] INFO net.minecraft.server.v1_8_R3.ReportedException: Saving entity NBT
    08.06 17:37:42 [Server] ERROR Failed to save chunk
    And I was wondering how I could prevent this error as I have never seen it before and it does not directly refer to my code
     
  2. Can you show us some of your code? :3
     
  3. Code (Text):
    import java.lang.reflect.Field;

    import net.minecraft.server.v1_8_R3.EntityVillager;
    import net.minecraft.server.v1_8_R3.ItemStack;
    import net.minecraft.server.v1_8_R3.MerchantRecipe;
    import net.minecraft.server.v1_8_R3.MerchantRecipeList;

    import org.bukkit.craftbukkit.v1_8_R3.entity.CraftVillager;
    import org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemStack;
    import org.bukkit.entity.Villager;
    import org.bukkit.plugin.java.JavaPlugin;

    public final class VillagerTradeAPI extends JavaPlugin {

        public static void clearTrades(final Villager villager) {

            final EntityVillager entityVillager = ((CraftVillager) villager)
                    .getHandle();

            try {
                final Field recipes = entityVillager.getClass().getDeclaredField(
                        "br");
                recipes.setAccessible(true);
                recipes.set(entityVillager, new MerchantRecipeList());
            }

            catch (Exception exc) {
                exc.printStackTrace();
            }

        }

        public static void addTrade(final Villager villager,
                final VillagerTrade villagerTrade) {

            final EntityVillager entityVillager = ((CraftVillager) villager)
                    .getHandle();

            try {
                final Field recipes = entityVillager.getClass().getDeclaredField(
                        "br");
                recipes.setAccessible(true);
                final MerchantRecipeList list = (MerchantRecipeList) recipes
                        .get(entityVillager);
                final ItemStack primaryItem = CraftItemStack
                        .asNMSCopy(VillagerTrade.getPrimaryItem(villagerTrade));

                MerchantRecipe recipe = null;

                if (VillagerTrade.hasSecondaryItem(villagerTrade)) {
                    final ItemStack secondaryItem = CraftItemStack
                            .asNMSCopy(VillagerTrade
                                    .getSecondaryItem(villagerTrade));
                    final ItemStack rewardItem = CraftItemStack
                            .asNMSCopy(VillagerTrade.getRewardItem(villagerTrade));
                    recipe = new MerchantRecipe(primaryItem, secondaryItem,
                            rewardItem);
                }

                else {
                    final ItemStack rewardItem = CraftItemStack
                            .asNMSCopy(VillagerTrade.getRewardItem(villagerTrade));
                    recipe = new MerchantRecipe(primaryItem, rewardItem);
                }

                final Field rewardXp = recipe.getClass().getDeclaredField(
                        "rewardExp");
                rewardXp.setAccessible(true);
                rewardXp.set(recipe, false);

                final Field maxUses = recipe.getClass().getDeclaredField("maxUses");
                maxUses.setAccessible(true);
                maxUses.set(recipe, 99999);

                list.add(recipe);
                recipes.set(entityVillager, list);
            }

            catch (Exception exc) {
                exc.printStackTrace();
            }

        }
    }
    Code (Text):
    import org.bukkit.inventory.ItemStack;

    public final class VillagerTrade {

        private ItemStack primaryItem;
        private ItemStack secondaryItem;
        private ItemStack rewardItem;

        public VillagerTrade(final ItemStack primaryItem,
                final ItemStack secondaryItem, final ItemStack rewardItem) {
            this.primaryItem = secondaryItem;
            this.secondaryItem = secondaryItem;
            this.rewardItem = rewardItem;
        }

        public VillagerTrade(final ItemStack primarySlot, final ItemStack rewardItem) {
            this.primaryItem = primarySlot;
            this.rewardItem = rewardItem;
        }

        @Deprecated
        public static boolean hasItem2(final VillagerTrade villagerTrade) {
            return villagerTrade.secondaryItem != null;
        }

        public static boolean hasSecondaryItem(final VillagerTrade villagerTrade) {
            return villagerTrade.secondaryItem != null;
        }

        @Deprecated
        public static ItemStack getItem1(final VillagerTrade villagerTrade) {
            return villagerTrade.secondaryItem;
        }

        public static ItemStack getPrimaryItem(final VillagerTrade villagerTrade) {
            return villagerTrade.primaryItem;
        }

        @Deprecated
        public static ItemStack getItem2(final VillagerTrade villagerTrade) {
            return villagerTrade.secondaryItem;
        }

        public static ItemStack getSecondaryItem(final VillagerTrade villagerTrade) {
            return villagerTrade.secondaryItem;
        }

        public static ItemStack getRewardItem(final VillagerTrade villagerTrade) {
            return villagerTrade.rewardItem;
        }
    }
    Code (Text):
    import java.util.ArrayList;
    import java.util.List;

    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Villager;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;

    import com.bear53.hcfcore.a.H;

    public class VillagerSpawn {

        public static void spawnBlocks(Player p) {
            Villager villager = (Villager) p.getWorld().spawnEntity(
                    p.getLocation(), EntityType.VILLAGER);
            villager.setCustomName(ChatColor.GREEN + "Blocks");
            villager.setCustomNameVisible(true);
            villager.addPotionEffect(new PotionEffect(PotionEffectType.SLOW,
                    Integer.MAX_VALUE, Integer.MAX_VALUE, false, false));
            List<VillagerTrade> t = new ArrayList<VillagerTrade>();
            VillagerTradeAPI.clearTrades(villager);
            t.add(new VillagerTrade(new ItemStack(Material.EMERALD, 1),
                    new ItemStack(Material.ICE, 64)));
            t.add(new VillagerTrade(new ItemStack(Material.EMERALD, 1),
                    new ItemStack(Material.SMOOTH_BRICK, 64, (byte) 2)));
            t.add(new VillagerTrade(new ItemStack(Material.EMERALD, 1),
                    new ItemStack(Material.SMOOTH_BRICK, 64, (byte) 3)));
            t.add(new VillagerTrade(new ItemStack(Material.EMERALD, 1),
                    new ItemStack(Material.MYCEL, 64)));
            t.add(new VillagerTrade(new ItemStack(Material.EMERALD, 1),
                    new ItemStack(Material.ENDER_STONE, 64)));
            t.add(new VillagerTrade(new ItemStack(Material.EMERALD, 1),
                    new ItemStack(Material.INK_SACK, 8)));
            for (VillagerTrade trade : t) {
                VillagerTradeAPI.addTrade(villager, trade);
            }
        }

        public static void spawnFarm(Player p) {
            Villager villager = (Villager) p.getWorld().spawnEntity(
                    p.getLocation(), EntityType.VILLAGER);
            villager.setCustomName(ChatColor.GREEN + "Farming Items");
            villager.setCustomNameVisible(true);
            villager.addPotionEffect(new PotionEffect(PotionEffectType.SLOW,
                    Integer.MAX_VALUE, Integer.MAX_VALUE, false, false));
            List<VillagerTrade> t = new ArrayList<VillagerTrade>();
            VillagerTradeAPI.clearTrades(villager);
            t.add(new VillagerTrade(new ItemStack(Material.EMERALD, 3),
                    new ItemStack(Material.NETHER_WARTS, 6)));
            t.add(new VillagerTrade(new ItemStack(Material.EMERALD, 3),
                    new ItemStack(Material.POTATO, 8)));
            t.add(new VillagerTrade(new ItemStack(Material.EMERALD, 1),
                    new ItemStack(Material.MELON_SEEDS, 6)));
            t.add(new VillagerTrade(new ItemStack(Material.EMERALD, 1),
                    new ItemStack(Material.SUGAR_CANE, 6)));
            t.add(new VillagerTrade(new ItemStack(Material.EMERALD, 3),
                    new ItemStack(Material.CARROT, 6)));
            for (VillagerTrade trade : t) {
                VillagerTradeAPI.addTrade(villager, trade);
            }
        }

        public static void spawnHorse(Player p) {
            Villager villager = (Villager) p.getWorld().spawnEntity(
                    p.getLocation(), EntityType.VILLAGER);
            villager.setCustomName(ChatColor.GREEN + "Horse Items");
            villager.setCustomNameVisible(true);
            villager.addPotionEffect(new PotionEffect(PotionEffectType.SLOW,
                    Integer.MAX_VALUE, Integer.MAX_VALUE, false, false));
            List<VillagerTrade> t = new ArrayList<VillagerTrade>();
            VillagerTradeAPI.clearTrades(villager);
            t.add(new VillagerTrade(new ItemStack(Material.EMERALD, 12),
                    new ItemStack(Material.DIAMOND_BARDING, 1)));
            t.add(new VillagerTrade(new ItemStack(Material.EMERALD, 5),
                    new ItemStack(Material.LEASH, 1)));
            t.add(new VillagerTrade(new ItemStack(Material.EMERALD, 3),
                    new ItemStack(Material.SADDLE, 1)));
            ItemStack su = new ItemStack(Material.SUGAR, 1);
            ItemMeta ss = su.getItemMeta();
            ss.setDisplayName(ChatColor.YELLOW + "Horse Speed Upgrade");
            List<String> l = new ArrayList<String>();
            l.add(ChatColor.DARK_PURPLE + "" + ChatColor.ITALIC
                    + "25% Speed increase");
            l.add(ChatColor.DARK_PURPLE + "" + ChatColor.ITALIC + "for horses.");
            l.add(ChatColor.DARK_PURPLE + "" + ChatColor.ITALIC
                    + "Speed is capped at 14 m/s");
            ss.setLore(l);
            su.setItemMeta(ss);
            t.add(new VillagerTrade(new ItemStack(Material.EMERALD_BLOCK, 10), su));
            for (VillagerTrade trade : t) {
                VillagerTradeAPI.addTrade(villager, trade);
            }
        }

        public static void spawnOres(Player p) {
            Villager villager = (Villager) p.getWorld().spawnEntity(
                    p.getLocation(), EntityType.VILLAGER);
            villager.setCustomName(ChatColor.GREEN + "Ores");
            villager.setCustomNameVisible(true);
            villager.addPotionEffect(new PotionEffect(PotionEffectType.SLOW,
                    Integer.MAX_VALUE, Integer.MAX_VALUE, false, false));
            List<VillagerTrade> t = new ArrayList<VillagerTrade>();
            VillagerTradeAPI.clearTrades(villager);
            t.add(new VillagerTrade(new ItemStack(Material.DIAMOND, 1),
                    new ItemStack(Material.EMERALD, 1)));
            t.add(new VillagerTrade(new ItemStack(Material.IRON_BLOCK, 1),
                    new ItemStack(Material.EMERALD, 1)));
            t.add(new VillagerTrade(new ItemStack(Material.GOLD_INGOT, 6),
                    new ItemStack(Material.EMERALD, 1)));
            t.add(new VillagerTrade(new ItemStack(Material.REDSTONE_BLOCK, 16),
                    new ItemStack(Material.EMERALD, 1)));
            t.add(new VillagerTrade(new ItemStack(Material.LAPIS_BLOCK, 16),
                    new ItemStack(Material.EMERALD, 1)));
            for (VillagerTrade trade : t) {
                VillagerTradeAPI.addTrade(villager, trade);
            }
        }

        public static void spawnRares(Player p) {
            Villager villager = (Villager) p.getWorld().spawnEntity(
                    p.getLocation(), EntityType.VILLAGER);
            villager.setCustomName(ChatColor.GREEN + "Rares");
            villager.setCustomNameVisible(true);
            villager.addPotionEffect(new PotionEffect(PotionEffectType.SLOW,
                    Integer.MAX_VALUE, Integer.MAX_VALUE));
            VillagerTradeAPI.clearTrades(villager);
            VillagerTrade trade = new VillagerTrade(new ItemStack(
                    Material.EMERALD_BLOCK, 25), new ItemStack(H.CROWBAR));
            VillagerTradeAPI.addTrade(villager, trade);
        }
    }
     
  4. It's probably fucking up when it tries to save the potion effects on the villager. Pretty sure the max potion effect level is like 255.

    You might want to set the effect strength to like 254(255), and the length to like 11(13200) minutes, then refresh it every 10(12000) minutes.
     
  5. ok thanks for that. and one other issue that has popped up is that when i try and spawn the Rare villager the entire server crashes with this error http://hastebin.com/nowikusata.coffee
     
  6. 08.06 17:21:17 [Server] ERROR net.minecraft.server.v1_8_R3.PotionBrewer.a(SourceFile:219)
    You're probably doing a potion wrong somewhere.
     
  7. could it have been setting the potion effect? but it does not make sence to me because the others work fine except for that one
     
  8. Look through all the code that runs when you spawn your 'rare villager'. Find the problem. If you can't find the problem, debug until you do.
     
  9. Thank you. Will report back once finished