generated from TropheusJ/quilt-template-mod
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3f3faa6
commit 4049643
Showing
8 changed files
with
251 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/java/io/github/fusionflux/portalcubed/content/prop/PropEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/io/github/fusionflux/portalcubed/content/prop/PropModels.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/io/github/fusionflux/portalcubed/content/prop/PropRenderer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
src/main/java/io/github/fusionflux/portalcubed/content/prop/PropType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/io/github/fusionflux/portalcubed/mixin/client/ItemRendererAccessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters