Skip to content

Commit

Permalink
Propped Frameworks
Browse files Browse the repository at this point in the history
  • Loading branch information
maximumpower55 committed Feb 3, 2024
1 parent 3f3faa6 commit 4049643
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.github.fusionflux.portalcubed.content.PortalCubedSerializers;
import io.github.fusionflux.portalcubed.content.PortalCubedSounds;
import io.github.fusionflux.portalcubed.content.PortalCubedTabs;
import io.github.fusionflux.portalcubed.content.prop.PropType;
import io.github.fusionflux.portalcubed.data.tags.PortalCubedEntityTags;
import io.github.fusionflux.portalcubed.framework.registration.Registrar;
import net.minecraft.Util;
Expand Down Expand Up @@ -40,6 +41,7 @@ public void onInitialize(ModContainer mod) {
PortalCubedItems.init();
PortalCubedTabs.init();
PortalCubedEntities.init();
PropType.init();
PortalCubedSerializers.init();

PortalCubedSounds.init();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.github.fusionflux.portalcubed.content.PortalCubedColorProviders;
import io.github.fusionflux.portalcubed.content.portal.PortalRenderer;
import io.github.fusionflux.portalcubed.content.prop.PropModels;
import io.github.fusionflux.portalcubed.framework.model.PortalCubedModelLoadingPlugin;
import io.github.fusionflux.portalcubed.framework.model.emissive.EmissiveLoader;
import net.fabricmc.fabric.api.client.model.loading.v1.PreparableModelLoadingPlugin;
Expand All @@ -15,6 +16,7 @@ public void onInitializeClient(ModContainer mod) {
PortalRenderer.init();
PortalCubedColorProviders.init();

PropModels.register();
PreparableModelLoadingPlugin.register(EmissiveLoader.INSTANCE, PortalCubedModelLoadingPlugin.INSTANCE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.github.fusionflux.portalcubed.content.prop;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializers;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.level.Level;

public class PropEntity extends Entity {
private static final EntityDataAccessor<Integer> VARIANT = SynchedEntityData.defineId(PropEntity.class, EntityDataSerializers.INT);

public final PropType type;

public PropEntity(PropType type, EntityType<?> entityType, Level level) {
super(entityType, level);
this.type = type;
}

public int getVariant() {
return entityData.get(VARIANT);
}

private void setVariant(int variant) {
entityData.set(VARIANT, variant);
}

@Override
protected void defineSynchedData() {
entityData.define(VARIANT, 0);
}

@Override
protected void addAdditionalSaveData(CompoundTag tag) {
tag.putInt("CustomModelData", getVariant());
}

@Override
protected void readAdditionalSaveData(CompoundTag tag) {
setVariant(tag.getInt("CustomModelData"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package io.github.fusionflux.portalcubed.content.prop;

import org.jetbrains.annotations.NotNull;
import org.quiltmc.qsl.resource.loader.api.ResourceLoader;
import org.quiltmc.qsl.resource.loader.api.reloader.ResourceReloaderKeys;
import org.quiltmc.qsl.resource.loader.api.reloader.SimpleSynchronousResourceReloader;

import io.github.fusionflux.portalcubed.PortalCubed;
import it.unimi.dsi.fastutil.objects.Object2ReferenceOpenHashMap;
import it.unimi.dsi.fastutil.objects.ReferenceArrayList;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.PackType;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.world.item.ItemStack;

public class PropModels implements SimpleSynchronousResourceReloader {
public static void register() {
var resourceLoader = ResourceLoader.get(PackType.CLIENT_RESOURCES);
resourceLoader.addReloaderOrdering(ResourceReloaderKeys.Client.MODELS, ID);
resourceLoader.registerReloader(new PropModels());
}

public static BakedModel getModel(PropType type, int variant) {
return MODELS.get(type).get(variant);
}

public static final ResourceLocation ID = PortalCubed.id("prop_models");
public static final Object2ReferenceOpenHashMap<PropType, ReferenceArrayList<BakedModel>> MODELS = new Object2ReferenceOpenHashMap<>();

@Override
public @NotNull ResourceLocation getQuiltId() {
return ID;
}

@Override
public void onResourceManagerReload(ResourceManager manager) {
var modelManager = Minecraft.getInstance().getModelManager();
for (var entry : PropType.ITEMS.entrySet()) {
var item = entry.getValue();
var stack = new ItemStack(item);
var model = modelManager.getModel(new ModelResourceLocation(BuiltInRegistries.ITEM.getKey(item), "inventory"));
var variantModels = new ReferenceArrayList<BakedModel>();
for (int variant : entry.getKey().variants) {
stack.getOrCreateTag().putInt("CustomModelData", variant);
var variantModel = model.getOverrides().resolve(model, stack, null, null, 42);
variantModels.add(variantModel);
}
MODELS.put(entry.getKey(), variantModels);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.github.fusionflux.portalcubed.content.prop;

import com.mojang.blaze3d.vertex.PoseStack;

import io.github.fusionflux.portalcubed.mixin.client.ItemRendererAccessor;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.Sheets;
import net.minecraft.client.renderer.entity.EntityRenderer;
import net.minecraft.client.renderer.entity.ItemRenderer;
import net.minecraft.client.renderer.entity.EntityRendererProvider.Context;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemDisplayContext;
import net.minecraft.world.item.ItemStack;

public class PropRenderer extends EntityRenderer<PropEntity> {
private final ItemRenderer itemRenderer;

public PropRenderer(Context ctx) {
super(ctx);
this.itemRenderer = ctx.getItemRenderer();
}

@Override
public void render(PropEntity prop, float yaw, float tickDelta, PoseStack matrices, MultiBufferSource vertexConsumers, int light) {
super.render(prop, yaw, tickDelta, matrices, vertexConsumers, light);

var model = PropModels.getModel(prop.type, prop.getVariant());
matrices.pushPose();
model.getTransforms().getTransform(ItemDisplayContext.GROUND).apply(false, matrices);
matrices.translate(-.5, -.5, -.5);
var consumer = vertexConsumers.getBuffer(Sheets.translucentItemSheet());
((ItemRendererAccessor) itemRenderer).callRenderModelLists(model, ItemStack.EMPTY, light, OverlayTexture.NO_OVERLAY, matrices, consumer);
matrices.popPose();
}

@Override
public ResourceLocation getTextureLocation(PropEntity entity) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package io.github.fusionflux.portalcubed.content.prop;

import java.util.Locale;

import org.apache.commons.lang3.function.TriFunction;
import org.apache.commons.lang3.stream.IntStreams;
import org.quiltmc.loader.api.minecraft.ClientOnly;
import org.quiltmc.loader.api.minecraft.MinecraftQuiltLoader;
import org.quiltmc.qsl.entity.extensions.api.QuiltEntityTypeBuilder;

import io.github.fusionflux.portalcubed.PortalCubed;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.fabricmc.api.EnvType;
import net.fabricmc.fabric.api.client.rendering.v1.EntityRendererRegistry;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.EntityDimensions;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.MobCategory;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec3;

public enum PropType {
BEANS (EntityDimensions.fixed(.5f, .5f)),
CHAIR (EntityDimensions.fixed(.5f, .5f)),
COMPANION_CUBE (EntityDimensions.fixed(.5f, .5f)),
COMPUTER (EntityDimensions.fixed(.5f, .5f)),
HOOPY (EntityDimensions.fixed(.5f, .5f)),
JUG (EntityDimensions.fixed(.5f, .5f)),
LIL_PINEAPPLE (EntityDimensions.fixed(.5f, .5f)),
MUG (EntityDimensions.fixed(.5f, .5f)),
OLD_AP_CUBE (EntityDimensions.fixed(.5f, .5f)),
PORTAL_1_COMPANION_CUBE(EntityDimensions.fixed(.5f, .5f)),
PORTAL_1_STORAGE_CUBE (EntityDimensions.fixed(.5f, .5f)),
RADIO (EntityDimensions.fixed(.5f, .5f)),
// REDIRECTION_CUBE,
// SCHRODINGER_CUBE,
STORAGE_CUBE (EntityDimensions.fixed(.5f, .5f));

public static final Object2ObjectOpenHashMap<PropType, Item> ITEMS = new Object2ObjectOpenHashMap<>();

public final int[] variants;
public final EntityType<PropEntity> entityType;

PropType(EntityDimensions dimensions) {
this(1, dimensions);
}

PropType(int variants, EntityDimensions dimensions) {
this(variants, dimensions, PropEntity::new);
}

PropType(int variants, EntityDimensions dimensions, TriFunction<PropType, EntityType<PropEntity>, Level, PropEntity> factory) {
this.variants = IntStreams.range(variants).toArray();
this.entityType = QuiltEntityTypeBuilder.<PropEntity>create(MobCategory.MISC, (entityType, level) -> factory.apply(this, entityType, level)).setDimensions(dimensions).build();
}

public static void init() {
for (var type : values())
type.register();
}

public void register() {
var id = PortalCubed.id(toString());
Registry.register(BuiltInRegistries.ENTITY_TYPE, id, this.entityType);
ITEMS.put(this, Registry.register(BuiltInRegistries.ITEM, id, new Item(new Item.Properties())));

if (MinecraftQuiltLoader.getEnvironmentType() == EnvType.CLIENT)
registerClient();
}

@ClientOnly
private void registerClient() {
EntityRendererRegistry.register(entityType, PropRenderer::new);
}

public boolean spawn(ServerLevel level, BlockPos pos) {
var entity = entityType.create(level);
entity.setPos(Vec3.atCenterOf(pos));
return level.addFreshEntity(entity);
}

@Override
public String toString() {
return name().toLowerCase(Locale.ROOT);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.github.fusionflux.portalcubed.mixin.client;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;

import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;

import net.minecraft.client.renderer.entity.ItemRenderer;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.world.item.ItemStack;

@Mixin(ItemRenderer.class)
public interface ItemRendererAccessor {
@Invoker
void callRenderModelLists(BakedModel model, ItemStack stack, int light, int overlay, PoseStack matrices, VertexConsumer consumer);
}
1 change: 1 addition & 0 deletions src/main/resources/portalcubed.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"client.BlockModelMixin",
"client.BlockElement_DeserializerMixin",
"client.BlockElementMixin",
"client.ItemRendererAccessor",
"client.SimpleBakedModelAccessor",
"client.ClientLevelMixin"
],
Expand Down

0 comments on commit 4049643

Please sign in to comment.