Skip to content

Commit 3a5ea89

Browse files
committed
treasure hunter
1 parent e86dd5a commit 3a5ea89

File tree

4 files changed

+44
-75
lines changed

4 files changed

+44
-75
lines changed

Diff for: src/main/java/de/dafuqs/spectrum/helpers/enchantments/TreasureHunterHelper.java

-35
This file was deleted.

Diff for: src/main/java/de/dafuqs/spectrum/loot/SpectrumLootPoolModifiers.java

+16-14
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import net.minecraft.loot.context.*;
1818
import net.minecraft.loot.entry.*;
1919
import net.minecraft.loot.provider.number.*;
20+
import net.minecraft.predicate.*;
2021
import net.minecraft.predicate.entity.*;
2122
import net.minecraft.registry.*;
2223
import net.minecraft.registry.entry.*;
@@ -220,20 +221,21 @@ public static void setup() {
220221
}
221222

222223
private static RegistryEntry.Reference<Enchantment> getTreasureHunter(RegistryWrapper.WrapperLookup wrapperLookup) {
223-
RegistryEntry.Reference<Enchantment> enchant;
224-
RegistryWrapper.Impl<Enchantment> registryWrapper = wrapperLookup.getWrapperOrThrow(RegistryKeys.ENCHANTMENT);
225-
enchant = registryWrapper.getOrThrow(SpectrumEnchantments.TREASURE_HUNTER);
226-
return enchant;
224+
RegistryWrapper.Impl<Enchantment> wrapper = wrapperLookup.getWrapperOrThrow(RegistryKeys.ENCHANTMENT);
225+
return wrapper.getOrThrow(SpectrumEnchantments.TREASURE_HUNTER);
227226
}
228227

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

233235
private static LootPool getLootPool(RegistryEntry.Reference<Enchantment> enchantment, TreasureHunterDropDefinition dropDefinition) {
234236
return new LootPool.Builder()
235237
.rolls(ConstantLootNumberProvider.create(1))
236-
.conditionally(Lootconditionbuilder(enchantment, dropDefinition.chancePerLevel).build())
238+
.conditionally(treasureHunter(enchantment, dropDefinition.chancePerLevel).build())
237239
.apply(GrantAdvancementLootFunction.builder(LootContext.EntityTarget.ATTACKING_PLAYER, List.of(SpectrumCommon.locate("mob_head"), dropDefinition.advancementUnlockId)))
238240
.with(ItemEntry.builder(dropDefinition.drop).build())
239241
.build();
@@ -242,7 +244,7 @@ private static LootPool getLootPool(RegistryEntry.Reference<Enchantment> enchant
242244
private static LootPool getFoxLootPool(RegistryEntry.Reference<Enchantment> enchantment, FoxEntity.Type foxType, TreasureHunterDropDefinition dropDefinition) {
243245
return new LootPool.Builder()
244246
.rolls(ConstantLootNumberProvider.create(1))
245-
.conditionally(Lootconditionbuilder(enchantment, dropDefinition.chancePerLevel).build())
247+
.conditionally(treasureHunter(enchantment, dropDefinition.chancePerLevel).build())
246248
.conditionally(EntityPropertiesLootCondition.builder(LootContext.EntityTarget.THIS, EntityPredicate.Builder.create().typeSpecific(EntitySubPredicateTypes.FOX.createPredicate(foxType)).build()).build())
247249
.apply(GrantAdvancementLootFunction.builder(LootContext.EntityTarget.ATTACKING_PLAYER, List.of(SpectrumCommon.locate("mob_head"), dropDefinition.advancementUnlockId)))
248250
.with(ItemEntry.builder(dropDefinition.drop).build())
@@ -252,7 +254,7 @@ private static LootPool getFoxLootPool(RegistryEntry.Reference<Enchantment> ench
252254
private static LootPool getMooshroomLootPool(RegistryEntry.Reference<Enchantment> enchantment, MooshroomEntity.Type mooshroomType, TreasureHunterDropDefinition dropDefinition) {
253255
return new LootPool.Builder()
254256
.rolls(ConstantLootNumberProvider.create(1))
255-
.conditionally(Lootconditionbuilder(enchantment, dropDefinition.chancePerLevel).build())
257+
.conditionally(treasureHunter(enchantment, dropDefinition.chancePerLevel).build())
256258
.conditionally(EntityPropertiesLootCondition.builder(LootContext.EntityTarget.THIS, EntityPredicate.Builder.create().typeSpecific(EntitySubPredicateTypes.MOOSHROOM.createPredicate(mooshroomType)).build()).build())
257259
.apply(GrantAdvancementLootFunction.builder(LootContext.EntityTarget.ATTACKING_PLAYER, List.of(SpectrumCommon.locate("mob_head"), dropDefinition.advancementUnlockId)))
258260
.with(ItemEntry.builder(dropDefinition.drop).build())
@@ -262,7 +264,7 @@ private static LootPool getMooshroomLootPool(RegistryEntry.Reference<Enchantment
262264
private static LootPool getShulkerLootPool(RegistryEntry.Reference<Enchantment> enchantment, @Nullable DyeColor dyeColor, TreasureHunterDropDefinition dropDefinition) {
263265
return new LootPool.Builder()
264266
.rolls(ConstantLootNumberProvider.create(1))
265-
.conditionally(Lootconditionbuilder(enchantment, dropDefinition.chancePerLevel).build())
267+
.conditionally(treasureHunter(enchantment, dropDefinition.chancePerLevel).build())
266268
.conditionally(EntityPropertiesLootCondition.builder(LootContext.EntityTarget.THIS, EntityPredicate.Builder.create().typeSpecific(new ShulkerPredicate(Optional.ofNullable(dyeColor))).build()).build())
267269
.apply(GrantAdvancementLootFunction.builder(LootContext.EntityTarget.ATTACKING_PLAYER, List.of(SpectrumCommon.locate("mob_head"), dropDefinition.advancementUnlockId)))
268270
.with(ItemEntry.builder(dropDefinition.drop).build())
@@ -272,7 +274,7 @@ private static LootPool getShulkerLootPool(RegistryEntry.Reference<Enchantment>
272274
private static LootPool getLizardLootPool(RegistryEntry.Reference<Enchantment> enchantment, InkColor linkColor, TreasureHunterDropDefinition dropDefinition) {
273275
return new LootPool.Builder()
274276
.rolls(ConstantLootNumberProvider.create(1))
275-
.conditionally(Lootconditionbuilder(enchantment, dropDefinition.chancePerLevel).build())
277+
.conditionally(treasureHunter(enchantment, dropDefinition.chancePerLevel).build())
276278
.conditionally(EntityPropertiesLootCondition.builder(LootContext.EntityTarget.THIS, EntityPredicate.Builder.create().typeSpecific(new LizardPredicate(Optional.of(linkColor), Optional.empty(), Optional.empty())).build()).build())
277279
.apply(GrantAdvancementLootFunction.builder(LootContext.EntityTarget.ATTACKING_PLAYER, List.of(SpectrumCommon.locate("mob_head"), dropDefinition.advancementUnlockId)))
278280
.with(ItemEntry.builder(dropDefinition.drop).build())
@@ -282,7 +284,7 @@ private static LootPool getLizardLootPool(RegistryEntry.Reference<Enchantment> e
282284
private static LootPool getAxolotlLootPool(RegistryEntry.Reference<Enchantment> enchantment, AxolotlEntity.Variant variant, TreasureHunterDropDefinition dropDefinition) {
283285
return new LootPool.Builder()
284286
.rolls(ConstantLootNumberProvider.create(1))
285-
.conditionally(Lootconditionbuilder(enchantment, dropDefinition.chancePerLevel).build())
287+
.conditionally(treasureHunter(enchantment, dropDefinition.chancePerLevel).build())
286288
.conditionally(EntityPropertiesLootCondition.builder(LootContext.EntityTarget.THIS, EntityPredicate.Builder.create().typeSpecific(EntitySubPredicateTypes.AXOLOTL.createPredicate(variant)).build()).build())
287289
.apply(GrantAdvancementLootFunction.builder(LootContext.EntityTarget.ATTACKING_PLAYER, List.of(SpectrumCommon.locate("mob_head"), dropDefinition.advancementUnlockId)))
288290
.with(ItemEntry.builder(dropDefinition.drop).build())
@@ -294,7 +296,7 @@ private static LootPool getFrogLootPool(RegistryEntry.Reference<Enchantment> enc
294296

295297
return new LootPool.Builder()
296298
.rolls(ConstantLootNumberProvider.create(1))
297-
.conditionally(Lootconditionbuilder(enchantment, dropDefinition.chancePerLevel).build())
299+
.conditionally(treasureHunter(enchantment, dropDefinition.chancePerLevel).build())
298300
.conditionally(EntityPropertiesLootCondition.builder(LootContext.EntityTarget.THIS, EntityPredicate.Builder.create().typeSpecific(EntitySubPredicateTypes.FROG.createPredicate(RegistryEntryList.of(entry))).build()).build())
299301
.apply(GrantAdvancementLootFunction.builder(LootContext.EntityTarget.ATTACKING_PLAYER, List.of(SpectrumCommon.locate("mob_head"), dropDefinition.advancementUnlockId)))
300302
.with(ItemEntry.builder(dropDefinition.drop).build())
@@ -304,7 +306,7 @@ private static LootPool getFrogLootPool(RegistryEntry.Reference<Enchantment> enc
304306
private static LootPool getParrotLootPool(RegistryEntry.Reference<Enchantment> enchantment, ParrotEntity.Variant variant, TreasureHunterDropDefinition dropDefinition) {
305307
return new LootPool.Builder()
306308
.rolls(ConstantLootNumberProvider.create(1))
307-
.conditionally(Lootconditionbuilder(enchantment, dropDefinition.chancePerLevel).build())
309+
.conditionally(treasureHunter(enchantment, dropDefinition.chancePerLevel).build())
308310
.conditionally(EntityPropertiesLootCondition.builder(LootContext.EntityTarget.THIS, EntityPredicate.Builder.create().typeSpecific(EntitySubPredicateTypes.PARROT.createPredicate(variant)).build()).build())
309311
.apply(GrantAdvancementLootFunction.builder(LootContext.EntityTarget.ATTACKING_PLAYER, List.of(SpectrumCommon.locate("mob_head"), dropDefinition.advancementUnlockId)))
310312
.with(ItemEntry.builder(dropDefinition.drop).build())

Diff for: src/main/java/de/dafuqs/spectrum/mixin/ServerPlayerEntityMixin.java

-5
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ public abstract class ServerPlayerEntityMixin {
2626
@Unique
2727
private long spectrum$lastGleamingPinTriggerTick = 0;
2828

29-
@Inject(at = @At("HEAD"), method = "onDeath(Lnet/minecraft/entity/damage/DamageSource;)V")
30-
protected void spectrum$dropPlayerHeadWithTreasureHunt(DamageSource source, CallbackInfo ci) {
31-
TreasureHunterHelper.doTreasureHunterForPlayer((ServerPlayerEntity) (Object) this, source);
32-
}
33-
3429
@Inject(at = @At("RETURN"), method = "damage(Lnet/minecraft/entity/damage/DamageSource;F)Z")
3530
public void spectrum$damageReturn(DamageSource source, float amount, CallbackInfoReturnable<Boolean> cir) {
3631
ServerWorld world = this.getServerWorld();

Diff for: src/main/java/de/dafuqs/spectrum/registries/SpectrumEventListeners.java

+28-21
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import net.fabricmc.fabric.api.resource.*;
2525
import net.minecraft.advancement.criterion.*;
2626
import net.minecraft.block.*;
27+
import net.minecraft.component.*;
28+
import net.minecraft.component.type.*;
2729
import net.minecraft.entity.*;
2830
import net.minecraft.entity.damage.*;
2931
import net.minecraft.entity.effect.*;
@@ -338,6 +340,7 @@ public static void register() {
338340
if (entity.getWorld().getLevelProperties().isHardcore() || HardcoreDeathComponent.isInHardcore(player)) {
339341
HardcoreDeathComponent.addHardcoreDeath(player.getServerWorld(), player.getGameProfile());
340342
}
343+
evaluateAndDropPlayerHead(player, damageSource);
341344
}
342345
});
343346

@@ -380,27 +383,31 @@ public Identifier getFabricId() {
380383
});
381384
}
382385

383-
// TODO - Remove this KubeJS specific compat code.
384-
// It could have been so much easier and performant, but KubeJS overrides the ENTIRE recipe manager
385-
// and cancels all sorts of functions at HEAD unconditionally, so Spectrum cannot mixin into it
386-
// public static void injectEnchantmentUpgradeRecipes(MinecraftServer minecraftServer) {
387-
// if (!enchantmentUpgradeRecipesToInject.isEmpty()) {
388-
// ImmutableMap<Identifier, Recipe<?>> collectedRecipes = enchantmentUpgradeRecipesToInject.stream().collect(ImmutableMap.toImmutableMap(EnchantmentUpgradeRecipe::getId, enchantmentUpgradeRecipe -> enchantmentUpgradeRecipe));
389-
// Map<RecipeType<?>, Map<Identifier, Recipe<?>>> recipes = ((RecipeManagerAccessor) minecraftServer.getRecipeManager()).getRecipes();
390-
//
391-
// ArrayList<Recipe<?>> newList = new ArrayList<>();
392-
// for (Map<Identifier, Recipe<?>> r : recipes.values()) {
393-
// newList.addAll(r.values());
394-
// }
395-
// for (Recipe<?> recipe : collectedRecipes.values()) {
396-
// if (!newList.contains(recipe)) {
397-
// newList.add(recipe);
398-
// }
399-
// }
400-
//
401-
// minecraftServer.getRecipeManager().setRecipes(newList);
402-
// }
403-
// }
386+
private static void evaluateAndDropPlayerHead(ServerPlayerEntity player, DamageSource source) {
387+
if (!player.isSpectator()) {
388+
// TODO: Can we evaluate a SpectrumLootPoolModifiers.treasureHunter() here instead?
389+
// code reuse is always nice
390+
ServerWorld serverWorld = player.getServerWorld();
391+
392+
boolean shouldDropHead = source.isIn(SpectrumDamageTypeTags.ALWAYS_DROPS_MOB_HEAD);
393+
if (!shouldDropHead && source.getAttacker() instanceof LivingEntity livingAttacker) {
394+
int damageSourceTreasureHunt = SpectrumEnchantmentHelper.getEquipmentLevel(
395+
serverWorld.getRegistryManager(),
396+
SpectrumEnchantments.TREASURE_HUNTER,
397+
livingAttacker);
398+
399+
shouldDropHead = damageSourceTreasureHunt > 0 && serverWorld.getRandom().nextFloat() < 0.2 * damageSourceTreasureHunt;
400+
}
401+
402+
if (shouldDropHead) {
403+
ItemStack headItemStack = new ItemStack(Items.PLAYER_HEAD);
404+
headItemStack.set(DataComponentTypes.PROFILE, new ProfileComponent(player.getGameProfile()));
405+
406+
ItemEntity headEntity = new ItemEntity(serverWorld, player.getX(), player.getY(), player.getZ(), headItemStack);
407+
serverWorld.spawnEntity(headEntity);
408+
}
409+
}
410+
}
404411

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

0 commit comments

Comments
 (0)