-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hopefully set up snowrealmagic compat
- Loading branch information
1 parent
f7c2e75
commit c041dd9
Showing
10 changed files
with
274 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 96 additions & 1 deletion
97
common/src/main/java/dev/lukebemish/tempest/impl/Services.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,121 @@ | ||
package dev.lukebemish.tempest.impl; | ||
|
||
import dev.lukebemish.tempest.impl.data.world.WeatherChunkData; | ||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.core.Registry; | ||
import net.minecraft.resources.ResourceLocation; | ||
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; | ||
import java.util.ServiceLoader; | ||
import java.util.function.Supplier; | ||
|
||
public class Services { | ||
public final class Services { | ||
private Services() {} | ||
|
||
public static final Platform PLATFORM = load(Platform.class); | ||
|
||
private static final List<Snower> SNOWERS; | ||
private static final List<Melter> MELTERS; | ||
|
||
public static boolean snow(ServerLevel level, BlockPos pos, BlockState original) { | ||
for (var snower : SNOWERS) { | ||
if (snower.snow(level, pos, original)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public static boolean melt(ServerLevel level, BlockPos pos, BlockState original) { | ||
for (var melter : MELTERS) { | ||
if (melter.melt(level, pos, original)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public static <T> T load(Class<T> clazz) { | ||
return ServiceLoader.load(clazz) | ||
.findFirst() | ||
.orElseThrow(() -> new NullPointerException("Failed to load service for " + clazz.getName())); | ||
} | ||
|
||
static { | ||
var melters = new ArrayList<Melter>(); | ||
var snowers = new ArrayList<Snower>(); | ||
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); | ||
} | ||
} | ||
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; | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
MELTERS = List.copyOf(melters); | ||
SNOWERS = List.copyOf(snowers); | ||
} | ||
|
||
public interface Platform { | ||
WeatherChunkData getChunkData(LevelChunk chunk); | ||
<S, T extends S> Supplier<T> register(Supplier<T> supplier, ResourceLocation location, Registry<S> registry); | ||
|
||
boolean modLoaded(String modId); | ||
} | ||
|
||
@FunctionalInterface | ||
public interface Melter { | ||
boolean melt(ServerLevel level, BlockPos pos, BlockState original); | ||
} | ||
|
||
@FunctionalInterface | ||
public interface Snower { | ||
boolean snow(ServerLevel level, BlockPos pos, BlockState original); | ||
} | ||
|
||
public interface CompatProvider { | ||
@Nullable Melter melter(); | ||
@Nullable Snower snower(); | ||
|
||
boolean shouldLoad(); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
...n/src/main/java/dev/lukebemish/tempest/impl/compat/snowrealmagic/SnowRealMagicCompat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package dev.lukebemish.tempest.impl.compat.snowrealmagic; | ||
|
||
import com.google.auto.service.AutoService; | ||
import dev.lukebemish.tempest.impl.Services; | ||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.core.Direction; | ||
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) | ||
public class SnowRealMagicCompat implements Services.CompatProvider { | ||
public interface SnowRealMagicPlatform { | ||
SnowRealMagicPlatform INSTANCE = Services.load(SnowRealMagicPlatform.class); | ||
|
||
boolean isVariant(Block snowVariant); | ||
|
||
BlockState decreaseLayer(Block snowVariant, BlockState original, Level level, BlockPos pos, boolean flag); | ||
int layers(BlockState original, Level level, BlockPos pos); | ||
} | ||
|
||
@Override | ||
public Services.@Nullable Melter melter() { | ||
return (level, pos, original) -> { | ||
Block snowVariant = original.getBlock(); | ||
if (SnowRealMagicPlatform.INSTANCE.isVariant(snowVariant)) { | ||
try { | ||
BlockState newState = SnowRealMagicPlatform.INSTANCE.decreaseLayer(snowVariant, original, level, pos, false); | ||
if (newState != original) { | ||
level.setBlockAndUpdate(pos, newState); | ||
return true; | ||
} | ||
} catch (Throwable e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
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); | ||
} | ||
} | ||
return false; | ||
}; | ||
} | ||
|
||
@Override | ||
public boolean shouldLoad() { | ||
return Services.PLATFORM.modLoaded("snowrealmagic"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...ev/lukebemish/tempest/impl/fabriquilt/compat/snowrealmagic/SnowRealMagicPlatformImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package dev.lukebemish.tempest.impl.fabriquilt.compat.snowrealmagic; | ||
|
||
import com.google.auto.service.AutoService; | ||
import dev.lukebemish.tempest.impl.compat.snowrealmagic.SnowRealMagicCompat; | ||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.world.level.Level; | ||
import net.minecraft.world.level.block.Block; | ||
import net.minecraft.world.level.block.state.BlockState; | ||
import snownee.snow.block.SnowVariant; | ||
|
||
@AutoService(SnowRealMagicCompat.SnowRealMagicPlatform.class) | ||
public class SnowRealMagicPlatformImpl implements SnowRealMagicCompat.SnowRealMagicPlatform { | ||
@Override | ||
public boolean isVariant(Block snowVariant) { | ||
return snowVariant instanceof SnowVariant; | ||
} | ||
|
||
@Override | ||
public BlockState decreaseLayer(Block snowVariant, BlockState original, Level level, BlockPos pos, boolean flag) { | ||
return ((SnowVariant) snowVariant).decreaseLayer(original, level, pos, flag); | ||
} | ||
|
||
@Override | ||
public int layers(BlockState original, Level level, BlockPos pos) { | ||
return ((SnowVariant) original.getBlock()).layers(original, level, pos); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.