From 01fa068af69d80902d23b0575cf3e12014128856 Mon Sep 17 00:00:00 2001 From: Martin Prokoph Date: Wed, 31 Jul 2024 12:01:59 +0200 Subject: [PATCH] Working particles on Polymer! --- .../motschen/decorative/DecorativeMain.java | 18 +++++++-------- .../blockentity/PoolSprinklerBlockEntity.java | 20 ++++++++++------- .../blockentity/ShowerHeadBlockEntity.java | 22 +++++++++++-------- .../decorative/init/BlockEntities.java | 3 ++- .../model/DirectionalItemDisplayModel.java | 2 +- .../decorative/util/ParticleUtil.java | 15 +++++++++++++ .../decorative/util/RegistryUtil.java | 7 +++--- 7 files changed, 56 insertions(+), 31 deletions(-) create mode 100644 src/main/java/eu/midnightdust/motschen/decorative/util/ParticleUtil.java diff --git a/src/main/java/eu/midnightdust/motschen/decorative/DecorativeMain.java b/src/main/java/eu/midnightdust/motschen/decorative/DecorativeMain.java index 37935c4..1881548 100755 --- a/src/main/java/eu/midnightdust/motschen/decorative/DecorativeMain.java +++ b/src/main/java/eu/midnightdust/motschen/decorative/DecorativeMain.java @@ -119,21 +119,21 @@ public class DecorativeMain implements ModInitializer { ItemGroup group; Text name = Text.translatable("itemGroup."+id.getNamespace()+"."+id.getPath()); - if (DecorativeConfig.polymerIntegration) { + //if (DecorativeConfig.polymerIntegration) { group = PolymerItemGroupUtils.builder().displayName(name).icon(() -> new ItemStack(icon)).entries(((displayContext, entries) -> { List groupItems = new ArrayList<>(); RegistryUtil.groupItems.stream().filter(itemEntry -> itemEntry.groupName() == name).forEach(itemEntry -> groupItems.add(itemEntry.stack())); entries.addAll(groupItems); })).build(); PolymerItemGroupUtils.registerPolymerItemGroup(id, group); - } else { - group = FabricItemGroup.builder().displayName(name).icon(() -> new ItemStack(icon)).entries(((displayContext, entries) -> { - List groupItems = new ArrayList<>(); - RegistryUtil.groupItems.stream().filter(itemEntry -> itemEntry.groupName() == name).forEach(itemEntry -> groupItems.add(itemEntry.stack())); - entries.addAll(groupItems); - })).build(); - Registry.register(Registries.ITEM_GROUP, id, group); - } +// } else { +// group = FabricItemGroup.builder().displayName(name).icon(() -> new ItemStack(icon)).entries(((displayContext, entries) -> { +// List groupItems = new ArrayList<>(); +// RegistryUtil.groupItems.stream().filter(itemEntry -> itemEntry.groupName() == name).forEach(itemEntry -> groupItems.add(itemEntry.stack())); +// entries.addAll(groupItems); +// })).build(); +// Registry.register(Registries.ITEM_GROUP, id, group); +// } return group; } } diff --git a/src/main/java/eu/midnightdust/motschen/decorative/block/blockentity/PoolSprinklerBlockEntity.java b/src/main/java/eu/midnightdust/motschen/decorative/block/blockentity/PoolSprinklerBlockEntity.java index c81c621..0bfc4a5 100755 --- a/src/main/java/eu/midnightdust/motschen/decorative/block/blockentity/PoolSprinklerBlockEntity.java +++ b/src/main/java/eu/midnightdust/motschen/decorative/block/blockentity/PoolSprinklerBlockEntity.java @@ -2,10 +2,13 @@ package eu.midnightdust.motschen.decorative.block.blockentity; import eu.midnightdust.motschen.decorative.block.PoolSprinkler; import eu.midnightdust.motschen.decorative.init.BlockEntities; +import eu.midnightdust.motschen.decorative.util.ParticleUtil; +import net.fabricmc.fabric.api.networking.v1.PlayerLookup; import net.minecraft.block.BlockState; import net.minecraft.block.entity.BlockEntity; import net.minecraft.particle.ParticleTypes; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Vec3d; import net.minecraft.world.World; public class PoolSprinklerBlockEntity extends BlockEntity { @@ -15,14 +18,15 @@ public class PoolSprinklerBlockEntity extends BlockEntity { } public static void tick(World world, BlockPos pos, BlockState state, PoolSprinklerBlockEntity blockEntity) { - if (state.get(PoolSprinkler.POWERED)) { - switch (state.get(PoolSprinkler.FACING)) { - case NORTH: world.addParticle(ParticleTypes.DRIPPING_WATER, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() - 0.34, 1, 1, 1); break; - case EAST: world.addParticle(ParticleTypes.DRIPPING_WATER, pos.getX() + 1.34, pos.getY() + 0.5, pos.getZ() + 0.5, 1, 1, 1); break; - case SOUTH: world.addParticle(ParticleTypes.DRIPPING_WATER, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 1.34, 1, 1, 1); break; - case WEST: world.addParticle(ParticleTypes.DRIPPING_WATER, pos.getX() - 0.34, pos.getY() + 0.5, pos.getZ() + 0.5, 1, 1, 1); break; - default: break; - } + if (state.get(PoolSprinkler.POWERED) && !world.isClient) { + Vec3d particlePos = switch (state.get(PoolSprinkler.FACING)) { + case NORTH -> new Vec3d( pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() - 0.34); + case EAST -> new Vec3d(pos.getX() + 1.34, pos.getY() + 0.5, pos.getZ() + 0.5); + case SOUTH -> new Vec3d(pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 1.34); + case WEST -> new Vec3d(pos.getX() - 0.34, pos.getY() + 0.5, pos.getZ() + 0.5); + default -> Vec3d.ZERO; + }; + PlayerLookup.tracking(blockEntity).forEach(watchingPlayer -> ParticleUtil.spawnParticle(watchingPlayer, ParticleTypes.DRIPPING_WATER, particlePos, new Vec3d(0, 0, 0), 1)); } } } diff --git a/src/main/java/eu/midnightdust/motschen/decorative/block/blockentity/ShowerHeadBlockEntity.java b/src/main/java/eu/midnightdust/motschen/decorative/block/blockentity/ShowerHeadBlockEntity.java index 8639eae..bdbe5f7 100755 --- a/src/main/java/eu/midnightdust/motschen/decorative/block/blockentity/ShowerHeadBlockEntity.java +++ b/src/main/java/eu/midnightdust/motschen/decorative/block/blockentity/ShowerHeadBlockEntity.java @@ -1,11 +1,14 @@ package eu.midnightdust.motschen.decorative.block.blockentity; -import eu.midnightdust.motschen.decorative.block.PoolSprinkler; +import eu.midnightdust.motschen.decorative.block.ShowerHead; import eu.midnightdust.motschen.decorative.init.BlockEntities; +import eu.midnightdust.motschen.decorative.util.ParticleUtil; +import net.fabricmc.fabric.api.networking.v1.PlayerLookup; import net.minecraft.block.BlockState; import net.minecraft.block.entity.BlockEntity; import net.minecraft.particle.ParticleTypes; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Vec3d; import net.minecraft.world.World; public class ShowerHeadBlockEntity extends BlockEntity { @@ -15,14 +18,15 @@ public class ShowerHeadBlockEntity extends BlockEntity { } public static void tick(World world, BlockPos pos, BlockState state, ShowerHeadBlockEntity blockEntity) { - if (state.get(PoolSprinkler.POWERED)) { - switch (state.get(PoolSprinkler.FACING)) { - case NORTH: world.addParticle(ParticleTypes.DRIPPING_WATER, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.625, 1, 1, 1); break; - case EAST: world.addParticle(ParticleTypes.DRIPPING_WATER, pos.getX() + 0.375, pos.getY() + 0.5, pos.getZ() + 0.5, 1, 1, 1); break; - case SOUTH: world.addParticle(ParticleTypes.DRIPPING_WATER, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.375, 1, 1, 1); break; - case WEST: world.addParticle(ParticleTypes.DRIPPING_WATER, pos.getX() + 0.625, pos.getY() + 0.5, pos.getZ() + 0.5, 1, 1, 1); break; - default: break; - } + if (state.get(ShowerHead.POWERED) && !world.isClient) { + Vec3d particlePos = switch (state.get(ShowerHead.FACING)) { + case NORTH -> new Vec3d( pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.625); + case EAST -> new Vec3d(pos.getX() + 0.375, pos.getY() + 0.5, pos.getZ() + 0.5); + case SOUTH -> new Vec3d(pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.375); + case WEST -> new Vec3d(pos.getX() + 0.625, pos.getY() + 0.5, pos.getZ() + 0.5); + default -> Vec3d.ZERO; + }; + PlayerLookup.tracking(blockEntity).forEach(watchingPlayer -> ParticleUtil.spawnParticle(watchingPlayer, ParticleTypes.DRIPPING_WATER, particlePos, new Vec3d(1, 0, 1), 1)); } } diff --git a/src/main/java/eu/midnightdust/motschen/decorative/init/BlockEntities.java b/src/main/java/eu/midnightdust/motschen/decorative/init/BlockEntities.java index d707b07..ef858ac 100755 --- a/src/main/java/eu/midnightdust/motschen/decorative/init/BlockEntities.java +++ b/src/main/java/eu/midnightdust/motschen/decorative/init/BlockEntities.java @@ -32,7 +32,8 @@ public class BlockEntities { } private static BlockEntityType registerBlockEntity(Identifier id, BlockEntityType.BlockEntityFactory factory, Block... blocks) { var blockEntity = Registry.register(Registries.BLOCK_ENTITY_TYPE, id, BlockEntityType.Builder.create(factory, blocks).build(null)); - if (DecorativeConfig.polymerIntegration) PolymerBlockUtils.registerBlockEntity(blockEntity); + //if (DecorativeConfig.polymerIntegration) + PolymerBlockUtils.registerBlockEntity(blockEntity); return blockEntity; } } diff --git a/src/main/java/eu/midnightdust/motschen/decorative/polymer/model/DirectionalItemDisplayModel.java b/src/main/java/eu/midnightdust/motschen/decorative/polymer/model/DirectionalItemDisplayModel.java index 74bf51a..5005152 100644 --- a/src/main/java/eu/midnightdust/motschen/decorative/polymer/model/DirectionalItemDisplayModel.java +++ b/src/main/java/eu/midnightdust/motschen/decorative/polymer/model/DirectionalItemDisplayModel.java @@ -73,7 +73,7 @@ public class DirectionalItemDisplayModel extends BlockModel { public float getRotation(BlockState state) { if (state.getBlock() instanceof FireHydrant) return state.get(FireHydrant.FACING).getHorizontal() * -90; else if (state.getBlock() instanceof WaterPump) return state.get(WaterPump.FACING).getHorizontal() * -90 - 90; - else if (state.getBlock() instanceof PoolSprinkler) return state.get(PoolSprinkler.FACING).getHorizontal() * -90; + else if (state.getBlock() instanceof PoolSprinkler) return state.get(PoolSprinkler.FACING).getHorizontal() * -90 - 90; else if (state.getBlock() instanceof ShowerHead) return state.get(ShowerHead.FACING).getHorizontal() * -90 + 90; else if (state.getBlock() instanceof Sign) return state.get(PoolSprinkler.FACING).getHorizontal() * -90 + 180; else return state.get(Guardrail.FACING).getHorizontal() * -90; diff --git a/src/main/java/eu/midnightdust/motschen/decorative/util/ParticleUtil.java b/src/main/java/eu/midnightdust/motschen/decorative/util/ParticleUtil.java new file mode 100644 index 0000000..05f42cb --- /dev/null +++ b/src/main/java/eu/midnightdust/motschen/decorative/util/ParticleUtil.java @@ -0,0 +1,15 @@ +package eu.midnightdust.motschen.decorative.util; + +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; +import net.minecraft.network.packet.s2c.play.ParticleS2CPacket; +import net.minecraft.particle.ParticleEffect; +import net.minecraft.particle.ParticleType; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.util.math.Vec3d; + +public class ParticleUtil { + public static void spawnParticle(ServerPlayerEntity player, ParticleType type, Vec3d pos, Vec3d offset, float speed) { + ServerPlayNetworking.getSender(player).sendPacket(new ParticleS2CPacket((ParticleEffect) type, false, pos.x, pos.y, pos.z, + (float) offset.x / 16f, (float) offset.y / 16f, (float) offset.z / 16f, speed, 1)); + } +} diff --git a/src/main/java/eu/midnightdust/motschen/decorative/util/RegistryUtil.java b/src/main/java/eu/midnightdust/motschen/decorative/util/RegistryUtil.java index 44aba32..39d1b7d 100644 --- a/src/main/java/eu/midnightdust/motschen/decorative/util/RegistryUtil.java +++ b/src/main/java/eu/midnightdust/motschen/decorative/util/RegistryUtil.java @@ -45,11 +45,12 @@ public class RegistryUtil { } public static Item blockItem(Block block) { - if (DecorativeConfig.polymerIntegration) { + //if (DecorativeConfig.polymerIntegration) { if (block instanceof PolymerBlock) return new FactoryBlockItem((Block & PolymerBlock) block, new Item.Settings()); else System.out.println(block); - } - return new BlockItem(block, new Item.Settings()); + //} + return null; + //return new BlockItem(block, new Item.Settings()); } public static void registerItem(Identifier id, Item item, ItemGroup group) {