Skip to content

Commit

Permalink
Fix classloader isolation
Browse files Browse the repository at this point in the history
  • Loading branch information
lukebemish committed Jan 8, 2024
1 parent b07d7a1 commit 4a884f6
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 71 deletions.
76 changes: 37 additions & 39 deletions common/src/main/java/dev/lukebemish/tempest/impl/Services.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,44 +50,39 @@ public static <T> T load(Class<T> clazz) {
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);
}
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);
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
};
}

Expand Down

0 comments on commit 4a884f6

Please sign in to comment.