Skip to content

Commit a66521f

Browse files
committed
Bad compile fix for CraftItemType for compostable
1 parent e6b568e commit a66521f

1 file changed

Lines changed: 30 additions & 3 deletions

File tree

‎paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemType.java‎

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,19 @@
1212
import net.minecraft.core.component.DataComponents;
1313
import net.minecraft.core.registries.Registries;
1414
import net.minecraft.server.level.ServerLevel;
15+
import net.minecraft.util.random.Weighted;
1516
import net.minecraft.world.item.BlockItem;
1617
import net.minecraft.world.item.Item;
1718
import net.minecraft.world.item.ItemStackTemplate;
19+
import net.minecraft.world.item.component.Compostable;
1820
import net.minecraft.world.item.component.CookingFuel;
1921
import net.minecraft.world.item.component.ItemAttributeModifiers;
2022
import net.minecraft.world.level.storage.loot.LootContext;
2123
import net.minecraft.world.level.storage.loot.LootParams;
2224
import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
25+
import net.minecraft.world.level.storage.loot.providers.number.ints.ContextIntProvider;
2326
import net.minecraft.world.level.storage.loot.providers.number.ints.ResolvableInt;
27+
import net.minecraft.world.level.storage.loot.providers.number.ints.WeightedListValue;
2428
import org.bukkit.Bukkit;
2529
import org.bukkit.Material;
2630
import org.bukkit.Registry;
@@ -193,9 +197,32 @@ public boolean isCompostable() {
193197

194198
@Override
195199
public float getCompostChance() {
196-
Preconditions.checkArgument(this.isCompostable(), "The item type " + this.getKey() + " is not compostable");
197-
// TODO - snapshot - how can get the chance if the component require context?
198-
return ComposterBlock.COMPOSTABLES.getFloat(this.getHandle());
200+
// TODO - snapshot - this cover vanilla but custom ones can break this.. maybe better deprecate this...
201+
final net.minecraft.world.item.ItemStack stack = new net.minecraft.world.item.ItemStack(this.getHandle());
202+
Compostable compostable = stack.get(DataComponents.COMPOSTABLE);
203+
Preconditions.checkArgument(compostable != null, "The item type " + this.getKey() + " is not compostable");
204+
if (compostable.layers() instanceof ResolvableInt.Constant) {
205+
// Constant (ex: [minecraft:compostable={layers:10}]) case it's the 100% of cases add layers to the composter.
206+
return 1;
207+
} else if (compostable.layers() instanceof ResolvableInt.Reference reference) {
208+
final ServerLevel serverLevel = ((CraftWorld) Bukkit.getWorlds().getFirst()).getHandle();
209+
final LootContext lootContext = new LootContext.Builder(
210+
new LootParams.Builder(serverLevel).create(LootContextParamSets.EMPTY)
211+
).create(Optional.empty());
212+
213+
return reference.getProvider(lootContext)
214+
.map(contextIntProvider -> {
215+
if (contextIntProvider instanceof WeightedListValue(
216+
net.minecraft.util.random.WeightedList<Holder<ContextIntProvider>> distribution
217+
)) {
218+
return distribution.unwrap().stream().mapToInt(Weighted::weight).max().orElse(0);
219+
} else {
220+
return 1;
221+
}
222+
}).orElse(0);
223+
}
224+
225+
return 0;
199226
}
200227

201228
@Override

0 commit comments

Comments
 (0)