Skip to content

Commit

Permalink
treasure hunter
Browse files Browse the repository at this point in the history
  • Loading branch information
DaFuqs committed Mar 7, 2025
1 parent e86dd5a commit 3a5ea89
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 75 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import net.minecraft.loot.context.*;
import net.minecraft.loot.entry.*;
import net.minecraft.loot.provider.number.*;
import net.minecraft.predicate.*;
import net.minecraft.predicate.entity.*;
import net.minecraft.registry.*;
import net.minecraft.registry.entry.*;
Expand Down Expand Up @@ -220,20 +221,21 @@ public static void setup() {
}

private static RegistryEntry.Reference<Enchantment> getTreasureHunter(RegistryWrapper.WrapperLookup wrapperLookup) {
RegistryEntry.Reference<Enchantment> enchant;
RegistryWrapper.Impl<Enchantment> registryWrapper = wrapperLookup.getWrapperOrThrow(RegistryKeys.ENCHANTMENT);
enchant = registryWrapper.getOrThrow(SpectrumEnchantments.TREASURE_HUNTER);
return enchant;
RegistryWrapper.Impl<Enchantment> wrapper = wrapperLookup.getWrapperOrThrow(RegistryKeys.ENCHANTMENT);
return wrapper.getOrThrow(SpectrumEnchantments.TREASURE_HUNTER);
}

public static LootCondition.Builder Lootconditionbuilder(RegistryEntry.Reference<Enchantment> enchantment, float chance) {
return () -> new RandomChanceWithEnchantedBonusLootCondition(chance, new EnchantmentLevelBasedValue.Linear(chance, chance), enchantment);
public static LootCondition.Builder treasureHunter(RegistryEntry.Reference<Enchantment> enchantment, float chance) {
return AnyOfLootCondition.builder(
DamageSourcePropertiesLootCondition.builder(DamageSourcePredicate.Builder.create().tag(TagPredicate.expected(SpectrumDamageTypeTags.ALWAYS_DROPS_MOB_HEAD))),
() -> new RandomChanceWithEnchantedBonusLootCondition(0.0F, new EnchantmentLevelBasedValue.Linear(chance + chance, chance), enchantment)
);
}

private static LootPool getLootPool(RegistryEntry.Reference<Enchantment> enchantment, TreasureHunterDropDefinition dropDefinition) {
return new LootPool.Builder()
.rolls(ConstantLootNumberProvider.create(1))
.conditionally(Lootconditionbuilder(enchantment, dropDefinition.chancePerLevel).build())
.conditionally(treasureHunter(enchantment, dropDefinition.chancePerLevel).build())
.apply(GrantAdvancementLootFunction.builder(LootContext.EntityTarget.ATTACKING_PLAYER, List.of(SpectrumCommon.locate("mob_head"), dropDefinition.advancementUnlockId)))
.with(ItemEntry.builder(dropDefinition.drop).build())
.build();
Expand All @@ -242,7 +244,7 @@ private static LootPool getLootPool(RegistryEntry.Reference<Enchantment> enchant
private static LootPool getFoxLootPool(RegistryEntry.Reference<Enchantment> enchantment, FoxEntity.Type foxType, TreasureHunterDropDefinition dropDefinition) {
return new LootPool.Builder()
.rolls(ConstantLootNumberProvider.create(1))
.conditionally(Lootconditionbuilder(enchantment, dropDefinition.chancePerLevel).build())
.conditionally(treasureHunter(enchantment, dropDefinition.chancePerLevel).build())
.conditionally(EntityPropertiesLootCondition.builder(LootContext.EntityTarget.THIS, EntityPredicate.Builder.create().typeSpecific(EntitySubPredicateTypes.FOX.createPredicate(foxType)).build()).build())
.apply(GrantAdvancementLootFunction.builder(LootContext.EntityTarget.ATTACKING_PLAYER, List.of(SpectrumCommon.locate("mob_head"), dropDefinition.advancementUnlockId)))
.with(ItemEntry.builder(dropDefinition.drop).build())
Expand All @@ -252,7 +254,7 @@ private static LootPool getFoxLootPool(RegistryEntry.Reference<Enchantment> ench
private static LootPool getMooshroomLootPool(RegistryEntry.Reference<Enchantment> enchantment, MooshroomEntity.Type mooshroomType, TreasureHunterDropDefinition dropDefinition) {
return new LootPool.Builder()
.rolls(ConstantLootNumberProvider.create(1))
.conditionally(Lootconditionbuilder(enchantment, dropDefinition.chancePerLevel).build())
.conditionally(treasureHunter(enchantment, dropDefinition.chancePerLevel).build())
.conditionally(EntityPropertiesLootCondition.builder(LootContext.EntityTarget.THIS, EntityPredicate.Builder.create().typeSpecific(EntitySubPredicateTypes.MOOSHROOM.createPredicate(mooshroomType)).build()).build())
.apply(GrantAdvancementLootFunction.builder(LootContext.EntityTarget.ATTACKING_PLAYER, List.of(SpectrumCommon.locate("mob_head"), dropDefinition.advancementUnlockId)))
.with(ItemEntry.builder(dropDefinition.drop).build())
Expand All @@ -262,7 +264,7 @@ private static LootPool getMooshroomLootPool(RegistryEntry.Reference<Enchantment
private static LootPool getShulkerLootPool(RegistryEntry.Reference<Enchantment> enchantment, @Nullable DyeColor dyeColor, TreasureHunterDropDefinition dropDefinition) {
return new LootPool.Builder()
.rolls(ConstantLootNumberProvider.create(1))
.conditionally(Lootconditionbuilder(enchantment, dropDefinition.chancePerLevel).build())
.conditionally(treasureHunter(enchantment, dropDefinition.chancePerLevel).build())
.conditionally(EntityPropertiesLootCondition.builder(LootContext.EntityTarget.THIS, EntityPredicate.Builder.create().typeSpecific(new ShulkerPredicate(Optional.ofNullable(dyeColor))).build()).build())
.apply(GrantAdvancementLootFunction.builder(LootContext.EntityTarget.ATTACKING_PLAYER, List.of(SpectrumCommon.locate("mob_head"), dropDefinition.advancementUnlockId)))
.with(ItemEntry.builder(dropDefinition.drop).build())
Expand All @@ -272,7 +274,7 @@ private static LootPool getShulkerLootPool(RegistryEntry.Reference<Enchantment>
private static LootPool getLizardLootPool(RegistryEntry.Reference<Enchantment> enchantment, InkColor linkColor, TreasureHunterDropDefinition dropDefinition) {
return new LootPool.Builder()
.rolls(ConstantLootNumberProvider.create(1))
.conditionally(Lootconditionbuilder(enchantment, dropDefinition.chancePerLevel).build())
.conditionally(treasureHunter(enchantment, dropDefinition.chancePerLevel).build())
.conditionally(EntityPropertiesLootCondition.builder(LootContext.EntityTarget.THIS, EntityPredicate.Builder.create().typeSpecific(new LizardPredicate(Optional.of(linkColor), Optional.empty(), Optional.empty())).build()).build())
.apply(GrantAdvancementLootFunction.builder(LootContext.EntityTarget.ATTACKING_PLAYER, List.of(SpectrumCommon.locate("mob_head"), dropDefinition.advancementUnlockId)))
.with(ItemEntry.builder(dropDefinition.drop).build())
Expand All @@ -282,7 +284,7 @@ private static LootPool getLizardLootPool(RegistryEntry.Reference<Enchantment> e
private static LootPool getAxolotlLootPool(RegistryEntry.Reference<Enchantment> enchantment, AxolotlEntity.Variant variant, TreasureHunterDropDefinition dropDefinition) {
return new LootPool.Builder()
.rolls(ConstantLootNumberProvider.create(1))
.conditionally(Lootconditionbuilder(enchantment, dropDefinition.chancePerLevel).build())
.conditionally(treasureHunter(enchantment, dropDefinition.chancePerLevel).build())
.conditionally(EntityPropertiesLootCondition.builder(LootContext.EntityTarget.THIS, EntityPredicate.Builder.create().typeSpecific(EntitySubPredicateTypes.AXOLOTL.createPredicate(variant)).build()).build())
.apply(GrantAdvancementLootFunction.builder(LootContext.EntityTarget.ATTACKING_PLAYER, List.of(SpectrumCommon.locate("mob_head"), dropDefinition.advancementUnlockId)))
.with(ItemEntry.builder(dropDefinition.drop).build())
Expand All @@ -294,7 +296,7 @@ private static LootPool getFrogLootPool(RegistryEntry.Reference<Enchantment> enc

return new LootPool.Builder()
.rolls(ConstantLootNumberProvider.create(1))
.conditionally(Lootconditionbuilder(enchantment, dropDefinition.chancePerLevel).build())
.conditionally(treasureHunter(enchantment, dropDefinition.chancePerLevel).build())
.conditionally(EntityPropertiesLootCondition.builder(LootContext.EntityTarget.THIS, EntityPredicate.Builder.create().typeSpecific(EntitySubPredicateTypes.FROG.createPredicate(RegistryEntryList.of(entry))).build()).build())
.apply(GrantAdvancementLootFunction.builder(LootContext.EntityTarget.ATTACKING_PLAYER, List.of(SpectrumCommon.locate("mob_head"), dropDefinition.advancementUnlockId)))
.with(ItemEntry.builder(dropDefinition.drop).build())
Expand All @@ -304,7 +306,7 @@ private static LootPool getFrogLootPool(RegistryEntry.Reference<Enchantment> enc
private static LootPool getParrotLootPool(RegistryEntry.Reference<Enchantment> enchantment, ParrotEntity.Variant variant, TreasureHunterDropDefinition dropDefinition) {
return new LootPool.Builder()
.rolls(ConstantLootNumberProvider.create(1))
.conditionally(Lootconditionbuilder(enchantment, dropDefinition.chancePerLevel).build())
.conditionally(treasureHunter(enchantment, dropDefinition.chancePerLevel).build())
.conditionally(EntityPropertiesLootCondition.builder(LootContext.EntityTarget.THIS, EntityPredicate.Builder.create().typeSpecific(EntitySubPredicateTypes.PARROT.createPredicate(variant)).build()).build())
.apply(GrantAdvancementLootFunction.builder(LootContext.EntityTarget.ATTACKING_PLAYER, List.of(SpectrumCommon.locate("mob_head"), dropDefinition.advancementUnlockId)))
.with(ItemEntry.builder(dropDefinition.drop).build())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ public abstract class ServerPlayerEntityMixin {
@Unique
private long spectrum$lastGleamingPinTriggerTick = 0;

@Inject(at = @At("HEAD"), method = "onDeath(Lnet/minecraft/entity/damage/DamageSource;)V")
protected void spectrum$dropPlayerHeadWithTreasureHunt(DamageSource source, CallbackInfo ci) {
TreasureHunterHelper.doTreasureHunterForPlayer((ServerPlayerEntity) (Object) this, source);
}

@Inject(at = @At("RETURN"), method = "damage(Lnet/minecraft/entity/damage/DamageSource;F)Z")
public void spectrum$damageReturn(DamageSource source, float amount, CallbackInfoReturnable<Boolean> cir) {
ServerWorld world = this.getServerWorld();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import net.fabricmc.fabric.api.resource.*;
import net.minecraft.advancement.criterion.*;
import net.minecraft.block.*;
import net.minecraft.component.*;
import net.minecraft.component.type.*;
import net.minecraft.entity.*;
import net.minecraft.entity.damage.*;
import net.minecraft.entity.effect.*;
Expand Down Expand Up @@ -338,6 +340,7 @@ public static void register() {
if (entity.getWorld().getLevelProperties().isHardcore() || HardcoreDeathComponent.isInHardcore(player)) {
HardcoreDeathComponent.addHardcoreDeath(player.getServerWorld(), player.getGameProfile());
}
evaluateAndDropPlayerHead(player, damageSource);
}
});

Expand Down Expand Up @@ -380,27 +383,31 @@ public Identifier getFabricId() {
});
}

// TODO - Remove this KubeJS specific compat code.
// It could have been so much easier and performant, but KubeJS overrides the ENTIRE recipe manager
// and cancels all sorts of functions at HEAD unconditionally, so Spectrum cannot mixin into it
// public static void injectEnchantmentUpgradeRecipes(MinecraftServer minecraftServer) {
// if (!enchantmentUpgradeRecipesToInject.isEmpty()) {
// ImmutableMap<Identifier, Recipe<?>> collectedRecipes = enchantmentUpgradeRecipesToInject.stream().collect(ImmutableMap.toImmutableMap(EnchantmentUpgradeRecipe::getId, enchantmentUpgradeRecipe -> enchantmentUpgradeRecipe));
// Map<RecipeType<?>, Map<Identifier, Recipe<?>>> recipes = ((RecipeManagerAccessor) minecraftServer.getRecipeManager()).getRecipes();
//
// ArrayList<Recipe<?>> newList = new ArrayList<>();
// for (Map<Identifier, Recipe<?>> r : recipes.values()) {
// newList.addAll(r.values());
// }
// for (Recipe<?> recipe : collectedRecipes.values()) {
// if (!newList.contains(recipe)) {
// newList.add(recipe);
// }
// }
//
// minecraftServer.getRecipeManager().setRecipes(newList);
// }
// }
private static void evaluateAndDropPlayerHead(ServerPlayerEntity player, DamageSource source) {
if (!player.isSpectator()) {
// TODO: Can we evaluate a SpectrumLootPoolModifiers.treasureHunter() here instead?
// code reuse is always nice
ServerWorld serverWorld = player.getServerWorld();

boolean shouldDropHead = source.isIn(SpectrumDamageTypeTags.ALWAYS_DROPS_MOB_HEAD);
if (!shouldDropHead && source.getAttacker() instanceof LivingEntity livingAttacker) {
int damageSourceTreasureHunt = SpectrumEnchantmentHelper.getEquipmentLevel(
serverWorld.getRegistryManager(),
SpectrumEnchantments.TREASURE_HUNTER,
livingAttacker);

shouldDropHead = damageSourceTreasureHunt > 0 && serverWorld.getRandom().nextFloat() < 0.2 * damageSourceTreasureHunt;
}

if (shouldDropHead) {
ItemStack headItemStack = new ItemStack(Items.PLAYER_HEAD);
headItemStack.set(DataComponentTypes.PROFILE, new ProfileComponent(player.getGameProfile()));

ItemEntity headEntity = new ItemEntity(serverWorld, player.getX(), player.getY(), player.getZ(), headItemStack);
serverWorld.spawnEntity(headEntity);
}
}
}

public static int getFluidLuminance(Fluid fluid) {
return fluidLuminance.getOrDefault(fluid, 0);
Expand Down

0 comments on commit 3a5ea89

Please sign in to comment.