From 2d319a8e504e514d3ee6541fd43ddf7027c609bd Mon Sep 17 00:00:00 2001 From: quat1024 Date: Mon, 3 Jul 2023 20:39:48 -0400 Subject: [PATCH] Particles. Intentionally omitted the blockcrack particle customization, i think it looks fine --- .../templates/api/ThemeableBlockEntity.java | 7 ++++ .../templates/block/TemplateBlock.java | 7 ++-- .../block/entity/TemplateEntity.java | 6 ++- .../mixin/particles/MixinEntity.java | 33 +++++++++++++++ .../mixin/particles/MixinLivingEntity.java | 41 +++++++++++++++++++ .../templates/models/block/slope_base.json | 3 ++ src/main/resources/fabric.mod.json | 6 ++- src/main/resources/templates.mixins.json | 13 ++++++ 8 files changed, 109 insertions(+), 7 deletions(-) create mode 100644 src/main/java/io/github/cottonmc/templates/api/ThemeableBlockEntity.java create mode 100644 src/main/java/io/github/cottonmc/templates/mixin/particles/MixinEntity.java create mode 100644 src/main/java/io/github/cottonmc/templates/mixin/particles/MixinLivingEntity.java create mode 100644 src/main/resources/templates.mixins.json diff --git a/src/main/java/io/github/cottonmc/templates/api/ThemeableBlockEntity.java b/src/main/java/io/github/cottonmc/templates/api/ThemeableBlockEntity.java new file mode 100644 index 0000000..6bd228c --- /dev/null +++ b/src/main/java/io/github/cottonmc/templates/api/ThemeableBlockEntity.java @@ -0,0 +1,7 @@ +package io.github.cottonmc.templates.api; + +import net.minecraft.block.BlockState; + +public interface ThemeableBlockEntity { + BlockState getThemeState(); +} diff --git a/src/main/java/io/github/cottonmc/templates/block/TemplateBlock.java b/src/main/java/io/github/cottonmc/templates/block/TemplateBlock.java index 1132e95..d188824 100644 --- a/src/main/java/io/github/cottonmc/templates/block/TemplateBlock.java +++ b/src/main/java/io/github/cottonmc/templates/block/TemplateBlock.java @@ -5,7 +5,6 @@ import net.minecraft.block.Block; import net.minecraft.block.BlockEntityProvider; import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; -import net.minecraft.block.entity.BlockEntity; import net.minecraft.entity.LivingEntity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.BlockItem; @@ -79,7 +78,7 @@ public abstract class TemplateBlock extends Block implements BlockEntityProvider } //Changing the theme - if(held.getItem() instanceof BlockItem bi && be.getRenderedState().getBlock() == Blocks.AIR) { + if(held.getItem() instanceof BlockItem bi && be.getThemeState().getBlock() == Blocks.AIR) { Block block = bi.getBlock(); ItemPlacementContext ctx = new ItemPlacementContext(new ItemUsageContext(player, hand, hit)); BlockState placementState = block.getPlacementState(ctx); @@ -104,8 +103,10 @@ public abstract class TemplateBlock extends Block implements BlockEntityProvider if(!state.isOf(newState.getBlock()) && world.getBlockEntity(pos) instanceof TemplateEntity template) { DefaultedList drops = DefaultedList.of(); - Block theme = template.getRenderedState().getBlock(); + //TODO: remember the specific ItemStack + Block theme = template.getThemeState().getBlock(); if(theme != Blocks.AIR) drops.add(new ItemStack(theme)); + if(template.hasSpentRedstoneTorch()) drops.add(new ItemStack(Items.REDSTONE_TORCH)); if(template.hasSpentGlowstoneDust()) drops.add(new ItemStack(Items.GLOWSTONE_DUST)); diff --git a/src/main/java/io/github/cottonmc/templates/block/entity/TemplateEntity.java b/src/main/java/io/github/cottonmc/templates/block/entity/TemplateEntity.java index 566e3df..39004c0 100644 --- a/src/main/java/io/github/cottonmc/templates/block/entity/TemplateEntity.java +++ b/src/main/java/io/github/cottonmc/templates/block/entity/TemplateEntity.java @@ -1,6 +1,7 @@ package io.github.cottonmc.templates.block.entity; import io.github.cottonmc.templates.Templates; +import io.github.cottonmc.templates.api.ThemeableBlockEntity; import net.fabricmc.fabric.api.rendering.data.v1.RenderAttachmentBlockEntity; import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; @@ -18,7 +19,7 @@ import org.jetbrains.annotations.Nullable; import java.util.Objects; -public class TemplateEntity extends BlockEntity implements RenderAttachmentBlockEntity { +public class TemplateEntity extends BlockEntity implements RenderAttachmentBlockEntity, ThemeableBlockEntity { protected BlockState renderedState = Blocks.AIR.getDefaultState(); //Whether the player has manually spent a redstone/glowstone item to upgrade the template. @@ -74,7 +75,8 @@ public class TemplateEntity extends BlockEntity implements RenderAttachmentBlock return renderedState; } - public BlockState getRenderedState() { + @Override + public BlockState getThemeState() { return renderedState; } diff --git a/src/main/java/io/github/cottonmc/templates/mixin/particles/MixinEntity.java b/src/main/java/io/github/cottonmc/templates/mixin/particles/MixinEntity.java new file mode 100644 index 0000000..7dc6281 --- /dev/null +++ b/src/main/java/io/github/cottonmc/templates/mixin/particles/MixinEntity.java @@ -0,0 +1,33 @@ +package io.github.cottonmc.templates.mixin.particles; + +import io.github.cottonmc.templates.api.ThemeableBlockEntity; +import net.minecraft.block.BlockState; +import net.minecraft.block.Blocks; +import net.minecraft.entity.Entity; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.ModifyArg; + +@Mixin(Entity.class) +public abstract class MixinEntity { + @Shadow @Deprecated public abstract BlockPos getLandingPos(); //TODO, somewhat expensive method + + @ModifyArg( + method = "spawnSprintingParticles", + at = @At(value = "INVOKE", target = "Lnet/minecraft/particle/BlockStateParticleEffect;(Lnet/minecraft/particle/ParticleType;Lnet/minecraft/block/BlockState;)V"), + require = 0 + ) + private BlockState templates$spawnSprintingParticles$modifyParticleState(BlockState origState) { + World world = ((Entity) (Object) this).getWorld(); + + if(world.getBlockEntity(getLandingPos()) instanceof ThemeableBlockEntity themeable) { + BlockState theme = themeable.getThemeState(); + if(theme.getBlock() != Blocks.AIR) return theme; + } + + return origState; + } +} diff --git a/src/main/java/io/github/cottonmc/templates/mixin/particles/MixinLivingEntity.java b/src/main/java/io/github/cottonmc/templates/mixin/particles/MixinLivingEntity.java new file mode 100644 index 0000000..af98954 --- /dev/null +++ b/src/main/java/io/github/cottonmc/templates/mixin/particles/MixinLivingEntity.java @@ -0,0 +1,41 @@ +package io.github.cottonmc.templates.mixin.particles; + +import io.github.cottonmc.templates.api.ThemeableBlockEntity; +import net.minecraft.block.BlockState; +import net.minecraft.block.Blocks; +import net.minecraft.entity.Entity; +import net.minecraft.entity.LivingEntity; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.ModifyArg; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(LivingEntity.class) +public class MixinLivingEntity { + @Unique private BlockPos lastFallCheckPos; + + @Inject(method = "fall", at = @At("HEAD")) + private void templates$onFall(double d, boolean bl, BlockState blockState, BlockPos blockPos, CallbackInfo ci) { + lastFallCheckPos = blockPos; + } + + @ModifyArg( + method = "fall", + at = @At(value = "INVOKE", target = "Lnet/minecraft/particle/BlockStateParticleEffect;(Lnet/minecraft/particle/ParticleType;Lnet/minecraft/block/BlockState;)V"), + require = 0 + ) + private BlockState templates$fall$modifyParticleState(BlockState origState) { + World world = ((Entity) (Object) this).getWorld(); + + if(lastFallCheckPos != null && world.getBlockEntity(lastFallCheckPos) instanceof ThemeableBlockEntity themeable) { + BlockState theme = themeable.getThemeState(); + if(theme.getBlock() != Blocks.AIR) return theme; + } + + return origState; + } +} diff --git a/src/main/resources/assets/templates/models/block/slope_base.json b/src/main/resources/assets/templates/models/block/slope_base.json index 8855a16..63cbaf3 100644 --- a/src/main/resources/assets/templates/models/block/slope_base.json +++ b/src/main/resources/assets/templates/models/block/slope_base.json @@ -1,6 +1,9 @@ { "parent": "block/block", "gui_light": "front", + "textures": { + "particle": "block/scaffolding_top" + }, "display": { "firstperson_righthand": { "rotation": [ 0, 135, 0 ], diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 853ebd0..846c0fa 100755 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -9,6 +9,9 @@ "contact": { "sources": "https://github.com/CottonMC/Templates" }, + "mixins": [ + "templates.mixins.json" + ], "environment": "*", "entrypoints": { "main": [ @@ -21,7 +24,6 @@ "depends": { "minecraft": ">=1.20.1", "fabricloader": "*", - "fabric-api": "*", - "fabric-renderer-indigo": "*" + "fabric-api": "*" } } diff --git a/src/main/resources/templates.mixins.json b/src/main/resources/templates.mixins.json new file mode 100644 index 0000000..93ccfd3 --- /dev/null +++ b/src/main/resources/templates.mixins.json @@ -0,0 +1,13 @@ +{ + "required": true, + "package": "io.github.cottonmc.templates.mixin", + "compatibilityLevel": "JAVA_17", + "mixins": [ + "particles.MixinEntity", + "particles.MixinLivingEntity" + ], + "injectors": { + "defaultRequire": 1 + }, + "refmap": "packages.refmap.json" +}