From 170cbe3a7d7aacf8755b7a613222ee6c61ea4e2c Mon Sep 17 00:00:00 2001 From: Sollace Date: Fri, 7 Oct 2022 16:52:35 +0200 Subject: [PATCH] More work on the hydrophobic spell --- .../unicopia/InteractionManager.java | 12 +++++++ .../magic/spell/effect/HydrophobicSpell.java | 34 +++++++++++-------- .../client/ClientInteractionManager.java | 15 ++++++++ .../unicopia/mixin/MixinFlowableFluid.java | 7 ++-- .../unicopia/util/NbtSerialisable.java | 5 +-- 5 files changed, 53 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/InteractionManager.java b/src/main/java/com/minelittlepony/unicopia/InteractionManager.java index 0a8bb759..92e1a6ff 100644 --- a/src/main/java/com/minelittlepony/unicopia/InteractionManager.java +++ b/src/main/java/com/minelittlepony/unicopia/InteractionManager.java @@ -1,8 +1,12 @@ package com.minelittlepony.unicopia; +import java.util.Optional; + import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import com.minelittlepony.unicopia.ability.magic.CasterView; +import com.minelittlepony.unicopia.block.data.Ether; import com.minelittlepony.unicopia.entity.player.dummy.DummyPlayerEntity; import com.minelittlepony.unicopia.entity.player.dummy.DummyServerPlayerEntity; import com.minelittlepony.unicopia.network.handler.ClientNetworkHandler; @@ -12,6 +16,7 @@ import com.mojang.authlib.minecraft.MinecraftSessionService; import net.minecraft.entity.Entity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.server.world.ServerWorld; +import net.minecraft.world.BlockView; import net.minecraft.world.World; public class InteractionManager { @@ -30,6 +35,13 @@ public class InteractionManager { return INSTANCE; } + public Optional getCasterView(BlockView view) { + if (view instanceof ServerWorld world) { + return Optional.of(Ether.get(world)); + } + return Optional.empty(); + } + public MinecraftSessionService getSessionService(World world) { if (world instanceof ServerWorld) { return ((ServerWorld)world).getServer().getSessionService(); diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/HydrophobicSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/HydrophobicSpell.java index 3f7d728e..a87adb23 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/HydrophobicSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/HydrophobicSpell.java @@ -4,10 +4,10 @@ import java.util.HashSet; import java.util.Set; import com.minelittlepony.unicopia.ability.magic.Caster; +import com.minelittlepony.unicopia.ability.magic.CasterView; import com.minelittlepony.unicopia.ability.magic.spell.Situation; import com.minelittlepony.unicopia.ability.magic.spell.trait.SpellTraits; import com.minelittlepony.unicopia.ability.magic.spell.trait.Trait; -import com.minelittlepony.unicopia.block.data.Ether; import com.minelittlepony.unicopia.entity.player.Pony; import com.minelittlepony.unicopia.particle.UParticles; import com.minelittlepony.unicopia.util.NbtSerialisable; @@ -70,10 +70,10 @@ public class HydrophobicSpell extends AbstractSpell { if (block instanceof FluidBlock) { world.setBlockState(pos, Blocks.AIR.getDefaultState(), Block.NOTIFY_LISTENERS); - storedFluidPositions.add(new Entry(pos, state.getFluidState())); + storedFluidPositions.add(new Entry(pos, state)); } else if (state.contains(Properties.WATERLOGGED)) { world.setBlockState(pos, state.cycle(Properties.WATERLOGGED), Block.NOTIFY_LISTENERS); - storedFluidPositions.add(new Entry(pos, state.getFluidState())); + storedFluidPositions.add(new Entry(pos, state)); } } }); @@ -125,14 +125,14 @@ public class HydrophobicSpell extends AbstractSpell { return range; } - record Entry (BlockPos pos, FluidState fluidState) { + record Entry (BlockPos pos, BlockState blockState) { public static final Serializer SERIALIZER = Serializer.of(compound -> new Entry( NbtSerialisable.BLOCK_POS.read(compound.getCompound("pos")), - NbtSerialisable.decode(FluidState.CODEC, compound.get("state")) + NbtSerialisable.decode(BlockState.CODEC, compound.get("blockState")).orElse(Blocks.AIR.getDefaultState()) ), entry -> { NbtCompound compound = new NbtCompound(); compound.put("pos", NbtSerialisable.BLOCK_POS.write(entry.pos)); - compound.put("state", NbtSerialisable.encode(FluidState.CODEC, entry.fluidState)); + compound.put("blockState", NbtSerialisable.encode(BlockState.CODEC, entry.blockState)); return compound; }); @@ -140,23 +140,29 @@ public class HydrophobicSpell extends AbstractSpell { BlockState state = world.getBlockState(pos); if (state.isAir()) { - world.setBlockState(pos, Fluids.WATER.getDefaultState().getBlockState(), Block.NOTIFY_LISTENERS); + if (blockState.contains(Properties.WATERLOGGED)) { + world.setBlockState(pos, blockState.getFluidState().getBlockState(), Block.NOTIFY_LISTENERS); + } else { + world.setBlockState(pos, blockState, Block.NOTIFY_LISTENERS); + } } else if (state.contains(Properties.WATERLOGGED)) { - world.setBlockState(pos, state.cycle(Properties.WATERLOGGED), Block.NOTIFY_LISTENERS); + world.setBlockState(pos, state.with(Properties.WATERLOGGED, true), Block.NOTIFY_LISTENERS); } } } public boolean blocksFlow(Caster caster, BlockPos pos, FluidState fluid) { - if (fluid.isIn(affectedFluid) && pos.isWithinDistance(caster.getOrigin(), getRange(caster) + 1)) { - System.out.println("AHA!"); - } return fluid.isIn(affectedFluid) && pos.isWithinDistance(caster.getOrigin(), getRange(caster) + 1); } - public static boolean blocksFluidFlow(World world, BlockPos pos, BlockState state, Fluid fluid) { - return Ether.get(world).findAllSpellsInRange(pos, 500, SpellType.HYDROPHOBIC).anyMatch(pair -> { - return pair.getValue().blocksFlow(pair.getKey(), pos, fluid.getDefaultState()); + public static boolean blocksFluidFlow(CasterView world, BlockPos pos, FluidState state) { + String caller = new Exception().getStackTrace()[1].getClassName(); + if (!"net.minecraft.fluid.FlowableFluid".contentEquals(caller)) { + System.out.println(caller); + } + + return world.findAllSpellsInRange(pos, 500, SpellType.HYDROPHOBIC).anyMatch(pair -> { + return pair.getValue().blocksFlow(pair.getKey(), pos, state); }); } } diff --git a/src/main/java/com/minelittlepony/unicopia/client/ClientInteractionManager.java b/src/main/java/com/minelittlepony/unicopia/client/ClientInteractionManager.java index f8b90fea..efc54a86 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/ClientInteractionManager.java +++ b/src/main/java/com/minelittlepony/unicopia/client/ClientInteractionManager.java @@ -1,5 +1,6 @@ package com.minelittlepony.unicopia.client; +import java.util.Optional; import java.util.function.Predicate; import org.jetbrains.annotations.NotNull; @@ -8,6 +9,8 @@ import org.jetbrains.annotations.Nullable; import com.minelittlepony.unicopia.FlightType; import com.minelittlepony.unicopia.InteractionManager; import com.minelittlepony.unicopia.USounds; +import com.minelittlepony.unicopia.ability.magic.CasterView; +import com.minelittlepony.unicopia.block.data.Ether; import com.minelittlepony.unicopia.client.gui.DismissSpellScreen; import com.minelittlepony.unicopia.client.sound.LoopedEntityTrackingSoundInstance; import com.minelittlepony.unicopia.client.sound.LoopingSoundInstance; @@ -32,8 +35,10 @@ import net.minecraft.entity.LivingEntity; import net.minecraft.entity.passive.BeeEntity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.vehicle.AbstractMinecartEntity; +import net.minecraft.server.world.ServerWorld; import net.minecraft.sound.SoundEvents; import net.minecraft.util.math.random.Random; +import net.minecraft.world.BlockView; import net.minecraft.world.World; public class ClientInteractionManager extends InteractionManager { @@ -41,6 +46,16 @@ public class ClientInteractionManager extends InteractionManager { private final ClientNetworkHandler handler = new ClientNetworkHandlerImpl(); private final MinecraftClient client = MinecraftClient.getInstance(); + private final Optional clientWorld = Optional.of(() -> MinecraftClient.getInstance().world); + + @Override + public Optional getCasterView(BlockView view) { + if (view instanceof ServerWorld world) { + return Optional.of(Ether.get(world)); + } + return clientWorld; + } + @Override public MinecraftSessionService getSessionService(World world) { return MinecraftClient.getInstance().getSessionService(); diff --git a/src/main/java/com/minelittlepony/unicopia/mixin/MixinFlowableFluid.java b/src/main/java/com/minelittlepony/unicopia/mixin/MixinFlowableFluid.java index 9f81743e..dff7a8f9 100644 --- a/src/main/java/com/minelittlepony/unicopia/mixin/MixinFlowableFluid.java +++ b/src/main/java/com/minelittlepony/unicopia/mixin/MixinFlowableFluid.java @@ -5,20 +5,19 @@ import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +import com.minelittlepony.unicopia.InteractionManager; import com.minelittlepony.unicopia.ability.magic.spell.effect.HydrophobicSpell; import net.minecraft.block.BlockState; -import net.minecraft.fluid.FlowableFluid; -import net.minecraft.fluid.Fluid; +import net.minecraft.fluid.*; import net.minecraft.util.math.BlockPos; import net.minecraft.world.BlockView; -import net.minecraft.world.World; @Mixin(FlowableFluid.class) abstract class MixinFlowableFluid { @Inject(method = "canFill", at = @At("HEAD"), cancellable = true) private void onCanFill(BlockView world, BlockPos pos, BlockState state, Fluid fluid, CallbackInfoReturnable info) { - if (world instanceof World w && HydrophobicSpell.blocksFluidFlow(w, pos, state, fluid)) { + if (InteractionManager.instance().getCasterView(world).filter(view -> HydrophobicSpell.blocksFluidFlow(view, pos, fluid.getDefaultState())).isPresent()) { info.setReturnValue(false); } } diff --git a/src/main/java/com/minelittlepony/unicopia/util/NbtSerialisable.java b/src/main/java/com/minelittlepony/unicopia/util/NbtSerialisable.java index 08f0abcd..c978dd6c 100644 --- a/src/main/java/com/minelittlepony/unicopia/util/NbtSerialisable.java +++ b/src/main/java/com/minelittlepony/unicopia/util/NbtSerialisable.java @@ -4,6 +4,7 @@ import java.util.*; import java.util.function.Function; import java.util.stream.Stream; +import com.mojang.datafixers.util.Pair; import com.mojang.serialization.Codec; import net.minecraft.nbt.*; @@ -45,8 +46,8 @@ public interface NbtSerialisable { return new Vec3d(list.getDouble(0), list.getDouble(1), list.getDouble(2)); } - static T decode(Codec codec, NbtElement nbt) { - return codec.decode(NbtOps.INSTANCE, nbt).result().get().getFirst(); + static Optional decode(Codec codec, NbtElement nbt) { + return codec.decode(NbtOps.INSTANCE, nbt).result().map(Pair::getFirst); } static NbtElement encode(Codec codec, T value) {