Solved How do you apply a vanilla loot table on a chest?

Discussion in 'Spigot Plugin Development' started by Hex_27, Feb 3, 2020.

  1. I am trying to apply a vanilla loot table to a chest

    Code (Java):
    BlockPosition pos = new BlockPosition(x,y,z);
            TileEntityLootable.a(ichunkaccess, ws.getRandom(), pos, LootTables.d);
    I took this code from WorldGenDungeons, but when I try to use it with IChunkAccess (gotten from chunk.getHandle()), the chest remains empty. Is there something I need to do to let the loot table apply?

    Code from WorldGenDungeons:
    Code (Java):
    generatoraccess.setTypeAndData(blockposition2, StructurePiece.a((IBlockAccess) generatoraccess, blockposition2, Blocks.CHEST.getBlockData()), 2);
                                        TileEntityLootable.a(generatoraccess, random, blockposition2, LootTables.d);
    *Prior to calling my code, I set that location to a chest with bukkit.
     
    #1 Hex_27, Feb 3, 2020
    Last edited: Feb 4, 2020
  2. This is a bad question, hence why you aren't getting any answers. Please post more of your code, and explain what you are trying to do with more detail.
     
    • Useful Useful x 1
  3. There doesn't seem to be any other code or explanation to show, I'm simply trying to apply vanilla's loot tables onto a chest, and that's the code I used to try it.

    Is there something specific I need to provide?
     
  4. Ah I think I understand your concern now, I found the problem.

    I was using IChunkAccess of the wrong coordinates to do it.
    Full code for anyone that needs it in future:
    Code (Java):

    world.getBlockAt(x,y,z).setType(Material.CHEST);
    IChunkAccess ica = ((CraftChunk) chunk).getHandle();
    WorldServer ws = cw.getHandle();
    BlockPosition pos = new BlockPosition(x,y,z);
    TileEntityLootable.a(ica, ws.getRandom(), pos, LootTables.w);
    //Ensure that the chunk and x,y,z coordinates are in the same chunk
    //LootTables has its fields obfuscated, but you can see what each field represents by viewing the class itself. My code uses an enum for this:
        private MinecraftKey getLootTable(TerraLootTable table){
            switch(table){
                case SPAWN_BONUS_CHEST:
                    return LootTables.b ;
                case END_CITY_TREASURE:
                    return LootTables.c ;
                case SIMPLE_DUNGEON:
                    return LootTables.d ;
                case VILLAGE_WEAPONSMITH:
                    return LootTables.e ;
                case VILLAGE_TOOLSMITH:
                    return LootTables.f ;
                case VILLAGE_ARMORER:
                    return LootTables.g ;
                case VILLAGE_CARTOGRAPHER:
                    return LootTables.h ;
                case VILLAGE_MASON:
                    return LootTables.i ;
                case VILLAGE_SHEPHERD:
                    return LootTables.j ;
                case VILLAGE_BUTCHER:
                    return LootTables.k ;
                case VILLAGE_FLETCHER:
                    return LootTables.l ;
                case VILLAGE_FISHER:
                    return LootTables.m ;
                case VILLAGE_TANNERY:
                    return LootTables.n ;
                case VILLAGE_TEMPLE:
                    return LootTables.o ;
                case VILLAGE_DESERT_HOUSE:
                    return LootTables.p ;
                case VILLAGE_PLAINS_HOUSE:
                    return LootTables.q ;
                case VILLAGE_TAIGA_HOUSE:
                    return LootTables.r ;
                case VILLAGE_SNOWY_HOUSE:
                    return LootTables.s ;
                case VILLAGE_SAVANNA_HOUSE:
                    return LootTables.t ;
                case ABANDONED_MINESHAFT:
                    return LootTables.u ;
                case NETHER_BRIDGE:
                    return LootTables.v ;
                case STRONGHOLD_LIBRARY:
                    return LootTables.w ;
                case STRONGHOLD_CROSSING:
                    return LootTables.x ;
                case STRONGHOLD_CORRIDOR:
                    return LootTables.y ;
                case DESERT_PYRAMID:
                    return LootTables.z ;
                case JUNGLE_TEMPLE:
                    return LootTables.A ;
                case JUNGLE_TEMPLE_DISPENSER:
                    return LootTables.B ;
                case IGLOO_CHEST:
                    return LootTables.C ;
                case WOODLAND_MANSION:
                    return LootTables.D ;
                case UNDERWATER_RUIN_SMALL:
                    return LootTables.E ;
                case UNDERWATER_RUIN_BIG:
                    return LootTables.F ;
                case BURIED_TREASURE:
                    return LootTables.G ;
                case SHIPWRECK_MAP:
                    return LootTables.H ;
                case SHIPWRECK_SUPPLY:
                    return LootTables.I ;
                case SHIPWRECK_TREASURE:
                    return LootTables.J ;
                case PILLAGER_OUTPOST:
                    return LootTables.K ;
            }
            return null;
        }
       
     
  5. Why not just use the existing API? Used for both mob and chest loot tables, though mob loot is slightly less functional than could be desired.
     
    • Agree Agree x 1
  6. There are some parts where I can't use the API because I'm working on world generation via NMS, so I had to go figure out how the NMS way worked first
     
    • Informative Informative x 1