Skip to content

Commit 9cf56f6

Browse files
committed
Move custom keys to VanillaInterface
1 parent a6f0039 commit 9cf56f6

File tree

8 files changed

+38
-35
lines changed

8 files changed

+38
-35
lines changed

src/main/java/net/goldenstack/loot/LootContext.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
package net.goldenstack.loot;
22

33
import net.goldenstack.loot.util.VanillaInterface;
4-
import net.kyori.adventure.nbt.CompoundBinaryTag;
54
import net.minestom.server.coordinate.Point;
65
import net.minestom.server.entity.Entity;
76
import net.minestom.server.entity.Player;
87
import net.minestom.server.entity.damage.DamageType;
98
import net.minestom.server.instance.Instance;
109
import net.minestom.server.instance.block.Block;
1110
import net.minestom.server.item.ItemStack;
12-
import net.minestom.server.utils.NamespaceID;
1311
import org.jetbrains.annotations.NotNull;
1412
import org.jetbrains.annotations.Nullable;
1513

1614
import java.util.HashMap;
1715
import java.util.Map;
1816
import java.util.NoSuchElementException;
1917
import java.util.Random;
20-
import java.util.function.Function;
2118

2219
/**
2320
* Stores a dynamic amount of information that may be relevant during the generation of loot.
@@ -36,21 +33,17 @@ public sealed interface LootContext permits LootContextImpl {
3633
@NotNull LootContext.Key<Entity> DIRECT_ATTACKING_ENTITY = LootContext.key("minecraft:direct_attacking_entity");
3734
@NotNull LootContext.Key<Entity> ATTACKING_ENTITY = LootContext.key("minecraft:attacking_entity");
3835
@NotNull LootContext.Key<Entity> THIS_ENTITY = LootContext.key("minecraft:this_entity");
39-
@NotNull LootContext.Key<VanillaInterface> VANILLA_INTERFACE = LootContext.key("trove:vanilla_interface");
40-
@NotNull LootContext.Key<Function<NamespaceID, LootTable>> REGISTERED_TABLES = LootContext.key("minecraft:registered_loot_tables");
41-
@NotNull LootContext.Key<Function<NamespaceID, LootPredicate>> REGISTERED_PREDICATES = LootContext.key("minecraft:registered_loot_predicates");
42-
@NotNull LootContext.Key<Function<NamespaceID, LootFunction>> REGISTERED_FUNCTIONS = LootContext.key("minecraft:registered_loot_functions");
43-
@NotNull LootContext.Key<Function<NamespaceID, CompoundBinaryTag>> COMMAND_STORAGE = LootContext.key("minecraft:command_storage");
4436
@NotNull LootContext.Key<Double> LUCK = LootContext.key("minecraft:luck");
4537
@NotNull LootContext.Key<Integer> ENCHANTMENT_LEVEL = LootContext.key("minecraft:enchantment_level");
4638

4739
/**
48-
* Creates a loot context from the provided map of key -> object.
40+
* Creates a loot context from the provided map of key -> object and vanilla interface.
41+
* @param vanilla this context's interface with vanilla features
4942
* @param data the values of the context
5043
* @return the new context instance
5144
*/
52-
static @NotNull LootContext from(@NotNull Map<Key<?>, Object> data) {
53-
return LootContextImpl.from(data);
45+
static @NotNull LootContext from(@NotNull VanillaInterface vanilla, @NotNull Map<Key<?>, Object> data) {
46+
return LootContextImpl.from(vanilla, data);
5447
}
5548

5649
/**
@@ -100,21 +93,27 @@ record Key<T>(@NotNull String id) {}
10093
*/
10194
<T> @NotNull T require(@NotNull Key<T> key);
10295

96+
/**
97+
* Returns this context's vanilla interface. This is not part of normal Minecraft loot contexts, but it's required
98+
* here for integration with other potential Minestom features.
99+
*/
100+
@NotNull VanillaInterface vanilla();
101+
103102
}
104103

105-
record LootContextImpl(@NotNull Map<String, Object> data) implements LootContext {
104+
record LootContextImpl(@NotNull VanillaInterface vanilla, @NotNull Map<String, Object> data) implements LootContext {
106105

107106
LootContextImpl {
108107
data = Map.copyOf(data);
109108
}
110109

111-
static @NotNull LootContext from(@NotNull Map<Key<?>, Object> data) {
110+
static @NotNull LootContext from(@NotNull VanillaInterface vanilla, @NotNull Map<Key<?>, Object> data) {
112111
Map<String, Object> mapped = new HashMap<>();
113112
for (Map.Entry<Key<?>, Object> entry : data.entrySet()) {
114113
mapped.put(entry.getKey().id(), entry.getValue());
115114
}
116115

117-
return new LootContextImpl(mapped);
116+
return new LootContextImpl(vanilla, mapped);
118117
}
119118

120119
@Override

src/main/java/net/goldenstack/loot/LootEntry.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,7 @@ record LootTable(@NotNull List<LootPredicate> predicates, @NotNull List<LootFunc
249249

250250
@Override
251251
public @NotNull List<ItemStack> apply(@NotNull LootContext context) {
252-
var tables = context.get(LootContext.REGISTERED_TABLES);
253-
if (tables == null) return List.of();
254-
255-
net.goldenstack.loot.LootTable table = tables.apply(value);
252+
var table = context.vanilla().getRegisteredTable(value);
256253
if (table == null) return List.of();
257254

258255
return LootFunction.apply(functions, table.apply(context), context);

src/main/java/net/goldenstack/loot/LootFunction.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ record Reference(@NotNull List<LootPredicate> predicates, @NotNull NamespaceID n
206206
public @NotNull ItemStack apply(@NotNull ItemStack input, @NotNull LootContext context) {
207207
if (!LootPredicate.all(predicates, context)) return input;
208208

209-
LootFunction function = context.require(LootContext.REGISTERED_FUNCTIONS).apply(name);
209+
LootFunction function = context.vanilla().getRegisteredFunction(name);
210210

211211
return function != null ? function.apply(input, context) : input;
212212
}
@@ -734,9 +734,7 @@ record EnchantWithLevels(@NotNull List<LootPredicate> predicates, @NotNull LootN
734734
public @NotNull ItemStack apply(@NotNull ItemStack input, @NotNull LootContext context) {
735735
if (!LootPredicate.all(predicates, context)) return input;
736736

737-
VanillaInterface vanilla = context.require(LootContext.VANILLA_INTERFACE);
738-
739-
return vanilla.enchantItem(context.require(LootContext.RANDOM), input, levels.getInt(context), options);
737+
return context.vanilla().enchantItem(context.require(LootContext.RANDOM), input, levels.getInt(context), options);
740738
}
741739
}
742740

@@ -840,7 +838,7 @@ record FurnaceSmelt(@NotNull List<LootPredicate> predicates) implements LootFunc
840838
public @NotNull ItemStack apply(@NotNull ItemStack input, @NotNull LootContext context) {
841839
if (!LootPredicate.all(predicates, context)) return input;
842840

843-
ItemStack smelted = context.require(LootContext.VANILLA_INTERFACE).smelt(input);
841+
ItemStack smelted = context.vanilla().smelt(input);
844842

845843
return smelted != null ? smelted.withAmount(input.amount()) : input;
846844
}

src/main/java/net/goldenstack/loot/LootNBT.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import net.goldenstack.loot.util.RelevantEntity;
44
import net.goldenstack.loot.util.Serial;
55
import net.goldenstack.loot.util.Template;
6-
import net.goldenstack.loot.util.VanillaInterface;
76
import net.kyori.adventure.nbt.BinaryTag;
87
import net.kyori.adventure.nbt.CompoundBinaryTag;
98
import net.kyori.adventure.nbt.IntBinaryTag;
@@ -47,7 +46,7 @@ record Storage(@NotNull NamespaceID source) implements LootNBT {
4746

4847
@Override
4948
public @Nullable BinaryTag getNBT(@NotNull LootContext context) {
50-
return context.require(LootContext.COMMAND_STORAGE).apply(source);
49+
return context.vanilla().getCommandStorage(source);
5150
}
5251
}
5352

@@ -98,9 +97,8 @@ record Entity(@NotNull RelevantEntity target) implements Target {
9897
@Override
9998
public @NotNull BinaryTag getNBT(@NotNull LootContext context) {
10099
var entity = context.require(target.key());
101-
VanillaInterface vanilla = context.require(LootContext.VANILLA_INTERFACE);
102100

103-
return vanilla.serializeEntity(entity);
101+
return context.vanilla().serializeEntity(entity);
104102
}
105103

106104
@Override

src/main/java/net/goldenstack/loot/LootNumber.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ public double getDouble(@NotNull LootContext context) {
174174
}
175175

176176
private NumberBinaryTag get(@NotNull LootContext context) {
177-
CompoundBinaryTag compound = context.require(LootContext.COMMAND_STORAGE).apply(storage);
177+
CompoundBinaryTag compound = context.vanilla().getCommandStorage(storage);
178178

179-
List<NBTReference> refs = path.get(compound);
179+
List<NBTReference> refs = path.get(compound != null ? compound : CompoundBinaryTag.empty());
180180
if (refs.size() != 1) return IntBinaryTag.intBinaryTag(0);
181181

182182
if (refs.getFirst().get() instanceof NumberBinaryTag number) {

src/main/java/net/goldenstack/loot/LootPredicate.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ record Reference(@NotNull NamespaceID name) implements LootPredicate {
233233

234234
@Override
235235
public boolean test(@NotNull LootContext context) {
236-
LootPredicate predicate = context.require(LootContext.REGISTERED_PREDICATES).apply(name);
236+
LootPredicate predicate = context.vanilla().getRegisteredPredicate(name);
237237

238238
return predicate != null && predicate.test(context);
239239
}
@@ -358,10 +358,8 @@ public boolean test(@NotNull LootContext context) {
358358
Entity entity = context.get(this.entity.key());
359359
if (entity == null) return false;
360360

361-
VanillaInterface vanilla = context.require(LootContext.VANILLA_INTERFACE);
362-
363361
for (var entry : scores.entrySet()) {
364-
Integer score = vanilla.getScore(entity, entry.getKey());
362+
Integer score = context.vanilla().getScore(entity, entry.getKey());
365363
if (score == null || !entry.getValue().check(context, score)) {
366364
return false;
367365
}

src/main/java/net/goldenstack/loot/LootScore.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ record Context(@NotNull RelevantEntity name) implements LootScore {
3434

3535
@Override
3636
public @NotNull Function<@NotNull String, @Nullable Integer> apply(@NotNull LootContext context) {
37-
return objective -> context.require(LootContext.VANILLA_INTERFACE).getScore(context.require(name.key()), objective);
37+
return objective -> context.vanilla().getScore(context.require(name.key()), objective);
3838
}
3939
}
4040

@@ -47,7 +47,7 @@ record Fixed(@NotNull String name) implements LootScore {
4747

4848
@Override
4949
public @NotNull Function<@NotNull String, @Nullable Integer> apply(@NotNull LootContext context) {
50-
return objective -> context.require(LootContext.VANILLA_INTERFACE).getScore(name, objective);
50+
return objective -> context.vanilla().getScore(name, objective);
5151
}
5252
}
5353

src/main/java/net/goldenstack/loot/util/VanillaInterface.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package net.goldenstack.loot.util;
22

3+
import net.goldenstack.loot.LootFunction;
4+
import net.goldenstack.loot.LootPredicate;
5+
import net.goldenstack.loot.LootTable;
36
import net.kyori.adventure.nbt.BinaryTag;
7+
import net.kyori.adventure.nbt.CompoundBinaryTag;
48
import net.minestom.server.entity.Entity;
59
import net.minestom.server.item.ItemStack;
610
import net.minestom.server.item.enchant.Enchantment;
711
import net.minestom.server.registry.DynamicRegistry;
12+
import net.minestom.server.utils.NamespaceID;
813
import org.jetbrains.annotations.NotNull;
914
import org.jetbrains.annotations.Nullable;
1015

@@ -23,4 +28,12 @@ public interface VanillaInterface {
2328

2429
@Nullable ItemStack smelt(@NotNull ItemStack input);
2530

31+
@Nullable LootTable getRegisteredTable(@NotNull NamespaceID key);
32+
33+
@Nullable LootPredicate getRegisteredPredicate(@NotNull NamespaceID key);
34+
35+
@Nullable LootFunction getRegisteredFunction(@NotNull NamespaceID key);
36+
37+
@Nullable CompoundBinaryTag getCommandStorage(@NotNull NamespaceID key);
38+
2639
}

0 commit comments

Comments
 (0)