More work on the hydrophobic spell

This commit is contained in:
Sollace 2022-10-07 16:52:35 +02:00
parent fc792c930e
commit 170cbe3a7d
5 changed files with 53 additions and 20 deletions

View file

@ -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<CasterView> 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();

View file

@ -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<Entry> 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);
});
}
}

View file

@ -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<CasterView> clientWorld = Optional.of(() -> MinecraftClient.getInstance().world);
@Override
public Optional<CasterView> 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();

View file

@ -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<Boolean> 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);
}
}

View file

@ -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> T decode(Codec<T> codec, NbtElement nbt) {
return codec.decode(NbtOps.INSTANCE, nbt).result().get().getFirst();
static <T> Optional<T> decode(Codec<T> codec, NbtElement nbt) {
return codec.decode(NbtOps.INSTANCE, nbt).result().map(Pair::getFirst);
}
static <T> NbtElement encode(Codec<T> codec, T value) {