diff --git a/common/src/main/java/dev/lukebemish/tempest/impl/Services.java b/common/src/main/java/dev/lukebemish/tempest/impl/Services.java index 1a14685..3b3426e 100644 --- a/common/src/main/java/dev/lukebemish/tempest/impl/Services.java +++ b/common/src/main/java/dev/lukebemish/tempest/impl/Services.java @@ -7,7 +7,6 @@ import net.minecraft.server.level.ServerLevel; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.chunk.LevelChunk; -import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.List; @@ -51,44 +50,39 @@ public static T load(Class clazz) { var snowers = new ArrayList(); for (var provider : ServiceLoader.load(CompatProvider.class)) { if (provider.shouldLoad()) { - var melter = provider.melter(); - if (melter != null) { - melters.add(new Melter() { - boolean valid = true; - - @Override - public boolean melt(ServerLevel level, BlockPos pos, BlockState original) { - if (valid) { - try { - return melter.melt(level, pos, original); - } catch (Throwable t) { - valid = false; - Constants.LOGGER.error("Failed to melt block at {} with provider {}", pos, provider.getClass().getName(), t); - } + var compat = provider.compat(); + melters.add(new Melter() { + boolean valid = true; + + @Override + public boolean melt(ServerLevel level, BlockPos pos, BlockState original) { + if (valid) { + try { + return compat.melt(level, pos, original); + } catch (Throwable t) { + valid = false; + Constants.LOGGER.error("Failed to melt block at {} with provider {}", pos, provider.getClass().getName(), t); } - return false; } - }); - } - var snower = provider.snower(); - if (snower != null) { - snowers.add(new Snower() { - boolean valid = true; - - @Override - public boolean snow(ServerLevel level, BlockPos pos, BlockState original) { - if (valid) { - try { - return snower.snow(level, pos, original); - } catch (Throwable t) { - valid = false; - Constants.LOGGER.error("Failed to snow block at {} with provider {}", pos, provider.getClass().getName(), t); - } + return false; + } + }); + snowers.add(new Snower() { + boolean valid = true; + + @Override + public boolean snow(ServerLevel level, BlockPos pos, BlockState original) { + if (valid) { + try { + return compat.snow(level, pos, original); + } catch (Throwable t) { + valid = false; + Constants.LOGGER.error("Failed to snow block at {} with provider {}", pos, provider.getClass().getName(), t); } - return false; } - }); - } + return false; + } + }); } } MELTERS = List.copyOf(melters); @@ -103,18 +97,22 @@ public interface Platform { } @FunctionalInterface - public interface Melter { + private interface Melter { boolean melt(ServerLevel level, BlockPos pos, BlockState original); } @FunctionalInterface - public interface Snower { + private interface Snower { + boolean snow(ServerLevel level, BlockPos pos, BlockState original); + } + + public interface Compat { + boolean melt(ServerLevel level, BlockPos pos, BlockState original); boolean snow(ServerLevel level, BlockPos pos, BlockState original); } public interface CompatProvider { - @Nullable Melter melter(); - @Nullable Snower snower(); + Compat compat(); boolean shouldLoad(); } diff --git a/common/src/main/java/dev/lukebemish/tempest/impl/compat/snowrealmagic/SnowRealMagicCompat.java b/common/src/main/java/dev/lukebemish/tempest/impl/compat/snowrealmagic/SnowRealMagicCompat.java index 6580963..eeb0170 100644 --- a/common/src/main/java/dev/lukebemish/tempest/impl/compat/snowrealmagic/SnowRealMagicCompat.java +++ b/common/src/main/java/dev/lukebemish/tempest/impl/compat/snowrealmagic/SnowRealMagicCompat.java @@ -4,12 +4,12 @@ import dev.lukebemish.tempest.impl.Services; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; +import net.minecraft.server.level.ServerLevel; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.context.DirectionalPlaceContext; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.state.BlockState; -import org.jetbrains.annotations.Nullable; import snownee.snow.Hooks; @AutoService(Services.CompatProvider.class) @@ -24,43 +24,44 @@ public interface SnowRealMagicPlatform { } @Override - public Services.@Nullable Melter melter() { - return (level, pos, original) -> { - var mutablePos = pos.mutable(); - var state = original; - for (int i = 0; i < 16; i++) { - Block snowVariant = state.getBlock(); - if (SnowRealMagicPlatform.INSTANCE.isVariant(snowVariant)) { - BlockState newState = SnowRealMagicPlatform.INSTANCE.decreaseLayer(snowVariant, state, level, mutablePos, false); - if (newState != state) { - level.setBlockAndUpdate(mutablePos, newState); - return true; + public Services.Compat compat() { + return new Services.Compat() { + @Override + public boolean melt(ServerLevel level, BlockPos pos, BlockState original) { + var mutablePos = pos.mutable(); + var state = original; + for (int i = 0; i < 16; i++) { + Block snowVariant = state.getBlock(); + if (SnowRealMagicPlatform.INSTANCE.isVariant(snowVariant)) { + BlockState newState = SnowRealMagicPlatform.INSTANCE.decreaseLayer(snowVariant, state, level, mutablePos, false); + if (newState != state) { + level.setBlockAndUpdate(mutablePos, newState); + return true; + } } + mutablePos.move(Direction.DOWN); + state = level.getBlockState(mutablePos); } - mutablePos.move(Direction.DOWN); - state = level.getBlockState(mutablePos); + return false; } - return false; - }; - } - @Override - public Services.@Nullable Snower snower() { - return (level, pos, original) -> { - var mutablePos = pos.mutable(); - for (int i = 0; i < 16; i++) { - mutablePos.move(Direction.DOWN); - var newState = level.getBlockState(mutablePos); - if ( - !( - (SnowRealMagicPlatform.INSTANCE.isVariant(newState.getBlock()) && SnowRealMagicPlatform.INSTANCE.layers(newState, level, mutablePos) < 8) || - Hooks.canContainState(newState) - ) && Hooks.canSnowSurvive(newState, level, mutablePos.above())) { - var finalPos = mutablePos.above(); - return Hooks.placeLayersOn(level, finalPos, 1, false, new DirectionalPlaceContext(level, finalPos, Direction.UP, ItemStack.EMPTY, Direction.DOWN), false, true); + @Override + public boolean snow(ServerLevel level, BlockPos pos, BlockState original) { + var mutablePos = pos.mutable(); + for (int i = 0; i < 16; i++) { + mutablePos.move(Direction.DOWN); + var newState = level.getBlockState(mutablePos); + if ( + !( + (SnowRealMagicPlatform.INSTANCE.isVariant(newState.getBlock()) && SnowRealMagicPlatform.INSTANCE.layers(newState, level, mutablePos) < 8) || + Hooks.canContainState(newState) + ) && Hooks.canSnowSurvive(newState, level, mutablePos.above())) { + var finalPos = mutablePos.above(); + return Hooks.placeLayersOn(level, finalPos, 1, false, new DirectionalPlaceContext(level, finalPos, Direction.UP, ItemStack.EMPTY, Direction.DOWN), false, true); + } } + return false; } - return false; }; }