commit 9c1166348e29e71da4f6922e9b707f0d92fd1a49 Author: Towdium Date: Sun Feb 21 20:19:10 2016 +0800 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2c770e0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,22 @@ +# eclipse +bin +*.launch +.settings +.metadata +.classpath +.project + +# idea +out +*.ipr +*.iws +*.iml +.idea + +# gradle +build +.gradle + +# other +eclipse +run diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..f16b4a9 --- /dev/null +++ b/build.gradle @@ -0,0 +1,82 @@ + +// For those who want the bleeding edge +buildscript { + repositories { + jcenter() + maven { + name = "forge" + url = "http://files.minecraftforge.net/maven" + } + } + dependencies { + classpath 'net.minecraftforge.gradle:ForgeGradle:2.1-SNAPSHOT' + } +} +apply plugin: 'net.minecraftforge.gradle.forge' + +/* +// for people who want stable - not yet functional for MC 1.8.8 - we require the forgegradle 2.1 snapshot +plugins { + id "net.minecraftforge.gradle.forge" version "2.0.2" +} +*/ +version = "1.0" +group= "com.yourname.modid" // http://maven.apache.org/guides/mini/guide-naming-conventions.html +archivesBaseName = "modid" + +minecraft { + version = "1.8.9-11.15.1.1722" + runDir = "run" + + // the mappings can be changed at any time, and must be in the following format. + // snapshot_YYYYMMDD snapshot are built nightly. + // stable_# stables are built at the discretion of the MCP team. + // Use non-default mappings at your own risk. they may not allways work. + // simply re-run your setup task after changing the mappings to update your workspace. + mappings = "stable_20" + // makeObfSourceJar = false // an Srg named sources jar is made by default. uncomment this to disable. +} + +dependencies { + // you may put jars on which you depend on in ./libs + // or you may define them like so.. + //compile "some.group:artifact:version:classifier" + //compile "some.group:artifact:version" + + // real examples + //compile 'com.mod-buildcraft:buildcraft:6.0.8:dev' // adds buildcraft to the dev env + //compile 'com.googlecode.efficient-java-matrix-library:ejml:0.24' // adds ejml to the dev env + + // the 'provided' configuration is for optional dependencies that exist at compile-time but might not at runtime. + //provided 'com.mod-buildcraft:buildcraft:6.0.8:dev' + + // the deobf configurations: 'deobfCompile' and 'deobfProvided' are the same as the normal compile and provided, + // except that these dependencies get remapped to your current MCP mappings + //deobfCompile 'com.mod-buildcraft:buildcraft:6.0.8:dev' + //deobfProvided 'com.mod-buildcraft:buildcraft:6.0.8:dev' + + // for more info... + // http://www.gradle.org/docs/current/userguide/artifact_dependencies_tutorial.html + // http://www.gradle.org/docs/current/userguide/dependency_management.html + +} + +processResources +{ + // this will ensure that this task is redone when the versions change. + inputs.property "version", project.version + inputs.property "mcversion", project.minecraft.version + + // replace stuff in mcmod.info, nothing else + from(sourceSets.main.resources.srcDirs) { + include 'mcmod.info' + + // replace version and mcversion + expand 'version':project.version, 'mcversion':project.minecraft.version + } + + // copy everything else, thats not the mcmod.info + from(sourceSets.main.resources.srcDirs) { + exclude 'mcmod.info' + } +} diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..30d399d 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..9397848 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Mon Sep 14 12:28:28 PDT 2015 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-2.7-bin.zip diff --git a/gradlew b/gradlew new file mode 100644 index 0000000..91a7e26 --- /dev/null +++ b/gradlew @@ -0,0 +1,164 @@ +#!/usr/bin/env bash + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# 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 +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; +esac + +# For Cygwin, ensure paths are in UNIX format before anything is touched. +if $cygwin ; then + [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` +fi + +# 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\"`/" >&- +APP_HOME="`pwd -P`" +cd "$SAVED" >&- + +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" ] ; 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"` + + # 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 + +# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules +function splitJvmOpts() { + JVM_OPTS=("$@") +} +eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS +JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" + +exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..8a0b282 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,90 @@ +@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 + +@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= + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@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 Windowz variants + +if not "%OS%" == "Windows_NT" goto win9xME_args +if "%@eval[2+2]" == "4" goto 4NT_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=%* +goto execute + +:4NT_args +@rem Get arguments from the 4NT Shell from JP Software +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/main/java/pers/towdium/tudicraft/Tudicraft.java b/src/main/java/pers/towdium/tudicraft/Tudicraft.java new file mode 100644 index 0000000..4b434c0 --- /dev/null +++ b/src/main/java/pers/towdium/tudicraft/Tudicraft.java @@ -0,0 +1,67 @@ +package pers.towdium.tudicraft; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.resources.model.ModelResourceLocation; +import net.minecraft.item.Item; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.common.SidedProxy; +import net.minecraftforge.fml.common.event.FMLInitializationEvent; +import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; +import net.minecraftforge.fml.common.network.NetworkRegistry; +import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper; +import net.minecraftforge.fml.common.registry.GameRegistry; +import net.minecraftforge.fml.relauncher.Side; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import pers.towdium.tudicraft.event.GuiEventHandler; +import pers.towdium.tudicraft.gui.GuiHandler; +import pers.towdium.tudicraft.item.ItemCalculator; +import pers.towdium.tudicraft.network.IProxy; +import pers.towdium.tudicraft.network.packages.PackageCalculatorUpdate; +import pers.towdium.tudicraft.network.packages.PackageSlotUpdate; + +/** + * @author Towdium + */ + +@Mod(modid = Tudicraft.Reference.MODID, name = Tudicraft.Reference.MODNAME, version = Tudicraft.Reference.VERSION) +public class Tudicraft { + public static Item itemCalculator = new ItemCalculator().setUnlocalizedName("itemCalculator"); + public static SimpleNetworkWrapper networkWrapper; + public static Logger log = LogManager.getLogger(Reference.MODID); + + @SidedProxy(clientSide = "pers.towdium.tudicraft.network.ProxyClient", serverSide = "pers.towdium.tudicraft.network.ProxyServer") + public static IProxy proxy; + + @Mod.Instance(Tudicraft.Reference.MODID) + public static Tudicraft instance; + + public static class Reference { + public static final String MODID = "tudicraft"; + public static final String MODNAME = "Tudicraft"; + public static final String VERSION = "0.0.1"; + } + + @Mod.EventHandler + public static void preInit(FMLPreInitializationEvent event){ + GameRegistry.registerItem(itemCalculator,itemCalculator.getUnlocalizedName().substring(5)); + networkWrapper = NetworkRegistry.INSTANCE.newSimpleChannel(Reference.MODID); + networkWrapper.registerMessage(PackageSlotUpdate.class, PackageSlotUpdate.class, 0, Side.SERVER); + networkWrapper.registerMessage(PackageCalculatorUpdate.class, PackageCalculatorUpdate.class, 0, Side.SERVER); + + } + + @Mod.EventHandler + public static void init(FMLInitializationEvent event){ + if(event.getSide().isClient()){ + Minecraft.getMinecraft().getRenderItem().getItemModelMesher(). + register(itemCalculator, 0, new ModelResourceLocation(Reference.MODID + ":" + itemCalculator.getUnlocalizedName().substring(5), "inventory")); + } + NetworkRegistry.INSTANCE.registerGuiHandler(Tudicraft.instance, new GuiHandler()); + MinecraftForge.EVENT_BUS.register(new GuiEventHandler()); + } + + + +} diff --git a/src/main/java/pers/towdium/tudicraft/core/Calculator.java b/src/main/java/pers/towdium/tudicraft/core/Calculator.java new file mode 100644 index 0000000..0670dcc --- /dev/null +++ b/src/main/java/pers/towdium/tudicraft/core/Calculator.java @@ -0,0 +1,263 @@ +package pers.towdium.tudicraft.core; + +import com.google.common.collect.ImmutableList; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import pers.towdium.tudicraft.Tudicraft; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; + +/** + * @author Towdium + */ +public class Calculator { + List operateRecords; + OperateRecord currentOperate; + List calculators; + List catalystBuffer; + ItemRecord itemRecord; + + + public Calculator (ItemStack itemStack, int amount){ + this(new ItemRecord(itemStack, amount), new LinkedList(), new LinkedList(), true); + } + + public Calculator (ItemRecord itemRecord, List operateRecords, List catalystBuffer, boolean exceedMode){ + this.catalystBuffer = catalystBuffer; + this.itemRecord = itemRecord; + this.operateRecords = operateRecords; + for (Recipe recipe : Tudicraft.proxy.getPlayerHandler().getAllRecipeOf(new ItemStack(itemRecord.item, itemRecord.meta))){ + if(checkRecipeValid(recipe)){ + long count = getOperateCount(recipe, exceedMode); + this.currentOperate = new OperateRecord(recipe, count); + ImmutableList.Builder builder = new ImmutableList.Builder<>(); + builder.addAll(operateRecords); + builder.add(currentOperate); + ImmutableList records = builder.build(); + ImmutableList.Builder calculatorBuilder = new ImmutableList.Builder<>(); + for(ItemStack itemStack : recipe.input){ + if(itemStack != null){ + calculatorBuilder.add(new Calculator( + new ItemRecord(itemStack, count*recipe.getInputAmount(itemStack)), records, catalystBuffer, true + )); + } + } + this.calculators = calculatorBuilder.build(); + return; + } + } + catalystBuffer.add(itemRecord); + } + + @Override + public String toString() { + return "Calculator:" + itemRecord.item.getUnlocalizedName() + " x " + itemRecord.amount; + } + + public CostRecord getCost(){ + if(currentOperate != null){ + CostRecord record = getCurrentCost(); + for(Calculator calculator : calculators){ + if(calculator.currentOperate != null){ + CostRecord buffer = calculator.getCost(); + record.merge(buffer); + } + } + return record; + }else { + return new CostRecord(); + } + } + + public CostRecord getCurrentCost(){ + List output = new LinkedList<>(); + List input = new LinkedList<>(); + for(ItemStack itemStack : currentOperate.recipe.output){ + if(itemStack != null){ + output.add(new ItemRecord(itemStack, currentOperate.count*itemStack.stackSize)); + } + } + for(ItemStack itemStack : currentOperate.recipe.input){ + if(itemStack != null){ + input.add(new ItemRecord(itemStack, currentOperate.count*itemStack.stackSize)); + } + } + return new Calculator.CostRecord(output, input); + } + + protected boolean checkRecipeValid(Recipe recipe){ + for (OperateRecord record : operateRecords){ + if(record.recipe.equals(recipe) && record.count == getOperateCount(recipe)){ + return false; + } + } + return true; + } + + protected long getOperateCount(Recipe recipe){ + return (int)Math.ceil((float)itemRecord.amount/itemRecord.getOutputAmount(recipe)); + } + + protected long getOperateCount(Recipe recipe, boolean exceed){ + if(exceed){ + return (int)Math.ceil((float)itemRecord.amount/itemRecord.getOutputAmount(recipe)); + } else { + return itemRecord.amount/itemRecord.getOutputAmount(recipe); + } + + + } + + public class CostRecord{ + List output; + List input; + List catalyst; + + public CostRecord(){ + output = new ArrayList<>(); + input = new ArrayList<>(); + } + + public CostRecord(List output, List input) { + this.output = output; + this.input = input; + } + + public void merge(CostRecord costRecord){ + LOOP1: + for(ItemRecord record : costRecord.output){ + for(ItemRecord record1 : output){ + if(record1.add(record)) + continue LOOP1; + } + output.add(record); + } + LOOP2: + for(ItemRecord record : costRecord.input){ + for(ItemRecord record1 : input){ + if(record1.add(record)) + continue LOOP2; + } + input.add(record); + } + } + + public void unify(){ + cancelIO(); + for (int i = 0; i < output.size(); i++) { + ItemRecord itemRecord = output.get(i); + if (!itemRecord.isSameType(Calculator.this.itemRecord)) { + CostRecord record = new Calculator(itemRecord, new LinkedList(), new LinkedList(), true).getCost(); + record.cancelIO(); + if (this.canCancelBy(record)) { + List buffer = record.input; + record.input = record.output; + record.output = buffer; + this.merge(record); + this.cancelIO(); + } else { + record = new Calculator(itemRecord, new LinkedList(), new LinkedList(), false).getCost(); + record.cancelIO(); + if(this.canCancelBy(record)) { + List buffer = record.input; + record.input = record.output; + record.output = buffer; + this.merge(record); + this.cancelIO(); + } + } + } + } + cancelEmpty(); + } + + protected boolean canCancelBy(CostRecord record){ + LOOP1: + for(ItemRecord itemRecIn : record.output){ + for(ItemRecord itemRec : output){ + if(itemRec.isSameType(itemRecIn) && itemRec.amount>=itemRecIn.amount){ + continue LOOP1; + } + } + return false; + } + LOOP2: + for(ItemRecord itemRecIn : record.input){ + for(ItemRecord itemRec : input){ + if(itemRec.isSameType(itemRecIn) && itemRec.amount>=itemRecIn.amount){ + continue LOOP2; + } + } + return false; + } + return true; + } + + protected void cancelIO(){ + ImmutableList.Builder catalystBuilder = new ImmutableList.Builder<>(); + if(catalyst != null){ + catalystBuilder.addAll(catalyst); + } + for (ItemRecord inputRec : input) { + for (ItemRecord outputRec : output) { + if (inputRec.cancel(outputRec) && inputRec.amount == 0) { + LOOP: + for (ItemRecord itemRecord : Calculator.this.catalystBuffer){ + if(itemRecord.isSameType(inputRec)){ + if(catalyst != null){ + for (ItemRecord exist : catalyst) { + if (itemRecord.isSameType(exist)) { + continue LOOP; + } + } + } + catalystBuilder.add(inputRec.copy()); + break; + } + } + } + } + } + catalyst = catalystBuilder.build(); + } + + protected void cancelEmpty(){ + ImmutableList.Builder builder = new ImmutableList.Builder<>(); + for(ItemRecord record : output){ + if(record.amount != 0){ + builder.add(record); + } + } + output = builder.build(); + builder = new ImmutableList.Builder<>(); + for(ItemRecord record : input){ + if(record.amount != 0){ + builder.add(record); + } + } + input = builder.build(); + } + } +} + +class OperateRecord{ + Recipe recipe; + long count; + + public OperateRecord(Recipe recipe, long count) { + this.recipe = recipe; + this.count = count; + } + + @Override + public boolean equals(Object obj) { + if(!(obj instanceof OperateRecord)){ + return false; + } else { + OperateRecord record = (OperateRecord) obj; + return record.recipe.equals(recipe) && record.count == count; + } + } +} \ No newline at end of file diff --git a/src/main/java/pers/towdium/tudicraft/core/ItemRecord.java b/src/main/java/pers/towdium/tudicraft/core/ItemRecord.java new file mode 100644 index 0000000..19ff989 --- /dev/null +++ b/src/main/java/pers/towdium/tudicraft/core/ItemRecord.java @@ -0,0 +1,80 @@ +package pers.towdium.tudicraft.core; + +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; + +/** + * @author Towdium + */ +public class ItemRecord{ + Item item; + int meta; + long amount; + + public ItemRecord(ItemStack itemStack, long amount){ + item = itemStack.getItem(); + meta = itemStack.getMetadata(); + this.amount = amount; + } + + public ItemRecord(Item item, int meta, long amount) { + this.item = item; + this.meta = meta; + this.amount = amount; + } + + @Override + public String toString() { + return "itemRecord:" + item.getUnlocalizedName() + " x " + amount; + } + + protected int getOutputAmount(Recipe recipe){ + for(int i = 0; i<4; i++){ + if(recipe.output[i].getItem() == item && recipe.output[i].getMetadata() == meta){ + return ItemStackWrapper.getUnifiedAmount(recipe.output[i]); + } + } + return 0; + } + + public boolean isSameType(ItemRecord record){ + return item == record.item && meta == record.meta; + } + + public boolean add(ItemRecord record){ + if (isSameType(record)){ + amount += record.amount; + return true; + }else { + return false; + } + } + + public boolean minus(ItemRecord record){ + if (isSameType(record)){ + amount -= record.amount; + return true; + }else { + return false; + } + } + + public boolean cancel(ItemRecord record){ + if (isSameType(record)){ + long i = amount>record.amount ? record.amount : amount; + amount -= i; + record.amount -= i; + return i!=0; + }else { + return false; + } + } + + public ItemRecord copy(){ + return new ItemRecord(item, meta, amount); + } + + public ItemStack toItemStack(){ + return new ItemStack(item, (int) amount, meta); + } +} diff --git a/src/main/java/pers/towdium/tudicraft/core/ItemStackWrapper.java b/src/main/java/pers/towdium/tudicraft/core/ItemStackWrapper.java new file mode 100644 index 0000000..5951aa2 --- /dev/null +++ b/src/main/java/pers/towdium/tudicraft/core/ItemStackWrapper.java @@ -0,0 +1,104 @@ +package pers.towdium.tudicraft.core; + +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraftforge.common.util.Constants; + +/** + * @author Towdium + */ +public class ItemStackWrapper { + + public static boolean isTypeEqual(ItemStack one, ItemStack two){ + return ItemStack.areItemsEqual(one, two); + } + + public static boolean isStackEqual(ItemStack one, ItemStack two){ + return ItemStack.areItemStacksEqual(one, two); + } + + public static boolean isSwapAcceptable(ItemStack in, ItemStack out){ + return !isTypeEqual(in, out) || getUnifiedAmount(in) < getUnifiedAmount(out); + } + + public static ItemStack unify(ItemStack itemStack){ + if(itemStack == null){ + return null; + } + NBTTagCompound nbtTagCompound = new NBTTagCompound(); + nbtTagCompound.setInteger("count", getNormalAmount(itemStack)); + nbtTagCompound.setBoolean("approx", false); + ItemStack buffer = new ItemStack(itemStack.getItem(), 1, itemStack.getMetadata()); + buffer.setTagCompound(nbtTagCompound); + return buffer; + } + + public static ItemStack unify(ItemStack itemStack, int amount){ + NBTTagCompound nbtTagCompound = new NBTTagCompound(); + nbtTagCompound.setInteger("count", amount*100); + nbtTagCompound.setBoolean("approx", false); + ItemStack buffer = new ItemStack(itemStack.getItem(), 1, itemStack.getMetadata()); + buffer.setTagCompound(nbtTagCompound); + return buffer; + } + + public static int getUnifiedAmount(ItemStack itemStack){ + if(itemStack.hasTagCompound()){ + return itemStack.getTagCompound().getInteger("count"); + }else { + return itemStack.stackSize; + } + } + + public static boolean getUnifiedApprox(ItemStack itemStack) { + return itemStack.hasTagCompound() && itemStack.getTagCompound().getBoolean("approx"); + } + + public static int getNormalAmount(ItemStack itemStack){ + return itemStack.stackSize*100; + } + + public static class NBT { + public static void initNBT(ItemStack itemStack){ + if(!itemStack.hasTagCompound()){ + itemStack.setTagCompound(new NBTTagCompound()); + } + } + + public static void setItem(ItemStack dest, String key, ItemStack content){ + initNBT(dest); + NBTTagCompound buffer = new NBTTagCompound(); + if (content != null){ + content.writeToNBT(buffer); + dest.getTagCompound().setTag(key, buffer); + }else { + dest.getTagCompound().removeTag(key); + } + + } + + public static ItemStack getItem(ItemStack itemStack, String key){ + NBTTagCompound tagCompound = itemStack.getTagCompound(); + if(tagCompound == null){ + return null; + } + NBTTagCompound itemTagCompound = tagCompound.getCompoundTag(key); + if(itemTagCompound == null){ + return null; + } + return ItemStack.loadItemStackFromNBT(itemTagCompound); + } + + public static void setString(ItemStack dest, String key, String value){ + initNBT(dest); + dest.getTagCompound().setString(key, value); + } + + public static String getString(ItemStack dest, String key){ + if(dest.getTagCompound() != null){ + return dest.getTagCompound().getString(key); + } + else return ""; + } + } +} diff --git a/src/main/java/pers/towdium/tudicraft/core/Recipe.java b/src/main/java/pers/towdium/tudicraft/core/Recipe.java new file mode 100644 index 0000000..f5849a4 --- /dev/null +++ b/src/main/java/pers/towdium/tudicraft/core/Recipe.java @@ -0,0 +1,116 @@ +package pers.towdium.tudicraft.core; + +import net.minecraft.item.ItemStack; + +/** + * @author Towdium + */ +public class Recipe { + ItemStack[] output; + ItemStack[] input; + + public Recipe(ItemStack[] output, ItemStack[] input) { + if(output.length != 4 || input.length != 12){ + throw new IllegalArgumentException("Incorrect array size"); + } + this.output = new ItemStack[4]; + for (int i = 0; i < 4; i++) { + ItemStack itemstack = output[i]; + if(itemstack != null){ + boolean flag = false; + for (int j = 0; j < i; j++) { + if (this.output[j] != null && ItemStackWrapper.isTypeEqual(this.output[j], itemstack)) { + this.output[j].stackSize += itemstack.stackSize; + flag = true; + break; + } + } + if (!flag) { + this.output[i] = itemstack.copy(); + } + } + } + this.input = new ItemStack[12]; + for (ItemStack itemstack : input) { + if(itemstack != null){ + for(int i=0; i<12; i++){ + if(ItemStackWrapper.isTypeEqual(this.input[i], itemstack)){ + this.input[i].stackSize += itemstack.stackSize; + break; + } else if (this.input[i] == null){ + this.input[i] = itemstack.copy(); + break; + } + } + } + } + } + + public ItemStack getOutput(ItemStack itemStack){ + for(int i = 0; i<4; i++){ + if(ItemStackWrapper.isTypeEqual(output[i], itemStack)){ + return output[i]; + } + } + return null; + } + + public int getOutputIndex(ItemStack itemStack){ + for(int i = 0; i<4; i++){ + if(ItemStackWrapper.isTypeEqual(output[i], itemStack)){ + return i; + } + } + return -1; + } + + public int getOutputAmount(ItemStack itemStack){ + for(int i = 0; i<4; i++){ + if(ItemStackWrapper.isTypeEqual(output[i], itemStack)){ + return ItemStackWrapper.getUnifiedAmount(output[i]); + } + } + return 0; + } + + public int getInputAmount(ItemStack itemStack){ + for(int i = 0; i<12; i++){ + if(ItemStackWrapper.isTypeEqual(input[i], itemStack)){ + return ItemStackWrapper.getUnifiedAmount(input[i]); + } + } + return 0; + } + + public boolean getHasOutput(ItemStack itemStack){ + return getOutputIndex(itemStack) != -1; + } + + @Override + public boolean equals(Object obj) { + if(obj instanceof Recipe){ + for(int i=0; i<4; i++){ + if(!ItemStackWrapper.isStackEqual(output[i], ((Recipe)obj).output[i])){ + return false; + } + } + for(int i=0; i<12; i++){ + if(!ItemStackWrapper.isStackEqual(input[i], ((Recipe)obj).input[i])){ + return false; + } + } + return true; + }else { + return false; + } + } + + public boolean isOutputEqual(Recipe recipe){ + for(int i=0; i<4; i++){ + if(!ItemStackWrapper.isStackEqual(output[i], recipe.output[i])){ + return false; + } + } + return true; + } +} diff --git a/src/main/java/pers/towdium/tudicraft/event/GuiEventHandler.java b/src/main/java/pers/towdium/tudicraft/event/GuiEventHandler.java new file mode 100644 index 0000000..3b96275 --- /dev/null +++ b/src/main/java/pers/towdium/tudicraft/event/GuiEventHandler.java @@ -0,0 +1,83 @@ +package pers.towdium.tudicraft.event; + + +import mezz.jei.gui.Focus; +import mezz.jei.gui.ItemListOverlay; +import mezz.jei.input.InputHandler; +import net.minecraft.client.gui.inventory.GuiContainer; +import net.minecraft.client.renderer.texture.TextureUtil; +import net.minecraft.inventory.Slot; +import net.minecraft.item.ItemStack; +import net.minecraftforge.client.event.GuiScreenEvent; +import net.minecraftforge.fml.common.eventhandler.EventPriority; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; +import org.lwjgl.input.Mouse; +import pers.towdium.tudicraft.Tudicraft; +import pers.towdium.tudicraft.gui.calculator.ContainerCalculator; +import pers.towdium.tudicraft.gui.calculator.GuiCalculator; +import pers.towdium.tudicraft.gui.recipeEditor.ContainerRecipeEditor; +import pers.towdium.tudicraft.gui.recipeEditor.GuiRecipeEditor; +import pers.towdium.tudicraft.gui.recipeEditor.InventoryRecipeEditor; +import pers.towdium.tudicraft.plugin.JEIPlugin; + +import java.lang.reflect.Method; +import java.util.Date; + +/** + * @author Towdium + */ +public class GuiEventHandler { + long time = new Date().getTime(); + + @SubscribeEvent(priority = EventPriority.HIGH) + public void onMouseClick(GuiScreenEvent.MouseInputEvent.Pre event){ + if(event.gui instanceof GuiRecipeEditor){ + if(new Date().getTime() - time < 500){ + event.setCanceled(true); + return; + } + if (((GuiRecipeEditor) event.gui).getActiveSlot() != -1){ + GuiRecipeEditor guiContainer = (GuiRecipeEditor) event.gui; + ItemStack itemStack = JEIPlugin.runtime.getItemListOverlay().getStackUnderMouse(); + Slot slot = ((GuiRecipeEditor) event.gui).inventorySlots.getSlot(((GuiRecipeEditor) event.gui).getActiveSlot()); + slot.inventory.setInventorySlotContents(slot.getSlotIndex(), itemStack); + if(Mouse.isButtonDown(0)){ + time = new Date().getTime(); + if(guiContainer.getSlotUnderMouse() != null){ + guiContainer.setActiveSlot(guiContainer.getSlotUnderMouse().getSlotIndex()); + ((ContainerRecipeEditor)((GuiRecipeEditor) event.gui).inventorySlots).getPlayer().playSound("random.click", 1f, 1f ); + event.setCanceled(true); + return; + } + int y = guiContainer.height - Mouse.getEventY() * guiContainer.height / guiContainer.mc.displayHeight - 1; + int x = Mouse.getEventX() * guiContainer.width / guiContainer.mc.displayWidth; + boolean over = ((ItemListOverlay)JEIPlugin.runtime.getItemListOverlay()).isMouseOver(x, y); + if(!over || (y>25 && y -1) + { + long systemTime = System.currentTimeMillis(); + + if(prevSystemTime > 0) + mouseoverTime += systemTime - prevSystemTime; + + prevSystemTime = systemTime; + } + else + { + mouseoverTime = 0; + } + + //render the button's tooltip + if(mouseoverTime > tooltipDelay) + { + String tooltip = GetButtonTooltip(mousedOverButtonId); + if(tooltip != null) + { + RenderTooltip(mouseX, mouseY, tooltip); + } + } + } + + /** + * Determines if a GuiButton is being mouseovered. + * @param mouseX + * @param mouseY + * @param button + * @return true if this button is mouseovered + */ + protected boolean IsButtonMouseovered(int mouseX, int mouseY, GuiButton button) + { + return mouseX >= button.xPosition + button.getButtonWidth() - 10 && mouseX <= button.xPosition + button.getButtonWidth() && + mouseY >= button.yPosition && mouseY <= button.yPosition + 10; + } + + /** + * Render anything special onto all buttons that have tooltips assigned to them. + */ + protected void RenderTooltipButtonEffect() + { + for(int i = 0; i < buttonList.size(); i++) + { + GuiButton button = buttonList.get(i); + + if(GetButtonTooltip(button.id) != null) + { + boolean flag = mc.fontRendererObj.getUnicodeFlag(); + mc.fontRendererObj.setUnicodeFlag(true); + mc.fontRendererObj.drawStringWithShadow("?", button.xPosition+button.getButtonWidth()-5, button.yPosition, 0x99FFFFFF); + mc.fontRendererObj.setUnicodeFlag(flag); + } + } + } + + /** + * Render anything special onto buttons that have tooltips assigned to them when they are mousevered. + * @param button + */ + protected void RenderTooltipButtonMouseoverEffect(GuiButton button) + { + boolean flag = mc.fontRendererObj.getUnicodeFlag(); + mc.fontRendererObj.setUnicodeFlag(true); + mc.fontRendererObj.drawStringWithShadow(FontCodes.AQUA + "?", button.xPosition+button.getButtonWidth()-5, button.yPosition, 0xFFFFFF); + mc.fontRendererObj.setUnicodeFlag(flag); + } + + /** + * Renders a tooltip at (x,y). + * @param x + * @param y + * @param tooltip + */ + protected void RenderTooltip(int x, int y, String tooltip) + { + String[] tooltipArray = ParseTooltipArrayFromString(tooltip); + + int tooltipWidth = GetTooltipWidth(tooltipArray); + int tooltipHeight = GetTooltipHeight(tooltipArray); + + int tooltipX = x + tooltipXOffset; + int tooltipY = y + tooltipYOffset; + + if(tooltipX > width - tooltipWidth - 7) + tooltipX = width - tooltipWidth - 7; + if(tooltipY > height - tooltipHeight - 8) + tooltipY = height - tooltipHeight - 8; + + //render the background inside box + int innerAlpha = -0xFEFFFF0; //very very dark purple + drawGradientRect(tooltipX, tooltipY - 1, tooltipX + tooltipWidth + 6, tooltipY, innerAlpha, innerAlpha); + drawGradientRect(tooltipX, tooltipY + tooltipHeight + 6, tooltipX + tooltipWidth + 6, tooltipY + tooltipHeight + 7, innerAlpha, innerAlpha); + drawGradientRect(tooltipX, tooltipY, tooltipX + tooltipWidth + 6, tooltipY + tooltipHeight + 6, innerAlpha, innerAlpha); + drawGradientRect(tooltipX - 1, tooltipY, tooltipX, tooltipY + tooltipHeight + 6, innerAlpha, innerAlpha); + drawGradientRect(tooltipX + tooltipWidth + 6, tooltipY, tooltipX + tooltipWidth + 7, tooltipY + tooltipHeight + 6, innerAlpha, innerAlpha); + + //render the background outside box + int outerAlpha1 = 0x505000FF; + int outerAlpha2 = (outerAlpha1 & 0xFEFEFE) >> 1 | outerAlpha1 & -0x1000000; + drawGradientRect(tooltipX, tooltipY + 1, tooltipX + 1, tooltipY + tooltipHeight + 6 - 1, outerAlpha1, outerAlpha2); + drawGradientRect(tooltipX + tooltipWidth + 5, tooltipY + 1, tooltipX + tooltipWidth + 7, tooltipY + tooltipHeight + 6 - 1, outerAlpha1, outerAlpha2); + drawGradientRect(tooltipX, tooltipY, tooltipX + tooltipWidth + 3, tooltipY + 1, outerAlpha1, outerAlpha1); + drawGradientRect(tooltipX, tooltipY + tooltipHeight + 5, tooltipX + tooltipWidth + 7, tooltipY + tooltipHeight + 6, outerAlpha2, outerAlpha2); + + //render the foreground text + int lineCount = 0; + for (String s : tooltipArray) + { + mc.fontRendererObj.drawStringWithShadow(s, tooltipX + 2, tooltipY + 2 + lineCount * LINE_HEIGHT, 0xFFFFFF); + lineCount++; + } + } + + /** + * Converts a String representation of a tooltip into a String[], and also decodes any font codes used. + * @param s Ex: "Hello,_nI am your _ltooltip_r and you love me." + * @return An array of Strings such that each String width does not exceed tooltipMaxWidth + */ + protected String[] ParseTooltipArrayFromString(String s) + { + s = DecodeStringCodes(s); + String[] tooltipSections = s.split(tooltipNewlineDelimeter); + ArrayList tooltipArrayList = new ArrayList(); + + for(String section : tooltipSections) + { + String tooltip = ""; + String[] tooltipWords = section.split(" "); + + for(int i = 0; i < tooltipWords.length; i++) + { + int lineWidthWithNextWord = mc.fontRendererObj.getStringWidth(tooltip + tooltipWords[i]); + if(lineWidthWithNextWord > tooltipMaxWidth) + { + tooltipArrayList.add(tooltip.trim()); + tooltip = tooltipWords[i] + " "; + } + else + { + tooltip += tooltipWords[i] + " "; + } + } + + tooltipArrayList.add(tooltip.trim()); + } + + String[] tooltipArray = new String[tooltipArrayList.size()]; + tooltipArrayList.toArray(tooltipArray); + + return tooltipArray; + } + + /** + * Decodes any font codes into something useable by the fontRendererObj. + * @param s E.x.: "Hello,_nI am your _ltooltip_r and you love me." + * @return E.x. output (html not included):
"Hello,
I am your tooltip and you love me." + */ + private String DecodeStringCodes(String s) + { + return s.replace("_0", FontCodes.BLACK) + .replace("_1", FontCodes.DARK_BLUE) + .replace("_2", FontCodes.DARK_GREEN) + .replace("_3", FontCodes.DARK_AQUA) + .replace("_4", FontCodes.DARK_RED) + .replace("_5", FontCodes.DARK_PURPLE) + .replace("_6", FontCodes.GOLD) + .replace("_7", FontCodes.GRAY) + .replace("_8", FontCodes.DARK_GREY) + .replace("_9", FontCodes.BLUE) + .replace("_a", FontCodes.GREEN) + .replace("_b", FontCodes.AQUA) + .replace("_c", FontCodes.RED) + .replace("_d", FontCodes.LIGHT_PURPLE) + .replace("_e", FontCodes.YELLOW) + .replace("_f", FontCodes.WHITE) + .replace("_k", FontCodes.OBFUSCATED) + .replace("_l", FontCodes.BOLD) + .replace("_m", FontCodes.STRIKETHROUGH) + .replace("_n", FontCodes.UNDERLINE) + .replace("_o", FontCodes.ITALICS) + .replace("_r", FontCodes.RESET); + } + + /*** + * Gets the width of the tooltip in pixels. + * @param tooltipArray + * @return + */ + private int GetTooltipWidth(String[] tooltipArray) + { + int longestWidth = 0; + for(String s : tooltipArray) + { + int width = mc.fontRendererObj.getStringWidth(s); + if(width > longestWidth) + longestWidth = width; + } + return longestWidth; + } + + /** + * Gets the height of the tooltip in pixels. + * @param tooltipArray + * @return + */ + private int GetTooltipHeight(String[] tooltipArray) + { + int tooltipHeight = mc.fontRendererObj.FONT_HEIGHT - 2; + if (tooltipArray.length > 1) + { + tooltipHeight += (tooltipArray.length - 1) * LINE_HEIGHT; + } + return tooltipHeight; + } + + /** + * Gets a protected/private field from a class using reflection. + * @param The return type of the field you are getting + * @param The class the field is in + * @param classToAccess The ".class" of the class the field is in + * @param instance The instance of the class + * @param fieldNames comma seperated names the field may have (i.e. obfuscated, non obfuscated). + * Obfustated field names can be found in fml/conf/fields.csv + * @return + */ + public static T GetFieldByReflection(Class classToAccess, E instance, String... fieldNames) + { + Field field = null; + for(String fieldName : fieldNames) + { + try + { + field = classToAccess.getDeclaredField(fieldName); + } + catch(NoSuchFieldException e){} + + if(field != null) + break; + } + + if(field != null) + { + field.setAccessible(true); + T fieldT = null; + try + { + fieldT = (T) field.get(instance); + } + catch (IllegalArgumentException e){} + catch (IllegalAccessException e){} + + return fieldT; + } + + return null; + } + + public class FontCodes + { + //color codes for rendered strings + public static final String BLACK = "\2470"; + public static final String DARK_BLUE = "\2471"; + public static final String DARK_GREEN = "\2472"; + public static final String DARK_AQUA = "\2473"; + public static final String DARK_RED = "\2474"; + public static final String DARK_PURPLE = "\2475"; + public static final String GOLD = "\2476"; + public static final String GRAY = "\2477"; + public static final String DARK_GREY = "\2478"; + public static final String BLUE = "\2479"; + public static final String GREEN = "\247a"; + public static final String AQUA = "\247b"; + public static final String RED = "\247c"; + public static final String LIGHT_PURPLE = "\247d"; + public static final String YELLOW = "\247e"; + public static final String WHITE = "\247f"; + + //font styles + public static final String OBFUSCATED = "\247k"; + public static final String BOLD = "\247l"; + public static final String STRIKETHROUGH = "\247m"; + public static final String UNDERLINE = "\247n"; + public static final String ITALICS = "\247o"; + + public static final String RESET = "\247r"; + } +} \ No newline at end of file diff --git a/src/main/java/pers/towdium/tudicraft/gui/calculator/ContainerCalculator.java b/src/main/java/pers/towdium/tudicraft/gui/calculator/ContainerCalculator.java new file mode 100644 index 0000000..fa1f0b2 --- /dev/null +++ b/src/main/java/pers/towdium/tudicraft/gui/calculator/ContainerCalculator.java @@ -0,0 +1,47 @@ +package pers.towdium.tudicraft.gui.calculator; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.Container; +import net.minecraft.inventory.Slot; +import net.minecraft.item.ItemStack; + + +/** + * @author Towdium + */ +public class ContainerCalculator extends Container { + EntityPlayer player; + InventoryCalculator inventoryCalculator; + + public ContainerCalculator(EntityPlayer player, ItemStack itemCalculator){ + this.player = player; + this.inventoryCalculator = new InventoryCalculator(itemCalculator); + addSlotToContainer(new Slot(inventoryCalculator, 0,9,9)); + int i, left, top; + i = 1; left = 8; top = 64; + for(int a = 0; a < 4; a++){ + for(int b =0; b < 9; b++){ + addSlotToContainer(new Slot(inventoryCalculator,i+9*a+b,left+b*18,top+a*18)); + } + } + } + + @Override + public boolean canInteractWith(EntityPlayer playerIn) { + return true; + } + + @Override + public ItemStack transferStackInSlot(EntityPlayer playerIn, int index) { + return null; + } + + @Override + public ItemStack slotClick(int slotId, int clickedButton, int mode, EntityPlayer playerIn) { + return null; + } + + public EntityPlayer getPlayer() { + return player; + } +} diff --git a/src/main/java/pers/towdium/tudicraft/gui/calculator/GuiCalculator.java b/src/main/java/pers/towdium/tudicraft/gui/calculator/GuiCalculator.java new file mode 100644 index 0000000..57139ec --- /dev/null +++ b/src/main/java/pers/towdium/tudicraft/gui/calculator/GuiCalculator.java @@ -0,0 +1,186 @@ +package pers.towdium.tudicraft.gui.calculator; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.FontRenderer; +import net.minecraft.client.gui.GuiButton; +import net.minecraft.client.gui.GuiTextField; +import net.minecraft.client.renderer.GlStateManager; +import net.minecraft.client.renderer.entity.RenderItem; +import net.minecraft.client.renderer.texture.TextureMap; +import net.minecraft.client.resources.model.IBakedModel; +import net.minecraft.client.resources.model.ModelManager; +import net.minecraft.inventory.Slot; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.util.ResourceLocation; +import net.minecraft.util.StatCollector; +import pers.towdium.tudicraft.Tudicraft; +import pers.towdium.tudicraft.core.Calculator; +import pers.towdium.tudicraft.core.ItemStackWrapper; +import pers.towdium.tudicraft.gui.GuiTooltipScreen; +import pers.towdium.tudicraft.gui.recipeEditor.ContainerRecipeEditor; +import pers.towdium.tudicraft.gui.recipeEditor.GuiRecipeEditor; +import pers.towdium.tudicraft.network.packages.PackageCalculatorUpdate; + +import java.io.IOException; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.List; + +/** + * @author Towdium + */ +public class GuiCalculator extends GuiTooltipScreen{ + GuiTextField textFieldAmount; + GuiButton edit; + Calculator.CostRecord costRecord; + int activeSlot = -1; + int page = 1; + EnumMode mode = EnumMode.input; + + + public enum EnumMode {input, output, catalyst} + + public GuiCalculator(ContainerCalculator containerCalculator){ + super(containerCalculator); + } + + @Override + public void initGui() { + super.initGui(); + edit = new GuiButton(3, guiLeft+125, guiTop+31, 44, 20, StatCollector.translateToLocal("gui.calculator.edit")); + edit.enabled = false; + buttonList.add(new GuiButton(1, guiLeft+7, guiTop+31, 79, 20, StatCollector.translateToLocal("gui.calculator.calculate"))); + buttonList.add(new GuiButton(2, guiLeft+125, guiTop+7, 44, 20, StatCollector.translateToLocal("gui.calculator.add"))); + buttonList.add(edit); + buttonList.add(new GuiButton(4, guiLeft+7, guiTop+139, 20, 20, "<")); + buttonList.add(new GuiButton(5, guiLeft+149, guiTop+139, 20, 20, ">")); + textFieldAmount = new GuiTextField(0, fontRendererObj, guiLeft+39, guiTop+8, 44, 18); + Slot dest = inventorySlots.getSlot(0); + ItemStack itemStack = ((ContainerCalculator)inventorySlots).getPlayer().getHeldItem(); + dest.inventory.setInventorySlotContents(dest.getSlotIndex(), ItemStackWrapper.NBT.getItem(itemStack, "dest")); + textFieldAmount.setText(ItemStackWrapper.NBT.getString(itemStack, "text")); + } + + @Override + protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) { + GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F); + this.mc.getTextureManager().bindTexture(new ResourceLocation(Tudicraft.Reference.MODID,"textures/gui/guiCalculator.png")); + this.drawTexturedModalRect(this.guiLeft, this.guiTop, 0, 0, this.xSize, this.ySize); + if(activeSlot == 0){ + Slot slot = inventorySlots.getSlot(activeSlot); + this.drawTexturedModalRect(this.guiLeft+slot.xDisplayPosition-2, this.guiTop+slot.yDisplayPosition-2, 176, 0, 20, 20); + }else if(activeSlot > 0){ + Slot slot = inventorySlots.getSlot(activeSlot); + this.drawTexturedModalRect(this.guiLeft+slot.xDisplayPosition-1, this.guiTop+slot.yDisplayPosition-1, 196, 0, 18, 18); + } + textFieldAmount.drawTextBox(); + } + + @Override + protected void drawGuiContainerForegroundLayer(int p_146979_1_, int p_146979_2_) { + this.fontRendererObj.drawString("x", 30, 13, 4210752); + } + + @Override + protected String GetButtonTooltip(int buttonId) { + switch (buttonId){ + case 2: return StatCollector.translateToLocal("gui.calculator.addTooltip"); + case 3: return StatCollector.translateToLocal("gui.calculator.editTooltip"); + } + + return null; + } + + @Override + protected void actionPerformed(GuiButton button) throws IOException { + switch (button.id){ + case 1: + if(inventorySlots.getSlot(0).getStack() != null){ + Calculator calculator = new Calculator(inventorySlots.getSlot(0).getStack(), Integer.valueOf(textFieldAmount.getText())); + Calculator.CostRecord record = calculator.getCost(); + record.unify(); + costRecord = record; + } + break; + case 2: + mc.displayGuiScreen(new GuiRecipeEditor(new ContainerRecipeEditor(((ContainerCalculator)inventorySlots).getPlayer()), this)); + break; + } + } + + @Override + protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException { + textFieldAmount.mouseClicked(mouseX, mouseY, mouseButton); + super.mouseClicked(mouseX, mouseY, mouseButton); + Slot slot = getSlotUnderMouse(); + if(slot != null && mouseButton == 0){ + activeSlot = slot.getSlotIndex(); + ((ContainerCalculator)inventorySlots).getPlayer().playSound("random.click", 1f, 1f ); + } + } + + @Override + public void drawScreen(int mouseX, int mouseY, float f) { + super.drawScreen(mouseX, mouseY, f); + //Method + } + + public int getActiveSlot() { + return activeSlot; + } + + public void setActiveSlot(int activeSlot) { + this.activeSlot = activeSlot; + } + + @Override + protected void keyTyped(char typedChar, int keyCode) throws IOException { + if (!this.textFieldAmount.textboxKeyTyped(typedChar, keyCode)){ + if(keyCode == 1){ + ItemStack itemStack = ((ContainerCalculator)inventorySlots).getPlayer().getHeldItem(); + ItemStackWrapper.NBT.setItem(itemStack, "dest", inventorySlots.getSlot(0).inventory.getStackInSlot(inventorySlots.getSlot(0).getSlotIndex())); + ItemStackWrapper.NBT.setString(itemStack, "text", textFieldAmount.getText()); + Tudicraft.networkWrapper.sendToServer(new PackageCalculatorUpdate(((ContainerCalculator)inventorySlots).getPlayer().getHeldItem())); + ((ContainerCalculator)inventorySlots).getPlayer().closeScreen(); + } + } + } + + public void updateScreen(){ + + } + + public void updateSlots(){ + + } + + @Override + public void setWorldAndResolution(Minecraft mc, int width, int height) { + super.setWorldAndResolution(mc, width, height); + ModelManager modelManager = null; + Field[] fields = mc.getClass().getDeclaredFields(); + for(Field field : fields){ + if(ModelManager.class.equals(field.getType())){ + field.setAccessible(true); + try { + modelManager = (ModelManager) field.get(mc); + break; + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + } + if(modelManager != null){ + itemRender = new RenderItem(mc.renderEngine, modelManager){ + @Override + public void renderItemOverlayIntoGUI(FontRenderer fr, ItemStack stack, int xPosition, int yPosition, String text) { + boolean b = fr.getUnicodeFlag(); + fr.setUnicodeFlag(true); + super.renderItemOverlayIntoGUI(fr, stack, xPosition, yPosition, "≈1.23k"); + fr.setUnicodeFlag(b); + } + }; + } + } +} diff --git a/src/main/java/pers/towdium/tudicraft/gui/calculator/InventoryCalculator.java b/src/main/java/pers/towdium/tudicraft/gui/calculator/InventoryCalculator.java new file mode 100644 index 0000000..9f46565 --- /dev/null +++ b/src/main/java/pers/towdium/tudicraft/gui/calculator/InventoryCalculator.java @@ -0,0 +1,17 @@ +package pers.towdium.tudicraft.gui.calculator; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.IInventory; +import net.minecraft.inventory.InventoryBasic; +import net.minecraft.item.ItemStack; +import net.minecraft.util.IChatComponent; + +/** + * @author Towdium + */ +public class InventoryCalculator extends InventoryBasic{ + + public InventoryCalculator(ItemStack itemStack) { + super("Calculator", false, 37); + } +} diff --git a/src/main/java/pers/towdium/tudicraft/gui/itemPicker/ContainerItemPicker.java b/src/main/java/pers/towdium/tudicraft/gui/itemPicker/ContainerItemPicker.java new file mode 100644 index 0000000..f6e73c6 --- /dev/null +++ b/src/main/java/pers/towdium/tudicraft/gui/itemPicker/ContainerItemPicker.java @@ -0,0 +1,39 @@ +package pers.towdium.tudicraft.gui.itemPicker; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.Container; +import net.minecraft.inventory.Slot; +import net.minecraft.item.ItemStack; +import pers.towdium.tudicraft.Tudicraft; + + +/** + * @author Towdium + */ +public class ContainerItemPicker extends Container { + + InventoryItemPicker inventoryItemPicker; + + public ContainerItemPicker(){ + inventoryItemPicker = new InventoryItemPicker(); + addSlotToContainer(new Slot(inventoryItemPicker ,0,0,0)); + addSlotToContainer(new Slot(inventoryItemPicker,1,18,0)); + + } + + @Override + public boolean canInteractWith(EntityPlayer playerIn) { + return true; + } + + @Override + public ItemStack transferStackInSlot(EntityPlayer playerIn, int index) { + return null; + } + + @Override + public ItemStack slotClick(int slotId, int clickedButton, int mode, EntityPlayer playerIn) { + Tudicraft.log.info("Activated"); + return null; + } +} diff --git a/src/main/java/pers/towdium/tudicraft/gui/itemPicker/GuiItemPicker.java b/src/main/java/pers/towdium/tudicraft/gui/itemPicker/GuiItemPicker.java new file mode 100644 index 0000000..62a2004 --- /dev/null +++ b/src/main/java/pers/towdium/tudicraft/gui/itemPicker/GuiItemPicker.java @@ -0,0 +1,84 @@ +package pers.towdium.tudicraft.gui.itemPicker; + +import net.minecraft.client.gui.GuiButton; +import net.minecraft.client.gui.GuiTextField; +import net.minecraft.client.gui.inventory.GuiContainer; +import net.minecraft.client.renderer.GlStateManager; +import net.minecraft.util.ResourceLocation; +import net.minecraft.util.StatCollector; +import pers.towdium.tudicraft.Tudicraft; +import pers.towdium.tudicraft.plugin.JEIPlugin; + +import java.io.IOException; + +/** + * @author Towdium + */ +public class GuiItemPicker extends GuiContainer{ + ContainerItemPicker containerItemPicker; + GuiTextField searchTextField; + + public GuiItemPicker(ContainerItemPicker containerItemPicker){ + super(containerItemPicker); + this.containerItemPicker = containerItemPicker; + } + + @Override + public void initGui() { + super.initGui(); + buttonList.add(new GuiButton(0, guiLeft+31, guiTop+7, 50, 20, StatCollector.translateToLocal("gui.itemPicker.confirm"))); + buttonList.add(new GuiButton(0, guiLeft+7, guiTop+137, 20, 20, "<")); + buttonList.add(new GuiButton(0, guiLeft+149, guiTop+137, 20, 20, ">")); + searchTextField = new GuiTextField(4, fontRendererObj, guiLeft+90, guiTop+7, 79, 20){ + @Override + public boolean textboxKeyTyped(char p_146201_1_, int p_146201_2_) { + boolean b = super.textboxKeyTyped(p_146201_1_, p_146201_2_); + Tudicraft.log.info("执行了!"+JEIPlugin.runtime.getItemListOverlay().getFilterText()); + JEIPlugin.runtime.getItemListOverlay().setFilterText("apple"); + return b; + } + }; + } + + @Override + protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) { + GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F); + this.mc.getTextureManager().bindTexture(new ResourceLocation(Tudicraft.Reference.MODID,"textures/gui/guiItemPicker.png")); + this.drawTexturedModalRect(this.guiLeft, this.guiTop, 0, 0, this.xSize, this.ySize); + } + + @Override + protected void drawGuiContainerForegroundLayer(int p_146979_1_, int p_146979_2_) { + + this.fontRendererObj.drawString(StatCollector.translateToLocal("container.itemPicker"), 8, 6, 4210752); + } + + @Override + public void drawScreen(int mouseX, int mouseY, float partialTicks) { + super.drawScreen(mouseX, mouseY, partialTicks); + searchTextField.drawTextBox(); + } + + @Override + public void onGuiClosed() { + super.onGuiClosed(); + } + + @Override + protected void actionPerformed(GuiButton button) throws IOException { + super.actionPerformed(button); + } + + @Override + protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException { + searchTextField.mouseClicked(mouseX, mouseY, mouseButton); + super.mouseClicked(mouseX, mouseY, mouseButton); + } + + @Override + protected void keyTyped(char typedChar, int keyCode) throws IOException { + if (!this.searchTextField.textboxKeyTyped(typedChar, keyCode)){ + super.keyTyped(typedChar, keyCode); + } + } +} diff --git a/src/main/java/pers/towdium/tudicraft/gui/itemPicker/InventoryItemPicker.java b/src/main/java/pers/towdium/tudicraft/gui/itemPicker/InventoryItemPicker.java new file mode 100644 index 0000000..971d8c1 --- /dev/null +++ b/src/main/java/pers/towdium/tudicraft/gui/itemPicker/InventoryItemPicker.java @@ -0,0 +1,15 @@ +package pers.towdium.tudicraft.gui.itemPicker; + +import net.minecraft.inventory.IInventory; +import net.minecraft.inventory.InventoryBasic; +import net.minecraft.item.ItemStack; + +/** + * @author Towdium + */ +public class InventoryItemPicker extends InventoryBasic { + public InventoryItemPicker() { + super("Item picker", false, 55); + + } +} diff --git a/src/main/java/pers/towdium/tudicraft/gui/recipeEditor/ContainerRecipeEditor.java b/src/main/java/pers/towdium/tudicraft/gui/recipeEditor/ContainerRecipeEditor.java new file mode 100644 index 0000000..b99dd6a --- /dev/null +++ b/src/main/java/pers/towdium/tudicraft/gui/recipeEditor/ContainerRecipeEditor.java @@ -0,0 +1,98 @@ +package pers.towdium.tudicraft.gui.recipeEditor; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.Container; +import net.minecraft.inventory.Slot; +import net.minecraft.item.ItemStack; +import pers.towdium.tudicraft.core.Recipe; + + +/** + * @author Towdium + */ +public class ContainerRecipeEditor extends Container { + + InventoryRecipeEditor inventoryRecipeEditor; + EntityPlayer player; + + public ContainerRecipeEditor(EntityPlayer player){ + inventoryRecipeEditor = new InventoryRecipeEditor(); + int i, left, top; + i = 0; left = 9; top = 9; + for(int a = 0; a < 2; a++){ + for(int b =0; b < 2; b++){ + addSlotToContainer(new Slot(inventoryRecipeEditor,i+2*a+b,left+b*59,top+a*24)); + } + } + i = 4; left = 9; top = 69; + for(int a = 0; a < 4; a++){ + for(int b =0; b < 3; b++){ + addSlotToContainer(new Slot(inventoryRecipeEditor,i+3*a+b,left+b*59,top+a*24)); + } + } + this.player = player; + } + + @Override + public ItemStack slotClick(int slotId, int clickedButton, int mode, EntityPlayer playerIn) { + if(slotId<0 || slotId>=16){ + return null; + } + Slot slot = getSlot(slotId); + if(slot != null){ + ItemStack itemStack = slot.inventory.getStackInSlot(slot.getSlotIndex()); + if(itemStack != null){ + if(clickedButton == 0){ + ItemStack itemStackBuffer = itemStack.copy(); + itemStackBuffer.stackSize += 1; + slot.inventory.setInventorySlotContents(slot.getSlotIndex(), itemStackBuffer); + slot.onSlotChanged(); + }else if(clickedButton == 1){ + if(itemStack.stackSize == 1){ + slot.inventory.setInventorySlotContents(slot.getSlotIndex(),null); + slot.onSlotChanged(); + }else { + ItemStack itemStackBuffer = itemStack.copy(); + itemStackBuffer.stackSize -= 1; + slot.inventory.setInventorySlotContents(slot.getSlotIndex(), itemStackBuffer); + slot.onSlotChanged(); + } + } + } + } + return null; + } + + @Override + public boolean canInteractWith(EntityPlayer playerIn) { + return true; + } + + @Override + public ItemStack transferStackInSlot(EntityPlayer playerIn, int index) { + return null; + } + + public EntityPlayer getPlayer() { + return player; + } + + public Recipe buildRecipe(){ + ItemStack[] output = new ItemStack[4]; + boolean flag1 = false, flag2 = false; + for(int i=0; i<4; i++){ + output[i] = getSlot(i).getStack(); + if(output[i] != null){ + flag1 = true; + } + } + ItemStack[] input = new ItemStack[12]; + for(int i=4; i<16; i++){ + input[i-4] = getSlot(i).getStack(); + if(input[i-4] != null){ + flag2 = true; + } + } + return flag1 && flag2 ? new Recipe(output, input) : null; + } +} diff --git a/src/main/java/pers/towdium/tudicraft/gui/recipeEditor/GuiRecipeEditor.java b/src/main/java/pers/towdium/tudicraft/gui/recipeEditor/GuiRecipeEditor.java new file mode 100644 index 0000000..1638eba --- /dev/null +++ b/src/main/java/pers/towdium/tudicraft/gui/recipeEditor/GuiRecipeEditor.java @@ -0,0 +1,116 @@ +package pers.towdium.tudicraft.gui.recipeEditor; + +import net.minecraft.client.gui.GuiButton; +import net.minecraft.client.gui.GuiScreen; +import net.minecraft.client.gui.inventory.GuiContainer; +import net.minecraft.client.renderer.GlStateManager; +import net.minecraft.inventory.Slot; +import net.minecraft.item.ItemStack; +import net.minecraft.util.ResourceLocation; +import net.minecraft.util.StatCollector; +import pers.towdium.tudicraft.Tudicraft; +import pers.towdium.tudicraft.core.Recipe; + +import java.io.IOException; +import java.sql.Time; + +/** + * @author Towdium + */ +public class GuiRecipeEditor extends GuiContainer{ + GuiScreen parent; + protected int activeSlot = -1; + + public GuiRecipeEditor(ContainerRecipeEditor containerRecipeEditor, GuiScreen parent){ + super(containerRecipeEditor); + this.parent = parent; + } + + public GuiRecipeEditor(ContainerRecipeEditor containerRecipeEditor){ + super(containerRecipeEditor); + } + + @Override + public void initGui() { + super.initGui(); + int i, left, top; + i = 0; left = 31 + guiLeft; top = 7 + guiTop; + for(int a = 0; a < 2; a++){ + for(int b =0; b < 2; b++){ + buttonList.add(new GuiButton(i+2*a+b, left+b*59, top+a*24, 20, 20, "X")); + } + } + i = 4; left = 31 + guiLeft; top = 67 + guiTop; + for(int a = 0; a < 4; a++){ + for(int b =0; b < 3; b++){ + buttonList.add(new GuiButton(i+3*a+b, left+b*59, top+a*24, 20, 20, "X")); + } + } + buttonList.add(new GuiButton(16, guiLeft+125, guiTop+7, 44, 20, StatCollector.translateToLocal("gui.recipeEditor.save"))); + buttonList.add(new GuiButton(17, guiLeft+125, guiTop+31, 44, 20, StatCollector.translateToLocal("gui.recipeEditor.clear"))); + } + + @Override + protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) { + GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F); + this.mc.getTextureManager().bindTexture(new ResourceLocation(Tudicraft.Reference.MODID,"textures/gui/guiRecipeEditor.png")); + this.drawTexturedModalRect(this.guiLeft, this.guiTop, 0, 0, this.xSize, this.ySize); + if(activeSlot != -1){ + Slot slot = inventorySlots.getSlot(activeSlot); + this.drawTexturedModalRect(this.guiLeft+slot.xDisplayPosition-2, this.guiTop+slot.yDisplayPosition-2, 176, 0, 20, 20); + } + } + + @Override + protected void actionPerformed(GuiButton button) throws IOException { + int buttonId = button.id; + if(buttonId <= 15){ + Slot slot = inventorySlots.getSlot(buttonId); + slot.inventory.setInventorySlotContents(slot.getSlotIndex(), null); + }else if(buttonId == 16) { + Tudicraft.proxy.getPlayerHandler().addRecipe(((ContainerRecipeEditor)inventorySlots).buildRecipe()); + mc.displayGuiScreen(parent); + }else if(buttonId == 17) { + for(Slot slot : inventorySlots.inventorySlots){ + slot.inventory.setInventorySlotContents(slot.getSlotIndex(), null); + } + } + } + + @Override + protected void mouseReleased(int mouseX, int mouseY, int state) {} + + @Override + protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException { + super.mouseClicked(mouseX, mouseY, mouseButton); + Slot slot = getSlotUnderMouse(); + if(slot != null && mouseButton == 0 && slot.getStack() == null){ + activeSlot = slot.getSlotIndex(); + ((ContainerRecipeEditor)inventorySlots).getPlayer().playSound("random.click", 1f, 1f ); + } + } + + @Override + protected void handleMouseClick(Slot slotIn, int slotId, int clickedButton, int clickType) { + if (slotIn != null) + { + slotId = slotIn.slotNumber; + } + mc.thePlayer.openContainer.slotClick(slotId, clickedButton, clickType, mc.thePlayer); + } + + @Override + protected void keyTyped(char typedChar, int keyCode) throws IOException { + if (keyCode == 1) { + mc.displayGuiScreen(parent); + } + } + + public int getActiveSlot() { + return activeSlot; + } + + public void setActiveSlot(int activeSlot) { + this.activeSlot = activeSlot; + } +} diff --git a/src/main/java/pers/towdium/tudicraft/gui/recipeEditor/InventoryRecipeEditor.java b/src/main/java/pers/towdium/tudicraft/gui/recipeEditor/InventoryRecipeEditor.java new file mode 100644 index 0000000..f97b468 --- /dev/null +++ b/src/main/java/pers/towdium/tudicraft/gui/recipeEditor/InventoryRecipeEditor.java @@ -0,0 +1,17 @@ +package pers.towdium.tudicraft.gui.recipeEditor; + +import net.minecraft.inventory.InventoryBasic; +import net.minecraft.item.ItemStack; +import pers.towdium.tudicraft.core.Recipe; + +/** + * @author Towdium + */ +public class InventoryRecipeEditor extends InventoryBasic { + public InventoryRecipeEditor() { + super("Recipe editor", false, 16); + } + + + +} diff --git a/src/main/java/pers/towdium/tudicraft/item/ItemCalculator.java b/src/main/java/pers/towdium/tudicraft/item/ItemCalculator.java new file mode 100644 index 0000000..cdc7299 --- /dev/null +++ b/src/main/java/pers/towdium/tudicraft/item/ItemCalculator.java @@ -0,0 +1,32 @@ +package pers.towdium.tudicraft.item; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.world.World; +import pers.towdium.tudicraft.Tudicraft; +import pers.towdium.tudicraft.gui.GuiHandler; + +import java.util.List; + +/** + * @author Towdium + */ +public class ItemCalculator extends Item { + + public ItemCalculator() { + setMaxStackSize(1); + } + + @Override + public ItemStack onItemRightClick(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn) { + playerIn.openGui(Tudicraft.instance, GuiHandler.GuiId.CALCULATOR, worldIn, 0, 0, 0); + return itemStackIn; + } + + @Override + public void addInformation(ItemStack stack, EntityPlayer playerIn, List tooltip, boolean advanced) { + //super.addInformation(stack, playerIn, tooltip, advanced); + tooltip.add("sdji"); + } +} diff --git a/src/main/java/pers/towdium/tudicraft/network/IPlayerHandler.java b/src/main/java/pers/towdium/tudicraft/network/IPlayerHandler.java new file mode 100644 index 0000000..c1c683a --- /dev/null +++ b/src/main/java/pers/towdium/tudicraft/network/IPlayerHandler.java @@ -0,0 +1,33 @@ +package pers.towdium.tudicraft.network; + +import com.google.common.collect.ImmutableList; +import net.minecraft.item.ItemStack; +import pers.towdium.tudicraft.core.Recipe; + +import java.util.List; + +/** + * @author Towdium + */ +public interface IPlayerHandler { + + boolean getHasRecipeOf(ItemStack itemStack); + + Recipe getRecipeOf(ItemStack itemStack); + + ImmutableList getAllRecipeOf(ItemStack itemStack); + + int getRecipeIndexOf(ItemStack itemStack); + + ImmutableList getAllRecipeIndexOf(ItemStack itemStack); + + void addRecipe(Recipe recipe); + + void removeRecipe(int index); + + void setRecipe(Recipe recipe, int index); + + Recipe getRecipe(int index); + + ImmutableList getCostOf(ItemStack itemStack, int amount); +} diff --git a/src/main/java/pers/towdium/tudicraft/network/IProxy.java b/src/main/java/pers/towdium/tudicraft/network/IProxy.java new file mode 100644 index 0000000..fa2838d --- /dev/null +++ b/src/main/java/pers/towdium/tudicraft/network/IProxy.java @@ -0,0 +1,15 @@ +package pers.towdium.tudicraft.network; + +import net.minecraft.item.ItemStack; +import pers.towdium.tudicraft.core.Recipe; + +/** + * @author Towdium + */ + + +public interface IProxy { + IPlayerHandler getPlayerHandler(); + + +} diff --git a/src/main/java/pers/towdium/tudicraft/network/PlayerHandlerClient.java b/src/main/java/pers/towdium/tudicraft/network/PlayerHandlerClient.java new file mode 100644 index 0000000..9745144 --- /dev/null +++ b/src/main/java/pers/towdium/tudicraft/network/PlayerHandlerClient.java @@ -0,0 +1,127 @@ +package pers.towdium.tudicraft.network; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import net.minecraft.item.ItemStack; +import pers.towdium.tudicraft.core.Recipe; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author Towdium + */ +public class PlayerHandlerClient implements IPlayerHandler { + List recipes = new ArrayList<>(); + + @Override + public void addRecipe(Recipe recipe) { + for (Recipe oneRecipe : recipes){ + if(oneRecipe.equals(recipe)){ + return; + } + } + if(recipe != null){ + recipes.add(recipe); + } + } + + @Override + public boolean getHasRecipeOf(ItemStack itemStack) { + for (Recipe recipe : recipes){ + if(recipe.getHasOutput(itemStack)){ + return true; + } + } + return false; + } + + @Override + public Recipe getRecipeOf(ItemStack itemStack) { + int i = getRecipeIndexOf(itemStack); + if(i == -1){ + return null; + }else { + return recipes.get(i); + } + } + + @Override + public ImmutableList getAllRecipeOf(ItemStack itemStack) { + ImmutableList.Builder recipeBuilder = new ImmutableList.Builder<>(); + for(Integer index : getAllRecipeIndexOf(itemStack)){ + recipeBuilder.add(recipes.get(index)); + } + return recipeBuilder.build(); + } + + @Override + public int getRecipeIndexOf(ItemStack itemStack) { + int[] result = new int[]{-1, -1, -1, -1}; + for(int[] record : getAllUnsortedRecipeIndexOf(itemStack)){ + result[record[1]] = record[0]; + } + for (int index : result) { + if (index != -1) { + return index; + } + } + return -1; + } + + @Override + public ImmutableList getAllRecipeIndexOf(ItemStack itemStack) { + ImmutableList.Builder> builder1 = new ImmutableList.Builder<>(); + for(int i=0; i<4; i++){ + builder1.add(new ImmutableList.Builder()); + } + ImmutableList> resultBuffer = builder1.build(); + for(int[] result : getAllUnsortedRecipeIndexOf(itemStack)){ + resultBuffer.get(result[1]).add(result[0]); + } + ImmutableList.Builder builder2 = new ImmutableList.Builder<>(); + for(ImmutableList.Builder priority : resultBuffer){ + builder2.addAll(priority.build().reverse()); + } + return builder2.build(); + } + + @Override + public void removeRecipe(int index) { + recipes.remove(index); + } + + @Override + public void setRecipe(Recipe recipe, int index) { + recipes.set(index, recipe); + } + + @Override + public Recipe getRecipe(int index) { + return recipes.get(index); + } + + //TODO + @Override + public ImmutableList getCostOf(ItemStack itemStack, int amount) { + ImmutableList.Builder builder = new ImmutableList.Builder<>(); + //recipes.get(getRecipeIndexOf(itemStack)) + + + return null; + } + + ImmutableList getAllUnsortedRecipeIndexOf(ItemStack itemStack){ + ImmutableList.Builder builder = new ImmutableList.Builder<>(); + int size = recipes.size(); + int buffer; + for(int i = 0; i getAllRecipeOf(ItemStack itemStack) { + return null; + } + + @Override + public int getRecipeIndexOf(ItemStack itemStack) { + return 0; + } + + @Override + public ImmutableList getAllRecipeIndexOf(ItemStack itemStack) { + return null; + } + + @Override + public void removeRecipe(int index) { + + } + + @Override + public void setRecipe(Recipe recipe, int index) { + + } + + @Override + public Recipe getRecipe(int index) { + return null; + } + + @Override + public ImmutableList getCostOf(ItemStack itemStack, int amount) { + return null; + } +} diff --git a/src/main/java/pers/towdium/tudicraft/network/ProxyClient.java b/src/main/java/pers/towdium/tudicraft/network/ProxyClient.java new file mode 100644 index 0000000..ebe44a5 --- /dev/null +++ b/src/main/java/pers/towdium/tudicraft/network/ProxyClient.java @@ -0,0 +1,13 @@ +package pers.towdium.tudicraft.network; + +/** + * @author Towdium + */ +public class ProxyClient implements IProxy { + PlayerHandlerClient playerHandler = new PlayerHandlerClient(); + + @Override + public IPlayerHandler getPlayerHandler() { + return playerHandler; + } +} diff --git a/src/main/java/pers/towdium/tudicraft/network/ProxyServer.java b/src/main/java/pers/towdium/tudicraft/network/ProxyServer.java new file mode 100644 index 0000000..3f80dcf --- /dev/null +++ b/src/main/java/pers/towdium/tudicraft/network/ProxyServer.java @@ -0,0 +1,13 @@ +package pers.towdium.tudicraft.network; + +/** + * @author Towdium + */ +public class ProxyServer implements IProxy{ + PlayerHandlerServer playerHandler = new PlayerHandlerServer(); + + @Override + public IPlayerHandler getPlayerHandler() { + return playerHandler; + } +} diff --git a/src/main/java/pers/towdium/tudicraft/network/packages/PackageCalculatorUpdate.java b/src/main/java/pers/towdium/tudicraft/network/packages/PackageCalculatorUpdate.java new file mode 100644 index 0000000..9bd1608 --- /dev/null +++ b/src/main/java/pers/towdium/tudicraft/network/packages/PackageCalculatorUpdate.java @@ -0,0 +1,41 @@ +package pers.towdium.tudicraft.network.packages; + +import io.netty.buffer.ByteBuf; +import net.minecraft.item.ItemStack; +import net.minecraftforge.fml.common.network.ByteBufUtils; +import net.minecraftforge.fml.common.network.simpleimpl.IMessage; +import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler; +import net.minecraftforge.fml.common.network.simpleimpl.MessageContext; +import pers.towdium.tudicraft.item.ItemCalculator; + +/** + * @author Towdium + */ +public class PackageCalculatorUpdate implements IMessage, IMessageHandler { + ItemStack itemStack; + + public PackageCalculatorUpdate(){} + + public PackageCalculatorUpdate(ItemStack itemStack) { + this.itemStack = itemStack; + } + + @Override + public void fromBytes(ByteBuf buf) { + itemStack = ByteBufUtils.readItemStack(buf); + } + + @Override + public void toBytes(ByteBuf buf) { + ByteBufUtils.writeItemStack(buf, itemStack); + } + + @Override + public IMessage onMessage(PackageCalculatorUpdate message, MessageContext ctx) { + if(ctx.getServerHandler().playerEntity.getHeldItem().getItem() instanceof ItemCalculator){ + ctx.getServerHandler().playerEntity.inventory.setInventorySlotContents( + ctx.getServerHandler().playerEntity.inventory.currentItem, message.itemStack); + } + return null; + } +} diff --git a/src/main/java/pers/towdium/tudicraft/network/packages/PackageRecipeUpdate.java b/src/main/java/pers/towdium/tudicraft/network/packages/PackageRecipeUpdate.java new file mode 100644 index 0000000..5dbdbaf --- /dev/null +++ b/src/main/java/pers/towdium/tudicraft/network/packages/PackageRecipeUpdate.java @@ -0,0 +1,41 @@ +package pers.towdium.tudicraft.network.packages; + +import io.netty.buffer.ByteBuf; +import net.minecraftforge.fml.common.network.simpleimpl.IMessage; +import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler; +import net.minecraftforge.fml.common.network.simpleimpl.MessageContext; +import pers.towdium.tudicraft.core.Recipe; + +/** + * @author Towdium + */ +public class PackageRecipeUpdate implements IMessage, IMessageHandler { + Recipe recipe; + + public PackageRecipeUpdate(){} + + + public PackageRecipeUpdate(Recipe recipe){ + this.recipe = recipe; + } + + @Override + public void toBytes(ByteBuf buffer) { + + } + + @Override + public void fromBytes(ByteBuf buffer) { + + } + + + + + @Override + public IMessage onMessage(PackageRecipeUpdate message, MessageContext context) { + + return null; + } + +} diff --git a/src/main/java/pers/towdium/tudicraft/network/packages/PackageSlotUpdate.java b/src/main/java/pers/towdium/tudicraft/network/packages/PackageSlotUpdate.java new file mode 100644 index 0000000..060cf16 --- /dev/null +++ b/src/main/java/pers/towdium/tudicraft/network/packages/PackageSlotUpdate.java @@ -0,0 +1,60 @@ +package pers.towdium.tudicraft.network.packages; + +import io.netty.buffer.ByteBuf; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import net.minecraftforge.fml.common.network.ByteBufUtils; +import net.minecraftforge.fml.common.network.simpleimpl.IMessage; +import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler; +import net.minecraftforge.fml.common.network.simpleimpl.MessageContext; + +/** + * @author Towdium + */ +public class PackageSlotUpdate implements IMessage, IMessageHandler { + int slot; + int playerId; + ItemStack itemStack=null; + + public PackageSlotUpdate(){} + + + public PackageSlotUpdate(EntityPlayer player, int index){ + playerId = player.getEntityId(); + slot = index; + this.itemStack = player.inventory.getStackInSlot(index); + } + + + + @Override + public void toBytes(ByteBuf buffer) { + buffer.writeByte(slot); + buffer.writeInt(playerId); + ByteBufUtils.writeItemStack(buffer, itemStack); + } + + @Override + public void fromBytes(ByteBuf buffer) { + slot = buffer.readByte(); + playerId = buffer.readInt(); + itemStack = ByteBufUtils.readItemStack(buffer); + } + + + + + @Override + public IMessage onMessage(PackageSlotUpdate message, MessageContext context) { + context.getServerHandler().playerEntity.inventory.setInventorySlotContents(0, message.itemStack); +// Entity player = FMLServerHandler.instance().getServer().getEntityWorld().getEntityByID(playerId); +// System.out.println("get entity"); +// +// if(player instanceof EntityPlayerMP){ +// System.out.println("get entityMP!"); +// ((EntityPlayerMP) player).getInventory()[0] = new ItemStack(new ItemCalculator(),10); +// } + return null; + } + +} diff --git a/src/main/java/pers/towdium/tudicraft/plugin/JEIPlugin.java b/src/main/java/pers/towdium/tudicraft/plugin/JEIPlugin.java new file mode 100644 index 0000000..ea5005b --- /dev/null +++ b/src/main/java/pers/towdium/tudicraft/plugin/JEIPlugin.java @@ -0,0 +1,42 @@ +package pers.towdium.tudicraft.plugin; + +import mezz.jei.JeiRuntime; +import mezz.jei.api.*; +import pers.towdium.tudicraft.Tudicraft; + +import javax.swing.plaf.TableUI; + +/** + * @author Towdium + */ +@mezz.jei.api.JEIPlugin +public class JEIPlugin implements IModPlugin{ + public static IJeiRuntime runtime; + + + @Override + public void onJeiHelpersAvailable(IJeiHelpers jeiHelpers) { + + } + + @Override + public void onItemRegistryAvailable(IItemRegistry itemRegistry) { + + } + + @Override + public void register(IModRegistry registry) { + + } + + @Override + public void onRecipeRegistryAvailable(IRecipeRegistry recipeRegistry) { + + } + + @Override + public void onRuntimeAvailable(IJeiRuntime jeiRuntime) { + Tudicraft.log.info("载入了!"); + runtime = jeiRuntime; + } +} diff --git a/src/main/resources/assets/tudicraft/lang/en_US.lang b/src/main/resources/assets/tudicraft/lang/en_US.lang new file mode 100644 index 0000000..6413990 --- /dev/null +++ b/src/main/resources/assets/tudicraft/lang/en_US.lang @@ -0,0 +1,10 @@ +gui.calculator.calculate=Calculate! +gui.calculator.edit=Edit +gui.calculator.add=Add +gui.calculator.editTooltip=Edit records of recipes +gui.calculator.addTooltip=Add preferred recipe +gui.itemPicker.confirm=Confirm +gui.itemPicker.search=Search +gui.recipeEditor.save=Save +gui.recipeEditor.clear=Clear +item.itemCalculator.name=Calculator diff --git a/src/main/resources/assets/tudicraft/models/item/itemCalculator.json b/src/main/resources/assets/tudicraft/models/item/itemCalculator.json new file mode 100644 index 0000000..d1726f2 --- /dev/null +++ b/src/main/resources/assets/tudicraft/models/item/itemCalculator.json @@ -0,0 +1,18 @@ +{ + "parent": "builtin/generated", + "textures": { + "layer0": "tudicraft:items/itemCalculator" + }, + "display": { + "thirdperson": { + "rotation": [ -90, 0, 0 ], + "translation": [ 0, 1, -3 ], + "scale": [ 0.55, 0.55, 0.55 ] + }, + "firstperson": { + "rotation": [ 0, -135, 25 ], + "translation": [ 0, 4, 2 ], + "scale": [ 1.7, 1.7, 1.7 ] + } + } +} diff --git a/src/main/resources/assets/tudicraft/textures/gui/containerCalculator.png b/src/main/resources/assets/tudicraft/textures/gui/containerCalculator.png new file mode 100644 index 0000000..127f322 Binary files /dev/null and b/src/main/resources/assets/tudicraft/textures/gui/containerCalculator.png differ diff --git a/src/main/resources/assets/tudicraft/textures/gui/guiCalculator.png b/src/main/resources/assets/tudicraft/textures/gui/guiCalculator.png new file mode 100644 index 0000000..5d5b827 Binary files /dev/null and b/src/main/resources/assets/tudicraft/textures/gui/guiCalculator.png differ diff --git a/src/main/resources/assets/tudicraft/textures/gui/guiItemPicker.png b/src/main/resources/assets/tudicraft/textures/gui/guiItemPicker.png new file mode 100644 index 0000000..815359d Binary files /dev/null and b/src/main/resources/assets/tudicraft/textures/gui/guiItemPicker.png differ diff --git a/src/main/resources/assets/tudicraft/textures/gui/guiRecipeEditor.png b/src/main/resources/assets/tudicraft/textures/gui/guiRecipeEditor.png new file mode 100644 index 0000000..36f9792 Binary files /dev/null and b/src/main/resources/assets/tudicraft/textures/gui/guiRecipeEditor.png differ diff --git a/src/main/resources/assets/tudicraft/textures/gui/slotActive.png b/src/main/resources/assets/tudicraft/textures/gui/slotActive.png new file mode 100644 index 0000000..57470bd Binary files /dev/null and b/src/main/resources/assets/tudicraft/textures/gui/slotActive.png differ diff --git a/src/main/resources/assets/tudicraft/textures/gui/slotInput.png b/src/main/resources/assets/tudicraft/textures/gui/slotInput.png new file mode 100644 index 0000000..98441e6 Binary files /dev/null and b/src/main/resources/assets/tudicraft/textures/gui/slotInput.png differ diff --git a/src/main/resources/assets/tudicraft/textures/gui/slotOutput.png b/src/main/resources/assets/tudicraft/textures/gui/slotOutput.png new file mode 100644 index 0000000..e324936 Binary files /dev/null and b/src/main/resources/assets/tudicraft/textures/gui/slotOutput.png differ diff --git a/src/main/resources/assets/tudicraft/textures/gui/slots.png b/src/main/resources/assets/tudicraft/textures/gui/slots.png new file mode 100644 index 0000000..37b5cd0 Binary files /dev/null and b/src/main/resources/assets/tudicraft/textures/gui/slots.png differ diff --git a/src/main/resources/assets/tudicraft/textures/items/itemCalculator.png b/src/main/resources/assets/tudicraft/textures/items/itemCalculator.png new file mode 100644 index 0000000..3e2ee59 Binary files /dev/null and b/src/main/resources/assets/tudicraft/textures/items/itemCalculator.png differ diff --git a/src/main/resources/mcmod.info b/src/main/resources/mcmod.info new file mode 100644 index 0000000..f480667 --- /dev/null +++ b/src/main/resources/mcmod.info @@ -0,0 +1,16 @@ +[ +{ + "modid": "examplemod", + "name": "Example Mod", + "description": "Example placeholder mod.", + "version": "${version}", + "mcversion": "${mcversion}", + "url": "", + "updateUrl": "", + "authorList": ["ExampleDude"], + "credits": "The Forge and FML guys, for making this example", + "logoFile": "", + "screenshots": [], + "dependencies": [] +} +]