Skip to content

Commit

Permalink
Move custom keys to VanillaInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
GoldenStack committed Aug 29, 2024
1 parent a6f0039 commit 9cf56f6
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 35 deletions.
27 changes: 13 additions & 14 deletions src/main/java/net/goldenstack/loot/LootContext.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
package net.goldenstack.loot;

import net.goldenstack.loot.util.VanillaInterface;
import net.kyori.adventure.nbt.CompoundBinaryTag;
import net.minestom.server.coordinate.Point;
import net.minestom.server.entity.Entity;
import net.minestom.server.entity.Player;
import net.minestom.server.entity.damage.DamageType;
import net.minestom.server.instance.Instance;
import net.minestom.server.instance.block.Block;
import net.minestom.server.item.ItemStack;
import net.minestom.server.utils.NamespaceID;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Random;
import java.util.function.Function;

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

/**
* Creates a loot context from the provided map of key -> object.
* Creates a loot context from the provided map of key -> object and vanilla interface.
* @param vanilla this context's interface with vanilla features
* @param data the values of the context
* @return the new context instance
*/
static @NotNull LootContext from(@NotNull Map<Key<?>, Object> data) {
return LootContextImpl.from(data);
static @NotNull LootContext from(@NotNull VanillaInterface vanilla, @NotNull Map<Key<?>, Object> data) {
return LootContextImpl.from(vanilla, data);
}

/**
Expand Down Expand Up @@ -100,21 +93,27 @@ record Key<T>(@NotNull String id) {}
*/
<T> @NotNull T require(@NotNull Key<T> key);

/**
* Returns this context's vanilla interface. This is not part of normal Minecraft loot contexts, but it's required
* here for integration with other potential Minestom features.
*/
@NotNull VanillaInterface vanilla();

}

record LootContextImpl(@NotNull Map<String, Object> data) implements LootContext {
record LootContextImpl(@NotNull VanillaInterface vanilla, @NotNull Map<String, Object> data) implements LootContext {

LootContextImpl {
data = Map.copyOf(data);
}

static @NotNull LootContext from(@NotNull Map<Key<?>, Object> data) {
static @NotNull LootContext from(@NotNull VanillaInterface vanilla, @NotNull Map<Key<?>, Object> data) {
Map<String, Object> mapped = new HashMap<>();
for (Map.Entry<Key<?>, Object> entry : data.entrySet()) {
mapped.put(entry.getKey().id(), entry.getValue());
}

return new LootContextImpl(mapped);
return new LootContextImpl(vanilla, mapped);
}

@Override
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/net/goldenstack/loot/LootEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,7 @@ record LootTable(@NotNull List<LootPredicate> predicates, @NotNull List<LootFunc

@Override
public @NotNull List<ItemStack> apply(@NotNull LootContext context) {
var tables = context.get(LootContext.REGISTERED_TABLES);
if (tables == null) return List.of();

net.goldenstack.loot.LootTable table = tables.apply(value);
var table = context.vanilla().getRegisteredTable(value);
if (table == null) return List.of();

return LootFunction.apply(functions, table.apply(context), context);
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/net/goldenstack/loot/LootFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ record Reference(@NotNull List<LootPredicate> predicates, @NotNull NamespaceID n
public @NotNull ItemStack apply(@NotNull ItemStack input, @NotNull LootContext context) {
if (!LootPredicate.all(predicates, context)) return input;

LootFunction function = context.require(LootContext.REGISTERED_FUNCTIONS).apply(name);
LootFunction function = context.vanilla().getRegisteredFunction(name);

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

VanillaInterface vanilla = context.require(LootContext.VANILLA_INTERFACE);

return vanilla.enchantItem(context.require(LootContext.RANDOM), input, levels.getInt(context), options);
return context.vanilla().enchantItem(context.require(LootContext.RANDOM), input, levels.getInt(context), options);
}
}

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

ItemStack smelted = context.require(LootContext.VANILLA_INTERFACE).smelt(input);
ItemStack smelted = context.vanilla().smelt(input);

return smelted != null ? smelted.withAmount(input.amount()) : input;
}
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/net/goldenstack/loot/LootNBT.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import net.goldenstack.loot.util.RelevantEntity;
import net.goldenstack.loot.util.Serial;
import net.goldenstack.loot.util.Template;
import net.goldenstack.loot.util.VanillaInterface;
import net.kyori.adventure.nbt.BinaryTag;
import net.kyori.adventure.nbt.CompoundBinaryTag;
import net.kyori.adventure.nbt.IntBinaryTag;
Expand Down Expand Up @@ -47,7 +46,7 @@ record Storage(@NotNull NamespaceID source) implements LootNBT {

@Override
public @Nullable BinaryTag getNBT(@NotNull LootContext context) {
return context.require(LootContext.COMMAND_STORAGE).apply(source);
return context.vanilla().getCommandStorage(source);
}
}

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

return vanilla.serializeEntity(entity);
return context.vanilla().serializeEntity(entity);
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/goldenstack/loot/LootNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ public double getDouble(@NotNull LootContext context) {
}

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

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

if (refs.getFirst().get() instanceof NumberBinaryTag number) {
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/net/goldenstack/loot/LootPredicate.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ record Reference(@NotNull NamespaceID name) implements LootPredicate {

@Override
public boolean test(@NotNull LootContext context) {
LootPredicate predicate = context.require(LootContext.REGISTERED_PREDICATES).apply(name);
LootPredicate predicate = context.vanilla().getRegisteredPredicate(name);

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

VanillaInterface vanilla = context.require(LootContext.VANILLA_INTERFACE);

for (var entry : scores.entrySet()) {
Integer score = vanilla.getScore(entity, entry.getKey());
Integer score = context.vanilla().getScore(entity, entry.getKey());
if (score == null || !entry.getValue().check(context, score)) {
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/goldenstack/loot/LootScore.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ record Context(@NotNull RelevantEntity name) implements LootScore {

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

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

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

Expand Down
13 changes: 13 additions & 0 deletions src/main/java/net/goldenstack/loot/util/VanillaInterface.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package net.goldenstack.loot.util;

import net.goldenstack.loot.LootFunction;
import net.goldenstack.loot.LootPredicate;
import net.goldenstack.loot.LootTable;
import net.kyori.adventure.nbt.BinaryTag;
import net.kyori.adventure.nbt.CompoundBinaryTag;
import net.minestom.server.entity.Entity;
import net.minestom.server.item.ItemStack;
import net.minestom.server.item.enchant.Enchantment;
import net.minestom.server.registry.DynamicRegistry;
import net.minestom.server.utils.NamespaceID;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

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

@Nullable ItemStack smelt(@NotNull ItemStack input);

@Nullable LootTable getRegisteredTable(@NotNull NamespaceID key);

@Nullable LootPredicate getRegisteredPredicate(@NotNull NamespaceID key);

@Nullable LootFunction getRegisteredFunction(@NotNull NamespaceID key);

@Nullable CompoundBinaryTag getCommandStorage(@NotNull NamespaceID key);

}

0 comments on commit 9cf56f6

Please sign in to comment.