diff --git a/README.md b/README.md index d207c86..6b303df 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ # XPBox This is a Minecraft 1.10.2 MOD introducing a XP container, inspired by ExPContainer made by clocka [[OP Link]](http://forum.minecraftuser.jp/viewtopic.php?t=15105). This MOD adds a block that stores players' xp and persists, be it broken or the player is dead (acting like ender chests). -1.12 version is on branch `mc-1.12`. +1.12/1.12.2 version is at another branch. \ No newline at end of file diff --git a/build.gradle b/build.gradle index bec470f..d55312b 100644 --- a/build.gradle +++ b/build.gradle @@ -4,32 +4,32 @@ buildscript { maven { url = "http://files.minecraftforge.net/maven" } } dependencies { - classpath 'net.minecraftforge.gradle:ForgeGradle:2.2-SNAPSHOT' + classpath 'net.minecraftforge.gradle:ForgeGradle:2.3-SNAPSHOT' } } apply plugin: 'net.minecraftforge.gradle.forge' -//Only edit below this line, the above code adds and enables the nessasary things for Forge to be setup. +//Only edit below this line, the above code adds and enables the necessary things for Forge to be setup. version = "1.0" -group= "plodsoft.xpbox" // http://maven.apache.org/guides/mini/guide-naming-conventions.html +group = "plodsoft.xpbox" // http://maven.apache.org/guides/mini/guide-naming-conventions.html archivesBaseName = "XPBox" -sourceCompatibility = targetCompatibility = "1.8" // Need this here so eclipse task generates correctly. +sourceCompatibility = targetCompatibility = '1.8' // Need this here so eclipse task generates correctly. compileJava { - sourceCompatibility = targetCompatibility = "1.8" + sourceCompatibility = targetCompatibility = '1.8' } minecraft { - version = "1.10.2-12.18.3.2185" + version = "1.12.2-14.23.2.2611" 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. + // Use non-default mappings at your own risk. they may not always work. // simply re-run your setup task after changing the mappings to update your workspace. - mappings = "snapshot_20161111" + mappings = "snapshot_20171003" // makeObfSourceJar = false // an Srg named sources jar is made by default. uncomment this to disable. } @@ -57,8 +57,7 @@ dependencies { } -processResources -{ +processResources { // this will ensure that this task is redone when the versions change. inputs.property "version", project.version inputs.property "mcversion", project.minecraft.version @@ -71,7 +70,7 @@ processResources expand 'version':project.version, 'mcversion':project.minecraft.version } - // copy everything else, thats not the mcmod.info + // copy everything else except the mcmod.info from(sourceSets.main.resources.srcDirs) { exclude 'mcmod.info' } diff --git a/gradlew b/gradlew new file mode 100755 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/xpbox/BlockXPBox.java b/src/main/java/xpbox/BlockXPBox.java index acdf15a..786f16d 100644 --- a/src/main/java/xpbox/BlockXPBox.java +++ b/src/main/java/xpbox/BlockXPBox.java @@ -6,124 +6,105 @@ import net.minecraft.creativetab.CreativeTabs; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; -import net.minecraft.init.Blocks; -import net.minecraft.init.Items; -import net.minecraft.item.ItemStack; import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumHand; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; -import net.minecraftforge.fml.common.registry.GameRegistry; -import javax.annotation.Nullable; - -import static xpbox.XPBox.blockXPBox; public class BlockXPBox extends Block { - public static final String NAME = "xpbox"; - - public BlockXPBox() { - super(Material.ROCK); - setRegistryName(NAME); - setUnlocalizedName(NAME); - setHardness(.5f); - setCreativeTab(CreativeTabs.DECORATIONS); - } + public static final String NAME = "xpbox"; - public static void registerRecipe() { - GameRegistry.addShapedRecipe(new ItemStack(blockXPBox), - "OGO", - "ICI", - "ERE", - 'O', Blocks.OBSIDIAN, - 'E', Items.ENDER_PEARL, - 'I', Items.IRON_INGOT, - 'R', Items.REDSTONE, - 'C', Blocks.CHEST, - 'G', Blocks.GLASS_PANE); - } + public BlockXPBox() { + super(Material.ROCK); + setRegistryName(NAME); + setUnlocalizedName(NAME); + setHardness(.5f); + setCreativeTab(CreativeTabs.DECORATIONS); + } - @Override - public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, @Nullable ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ) { - if (!worldIn.isRemote) { - sendXPMessage(playerIn); - } - return true; - } + @Override + public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) { + if (!worldIn.isRemote) { + sendXPMessage(playerIn); + } + return true; + } - private static void sendXPMessage(EntityPlayer player) { - PacketHandler.wrapper.sendTo( - new MessageXP(player.getCapability(XPBox.XP_CAPABILITY, null).value, - getTotalXP(player)), - (EntityPlayerMP) player); - } + private static void sendXPMessage(EntityPlayer player) { + PacketHandler.wrapper.sendTo( + new MessageXP(player.getCapability(XPBox.XP_CAPABILITY, null).value, + getTotalXP(player)), + (EntityPlayerMP) player); + } - public static int getTotalXP(EntityPlayer player) { - int xp = 0; - for (int lv = 0; lv < player.experienceLevel; ++lv) { - xp += xpBarCap(lv); - } - return xp + Math.round(player.experience * player.xpBarCap()); - } + public static int getTotalXP(EntityPlayer player) { + int xp = 0; + for (int lv = 0; lv < player.experienceLevel; ++lv) { + xp += xpBarCap(lv); + } + return xp + Math.round(player.experience * player.xpBarCap()); + } - private static int xpBarCap(int lv) { - return lv >= 30 ? 112 + (lv - 30) * 9 : (lv >= 15 ? 37 + (lv - 15) * 5 : 7 + lv * 2); - } + // copy from EntityPlayer.xpBarCap + private static int xpBarCap(int lv) { + return lv >= 30 ? 112 + (lv - 30) * 9 : (lv >= 15 ? 37 + (lv - 15) * 5 : 7 + lv * 2); + } - public static void updateXP(EntityPlayer player, int op, int xp) { - int exp; - XPCapability cap = player.getCapability(XPBox.XP_CAPABILITY, null); + public static void updateXP(EntityPlayer player, int op, int xp) { + int exp; + XPCapability cap = player.getCapability(XPBox.XP_CAPABILITY, null); - switch (op) { - case MessageOp.WITHDRAW: - if (xp > cap.value) - xp = cap.value; - if (0 == xp) - break; - player.addExperience(xp); - cap.value -= xp; - break; - case MessageOp.DEPOSIT: - exp = getTotalXP(player); - if (xp > exp) + switch (op) { + case MessageOp.WITHDRAW: + if (xp > cap.value) + xp = cap.value; + if (0 == xp) + break; + player.addExperience(xp); + cap.value -= xp; break; - if (xp + cap.value > GuiXPBox.MAX) - xp = GuiXPBox.MAX - cap.value; - if (0 == xp) + case MessageOp.DEPOSIT: + exp = getTotalXP(player); + if (xp > exp) + break; + if (xp + cap.value > GuiXPBox.MAX) + xp = GuiXPBox.MAX - cap.value; + if (0 == xp) + break; + player.experience = 0; + player.experienceLevel = 0; + player.experienceTotal = 0; + player.addExperience(exp - xp); + cap.value += xp; break; - player.experience = 0; - player.experienceLevel = 0; - player.experienceTotal = 0; - player.addExperience(exp - xp); - cap.value += xp; - break; - case MessageOp.SETLEVEL: - exp = getTotalXP(player); - if (exp + cap.value < exp) - exp = Integer.MAX_VALUE; - else - exp += cap.value; - if (0 == exp) - break; - player.experience = 0; - player.experienceLevel = 0; - player.experienceTotal = 0; - for (int lv = 0; lv < xp; ++lv) { - int t = xpBarCap(lv); - if (exp >= t) { - exp -= t; - player.addExperience(t); - } else { - player.addExperience(exp); - exp = 0; - break; + case MessageOp.SETLEVEL: + exp = getTotalXP(player); + if (exp + cap.value < exp) + exp = Integer.MAX_VALUE; + else + exp += cap.value; + if (0 == exp) + break; + player.experience = 0; + player.experienceLevel = 0; + player.experienceTotal = 0; + for (int lv = 0; lv < xp; ++lv) { + int t = xpBarCap(lv); + if (exp >= t) { + exp -= t; + player.addExperience(t); + } else { + player.addExperience(exp); + exp = 0; + break; + } } - } - if (exp > GuiXPBox.MAX) - exp = GuiXPBox.MAX; - cap.value = exp; - break; - } - sendXPMessage(player); - } + if (exp > GuiXPBox.MAX) + exp = GuiXPBox.MAX; + cap.value = exp; + break; + } + sendXPMessage(player); + } } diff --git a/src/main/java/xpbox/ClientProxy.java b/src/main/java/xpbox/ClientProxy.java index 29db7e4..6436a7a 100644 --- a/src/main/java/xpbox/ClientProxy.java +++ b/src/main/java/xpbox/ClientProxy.java @@ -5,9 +5,9 @@ import net.minecraftforge.client.model.ModelLoader; public class ClientProxy extends CommonProxy { - @Override - public void registerItemRenderer(Item item) { - ModelLoader.setCustomModelResourceLocation(item, 0, - new ModelResourceLocation(item.getRegistryName(), "inventory")); - } + @Override + public void registerItemRenderer(Item item) { + ModelLoader.setCustomModelResourceLocation(item, 0, + new ModelResourceLocation(item.getRegistryName(), "inventory")); + } } diff --git a/src/main/java/xpbox/CommonProxy.java b/src/main/java/xpbox/CommonProxy.java index 1089da3..3bc4a13 100644 --- a/src/main/java/xpbox/CommonProxy.java +++ b/src/main/java/xpbox/CommonProxy.java @@ -3,5 +3,6 @@ import net.minecraft.item.Item; public class CommonProxy { - public void registerItemRenderer(Item item) {} + public void registerItemRenderer(Item item) { + } } diff --git a/src/main/java/xpbox/EventHandler.java b/src/main/java/xpbox/EventHandler.java new file mode 100644 index 0000000..28b3dfd --- /dev/null +++ b/src/main/java/xpbox/EventHandler.java @@ -0,0 +1,47 @@ +package xpbox; + +import net.minecraft.block.Block; +import net.minecraft.entity.Entity; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.Item; +import net.minecraft.item.ItemBlock; +import net.minecraft.util.ResourceLocation; +import net.minecraftforge.client.event.ModelRegistryEvent; +import net.minecraftforge.event.AttachCapabilitiesEvent; +import net.minecraftforge.event.RegistryEvent; +import net.minecraftforge.event.entity.player.PlayerEvent; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; + +@Mod.EventBusSubscriber +public class EventHandler { + @SubscribeEvent + public static void attachCapability(AttachCapabilitiesEvent e) { + if (e.getObject() instanceof EntityPlayer + && !e.getObject().hasCapability(XPBox.XP_CAPABILITY, null)) + e.addCapability(new ResourceLocation(XPBox.MODID, "xpCapability"), + new XPCapability.Provider()); + } + + @SubscribeEvent + public static void onPlayerClone(PlayerEvent.Clone e) { + // may be returning from the End or respawning after death + e.getEntity().getCapability(XPBox.XP_CAPABILITY, null).value = + e.getOriginal().getCapability(XPBox.XP_CAPABILITY, null).value; + } + + @SubscribeEvent + public static void registerBlocks(RegistryEvent.Register event) { + event.getRegistry().register(XPBox.blockXPBox); + } + + @SubscribeEvent + public static void registerItems(RegistryEvent.Register event) { + event.getRegistry().register(new ItemBlock(XPBox.blockXPBox).setRegistryName(XPBox.blockXPBox.getRegistryName())); + } + + @SubscribeEvent + public static void registerModels(ModelRegistryEvent event) { + XPBox.proxy.registerItemRenderer(Item.getItemFromBlock(XPBox.blockXPBox)); + } +} diff --git a/src/main/java/xpbox/GuiXPBox.java b/src/main/java/xpbox/GuiXPBox.java index 9088bef..9168fa4 100644 --- a/src/main/java/xpbox/GuiXPBox.java +++ b/src/main/java/xpbox/GuiXPBox.java @@ -9,236 +9,245 @@ import java.io.IOException; public class GuiXPBox extends GuiScreen { - private static final ResourceLocation GUI_TEXTURE = - new ResourceLocation(XPBox.MODID, "textures/gui/xpbox.png"); - - private static final int[] TextureX = { 2, 13, 24, 35, 46, 57, 68, 79, 90, 101, 112 }; - - private static final int GUI_WIDTH = 176, GUI_HEIGHT = 166; - private static final int BUTTON_WIDTH = 20, BUTTON_HEIGHT = 20; - - private static final int DIGITS = 7; - public static final int MAX = 9999999; - - private static final int BTN_0 = 0, - BTN_1 = 1, - BTN_2 = 2, - BTN_3 = 3, - BTN_4 = 4, - BTN_5 = 5, - BTN_6 = 6, - BTN_7 = 7, - BTN_8 = 8, - BTN_9 = 9, - BTN_DEL = 10, - BTN_ALLDEP = 11, - BTN_ALLWD = 12, - BTN_DEP = 13, - BTN_WD = 14, - BTN_SETLVL = 15; - - public static GuiXPBox instance; - public int xp, playerXP; - private int inputXP; - private int[] xpDigits, playerXPDigits, inputXPDigits; - - public GuiXPBox(int xp, int playerXP) { - this.xp = xp; - this.playerXP = playerXP; - xpDigits = new int[DIGITS]; - playerXPDigits = new int[DIGITS]; - inputXPDigits = new int[DIGITS]; - - setInputXPDigits(); - - instance = this; - } - - public void setXPAndPlayerXP(int xp, int playerXP) { - this.xp = xp; - this.playerXP = playerXP; - updateScreen(); - } - - @Override - public void onGuiClosed() { - instance = null; - } - - private void setXPAndPlayerXPDigits() { - int i = DIGITS; - int t = xp; - - do { - xpDigits[--i] = t % 10; - } while ((t /= 10) != 0); - while (--i >= 0) - xpDigits[i] = 10; - - i = DIGITS; - t = playerXP; - if (t > MAX) - t = MAX; - do { - playerXPDigits[--i] = t % 10; - } while ((t /= 10) != 0); - while (--i >= 0) - playerXPDigits[i] = 10; - } - - private void setInputXPDigits() { - int i = DIGITS; - int t = inputXP; - - do { - inputXPDigits[--i] = t % 10; - } while ((t /= 10) != 0); - while (--i >= 0) - inputXPDigits[i] = 10; - } - - @Override - public void initGui() { - super.initGui(); - int x = width - GUI_WIDTH >> 1; - int y = height - GUI_HEIGHT >> 1; - - buttonList.add(new GuiButton(BTN_1, x + 11, y + 62, BUTTON_WIDTH, BUTTON_HEIGHT, I18n.translateToLocal("gui.button_1"))); - buttonList.add(new GuiButton(BTN_2, x + 11 + 25, y + 62, BUTTON_WIDTH, BUTTON_HEIGHT, I18n.translateToLocal("gui.button_2"))); - buttonList.add(new GuiButton(BTN_3, x + 11 + 50, y + 62, BUTTON_WIDTH, BUTTON_HEIGHT, I18n.translateToLocal("gui.button_3"))); - buttonList.add(new GuiButton(BTN_4, x + 11, y + 62 + 25, BUTTON_WIDTH, BUTTON_HEIGHT, I18n.translateToLocal("gui.button_4"))); - buttonList.add(new GuiButton(BTN_5, x + 11 + 25, y + 62 + 25, BUTTON_WIDTH, BUTTON_HEIGHT, I18n.translateToLocal("gui.button_5"))); - buttonList.add(new GuiButton(BTN_6, x + 11 + 50, y + 62 + 25, BUTTON_WIDTH, BUTTON_HEIGHT, I18n.translateToLocal("gui.button_6"))); - buttonList.add(new GuiButton(BTN_7, x + 11, y + 62 + 50, BUTTON_WIDTH, BUTTON_HEIGHT, I18n.translateToLocal("gui.button_7"))); - buttonList.add(new GuiButton(BTN_8, x + 11 + 25, y + 62 + 50, BUTTON_WIDTH, BUTTON_HEIGHT, I18n.translateToLocal("gui.button_8"))); - buttonList.add(new GuiButton(BTN_9, x + 11 + 50, y + 62 + 50, BUTTON_WIDTH, BUTTON_HEIGHT, I18n.translateToLocal("gui.button_9"))); - buttonList.add(new GuiButton(BTN_0, x + 11, y + 62 + 75, BUTTON_WIDTH, BUTTON_HEIGHT, I18n.translateToLocal("gui.button_0"))); - buttonList.add(new GuiButton(BTN_DEL, x + 11 + 25, y + 62 + 75, BUTTON_WIDTH * 2 + 5, BUTTON_HEIGHT, I18n.translateToLocal("gui.button_del"))); - buttonList.add(new GuiButton(BTN_ALLDEP, x + 11 + 80, y + 62, 75, BUTTON_HEIGHT, I18n.translateToLocal("gui.button_alldep"))); - buttonList.add(new GuiButton(BTN_ALLWD, x + 11 + 80, y + 62 + 25, 75, BUTTON_HEIGHT, I18n.translateToLocal("gui.button_allwd"))); - buttonList.add(new GuiButton(BTN_DEP, x + 11 + 80, y + 62 + 75, 35, BUTTON_HEIGHT, I18n.translateToLocal("gui.button_dep"))); - buttonList.add(new GuiButton(BTN_WD, x + 11 + 120, y + 62 + 75, 35, BUTTON_HEIGHT, I18n.translateToLocal("gui.button_wd"))); - buttonList.add(new GuiButton(BTN_SETLVL, x + 11 + 80, y + 62 + 50, 75, BUTTON_HEIGHT, I18n.translateToLocal("gui.button_setlv"))); - } - - private void addInput(int d) { - inputXP = inputXP * 10 + d; - if (inputXP > MAX) - inputXP = MAX; - setInputXPDigits(); - } - - @Override - protected void keyTyped(char typedChar, int code) throws IOException { - super.keyTyped(typedChar, code); - switch (typedChar) { - case 13: onButtonClicked(BTN_SETLVL); break; - case 8:case 127: onButtonClicked(BTN_DEL); break; - default: - if (typedChar >= '0' && typedChar <= '9') - addInput(typedChar - '0'); - } - } - - @Override - public void drawScreen(int mouseX, int mouseY, float partialTicks) { - GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0f); - mc.getTextureManager().bindTexture(GUI_TEXTURE); - int x = width - GUI_WIDTH >> 1; - int y = height - GUI_HEIGHT >> 1; - drawTexturedModalRect(x, y, 0, 0, GUI_WIDTH, GUI_HEIGHT); - - drawTexturedModalRect(x + 116, y + 14, 123, 175, 15, 12); - - drawTexturedModalRect(x + 116, y + 28, 123, 190, 15, 12); - - drawTexturedModalRect(x + 59 + 0, y + 42, TextureX[inputXPDigits[0]], 205, 8, 12); - drawTexturedModalRect(x + 59 + 8, y + 42, TextureX[inputXPDigits[1]], 205, 8, 12); - drawTexturedModalRect(x + 59 + 16, y + 42, TextureX[inputXPDigits[2]], 205, 8, 12); - drawTexturedModalRect(x + 59 + 24, y + 42, TextureX[inputXPDigits[3]], 205, 8, 12); - drawTexturedModalRect(x + 59 + 32, y + 42, TextureX[inputXPDigits[4]], 205, 8, 12); - drawTexturedModalRect(x + 59 + 40, y + 42, TextureX[inputXPDigits[5]], 205, 8, 12); - drawTexturedModalRect(x + 59 + 48, y + 42, TextureX[inputXPDigits[6]], 205, 8, 12); - drawTexturedModalRect(x + 116, y + 42, 123, 205, 15, 12); - - updateScreen(); - - fontRendererObj.drawString(I18n.translateToLocal("gui.title"), x + 8, y + 4, 0x404040); - - super.drawScreen(mouseX, mouseY, partialTicks); - } - - @Override - public void updateScreen() { - setXPAndPlayerXPDigits(); - int x = width - GUI_WIDTH >> 1; - int y = height - GUI_HEIGHT >> 1; - - drawTexturedModalRect(x + 59 + 0, y + 14, TextureX[xpDigits[0]], 175, 8, 12); - drawTexturedModalRect(x + 59 + 8, y + 14, TextureX[xpDigits[1]], 175, 8, 12); - drawTexturedModalRect(x + 59 + 16, y + 14, TextureX[xpDigits[2]], 175, 8, 12); - drawTexturedModalRect(x + 59 + 24, y + 14, TextureX[xpDigits[3]], 175, 8, 12); - drawTexturedModalRect(x + 59 + 32, y + 14, TextureX[xpDigits[4]], 175, 8, 12); - drawTexturedModalRect(x + 59 + 40, y + 14, TextureX[xpDigits[5]], 175, 8, 12); - drawTexturedModalRect(x + 59 + 48, y + 14, TextureX[xpDigits[6]], 175, 8, 12); - - drawTexturedModalRect(x + 59 + 0, y + 28, TextureX[playerXPDigits[0]], 190, 8, 12); - drawTexturedModalRect(x + 59 + 8, y + 28, TextureX[playerXPDigits[1]], 190, 8, 12); - drawTexturedModalRect(x + 59 + 16, y + 28, TextureX[playerXPDigits[2]], 190, 8, 12); - drawTexturedModalRect(x + 59 + 24, y + 28, TextureX[playerXPDigits[3]], 190, 8, 12); - drawTexturedModalRect(x + 59 + 32, y + 28, TextureX[playerXPDigits[4]], 190, 8, 12); - drawTexturedModalRect(x + 59 + 40, y + 28, TextureX[playerXPDigits[5]], 190, 8, 12); - drawTexturedModalRect(x + 59 + 48, y + 28, TextureX[playerXPDigits[6]], 190, 8, 12); - } - - @Override - protected void actionPerformed(GuiButton btn) throws IOException { - onButtonClicked(btn.id); - } - - private void onButtonClicked(int id) { - int exp; - switch (id) { - case BTN_DEL: inputXP = 0; setInputXPDigits(); break; - case BTN_ALLDEP: - if (0 == playerXP) + private static final ResourceLocation GUI_TEXTURE = + new ResourceLocation(XPBox.MODID, "textures/gui/xpbox.png"); + + private static final int[] TextureX = {2, 13, 24, 35, 46, 57, 68, 79, 90, 101, 112}; + + private static final int GUI_WIDTH = 176, GUI_HEIGHT = 166; + private static final int BUTTON_WIDTH = 20, BUTTON_HEIGHT = 20; + + private static final int DIGITS = 7; + public static final int MAX = 9999999; + + private static final int BTN_0 = 0, + BTN_1 = 1, + BTN_2 = 2, + BTN_3 = 3, + BTN_4 = 4, + BTN_5 = 5, + BTN_6 = 6, + BTN_7 = 7, + BTN_8 = 8, + BTN_9 = 9, + BTN_DEL = 10, + BTN_ALLDEP = 11, + BTN_ALLWD = 12, + BTN_DEP = 13, + BTN_WD = 14, + BTN_SETLVL = 15; + + public static GuiXPBox instance; + public int xp, playerXP; + private int inputXP; + private int[] xpDigits, playerXPDigits, inputXPDigits; + + public GuiXPBox(int xp, int playerXP) { + this.xp = xp; + this.playerXP = playerXP; + xpDigits = new int[DIGITS]; + playerXPDigits = new int[DIGITS]; + inputXPDigits = new int[DIGITS]; + + setInputXPDigits(); + + instance = this; + } + + public void setXPAndPlayerXP(int xp, int playerXP) { + this.xp = xp; + this.playerXP = playerXP; + updateScreen(); + } + + @Override + public void onGuiClosed() { + instance = null; + } + + private void setXPAndPlayerXPDigits() { + int i = DIGITS; + int t = xp; + + do { + xpDigits[--i] = t % 10; + } while ((t /= 10) != 0); + while (--i >= 0) + xpDigits[i] = 10; + + i = DIGITS; + t = playerXP; + if (t > MAX) + t = MAX; + do { + playerXPDigits[--i] = t % 10; + } while ((t /= 10) != 0); + while (--i >= 0) + playerXPDigits[i] = 10; + } + + private void setInputXPDigits() { + int i = DIGITS; + int t = inputXP; + + do { + inputXPDigits[--i] = t % 10; + } while ((t /= 10) != 0); + while (--i >= 0) + inputXPDigits[i] = 10; + } + + @Override + public void initGui() { + super.initGui(); + int x = width - GUI_WIDTH >> 1; + int y = height - GUI_HEIGHT >> 1; + + buttonList.add(new GuiButton(BTN_1, x + 11, y + 62, BUTTON_WIDTH, BUTTON_HEIGHT, I18n.translateToLocal("gui.button_1"))); + buttonList.add(new GuiButton(BTN_2, x + 11 + 25, y + 62, BUTTON_WIDTH, BUTTON_HEIGHT, I18n.translateToLocal("gui.button_2"))); + buttonList.add(new GuiButton(BTN_3, x + 11 + 50, y + 62, BUTTON_WIDTH, BUTTON_HEIGHT, I18n.translateToLocal("gui.button_3"))); + buttonList.add(new GuiButton(BTN_4, x + 11, y + 62 + 25, BUTTON_WIDTH, BUTTON_HEIGHT, I18n.translateToLocal("gui.button_4"))); + buttonList.add(new GuiButton(BTN_5, x + 11 + 25, y + 62 + 25, BUTTON_WIDTH, BUTTON_HEIGHT, I18n.translateToLocal("gui.button_5"))); + buttonList.add(new GuiButton(BTN_6, x + 11 + 50, y + 62 + 25, BUTTON_WIDTH, BUTTON_HEIGHT, I18n.translateToLocal("gui.button_6"))); + buttonList.add(new GuiButton(BTN_7, x + 11, y + 62 + 50, BUTTON_WIDTH, BUTTON_HEIGHT, I18n.translateToLocal("gui.button_7"))); + buttonList.add(new GuiButton(BTN_8, x + 11 + 25, y + 62 + 50, BUTTON_WIDTH, BUTTON_HEIGHT, I18n.translateToLocal("gui.button_8"))); + buttonList.add(new GuiButton(BTN_9, x + 11 + 50, y + 62 + 50, BUTTON_WIDTH, BUTTON_HEIGHT, I18n.translateToLocal("gui.button_9"))); + buttonList.add(new GuiButton(BTN_0, x + 11, y + 62 + 75, BUTTON_WIDTH, BUTTON_HEIGHT, I18n.translateToLocal("gui.button_0"))); + buttonList.add(new GuiButton(BTN_DEL, x + 11 + 25, y + 62 + 75, BUTTON_WIDTH * 2 + 5, BUTTON_HEIGHT, I18n.translateToLocal("gui.button_del"))); + buttonList.add(new GuiButton(BTN_ALLDEP, x + 11 + 80, y + 62, 75, BUTTON_HEIGHT, I18n.translateToLocal("gui.button_alldep"))); + buttonList.add(new GuiButton(BTN_ALLWD, x + 11 + 80, y + 62 + 25, 75, BUTTON_HEIGHT, I18n.translateToLocal("gui.button_allwd"))); + buttonList.add(new GuiButton(BTN_DEP, x + 11 + 80, y + 62 + 75, 35, BUTTON_HEIGHT, I18n.translateToLocal("gui.button_dep"))); + buttonList.add(new GuiButton(BTN_WD, x + 11 + 120, y + 62 + 75, 35, BUTTON_HEIGHT, I18n.translateToLocal("gui.button_wd"))); + buttonList.add(new GuiButton(BTN_SETLVL, x + 11 + 80, y + 62 + 50, 75, BUTTON_HEIGHT, I18n.translateToLocal("gui.button_setlv"))); + } + + private void addInput(int d) { + inputXP = inputXP * 10 + d; + if (inputXP > MAX) + inputXP = MAX; + setInputXPDigits(); + } + + @Override + protected void keyTyped(char typedChar, int code) throws IOException { + super.keyTyped(typedChar, code); + switch (typedChar) { + case 13: + onButtonClicked(BTN_SETLVL); break; - inputXP = 0; - PacketHandler.wrapper.sendToServer(new MessageOp(MessageOp.DEPOSIT, playerXP)); - break; - case BTN_ALLWD: - if (0 == xp) + case 8: + case 127: + onButtonClicked(BTN_DEL); break; - inputXP = 0; - PacketHandler.wrapper.sendToServer(new MessageOp(MessageOp.WITHDRAW, xp)); - break; - case BTN_DEP: - exp = inputXP; - if (exp > playerXP) - exp = playerXP; - if (exp + xp > MAX) - exp = MAX - xp; - if (0 == exp) + default: + if (typedChar >= '0' && typedChar <= '9') + addInput(typedChar - '0'); + } + } + + @Override + public void drawScreen(int mouseX, int mouseY, float partialTicks) { + GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0f); + mc.getTextureManager().bindTexture(GUI_TEXTURE); + int x = width - GUI_WIDTH >> 1; + int y = height - GUI_HEIGHT >> 1; + drawTexturedModalRect(x, y, 0, 0, GUI_WIDTH, GUI_HEIGHT); + + drawTexturedModalRect(x + 116, y + 14, 123, 175, 15, 12); + + drawTexturedModalRect(x + 116, y + 28, 123, 190, 15, 12); + + drawTexturedModalRect(x + 59 + 0, y + 42, TextureX[inputXPDigits[0]], 205, 8, 12); + drawTexturedModalRect(x + 59 + 8, y + 42, TextureX[inputXPDigits[1]], 205, 8, 12); + drawTexturedModalRect(x + 59 + 16, y + 42, TextureX[inputXPDigits[2]], 205, 8, 12); + drawTexturedModalRect(x + 59 + 24, y + 42, TextureX[inputXPDigits[3]], 205, 8, 12); + drawTexturedModalRect(x + 59 + 32, y + 42, TextureX[inputXPDigits[4]], 205, 8, 12); + drawTexturedModalRect(x + 59 + 40, y + 42, TextureX[inputXPDigits[5]], 205, 8, 12); + drawTexturedModalRect(x + 59 + 48, y + 42, TextureX[inputXPDigits[6]], 205, 8, 12); + drawTexturedModalRect(x + 116, y + 42, 123, 205, 15, 12); + + updateScreen(); + + fontRenderer.drawString(I18n.translateToLocal("gui.title"), x + 8, y + 4, 0x404040); + + super.drawScreen(mouseX, mouseY, partialTicks); + } + + @Override + public void updateScreen() { + setXPAndPlayerXPDigits(); + int x = width - GUI_WIDTH >> 1; + int y = height - GUI_HEIGHT >> 1; + + drawTexturedModalRect(x + 59 + 0, y + 14, TextureX[xpDigits[0]], 175, 8, 12); + drawTexturedModalRect(x + 59 + 8, y + 14, TextureX[xpDigits[1]], 175, 8, 12); + drawTexturedModalRect(x + 59 + 16, y + 14, TextureX[xpDigits[2]], 175, 8, 12); + drawTexturedModalRect(x + 59 + 24, y + 14, TextureX[xpDigits[3]], 175, 8, 12); + drawTexturedModalRect(x + 59 + 32, y + 14, TextureX[xpDigits[4]], 175, 8, 12); + drawTexturedModalRect(x + 59 + 40, y + 14, TextureX[xpDigits[5]], 175, 8, 12); + drawTexturedModalRect(x + 59 + 48, y + 14, TextureX[xpDigits[6]], 175, 8, 12); + + drawTexturedModalRect(x + 59 + 0, y + 28, TextureX[playerXPDigits[0]], 190, 8, 12); + drawTexturedModalRect(x + 59 + 8, y + 28, TextureX[playerXPDigits[1]], 190, 8, 12); + drawTexturedModalRect(x + 59 + 16, y + 28, TextureX[playerXPDigits[2]], 190, 8, 12); + drawTexturedModalRect(x + 59 + 24, y + 28, TextureX[playerXPDigits[3]], 190, 8, 12); + drawTexturedModalRect(x + 59 + 32, y + 28, TextureX[playerXPDigits[4]], 190, 8, 12); + drawTexturedModalRect(x + 59 + 40, y + 28, TextureX[playerXPDigits[5]], 190, 8, 12); + drawTexturedModalRect(x + 59 + 48, y + 28, TextureX[playerXPDigits[6]], 190, 8, 12); + } + + @Override + protected void actionPerformed(GuiButton btn) throws IOException { + onButtonClicked(btn.id); + } + + private void onButtonClicked(int id) { + int exp; + switch (id) { + case BTN_DEL: + inputXP = 0; + setInputXPDigits(); break; - inputXP = 0; - PacketHandler.wrapper.sendToServer(new MessageOp(MessageOp.DEPOSIT, exp)); - break; - case BTN_WD: - exp = inputXP; - if (exp > xp) - exp = xp; - if (0 == exp) + case BTN_ALLDEP: + if (0 == playerXP) + break; + inputXP = 0; + PacketHandler.wrapper.sendToServer(new MessageOp(MessageOp.DEPOSIT, playerXP)); break; - inputXP = 0; - PacketHandler.wrapper.sendToServer(new MessageOp(MessageOp.WITHDRAW, exp)); - break; - case BTN_SETLVL: - PacketHandler.wrapper.sendToServer(new MessageOp(MessageOp.SETLEVEL, inputXP)); - inputXP = 0; - break; - default: - addInput(id); break; - } - setInputXPDigits(); - } + case BTN_ALLWD: + if (0 == xp) + break; + inputXP = 0; + PacketHandler.wrapper.sendToServer(new MessageOp(MessageOp.WITHDRAW, xp)); + break; + case BTN_DEP: + exp = inputXP; + if (exp > playerXP) + exp = playerXP; + if (exp + xp > MAX) + exp = MAX - xp; + if (0 == exp) + break; + inputXP = 0; + PacketHandler.wrapper.sendToServer(new MessageOp(MessageOp.DEPOSIT, exp)); + break; + case BTN_WD: + exp = inputXP; + if (exp > xp) + exp = xp; + if (0 == exp) + break; + inputXP = 0; + PacketHandler.wrapper.sendToServer(new MessageOp(MessageOp.WITHDRAW, exp)); + break; + case BTN_SETLVL: + PacketHandler.wrapper.sendToServer(new MessageOp(MessageOp.SETLEVEL, inputXP)); + inputXP = 0; + break; + default: + addInput(id); + break; + } + setInputXPDigits(); + } } diff --git a/src/main/java/xpbox/MessageOp.java b/src/main/java/xpbox/MessageOp.java index f0f8108..6d3da05 100644 --- a/src/main/java/xpbox/MessageOp.java +++ b/src/main/java/xpbox/MessageOp.java @@ -7,39 +7,40 @@ import net.minecraftforge.fml.common.network.simpleimpl.MessageContext; public class MessageOp implements IMessage { - public int op; - public int xp; - - public static final int DEPOSIT = 1; - public static final int WITHDRAW = 2; - public static final int SETLEVEL = 3; - - public MessageOp() {} - - public MessageOp(int op, int xp) { - this.op = op; - this.xp = xp; - } - - @Override - public void fromBytes(ByteBuf buf) { - op = buf.readInt(); - xp = buf.readInt(); - } - - @Override - public void toBytes(ByteBuf buf) { - buf.writeInt(op); - buf.writeInt(xp); - } - - public static class Handler implements IMessageHandler { - @Override - public IMessage onMessage(MessageOp message, MessageContext ctx) { - FMLCommonHandler.instance().getWorldThread(ctx.netHandler).addScheduledTask( - () -> BlockXPBox.updateXP(ctx.getServerHandler().playerEntity, - message.op, message.xp)); - return null; - } - } + public int op; + public int xp; + + public static final int DEPOSIT = 1; + public static final int WITHDRAW = 2; + public static final int SETLEVEL = 3; + + public MessageOp() { + } + + public MessageOp(int op, int xp) { + this.op = op; + this.xp = xp; + } + + @Override + public void fromBytes(ByteBuf buf) { + op = buf.readInt(); + xp = buf.readInt(); + } + + @Override + public void toBytes(ByteBuf buf) { + buf.writeInt(op); + buf.writeInt(xp); + } + + public static class Handler implements IMessageHandler { + @Override + public IMessage onMessage(MessageOp message, MessageContext ctx) { + FMLCommonHandler.instance().getWorldThread(ctx.netHandler).addScheduledTask( + () -> BlockXPBox.updateXP(ctx.getServerHandler().player, + message.op, message.xp)); + return null; + } + } } diff --git a/src/main/java/xpbox/MessageXP.java b/src/main/java/xpbox/MessageXP.java index a77d87f..00285f6 100644 --- a/src/main/java/xpbox/MessageXP.java +++ b/src/main/java/xpbox/MessageXP.java @@ -8,40 +8,41 @@ import net.minecraftforge.fml.common.network.simpleimpl.MessageContext; public class MessageXP implements IMessage { - public int xp; - public int playerXP; + public int xp; + public int playerXP; - public MessageXP(int xp, int playerXP) { - this.xp = xp; - this.playerXP = playerXP; - } + public MessageXP(int xp, int playerXP) { + this.xp = xp; + this.playerXP = playerXP; + } - public MessageXP() {} + public MessageXP() { + } - @Override - public void fromBytes(ByteBuf buf) { - xp = buf.readInt(); - playerXP = buf.readInt(); - } + @Override + public void fromBytes(ByteBuf buf) { + xp = buf.readInt(); + playerXP = buf.readInt(); + } - @Override - public void toBytes(ByteBuf buf) { - buf.writeInt(xp); - buf.writeInt(playerXP); - } + @Override + public void toBytes(ByteBuf buf) { + buf.writeInt(xp); + buf.writeInt(playerXP); + } - public static class Handler implements IMessageHandler { - @Override - public IMessage onMessage(MessageXP message, MessageContext ctx) { - FMLCommonHandler.instance().getWorldThread(ctx.netHandler).addScheduledTask( - () -> { - if (null == GuiXPBox.instance) - Minecraft.getMinecraft().displayGuiScreen( - new GuiXPBox(message.xp, message.playerXP)); - else - GuiXPBox.instance.setXPAndPlayerXP(message.xp, message.playerXP); - }); - return null; - } - } + public static class Handler implements IMessageHandler { + @Override + public IMessage onMessage(MessageXP message, MessageContext ctx) { + FMLCommonHandler.instance().getWorldThread(ctx.netHandler).addScheduledTask( + () -> { + if (null == GuiXPBox.instance) + Minecraft.getMinecraft().displayGuiScreen( + new GuiXPBox(message.xp, message.playerXP)); + else + GuiXPBox.instance.setXPAndPlayerXP(message.xp, message.playerXP); + }); + return null; + } + } } diff --git a/src/main/java/xpbox/PacketHandler.java b/src/main/java/xpbox/PacketHandler.java index 3ab0601..c7ceb54 100644 --- a/src/main/java/xpbox/PacketHandler.java +++ b/src/main/java/xpbox/PacketHandler.java @@ -5,12 +5,12 @@ import net.minecraftforge.fml.relauncher.Side; public class PacketHandler { - public static SimpleNetworkWrapper wrapper; + public static SimpleNetworkWrapper wrapper; - public static void init() { - wrapper = NetworkRegistry.INSTANCE.newSimpleChannel(XPBox.MODID); + public static void init() { + wrapper = NetworkRegistry.INSTANCE.newSimpleChannel(XPBox.MODID); - wrapper.registerMessage(MessageXP.Handler.class, MessageXP.class, 1, Side.CLIENT); - wrapper.registerMessage(MessageOp.Handler.class, MessageOp.class, 2, Side.SERVER); - } + wrapper.registerMessage(MessageXP.Handler.class, MessageXP.class, 1, Side.CLIENT); + wrapper.registerMessage(MessageOp.Handler.class, MessageOp.class, 2, Side.SERVER); + } } diff --git a/src/main/java/xpbox/XPBox.java b/src/main/java/xpbox/XPBox.java index c9fdac0..91f6462 100644 --- a/src/main/java/xpbox/XPBox.java +++ b/src/main/java/xpbox/XPBox.java @@ -1,78 +1,45 @@ package xpbox; import net.minecraft.block.Block; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.init.Blocks; -import net.minecraft.init.Items; -import net.minecraft.item.ItemBlock; -import net.minecraft.item.ItemStack; -import net.minecraft.util.ResourceLocation; -import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.capabilities.*; -import net.minecraftforge.event.AttachCapabilitiesEvent; -import net.minecraftforge.event.entity.player.PlayerEvent; 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.eventhandler.SubscribeEvent; -import net.minecraftforge.fml.common.registry.GameRegistry; @Mod(modid = XPBox.MODID, name = XPBox.NAME, version = XPBox.VERSION, - acceptedMinecraftVersions = "[1.10.2]") + acceptedMinecraftVersions = "[1.12.2]", + useMetadata = true) public class XPBox { - public static final String MODID = "xpbox"; - public static final String NAME = "XPBox"; - public static final String VERSION = "1.0"; + public static final String MODID = "xpbox"; + public static final String NAME = "XPBox"; + public static final String VERSION = "1.0"; - public static Block blockXPBox; + public static Block blockXPBox = new BlockXPBox(); - @CapabilityInject(XPCapability.class) - public static final Capability XP_CAPABILITY = null; + @CapabilityInject(XPCapability.class) + public static final Capability XP_CAPABILITY = null; - @SidedProxy(serverSide = "xpbox.CommonProxy", - clientSide = "xpbox.ClientProxy") - public static CommonProxy proxy; + @Mod.Instance(MODID) + public static XPBox instance; - @Mod.EventHandler - public void preInit(FMLPreInitializationEvent e) { - // register block - GameRegistry.register(blockXPBox = new BlockXPBox()); - ItemBlock item = new ItemBlock(blockXPBox); - GameRegistry.register(item, blockXPBox.getRegistryName()); + @SidedProxy(serverSide = "xpbox.CommonProxy", + clientSide = "xpbox.ClientProxy") + public static CommonProxy proxy; - proxy.registerItemRenderer(item); + @Mod.EventHandler + public void preInit(FMLPreInitializationEvent e) { + // init network + PacketHandler.init(); + } - // init network - PacketHandler.init(); - } + @Mod.EventHandler + public void init(FMLInitializationEvent e) { + // register xp capability + CapabilityManager.INSTANCE.register(XPCapability.class, + new XPCapability.Storage(), + XPCapability::new); - @Mod.EventHandler - public void init(FMLInitializationEvent e) { - // register recipe - BlockXPBox.registerRecipe(); + } - // register xp capability - CapabilityManager.INSTANCE.register(XPCapability.class, - new XPCapability.Storage(), - XPCapability::new); - - // register event handler - MinecraftForge.EVENT_BUS.register(this); - } - - @SubscribeEvent - public void attachCapability(AttachCapabilitiesEvent.Entity e) { - if (e.getEntity() instanceof EntityPlayer - && !e.getEntity().hasCapability(XP_CAPABILITY, null)) - e.addCapability(new ResourceLocation(MODID, "xpCapability"), - new XPCapability.Provider()); - } - - @SubscribeEvent - public void onPlayerClone(PlayerEvent.Clone e) { - // may be returning from the End or respawning after death - e.getEntity().getCapability(XPBox.XP_CAPABILITY, null).value = - e.getOriginal().getCapability(XPBox.XP_CAPABILITY, null).value; - } } diff --git a/src/main/java/xpbox/XPCapability.java b/src/main/java/xpbox/XPCapability.java index 4437757..9095e3a 100644 --- a/src/main/java/xpbox/XPCapability.java +++ b/src/main/java/xpbox/XPCapability.java @@ -9,44 +9,44 @@ import javax.annotation.Nullable; public class XPCapability { - public int value; - - public static class Storage implements Capability.IStorage { - @Override - public void readNBT(Capability capability, XPCapability instance, EnumFacing side, NBTBase nbt) { - instance.value = ((NBTTagInt) nbt).getInt(); - } - - @Override - public NBTBase writeNBT(Capability capability, XPCapability instance, EnumFacing side) { - return new NBTTagInt(instance.value); - } - } - - public static class Provider implements ICapabilitySerializable { - - private XPCapability cap = new XPCapability(); - - @Override - public boolean hasCapability(Capability capability, @Nullable EnumFacing facing) { - return capability == XPBox.XP_CAPABILITY; - } - - @Override - public T getCapability(Capability capability, @Nullable EnumFacing facing) { - if (capability == XPBox.XP_CAPABILITY) - return (T) cap; - return null; - } - - @Override - public NBTTagInt serializeNBT() { - return new NBTTagInt(cap.value); - } - - @Override - public void deserializeNBT(NBTTagInt nbt) { - cap.value = nbt.getInt(); - } - } + public int value; + + public static class Storage implements Capability.IStorage { + @Override + public void readNBT(Capability capability, XPCapability instance, EnumFacing side, NBTBase nbt) { + instance.value = ((NBTTagInt) nbt).getInt(); + } + + @Override + public NBTBase writeNBT(Capability capability, XPCapability instance, EnumFacing side) { + return new NBTTagInt(instance.value); + } + } + + public static class Provider implements ICapabilitySerializable { + + private XPCapability cap = new XPCapability(); + + @Override + public boolean hasCapability(Capability capability, @Nullable EnumFacing facing) { + return capability == XPBox.XP_CAPABILITY; + } + + @Override + public T getCapability(Capability capability, @Nullable EnumFacing facing) { + if (capability == XPBox.XP_CAPABILITY) + return (T) cap; + return null; + } + + @Override + public NBTTagInt serializeNBT() { + return new NBTTagInt(cap.value); + } + + @Override + public void deserializeNBT(NBTTagInt nbt) { + cap.value = nbt.getInt(); + } + } } diff --git a/src/main/resources/assets/xpbox/recipes/xpbox.json b/src/main/resources/assets/xpbox/recipes/xpbox.json new file mode 100644 index 0000000..281daf1 --- /dev/null +++ b/src/main/resources/assets/xpbox/recipes/xpbox.json @@ -0,0 +1,31 @@ +{ + "type": "minecraft:crafting_shaped", + "pattern": [ + "OGO", + "ICI", + "ERE" + ], + "key": { + "O": { + "item": "minecraft:obsidian" + }, + "E": { + "item": "minecraft:ender_pearl" + }, + "I": { + "item": "minecraft:iron_ingot" + }, + "R": { + "item": "minecraft:redstone" + }, + "C": { + "item": "minecraft:chest" + }, + "G": { + "item": "minecraft:glass_pane" + } + }, + "result": { + "item": "xpbox:xpbox" + } +} \ No newline at end of file diff --git a/src/main/resources/mcmod.info b/src/main/resources/mcmod.info index b1acbf3..e8e812c 100644 --- a/src/main/resources/mcmod.info +++ b/src/main/resources/mcmod.info @@ -8,7 +8,7 @@ "url": "", "updateUrl": "", "authorList": ["clocka", "plodsoft"], - "credits": "original mod was made by clocka, I (plodsoft) port it to 1.10.2", + "credits": "original mod was made by clocka, I (plodsoft) port it to 1.10.2/1.12.2", "logoFile": "", "screenshots": [], "dependencies": []