private List<ItemStack> getEntityLoot_Table(EntityLiving el){ ResourceLocation location = (ResourceLocation)ReflectionHelper.getPrivateValue(EntityLiving.class, el, "deathLootTable","field_184659_bA"); if(location==null){ Method getLT = ReflectionHelper.findMethod(EntityLiving.class,"getLootTable","func_184647_J"); try { location = (ResourceLocation)getLT.invoke(el); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { RunesofWizardry_Classics.log().error("Exception when trying to get LootTable from entity: "+el.getName(),e); return getEntityLoot_Hacky(el); } } if(location==null){ RunesofWizardry_Classics.log().warn(el.getName()+" does not have a LootTable. falling back to kill method"); return getEntityLoot_Hacky(el); } LootTableManager manager = el.world.getLootTableManager(); LootTable table = manager.getLootTableFromLocation(location); return LootUtils.tableToItemStacks(table); }
/** * Fill this inventory with loot. * <p> * Does nothing if no loot table has been set, loot has already been generated or this is being called on the client side. * * @param player The player whose Luck to use when generating loot */ public void fillWithLoot(@Nullable EntityPlayer player) { final World world = worldContainer.getContainedWorld(); if (lootTableLocation != null && !world.isRemote) { final LootTable lootTable = world.getLootTableManager().getLootTableFromLocation(lootTableLocation); lootTableLocation = null; final Random random = lootTableSeed == 0 ? new Random() : new Random(lootTableSeed); final LootContext.Builder builder = new LootContext.Builder((WorldServer) world); if (player != null) { builder.withLuck(player.getLuck()); } InventoryUtils.fillItemHandlerWithLoot(this, lootTable, random, builder.build()); } }
/** * Fill an {@link IItemHandler} with random loot from a {@link LootTable}. * <p> * Adapted from {@link LootTable#fillInventory}. * * @param itemHandler The inventory to fill with loot * @param lootTable The LootTable to generate loot from * @param random The Random object to use in the loot generation * @param context The LootContext to use in the loot generation */ public static void fillItemHandlerWithLoot(IItemHandler itemHandler, LootTable lootTable, Random random, LootContext context) { final List<ItemStack> items = lootTable.generateLootForPools(random, context); final List<Integer> emptySlots = getEmptySlotsRandomized(itemHandler, random); try { SHUFFLE_ITEMS.invoke(lootTable, items, emptySlots.size(), random); } catch (Throwable throwable) { Throwables.propagate(throwable); } for (ItemStack itemStack : items) { if (emptySlots.isEmpty()) { Logger.warn("Tried to over-fill %s while generating loot."); return; } final int slot = emptySlots.remove(emptySlots.size() - 1); final ItemStack remainder = itemHandler.insertItem(slot, itemStack, false); if (remainder != null) { Logger.warn("Couldn't fully insert %s into slot %d of %s, %d items remain.", itemStack, slot, itemHandler, remainder.stackSize); } } }
public static void generateLoot(ItemStackHandler items, String tableStr, long seed, World world, EntityPlayer player) { Random rnd = new Random(seed); double maxFullness = (0.6 + rnd.nextDouble() * 0.2); int maxOccupiedSlots = (int)Math.ceil(items.getSlots() * maxFullness); LootTableManager manager = world.getLootTableManager(); LootTable table = manager.getLootTableFromLocation(new ResourceLocation(tableStr)); LootContext context = new LootContext(((player != null) ? player.getLuck() : 0), (WorldServer)world, manager, player, null, null); List<ItemStack> loot = table.generateLootForPools(rnd, context); Collections.shuffle(loot); List<Integer> randomizedSlots = new ArrayList<Integer>(items.getSlots()); for (int i = 0; i < items.getSlots(); i++) randomizedSlots.add(i); Collections.shuffle(randomizedSlots); for (int i = 0; (i < maxOccupiedSlots) && (i < loot.size()); i++) { ItemStack stack = loot.get(i); int slot = randomizedSlots.get(i); items.setStackInSlot(slot, stack); } }
@Override public @Nonnull EnumActionResult onItemUseFirst(@Nonnull EntityPlayer player, @Nonnull World world, @Nonnull BlockPos pos, @Nonnull EnumFacing side, float hitX, float hitY, float hitZ, @Nonnull EnumHand hand) { if (world.isRemote || System.getProperty("INDEV") == null || !player.isCreative()) { return EnumActionResult.PASS; } TileEntity te = world.getTileEntity(pos); if (te instanceof TileEntityChest) { TileEntityChest chest = (TileEntityChest) te; chest.clear(); LootContext.Builder lootcontext$builder = new LootContext.Builder((WorldServer) world); lootcontext$builder.withLuck(player.getLuck()); LootTable loottable = world.getLootTableManager().getLootTableFromLocation(LootTableList.CHESTS_SIMPLE_DUNGEON); // LootTable loottable = world.getLootTableManager().getLootTableFromLocation(LootTableList.CHESTS_IGLOO_CHEST); loottable.fillInventory(chest, world.rand, lootcontext$builder.build()); return EnumActionResult.PASS; } return EnumActionResult.PASS; }
private static void addPool(LootTable table) { LootEntry common = new LootEntryTable(new ResourceLocation("lootslashconquer:chests/common_chest"), 60, 1, new LootCondition[0], "common"); LootEntry uncommon = new LootEntryTable(new ResourceLocation("lootslashconquer:chests/uncommon_chest"), 25, 1, new LootCondition[0], "uncommon"); LootEntry rare = new LootEntryTable(new ResourceLocation("lootslashconquer:chests/rare_chest"), 10, 1, new LootCondition[0], "rare"); LootEntry legendary = new LootEntryTable(new ResourceLocation("lootslashconquer:chests/legendary_chest"), 5, 1, new LootCondition[0], "legendary"); LootEntry exotic = new LootEntryTable(new ResourceLocation("lootslashconquer:chests/exotic_chest"), 2, 1, new LootCondition[0], "exotic"); LootPool pool = new LootPool(new LootEntry[] { common, uncommon, rare, legendary, exotic }, new LootCondition[0], new RandomValueRange(0, 1), new RandomValueRange(0), "loot"); table.addPool(pool); }
@SubscribeEvent public static void lootLoaded(LootTableLoadEvent event) { if (event.getName().equals(LootTableList.CHESTS_SIMPLE_DUNGEON)) { LootTable customTable = event.getLootTableManager().getLootTableFromLocation(new ResourceLocation(BetterThanWeagles.MODID, "custom/simple_dungeon_chest")); LootPool customPool = customTable.getPool("weagles"); event.getTable().addPool(customPool); } }
/** * drops the loot of this entity upon death */ protected void dropLoot(boolean wasRecentlyHit, int lootingModifier, DamageSource source) { ResourceLocation resourcelocation = this.deathLootTable; if (resourcelocation == null) { resourcelocation = this.getLootTable(); } if (resourcelocation != null) { LootTable loottable = this.world.getLootTableManager().getLootTableFromLocation(resourcelocation); this.deathLootTable = null; LootContext.Builder lootcontext$builder = (new LootContext.Builder((WorldServer)this.world)).withLootedEntity(this).withDamageSource(source); if (wasRecentlyHit && this.attackingPlayer != null) { lootcontext$builder = lootcontext$builder.withPlayer(this.attackingPlayer).withLuck(this.attackingPlayer.getLuck()); } for (ItemStack itemstack : loottable.generateLootForPools(this.deathLootTableSeed == 0L ? this.rand : new Random(this.deathLootTableSeed), lootcontext$builder.build())) { this.entityDropItem(itemstack, 0.0F); } this.dropEquipment(wasRecentlyHit, lootingModifier); } else { super.dropLoot(wasRecentlyHit, lootingModifier, source); } }
public static void addLoot(LootTable table, ResourceLocation tableName) { if(BBConfig.spawnMarshmallows) { if(tableName.equals(LootTableList.CHESTS_SIMPLE_DUNGEON)) { LootUtil.addItemToTable(table, "main", RegisterItems.marshmallow, 100, 1, 5); } else if(tableName.equals(LootTableList.CHESTS_DESERT_PYRAMID)) { LootUtil.addItemToTable(table, "main", RegisterItems.marshmallow, 200, 1, 10); } else if(tableName.equals(LootTableList.CHESTS_JUNGLE_TEMPLE)) { LootUtil.addItemToTable(table, "main", RegisterItems.marshmallow, 200, 1, 10); } else if(tableName.equals(LootTableList.CHESTS_STRONGHOLD_CROSSING)) { LootUtil.addItemToTable(table, "main", RegisterItems.marshmallow, 150, 1, 12); } else if(tableName.equals(LootTableList.CHESTS_STRONGHOLD_CORRIDOR)) { LootUtil.addItemToTable(table, "main", RegisterItems.marshmallow, 150, 1, 12); } else if(tableName.equals(LootTableList.CHESTS_ABANDONED_MINESHAFT)) { LootUtil.addItemToTable(table, "main", RegisterItems.marshmallow, 150, 1, 12); } } }
public static LootTable loadLootTable(ResourceLocation name, LootTable table) { LootTableLoadEvent event = new LootTableLoadEvent(name, table); if (MinecraftForge.EVENT_BUS.post(event)) return LootTable.EMPTY_LOOT_TABLE; return event.getTable(); }
public static LootTable loadLootTable(Gson gson, ResourceLocation name, String data, boolean custom) { Deque<LootTableContext> que = lootContext.get(); if (que == null) { que = Queues.newArrayDeque(); lootContext.set(que); } LootTable ret = null; try { que.push(new LootTableContext(name, custom)); ret = gson.fromJson(data, LootTable.class); que.pop(); } catch (JsonParseException e) { que.pop(); throw e; } if (!custom) ret = ForgeEventFactory.loadLootTable(name, ret); if (ret != null) ret.freeze(); return ret; }
/** * drops the loot of this entity upon death */ protected void dropLoot(boolean wasRecentlyHit, int lootingModifier, DamageSource source) { ResourceLocation resourcelocation = this.deathLootTable; if (resourcelocation == null) { resourcelocation = this.getLootTable(); } if (resourcelocation != null) { LootTable loottable = this.worldObj.getLootTableManager().getLootTableFromLocation(resourcelocation); this.deathLootTable = null; LootContext.Builder lootcontext$builder = (new LootContext.Builder((WorldServer)this.worldObj)).withLootedEntity(this).withDamageSource(source); if (wasRecentlyHit && this.attackingPlayer != null) { lootcontext$builder = lootcontext$builder.withPlayer(this.attackingPlayer).withLuck(this.attackingPlayer.getLuck()); } for (ItemStack itemstack : loottable.generateLootForPools(this.deathLootTableSeed == 0L ? this.rand : new Random(this.deathLootTableSeed), lootcontext$builder.build())) { this.entityDropItem(itemstack, 0.0F); } this.dropEquipment(wasRecentlyHit, lootingModifier); } else { super.dropLoot(wasRecentlyHit, lootingModifier, source); } }
/** * Converts a LootTable to a list of possible drops, only looks for Item and metadata. * @param table the loot table to get items from * @return a LinkedList of the stacks in the loot table */ public static List<ItemStack> tableToItemStacks(LootTable table){ List<ItemStack> stacks = new LinkedList<>(); for(LootPool p:getPools(table)){ for(LootEntry entry:getEntries(p)){ if(entry instanceof LootEntryItem){ LootEntryItem ei = (LootEntryItem)entry; Item item = getItem(ei); LootFunction[] functs = getFunctions(ei); boolean metaSet = false; for(LootFunction func:functs){ if(func instanceof SetMetadata){ metaSet=true; RandomValueRange range = (RandomValueRange)ReflectionHelper.getPrivateValue(SetMetadata.class, (SetMetadata)func, "metaRange","field_186573_b"); int meta = MathHelper.floor(range.getMin()); stacks.add(new ItemStack(item,1,meta)); } } if(!metaSet)stacks.add(new ItemStack(item)); } /* won't bother with this case for now else if(entry instanceof LootEntryTable){ //restart with that table ResourceLocation location = (ResourceLocation) ReflectionHelper.getPrivateValue(LootEntryTable.class, (LootEntryTable)entry, "table","field_186371_a"); } */ } } return stacks; }
@SubscribeEvent public void onLootTableLoad(LootTableLoadEvent evt) { LootTable table = evt.getTable(); FyrestonePool lp = new FyrestonePool(); if (evt.getName() == LootTableList.CHESTS_SIMPLE_DUNGEON) { lp.addItem(createLootEntry(ItemRegister.itemMysticalOrb, 0, 1, 1, 0.25)); } if (evt.getName() == LootTableList.CHESTS_JUNGLE_TEMPLE) { lp.addItem(createLootEntry(ItemRegister.itemMysticalOrb, 0, 1, 1, 0.3)); } if (evt.getName() == LootTableList.CHESTS_VILLAGE_BLACKSMITH) { lp.addItem(createLootEntry(ItemRegister.itemMysticalOrb, 0, 1, 1, 0.25)); } if (evt.getName() == LootTableList.ENTITIES_ENDERMAN) { lp.addItem(createLootEntry(ItemRegister.itemMysticalOrb, 0, 1, 1, 0.1)); } if (!lp.isEmpty()) { table.addPool(lp); } }
@SubscribeEvent public static void onLootTableLoad(LootTableLoadEvent e) { final ResourceLocation rl = e.getName(); if (rl.equals(LootTableList.CHESTS_VILLAGE_BLACKSMITH) || rl.equals(LootTableList.CHESTS_ABANDONED_MINESHAFT) || rl.equals(LootTableList.CHESTS_JUNGLE_TEMPLE)) { final LootTable lt = e.getTable(); LootPool lp = lt.getPool("pool0"); if (lp == null) lp = lt.getPool("main"); if (lp != null) { lp.addEntry(new LootEntryItem(MCFluxResources.UPCHIP, 20, 0, new LootFunction[0], new LootCondition[0], "mcflux:loot/upchip")); } } }
@SubscribeEvent(priority= EventPriority.NORMAL) public void onLootLoad(LootTableLoadEvent event){ if(event.getName().getResourceDomain().equals("minecraft") == false) return; // loot table from another mod (too fancy for ALT) String categoryAndEntry = event.getName().getResourcePath(); // e.g. "chests/abandoned_mineshaft" if(categoryAndEntry.contains("/") == false) return; // not valid // category is "chests" or "entities" String category = categoryAndEntry.substring(0,categoryAndEntry.indexOf('/')); // entry is the name of the loot table (e.g. "abandoned_mineshaft") String entry = categoryAndEntry.substring(categoryAndEntry.indexOf('/')+1,categoryAndEntry.length()); final Map<String, Map<String, List<LootPool>>> additional_loot = AdditionalLootTables.getAdditionalLootTables(); if(additional_loot.containsKey(category) && additional_loot.get(category).containsKey(entry)){ List<LootPool> pools = additional_loot.get(category).get(entry); if(pools == null || pools.isEmpty()) return; // nothing to add if(event.getTable() == null) { // table was removed by another mod FMLLog.info("%s: creating new loot table %s", MODID, event.getName()); event.setTable(new LootTable(pools.toArray(new LootPool[pools.size()]))); } else { // table exists, add pools to it FMLLog.info("%s: adding more loot to loot table %s", MODID, event.getName()); for (LootPool pool : pools) { event.getTable().addPool(pool); } } } }
@SubscribeEvent public void onLootTableLoad (LootTableLoadEvent event) { final LootTable table = event.getTable(); if (skeletonDropDust && event.getName().equals(LootTableList.ENTITIES_WITHER_SKELETON)) { final LootPool pool1 = table.getPool("pool1"); if (pool1 != null) { pool1.addEntry(new LootEntryItem(itemMaterial, dustDropWeight, 0, new LootFunction[0], new LootCondition[0], DarkUtils.MOD_ID + ":wither_dust")); } } }
/** * Adds loot to the minecart's contents. */ public void addLoot(@Nullable EntityPlayer player) { if (this.lootTable != null) { LootTable loottable = this.worldObj.getLootTableManager().getLootTableFromLocation(this.lootTable); this.lootTable = null; Random random; if (this.lootTableSeed == 0L) { random = new Random(); } else { random = new Random(this.lootTableSeed); } LootContext.Builder lootcontext$builder = new LootContext.Builder((WorldServer)this.worldObj); if (player != null) { lootcontext$builder.withLuck(player.getLuck()); } loottable.fillInventory(this, random, lootcontext$builder.build()); } }
public static void addItemToTable(LootTable table, String poolName, Item item, int weight, int minQuantity, int maxQuantity) { addItemToTable(table, poolName, item, weight, 1, createCountFunction(minQuantity, maxQuantity)); }
public static void addItemToTable(LootTable table, String poolName, Item item, int weight, int quality, LootFunction... functions) { addItemToTable(table, poolName, item, weight, quality, functions, NO_CONDITIONS); }
public static void addItemToTable(LootTable table, String poolName, Item item, int weight, int quality, LootFunction[] functions, LootCondition[] conditions) { table.getPool("main").addEntry(new LootEntryItem(item, weight, quality, functions, conditions, item.getRegistryName().toString())); }
public LootTableLoadEvent(ResourceLocation name, LootTable table) { this.name = name; this.table = table; }
public LootTable getTable() { return this.table; }
public void setTable(LootTable table) { this.table = table; }
public static List<LootPool> getPools(LootTable table) { return ReflectionHelper.getPrivateValue(LootTable.class, table, "pools", "field_186466_c"); }
public static void createPoolIfNotExists(LootTable lootTable, String poolId) { if (poolId.equals(VANILLA_LOOT_POOL_ID) || lootTable.getPool(poolId) != null) return; lootTable.addPool(new LootPool(new LootEntry[] {}, new LootCondition[] {}, new RandomValueRange(1), new RandomValueRange(0), poolId)); }
@Override public void update() { World world = this.getWorld(); Random rand = world.rand; if (rand.nextDouble() < this.getFishSpeed() && isValidPosition() && isEquipmentValid() && world instanceof WorldServer && world != null && world.getWorldTime() % Const.TICKS_PER_SEC == 0) { LootContext.Builder lootcontext$builder = new LootContext.Builder((WorldServer) world); int luck = EnchantmentHelper.getEnchantmentLevel(Enchantments.LUCK_OF_THE_SEA, this.getStackInSlot(toolSlot)); lootcontext$builder.withLuck((float) luck); // java.lang.NullPointerException: Ticking block entity at com.lothrazar.cyclicmagic.block.tileentity.TileEntityFishing.func_73660_a(TileEntityFishing.java:58) LootTableManager loot = world.getLootTableManager(); if (loot == null) { return; } LootTable table = loot.getLootTableFromLocation(LootTableList.GAMEPLAY_FISHING); if (table == null) { return; } LootContext context = lootcontext$builder.build(); if (context == null) { return; } for (ItemStack itemstack : table.generateLootForPools(rand, context)) { UtilParticle.spawnParticle(world, EnumParticleTypes.WATER_WAKE, pos.up()); //damage phase. int mending = EnchantmentHelper.getEnchantmentLevel(Enchantments.MENDING, this.getStackInSlot(toolSlot)); if (mending == 0) { damageTool(); } else { if (rand.nextDouble() < 0.25) {//25% chance damage damageTool(); } else if (rand.nextDouble() < 0.60) {//60-25 = 40 chance repair attemptRepairTool(); } //else do nothing, leave it flat. mimics getting damaged and repaired right away } //loot phase this.sendOutputItem(itemstack); } } }