net.minestom.server.recipe.RecipeManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of minestom-snapshots Show documentation
Show all versions of minestom-snapshots Show documentation
1.20.4 Lightweight Minecraft server
package net.minestom.server.recipe;
import net.minestom.server.entity.Player;
import net.minestom.server.network.packet.server.CachedPacket;
import net.minestom.server.network.packet.server.SendablePacket;
import net.minestom.server.network.packet.server.play.DeclareRecipesPacket;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Predicate;
public final class RecipeManager {
private final CachedPacket declareRecipesPacket = new CachedPacket(this::createDeclareRecipesPacket);
private final Map> recipes = new ConcurrentHashMap<>();
public void addRecipe(@NotNull Recipe recipe, @NotNull Predicate predicate) {
var previous = recipes.put(recipe, predicate);
if (previous == null) {
declareRecipesPacket.invalidate();
}
}
public void addRecipe(@NotNull Recipe recipe) {
addRecipe(recipe, player -> true);
}
public void removeRecipe(@NotNull Recipe recipe) {
if (this.recipes.remove(recipe) != null) {
declareRecipesPacket.invalidate();
}
}
public List consumeRecipes(Player player) {
return recipes.entrySet().stream()
.filter(entry -> entry.getValue().test(player))
.map(Map.Entry::getKey)
.toList();
}
public @NotNull Set getRecipes() {
return recipes.keySet();
}
public @NotNull SendablePacket getDeclareRecipesPacket() {
return declareRecipesPacket;
}
private @NotNull DeclareRecipesPacket createDeclareRecipesPacket() {
return new DeclareRecipesPacket(List.copyOf(recipes.keySet()));
}
}