调整幸运集装箱的抽取方式
This commit is contained in:
parent
67a7acb39d
commit
d42138bb9d
6 changed files with 75 additions and 22 deletions
|
@ -4,6 +4,7 @@ import com.atsuishio.superbwarfare.block.LuckyContainerBlock;
|
|||
import com.atsuishio.superbwarfare.data.container.ContainerDataManager;
|
||||
import com.atsuishio.superbwarfare.init.ModBlockEntities;
|
||||
import com.atsuishio.superbwarfare.tools.ParticleTool;
|
||||
import it.unimi.dsi.fastutil.Pair;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.HolderLookup;
|
||||
import net.minecraft.core.particles.ParticleTypes;
|
||||
|
@ -80,8 +81,19 @@ public class LuckyContainerBlockEntity extends BlockEntity implements GeoBlockEn
|
|||
ContainerDataManager dataManager = ContainerDataManager.INSTANCE;
|
||||
var list = dataManager.getEntityTypes(this.location);
|
||||
if (list.isPresent()) {
|
||||
int rand = this.level.random.nextInt(list.get().size());
|
||||
return EntityType.byString(list.get().get(rand)).orElse(null);
|
||||
var pool = list.get();
|
||||
int sum = pool.stream().mapToInt(Pair::second).sum();
|
||||
if (sum <= 0) return null;
|
||||
|
||||
int rand = this.level.random.nextInt(sum);
|
||||
|
||||
int cumulativeWeight = 0;
|
||||
for (var entry : pool) {
|
||||
cumulativeWeight += entry.second();
|
||||
if (rand < cumulativeWeight) {
|
||||
return EntityType.byString(entry.first()).orElse(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
|
|
@ -4,6 +4,7 @@ import com.atsuishio.superbwarfare.Mod;
|
|||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import it.unimi.dsi.fastutil.Pair;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.server.packs.resources.ResourceManager;
|
||||
import net.minecraft.server.packs.resources.SimpleJsonResourceReloadListener;
|
||||
|
@ -22,7 +23,7 @@ public class ContainerDataManager extends SimpleJsonResourceReloadListener {
|
|||
|
||||
private static final Gson GSON = new Gson();
|
||||
private static final String DIRECTORY = "containers";
|
||||
private final Map<ResourceLocation, List<String>> containerData = new HashMap<>();
|
||||
private final Map<ResourceLocation, List<Pair<String, Integer>>> containerData = new HashMap<>();
|
||||
|
||||
public ContainerDataManager() {
|
||||
super(GSON, DIRECTORY);
|
||||
|
@ -41,8 +42,18 @@ public class ContainerDataManager extends SimpleJsonResourceReloadListener {
|
|||
pObject.forEach((id, json) -> {
|
||||
try {
|
||||
JsonObject obj = json.getAsJsonObject();
|
||||
List<String> list = new ArrayList<>();
|
||||
obj.getAsJsonArray("EntityTypes").forEach(e -> list.add(e.getAsString()));
|
||||
List<Pair<String, Integer>> list = new ArrayList<>();
|
||||
var array = obj.getAsJsonArray("List");
|
||||
for (var arr : array) {
|
||||
if (arr.isJsonObject()) {
|
||||
JsonObject obj2 = arr.getAsJsonObject();
|
||||
String type = obj2.get("Type").getAsString();
|
||||
int weight = obj2.get("Weight").getAsInt();
|
||||
list.add(Pair.of(type, weight));
|
||||
} else {
|
||||
list.add(Pair.of(arr.getAsString(), 1));
|
||||
}
|
||||
}
|
||||
containerData.put(id, list);
|
||||
} catch (Exception e) {
|
||||
Mod.LOGGER.error("Failed to load container data for {}", id);
|
||||
|
@ -50,7 +61,7 @@ public class ContainerDataManager extends SimpleJsonResourceReloadListener {
|
|||
});
|
||||
}
|
||||
|
||||
public Optional<List<String>> getEntityTypes(ResourceLocation id) {
|
||||
public Optional<List<Pair<String, Integer>>> getEntityTypes(ResourceLocation id) {
|
||||
return Optional.ofNullable(containerData.get(id));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,13 @@
|
|||
{
|
||||
"EntityTypes": [
|
||||
"superbwarfare:ah_6",
|
||||
"superbwarfare:tom_6",
|
||||
"List": [
|
||||
{
|
||||
"Type": "superbwarfare:tom_6",
|
||||
"Weight": 3
|
||||
},
|
||||
{
|
||||
"Type": "superbwarfare:ah_6",
|
||||
"Weight": 2
|
||||
},
|
||||
"superbwarfare:a_10a"
|
||||
]
|
||||
}
|
|
@ -1,9 +1,21 @@
|
|||
{
|
||||
"EntityTypes": [
|
||||
"superbwarfare:mk_42",
|
||||
"superbwarfare:hpj_11",
|
||||
"superbwarfare:mle_1934",
|
||||
"superbwarfare:annihilator",
|
||||
"superbwarfare:type_63"
|
||||
"List": [
|
||||
{
|
||||
"Type": "superbwarfare:type_63",
|
||||
"Weight": 5
|
||||
},
|
||||
{
|
||||
"Type": "superbwarfare:mk_42",
|
||||
"Weight": 4
|
||||
},
|
||||
{
|
||||
"Type": "superbwarfare:mle_1934",
|
||||
"Weight": 3
|
||||
},
|
||||
{
|
||||
"Type": "superbwarfare:hpj_11",
|
||||
"Weight": 2
|
||||
},
|
||||
"superbwarfare:annihilator"
|
||||
]
|
||||
}
|
|
@ -1,9 +1,21 @@
|
|||
{
|
||||
"EntityTypes": [
|
||||
"superbwarfare:wheel_chair",
|
||||
"superbwarfare:lav_150",
|
||||
"superbwarfare:bmp_2",
|
||||
"superbwarfare:yx_100",
|
||||
"superbwarfare:prism_tank"
|
||||
"List": [
|
||||
{
|
||||
"Type": "superbwarfare:lav_150",
|
||||
"Weight": 5
|
||||
},
|
||||
{
|
||||
"Type": "superbwarfare:bmp_2",
|
||||
"Weight": 4
|
||||
},
|
||||
{
|
||||
"Type": "superbwarfare:wheel_chair",
|
||||
"Weight": 3
|
||||
},
|
||||
{
|
||||
"Type": "superbwarfare:prism_tank",
|
||||
"Weight": 2
|
||||
},
|
||||
"superbwarfare:yx_100"
|
||||
]
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
{
|
||||
"EntityTypes": [
|
||||
"List": [
|
||||
"superbwarfare:speedboat",
|
||||
"superbwarfare:wheel_chair",
|
||||
"superbwarfare:ah_6",
|
||||
|
|
Loading…
Add table
Reference in a new issue