11package com .bokmcdok .butterflies .data .loot ;
22
3- import com .bokmcdok .butterflies .ButterfliesMod ;
43import com .bokmcdok .butterflies .butterfly_data .ButterflyRegistry ;
4+ import com .bokmcdok .butterflies .butterfly_data .ButterflyTrait ;
55import com .bokmcdok .butterflies .registries .ButterflyEntityTypeRegistry ;
66import com .bokmcdok .butterflies .registries .EntityTypeRegistry ;
77import com .bokmcdok .butterflies .registries .ItemRegistry ;
8- import com .bokmcdok .butterflies .butterfly_data .ButterflyData ;
98import net .minecraft .core .registries .BuiltInRegistries ;
109import net .minecraft .data .loot .EntityLootSubProvider ;
1110import net .minecraft .world .entity .EntityType ;
1211import net .minecraft .world .flag .FeatureFlags ;
1312import net .minecraft .world .item .Items ;
13+ import net .minecraft .world .level .ItemLike ;
1414import net .minecraft .world .level .storage .loot .LootPool ;
1515import net .minecraft .world .level .storage .loot .LootTable ;
1616import net .minecraft .world .level .storage .loot .entries .LootItem ;
17+ import net .minecraft .world .level .storage .loot .functions .LootItemFunction ;
1718import net .minecraft .world .level .storage .loot .functions .LootingEnchantFunction ;
1819import net .minecraft .world .level .storage .loot .functions .SetItemCountFunction ;
1920import net .minecraft .world .level .storage .loot .providers .number .ConstantValue ;
21+ import net .minecraft .world .level .storage .loot .providers .number .NumberProvider ;
2022import net .minecraft .world .level .storage .loot .providers .number .UniformGenerator ;
21- import org .apache .commons .lang3 .StringUtils ;
2223import org .jetbrains .annotations .NotNull ;
2324
24- import java .util .List ;
25- import java .util .Optional ;
25+ import java .util .Set ;
26+ import java .util .stream . Collectors ;
2627import java .util .stream .Stream ;
27- import java .util .stream .StreamSupport ;
2828
2929/**
3030 * Adds loot tables for entities.
3131 */
3232public class ModEntityLootSubProvider extends EntityLootSubProvider {
33- private static final List <String > SILK_SPECIES = List .of (
34- "atlas_chrysalis" ,
35- "carpet_chrysalis" ,
36- "domestic_silk_chrysalis" ,
37- "oak-silk_chrysalis" );
33+
34+ private final Set <EntityType <?>> knownEntityTypes ;
35+ private final Set <EntityType <?>> silkChrysalises ;
3836
3937 /**
4038 * Construction.
4139 */
4240 public ModEntityLootSubProvider () {
4341 super (FeatureFlags .REGISTRY .allFlags ());
42+
43+ this .silkChrysalises = ButterflyRegistry .getButterflyDataCollection ().stream ()
44+ .filter (data -> data .hasTrait (ButterflyTrait .SILK ))
45+ .map (data -> ButterflyEntityTypeRegistry .CHRYSALISES .get (data .butterflyIndex ()).get ())
46+ .collect (Collectors .toUnmodifiableSet ());
47+
48+ this .knownEntityTypes = Stream .concat (
49+ silkChrysalises .stream (),
50+ Stream .of (EntityTypeRegistry .BUTTERFLY_GOLEM .get ())
51+ ).collect (Collectors .toUnmodifiableSet ());
4452 }
4553
4654 /**
4755 * Entry point.
4856 */
4957 @ Override
5058 public void generate () {
51- SILK_SPECIES .stream ()
52- .map (ButterflyRegistry ::getButterflyIndex )
53- .forEach (this ::addChrysalisSilkLoot );
54-
59+ silkChrysalises .forEach (this ::addChrysalisSilkLoot );
5560 add (EntityTypeRegistry .BUTTERFLY_GOLEM .get (), createButterflyGolemLoot ());
5661 }
5762
@@ -62,7 +67,7 @@ public void generate() {
6267 */
6368 @ Override
6469 protected boolean canHaveLootTable (@ NotNull EntityType <?> entityType ) {
65- return super . canHaveLootTable (entityType ) || entityType . equals ( EntityTypeRegistry . BUTTERFLY_GOLEM . get () );
70+ return knownEntityTypes . contains (entityType ) || super . canHaveLootTable ( entityType );
6671 }
6772
6873 /**
@@ -72,29 +77,19 @@ protected boolean canHaveLootTable(@NotNull EntityType<?> entityType) {
7277 @ NotNull
7378 @ Override
7479 protected Stream <EntityType <?>> getKnownEntityTypes () {
75- Stream <EntityType <?>> silkChrysalisesStream = StreamSupport .stream (BuiltInRegistries .ENTITY_TYPE .spliterator (), false )
76- .filter (entry ->
77- Optional .of (BuiltInRegistries .ENTITY_TYPE .getKey (entry ))
78- .filter (key -> key .getNamespace ().equals (ButterfliesMod .MOD_ID )
79- && StringUtils .equalsAny (key .getPath (), SILK_SPECIES .toArray (new String [0 ])))
80- .isPresent ()
81- );
82-
83- return Stream .concat (silkChrysalisesStream , Stream .of (EntityTypeRegistry .BUTTERFLY_GOLEM .get ()));
80+ return knownEntityTypes .stream ();
8481 }
8582
8683 /**
8784 * Adds a silk drop to a chrysalis.
88- * @param butterflyIndex The butterfly index .
85+ * @param entityType The entity to add a loot table for .
8986 */
90- private void addChrysalisSilkLoot (int butterflyIndex ) {
91- add (ButterflyEntityTypeRegistry .CHRYSALISES .get (butterflyIndex ).get (),
92- LootTable .lootTable ()
93- .withPool (LootPool .lootPool ()
94- .setRolls (ConstantValue .exactly (1.0f ))
95- .add (LootItem .lootTableItem (ItemRegistry .SILK .get ())
96- .apply (SetItemCountFunction .setCount (UniformGenerator .between (1.0f , 3.0f )))
97- .apply (LootingEnchantFunction .lootingMultiplier (UniformGenerator .between (0.0f , 1.0f ))))));
87+ private void addChrysalisSilkLoot (EntityType <?> entityType ) {
88+ add (entityType , singleRollCountedLoot (
89+ ItemRegistry .SILK .get (),
90+ UniformGenerator .between (1.0f , 3.0f ),
91+ LootingEnchantFunction .lootingMultiplier (UniformGenerator .between (0.0f , 1.0f ))
92+ ));
9893 }
9994
10095 /**
@@ -103,13 +98,43 @@ private void addChrysalisSilkLoot(int butterflyIndex) {
10398 */
10499 private LootTable .Builder createButterflyGolemLoot () {
105100 return LootTable .lootTable ()
106- .withPool (LootPool .lootPool ()
107- .setRolls (ConstantValue .exactly (1.0f ))
108- .add (LootItem .lootTableItem (Items .POPPY )
109- .apply (SetItemCountFunction .setCount (UniformGenerator .between (0.0f , 2.0f )))))
110- .withPool (LootPool .lootPool ()
111- .setRolls (ConstantValue .exactly (1.0f ))
112- .add (LootItem .lootTableItem (Items .IRON_INGOT )
113- .apply (SetItemCountFunction .setCount (UniformGenerator .between (3.0f , 5.0f )))));
101+ .withPool (singleRollPool (Items .POPPY , UniformGenerator .between (0.0f , 2.0f )))
102+ .withPool (singleRollPool (Items .IRON_INGOT , UniformGenerator .between (3.0f , 5.0f )));
103+ }
104+
105+ /**
106+ * Creates a single roll for counted loot.
107+ * @param item The item to roll for.
108+ * @param count The count for the loot.
109+ * @param functions Any functions that can be applied to the roll.
110+ * @return A single roll for a loot table.
111+ */
112+ private LootTable .Builder singleRollCountedLoot (ItemLike item ,
113+ NumberProvider count ,
114+ LootItemFunction .Builder ... functions ) {
115+ LootPool .Builder pool = LootPool .lootPool ()
116+ .setRolls (ConstantValue .exactly (1.0f ))
117+ .add (LootItem .lootTableItem (item ).apply (SetItemCountFunction .setCount (count )));
118+
119+ for (LootItemFunction .Builder function : functions ) {
120+ pool .apply (function );
121+ }
122+
123+ return LootTable .lootTable ().withPool (pool );
124+ }
125+
126+
127+ /**
128+ * Creates a single roll for a loot table.
129+ * @param item The item to roll for.
130+ * @param count The count for the loot.
131+ * @return A single roll for a loot table.
132+ */
133+ private LootPool .Builder singleRollPool (ItemLike item ,
134+ NumberProvider count ) {
135+ return LootPool .lootPool ()
136+ .setRolls (ConstantValue .exactly (1.0f ))
137+ .add (LootItem .lootTableItem (item )
138+ .apply (SetItemCountFunction .setCount (count )));
114139 }
115140}
0 commit comments