Skip to content

Commit

Permalink
feat: #333 (#402)
Browse files Browse the repository at this point in the history
  • Loading branch information
IWareQ authored Sep 20, 2024
1 parent d1d3a7c commit fd24542
Show file tree
Hide file tree
Showing 35 changed files with 286 additions and 160 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package org.allaymc.server.loottable;
package org.allaymc.api.loottable;

import com.google.gson.JsonObject;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.allaymc.server.loottable.condition.Condition;
import org.allaymc.server.loottable.condition.ConditionDeserializer;
import org.allaymc.server.loottable.context.Context;
import org.allaymc.server.loottable.entry.Entry;
import org.allaymc.server.loottable.entry.EntryDeserializer;
import org.allaymc.server.loottable.function.Function;
import org.allaymc.server.loottable.function.FunctionDeserializer;
import org.allaymc.api.loottable.condition.Condition;
import org.allaymc.api.loottable.condition.ConditionDeserializer;
import org.allaymc.api.loottable.context.Context;
import org.allaymc.api.loottable.entry.Entry;
import org.allaymc.api.loottable.entry.EntryDeserializer;
import org.allaymc.api.loottable.function.Function;
import org.allaymc.api.loottable.function.FunctionDeserializer;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -24,39 +24,63 @@ public class LootTableType<CONTEXT_TYPE extends Context> {
protected Map<String, ConditionDeserializer<CONTEXT_TYPE>> conditions;
protected Map<String, EntryDeserializer<CONTEXT_TYPE>> entries;

public static <CONTEXT_TYPE extends Context> LootTableTypeBuilder<CONTEXT_TYPE> builder() {
return new LootTableTypeBuilder<>();
public static <CONTEXT_TYPE extends Context> Builder<CONTEXT_TYPE> builder() {
return new Builder<>();
}

/**
* Gets the function from the given name and JsonObject.
*
* @param name The name of the function.
* @param json The JsonObject containing the function data.
*
* @return The function.
*/
public Function getFunction(String name, JsonObject json) {
return functions.get(name).deserialize(json);
}

/**
* Gets the condition from the given name and JsonObject.
*
* @param name The name of the condition.
* @param json The JsonObject containing the condition data.
*
* @return The condition.
*/
public Condition<CONTEXT_TYPE> getCondition(String name, JsonObject json) {
return conditions.get(name).deserialize(json);
}

/**
* Gets the entry from the given type and JsonObject.
*
* @param type The type of the entry.
* @param json The JsonObject containing the entry data.
*
* @return The entry.
*/
public Entry<CONTEXT_TYPE> getEntry(String type, JsonObject json) {
return entries.get(type).deserialize(json, this);
}

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public static class LootTableTypeBuilder<CONTEXT_TYPE extends Context> {
public static class Builder<CONTEXT_TYPE extends Context> {
private final Map<String, FunctionDeserializer> functions = new HashMap<>();
private final Map<String, ConditionDeserializer<CONTEXT_TYPE>> conditions = new HashMap<>();
private final Map<String, EntryDeserializer<CONTEXT_TYPE>> entries = new HashMap<>();

public LootTableTypeBuilder<CONTEXT_TYPE> supportFunction(String name, FunctionDeserializer deserializer) {
public Builder<CONTEXT_TYPE> supportFunction(String name, FunctionDeserializer deserializer) {
this.functions.putIfAbsent(name, deserializer);
return this;
}

public LootTableTypeBuilder<CONTEXT_TYPE> supportCondition(String name, ConditionDeserializer<CONTEXT_TYPE> deserializer) {
public Builder<CONTEXT_TYPE> supportCondition(String name, ConditionDeserializer<CONTEXT_TYPE> deserializer) {
this.conditions.putIfAbsent(name, deserializer);
return this;
}

public LootTableTypeBuilder<CONTEXT_TYPE> supportEntry(String type, EntryDeserializer<CONTEXT_TYPE> deserializer) {
public Builder<CONTEXT_TYPE> supportEntry(String type, EntryDeserializer<CONTEXT_TYPE> deserializer) {
this.entries.putIfAbsent(type, deserializer);
return this;
}
Expand Down
13 changes: 13 additions & 0 deletions Allay-API/src/main/java/org/allaymc/api/loottable/Rolls.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.allaymc.api.loottable;

/**
* @author daoge_cmd
*/
public interface Rolls {
/**
* Get the number of rolls
*
* @return the number of rolls
*/
int getRolls();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.allaymc.api.loottable.condition;

import org.allaymc.api.loottable.context.Context;

/**
* @author daoge_cmd
*/
public interface Condition<CONTEXT_TYPE extends Context> {
/**
* Tests the condition with the given context.
*
* @param context The context to test the condition with.
*
* @return {@code true} if the condition passes, {@code false} otherwise.
*/
boolean test(CONTEXT_TYPE context);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.allaymc.api.loottable.condition;

import com.google.gson.JsonObject;
import org.allaymc.api.loottable.context.Context;

/**
* @author daoge_cmd
*/
public interface ConditionDeserializer<CONTEXT_TYPE extends Context> {

/**
* Deserializes a {@link JsonObject} into a {@link Condition} object.
*
* @param json The {@link JsonObject} to deserialize.
*
* @return The deserialized {@link Condition} object.
*/
Condition<CONTEXT_TYPE> deserialize(JsonObject json);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.allaymc.api.loottable.condition;

import org.allaymc.api.loottable.context.Context;

import java.util.List;

/**
* @author daoge_cmd
*/
public record Conditions<CONTEXT_TYPE extends Context>(
List<Condition<CONTEXT_TYPE>> conditions
) {
/**
* Tests the conditions with the given context.
*
* @param context The context to test the conditions with.
*
* @return {@code true} if the conditions pass, {@code false} otherwise.
*/
public boolean test(CONTEXT_TYPE context) {
return conditions.stream().allMatch(condition -> condition.test(context));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.allaymc.server.loottable.context;
package org.allaymc.api.loottable.context;

/**
* @author daoge_cmd
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.allaymc.server.loottable.entry;
package org.allaymc.api.loottable.entry;

import org.allaymc.api.item.ItemStack;
import org.allaymc.server.loottable.context.Context;
import org.allaymc.api.loottable.context.Context;

import java.util.List;
import java.util.Set;
Expand All @@ -13,7 +13,7 @@
public record Entries<CONTEXT_TYPE extends Context>(List<Entry<CONTEXT_TYPE>> entries) {
public Set<ItemStack> loot(CONTEXT_TYPE context) {
var validEntries = entries.stream()
.filter(e -> e.test(context))
.filter(entry -> entry.test(context))
.toList();
var weightSum = validEntries.stream().mapToInt(Entry::getWeight).sum();

Expand Down
53 changes: 53 additions & 0 deletions Allay-API/src/main/java/org/allaymc/api/loottable/entry/Entry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.allaymc.api.loottable.entry;

import org.allaymc.api.item.ItemStack;
import org.allaymc.api.loottable.condition.Conditions;
import org.allaymc.api.loottable.context.Context;

import java.util.Set;

/**
* @author daoge_cmd
*/
public interface Entry<CONTEXT_TYPE extends Context> {
/**
* Loot method that returns a set of ItemStack objects.
*
* @param context The context to loot with.
*
* @return A set of ItemStack objects.
*/
Set<ItemStack> loot(CONTEXT_TYPE context);

/**
* Tests the entry's conditions with the given context.
*
* @param context The context to test the conditions with.
*
* @return {@code true} if the conditions pass, {@code false} otherwise.
*/
default boolean test(CONTEXT_TYPE context) {
return getConditions().test(context);
}

/**
* Returns the name of the entry.
*
* @return The name of the entry.
*/
String getName();

/**
* Returns the weight of the entry.
*
* @return The weight of the entry.
*/
int getWeight();

/**
* Returns the conditions of the entry.
*
* @return The conditions of the entry.
*/
Conditions<CONTEXT_TYPE> getConditions();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.allaymc.api.loottable.entry;

import com.google.gson.JsonObject;
import org.allaymc.api.loottable.LootTableType;
import org.allaymc.api.loottable.context.Context;

/**
* @author daoge_cmd
*/
public interface EntryDeserializer<CONTEXT_TYPE extends Context> {
/**
* Deserializes an {@link Entry} from a {@link JsonObject}.
*
* @param json The {@link JsonObject} to deserialize from.
* @param lootTableType The {@link LootTableType} of the loot table.
*
* @return A new {@link Entry} instance.
*/
Entry<CONTEXT_TYPE> deserialize(JsonObject json, LootTableType<CONTEXT_TYPE> lootTableType);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.allaymc.api.loottable.function;

import org.allaymc.api.item.ItemStack;

/**
* @author daoge_cmd
*/
public interface Function {
/**
* Applies this function to the given ItemStack.
*
* @param itemStack The ItemStack to apply the function to.
*/
void apply(ItemStack itemStack);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.allaymc.api.loottable.function;

import com.google.gson.JsonObject;

/**
* @author daoge_cmd
*/
public interface FunctionDeserializer {
/**
* Deserializes a {@code JsonObject} into a {@code Function} object.
*
* @param json The JsonObject to deserialize.
*
* @return The deserialized {@code Function} object.
*/
Function deserialize(JsonObject json);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.allaymc.server.loottable.function;
package org.allaymc.api.loottable.function;

import org.allaymc.api.item.ItemStack;

Expand All @@ -8,6 +8,11 @@
* @author daoge_cmd
*/
public record Functions(List<Function> functions) {
/**
* Applies each function in the list to the given item stack.
*
* @param itemStack The item stack to apply the functions to.
*/
public void apply(ItemStack itemStack) {
functions.forEach(function -> function.apply(itemStack));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
import com.google.gson.JsonParser;
import lombok.extern.slf4j.Slf4j;
import org.allaymc.api.block.data.BlockId;
import org.allaymc.api.loottable.LootTableType;
import org.allaymc.api.loottable.Rolls;
import org.allaymc.api.loottable.condition.Condition;
import org.allaymc.api.loottable.condition.Conditions;
import org.allaymc.api.loottable.entry.Entries;
import org.allaymc.api.loottable.entry.Entry;
import org.allaymc.api.utils.Utils;
import org.allaymc.server.loottable.LootTable;
import org.allaymc.server.loottable.LootTableType;
import org.allaymc.server.loottable.Pool;
import org.allaymc.server.loottable.Rolls;
import org.allaymc.server.loottable.condition.Condition;
import org.allaymc.server.loottable.condition.Conditions;
import org.allaymc.server.loottable.condition.MatchToolCondition;
import org.allaymc.server.loottable.context.BreakBlockContext;
import org.allaymc.server.loottable.entry.EmptyEntry;
import org.allaymc.server.loottable.entry.Entries;
import org.allaymc.server.loottable.entry.Entry;
import org.allaymc.server.loottable.entry.ItemEntry;
import org.allaymc.server.loottable.function.SetCountFunction;
import org.allaymc.server.loottable.function.SetDamageFunction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import org.allaymc.api.entity.component.player.EntityPlayerBaseComponent;
import org.allaymc.api.entity.component.player.EntityPlayerNetworkComponent;
import org.allaymc.api.utils.MathUtils;
import org.allaymc.server.component.annotation.Identifier;
import org.allaymc.server.component.annotation.Dependency;
import org.allaymc.server.component.annotation.Identifier;
import org.cloudburstmc.math.vector.Vector3i;
import org.cloudburstmc.protocol.bedrock.data.inventory.ContainerSlotType;
import org.cloudburstmc.protocol.bedrock.data.inventory.FullContainerName;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.allaymc.server.loottable;

import org.allaymc.api.item.ItemStack;
import org.allaymc.server.loottable.context.Context;
import org.allaymc.api.loottable.LootTableType;
import org.allaymc.api.loottable.context.Context;

import java.util.List;
import java.util.Set;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package org.allaymc.server.loottable;

import org.allaymc.api.item.ItemStack;
import org.allaymc.server.loottable.condition.Conditions;
import org.allaymc.server.loottable.context.Context;
import org.allaymc.server.loottable.entry.Entries;
import org.allaymc.api.loottable.Rolls;
import org.allaymc.api.loottable.condition.Conditions;
import org.allaymc.api.loottable.context.Context;
import org.allaymc.api.loottable.entry.Entries;

import java.util.HashSet;
import java.util.Set;
Expand All @@ -16,7 +17,7 @@ public record Pool<CONTEXT_TYPE extends Context>(
Rolls rolls,
Entries<CONTEXT_TYPE> entries
) {
Set<ItemStack> loot(CONTEXT_TYPE context) {
public Set<ItemStack> loot(CONTEXT_TYPE context) {
if (!conditions.test(context)) return Set.of();
Set<ItemStack> items = new HashSet<>();
for (var i = 0; i < rolls.getRolls(); i++) {
Expand Down

This file was deleted.

Loading

0 comments on commit fd24542

Please sign in to comment.