-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
614 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/java/net/frozenblock/lib/loot/LootTableModifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright (C) 2025 FrozenBlock | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package net.frozenblock.lib.loot; | ||
|
||
import net.fabricmc.fabric.api.loot.v3.LootTableEvents; | ||
import net.minecraft.resources.ResourceKey; | ||
import net.minecraft.world.level.storage.loot.LootTable; | ||
|
||
public class LootTableModifier { | ||
|
||
public static void editTable(ResourceKey<LootTable> targetLootTable, boolean requiresBuiltIn, Edit listener) { | ||
LootTableEvents.Replace modification = (id, lootTable, source, registries) -> { | ||
if ((requiresBuiltIn && !source.isBuiltin()) || !targetLootTable.equals(id)) return null; | ||
MutableLootTable mutableLootTable = new MutableLootTable(lootTable); | ||
listener.editLootTable(id, mutableLootTable); | ||
return mutableLootTable.build(); | ||
}; | ||
|
||
LootTableEvents.REPLACE.register(modification); | ||
} | ||
|
||
@FunctionalInterface | ||
public interface Edit { | ||
/** | ||
* Edits loot tables. | ||
* | ||
* @param id The loot table key. | ||
* @param mutableLootTable The mutable copy of the loot table. | ||
*/ | ||
void editLootTable(ResourceKey<LootTable> id, MutableLootTable mutableLootTable); | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
src/main/java/net/frozenblock/lib/loot/MutableLootItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Copyright (C) 2025 FrozenBlock | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package net.frozenblock.lib.loot; | ||
|
||
import java.util.List; | ||
import net.minecraft.core.Holder; | ||
import net.minecraft.world.item.Item; | ||
import net.minecraft.world.level.ItemLike; | ||
import net.minecraft.world.level.storage.loot.entries.LootItem; | ||
import net.minecraft.world.level.storage.loot.functions.LootItemFunction; | ||
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class MutableLootItem { | ||
public final List<LootItemCondition> conditions; | ||
public int weight; | ||
public int quality; | ||
public final List<LootItemFunction> functions; | ||
public Holder<Item> item; | ||
|
||
public MutableLootItem(LootItem original) { | ||
this.conditions = original.conditions; | ||
this.weight = original.weight; | ||
this.quality = original.quality; | ||
this.functions = original.functions; | ||
this.item = original.item; | ||
} | ||
|
||
public MutableLootItem(Holder<Item> item, int weight, int quality, List<LootItemCondition> conditions, List<LootItemFunction> functions) { | ||
this.item = item; | ||
this.conditions = conditions; | ||
this.weight = weight; | ||
this.quality = quality; | ||
this.functions = functions; | ||
} | ||
|
||
public MutableLootItem(@NotNull ItemLike item, int weight, int quality, List<LootItemCondition> conditions, List<LootItemFunction> functions) { | ||
this.item = item.asItem().builtInRegistryHolder(); | ||
this.conditions = conditions; | ||
this.weight = weight; | ||
this.quality = quality; | ||
this.functions = functions; | ||
} | ||
|
||
public LootItem build() { | ||
return new LootItem(item, weight, quality, conditions, functions); | ||
} | ||
|
||
public int getWeight() { | ||
return weight; | ||
} | ||
|
||
public void setWeight(int weight) { | ||
this.weight = weight; | ||
} | ||
|
||
public int getQuality() { | ||
return quality; | ||
} | ||
|
||
public void setQuality(int quality) { | ||
this.quality = quality; | ||
} | ||
|
||
public Holder<Item> getItemHolder() { | ||
return item; | ||
} | ||
|
||
public Item getItem() { | ||
return item.value(); | ||
} | ||
|
||
public void setItem(Holder<Item> item) { | ||
this.item = item; | ||
} | ||
|
||
public void setItem(@NotNull ItemLike item) { | ||
this.item = item.asItem().builtInRegistryHolder(); | ||
} | ||
} |
Oops, something went wrong.