diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..12f8644 --- /dev/null +++ b/.gitignore @@ -0,0 +1,25 @@ +# eclipse +bin +*.launch +.settings +.metadata +.classpath +.project + +# idea +out +*.ipr +*.iws +*.iml +.idea + +# gradle +build +.gradle + +# other +eclipse +run + +# Files from Forge MDK +forge*changelog.txt diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..22a4f99 --- /dev/null +++ b/build.gradle @@ -0,0 +1,119 @@ +buildscript { + repositories { + maven { url = 'https://files.minecraftforge.net/maven' } + jcenter() + mavenCentral() + } + dependencies { + classpath group: 'net.minecraftforge.gradle', name: 'ForgeGradle', version: '3.+', changing: true + } +} +apply plugin: 'net.minecraftforge.gradle' +apply plugin: 'eclipse' +apply plugin: 'maven-publish' + +version = '1.0' +group = 'com.amadornes.mcmultipart' +archivesBaseName = 'MCMultiPart' + +sourceCompatibility = targetCompatibility = compileJava.sourceCompatibility = compileJava.targetCompatibility = '1.8' + +sourceSets { + examples { + java { + compileClasspath += main.output + runtimeClasspath += main.output + } + } +} + +configurations { + examplesCompile.extendsFrom compile + examplesRuntime.extendsFrom runtime +} + +minecraft { + mappings channel: 'snapshot', version: '20190719-1.14.3' + + runs { + client { + workingDirectory project.file('run') + + property 'forge.logging.markers', 'SCAN,REGISTRIES,REGISTRYDUMP' + + property 'forge.logging.console.level', 'debug' + + mods { + examplemod { + source sourceSets.main + } + } + } + + server { + workingDirectory project.file('run') + + property 'forge.logging.markers', 'SCAN,REGISTRIES,REGISTRYDUMP' + + property 'forge.logging.console.level', 'debug' + + mods { + examplemod { + source sourceSets.main + } + } + } + + data { + workingDirectory project.file('run') + + property 'forge.logging.markers', 'SCAN,REGISTRIES,REGISTRYDUMP' + + property 'forge.logging.console.level', 'debug' + + args '--mod', 'examplemod', '--all', '--output', file('src/generated/resources/') + + mods { + examplemod { + source sourceSets.main + } + } + } + } +} + +dependencies { + minecraft 'net.minecraftforge:forge:1.14.4-28.1.102' +} + +jar { + manifest { + attributes([ + "Specification-Title": "MCMultiPart", + "Specification-Vendor": "amadornes", + "Specification-Version": "3", + "Implementation-Title": project.name, + "Implementation-Version": "${version}", + "Implementation-Vendor" :"amadornes", + "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ") + ]) + } +} + +def reobfFile = file("$buildDir/reobfJar/output.jar") +def reobfArtifact = artifacts.add('default', reobfFile) { + type 'jar' + builtBy 'reobfJar' +} +publishing { + publications { + mavenJava(MavenPublication) { + artifact reobfArtifact + } + } + repositories { + maven { + url "file:///${project.projectDir}/mcmodsrepo" + } + } +} \ No newline at end of file diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..878bf1f --- /dev/null +++ b/gradle.properties @@ -0,0 +1,4 @@ +# Sets default memory used for gradle commands. Can be overridden by user or command line properties. +# This is required to provide enough memory for the Minecraft decompilation process. +org.gradle.jvmargs=-Xmx3G +org.gradle.daemon=false \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..7a3265e Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..949819d --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-4.9-bin.zip diff --git a/gradlew b/gradlew new file mode 100644 index 0000000..cccdd3d --- /dev/null +++ b/gradlew @@ -0,0 +1,172 @@ +#!/usr/bin/env sh + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..f955316 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,84 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/src/examples/java/com/amadornes/mcmultipart/examples/ExampleCounterButton.java b/src/examples/java/com/amadornes/mcmultipart/examples/ExampleCounterButton.java new file mode 100644 index 0000000..7b8f7a5 --- /dev/null +++ b/src/examples/java/com/amadornes/mcmultipart/examples/ExampleCounterButton.java @@ -0,0 +1,62 @@ +package com.amadornes.mcmultipart.examples; + +import com.amadornes.mcmultipart.api.BlockSlot; +import com.amadornes.mcmultipart.api.IMultipart; +import net.minecraft.block.Block; +import net.minecraft.block.BlockState; +import net.minecraft.block.material.Material; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.state.IProperty; +import net.minecraft.state.IntegerProperty; +import net.minecraft.state.StateContainer; +import net.minecraft.util.Hand; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.BlockRayTraceResult; +import net.minecraft.util.math.shapes.ISelectionContext; +import net.minecraft.util.math.shapes.VoxelShape; +import net.minecraft.world.IBlockReader; +import net.minecraft.world.World; + +import javax.annotation.Nonnull; + +import static com.amadornes.mcmultipart.api.MultipartHelper.replaceBlockState; + +public class ExampleCounterButton extends Block implements IMultipart { + + private static final IProperty COUNT = IntegerProperty.create("count", 0, 15); + + private static final BlockSlot SLOT = BlockSlot.get("mcmpexamples", "counter_button_slot"); + + private static final VoxelShape SHAPE = makeCuboidShape(4, 4, 4, 12, 12, 12); + + public ExampleCounterButton() { + super(Block.Properties.create(Material.ROCK)); + } + + @Override + protected void fillStateContainer(StateContainer.Builder builder) { + builder.add(COUNT); + } + + @Override + @Nonnull + public BlockSlot getSlot(@Nonnull BlockState state) { + return SLOT; + } + + @Override + @Nonnull + public VoxelShape getShape(BlockState state, IBlockReader worldIn, BlockPos pos, ISelectionContext context) { + return SHAPE; + } + + @Override + public boolean onBlockActivated(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult hit) { + // On the server, count up once per click + if (!world.isRemote) { + replaceBlockState(world, pos, state, state.cycle(COUNT)); + } + return true; + } + +} diff --git a/src/examples/java/com/amadornes/mcmultipart/examples/ExampleCoverBlock.java b/src/examples/java/com/amadornes/mcmultipart/examples/ExampleCoverBlock.java new file mode 100644 index 0000000..bc09655 --- /dev/null +++ b/src/examples/java/com/amadornes/mcmultipart/examples/ExampleCoverBlock.java @@ -0,0 +1,64 @@ +package com.amadornes.mcmultipart.examples; + +import com.amadornes.mcmultipart.api.BlockSlot; +import com.amadornes.mcmultipart.api.IMultipart; +import net.minecraft.block.Block; +import net.minecraft.block.BlockState; +import net.minecraft.block.material.Material; +import net.minecraft.state.IProperty; +import net.minecraft.state.StateContainer; +import net.minecraft.state.properties.BlockStateProperties; +import net.minecraft.state.properties.Half; +import net.minecraft.util.Direction; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.shapes.ISelectionContext; +import net.minecraft.util.math.shapes.VoxelShape; +import net.minecraft.world.IBlockReader; + +import javax.annotation.Nonnull; +import java.util.EnumMap; +import java.util.Map; + +public class ExampleCoverBlock extends Block implements IMultipart { + + private static final IProperty FACE = BlockStateProperties.FACING; + + private static final Map SLOTS = new EnumMap<>(Direction.class); + + private static final Map SHAPES = new EnumMap<>(Direction.class); + + static { + for (Direction direction : Direction.values()) { + SLOTS.put(direction, BlockSlot.get("mcmpexamples", "cover_" + direction.getName())); + } + + SHAPES.put(Direction.DOWN, makeCuboidShape(0, 0, 0, 16, 2, 16)); + SHAPES.put(Direction.UP, makeCuboidShape(0, 14, 0, 16, 16, 16)); + SHAPES.put(Direction.NORTH, makeCuboidShape(0, 0, 0, 16, 16, 2)); + SHAPES.put(Direction.SOUTH, makeCuboidShape(0, 0, 14, 16, 16, 16)); + SHAPES.put(Direction.WEST, makeCuboidShape(0, 0, 0, 2, 16, 16)); + SHAPES.put(Direction.EAST, makeCuboidShape(14, 0, 0, 16, 16, 16)); + } + + public ExampleCoverBlock() { + super(Properties.create(Material.ROCK)); + } + + @Override + protected void fillStateContainer(StateContainer.Builder builder) { + builder.add(FACE); + } + + @Override + @Nonnull + public BlockSlot getSlot(@Nonnull BlockState state) { + return SLOTS.get(state.get(FACE)); + } + + @Override + @Nonnull + public VoxelShape getShape(BlockState state, IBlockReader world, BlockPos pos, ISelectionContext context) { + return SHAPES.get(state.get(FACE)); + } + +} diff --git a/src/examples/java/com/amadornes/mcmultipart/examples/ExamplePipe.java b/src/examples/java/com/amadornes/mcmultipart/examples/ExamplePipe.java new file mode 100644 index 0000000..a448bf4 --- /dev/null +++ b/src/examples/java/com/amadornes/mcmultipart/examples/ExamplePipe.java @@ -0,0 +1,104 @@ +package com.amadornes.mcmultipart.examples; + +import com.amadornes.mcmultipart.api.BlockSlot; +import com.amadornes.mcmultipart.api.IMultipart; +import net.minecraft.block.Block; +import net.minecraft.block.BlockState; +import net.minecraft.block.material.Material; +import net.minecraft.state.BooleanProperty; +import net.minecraft.state.IProperty; +import net.minecraft.state.StateContainer; +import net.minecraft.util.Direction; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.shapes.IBooleanFunction; +import net.minecraft.util.math.shapes.ISelectionContext; +import net.minecraft.util.math.shapes.VoxelShape; +import net.minecraft.util.math.shapes.VoxelShapes; +import net.minecraft.world.IBlockReader; +import net.minecraft.world.IWorld; + +import javax.annotation.Nonnull; +import java.util.EnumMap; +import java.util.IdentityHashMap; +import java.util.Map; + +import static com.amadornes.mcmultipart.api.MultipartHelper.getBlockState; +import static com.amadornes.mcmultipart.api.MultipartHelper.testShapeOcclusion; + +public class ExamplePipe extends Block implements IMultipart { + + private static final Map> CONNECTION_PROPERTIES = new EnumMap<>(Direction.class); + + private static final BlockSlot SLOT = BlockSlot.get("mcmpexamples", "pipe_slot"); + + private static final VoxelShape CENTER_SHAPE = makeCuboidShape(4, 4, 4, 12, 12, 12); + private static final Map SIDE_SHAPES = new EnumMap<>(Direction.class); + private static final Map SHAPES = new IdentityHashMap<>(); + + static { + for (Direction direction : Direction.values()) { + CONNECTION_PROPERTIES.put(direction, BooleanProperty.create(direction.getName())); + } + + SIDE_SHAPES.put(Direction.DOWN, makeCuboidShape(4, 0, 4, 12, 4, 12)); + SIDE_SHAPES.put(Direction.UP, makeCuboidShape(4, 12, 4, 12, 16, 12)); + SIDE_SHAPES.put(Direction.NORTH, makeCuboidShape(4, 4, 0, 12, 12, 4)); + SIDE_SHAPES.put(Direction.SOUTH, makeCuboidShape(4, 4, 12, 12, 12, 16)); + SIDE_SHAPES.put(Direction.WEST, makeCuboidShape(0, 4, 4, 4, 12, 12)); + SIDE_SHAPES.put(Direction.EAST, makeCuboidShape(12, 4, 4, 16, 12, 12)); + } + + public ExamplePipe() { + super(Properties.create(Material.ROCK)); + } + + @Override + protected void fillStateContainer(StateContainer.Builder builder) { + CONNECTION_PROPERTIES.values().forEach(builder::add); + } + + @Override + @Nonnull + public BlockSlot getSlot(@Nonnull BlockState state) { + return SLOT; + } + + @Override + @Nonnull + public VoxelShape getShape(BlockState state, IBlockReader world, BlockPos pos, ISelectionContext context) { + return SHAPES.computeIfAbsent(state, ExamplePipe::computeShape); + } + + @Nonnull + @Override + public VoxelShape getOcclusionShape(@Nonnull BlockState state, @Nonnull IBlockReader world, @Nonnull BlockPos pos) { + return CENTER_SHAPE; // The pipe only "occupies" the center of the block + } + + @Nonnull + @Override + public BlockState updatePostPlacement(@Nonnull BlockState state, Direction side, BlockState neighborState, IWorld world, BlockPos pos, BlockPos neighborPos) { + IProperty property = CONNECTION_PROPERTIES.get(side); + // Check if there's a pipe in the neighboring block + if (getBlockState(world, neighborPos, SLOT).getBlock() == this) { + VoxelShape shape = SIDE_SHAPES.get(side); + VoxelShape neighborShape = SIDE_SHAPES.get(side.getOpposite()); + // If neither this block's connection space nor the other's is taken up, connect + if (!testShapeOcclusion(world, pos, shape) && !testShapeOcclusion(world, neighborPos, neighborShape)) { + return state.with(property, true); + } + } + return state.with(property, false); + } + + private static VoxelShape computeShape(BlockState state) { + VoxelShape shape = CENTER_SHAPE; + for (Direction direction : Direction.values()) { + if (state.get(CONNECTION_PROPERTIES.get(direction))) { + shape = VoxelShapes.combineAndSimplify(shape, SIDE_SHAPES.get(direction), IBooleanFunction.OR); + } + } + return shape; + } + +} diff --git a/src/main/java/com/amadornes/mcmultipart/MCMultiPart.java b/src/main/java/com/amadornes/mcmultipart/MCMultiPart.java new file mode 100644 index 0000000..9a21337 --- /dev/null +++ b/src/main/java/com/amadornes/mcmultipart/MCMultiPart.java @@ -0,0 +1,26 @@ +package com.amadornes.mcmultipart; + +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; +import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent; +import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +@Mod(MCMultiPart.MODID) +public class MCMultiPart { + + public static final String MODID = "mcmultipart"; + + public MCMultiPart() { + FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup); + FMLJavaModLoadingContext.get().getModEventBus().addListener(this::doClientStuff); + } + + private void setup(FMLCommonSetupEvent event) { + } + + private void doClientStuff(FMLClientSetupEvent event) { + } + +} diff --git a/src/main/java/com/amadornes/mcmultipart/api/BlockSlot.java b/src/main/java/com/amadornes/mcmultipart/api/BlockSlot.java new file mode 100644 index 0000000..3e000df --- /dev/null +++ b/src/main/java/com/amadornes/mcmultipart/api/BlockSlot.java @@ -0,0 +1,47 @@ +package com.amadornes.mcmultipart.api; + +import com.amadornes.mcmultipart.MCMultiPart; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; +import net.minecraft.util.ResourceLocation; + +import javax.annotation.Nonnull; + +public final class BlockSlot { + + /** + * The default slot. + *

+ * This indicates that multipart behavior is not supported. + */ + @Nonnull + public static final BlockSlot FULL_BLOCK = get(MCMultiPart.MODID, "full_block"); + + private static final LoadingCache SLOTS = CacheBuilder.newBuilder().build(CacheLoader.from(BlockSlot::new)); + + /** + * Gets (or creates) a block slot by name. + */ + public static BlockSlot get(String namespace, String path) { + return get(new ResourceLocation(namespace, path)); + } + + /** + * Gets (or creates) a block slot by name. + */ + public static BlockSlot get(ResourceLocation name) { + return SLOTS.getUnchecked(name); + } + + private BlockSlot(ResourceLocation name) { + this.name = name; + } + + private final ResourceLocation name; + + public ResourceLocation getName() { + return name; + } + +} \ No newline at end of file diff --git a/src/main/java/com/amadornes/mcmultipart/api/IMultipart.java b/src/main/java/com/amadornes/mcmultipart/api/IMultipart.java new file mode 100644 index 0000000..e03c9e5 --- /dev/null +++ b/src/main/java/com/amadornes/mcmultipart/api/IMultipart.java @@ -0,0 +1,79 @@ +package com.amadornes.mcmultipart.api; + +import net.minecraft.block.Block; +import net.minecraft.block.BlockState; +import net.minecraft.util.ActionResultType; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.shapes.ISelectionContext; +import net.minecraft.util.math.shapes.VoxelShape; +import net.minecraft.world.IBlockReader; +import net.minecraft.world.World; + +public interface IMultipart { + + /** + * Gets the {@link Block} represented by this multipart. + *

+ * Defaults to casting the current object. + */ + default Block getMultipartBlock() { + return (Block) this; + } + + /** + * Gets the slot that the given state takes up when placed in the same space as other blocks. + *

+ * Returning {@link BlockSlot#FULL_BLOCK} signifies that the given state does not support multipart behavior. + */ + BlockSlot getSlot(BlockState state); + + /** + * Gets the shape for a given state when performing an occlusion test.
+ * This is the part of the block that cannot and will not be intersected by other parts in the same block space. + *

+ * The state may not yet be in the world, so perform any necessary checks. + *

+ * Example: In the case of a pipe or tube, this should only return the center of said block, since the sides can + * be covered to block the connection. + */ + default VoxelShape getOcclusionShape(BlockState state, IBlockReader world, BlockPos pos) { + return state.getShape(world, pos, ISelectionContext.dummy()); + } + + /** + * Performs an occlusion test between a state and another state.
+ * Either of them may not be in the world, so perform any necessary checks. + *

+ * Returning {@link ActionResultType#SUCCESS} indicates that the blocks do not intersect with each other.
+ * Returning {@link ActionResultType#FAIL} indicates that the blocks intersect with each other.
+ * Returning {@link ActionResultType#PASS} indicates that this block does not perform special occlusion testing. + *

+ * When testing two blocks against each other:
+ * If either block returns {@link ActionResultType#FAIL}, they intersect.
+ * If neither fail and either block returns {@link ActionResultType#SUCCESS}, they don't intersect.
+ * If both return {@link ActionResultType#PASS}, a default occlusion test is to be performed by the caller. + */ + default ActionResultType testOcclusion(BlockState state, IBlockReader world, BlockPos pos, BlockState otherState) { + return ActionResultType.PASS; + } + + /** + * Called when another state is added to the current block space. + */ + default void onStateAdded(BlockState state, World world, BlockPos pos, Block block, BlockState addedState) { + } + + /** + * Called when another state is removed from the current block space. + */ + default void onStateRemoved(BlockState state, World world, BlockPos pos, Block block, BlockState removedState) { + } + + /** + * Called when another state is replaced in the current block space. + */ + default void onStateReplaced(BlockState state, World world, BlockPos pos, Block block, BlockState beforeState, BlockState afterState) { + } + + +} diff --git a/src/main/java/com/amadornes/mcmultipart/api/MultipartHelper.java b/src/main/java/com/amadornes/mcmultipart/api/MultipartHelper.java new file mode 100644 index 0000000..b39b1bf --- /dev/null +++ b/src/main/java/com/amadornes/mcmultipart/api/MultipartHelper.java @@ -0,0 +1,82 @@ +package com.amadornes.mcmultipart.api; + +import com.amadornes.mcmultipart.util.MultipartHelperInternal; +import net.minecraft.block.Block; +import net.minecraft.block.BlockState; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.shapes.VoxelShape; +import net.minecraft.world.IBlockReader; +import net.minecraft.world.IWorld; + +import javax.annotation.Nullable; +import java.util.Collections; +import java.util.Set; + +public final class MultipartHelper { + + private MultipartHelper() { + } + + @Nullable + public static IMultipart getMultipart(Block block) { + return MultipartHelperInternal.getMultipart(block); + } + + public static Set getAllBlockStates(IBlockReader world, BlockPos pos) { + return MultipartHelperInternal.getAllBlockStates(world, pos); + } + + public static BlockState getBlockState(IBlockReader world, BlockPos pos, BlockSlot slot) { + return MultipartHelperInternal.getBlockState(world, pos, slot); + } + + @Nullable + public static TileEntity getTileEntity(IBlockReader world, BlockPos pos, BlockSlot slot) { + return MultipartHelperInternal.getTileEntity(world, pos, slot); + } + + @Nullable + public static TileEntity getTileEntity(IBlockReader world, BlockPos pos, BlockState state) { + IMultipart part = getMultipart(state.getBlock()); + if (part == null) return null; + return getTileEntity(world, pos, part.getSlot(state)); + } + + public static boolean canAddBlockState(IWorld world, BlockPos pos, BlockState state) { + return MultipartHelperInternal.canAddBlockState(world, pos, state); + } + + public static void addBlockState(IWorld world, BlockPos pos, BlockState state) { + addBlockState(world, pos, state, 3); + } + + public static void addBlockState(IWorld world, BlockPos pos, BlockState state, int flags) { + MultipartHelperInternal.addBlockState(world, pos, state, flags); + } + + public static boolean canReplaceBlockState(IWorld world, BlockPos pos, BlockState originalState, BlockState newState) { + return MultipartHelperInternal.canReplaceBlockState(world, pos, originalState, newState); + } + + public static void replaceBlockState(IWorld world, BlockPos pos, BlockState originalState, BlockState newState) { + replaceBlockState(world, pos, originalState, newState, 3); + } + + public static void replaceBlockState(IWorld world, BlockPos pos, BlockState originalState, BlockState newState, int flags) { + MultipartHelperInternal.replaceBlockState(world, pos, originalState, newState, flags); + } + + public static boolean removeBlockState(IWorld world, BlockPos pos, BlockState state, boolean isMoving) { + return MultipartHelperInternal.removeBlockState(world, pos, state, isMoving); + } + + public static boolean destroyBlockState(IWorld world, BlockPos pos, BlockState state, boolean dropBlock) { + return MultipartHelperInternal.destroyBlockState(world, pos, state, dropBlock); + } + + public static boolean testShapeOcclusion(IBlockReader world, BlockPos pos, VoxelShape shape) { + return MultipartHelperInternal.testOcclusion(world, pos, shape); + } + +} diff --git a/src/main/java/com/amadornes/mcmultipart/api/package-info.java b/src/main/java/com/amadornes/mcmultipart/api/package-info.java new file mode 100644 index 0000000..39b9373 --- /dev/null +++ b/src/main/java/com/amadornes/mcmultipart/api/package-info.java @@ -0,0 +1,7 @@ +@ParametersAreNonnullByDefault +@MethodsReturnNonnullByDefault +package com.amadornes.mcmultipart.api; + +import mcp.MethodsReturnNonnullByDefault; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/src/main/java/com/amadornes/mcmultipart/util/MultipartHelperInternal.java b/src/main/java/com/amadornes/mcmultipart/util/MultipartHelperInternal.java new file mode 100644 index 0000000..6d44f98 --- /dev/null +++ b/src/main/java/com/amadornes/mcmultipart/util/MultipartHelperInternal.java @@ -0,0 +1,67 @@ +package com.amadornes.mcmultipart.util; + +import com.amadornes.mcmultipart.api.BlockSlot; +import com.amadornes.mcmultipart.api.IMultipart; +import net.minecraft.block.Block; +import net.minecraft.block.BlockState; +import net.minecraft.block.Blocks; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.shapes.VoxelShape; +import net.minecraft.world.IBlockReader; +import net.minecraft.world.IWorld; + +import javax.annotation.Nullable; +import java.util.Collections; +import java.util.Set; + +public final class MultipartHelperInternal { + + private MultipartHelperInternal() { + } + + @Nullable + public static IMultipart getMultipart(Block block) { + return null; + } + + public static Set getAllBlockStates(IBlockReader world, BlockPos pos) { + return Collections.singleton(world.getBlockState(pos)); + } + + public static BlockState getBlockState(IBlockReader world, BlockPos pos, BlockSlot slot) { + return slot == BlockSlot.FULL_BLOCK ? world.getBlockState(pos) : Blocks.AIR.getDefaultState(); + } + + @Nullable + public static TileEntity getTileEntity(IBlockReader world, BlockPos pos, BlockSlot slot) { + return slot == BlockSlot.FULL_BLOCK ? world.getTileEntity(pos) : null; + } + + public static boolean canAddBlockState(IWorld world, BlockPos pos, BlockState state) { + return false; + } + + public static void addBlockState(IWorld world, BlockPos pos, BlockState state, int flags) { + } + + public static boolean canReplaceBlockState(IWorld world, BlockPos pos, BlockState originalState, BlockState newState) { + return false; + } + + public static void replaceBlockState(IWorld world, BlockPos pos, BlockState originalState, BlockState newState, int flags) { + } + + public static boolean removeBlockState(IWorld world, BlockPos pos, BlockState state, boolean isMoving) { + return false; + } + + public static boolean destroyBlockState(IWorld world, BlockPos pos, BlockState state, boolean dropBlock) { + return false; + } + + public static boolean testOcclusion(IBlockReader world, BlockPos pos, VoxelShape shape) { + return false; + } + +} diff --git a/src/main/resources/META-INF/mods.toml b/src/main/resources/META-INF/mods.toml new file mode 100644 index 0000000..105a1db --- /dev/null +++ b/src/main/resources/META-INF/mods.toml @@ -0,0 +1,25 @@ +modLoader="javafml" +loaderVersion="[28,)" +issueTrackerURL="http://my.issue.tracker/" +[[mods]] +modId="mcmultipart" +version="${file.jarVersion}" +displayName="MCMultiPart" +displayURL="https://github.com/amadornes/MCMultiPart" +authors="amadornes" +description=''' +A universal multipart API for modern Minecraft. +''' + +[[dependencies.mcmultipart]] + modId="forge" + mandatory=true + versionRange="[28,)" + ordering="NONE" + side="BOTH" +[[dependencies.mcmultipart]] + modId="minecraft" + mandatory=true + versionRange="[1.14.4]" + ordering="NONE" + side="BOTH" diff --git a/src/main/resources/pack.mcmeta b/src/main/resources/pack.mcmeta new file mode 100644 index 0000000..a1e342a --- /dev/null +++ b/src/main/resources/pack.mcmeta @@ -0,0 +1,6 @@ +{ + "pack": { + "description": "MCMultiPart resources", + "pack_format": 4 + } +}