Dang, BlockState is hard

YEET
This commit is contained in:
ah-OOG-ah committed 2024-02-29 21:17:46 -05:00
1 parent fff5b2cb01
commit 9c81c96bb3
4 files changed
+44 -54

No files matched your search

@@ -1,14 +0,0 @@
package com.gtnewhorizons.angelica.api;
import net.minecraftforge.common.util.ForgeDirection;
/**
* <p>Modern versions have BlockState, which allows you to query properties in a fairly standardized way. We don't have
* that on 1.7.10, so let's roll our own. Blocks can implement this to convert metas to properties.
*/
public interface BlockState {
default boolean hasFacing() { return false; }
default ForgeDirection getFacing(int meta) { return ForgeDirection.UNKNOWN; }
}
@@ -3,7 +3,6 @@ package com.gtnewhorizons.angelica.common;
import com.gtnewhorizons.angelica.api.QuadProvider;
import com.gtnewhorizons.angelica.compat.mojang.BlockPos;
import com.gtnewhorizons.angelica.compat.nd.Quad;
import com.gtnewhorizons.angelica.api.BlockState;
import com.gtnewhorizons.angelica.models.json.JsonModel;
import com.gtnewhorizons.angelica.models.json.Loader;
import com.gtnewhorizons.angelica.utils.ObjectPooler;
@@ -19,7 +18,7 @@ import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import org.jetbrains.annotations.NotNull;
public class BlockTest extends Block implements QuadProvider, BlockState {
public class BlockTest extends Block implements QuadProvider {
public static final ResourceLocation modelId = new ResourceLocation("blocks/lectern");
public static final List<Quad> EMPTY = ObjectImmutableList.of();
@@ -29,6 +28,11 @@ public class BlockTest extends Block implements QuadProvider, BlockState {
super(Material.rock);
}
@Override
public boolean isOpaqueCube() {
return false;
}
@Override
public void registerBlockIcons(IIconRegister reg) {
@@ -61,15 +65,4 @@ public class BlockTest extends Block implements QuadProvider, BlockState {
// Face the placed side
return side;
}
@Override
public boolean hasFacing() {
return true;
}
@Override
public ForgeDirection getFacing(int meta) {
return ForgeDirection.values()[meta];
}
}
@@ -1,7 +1,5 @@
package com.gtnewhorizons.angelica.models.json;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
@@ -48,7 +46,6 @@ public class JsonModel implements QuadProvider {
private final Map<String, String> textures;
private final Map<String, String> flatTextures = new Object2ObjectOpenHashMap<>();
private List<ModelElement> elements;
static final Gson GSON = new GsonBuilder().registerTypeAdapter(JsonModel.class, new Deserializer()).create();
private List<Quad> bakedQuadStore = new ObjectArrayList<>();
JsonModel(@Nullable ResourceLocation parentId, boolean useAO, Map<ModelDisplay.Position, ModelDisplay> display, Map<String, String> textures, List<ModelElement> elements) {
@@ -128,11 +125,16 @@ public class JsonModel implements QuadProvider {
@Override
public List<Quad> getQuads(IBlockAccess world, BlockPos pos, Block block, int meta, ForgeDirection dir, Random random, ObjectPooler<Quad> quadPool) {
return this.bakedQuadStore.stream().map(q -> {
final Quad q1 = quadPool.getInstance();
q1.copyFrom(q);
return q1;
}).collect(ObjectImmutableList.toListWithExpectedSize(bakedQuadStore.size()));
final List<Quad> ret = new ObjectArrayList<>(this.bakedQuadStore.size());
//noinspection ForLoopReplaceableByForEach
for (int i = 0; i < bakedQuadStore.size(); ++i) {
final Quad q = quadPool.getInstance();
q.copyFrom(this.bakedQuadStore.get(i));
ret.add(q);
}
return ret;
}
public void resolveParents(Function<ResourceLocation, JsonModel> modelLoader) {
@@ -178,7 +180,7 @@ public class JsonModel implements QuadProvider {
}
}
private static class Deserializer implements JsonDeserializer<JsonModel> {
static class Deserializer implements JsonDeserializer<JsonModel> {
private Vector3f loadVec3(JsonObject in, String name) {
@@ -1,5 +1,7 @@
package com.gtnewhorizons.angelica.models.json;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.gtnewhorizons.angelica.loading.AngelicaTweaker;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
@@ -8,20 +10,23 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Map;
import net.minecraft.block.Block;
import net.minecraft.util.ResourceLocation;
import static com.gtnewhorizons.angelica.models.json.JsonModel.GSON;
/**
* Model loading should proceed as follows:
* Models to load are registered in PREINIT.
* All registered models are recursively loaded in INIT.
* Models registered for baking are baked on the first tick of the game.
*
* As for icons, register them Block::registerBlockIcons(). Whatever gets them in the block texture atlas.
* Model loading should proceed as follows: <ul>
* <li>Models to load are registered in PREINIT.</li>
* <li>All registered models are recursively loaded in INIT.</li>
* <li>Models registered for baking are baked on the first tick of the game.</li>
*</ul>
* <p>Alternatively, you can register blocks with blockstate variants in preinit, and the rest is handled by
* {@link Loader}.
* <p>As for icons, register them in {@link Block#registerBlockIcons}. Whatever gets them in the block texture atlas.
*/
public class Loader {
static final Gson GSON = new GsonBuilder()
.registerTypeAdapter(JsonModel.class, new JsonModel.Deserializer()).create();
private static final List<ResourceLocation> unloadedModels = new ObjectArrayList<>();
private static final Map<ResourceLocation, JsonModel> loadedModels = new Object2ObjectOpenHashMap<>();
private static final List<ResourceLocation> modelsToBake = new ObjectArrayList<>();
@@ -38,18 +43,22 @@ public class Loader {
if (l == null) continue;
if (loadedModels.containsKey(l)) continue;
try (final InputStream is = Loader.class.getResourceAsStream(
"/assets/" + l.getResourceDomain() + "/models/" + l.getResourcePath() + ".json"
)) {
final JsonModel model = loadJson(
"/assets/" + l.getResourceDomain() + "/models/" + l.getResourcePath() + ".json", JsonModel.class);
unloadedModels.addAll(model.getParents());
loadedModels.put(l, model);
}
}
final JsonModel model = GSON.fromJson(new InputStreamReader(is), JsonModel.class);
unloadedModels.addAll(model.getParents());
loadedModels.put(l, model);
} catch (IOException | NullPointerException e) {
private static <T> T loadJson(String path, Class<T> clazz) {
try (final InputStream is = Loader.class.getResourceAsStream(path)) {
AngelicaTweaker.LOGGER.fatal("Could not find /assets/" + l.getResourceDomain() + "/models/" + l.getResourcePath() + ".json");
throw new RuntimeException(e);
}
return GSON.fromJson(new InputStreamReader(is), clazz);
} catch (IOException | NullPointerException e) {
AngelicaTweaker.LOGGER.fatal("Could not find " + path);
throw new RuntimeException(e);
}
}