From 3e618f69839ef74e19837e0d1e76d0943301ea3d Mon Sep 17 00:00:00 2001 From: Yeregorix Date: Fri, 28 Jan 2022 20:23:29 +0100 Subject: [PATCH] Begin 1.1.0: Gradle 7, Sponge API 8 --- build.gradle | 99 ++++++-------- .../superpiston/SuperPiston.java | 78 +++++------ .../superpiston/SuperPistonTimings.java | 2 +- .../PistonStructureCalculationEvent.java | 30 +++-- .../api/structure/PistonStructure.java | 2 +- .../DefaultStructureCalculator.java | 69 ++++++---- .../superpiston/config/world/WorldConfig.java | 44 +++---- .../superpiston/event/PistonListener.java | 6 +- .../superpiston/impl/BlockUtil.java | 30 +---- .../superpiston/impl/ReactionUtil.java | 43 +++---- .../SuperPistonStructureCalculator.java | 10 +- .../AbstractStructureCalculationEvent.java | 18 +-- .../event/PostStructureCalculationEvent.java | 10 +- .../event/PreStructureCalculationEvent.java | 10 +- ...seMixin.java => PistonBaseBlockMixin.java} | 26 ++-- ...java => PistonStructureResolverMixin.java} | 48 +++---- .../superpiston/util/IOUtil.java | 7 - .../superpiston/util/RegistryUtil.java | 121 ++++++++++++++++++ .../resources/META-INF/sponge_plugins.json | 35 +++++ .../resources/META-INF/superpiston_at.cfg | 0 src/main/resources/mixins.superpiston.json | 7 +- 21 files changed, 414 insertions(+), 281 deletions(-) rename src/main/java/net/smoofyuniverse/superpiston/mixin/block/{BlockPistonBaseMixin.java => PistonBaseBlockMixin.java} (53%) rename src/main/java/net/smoofyuniverse/superpiston/mixin/block/{BlockPistonStructureHelperMixin.java => PistonStructureResolverMixin.java} (70%) create mode 100644 src/main/java/net/smoofyuniverse/superpiston/util/RegistryUtil.java create mode 100644 src/main/resources/META-INF/sponge_plugins.json delete mode 100644 src/main/resources/META-INF/superpiston_at.cfg diff --git a/build.gradle b/build.gradle index 797916c..ae77bc1 100644 --- a/build.gradle +++ b/build.gradle @@ -20,47 +20,22 @@ * SOFTWARE. */ -buildscript { - repositories { - maven { - name = 'forge' - url = 'https://maven.minecraftforge.net' - } - maven { - name = 'sponge' - url = 'https://repo.spongepowered.org/repository/maven-public/' - } - } - - dependencies { - classpath 'net.minecraftforge.gradle:ForgeGradle:2.3-SNAPSHOT' - classpath 'org.spongepowered:mixingradle:0.6-SNAPSHOT' - } -} - plugins { - id 'java' - id 'com.github.johnrengelman.shadow' version '4.0.4' + id 'java-library' + id 'com.github.johnrengelman.shadow' version '7.0.0' + id 'org.spongepowered.gradle.vanilla' version '0.2' } -apply plugin: 'net.minecraftforge.gradle.forge' -apply plugin: 'org.spongepowered.mixin' +group 'net.smoofyuniverse' -sourceCompatibility = 1.8 -targetCompatibility = 1.8 - -minecraft { - version = '1.12.2-14.23.5.2838' - mappings = 'snapshot_20180808' - - runDir = 'run' - makeObfSourceJar = false +java { + toolchain { + languageVersion = JavaLanguageVersion.of(8) + } } -sourceSets { - main { - ext.refMap = 'mixins.superpiston.refmap.json' - } +tasks.withType(JavaCompile).configureEach { + options.encoding = "utf-8" } repositories { @@ -71,31 +46,37 @@ repositories { } } +configurations { + spongevanilla +} + dependencies { - compile 'org.spongepowered:spongecommon:1.12.2-7.3.0:dev' - compile 'net.smoofyuniverse:oreupdater:1.0.1' - compile 'net.smoofyuniverse:worldmap:1.0.0' + implementation 'org.spongepowered:sponge:1.16.5-8.0.0-SNAPSHOT:dev' + implementation 'org.spongepowered:timings:1.0-SNAPSHOT' + implementation 'org.spongepowered:mixin:0.8.4' + implementation 'org.ow2.asm:asm-util:9.2' + + implementation 'net.smoofyuniverse:oreupdater:1.1.1' + implementation 'net.smoofyuniverse:worldmap:1.1.0' + + spongevanilla 'org.spongepowered:spongevanilla:1.16.5-8.0.0-RC1052:universal' +} + +minecraft { + version("1.16.5") } jar { classifier 'base' manifest { attributes( - 'FMLCorePluginContainsFMLMod': 'true', - 'TweakClass': 'org.spongepowered.asm.launch.MixinTweaker', - 'MixinConfigs': 'mixins.superpiston.json', - 'FMLAT': 'superpiston_at.cfg', - 'ForceLoadAsMod' : true + 'MixinConfigs': 'mixins.superpiston.json' ) } } -reobf.jar.mappingType = 'SEARGE' - shadowJar { classifier '' - mainSpec.sourcePaths.clear() - dependsOn reobfJar dependencies { include dependency('net.smoofyuniverse:oreapi') @@ -104,22 +85,16 @@ shadowJar { } relocate 'net.smoofyuniverse.ore', 'net.smoofyuniverse.superpiston.ore' - relocate 'net.smoofyuniverse.map', 'net.smoofyuniverse.autopickup.map' - - exclude "dummyThing" - afterEvaluate { - from zipTree(reobfJar.jar) - } + relocate 'net.smoofyuniverse.map', 'net.smoofyuniverse.superpiston.map' } -task updatePlugin <<{ - copy { - from 'build/libs/SuperPiston.jar' - into 'run/forge/mods' - } - copy { - from 'build/libs/SuperPiston.jar' - into 'run/vanilla/mods' +task setupVanillaServer(type: Copy) { + into 'run/vanilla' + + from configurations.spongevanilla + rename('spongevanilla-(.*).jar', 'spongevanilla.jar') + + into('mods') { + from shadowJar } } -updatePlugin.dependsOn shadowJar \ No newline at end of file diff --git a/src/main/java/net/smoofyuniverse/superpiston/SuperPiston.java b/src/main/java/net/smoofyuniverse/superpiston/SuperPiston.java index eae8423..10bc3ac 100644 --- a/src/main/java/net/smoofyuniverse/superpiston/SuperPiston.java +++ b/src/main/java/net/smoofyuniverse/superpiston/SuperPiston.java @@ -27,30 +27,32 @@ import net.smoofyuniverse.map.WorldMapLoader; import net.smoofyuniverse.ore.update.UpdateChecker; import net.smoofyuniverse.superpiston.config.world.WorldConfig; -import net.smoofyuniverse.superpiston.config.world.WorldConfig.Immutable; +import net.smoofyuniverse.superpiston.config.world.WorldConfig.Resolved; import net.smoofyuniverse.superpiston.event.PistonListener; import net.smoofyuniverse.superpiston.impl.internal.InternalServer; -import net.smoofyuniverse.superpiston.util.IOUtil; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.spongepowered.api.Game; +import org.spongepowered.api.Server; import org.spongepowered.api.config.ConfigDir; import org.spongepowered.api.event.Listener; -import org.spongepowered.api.event.game.GameReloadEvent; -import org.spongepowered.api.event.game.state.GameInitializationEvent; -import org.spongepowered.api.event.game.state.GamePreInitializationEvent; -import org.spongepowered.api.event.game.state.GameStartedServerEvent; -import org.spongepowered.api.plugin.Plugin; -import org.spongepowered.api.plugin.PluginContainer; -import org.spongepowered.api.world.World; +import org.spongepowered.api.event.lifecycle.ConstructPluginEvent; +import org.spongepowered.api.event.lifecycle.RefreshGameEvent; +import org.spongepowered.api.event.lifecycle.StartedEngineEvent; +import org.spongepowered.api.event.lifecycle.StartingEngineEvent; +import org.spongepowered.api.world.server.ServerWorld; +import org.spongepowered.configurate.CommentedConfigurationNode; +import org.spongepowered.configurate.ConfigurationOptions; +import org.spongepowered.configurate.hocon.HoconConfigurationLoader; +import org.spongepowered.configurate.loader.ConfigurationLoader; +import org.spongepowered.plugin.PluginContainer; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -@Plugin(id = "superpiston", name = "SuperPiston", version = "1.0.7", authors = "Yeregorix", description = "Allows to modify vanilla pistons") public class SuperPiston { - public static final Logger LOGGER = LoggerFactory.getLogger("SuperPiston"); + public static final Logger LOGGER = LogManager.getLogger("SuperPiston"); private static SuperPiston instance; @Inject @@ -61,8 +63,10 @@ public class SuperPiston { @Inject private PluginContainer container; - private WorldMapLoader configMapLoader; - private WorldMap configMap; + private ConfigurationOptions configOptions; + + private WorldMapLoader configMapLoader; + private WorldMap configMap; public SuperPiston() { if (instance != null) @@ -71,57 +75,57 @@ public SuperPiston() { } @Listener - public void onGamePreInit(GamePreInitializationEvent e) { + public void onConstructPlugin(ConstructPluginEvent e) { + this.configOptions = ConfigurationOptions.defaults().serializers(this.game.configManager().serializers()); + try { Files.createDirectories(this.configDir); } catch (IOException ignored) { } - this.configMapLoader = new WorldMapLoader(LOGGER, - IOUtil.createConfigLoader(this.configDir.resolve("map.conf")), + this.configMapLoader = new WorldMapLoader(LOGGER, + createConfigLoader(this.configDir.resolve("map.conf")), this.configDir.resolve("configs"), WorldConfig.VANILLA) { @Override - protected WorldConfig.Immutable loadConfig(Path file) throws Exception { - return WorldConfig.load(file).toImmutable(); + protected Resolved loadConfig(Path file) throws Exception { + return WorldConfig.load(file).resolve(); } }; } + public ConfigurationLoader createConfigLoader(Path file) { + return HoconConfigurationLoader.builder().defaultOptions(this.configOptions).path(file).build(); + } + @Listener - public void onGameInit(GameInitializationEvent e) { + public void onServerStarting(StartingEngineEvent e) { loadConfigs(); - this.game.getEventManager().registerListeners(this, new PistonListener(this)); + this.game.eventManager().registerListeners(this.container, new PistonListener(this)); - this.game.getEventManager().registerListeners(this, new UpdateChecker(LOGGER, this.container, - IOUtil.createConfigLoader(this.configDir.resolve("update.conf")), "Yeregorix", "SuperPiston")); + this.game.eventManager().registerListeners(this.container, new UpdateChecker(LOGGER, this.container, + createConfigLoader(this.configDir.resolve("update.conf")), "Yeregorix", "SuperPiston")); } private void loadConfigs() { - if (Files.exists(this.configDir.resolve("worlds")) && Files.notExists(this.configDir.resolve("map.conf"))) { - LOGGER.info("Updating config directory structure ..."); - Path worlds = IOUtil.backup(this.configDir).orElse(this.configDir).resolve("worlds"); - this.configMap = this.configMapLoader.importWorlds(worlds); - } else { - this.configMap = this.configMapLoader.load(); - } + this.configMap = this.configMapLoader.load(); } @Listener - public void onGameReload(GameReloadEvent e) { + public void onRefreshGame(RefreshGameEvent e) { loadConfigs(); } @Listener - public void onServerStarted(GameStartedServerEvent e) { - if (this.game.getServer() instanceof InternalServer) - LOGGER.info("SuperPiston " + this.container.getVersion().orElse("?") + " was loaded successfully."); + public void onServerStarted(StartedEngineEvent e) { + if (e.engine() instanceof InternalServer) + LOGGER.info("SuperPiston " + this.container.metadata().version() + " was loaded successfully."); else LOGGER.error("!!WARNING!! SuperPiston was not loaded correctly. Be sure that the jar file is at the root of your mods folder!"); } - public WorldConfig.Immutable getConfig(World world) { - return this.configMap.get(world.getProperties()); + public Resolved getConfig(ServerWorld world) { + return this.configMap.get(world.properties()); } public PluginContainer getContainer() { diff --git a/src/main/java/net/smoofyuniverse/superpiston/SuperPistonTimings.java b/src/main/java/net/smoofyuniverse/superpiston/SuperPistonTimings.java index 71a00d9..92aaf3f 100644 --- a/src/main/java/net/smoofyuniverse/superpiston/SuperPistonTimings.java +++ b/src/main/java/net/smoofyuniverse/superpiston/SuperPistonTimings.java @@ -26,5 +26,5 @@ import co.aikar.timings.Timings; public class SuperPistonTimings { - public static final Timing CALCULATION = Timings.of(SuperPiston.get(), "Calculation"); + public static final Timing CALCULATION = Timings.of(SuperPiston.get().getContainer(), "Calculation"); } diff --git a/src/main/java/net/smoofyuniverse/superpiston/api/event/PistonStructureCalculationEvent.java b/src/main/java/net/smoofyuniverse/superpiston/api/event/PistonStructureCalculationEvent.java index f2dc4a4..b0c257c 100644 --- a/src/main/java/net/smoofyuniverse/superpiston/api/event/PistonStructureCalculationEvent.java +++ b/src/main/java/net/smoofyuniverse/superpiston/api/event/PistonStructureCalculationEvent.java @@ -26,38 +26,46 @@ import net.smoofyuniverse.superpiston.api.structure.calculator.PistonStructureCalculator; import org.spongepowered.api.block.BlockSnapshot; import org.spongepowered.api.event.Cancellable; -import org.spongepowered.api.event.world.TargetWorldEvent; +import org.spongepowered.api.event.Event; import org.spongepowered.api.util.Direction; +import org.spongepowered.api.world.server.ServerWorld; /** * Base event for when a piston determines a {@link PistonStructure}. */ -public interface PistonStructureCalculationEvent extends TargetWorldEvent, Cancellable { +public interface PistonStructureCalculationEvent extends Event, Cancellable { + + /** + * Gets the world. + * + * @return The world. + */ + ServerWorld world(); /** * Gets the block representing the piston. * In a modded environment this block might not even be a real piston. * Do not use this block to determine the direction of the piston. - * Use {@link PistonStructureCalculationEvent#getPistonDirection()} instead. + * Use {@link PistonStructureCalculationEvent#pistonDirection()} instead. * * @return The block representing the piston. */ - BlockSnapshot getPiston(); + BlockSnapshot piston(); /** * @return The direction of the piston. */ - Direction getPistonDirection(); + Direction pistonDirection(); /** * Gets the direction of the movement. - * If it matches {@link PistonStructureCalculationEvent#getPistonDirection()} then the piston is extending. + * If it matches {@link PistonStructureCalculationEvent#pistonDirection()} then the piston is extending. * If it matches the opposite then the piston is retracting. * If it matches something else then a mod is doing weird stuff. * * @return The direction of the movement. */ - Direction getPistonMovement(); + Direction pistonMovement(); /** * Called before the {@link PistonStructure} is calculated. @@ -68,12 +76,12 @@ public interface Pre extends PistonStructureCalculationEvent { /** * @return The original calculator. */ - PistonStructureCalculator getOriginalCalculator(); + PistonStructureCalculator originalCalculator(); /** * @return The calculator that will be used. */ - PistonStructureCalculator getCalculator(); + PistonStructureCalculator calculator(); /** * Sets the calculator the will be used. @@ -91,11 +99,11 @@ public interface Post extends PistonStructureCalculationEvent { /** * @return The calculator that has been used. */ - PistonStructureCalculator getCalculator(); + PistonStructureCalculator calculator(); /** * @return The calculated structure. */ - PistonStructure getStructure(); + PistonStructure structure(); } } diff --git a/src/main/java/net/smoofyuniverse/superpiston/api/structure/PistonStructure.java b/src/main/java/net/smoofyuniverse/superpiston/api/structure/PistonStructure.java index a93dc23..15a959f 100644 --- a/src/main/java/net/smoofyuniverse/superpiston/api/structure/PistonStructure.java +++ b/src/main/java/net/smoofyuniverse/superpiston/api/structure/PistonStructure.java @@ -22,8 +22,8 @@ package net.smoofyuniverse.superpiston.api.structure; -import com.flowpowered.math.vector.Vector3i; import com.google.common.collect.ImmutableList; +import org.spongepowered.math.vector.Vector3i; import java.util.Collection; import java.util.List; diff --git a/src/main/java/net/smoofyuniverse/superpiston/api/structure/calculator/DefaultStructureCalculator.java b/src/main/java/net/smoofyuniverse/superpiston/api/structure/calculator/DefaultStructureCalculator.java index 8b0aaac..148fc77 100644 --- a/src/main/java/net/smoofyuniverse/superpiston/api/structure/calculator/DefaultStructureCalculator.java +++ b/src/main/java/net/smoofyuniverse/superpiston/api/structure/calculator/DefaultStructureCalculator.java @@ -22,15 +22,17 @@ package net.smoofyuniverse.superpiston.api.structure.calculator; -import com.flowpowered.math.vector.Vector3i; +import com.google.common.collect.ImmutableList; import net.smoofyuniverse.superpiston.api.structure.PistonStructure; import net.smoofyuniverse.superpiston.impl.BlockUtil; import net.smoofyuniverse.superpiston.impl.ReactionUtil; import org.spongepowered.api.block.BlockSnapshot; import org.spongepowered.api.block.BlockState; +import org.spongepowered.api.block.BlockType; import org.spongepowered.api.block.BlockTypes; import org.spongepowered.api.util.Direction; -import org.spongepowered.api.world.World; +import org.spongepowered.api.world.server.ServerWorld; +import org.spongepowered.math.vector.Vector3i; import java.util.ArrayList; import java.util.HashSet; @@ -42,8 +44,9 @@ */ public class DefaultStructureCalculator implements PistonStructureCalculator { private static final Direction[] CARDINALS = {Direction.DOWN, Direction.UP, Direction.NORTH, Direction.SOUTH, Direction.WEST, Direction.EAST}; + private static final List STICKY_BLOCKS = ImmutableList.of(BlockTypes.SLIME_BLOCK.get(), BlockTypes.HONEY_BLOCK.get()); - protected final World world; + protected final ServerWorld world; protected final BlockSnapshot piston; protected final Direction direction, movement; @@ -54,8 +57,8 @@ public class DefaultStructureCalculator implements PistonStructureCalculator { protected Set toMoveSet; protected int maxBlocks = 12; - public DefaultStructureCalculator(World world, BlockSnapshot piston, Direction direction, Direction movement) { - if (direction != movement && direction != movement.getOpposite()) + public DefaultStructureCalculator(ServerWorld world, BlockSnapshot piston, Direction direction, Direction movement) { + if (direction != movement && direction != movement.opposite()) throw new IllegalArgumentException("movement"); this.world = world; @@ -68,7 +71,7 @@ public DefaultStructureCalculator(World world, BlockSnapshot piston, Direction d for (Direction dir : CARDINALS) { if (dir == movement) continue; - if (dir == movement.getOpposite()) + if (dir == movement.opposite()) continue; this.sides[i] = dir; i++; @@ -84,7 +87,7 @@ public PistonStructure calculateStructure() { this.toMoveSet = new HashSet<>(); Vector3i offset = this.direction.asBlockOffset(); - boolean moveable = calculate(this.piston.getPosition().add(this.extending ? offset : offset.mul(2))); + boolean moveable = calculate(this.piston.position().add(this.extending ? offset : offset.mul(2))); PistonStructure structure = moveable ? new PistonStructure(this.toMove, this.toDestroy) : new PistonStructure(false); this.toMove = null; @@ -95,7 +98,7 @@ public PistonStructure calculateStructure() { @SuppressWarnings("ForLoopReplaceableByForEach") protected boolean calculate(Vector3i origin) { - BlockState state = this.world.getBlock(origin); + BlockState state = this.world.block(origin); MovementReaction reaction = getReaction(state, origin); if (reaction == MovementReaction.DESTROY) { @@ -110,7 +113,7 @@ protected boolean calculate(Vector3i origin) { for (int i = 0; i < this.toMove.size(); ++i) { Vector3i pos = this.toMove.get(i); - if (isSticky(this.world.getBlock(pos), pos) && !addBranchingBlocks(pos)) + if (isSticky(this.world.block(pos), pos) && !addBranchingBlocks(pos)) return false; } @@ -118,15 +121,15 @@ protected boolean calculate(Vector3i origin) { } protected boolean addBlockLine(Vector3i origin, Direction dir) { - BlockState state = this.world.getBlock(origin); + BlockState state = this.world.block(origin); - if (BlockUtil.isAir(this.world, state, origin)) + if (BlockUtil.isAir(state)) return true; if (this.toMoveSet.contains(origin)) return true; - if (this.piston.getPosition().equals(origin)) + if (this.piston.position().equals(origin)) return true; MovementReaction reaction = getReaction(state, origin); @@ -141,13 +144,19 @@ protected boolean addBlockLine(Vector3i origin, Direction dir) { Vector3i pos = origin; while (isSticky(state, pos)) { + BlockState prevState = state; + Vector3i prevPos = pos; + pos = pos.sub(offset); - state = this.world.getBlock(pos); + state = this.world.block(pos); + + if (BlockUtil.isAir(state)) + break; - if (BlockUtil.isAir(this.world, state, pos)) + if (this.piston.position().equals(pos)) break; - if (this.piston.getPosition().equals(pos)) + if (!canStickToEachOther(prevState, prevPos, state, pos)) break; if (getReaction(state, pos) != MovementReaction.NORMAL) @@ -177,19 +186,19 @@ protected boolean addBlockLine(Vector3i origin, Direction dir) { for (int l = 0; l <= k + m; ++l) { Vector3i pos2 = this.toMove.get(l); - if (isSticky(this.world.getBlock(pos2), pos2) && !addBranchingBlocks(pos2)) + if (isSticky(this.world.block(pos2), pos2) && !addBranchingBlocks(pos2)) return false; } return true; } - state = this.world.getBlock(pos); + state = this.world.block(pos); - if (BlockUtil.isAir(this.world, state, pos)) + if (BlockUtil.isAir(state)) return true; - if (this.piston.getPosition().equals(pos)) + if (this.piston.position().equals(pos)) return false; reaction = getReaction(state, pos); @@ -212,27 +221,37 @@ protected boolean addBlockLine(Vector3i origin, Direction dir) { } protected boolean addBranchingBlocks(Vector3i origin) { + BlockState originState = this.world.block(origin); for (Direction dir : this.sides) { - if (!addBlockLine(origin.add(dir.asBlockOffset()), dir)) + Vector3i branch = origin.add(dir.asBlockOffset()); + if (canStickToEachOther(originState, origin, this.world.block(branch), branch) && !addBlockLine(branch, dir)) return false; } return true; } - protected void reorderListAtCollision(int a, int b) { + protected void reorderListAtCollision(int start, int pivot) { int size = this.toMove.size(); List list = new ArrayList<>(size); - list.addAll(this.toMove.subList(0, b)); - list.addAll(this.toMove.subList(size - a, size)); - list.addAll(this.toMove.subList(b, size - a)); + list.addAll(this.toMove.subList(0, pivot)); + list.addAll(this.toMove.subList(size - start, size)); + list.addAll(this.toMove.subList(pivot, size - start)); this.toMove.clear(); this.toMove.addAll(list); } public boolean isSticky(BlockState state, Vector3i pos) { - return state.getType() == BlockTypes.SLIME; + return STICKY_BLOCKS.contains(state.type()); + } + + public boolean canStickToEachOther(BlockState state1, Vector3i pos1, BlockState state2, Vector3i pos2) { + boolean sticky1 = isSticky(state1, pos1); + boolean sticky2 = isSticky(state2, pos2); + if (sticky1 && sticky2) + return state1.type() == state2.type(); + return sticky1 || sticky2; } public MovementReaction getReaction(BlockState state, Vector3i pos) { diff --git a/src/main/java/net/smoofyuniverse/superpiston/config/world/WorldConfig.java b/src/main/java/net/smoofyuniverse/superpiston/config/world/WorldConfig.java index e38a4aa..5ab6e48 100644 --- a/src/main/java/net/smoofyuniverse/superpiston/config/world/WorldConfig.java +++ b/src/main/java/net/smoofyuniverse/superpiston/config/world/WorldConfig.java @@ -23,17 +23,15 @@ package net.smoofyuniverse.superpiston.config.world; import com.google.common.collect.ImmutableMap; -import com.google.common.reflect.TypeToken; import net.smoofyuniverse.superpiston.SuperPiston; import net.smoofyuniverse.superpiston.api.structure.calculator.DefaultStructureCalculator.MovementReaction; import net.smoofyuniverse.superpiston.util.IOUtil; -import ninja.leaping.configurate.ConfigurationNode; -import ninja.leaping.configurate.commented.CommentedConfigurationNode; -import ninja.leaping.configurate.loader.ConfigurationLoader; -import ninja.leaping.configurate.objectmapping.ObjectMappingException; -import ninja.leaping.configurate.objectmapping.Setting; -import ninja.leaping.configurate.objectmapping.serialize.ConfigSerializable; import org.spongepowered.api.block.BlockState; +import org.spongepowered.configurate.CommentedConfigurationNode; +import org.spongepowered.configurate.ConfigurationNode; +import org.spongepowered.configurate.loader.ConfigurationLoader; +import org.spongepowered.configurate.objectmapping.ConfigSerializable; +import org.spongepowered.configurate.objectmapping.meta.Setting; import java.io.IOException; import java.nio.file.Path; @@ -41,51 +39,51 @@ import java.util.Map; import static net.smoofyuniverse.superpiston.util.MathUtil.clamp; +import static net.smoofyuniverse.superpiston.util.RegistryUtil.resolveBlockStates; @ConfigSerializable public class WorldConfig { public static final int CURRENT_VERSION = 1, MINIMUM__VERSION = 1; - public static final TypeToken TOKEN = TypeToken.of(WorldConfig.class); - public static final Immutable VANILLA = new WorldConfig().toImmutable(); + public static final Resolved VANILLA = new Resolved(ImmutableMap.of(), ImmutableMap.of(), 12); @Setting(value = "BlockReactions") - public Map blockReactions = new HashMap<>(); + public Map blockReactions = new HashMap<>(); @Setting(value = "StickyBlocks") - public Map stickyBlocks = new HashMap<>(); + public Map stickyBlocks = new HashMap<>(); @Setting(value = "MaxBlocks") public int maxBlocks = 12; - public Immutable toImmutable() { - return new Immutable(this.blockReactions, this.stickyBlocks, this.maxBlocks); + public Resolved resolve() { + return new Resolved(resolveBlockStates(this.blockReactions), resolveBlockStates(this.stickyBlocks), this.maxBlocks); } - public static WorldConfig load(Path file) throws IOException, ObjectMappingException { - ConfigurationLoader loader = IOUtil.createConfigLoader(file); + public static WorldConfig load(Path file) throws IOException { + ConfigurationLoader loader = SuperPiston.get().createConfigLoader(file); CommentedConfigurationNode root = loader.load(); - int version = root.getNode("Version").getInt(); + int version = root.node("Version").getInt(); if ((version > CURRENT_VERSION || version < MINIMUM__VERSION) && IOUtil.backup(file).isPresent()) { SuperPiston.LOGGER.info("Your config version is not supported. A new one will be generated."); - root = loader.createEmptyNode(); + root = loader.createNode(); } - ConfigurationNode cfgNode = root.getNode("Config"); - WorldConfig cfg = cfgNode.getValue(TOKEN, new WorldConfig()); + ConfigurationNode cfgNode = root.node("Config"); + WorldConfig cfg = cfgNode.get(WorldConfig.class, new WorldConfig()); cfg.maxBlocks = clamp(cfg.maxBlocks, 1, 500); - root.getNode("Version").setValue(CURRENT_VERSION); - cfgNode.setValue(TOKEN, cfg); + root.node("Version").set(CURRENT_VERSION); + cfgNode.set(cfg); loader.save(root); return cfg; } - public static class Immutable { + public static class Resolved { public final Map blockReactions; public final Map stickyBlocks; public final int maxBlocks; - public Immutable(Map blockReactions, Map stickyBlocks, int maxBlocks) { + public Resolved(Map blockReactions, Map stickyBlocks, int maxBlocks) { this.blockReactions = ImmutableMap.copyOf(blockReactions); this.stickyBlocks = ImmutableMap.copyOf(stickyBlocks); this.maxBlocks = maxBlocks; diff --git a/src/main/java/net/smoofyuniverse/superpiston/event/PistonListener.java b/src/main/java/net/smoofyuniverse/superpiston/event/PistonListener.java index 07ac62c..fb6e3ae 100644 --- a/src/main/java/net/smoofyuniverse/superpiston/event/PistonListener.java +++ b/src/main/java/net/smoofyuniverse/superpiston/event/PistonListener.java @@ -27,7 +27,7 @@ import net.smoofyuniverse.superpiston.impl.calculator.SuperPistonStructureCalculator; import org.spongepowered.api.event.Listener; import org.spongepowered.api.event.Order; -import org.spongepowered.api.world.World; +import org.spongepowered.api.world.server.ServerWorld; public class PistonListener { private final SuperPiston plugin; @@ -38,8 +38,8 @@ public PistonListener(SuperPiston plugin) { @Listener(order = Order.FIRST) public void onPreStructureCalculation(PistonStructureCalculationEvent.Pre e) { - World world = e.getTargetWorld(); + ServerWorld world = e.world(); e.setCalculator(new SuperPistonStructureCalculator(world, - e.getPiston(), e.getPistonDirection(), e.getPistonMovement(), this.plugin.getConfig(world))); + e.piston(), e.pistonDirection(), e.pistonMovement(), this.plugin.getConfig(world))); } } diff --git a/src/main/java/net/smoofyuniverse/superpiston/impl/BlockUtil.java b/src/main/java/net/smoofyuniverse/superpiston/impl/BlockUtil.java index 492be02..735d6be 100644 --- a/src/main/java/net/smoofyuniverse/superpiston/impl/BlockUtil.java +++ b/src/main/java/net/smoofyuniverse/superpiston/impl/BlockUtil.java @@ -22,34 +22,16 @@ package net.smoofyuniverse.superpiston.impl; -import com.flowpowered.math.vector.Vector3i; -import net.minecraft.block.material.Material; -import net.minecraft.block.state.IBlockState; -import net.minecraft.world.IBlockAccess; -import org.spongepowered.api.block.BlockState; -import org.spongepowered.api.world.World; -import org.spongepowered.common.util.VecHelper; +import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.level.material.Material; public class BlockUtil { - private static final boolean useForge = detectForge(); - @SuppressWarnings("deprecation") - public static boolean hasTileEntity(BlockState state) { - IBlockState nmsState = (IBlockState) state; - return useForge ? nmsState.getBlock().hasTileEntity(nmsState) : nmsState.getBlock().hasTileEntity(); + public static boolean isEntityBlock(org.spongepowered.api.block.BlockState state) { + return ((BlockState) state).getBlock().isEntityBlock(); } - public static boolean isAir(World world, BlockState state, Vector3i pos) { - IBlockState nmsState = (IBlockState) state; - return useForge ? nmsState.getBlock().isAir(nmsState, (IBlockAccess) world, VecHelper.toBlockPos(pos)) : nmsState.getMaterial() == Material.AIR; - } - - private static boolean detectForge() { - try { - Class.forName("net.minecraftforge.common.ForgeVersion"); - return true; - } catch (ClassNotFoundException e) { - return false; - } + public static boolean isAir(org.spongepowered.api.block.BlockState state) { + return ((BlockState) state).getMaterial() == Material.AIR; } } diff --git a/src/main/java/net/smoofyuniverse/superpiston/impl/ReactionUtil.java b/src/main/java/net/smoofyuniverse/superpiston/impl/ReactionUtil.java index ead5aee..393623d 100644 --- a/src/main/java/net/smoofyuniverse/superpiston/impl/ReactionUtil.java +++ b/src/main/java/net/smoofyuniverse/superpiston/impl/ReactionUtil.java @@ -22,51 +22,50 @@ package net.smoofyuniverse.superpiston.impl; -import com.flowpowered.math.vector.Vector3i; -import net.minecraft.block.Block; -import net.minecraft.block.BlockPistonBase; -import net.minecraft.block.material.EnumPushReaction; -import net.minecraft.block.state.IBlockState; -import net.minecraft.init.Blocks; -import net.minecraft.util.math.BlockPos; +import net.minecraft.core.BlockPos; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.Blocks; +import net.minecraft.world.level.block.piston.PistonBaseBlock; +import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.level.material.PushReaction; import net.smoofyuniverse.superpiston.api.structure.calculator.DefaultStructureCalculator.MovementReaction; -import org.spongepowered.api.block.BlockState; import org.spongepowered.api.util.Direction; -import org.spongepowered.api.world.World; +import org.spongepowered.api.world.server.ServerWorld; import org.spongepowered.common.util.VecHelper; +import org.spongepowered.math.vector.Vector3i; public class ReactionUtil { - public static MovementReaction getDefaultReaction(World world, BlockState state, Vector3i pos, Direction movement) { + public static MovementReaction getDefaultReaction(ServerWorld world, org.spongepowered.api.block.BlockState state, Vector3i pos, Direction movement) { BlockPos blockPos = VecHelper.toBlockPos(pos); - net.minecraft.world.World nmsWorld = (net.minecraft.world.World) world; + ServerLevel level = (ServerLevel) world; - if (!nmsWorld.getWorldBorder().contains(blockPos)) + if (!level.getWorldBorder().isWithinBounds(blockPos)) return MovementReaction.BLOCK; - if (pos.getY() < 0 || (movement == Direction.DOWN && pos.getY() == 0)) + if (pos.y() < 0 || (movement == Direction.DOWN && pos.y() == 0)) return MovementReaction.BLOCK; - int h = nmsWorld.getHeight() - 1; - if (pos.getY() > h || (movement == Direction.UP && pos.getY() == h)) + int h = level.getMaxBuildHeight() - 1; + if (pos.y() > h || (movement == Direction.UP && pos.y() == h)) return MovementReaction.BLOCK; - if (BlockUtil.hasTileEntity(state)) + if (BlockUtil.isEntityBlock(state)) return MovementReaction.BLOCK; - IBlockState nmsState = (IBlockState) state; + BlockState nmsState = (BlockState) state; Block block = nmsState.getBlock(); - if (block == Blocks.PISTON || block == Blocks.STICKY_PISTON) - return nmsState.getValue(BlockPistonBase.EXTENDED) ? MovementReaction.BLOCK : MovementReaction.NORMAL; + return nmsState.getValue(PistonBaseBlock.EXTENDED) ? MovementReaction.BLOCK : MovementReaction.NORMAL; - if (block == Blocks.OBSIDIAN || nmsState.getBlockHardness(nmsWorld, blockPos) == -1.0f) + if (block == Blocks.OBSIDIAN || nmsState.getDestroySpeed(level, blockPos) == -1.0f) return MovementReaction.BLOCK; - return fromNMS(nmsState.getPushReaction()); + return fromNMS(nmsState.getPistonPushReaction()); } - public static MovementReaction fromNMS(EnumPushReaction reaction) { + public static MovementReaction fromNMS(PushReaction reaction) { switch (reaction) { case NORMAL: return MovementReaction.NORMAL; diff --git a/src/main/java/net/smoofyuniverse/superpiston/impl/calculator/SuperPistonStructureCalculator.java b/src/main/java/net/smoofyuniverse/superpiston/impl/calculator/SuperPistonStructureCalculator.java index da57b4a..d808fe0 100644 --- a/src/main/java/net/smoofyuniverse/superpiston/impl/calculator/SuperPistonStructureCalculator.java +++ b/src/main/java/net/smoofyuniverse/superpiston/impl/calculator/SuperPistonStructureCalculator.java @@ -22,18 +22,18 @@ package net.smoofyuniverse.superpiston.impl.calculator; -import com.flowpowered.math.vector.Vector3i; import net.smoofyuniverse.superpiston.api.structure.calculator.DefaultStructureCalculator; -import net.smoofyuniverse.superpiston.config.world.WorldConfig; +import net.smoofyuniverse.superpiston.config.world.WorldConfig.Resolved; import org.spongepowered.api.block.BlockSnapshot; import org.spongepowered.api.block.BlockState; import org.spongepowered.api.util.Direction; -import org.spongepowered.api.world.World; +import org.spongepowered.api.world.server.ServerWorld; +import org.spongepowered.math.vector.Vector3i; public class SuperPistonStructureCalculator extends DefaultStructureCalculator { - public final WorldConfig.Immutable config; + public final Resolved config; - public SuperPistonStructureCalculator(World world, BlockSnapshot piston, Direction direction, Direction movement, WorldConfig.Immutable config) { + public SuperPistonStructureCalculator(ServerWorld world, BlockSnapshot piston, Direction direction, Direction movement, Resolved config) { super(world, piston, direction, movement); if (config == null) throw new IllegalArgumentException("config"); diff --git a/src/main/java/net/smoofyuniverse/superpiston/impl/event/AbstractStructureCalculationEvent.java b/src/main/java/net/smoofyuniverse/superpiston/impl/event/AbstractStructureCalculationEvent.java index 5d9cffe..4876f8f 100644 --- a/src/main/java/net/smoofyuniverse/superpiston/impl/event/AbstractStructureCalculationEvent.java +++ b/src/main/java/net/smoofyuniverse/superpiston/impl/event/AbstractStructureCalculationEvent.java @@ -24,19 +24,19 @@ import net.smoofyuniverse.superpiston.api.event.PistonStructureCalculationEvent; import org.spongepowered.api.block.BlockSnapshot; -import org.spongepowered.api.event.cause.Cause; +import org.spongepowered.api.event.Cause; import org.spongepowered.api.event.impl.AbstractEvent; import org.spongepowered.api.util.Direction; -import org.spongepowered.api.world.World; +import org.spongepowered.api.world.server.ServerWorld; public abstract class AbstractStructureCalculationEvent extends AbstractEvent implements PistonStructureCalculationEvent { private final Cause cause; - private final World world; + private final ServerWorld world; private final BlockSnapshot piston; private final Direction direction, movement; private boolean cancelled = false; - public AbstractStructureCalculationEvent(Cause cause, World world, BlockSnapshot piston, Direction direction, Direction movement) { + public AbstractStructureCalculationEvent(Cause cause, ServerWorld world, BlockSnapshot piston, Direction direction, Direction movement) { this.cause = cause; this.world = world; this.piston = piston; @@ -45,27 +45,27 @@ public AbstractStructureCalculationEvent(Cause cause, World world, BlockSnapshot } @Override - public Cause getCause() { + public Cause cause() { return this.cause; } @Override - public World getTargetWorld() { + public ServerWorld world() { return this.world; } @Override - public BlockSnapshot getPiston() { + public BlockSnapshot piston() { return this.piston; } @Override - public Direction getPistonDirection() { + public Direction pistonDirection() { return this.direction; } @Override - public Direction getPistonMovement() { + public Direction pistonMovement() { return this.movement; } diff --git a/src/main/java/net/smoofyuniverse/superpiston/impl/event/PostStructureCalculationEvent.java b/src/main/java/net/smoofyuniverse/superpiston/impl/event/PostStructureCalculationEvent.java index 16a2102..1f6fc86 100644 --- a/src/main/java/net/smoofyuniverse/superpiston/impl/event/PostStructureCalculationEvent.java +++ b/src/main/java/net/smoofyuniverse/superpiston/impl/event/PostStructureCalculationEvent.java @@ -26,27 +26,27 @@ import net.smoofyuniverse.superpiston.api.structure.PistonStructure; import net.smoofyuniverse.superpiston.api.structure.calculator.PistonStructureCalculator; import org.spongepowered.api.block.BlockSnapshot; -import org.spongepowered.api.event.cause.Cause; +import org.spongepowered.api.event.Cause; import org.spongepowered.api.util.Direction; -import org.spongepowered.api.world.World; +import org.spongepowered.api.world.server.ServerWorld; public class PostStructureCalculationEvent extends AbstractStructureCalculationEvent implements PistonStructureCalculationEvent.Post { private final PistonStructureCalculator calculator; private final PistonStructure structure; - public PostStructureCalculationEvent(Cause cause, World world, BlockSnapshot piston, Direction direction, Direction movement, PistonStructureCalculator calculator, PistonStructure structure) { + public PostStructureCalculationEvent(Cause cause, ServerWorld world, BlockSnapshot piston, Direction direction, Direction movement, PistonStructureCalculator calculator, PistonStructure structure) { super(cause, world, piston, direction, movement); this.calculator = calculator; this.structure = structure; } @Override - public PistonStructureCalculator getCalculator() { + public PistonStructureCalculator calculator() { return this.calculator; } @Override - public PistonStructure getStructure() { + public PistonStructure structure() { return this.structure; } } diff --git a/src/main/java/net/smoofyuniverse/superpiston/impl/event/PreStructureCalculationEvent.java b/src/main/java/net/smoofyuniverse/superpiston/impl/event/PreStructureCalculationEvent.java index 86dcd2d..ded63c1 100644 --- a/src/main/java/net/smoofyuniverse/superpiston/impl/event/PreStructureCalculationEvent.java +++ b/src/main/java/net/smoofyuniverse/superpiston/impl/event/PreStructureCalculationEvent.java @@ -25,27 +25,27 @@ import net.smoofyuniverse.superpiston.api.event.PistonStructureCalculationEvent; import net.smoofyuniverse.superpiston.api.structure.calculator.PistonStructureCalculator; import org.spongepowered.api.block.BlockSnapshot; -import org.spongepowered.api.event.cause.Cause; +import org.spongepowered.api.event.Cause; import org.spongepowered.api.util.Direction; -import org.spongepowered.api.world.World; +import org.spongepowered.api.world.server.ServerWorld; public class PreStructureCalculationEvent extends AbstractStructureCalculationEvent implements PistonStructureCalculationEvent.Pre { private final PistonStructureCalculator originalCalculator; private PistonStructureCalculator calculator; - public PreStructureCalculationEvent(Cause cause, World world, BlockSnapshot piston, Direction direction, Direction movement, PistonStructureCalculator calculator) { + public PreStructureCalculationEvent(Cause cause, ServerWorld world, BlockSnapshot piston, Direction direction, Direction movement, PistonStructureCalculator calculator) { super(cause, world, piston, direction, movement); this.originalCalculator = calculator; this.calculator = calculator; } @Override - public PistonStructureCalculator getOriginalCalculator() { + public PistonStructureCalculator originalCalculator() { return this.originalCalculator; } @Override - public PistonStructureCalculator getCalculator() { + public PistonStructureCalculator calculator() { return this.calculator; } diff --git a/src/main/java/net/smoofyuniverse/superpiston/mixin/block/BlockPistonBaseMixin.java b/src/main/java/net/smoofyuniverse/superpiston/mixin/block/PistonBaseBlockMixin.java similarity index 53% rename from src/main/java/net/smoofyuniverse/superpiston/mixin/block/BlockPistonBaseMixin.java rename to src/main/java/net/smoofyuniverse/superpiston/mixin/block/PistonBaseBlockMixin.java index b7176ac..d960eca 100644 --- a/src/main/java/net/smoofyuniverse/superpiston/mixin/block/BlockPistonBaseMixin.java +++ b/src/main/java/net/smoofyuniverse/superpiston/mixin/block/PistonBaseBlockMixin.java @@ -22,26 +22,26 @@ package net.smoofyuniverse.superpiston.mixin.block; -import net.minecraft.block.BlockPistonBase; -import net.minecraft.block.material.EnumPushReaction; -import net.minecraft.block.state.IBlockState; -import net.minecraft.util.EnumFacing; -import net.minecraft.util.math.BlockPos; -import net.minecraft.world.World; +import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.piston.PistonBaseBlock; +import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.level.material.PushReaction; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Redirect; -@Mixin(BlockPistonBase.class) -public abstract class BlockPistonBaseMixin { +@Mixin(PistonBaseBlock.class) +public abstract class PistonBaseBlockMixin { - @Redirect(method = "eventReceived", at = @At(value = "INVOKE", target = "Lnet/minecraft/block/state/IBlockState;getPushReaction()Lnet/minecraft/block/material/EnumPushReaction;")) - public EnumPushReaction onGetPushReaction(IBlockState stateIn, IBlockState state, World world, BlockPos pos, int id, int param) { - return EnumPushReaction.NORMAL; + @Redirect(method = "triggerEvent", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/block/state/BlockState;getPistonPushReaction()Lnet/minecraft/world/level/material/PushReaction;")) + public PushReaction alwaysPushNormal(BlockState stateIn, BlockState state, Level level, BlockPos pos, int id, int param) { + return PushReaction.NORMAL; } - @Redirect(method = "eventReceived", at = @At(value = "INVOKE", target = "Lnet/minecraft/block/BlockPistonBase;canPush(Lnet/minecraft/block/state/IBlockState;Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/util/EnumFacing;ZLnet/minecraft/util/EnumFacing;)Z")) - public boolean onCanPull(IBlockState state, World world, BlockPos pos, EnumFacing facing, boolean destroy, EnumFacing facing2) { + @Redirect(method = "triggerEvent", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/block/piston/PistonBaseBlock;isPushable(Lnet/minecraft/world/level/block/state/BlockState;Lnet/minecraft/world/level/Level;Lnet/minecraft/core/BlockPos;Lnet/minecraft/core/Direction;ZLnet/minecraft/core/Direction;)Z")) + public boolean alwaysPushable(BlockState state, Level level, BlockPos pos, Direction facing, boolean destroy, Direction facing2) { return true; } } diff --git a/src/main/java/net/smoofyuniverse/superpiston/mixin/block/BlockPistonStructureHelperMixin.java b/src/main/java/net/smoofyuniverse/superpiston/mixin/block/PistonStructureResolverMixin.java similarity index 70% rename from src/main/java/net/smoofyuniverse/superpiston/mixin/block/BlockPistonStructureHelperMixin.java rename to src/main/java/net/smoofyuniverse/superpiston/mixin/block/PistonStructureResolverMixin.java index c4df52a..ac527fb 100644 --- a/src/main/java/net/smoofyuniverse/superpiston/mixin/block/BlockPistonStructureHelperMixin.java +++ b/src/main/java/net/smoofyuniverse/superpiston/mixin/block/PistonStructureResolverMixin.java @@ -22,11 +22,9 @@ package net.smoofyuniverse.superpiston.mixin.block; -import com.flowpowered.math.vector.Vector3i; -import net.minecraft.block.state.BlockPistonStructureHelper; -import net.minecraft.util.EnumFacing; -import net.minecraft.util.math.BlockPos; -import net.minecraft.world.World; +import net.minecraft.core.BlockPos; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.piston.PistonStructureResolver; import net.smoofyuniverse.superpiston.SuperPiston; import net.smoofyuniverse.superpiston.SuperPistonTimings; import net.smoofyuniverse.superpiston.api.structure.PistonStructure; @@ -36,8 +34,9 @@ import net.smoofyuniverse.superpiston.impl.event.PreStructureCalculationEvent; import org.spongepowered.api.Sponge; import org.spongepowered.api.block.BlockSnapshot; -import org.spongepowered.api.event.cause.Cause; +import org.spongepowered.api.event.Cause; import org.spongepowered.api.util.Direction; +import org.spongepowered.api.world.server.ServerWorld; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Overwrite; @@ -47,17 +46,18 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.common.util.Constants; import org.spongepowered.common.util.VecHelper; +import org.spongepowered.math.vector.Vector3i; import java.util.List; -@Mixin(BlockPistonStructureHelper.class) -public class BlockPistonStructureHelperMixin { +@Mixin(PistonStructureResolver.class) +public class PistonStructureResolverMixin { @Shadow @Final - private World world; + private Level level; @Shadow @Final - private List toMove; + private List toPush; @Shadow @Final private List toDestroy; @@ -66,17 +66,17 @@ public class BlockPistonStructureHelperMixin { private BlockSnapshot piston; @Inject(method = "", at = @At("RETURN")) - public void onInit(World world, BlockPos pos, EnumFacing pistonFacing, boolean extending, CallbackInfo ci) { - this.piston = ((org.spongepowered.api.world.World) this.world).createSnapshot(pos.getX(), pos.getY(), pos.getZ()); - this.direction = Constants.DirectionFunctions.getFor(pistonFacing); - this.movement = extending ? this.direction : this.direction.getOpposite(); + public void onInit(Level level, BlockPos pos, net.minecraft.core.Direction pistonDirection, boolean extending, CallbackInfo ci) { + this.piston = ((ServerWorld) this.level).createSnapshot(pos.getX(), pos.getY(), pos.getZ()); + this.direction = Constants.DirectionFunctions.getFor(pistonDirection); + this.movement = extending ? this.direction : this.direction.opposite(); } /** * @author Yeregorix */ @Overwrite - public boolean canMove() { + public boolean resolve() { SuperPistonTimings.CALCULATION.startTiming(); boolean r = calculate(); SuperPistonTimings.CALCULATION.stopTiming(); @@ -85,21 +85,21 @@ public boolean canMove() { } public boolean calculate() { - this.toMove.clear(); + this.toPush.clear(); this.toDestroy.clear(); - Cause cause = Sponge.getCauseStackManager().getCurrentCause(); + Cause cause = Sponge.server().causeStackManager().currentCause(); PreStructureCalculationEvent preEvent = new PreStructureCalculationEvent( - cause, (org.spongepowered.api.world.World) this.world, this.piston, this.direction, this.movement, - new DefaultStructureCalculator((org.spongepowered.api.world.World) this.world, this.piston, this.direction, this.movement)); + cause, (ServerWorld) this.level, this.piston, this.direction, this.movement, + new DefaultStructureCalculator((ServerWorld) this.level, this.piston, this.direction, this.movement)); - Sponge.getEventManager().post(preEvent); + Sponge.eventManager().post(preEvent); if (preEvent.isCancelled()) return false; - PistonStructureCalculator calculator = preEvent.getCalculator(); + PistonStructureCalculator calculator = preEvent.calculator(); PistonStructure structure = null; try { structure = calculator.calculateStructure(); @@ -111,16 +111,16 @@ public boolean calculate() { return false; PostStructureCalculationEvent postEvent = new PostStructureCalculationEvent( - cause, (org.spongepowered.api.world.World) this.world, this.piston, this.direction, this.movement, + cause, (ServerWorld) this.level, this.piston, this.direction, this.movement, calculator, structure); - Sponge.getEventManager().post(postEvent); + Sponge.eventManager().post(postEvent); if (postEvent.isCancelled()) return false; for (Vector3i pos : structure.getBlocksToMove()) - this.toMove.add(VecHelper.toBlockPos(pos)); + this.toPush.add(VecHelper.toBlockPos(pos)); for (Vector3i pos : structure.getBlocksToDestroy()) this.toDestroy.add(VecHelper.toBlockPos(pos)); diff --git a/src/main/java/net/smoofyuniverse/superpiston/util/IOUtil.java b/src/main/java/net/smoofyuniverse/superpiston/util/IOUtil.java index 45d0f74..f1f6b69 100644 --- a/src/main/java/net/smoofyuniverse/superpiston/util/IOUtil.java +++ b/src/main/java/net/smoofyuniverse/superpiston/util/IOUtil.java @@ -23,9 +23,6 @@ package net.smoofyuniverse.superpiston.util; import net.smoofyuniverse.superpiston.SuperPiston; -import ninja.leaping.configurate.commented.CommentedConfigurationNode; -import ninja.leaping.configurate.hocon.HoconConfigurationLoader; -import ninja.leaping.configurate.loader.ConfigurationLoader; import java.io.IOException; import java.nio.file.Files; @@ -34,10 +31,6 @@ public class IOUtil { - public static ConfigurationLoader createConfigLoader(Path file) { - return HoconConfigurationLoader.builder().setPath(file).build(); - } - public static Optional backup(Path file) { if (!Files.exists(file)) return Optional.empty(); diff --git a/src/main/java/net/smoofyuniverse/superpiston/util/RegistryUtil.java b/src/main/java/net/smoofyuniverse/superpiston/util/RegistryUtil.java new file mode 100644 index 0000000..22400ed --- /dev/null +++ b/src/main/java/net/smoofyuniverse/superpiston/util/RegistryUtil.java @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2018-2021 Hugo Dupanloup (Yeregorix) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.smoofyuniverse.superpiston.util; + +import net.smoofyuniverse.superpiston.SuperPiston; +import org.spongepowered.api.ResourceKey; +import org.spongepowered.api.block.BlockState; +import org.spongepowered.api.block.BlockType; +import org.spongepowered.api.registry.Registry; +import org.spongepowered.api.registry.RegistryTypes; + +import java.util.*; +import java.util.Map.Entry; +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; +import java.util.stream.Stream; + +public class RegistryUtil { + + public static Map resolveBlockStates(Map map) { + Registry blockTypeRegistry = RegistryTypes.BLOCK_TYPE.get(); + BlockState.Builder blockStateBuilder = BlockState.builder(); + + Map states = new HashMap<>(); + Set unknownKeys = new HashSet<>(), invalidPatterns = new HashSet<>(); + + for (Entry entry : map.entrySet()) { + String key = entry.getKey(); + V value = entry.getValue(); + + boolean negate = key.startsWith("-"); + if (negate) + key = key.substring(1); + + if (key.startsWith("regex!")) { + key = key.substring(6); + + Pattern pattern; + try { + pattern = Pattern.compile(key, Pattern.CASE_INSENSITIVE); + } catch (PatternSyntaxException e) { + invalidPatterns.add(key); + continue; + } + + Stream matchedStates = blockTypeRegistry.stream() + .flatMap(type -> type.validStates().stream()) + .filter(state -> pattern.matcher(state.toString()).matches()); + + if (negate) + matchedStates.forEach(states::remove); + else + matchedStates.forEach(state -> states.put(state, value)); + continue; + } + + try { + Optional type = blockTypeRegistry.findValue(ResourceKey.resolve(key)); + if (type.isPresent()) { + List typeStates = type.get().validStates(); + if (negate) + typeStates.forEach(states::remove); + else + typeStates.forEach(state -> states.put(state, value)); + continue; + } + } catch (Exception ignored) { + } + + try { + BlockState state = blockStateBuilder.reset().fromString(key).build(); + if (negate) + states.remove(state); + else + states.put(state, value); + continue; + } catch (Exception ignored) { + } + + unknownKeys.add(key); + } + + if (!unknownKeys.isEmpty()) { + StringBuilder sb = new StringBuilder(); + sb.append("Unknown block states:"); + for (String key : unknownKeys) + sb.append(' ').append(key); + SuperPiston.LOGGER.warn(sb.toString()); + } + + if (!invalidPatterns.isEmpty()) { + StringBuilder sb = new StringBuilder(); + sb.append("Invalid block states patterns:"); + for (String pattern : invalidPatterns) + sb.append(' ').append(pattern); + SuperPiston.LOGGER.warn(sb.toString()); + } + + return states; + } +} diff --git a/src/main/resources/META-INF/sponge_plugins.json b/src/main/resources/META-INF/sponge_plugins.json new file mode 100644 index 0000000..8421ead --- /dev/null +++ b/src/main/resources/META-INF/sponge_plugins.json @@ -0,0 +1,35 @@ +{ + "loader": { + "name": "java_plain", + "version": "1.0" + }, + "license": "MIT", + "plugins": [ + { + "id": "superpiston", + "name": "SuperPiston", + "version": "1.1.0", + "entrypoint": "net.smoofyuniverse.superpiston.SuperPiston", + "description": "Configurable piston behavior", + "links": { + "homepage": "https://ore.spongepowered.org/Yeregorix/SuperPiston", + "source": "https://github.com/Yeregorix/SuperPiston/", + "issues": "https://github.com/Yeregorix/SuperPiston/issues" + }, + "contributors": [ + { + "name": "Yeregorix", + "description": "Lead Developer" + } + ], + "dependencies": [ + { + "id": "spongeapi", + "version": "8.0.0", + "load-order": "AFTER", + "optional": false + } + ] + } + ] +} diff --git a/src/main/resources/META-INF/superpiston_at.cfg b/src/main/resources/META-INF/superpiston_at.cfg deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/resources/mixins.superpiston.json b/src/main/resources/mixins.superpiston.json index 36c8d57..4426a00 100644 --- a/src/main/resources/mixins.superpiston.json +++ b/src/main/resources/mixins.superpiston.json @@ -1,13 +1,12 @@ { "required": true, - "minVersion": "0.7.10", + "minVersion": "0.8", "package": "net.smoofyuniverse.superpiston.mixin", "target": "@env(DEFAULT)", "compatibilityLevel": "JAVA_8", - "refmap": "mixins.superpiston.refmap.json", "server": [ - "block.BlockPistonBaseMixin", - "block.BlockPistonStructureHelperMixin", + "block.PistonBaseBlockMixin", + "block.PistonStructureResolverMixin", "server.MinecraftServerMixin" ], "injectors": {