Skip to content

Commit

Permalink
feat(despoil-loot): only guarantee a loot drop for the despoiling Sickle
Browse files Browse the repository at this point in the history
  • Loading branch information
Elenterius committed Jun 16, 2023
1 parent c2f3503 commit cc5707b
Showing 1 changed file with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public LootItemCondition[] getConditions() {
return conditions;
}

private static int getDespoilLevel(LootContext lootContext) {
protected static int getDespoilLevel(LootContext lootContext) {
Entity killer = lootContext.getParamOrNull(LootContextParams.KILLER_ENTITY);
if (killer instanceof LivingEntity livingEntity) {
return ModEnchantments.DESPOIL.get().getSlotItems(livingEntity).values().stream()
Expand All @@ -92,10 +92,19 @@ private static int getDespoilLevel(LootContext lootContext) {
return 0;
}

private static int getDespoilLevel(ItemStack stack) {
protected static int getDespoilLevel(ItemStack stack) {
return stack.getEnchantmentLevel(ModEnchantments.DESPOIL.get());
}

protected static boolean isHolding(LootContext lootContext, Item item) {
Entity killer = lootContext.getParamOrNull(LootContextParams.KILLER_ENTITY);
if (killer instanceof LivingEntity livingEntity) {
return livingEntity.isHolding(item);
}

return false;
}

protected DynamicLootTable buildLootTable(LivingEntity livingEntity) {
EntityType<?> type = livingEntity.getType();
boolean hasFangs = type.is(ModEntityTags.SHARP_FANG);
Expand Down Expand Up @@ -128,16 +137,20 @@ protected DynamicLootTable buildLootTable(LivingEntity livingEntity) {
@Override
protected ObjectArrayList<ItemStack> doApply(ObjectArrayList<ItemStack> generatedLoot, LootContext context) {
if (context.getParamOrNull(LootContextParams.THIS_ENTITY) instanceof LivingEntity victim) {
int despoilLevel = getDespoilLevel(context);
int lootingLevel = context.getLootingModifier();
final int despoilLevel = getDespoilLevel(context);

if (despoilLevel > 0) {
RandomSource random = context.getRandom();
final int lootingLevel = context.getLootingModifier();

DynamicLootTable lootTable = buildLootTable(victim);
if (lootTable.isEmpty()) return generatedLoot;

if (!isHolding(context, ModItems.DESPOIL_SICKLE.get())) {
lootTable.add(EMPTY, 15); //only the despoil sickle has a 100% guarantee to drop despoil loot
}

int diceRolls = despoilLevel;
for (; diceRolls > 0; diceRolls--) {
lootTable.getRandomItemStack(random, lootingLevel).filter(stack -> !stack.isEmpty()).ifPresent(generatedLoot::add);
for (int rolls = despoilLevel; rolls > 0; rolls--) {
lootTable.getRandomItemStack(context.getRandom(), lootingLevel).filter(stack -> !stack.isEmpty()).ifPresent(generatedLoot::add);
}
}
}
Expand Down

0 comments on commit cc5707b

Please sign in to comment.