mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-04-05 15:26:28 +00:00
Repackage utils
This commit is contained in:
522
src/main/java/com/volmit/iris/engine/data/B.java
Normal file
522
src/main/java/com/volmit/iris/engine/data/B.java
Normal file
@@ -0,0 +1,522 @@
|
||||
/*
|
||||
* Iris is a World Generator for Minecraft Bukkit Servers
|
||||
* Copyright (c) 2021 Arcane Arts (Volmit Software)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.volmit.iris.engine.data;
|
||||
|
||||
import com.volmit.iris.Iris;
|
||||
import com.volmit.iris.util.collection.KList;
|
||||
import com.volmit.iris.util.collection.KMap;
|
||||
import com.volmit.iris.util.collection.KSet;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
|
||||
public class B {
|
||||
private static final Material AIR_MATERIAL = Material.AIR;
|
||||
private static final BlockData AIR = AIR_MATERIAL.createBlockData();
|
||||
private static final KSet<String> nullBlockDataCache = new KSet<>();
|
||||
private static final KSet<String> nullMaterialCache = new KSet<>();
|
||||
private static final KMap<Material, Boolean> solidCache = new KMap<>();
|
||||
private static final KMap<Material, Boolean> updatableCache = new KMap<>();
|
||||
private static final KMap<Material, Boolean> foliageCache = new KMap<>();
|
||||
private static final KMap<Material, Boolean> litCache = new KMap<>();
|
||||
private static final KMap<Material, Boolean> decorantCache = new KMap<>();
|
||||
private static final KMap<Material, Boolean> storageCache = new KMap<>();
|
||||
private static final KMap<Material, Boolean> storageChestCache = new KMap<>();
|
||||
private static final KMap<String, BlockData> blockDataCache = new KMap<>();
|
||||
private static final KMap<String, Material> materialCache = new KMap<>();
|
||||
|
||||
public static boolean isWater(BlockData b) {
|
||||
return b.getMaterial().equals(Material.WATER);
|
||||
}
|
||||
|
||||
public static BlockData getAir() {
|
||||
return AIR;
|
||||
}
|
||||
|
||||
public static Material getMaterial(String bdx) {
|
||||
Material mat = getMaterialOrNull(bdx);
|
||||
|
||||
if (mat != null) {
|
||||
return mat;
|
||||
}
|
||||
|
||||
return AIR_MATERIAL;
|
||||
}
|
||||
|
||||
public static Material getMaterialOrNull(String bdxx) {
|
||||
String bx = bdxx.trim().toUpperCase();
|
||||
|
||||
if (nullMaterialCache.contains(bx)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Material mat = materialCache.get(bx);
|
||||
|
||||
if (mat != null) {
|
||||
return mat;
|
||||
}
|
||||
|
||||
try {
|
||||
Material mm = Material.valueOf(bx);
|
||||
materialCache.put(bx, mm);
|
||||
return mm;
|
||||
} catch (Throwable e) {Iris.reportError(e);
|
||||
nullMaterialCache.add(bx);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isSolid(BlockData mat) {
|
||||
return isSolid(mat.getMaterial());
|
||||
}
|
||||
|
||||
public static boolean isSolid(Material mat) {
|
||||
Boolean solid = solidCache.get(mat);
|
||||
|
||||
if (solid != null) {
|
||||
return solid;
|
||||
}
|
||||
|
||||
solid = mat.isSolid();
|
||||
solidCache.put(mat, solid);
|
||||
|
||||
return solid;
|
||||
}
|
||||
|
||||
public static BlockData getOrNull(String bdxf) {
|
||||
try {
|
||||
String bd = bdxf.trim();
|
||||
|
||||
BlockData bdx = parseBlockData(bd);
|
||||
|
||||
if (bdx == null) {
|
||||
Iris.warn("Unknown Block Data '" + bd + "'");
|
||||
return AIR;
|
||||
}
|
||||
|
||||
return bdx;
|
||||
} catch (Throwable e) {Iris.reportError(e);
|
||||
Iris.warn("Unknown Block Data '" + bdxf + "'");
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static BlockData get(String bdxf) {
|
||||
BlockData bd = getOrNull(bdxf);
|
||||
|
||||
if (bd != null) {
|
||||
return bd;
|
||||
}
|
||||
|
||||
return AIR;
|
||||
}
|
||||
|
||||
private static BlockData parseBlockDataOrNull(String ix) {
|
||||
if (nullBlockDataCache.contains(ix)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
BlockData bb = blockDataCache.get(ix);
|
||||
|
||||
if (bb != null) {
|
||||
return bb;
|
||||
}
|
||||
|
||||
BlockData bx = Bukkit.createBlockData(ix);
|
||||
blockDataCache.put(ix, bx);
|
||||
return bx;
|
||||
} catch (Throwable e) {Iris.reportError(e);
|
||||
|
||||
}
|
||||
|
||||
String i = ix.toUpperCase().trim();
|
||||
i = i.equals("WOOL") ? "WHITE_WOOL" : i;
|
||||
i = i.equals("CONCRETE") ? "WHITE_CONCRETE" : i;
|
||||
|
||||
try {
|
||||
BlockData bd = Material.valueOf(i).createBlockData();
|
||||
blockDataCache.put(ix, bd);
|
||||
} catch (Throwable e) {Iris.reportError(e);
|
||||
|
||||
}
|
||||
|
||||
nullBlockDataCache.add(ix);
|
||||
return null;
|
||||
}
|
||||
|
||||
private static BlockData parseBlockData(String ix) {
|
||||
BlockData bd = parseBlockDataOrNull(ix);
|
||||
|
||||
if (bd != null) {
|
||||
return bd;
|
||||
}
|
||||
|
||||
return AIR;
|
||||
}
|
||||
|
||||
public static boolean isStorage(BlockData mat) {
|
||||
Material mm = mat.getMaterial();
|
||||
Boolean f = storageCache.get(mm);
|
||||
|
||||
if (f != null) {
|
||||
return f;
|
||||
}
|
||||
|
||||
f = mm.equals(B.getMaterial("CHEST"))
|
||||
|| mm.equals(B.getMaterial("TRAPPED_CHEST"))
|
||||
|| mm.equals(B.getMaterial("SHULKER_BOX"))
|
||||
|| mm.equals(B.getMaterial("WHITE_SHULKER_BOX"))
|
||||
|| mm.equals(B.getMaterial("ORANGE_SHULKER_BOX"))
|
||||
|| mm.equals(B.getMaterial("MAGENTA_SHULKER_BOX"))
|
||||
|| mm.equals(B.getMaterial("LIGHT_BLUE_SHULKER_BOX"))
|
||||
|| mm.equals(B.getMaterial("YELLOW_SHULKER_BOX"))
|
||||
|| mm.equals(B.getMaterial("LIME_SHULKER_BOX"))
|
||||
|| mm.equals(B.getMaterial("PINK_SHULKER_BOX"))
|
||||
|| mm.equals(B.getMaterial("GRAY_SHULKER_BOX"))
|
||||
|| mm.equals(B.getMaterial("LIGHT_GRAY_SHULKER_BOX"))
|
||||
|| mm.equals(B.getMaterial("CYAN_SHULKER_BOX"))
|
||||
|| mm.equals(B.getMaterial("PURPLE_SHULKER_BOX"))
|
||||
|| mm.equals(B.getMaterial("BLUE_SHULKER_BOX"))
|
||||
|| mm.equals(B.getMaterial("BROWN_SHULKER_BOX"))
|
||||
|| mm.equals(B.getMaterial("GREEN_SHULKER_BOX"))
|
||||
|| mm.equals(B.getMaterial("RED_SHULKER_BOX"))
|
||||
|| mm.equals(B.getMaterial("BLACK_SHULKER_BOX"))
|
||||
|| mm.equals(B.getMaterial("BARREL"))
|
||||
|| mm.equals(B.getMaterial("DISPENSER"))
|
||||
|| mm.equals(B.getMaterial("DROPPER"))
|
||||
|| mm.equals(B.getMaterial("HOPPER"))
|
||||
|| mm.equals(B.getMaterial("FURNACE"))
|
||||
|| mm.equals(B.getMaterial("BLAST_FURNACE"))
|
||||
|| mm.equals(B.getMaterial("SMOKER"));
|
||||
storageCache.put(mm, f);
|
||||
return f;
|
||||
}
|
||||
|
||||
public static boolean isStorageChest(BlockData mat) {
|
||||
if (!isStorage(mat)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Material mm = mat.getMaterial();
|
||||
Boolean f = storageChestCache.get(mm);
|
||||
|
||||
if (f != null) {
|
||||
return f;
|
||||
}
|
||||
|
||||
f = mm.equals(B.getMaterial("CHEST"))
|
||||
|| mm.equals(B.getMaterial("TRAPPED_CHEST"))
|
||||
|| mm.equals(B.getMaterial("SHULKER_BOX"))
|
||||
|| mm.equals(B.getMaterial("WHITE_SHULKER_BOX"))
|
||||
|| mm.equals(B.getMaterial("ORANGE_SHULKER_BOX"))
|
||||
|| mm.equals(B.getMaterial("MAGENTA_SHULKER_BOX"))
|
||||
|| mm.equals(B.getMaterial("LIGHT_BLUE_SHULKER_BOX"))
|
||||
|| mm.equals(B.getMaterial("YELLOW_SHULKER_BOX"))
|
||||
|| mm.equals(B.getMaterial("LIME_SHULKER_BOX"))
|
||||
|| mm.equals(B.getMaterial("PINK_SHULKER_BOX"))
|
||||
|| mm.equals(B.getMaterial("GRAY_SHULKER_BOX"))
|
||||
|| mm.equals(B.getMaterial("LIGHT_GRAY_SHULKER_BOX"))
|
||||
|| mm.equals(B.getMaterial("CYAN_SHULKER_BOX"))
|
||||
|| mm.equals(B.getMaterial("PURPLE_SHULKER_BOX"))
|
||||
|| mm.equals(B.getMaterial("BLUE_SHULKER_BOX"))
|
||||
|| mm.equals(B.getMaterial("BROWN_SHULKER_BOX"))
|
||||
|| mm.equals(B.getMaterial("GREEN_SHULKER_BOX"))
|
||||
|| mm.equals(B.getMaterial("RED_SHULKER_BOX"))
|
||||
|| mm.equals(B.getMaterial("BLACK_SHULKER_BOX"))
|
||||
|| mm.equals(B.getMaterial("BARREL"))
|
||||
|| mm.equals(B.getMaterial("DISPENSER"))
|
||||
|| mm.equals(B.getMaterial("DROPPER"))
|
||||
|| mm.equals(B.getMaterial("HOPPER"));
|
||||
storageChestCache.put(mm, f);
|
||||
return f;
|
||||
}
|
||||
|
||||
public static boolean isLit(BlockData mat) {
|
||||
Material mm = mat.getMaterial();
|
||||
Boolean f = litCache.get(mm);
|
||||
|
||||
if (f != null) {
|
||||
return f;
|
||||
}
|
||||
|
||||
f = mm.equals(B.getMaterial("GLOWSTONE"))
|
||||
|| mm.equals(B.getMaterial("END_ROD"))
|
||||
|| mm.equals(B.getMaterial("SOUL_SAND"))
|
||||
|| mm.equals(B.getMaterial("TORCH"))
|
||||
|| mm.equals(Material.REDSTONE_TORCH)
|
||||
|| mm.equals(B.getMaterial("SOUL_TORCH"))
|
||||
|| mm.equals(Material.REDSTONE_WALL_TORCH)
|
||||
|| mm.equals(Material.WALL_TORCH)
|
||||
|| mm.equals(B.getMaterial("SOUL_WALL_TORCH"))
|
||||
|| mm.equals(B.getMaterial("LANTERN"))
|
||||
|| mm.equals(Material.JACK_O_LANTERN)
|
||||
|| mm.equals(Material.REDSTONE_LAMP)
|
||||
|| mm.equals(Material.MAGMA_BLOCK)
|
||||
|| mm.equals(B.getMaterial("SHROOMLIGHT"))
|
||||
|| mm.equals(B.getMaterial("SEA_LANTERN"))
|
||||
|| mm.equals(B.getMaterial("SOUL_LANTERN"))
|
||||
|| mm.equals(Material.FIRE)
|
||||
|| mm.equals(B.getMaterial("SOUL_FIRE"))
|
||||
|| mm.equals(B.getMaterial("SEA_PICKLE"))
|
||||
|| mm.equals(Material.BREWING_STAND)
|
||||
|| mm.equals(Material.REDSTONE_ORE);
|
||||
litCache.put(mm, f);
|
||||
return f;
|
||||
}
|
||||
|
||||
public static boolean isUpdatable(BlockData mat) {
|
||||
Boolean u = updatableCache.get(mat.getMaterial());
|
||||
|
||||
if (u != null) {
|
||||
return u;
|
||||
}
|
||||
|
||||
u = isLit(mat) || isStorage(mat);
|
||||
updatableCache.put(mat.getMaterial(), u);
|
||||
return u;
|
||||
}
|
||||
|
||||
public static boolean isFoliage(Material d) {
|
||||
return isFoliage(d.createBlockData());
|
||||
}
|
||||
|
||||
public static boolean isFoliage(BlockData d) {
|
||||
Boolean f = foliageCache.get(d.getMaterial());
|
||||
if (f != null) {
|
||||
return f;
|
||||
}
|
||||
|
||||
if (isFluid(d) || isAir(d) || isSolid(d)) {
|
||||
foliageCache.put(d.getMaterial(), false);
|
||||
return false;
|
||||
}
|
||||
|
||||
Material mat = d.getMaterial();
|
||||
f = mat.equals(Material.POPPY)
|
||||
|| mat.equals(Material.DANDELION)
|
||||
|| mat.equals(B.getMaterial("CORNFLOWER"))
|
||||
|| mat.equals(B.getMaterial("SWEET_BERRY_BUSH"))
|
||||
|| mat.equals(B.getMaterial("CRIMSON_ROOTS"))
|
||||
|| mat.equals(B.getMaterial("WARPED_ROOTS"))
|
||||
|| mat.equals(B.getMaterial("NETHER_SPROUTS"))
|
||||
|| mat.equals(B.getMaterial("ALLIUM"))
|
||||
|| mat.equals(B.getMaterial("AZURE_BLUET"))
|
||||
|| mat.equals(B.getMaterial("BLUE_ORCHID"))
|
||||
|| mat.equals(B.getMaterial("POPPY"))
|
||||
|| mat.equals(B.getMaterial("DANDELION"))
|
||||
|| mat.equals(B.getMaterial("OXEYE_DAISY"))
|
||||
|| mat.equals(B.getMaterial("LILY_OF_THE_VALLEY"))
|
||||
|| mat.equals(B.getMaterial("WITHER_ROSE"))
|
||||
|| mat.equals(Material.DARK_OAK_SAPLING)
|
||||
|| mat.equals(Material.ACACIA_SAPLING)
|
||||
|| mat.equals(Material.JUNGLE_SAPLING)
|
||||
|| mat.equals(Material.BIRCH_SAPLING)
|
||||
|| mat.equals(Material.SPRUCE_SAPLING)
|
||||
|| mat.equals(Material.OAK_SAPLING)
|
||||
|| mat.equals(Material.ORANGE_TULIP)
|
||||
|| mat.equals(Material.PINK_TULIP)
|
||||
|| mat.equals(Material.RED_TULIP)
|
||||
|| mat.equals(Material.WHITE_TULIP)
|
||||
|| mat.equals(Material.FERN)
|
||||
|| mat.equals(Material.LARGE_FERN)
|
||||
|| mat.equals(Material.GRASS)
|
||||
|| mat.equals(Material.TALL_GRASS);
|
||||
foliageCache.put(d.getMaterial(), f);
|
||||
return f;
|
||||
}
|
||||
|
||||
public static boolean canPlaceOnto(Material mat, Material onto) {
|
||||
String key = mat.name() + "" + onto.name();
|
||||
|
||||
if (isFoliage(mat)) {
|
||||
if (!isFoliagePlantable(onto)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (onto.equals(Material.AIR) || onto.equals(B.getMaterial("CAVE_AIR")) || onto.equals(B.getMaterial("VOID_AIR"))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (onto.equals(Material.GRASS_BLOCK) && mat.equals(Material.DEAD_BUSH)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (onto.equals(Material.DIRT_PATH)) {
|
||||
if (!mat.isSolid()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (onto.equals(Material.ACACIA_LEAVES)
|
||||
|| onto.equals(Material.BIRCH_LEAVES)
|
||||
|| onto.equals(Material.DARK_OAK_LEAVES)
|
||||
|| onto.equals(Material.JUNGLE_LEAVES)
|
||||
|| onto.equals(Material.OAK_LEAVES)
|
||||
|| onto.equals(Material.SPRUCE_LEAVES)) {
|
||||
return mat.isSolid();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean isDecorant(BlockData m) {
|
||||
Material mm = m.getMaterial();
|
||||
Boolean f = decorantCache.get(mm);
|
||||
|
||||
if (f != null) {
|
||||
return f;
|
||||
}
|
||||
|
||||
f = mm.equals(Material.GRASS)
|
||||
|| mm.equals(Material.TALL_GRASS)
|
||||
|| mm.equals(B.getMaterial("CORNFLOWER"))
|
||||
|| mm.equals(Material.SUNFLOWER)
|
||||
|| mm.equals(Material.CHORUS_FLOWER)
|
||||
|| mm.equals(Material.POPPY)
|
||||
|| mm.equals(Material.DANDELION)
|
||||
|| mm.equals(Material.OXEYE_DAISY)
|
||||
|| mm.equals(Material.ORANGE_TULIP)
|
||||
|| mm.equals(Material.PINK_TULIP)
|
||||
|| mm.equals(Material.RED_TULIP)
|
||||
|| mm.equals(Material.WHITE_TULIP)
|
||||
|| mm.equals(Material.LILAC)
|
||||
|| mm.equals(Material.DEAD_BUSH)
|
||||
|| mm.equals(B.getMaterial("SWEET_BERRY_BUSH"))
|
||||
|| mm.equals(Material.ROSE_BUSH)
|
||||
|| mm.equals(B.getMaterial("WITHER_ROSE"))
|
||||
|| mm.equals(Material.ALLIUM)
|
||||
|| mm.equals(Material.BLUE_ORCHID)
|
||||
|| mm.equals(B.getMaterial("LILY_OF_THE_VALLEY"))
|
||||
|| mm.equals(B.getMaterial("CRIMSON_FUNGUS"))
|
||||
|| mm.equals(B.getMaterial("WARPED_FUNGUS"))
|
||||
|| mm.equals(Material.RED_MUSHROOM)
|
||||
|| mm.equals(Material.BROWN_MUSHROOM)
|
||||
|| mm.equals(B.getMaterial("CRIMSON_ROOTS"))
|
||||
|| mm.equals(B.getMaterial("AZURE_BLUET"))
|
||||
|| mm.equals(B.getMaterial("WEEPING_VINES"))
|
||||
|| mm.equals(B.getMaterial("WEEPING_VINES_PLANT"))
|
||||
|| mm.equals(B.getMaterial("WARPED_ROOTS"))
|
||||
|| mm.equals(B.getMaterial("NETHER_SPROUTS"))
|
||||
|| mm.equals(B.getMaterial("TWISTING_VINES"))
|
||||
|| mm.equals(B.getMaterial("TWISTING_VINES_PLANT"))
|
||||
|| mm.equals(Material.SUGAR_CANE)
|
||||
|| mm.equals(Material.WHEAT)
|
||||
|| mm.equals(Material.POTATOES)
|
||||
|| mm.equals(Material.CARROTS)
|
||||
|| mm.equals(Material.BEETROOTS)
|
||||
|| mm.equals(Material.NETHER_WART)
|
||||
|| mm.equals(B.getMaterial("SEA_PICKLE"))
|
||||
|| mm.equals(B.getMaterial("SEAGRASS"))
|
||||
|| mm.equals(B.getMaterial("ACACIA_BUTTON"))
|
||||
|| mm.equals(B.getMaterial("BIRCH_BUTTON"))
|
||||
|| mm.equals(B.getMaterial("CRIMSON_BUTTON"))
|
||||
|| mm.equals(B.getMaterial("DARK_OAK_BUTTON"))
|
||||
|| mm.equals(B.getMaterial("JUNGLE_BUTTON"))
|
||||
|| mm.equals(B.getMaterial("OAK_BUTTON"))
|
||||
|| mm.equals(B.getMaterial("POLISHED_BLACKSTONE_BUTTON"))
|
||||
|| mm.equals(B.getMaterial("SPRUCE_BUTTON"))
|
||||
|| mm.equals(B.getMaterial("STONE_BUTTON"))
|
||||
|| mm.equals(B.getMaterial("WARPED_BUTTON"))
|
||||
|| mm.equals(Material.TORCH)
|
||||
|| mm.equals(B.getMaterial("SOUL_TORCH"));
|
||||
decorantCache.put(mm, f);
|
||||
return f;
|
||||
}
|
||||
|
||||
public static KList<BlockData> get(KList<String> find) {
|
||||
KList<BlockData> b = new KList<>();
|
||||
|
||||
for (String i : find) {
|
||||
BlockData bd = get(i);
|
||||
|
||||
if (bd != null) {
|
||||
b.add(bd);
|
||||
}
|
||||
}
|
||||
|
||||
return b;
|
||||
}
|
||||
|
||||
public static boolean isFoliagePlantable(BlockData d) {
|
||||
return d.getMaterial().equals(Material.GRASS_BLOCK)
|
||||
|| d.getMaterial().equals(Material.DIRT)
|
||||
|| d.getMaterial().equals(Material.COARSE_DIRT)
|
||||
|| d.getMaterial().equals(Material.PODZOL);
|
||||
}
|
||||
|
||||
public static boolean isFoliagePlantable(Material d) {
|
||||
return d.equals(Material.GRASS_BLOCK)
|
||||
|| d.equals(Material.DIRT)
|
||||
|| d.equals(Material.COARSE_DIRT)
|
||||
|| d.equals(Material.PODZOL);
|
||||
}
|
||||
|
||||
public static boolean isFluid(BlockData d) {
|
||||
return d.getMaterial().equals(Material.WATER) || d.getMaterial().equals(Material.LAVA);
|
||||
}
|
||||
|
||||
public static boolean isAirOrFluid(BlockData d) {
|
||||
return isAir(d) || isFluid(d);
|
||||
}
|
||||
|
||||
public static boolean isAir(BlockData d) {
|
||||
if (d == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return d.getMaterial().equals(Material.AIR) || d.getMaterial().equals(Material.CAVE_AIR) || d.getMaterial().equals(Material.VOID_AIR);
|
||||
}
|
||||
|
||||
|
||||
public static String[] getBlockTypes() {
|
||||
KList<String> bt = new KList<>();
|
||||
|
||||
for (Material i : Material.values()) {
|
||||
if (i.isBlock()) {
|
||||
String v = i.createBlockData().getAsString(true);
|
||||
|
||||
if (v.contains("[")) {
|
||||
v = v.split("\\Q[\\E")[0];
|
||||
}
|
||||
|
||||
if (v.contains(":")) {
|
||||
v = v.split("\\Q:\\E")[1];
|
||||
}
|
||||
|
||||
bt.add(v);
|
||||
}
|
||||
}
|
||||
|
||||
return bt.toArray(new String[0]);
|
||||
}
|
||||
|
||||
public static String[] getItemTypes() {
|
||||
KList<String> bt = new KList<>();
|
||||
|
||||
for (Material i : Material.values()) {
|
||||
String v = i.name().toLowerCase().trim();
|
||||
bt.add(v);
|
||||
}
|
||||
|
||||
return bt.toArray(new String[0]);
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,7 @@
|
||||
|
||||
package com.volmit.iris.engine.data;
|
||||
|
||||
import com.volmit.iris.util.KList;
|
||||
import com.volmit.iris.util.collection.KList;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
|
||||
@@ -29,9 +29,8 @@ import com.volmit.iris.engine.data.nbt.tag.CompoundTag;
|
||||
import com.volmit.iris.engine.data.nbt.tag.StringTag;
|
||||
import com.volmit.iris.engine.parallel.BurstExecutor;
|
||||
import com.volmit.iris.engine.parallel.MultiBurst;
|
||||
import com.volmit.iris.util.B;
|
||||
import com.volmit.iris.util.KList;
|
||||
import com.volmit.iris.util.KMap;
|
||||
import com.volmit.iris.util.collection.KList;
|
||||
import com.volmit.iris.util.collection.KMap;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.block.Biome;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
|
||||
@@ -0,0 +1,229 @@
|
||||
/*
|
||||
* Iris is a World Generator for Minecraft Bukkit Servers
|
||||
* Copyright (c) 2021 Arcane Arts (Volmit Software)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.volmit.iris.engine.data.chunk;
|
||||
|
||||
import com.volmit.iris.Iris;
|
||||
import com.volmit.iris.core.nms.BiomeBaseInjector;
|
||||
import com.volmit.iris.core.nms.INMS;
|
||||
import com.volmit.iris.util.data.IrisBiomeStorage;
|
||||
import com.volmit.iris.util.fakenews.HeightedFakeWorld;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.Biome;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
import org.bukkit.generator.ChunkGenerator.BiomeGrid;
|
||||
import org.bukkit.generator.ChunkGenerator.ChunkData;
|
||||
import org.bukkit.material.MaterialData;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public class LinkedTerrainChunk implements TerrainChunk {
|
||||
private final Biome[] biome2D;
|
||||
private final IrisBiomeStorage biome3D;
|
||||
private ChunkData rawChunkData;
|
||||
private final BiomeGrid storage;
|
||||
|
||||
public LinkedTerrainChunk(int maxHeight) {
|
||||
this(null, maxHeight);
|
||||
}
|
||||
|
||||
public LinkedTerrainChunk(BiomeGrid storage, ChunkData data) {
|
||||
this.storage = storage;
|
||||
rawChunkData = data;
|
||||
biome2D = storage != null ? null : Iris.biome3d ? null : new Biome[256];
|
||||
biome3D = storage != null ? null : Iris.biome3d ? new IrisBiomeStorage() : null;
|
||||
}
|
||||
|
||||
public LinkedTerrainChunk(BiomeGrid storage, int maxHeight) {
|
||||
this.storage = storage;
|
||||
rawChunkData = createChunkData(maxHeight);
|
||||
biome2D = storage != null ? null : Iris.biome3d ? null : new Biome[256];
|
||||
biome3D = storage != null ? null : Iris.biome3d ? new IrisBiomeStorage() : null;
|
||||
}
|
||||
|
||||
private ChunkData createChunkData(int maxHeight) {
|
||||
try {
|
||||
return Bukkit.createChunkData(new HeightedFakeWorld(maxHeight));
|
||||
} catch (Throwable e) {Iris.reportError(e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BiomeBaseInjector getBiomeBaseInjector() {
|
||||
return (x, y, z, bb) -> INMS.get().forceBiomeInto(x, y, z, bb, storage);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Biome getBiome(int x, int z) {
|
||||
if (storage != null) {
|
||||
return storage.getBiome(x, z);
|
||||
}
|
||||
|
||||
if (biome2D != null) {
|
||||
return biome2D[(z << 4) | x];
|
||||
}
|
||||
|
||||
return biome3D.getBiome(x, 0, z);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Biome getBiome(int x, int y, int z) {
|
||||
if (storage != null) {
|
||||
return storage.getBiome(x, y, z);
|
||||
}
|
||||
|
||||
if (biome2D != null) {
|
||||
return biome2D[(z << 4) | x];
|
||||
}
|
||||
|
||||
return biome3D.getBiome(x, y, z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBiome(int x, int z, Biome bio) {
|
||||
if (storage != null) {
|
||||
storage.setBiome(x, z, bio);
|
||||
return;
|
||||
}
|
||||
|
||||
if (biome2D != null) {
|
||||
biome2D[(z << 4) | x] = bio;
|
||||
return;
|
||||
}
|
||||
|
||||
biome3D.setBiome(x, 0, z, bio);
|
||||
}
|
||||
|
||||
public BiomeGrid getRawBiome() {
|
||||
return storage;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBiome(int x, int y, int z, Biome bio) {
|
||||
if (storage != null) {
|
||||
storage.setBiome(x, y, z, bio);
|
||||
return;
|
||||
}
|
||||
|
||||
if (biome2D != null) {
|
||||
biome2D[(z << 4) | x] = bio;
|
||||
return;
|
||||
}
|
||||
|
||||
biome3D.setBiome(x, y, z, bio);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMinHeight() {
|
||||
return rawChunkData.getMinHeight();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxHeight() {
|
||||
return rawChunkData.getMaxHeight();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBlock(int x, int y, int z, BlockData blockData) {
|
||||
rawChunkData.setBlock(x, y, z, blockData);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public BlockData getBlockData(int x, int y, int z) {
|
||||
return rawChunkData.getBlockData(x, y, z);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
@Override
|
||||
public void setBlock(int x, int y, int z, @NotNull Material material) {
|
||||
rawChunkData.setBlock(x, y, z, material);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
@Override
|
||||
public void setBlock(int x, int y, int z, @NotNull MaterialData material) {
|
||||
rawChunkData.setBlock(x, y, z, material);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
@Override
|
||||
public void setRegion(int xMin, int yMin, int zMin, int xMax, int yMax, int zMax, @NotNull Material material) {
|
||||
rawChunkData.setRegion(xMin, yMin, zMin, xMax, yMax, zMax, material);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
@Override
|
||||
public void setRegion(int xMin, int yMin, int zMin, int xMax, int yMax, int zMax, @NotNull MaterialData material) {
|
||||
rawChunkData.setRegion(xMin, yMin, zMin, xMax, yMax, zMax, material);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRegion(int xMin, int yMin, int zMin, int xMax, int yMax, int zMax, @NotNull BlockData blockData) {
|
||||
rawChunkData.setRegion(xMin, yMin, zMin, xMax, yMax, zMax, blockData);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Deprecated
|
||||
@Override
|
||||
public Material getType(int x, int y, int z) {
|
||||
return rawChunkData.getType(x, y, z);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Deprecated
|
||||
@Override
|
||||
public MaterialData getTypeAndData(int x, int y, int z) {
|
||||
return rawChunkData.getTypeAndData(x, y, z);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
@Override
|
||||
public byte getData(int x, int y, int z) {
|
||||
return rawChunkData.getData(x, y, z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChunkData getRaw() {
|
||||
return rawChunkData;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRaw(ChunkData data) {
|
||||
rawChunkData = data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void inject(BiomeGrid biome) {
|
||||
if (biome2D != null) {
|
||||
for (int i = 0; i < 16; i++) {
|
||||
for (int j = 0; j < 16; j++) {
|
||||
biome.setBiome(i, j, getBiome(i, j));
|
||||
}
|
||||
}
|
||||
} else if (biome3D != null) {
|
||||
biome3D.inject(biome);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
/*
|
||||
* Iris is a World Generator for Minecraft Bukkit Servers
|
||||
* Copyright (c) 2021 Arcane Arts (Volmit Software)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.volmit.iris.engine.data.chunk;
|
||||
|
||||
import com.volmit.iris.core.nms.BiomeBaseInjector;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.Biome;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
import org.bukkit.generator.ChunkGenerator.BiomeGrid;
|
||||
import org.bukkit.generator.ChunkGenerator.ChunkData;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public interface TerrainChunk extends BiomeGrid, ChunkData {
|
||||
static TerrainChunk create(World world) {
|
||||
return create(world.getMaxHeight());
|
||||
}
|
||||
|
||||
static TerrainChunk create(int maxHeight) {
|
||||
return new LinkedTerrainChunk(maxHeight);
|
||||
}
|
||||
|
||||
static TerrainChunk create(World world, BiomeGrid grid) {
|
||||
return create(world.getMaxHeight(), grid);
|
||||
}
|
||||
|
||||
static TerrainChunk create(ChunkData raw, BiomeGrid grid) {
|
||||
return new LinkedTerrainChunk(grid, raw);
|
||||
}
|
||||
|
||||
static TerrainChunk create(int maxHeight, BiomeGrid grid) {
|
||||
return new LinkedTerrainChunk(grid, maxHeight);
|
||||
}
|
||||
|
||||
BiomeBaseInjector getBiomeBaseInjector();
|
||||
|
||||
void setRaw(ChunkData data);
|
||||
|
||||
/**
|
||||
* Get biome at x, z within chunk being generated
|
||||
*
|
||||
* @param x - 0-15
|
||||
* @param z - 0-15
|
||||
* @return Biome value
|
||||
* @deprecated biomes are now 3-dimensional
|
||||
*/
|
||||
@NotNull
|
||||
@Deprecated
|
||||
Biome getBiome(int x, int z);
|
||||
|
||||
/**
|
||||
* Get biome at x, z within chunk being generated
|
||||
*
|
||||
* @param x - 0-15
|
||||
* @param y - 0-255
|
||||
* @param z - 0-15
|
||||
* @return Biome value
|
||||
*/
|
||||
@NotNull
|
||||
Biome getBiome(int x, int y, int z);
|
||||
|
||||
/**
|
||||
* Set biome at x, z within chunk being generated
|
||||
*
|
||||
* @param x - 0-15
|
||||
* @param z - 0-15
|
||||
* @param bio - Biome value
|
||||
* @deprecated biomes are now 3-dimensional
|
||||
*/
|
||||
@Deprecated
|
||||
void setBiome(int x, int z, @NotNull Biome bio);
|
||||
|
||||
/**
|
||||
* Set biome at x, z within chunk being generated
|
||||
*
|
||||
* @param x - 0-15
|
||||
* @param y - 0-255
|
||||
* @param z - 0-15
|
||||
* @param bio - Biome value
|
||||
*/
|
||||
void setBiome(int x, int y, int z, @NotNull Biome bio);
|
||||
|
||||
/**
|
||||
* Get the maximum height for the chunk.
|
||||
* <p>
|
||||
* Setting blocks at or above this height will do nothing.
|
||||
*
|
||||
* @return the maximum height
|
||||
*/
|
||||
int getMaxHeight();
|
||||
|
||||
/**
|
||||
* Set the block at x,y,z in the chunk data to material.
|
||||
* <p>
|
||||
* Setting blocks outside the chunk's bounds does nothing.
|
||||
*
|
||||
* @param x the x location in the chunk from 0-15 inclusive
|
||||
* @param y the y location in the chunk from 0 (inclusive) - maxHeight
|
||||
* (exclusive)
|
||||
* @param z the z location in the chunk from 0-15 inclusive
|
||||
* @param blockData the type to set the block to
|
||||
*/
|
||||
void setBlock(int x, int y, int z, @NotNull BlockData blockData);
|
||||
|
||||
/**
|
||||
* Get the type and data of the block at x, y, z.
|
||||
* <p>
|
||||
* Getting blocks outside the chunk's bounds returns air.
|
||||
*
|
||||
* @param x the x location in the chunk from 0-15 inclusive
|
||||
* @param y the y location in the chunk from 0 (inclusive) - maxHeight
|
||||
* (exclusive)
|
||||
* @param z the z location in the chunk from 0-15 inclusive
|
||||
* @return the data of the block or the BlockData for air if x, y or z are
|
||||
* outside the chunk's bounds
|
||||
*/
|
||||
@NotNull
|
||||
BlockData getBlockData(int x, int y, int z);
|
||||
|
||||
ChunkData getRaw();
|
||||
|
||||
void inject(BiomeGrid biome);
|
||||
}
|
||||
@@ -0,0 +1,228 @@
|
||||
/*
|
||||
* Iris is a World Generator for Minecraft Bukkit Servers
|
||||
* Copyright (c) 2021 Arcane Arts (Volmit Software)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.volmit.iris.engine.data.loader;
|
||||
|
||||
import com.volmit.iris.Iris;
|
||||
import com.volmit.iris.core.IrisDataManager;
|
||||
import com.volmit.iris.engine.object.IrisObject;
|
||||
import com.volmit.iris.util.collection.KList;
|
||||
import com.volmit.iris.util.collection.KMap;
|
||||
import com.volmit.iris.util.collection.KSet;
|
||||
import com.volmit.iris.util.format.C;
|
||||
import com.volmit.iris.util.format.Form;
|
||||
import com.volmit.iris.util.math.M;
|
||||
import com.volmit.iris.util.scheduling.ChronoLatch;
|
||||
import com.volmit.iris.util.scheduling.J;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class ObjectResourceLoader extends ResourceLoader<IrisObject> {
|
||||
private final ChronoLatch useFlip = new ChronoLatch(2222);
|
||||
private final KMap<String, Long> useCache = new KMap<>();
|
||||
private final ChronoLatch cl;
|
||||
private final AtomicInteger unload;
|
||||
|
||||
public ObjectResourceLoader(File root, IrisDataManager idm, String folderName, String resourceTypeName) {
|
||||
super(root, idm, folderName, resourceTypeName, IrisObject.class);
|
||||
cl = new ChronoLatch(30000);
|
||||
unload = new AtomicInteger(0);
|
||||
}
|
||||
|
||||
public int getSize() {
|
||||
return loadCache.size();
|
||||
}
|
||||
|
||||
public int getTotalStorage() {
|
||||
int m = 0;
|
||||
|
||||
for (IrisObject i : loadCache.values()) {
|
||||
m += i.getBlocks().size();
|
||||
}
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
public void clean() {
|
||||
if (useFlip.flip()) {
|
||||
unloadLast(30000);
|
||||
}
|
||||
}
|
||||
|
||||
public void unloadLast(long age) {
|
||||
String v = getOldest();
|
||||
|
||||
if (v == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (M.ms() - useCache.get(v) > age) {
|
||||
unload(v);
|
||||
}
|
||||
}
|
||||
|
||||
private String getOldest() {
|
||||
long min = M.ms();
|
||||
String v = null;
|
||||
|
||||
for (String i : useCache.k()) {
|
||||
long t = useCache.get(i);
|
||||
if (t < min) {
|
||||
min = t;
|
||||
v = i;
|
||||
}
|
||||
}
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
private void unload(String v) {
|
||||
lock.lock();
|
||||
useCache.remove(v);
|
||||
loadCache.remove(v);
|
||||
lock.unlock();
|
||||
unload.getAndIncrement();
|
||||
|
||||
if (unload.get() == 1) {
|
||||
cl.flip();
|
||||
}
|
||||
|
||||
if (cl.flip()) {
|
||||
J.a(() -> {
|
||||
Iris.verbose("Unloaded " + C.WHITE + unload.get() + " " + resourceTypeName + (unload.get() == 1 ? "" : "s") + C.GRAY + " to optimize memory usage." + " (" + Form.f(getLoadCache().size()) + " " + resourceTypeName + (loadCache.size() == 1 ? "" : "s") + " Loaded)");
|
||||
unload.set(0);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public IrisObject loadFile(File j, String key, String name) {
|
||||
lock.lock();
|
||||
try {
|
||||
IrisObject t = new IrisObject(0, 0, 0);
|
||||
t.read(j);
|
||||
loadCache.put(key, t);
|
||||
logLoad(j);
|
||||
t.setLoadKey(name);
|
||||
t.setLoader(manager);
|
||||
t.setLoadFile(j);
|
||||
lock.unlock();
|
||||
return t;
|
||||
} catch (Throwable e) {
|
||||
Iris.reportError(e);
|
||||
lock.unlock();
|
||||
Iris.warn("Couldn't read " + resourceTypeName + " file: " + j.getPath() + ": " + e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public String[] getPossibleKeys() {
|
||||
if (possibleKeys != null) {
|
||||
return possibleKeys;
|
||||
}
|
||||
|
||||
Iris.info("Building " + resourceTypeName + " Possibility Lists");
|
||||
KSet<String> m = new KSet<>();
|
||||
|
||||
for (File i : getFolders()) {
|
||||
for (File j : i.listFiles()) {
|
||||
if (j.isFile() && j.getName().endsWith(".iob")) {
|
||||
m.add(j.getName().replaceAll("\\Q.iob\\E", ""));
|
||||
} else if (j.isDirectory()) {
|
||||
for (File k : j.listFiles()) {
|
||||
if (k.isFile() && k.getName().endsWith(".iob")) {
|
||||
m.add(j.getName() + "/" + k.getName().replaceAll("\\Q.iob\\E", ""));
|
||||
} else if (k.isDirectory()) {
|
||||
for (File l : k.listFiles()) {
|
||||
if (l.isFile() && l.getName().endsWith(".iob")) {
|
||||
m.add(j.getName() + "/" + k.getName() + "/" + l.getName().replaceAll("\\Q.iob\\E", ""));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
KList<String> v = new KList<>(m);
|
||||
possibleKeys = v.toArray(new String[0]);
|
||||
return possibleKeys;
|
||||
}
|
||||
|
||||
public File findFile(String name) {
|
||||
lock.lock();
|
||||
for (File i : getFolders(name)) {
|
||||
for (File j : i.listFiles()) {
|
||||
if (j.isFile() && j.getName().endsWith(".iob") && j.getName().split("\\Q.\\E")[0].equals(name)) {
|
||||
lock.unlock();
|
||||
return j;
|
||||
}
|
||||
}
|
||||
|
||||
File file = new File(i, name + ".iob");
|
||||
|
||||
if (file.exists()) {
|
||||
lock.unlock();
|
||||
return file;
|
||||
}
|
||||
}
|
||||
|
||||
Iris.warn("Couldn't find " + resourceTypeName + ": " + name);
|
||||
|
||||
lock.unlock();
|
||||
return null;
|
||||
}
|
||||
|
||||
public IrisObject load(String name) {
|
||||
return load(name, true);
|
||||
}
|
||||
|
||||
public IrisObject load(String name, boolean warn) {
|
||||
String key = name + "-" + objectClass.getCanonicalName();
|
||||
|
||||
if (loadCache.containsKey(key)) {
|
||||
IrisObject t = loadCache.get(key);
|
||||
useCache.put(key, M.ms());
|
||||
return t;
|
||||
}
|
||||
|
||||
lock.lock();
|
||||
for (File i : getFolders(name)) {
|
||||
for (File j : i.listFiles()) {
|
||||
if (j.isFile() && j.getName().endsWith(".iob") && j.getName().split("\\Q.\\E")[0].equals(name)) {
|
||||
useCache.put(key, M.ms());
|
||||
lock.unlock();
|
||||
return loadFile(j, key, name);
|
||||
}
|
||||
}
|
||||
|
||||
File file = new File(i, name + ".iob");
|
||||
|
||||
if (file.exists()) {
|
||||
useCache.put(key, M.ms());
|
||||
lock.unlock();
|
||||
return loadFile(file, key, name);
|
||||
}
|
||||
}
|
||||
|
||||
Iris.warn("Couldn't find " + resourceTypeName + ": " + name);
|
||||
|
||||
lock.unlock();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,265 @@
|
||||
/*
|
||||
* Iris is a World Generator for Minecraft Bukkit Servers
|
||||
* Copyright (c) 2021 Arcane Arts (Volmit Software)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.volmit.iris.engine.data.loader;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.volmit.iris.Iris;
|
||||
import com.volmit.iris.core.IrisDataManager;
|
||||
import com.volmit.iris.engine.object.IrisRegistrant;
|
||||
import com.volmit.iris.util.collection.KList;
|
||||
import com.volmit.iris.util.collection.KMap;
|
||||
import com.volmit.iris.util.collection.KSet;
|
||||
import com.volmit.iris.util.format.C;
|
||||
import com.volmit.iris.util.format.Form;
|
||||
import com.volmit.iris.util.io.IO;
|
||||
import com.volmit.iris.util.scheduling.ChronoLatch;
|
||||
import com.volmit.iris.util.scheduling.IrisLock;
|
||||
import com.volmit.iris.util.scheduling.J;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
@Data
|
||||
public class ResourceLoader<T extends IrisRegistrant> {
|
||||
protected File root;
|
||||
protected String folderName;
|
||||
protected String resourceTypeName;
|
||||
protected KMap<String, File> folderMapCache;
|
||||
protected KMap<String, T> loadCache;
|
||||
protected KList<File> folderCache;
|
||||
protected Class<? extends T> objectClass;
|
||||
protected String cname;
|
||||
protected IrisLock lock;
|
||||
protected String[] possibleKeys = null;
|
||||
protected IrisDataManager manager;
|
||||
protected AtomicInteger loads;
|
||||
protected ChronoLatch sec;
|
||||
|
||||
public ResourceLoader(File root, IrisDataManager manager, String folderName, String resourceTypeName, Class<? extends T> objectClass) {
|
||||
lock = new IrisLock("Res");
|
||||
this.manager = manager;
|
||||
sec = new ChronoLatch(5000);
|
||||
loads = new AtomicInteger();
|
||||
folderMapCache = new KMap<>();
|
||||
this.objectClass = objectClass;
|
||||
cname = objectClass.getCanonicalName();
|
||||
this.resourceTypeName = resourceTypeName;
|
||||
this.root = root;
|
||||
this.folderName = folderName;
|
||||
loadCache = new KMap<>();
|
||||
}
|
||||
|
||||
public void logLoad(File path) {
|
||||
loads.getAndIncrement();
|
||||
|
||||
if (loads.get() == 1) {
|
||||
sec.flip();
|
||||
}
|
||||
|
||||
if (sec.flip()) {
|
||||
J.a(() -> {
|
||||
Iris.verbose("Loaded " + C.WHITE + loads.get() + " " + resourceTypeName + (loads.get() == 1 ? "" : "s") + C.GRAY + " (" + Form.f(getLoadCache().size()) + " " + resourceTypeName + (loadCache.size() == 1 ? "" : "s") + " Loaded)");
|
||||
loads.set(0);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public void failLoad(File path, Throwable e) {
|
||||
J.a(() -> Iris.warn("Couldn't Load " + resourceTypeName + " file: " + path.getPath() + ": " + e.getMessage()));
|
||||
}
|
||||
|
||||
public String[] getPossibleKeys() {
|
||||
if (possibleKeys != null) {
|
||||
return possibleKeys;
|
||||
}
|
||||
|
||||
Iris.info("Building " + resourceTypeName + " Registry Lists");
|
||||
KSet<String> m = new KSet<>();
|
||||
|
||||
for (File i : getFolders()) {
|
||||
for (File j : i.listFiles()) {
|
||||
if (j.isFile() && j.getName().endsWith(".json")) {
|
||||
m.add(j.getName().replaceAll("\\Q.json\\E", ""));
|
||||
} else if (j.isDirectory()) {
|
||||
for (File k : j.listFiles()) {
|
||||
if (k.isFile() && k.getName().endsWith(".json")) {
|
||||
m.add(j.getName() + "/" + k.getName().replaceAll("\\Q.json\\E", ""));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
KList<String> v = new KList<>(m);
|
||||
possibleKeys = v.toArray(new String[0]);
|
||||
return possibleKeys;
|
||||
}
|
||||
|
||||
public long count() {
|
||||
return loadCache.size();
|
||||
}
|
||||
|
||||
protected T loadFile(File j, String key, String name) {
|
||||
try {
|
||||
T t = new Gson().fromJson(IO.readAll(j), objectClass);
|
||||
loadCache.put(key, t);
|
||||
logLoad(j);
|
||||
t.setLoadKey(name);
|
||||
t.setLoadFile(j);
|
||||
t.setLoader(manager);
|
||||
lock.unlock();
|
||||
return t;
|
||||
} catch (Throwable e) {Iris.reportError(e);
|
||||
lock.unlock();
|
||||
failLoad(j, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public KList<T> loadAll(KList<String> s)
|
||||
{
|
||||
KList<T> m = new KList<>();
|
||||
|
||||
for(String i : s)
|
||||
{
|
||||
T t = load(i);
|
||||
|
||||
if(t != null)
|
||||
{
|
||||
m.add(t);
|
||||
}
|
||||
}
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
public T load(String name) {
|
||||
return load(name, true);
|
||||
}
|
||||
|
||||
public T load(String name, boolean warn) {
|
||||
if (name == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (name.trim().isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String key = name + "-" + cname;
|
||||
|
||||
if (loadCache.containsKey(key)) {
|
||||
return loadCache.get(key);
|
||||
}
|
||||
|
||||
lock.lock();
|
||||
for (File i : getFolders(name)) {
|
||||
for (File j : i.listFiles()) {
|
||||
if (j.isFile() && j.getName().endsWith(".json") && j.getName().split("\\Q.\\E")[0].equals(name)) {
|
||||
return loadFile(j, key, name);
|
||||
}
|
||||
}
|
||||
|
||||
File file = new File(i, name + ".json");
|
||||
|
||||
if (file.exists()) {
|
||||
return loadFile(file, key, name);
|
||||
}
|
||||
}
|
||||
|
||||
if (warn && !resourceTypeName.equals("Dimension")) {
|
||||
J.a(() -> Iris.warn("Couldn't find " + resourceTypeName + ": " + name));
|
||||
}
|
||||
|
||||
lock.unlock();
|
||||
return null;
|
||||
}
|
||||
|
||||
public KList<File> getFolders() {
|
||||
lock.lock();
|
||||
if (folderCache == null) {
|
||||
folderCache = new KList<>();
|
||||
|
||||
for (File i : root.listFiles()) {
|
||||
if (i.isDirectory()) {
|
||||
if (i.getName().equals(folderName)) {
|
||||
folderCache.add(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
lock.unlock();
|
||||
|
||||
return folderCache;
|
||||
}
|
||||
|
||||
public KList<File> getFolders(String rc) {
|
||||
KList<File> folders = getFolders().copy();
|
||||
|
||||
if (rc.contains(":")) {
|
||||
for (File i : folders.copy()) {
|
||||
if (!rc.startsWith(i.getName() + ":")) {
|
||||
folders.remove(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return folders;
|
||||
}
|
||||
|
||||
public void clearCache() {
|
||||
lock.lock();
|
||||
possibleKeys = null;
|
||||
loadCache.clear();
|
||||
folderCache = null;
|
||||
lock.unlock();
|
||||
}
|
||||
|
||||
public File fileFor(T b) {
|
||||
for (File i : getFolders()) {
|
||||
for (File j : i.listFiles()) {
|
||||
if (j.isFile() && j.getName().endsWith(".json") && j.getName().split("\\Q.\\E")[0].equals(b.getLoadKey())) {
|
||||
return j;
|
||||
}
|
||||
}
|
||||
|
||||
File file = new File(i, b.getLoadKey() + ".json");
|
||||
|
||||
if (file.exists()) {
|
||||
return file;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean isLoaded(String next) {
|
||||
return loadCache.containsKey(next);
|
||||
}
|
||||
|
||||
public void clearList() {
|
||||
lock.lock();
|
||||
folderCache = null;
|
||||
possibleKeys = null;
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
@@ -23,7 +23,7 @@ import com.volmit.iris.engine.data.nbt.tag.ByteArrayTag;
|
||||
import com.volmit.iris.engine.data.nbt.tag.CompoundTag;
|
||||
import com.volmit.iris.engine.data.nbt.tag.ListTag;
|
||||
import com.volmit.iris.engine.data.nbt.tag.LongArrayTag;
|
||||
import com.volmit.iris.util.KMap;
|
||||
import com.volmit.iris.util.collection.KMap;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
package com.volmit.iris.engine.data.nbt.tag;
|
||||
|
||||
import com.volmit.iris.engine.data.io.MaxDepthIO;
|
||||
import com.volmit.iris.util.KMap;
|
||||
import com.volmit.iris.util.collection.KMap;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
package com.volmit.iris.engine.data.nbt.tag;
|
||||
|
||||
import com.volmit.iris.engine.data.io.MaxDepthIO;
|
||||
import com.volmit.iris.util.KList;
|
||||
import com.volmit.iris.util.collection.KList;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
Reference in New Issue
Block a user