From 3c2aa6649aafe286817b34cbe4ca7b6a91fbfb1b Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Wed, 26 Oct 2022 18:40:06 +0700 Subject: [PATCH] KilledByRealPlayerOrIndirectly Fixes #202 --- .../mc/otm/datagen/loot/LootModifiersData.kt | 3 +- .../data/KilledByRealPlayerOrIndirectly.kt | 57 +++++++++++++++++++ .../mc/otm/registry/MLootItemConditions.kt | 3 + 3 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/ru/dbotthepony/mc/otm/data/KilledByRealPlayerOrIndirectly.kt diff --git a/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/loot/LootModifiersData.kt b/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/loot/LootModifiersData.kt index d96d09c95..da2442a1d 100644 --- a/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/loot/LootModifiersData.kt +++ b/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/loot/LootModifiersData.kt @@ -13,6 +13,7 @@ import ru.dbotthepony.mc.otm.data.IRandomizableItem import ru.dbotthepony.mc.otm.data.ItemInInventoryCondition import ru.dbotthepony.mc.otm.data.KilledByRealPlayer import ru.dbotthepony.mc.otm.data.BasicLootAppender +import ru.dbotthepony.mc.otm.data.KilledByRealPlayerOrIndirectly import ru.dbotthepony.mc.otm.data.PlainLootAppender import ru.dbotthepony.mc.otm.data.RandomizableItemLootAppender import ru.dbotthepony.mc.otm.registry.MItems @@ -127,7 +128,7 @@ fun addLootModifiers(it: LootModifiers) { arrayOf( LootTableIdCondition(EntityType.ENDERMAN.defaultLootTable), HasExosuitCondition.INVERTED, - KilledByRealPlayer, + KilledByRealPlayerOrIndirectly, ChanceWithPlaytimeCondition( minPlaytime = 20 * 60 * 10, maxPlaytime = 20 * 60 * 120, diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/data/KilledByRealPlayerOrIndirectly.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/data/KilledByRealPlayerOrIndirectly.kt new file mode 100644 index 000000000..99bd4b5b3 --- /dev/null +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/data/KilledByRealPlayerOrIndirectly.kt @@ -0,0 +1,57 @@ +package ru.dbotthepony.mc.otm.data + +import com.google.gson.JsonDeserializationContext +import com.google.gson.JsonObject +import com.google.gson.JsonSerializationContext +import net.minecraft.world.entity.OwnableEntity +import net.minecraft.world.level.storage.loot.LootContext +import net.minecraft.world.level.storage.loot.Serializer +import net.minecraft.world.level.storage.loot.parameters.LootContextParams +import net.minecraft.world.level.storage.loot.predicates.InvertedLootItemCondition +import net.minecraft.world.level.storage.loot.predicates.LootItemCondition +import net.minecraft.world.level.storage.loot.predicates.LootItemConditionType +import net.minecraftforge.common.util.FakePlayer +import ru.dbotthepony.mc.otm.NULLABLE_MINECRAFT_SERVER +import ru.dbotthepony.mc.otm.registry.MLootItemConditions + +object KilledByRealPlayerOrIndirectly : LootItemCondition, Serializer, LootItemCondition.Builder { + override fun test(t: LootContext): Boolean { + if (t.hasParam(LootContextParams.LAST_DAMAGE_PLAYER) && t[LootContextParams.LAST_DAMAGE_PLAYER] !is FakePlayer) { + return true + } + + if (t.hasParam(LootContextParams.KILLER_ENTITY)) { + val killer = t[LootContextParams.KILLER_ENTITY] as? OwnableEntity + + if (killer != null) { + val owner = killer.owner + + if (owner != null && owner !is FakePlayer) { + return true + } + + val ply = NULLABLE_MINECRAFT_SERVER?.playerList?.getPlayer(killer.ownerUUID ?: return false) + return ply != null && ply !is FakePlayer + } + } + + return false + } + + override fun getType(): LootItemConditionType { + return MLootItemConditions.KILLED_BY_REAL_PLAYER_OR_INDIRECTLY + } + + override fun serialize(p_79325_: JsonObject, p_79326_: KilledByRealPlayerOrIndirectly, p_79327_: JsonSerializationContext) { + } + + override fun deserialize(p_79323_: JsonObject, p_79324_: JsonDeserializationContext): KilledByRealPlayerOrIndirectly { + return this + } + + override fun build(): LootItemCondition { + return this + } + + val INVERTED: LootItemCondition = InvertedLootItemCondition.invert(this).build() +} diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MLootItemConditions.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MLootItemConditions.kt index 0c63f1de8..c8dd6339a 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MLootItemConditions.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MLootItemConditions.kt @@ -10,10 +10,12 @@ import ru.dbotthepony.mc.otm.data.ChanceWithPlaytimeCondition import ru.dbotthepony.mc.otm.data.HasExosuitCondition import ru.dbotthepony.mc.otm.data.ItemInInventoryCondition import ru.dbotthepony.mc.otm.data.KilledByRealPlayer +import ru.dbotthepony.mc.otm.data.KilledByRealPlayerOrIndirectly object MLootItemConditions { val HAS_EXOSUIT = LootItemConditionType(HasExosuitCondition) val KILLED_BY_REAL_PLAYER = LootItemConditionType(KilledByRealPlayer) + val KILLED_BY_REAL_PLAYER_OR_INDIRECTLY = LootItemConditionType(KilledByRealPlayerOrIndirectly) val CHANCE_WITH_PLAYTIME = LootItemConditionType(ChanceWithPlaytimeCondition) val ITEM_IN_INVENTORY = LootItemConditionType(ItemInInventoryCondition) @@ -23,6 +25,7 @@ object MLootItemConditions { Registry.LOOT_CONDITION_TYPE.register(ResourceLocation(OverdriveThatMatters.MOD_ID, "chance_with_playtime"), CHANCE_WITH_PLAYTIME) Registry.LOOT_CONDITION_TYPE.register(ResourceLocation(OverdriveThatMatters.MOD_ID, "item_in_inventory"), ITEM_IN_INVENTORY) Registry.LOOT_CONDITION_TYPE.register(ResourceLocation(OverdriveThatMatters.MOD_ID, "killed_by_real_player"), KILLED_BY_REAL_PLAYER) + Registry.LOOT_CONDITION_TYPE.register(ResourceLocation(OverdriveThatMatters.MOD_ID, "killed_by_real_player_or_indirectly"), KILLED_BY_REAL_PLAYER_OR_INDIRECTLY) } } }