├── .gitignore ├── README.md ├── build.gradle ├── client └── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── server ├── build.gradle └── src │ └── main │ └── java │ └── uk │ └── co │ └── thinkofdeath │ └── prismarine │ └── server │ ├── Configuration.java │ ├── Main.java │ ├── PrismarineServer.java │ └── network │ ├── HandshakingHandler.java │ ├── LoginHandler.java │ ├── PlayHandler.java │ └── StatusHandler.java ├── settings.gradle └── shared ├── build.gradle └── src └── main └── java └── uk └── co └── thinkofdeath └── prismarine ├── NeatFormatter.java ├── Prismarine.java ├── block ├── Block.java └── BlockRegistry.java ├── chat ├── ChatSerializer.java ├── Color.java ├── Component.java ├── ComponentSerializer.java ├── RootComponent.java └── TextComponent.java ├── entity ├── Entity.java └── metadata │ ├── EntityMetadata.java │ ├── MetaEntry.java │ └── MetaKey.java ├── game ├── Difficulty.java ├── Dimension.java ├── GameMode.java └── Position.java ├── item ├── Item.java ├── ItemRegistry.java └── ItemStack.java ├── log └── LogUtil.java ├── network ├── CipherCodec.java ├── CompressionCodec.java ├── ConnectionInitializer.java ├── Constants.java ├── MCByteBuf.java ├── NetworkHandler.java ├── NetworkManager.java ├── NullHandler.java ├── PacketCodec.java ├── VarIntFrameCodec.java ├── login │ ├── LoginResponse.java │ └── Property.java ├── ping │ ├── Ping.java │ ├── PingPlayers.java │ └── PingVersion.java └── protocol │ ├── IHandshakingHandlerClientbound.java │ ├── IHandshakingHandlerServerbound.java │ ├── ILoginHandlerClientbound.java │ ├── ILoginHandlerServerbound.java │ ├── IPlayHandlerClientbound.java │ ├── IPlayHandlerServerbound.java │ ├── IStatusHandlerClientbound.java │ ├── IStatusHandlerServerbound.java │ ├── Packet.java │ ├── PacketHandler.java │ ├── Protocol.java │ ├── ProtocolDirection.java │ ├── handshaking │ └── Handshake.java │ ├── login │ ├── EncryptionRequest.java │ ├── EncryptionResponse.java │ ├── LoginDisconnect.java │ ├── LoginStart.java │ ├── LoginSuccess.java │ └── SetInitialCompression.java │ ├── play │ ├── Animation.java │ ├── ChatMessage.java │ ├── CollectItem.java │ ├── DestroyEntities.java │ ├── Entity.java │ ├── EntityAttach.java │ ├── EntityEffect.java │ ├── EntityEquipment.java │ ├── EntityHeadLook.java │ ├── EntityLook.java │ ├── EntityMove.java │ ├── EntityMoveLook.java │ ├── EntityRemoveEffect.java │ ├── EntitySetMetadata.java │ ├── EntityStatus.java │ ├── EntityTeleport.java │ ├── EntityVelocity.java │ ├── JoinGame.java │ ├── KeepAlivePing.java │ ├── KeepAlivePong.java │ ├── PlayerTeleport.java │ ├── Respawn.java │ ├── ServerMessage.java │ ├── SetExperience.java │ ├── SetHeldItem.java │ ├── SpawnExperienceOrb.java │ ├── SpawnLivingEntity.java │ ├── SpawnObject.java │ ├── SpawnPainting.java │ ├── SpawnPlayer.java │ ├── SpawnPosition.java │ ├── TimeUpdate.java │ ├── UpdateHealth.java │ └── UseBed.java │ └── status │ ├── StatusPing.java │ ├── StatusPong.java │ ├── StatusReponse.java │ └── StatusRequest.java ├── registry └── Registry.java └── util ├── IntMap.java └── Stringable.java /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | /build 3 | /test-server 4 | 5 | *.iml -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Prismarine 2 | ======== 3 | 4 | A small minecraft server that aims to do the minimum required 5 | for a basic minecraft server. 6 | 7 | I'm working on this for a fun little hobby project, don't expect it 8 | to go anywhere :) -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | apply plugin: 'java' 18 | apply plugin: 'application' 19 | 20 | sourceCompatibility = 1.8 21 | 22 | repositories { 23 | mavenCentral() 24 | } 25 | 26 | dependencies { 27 | } 28 | -------------------------------------------------------------------------------- /client/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | 3 | sourceCompatibility = 1.8 4 | version = '1.0' 5 | 6 | repositories { 7 | mavenCentral() 8 | } 9 | 10 | dependencies { 11 | testCompile group: 'junit', name: 'junit', version: '4.11' 12 | } 13 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Thinkofname/Prismarine-Standalone/b324a20910414f4facdcc3aabe76901642f6f942/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Fri Aug 15 22:54:52 BST 2014 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.0-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /server/build.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | apply plugin: 'java' 18 | apply plugin: 'application' 19 | 20 | sourceCompatibility = 1.8 21 | mainClassName = "uk.co.thinkofdeath.prismarine.server.Main" 22 | 23 | repositories { 24 | mavenCentral() 25 | } 26 | 27 | jar { 28 | archiveName = "Prismarine-Server.jar" 29 | manifest.attributes("Main-Class": mainClassName) 30 | from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } } 31 | } 32 | 33 | dependencies { 34 | compile project(":shared") 35 | compile group: "io.netty", name: "netty-all", version: "5.0.0.Alpha1" 36 | compile group: "com.google.code.gson", name: "gson", version: "2.3" 37 | compile group: "com.google.guava", name: "guava", version: "17.0" 38 | testCompile group: 'junit', name: 'junit', version: '4.11' 39 | } 40 | -------------------------------------------------------------------------------- /server/src/main/java/uk/co/thinkofdeath/prismarine/server/Configuration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.server; 18 | 19 | public class Configuration { 20 | 21 | private String bindAddress = "0.0.0.0"; 22 | private int port = 25565; 23 | private boolean onlineMode = true; 24 | 25 | public Configuration() { 26 | 27 | } 28 | 29 | public String getBindAddress() { 30 | return bindAddress; 31 | } 32 | 33 | public void setBindAddress(String bindAddress) { 34 | this.bindAddress = bindAddress; 35 | } 36 | 37 | public int getPort() { 38 | return port; 39 | } 40 | 41 | public void setPort(int port) { 42 | this.port = port; 43 | } 44 | 45 | public boolean isOnlineMode() { 46 | return onlineMode; 47 | } 48 | 49 | public void setOnlineMode(boolean onlineMode) { 50 | this.onlineMode = onlineMode; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /server/src/main/java/uk/co/thinkofdeath/prismarine/server/Main.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.server; 18 | 19 | import uk.co.thinkofdeath.prismarine.NeatFormatter; 20 | 21 | import java.util.Scanner; 22 | import java.util.logging.ConsoleHandler; 23 | import java.util.logging.Handler; 24 | import java.util.logging.Logger; 25 | 26 | public class Main { 27 | 28 | public static void main(String[] args) { 29 | ConsoleHandler console = new ConsoleHandler(); 30 | console.setFormatter(new NeatFormatter()); 31 | Logger root = Logger.getLogger(""); 32 | for (Handler handler : root.getHandlers()) { 33 | root.removeHandler(handler); 34 | } 35 | root.addHandler(console); 36 | 37 | Configuration config = new Configuration(); 38 | PrismarineServer prismarine = new PrismarineServer(config); 39 | prismarine.start(); 40 | 41 | Scanner scanner = new Scanner(System.in); 42 | while (scanner.hasNextLine()) { 43 | String line = scanner.nextLine(); 44 | if (line.equalsIgnoreCase("exit")) { 45 | prismarine.close(); 46 | return; 47 | } 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /server/src/main/java/uk/co/thinkofdeath/prismarine/server/PrismarineServer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.server; 18 | 19 | import uk.co.thinkofdeath.prismarine.Prismarine; 20 | import uk.co.thinkofdeath.prismarine.log.LogUtil; 21 | import uk.co.thinkofdeath.prismarine.server.network.HandshakingHandler; 22 | 23 | import java.util.logging.Logger; 24 | 25 | public class PrismarineServer extends Prismarine { 26 | 27 | private static final Logger logger = LogUtil.get(PrismarineServer.class); 28 | private final Configuration config; 29 | 30 | public PrismarineServer(Configuration config) { 31 | this.config = config; 32 | logger.info("Starting Prismarine"); 33 | init(); 34 | } 35 | 36 | public void start() { 37 | getNetworkManager().listen(config.getBindAddress(), config.getPort(), HandshakingHandler::new); 38 | } 39 | 40 | public void close() { 41 | logger.info("Shutting down Prismarine"); 42 | getNetworkManager().close(); 43 | 44 | logger.info("Shutdown complete"); 45 | } 46 | 47 | public boolean isOnlineMode() { 48 | return config.isOnlineMode(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /server/src/main/java/uk/co/thinkofdeath/prismarine/server/network/HandshakingHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.server.network; 18 | 19 | import uk.co.thinkofdeath.prismarine.chat.TextComponent; 20 | import uk.co.thinkofdeath.prismarine.network.Constants; 21 | import uk.co.thinkofdeath.prismarine.network.NetworkHandler; 22 | import uk.co.thinkofdeath.prismarine.network.PacketCodec; 23 | import uk.co.thinkofdeath.prismarine.network.protocol.IHandshakingHandlerServerbound; 24 | import uk.co.thinkofdeath.prismarine.network.protocol.Protocol; 25 | import uk.co.thinkofdeath.prismarine.network.protocol.handshaking.Handshake; 26 | 27 | public class HandshakingHandler implements IHandshakingHandlerServerbound { 28 | 29 | private NetworkHandler handler; 30 | 31 | @Override 32 | public void handle(Handshake handshake) { 33 | switch (handshake.getNext()) { 34 | case STATUS: 35 | handler.getChannel().pipeline().get(PacketCodec.class) 36 | .setProtocol(Protocol.STATUS); 37 | handler.setHandler(new StatusHandler()); 38 | break; 39 | case LOGIN: 40 | handler.getChannel().pipeline().get(PacketCodec.class) 41 | .setProtocol(Protocol.LOGIN); 42 | handler.setHandler(new LoginHandler()); 43 | 44 | if (handshake.getProtocolVersion() < Constants.PROTOCOL_VERSION) { 45 | handler.disconnect(new TextComponent("Client out of date. This server is using " + Constants.MINECRAFT_VERSION 46 | + " (" + handshake.getProtocolVersion() + ":" + Constants.PROTOCOL_VERSION + ")")); 47 | return; 48 | } else if (handshake.getProtocolVersion() > Constants.PROTOCOL_VERSION) { 49 | handler.disconnect(new TextComponent("Server out of data. This server is using " + Constants.MINECRAFT_VERSION 50 | + " (" + handshake.getProtocolVersion() + ":" + Constants.PROTOCOL_VERSION + ")")); 51 | return; 52 | } 53 | break; 54 | default: 55 | throw new RuntimeException("Unknown state"); 56 | } 57 | } 58 | 59 | @Override 60 | public void setNetworkHandler(NetworkHandler handler) { 61 | this.handler = handler; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /server/src/main/java/uk/co/thinkofdeath/prismarine/server/network/PlayHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.server.network; 18 | 19 | import io.netty.util.concurrent.ScheduledFuture; 20 | import uk.co.thinkofdeath.prismarine.chat.Color; 21 | import uk.co.thinkofdeath.prismarine.chat.TextComponent; 22 | import uk.co.thinkofdeath.prismarine.game.Difficulty; 23 | import uk.co.thinkofdeath.prismarine.game.Dimension; 24 | import uk.co.thinkofdeath.prismarine.game.GameMode; 25 | import uk.co.thinkofdeath.prismarine.log.LogUtil; 26 | import uk.co.thinkofdeath.prismarine.network.NetworkHandler; 27 | import uk.co.thinkofdeath.prismarine.network.login.Property; 28 | import uk.co.thinkofdeath.prismarine.network.protocol.IPlayHandlerServerbound; 29 | import uk.co.thinkofdeath.prismarine.network.protocol.play.*; 30 | 31 | import java.util.Random; 32 | import java.util.UUID; 33 | import java.util.concurrent.TimeUnit; 34 | import java.util.logging.Logger; 35 | 36 | public class PlayHandler implements IPlayHandlerServerbound { 37 | 38 | private static final Logger logger = LogUtil.get(PlayHandler.class); 39 | private final String username; 40 | private final UUID uuid; 41 | private final Property[] properties; 42 | private NetworkHandler handler; 43 | 44 | private ScheduledFuture pingTask; 45 | private Random random = new Random(); 46 | private int lastPingId = -1; 47 | 48 | public PlayHandler(String username, UUID uuid, Property[] properties) { 49 | this.username = username; 50 | this.uuid = uuid; 51 | this.properties = properties; 52 | } 53 | 54 | @Override 55 | public void handle(KeepAlivePong keepAlivePong) { 56 | if (lastPingId != keepAlivePong.getId()) { 57 | handler.disconnect(new TextComponent("Incorrect Keep Alive")); 58 | } 59 | lastPingId = -1; 60 | } 61 | 62 | @Override 63 | public void handle(ChatMessage chatMessage) { 64 | TextComponent component = new TextComponent("<"); 65 | TextComponent user = new TextComponent(username); 66 | user.setColor(Color.AQUA); 67 | component.addComponent(user); 68 | component.addComponent(new TextComponent("> ")); 69 | component.addComponent(new TextComponent(chatMessage.getMessage())); 70 | handler.sendPacket(new ServerMessage( 71 | component, 72 | ServerMessage.Type.CHAT 73 | )); 74 | } 75 | 76 | @Override 77 | public void setNetworkHandler(NetworkHandler handler) { 78 | this.handler = handler; 79 | handler.getChannel().closeFuture().addListener(f -> pingTask.cancel(true)); 80 | pingTask = handler.getChannel().eventLoop().scheduleAtFixedRate(() -> { 81 | if (lastPingId != -1) { 82 | handler.disconnect(new TextComponent("Timed out")); 83 | return; 84 | } 85 | lastPingId = random.nextInt(Short.MAX_VALUE); 86 | handler.sendPacket(new KeepAlivePing(lastPingId)); 87 | }, 5, 10, TimeUnit.SECONDS); 88 | } 89 | 90 | public void join() { 91 | handler.sendPacket(new JoinGame( 92 | 0, 93 | GameMode.CREATIVE, 94 | false, 95 | Dimension.OVERWORLD, 96 | Difficulty.EASY, 97 | 20, 98 | "", 99 | false 100 | )); 101 | handler.sendPacket(new PlayerTeleport( 102 | 0, 128, 0, 103 | 0, 0, 104 | 0 105 | )); 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /server/src/main/java/uk/co/thinkofdeath/prismarine/server/network/StatusHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.server.network; 18 | 19 | import uk.co.thinkofdeath.prismarine.chat.Color; 20 | import uk.co.thinkofdeath.prismarine.chat.Component; 21 | import uk.co.thinkofdeath.prismarine.chat.TextComponent; 22 | import uk.co.thinkofdeath.prismarine.network.Constants; 23 | import uk.co.thinkofdeath.prismarine.network.NetworkHandler; 24 | import uk.co.thinkofdeath.prismarine.network.ping.Ping; 25 | import uk.co.thinkofdeath.prismarine.network.protocol.IStatusHandlerServerbound; 26 | import uk.co.thinkofdeath.prismarine.network.protocol.status.StatusPing; 27 | import uk.co.thinkofdeath.prismarine.network.protocol.status.StatusPong; 28 | import uk.co.thinkofdeath.prismarine.network.protocol.status.StatusReponse; 29 | import uk.co.thinkofdeath.prismarine.network.protocol.status.StatusRequest; 30 | 31 | public class StatusHandler implements IStatusHandlerServerbound { 32 | 33 | private NetworkHandler handler; 34 | private State currentState = State.WAITING_REQUEST; 35 | 36 | @Override 37 | public void setNetworkHandler(NetworkHandler handler) { 38 | this.handler = handler; 39 | } 40 | 41 | @Override 42 | public void handle(StatusRequest statusRequest) { 43 | require(State.WAITING_REQUEST); 44 | 45 | Ping ping = new Ping(); 46 | Component motd = new TextComponent("Hello "); 47 | motd.setColor(Color.GREEN); 48 | Component world = new TextComponent("world"); 49 | world.setColor(Color.BLUE); 50 | motd.addComponent(world); 51 | ping.setDescription(motd); 52 | ping.getVersion().setName("MicroMC - " + Constants.MINECRAFT_VERSION); 53 | ping.getVersion().setProtocol(Constants.PROTOCOL_VERSION); 54 | ping.getPlayers().setMax(20); 55 | 56 | handler.sendPacket(new StatusReponse(ping)); 57 | currentState = State.WAITING_PING; 58 | } 59 | 60 | @Override 61 | public void handle(StatusPing statusPing) { 62 | require(State.WAITING_PING); 63 | handler.getChannel().write(new StatusPong(statusPing.getTime())) 64 | .addListener(f -> handler.getChannel().close()); 65 | currentState = State.DONE; 66 | } 67 | 68 | private void require(State state) { 69 | if (state != currentState) { 70 | throw new RuntimeException("Incorrect state"); 71 | } 72 | } 73 | 74 | private static enum State { 75 | WAITING_REQUEST, 76 | WAITING_PING, 77 | DONE 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | rootProject.name = 'Prismarine' 18 | 19 | include 'shared' 20 | include 'server' 21 | include 'client' 22 | 23 | -------------------------------------------------------------------------------- /shared/build.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | apply plugin: 'java' 18 | 19 | sourceCompatibility = 1.8 20 | 21 | repositories { 22 | mavenCentral() 23 | } 24 | 25 | dependencies { 26 | compile group: "io.netty", name: "netty-all", version: "5.0.0.Alpha1" 27 | compile group: "com.google.code.gson", name: "gson", version: "2.3" 28 | compile group: "com.google.guava", name: "guava", version: "17.0" 29 | testCompile group: 'junit', name: 'junit', version: '4.11' 30 | } 31 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/NeatFormatter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine; 18 | 19 | import java.util.Calendar; 20 | import java.util.logging.Formatter; 21 | import java.util.logging.LogRecord; 22 | 23 | public class NeatFormatter extends Formatter { 24 | 25 | @Override 26 | public String format(LogRecord record) { 27 | StringBuilder builder = new StringBuilder(); 28 | Calendar calendar = Calendar.getInstance(); 29 | calendar.setTimeInMillis(record.getMillis()); 30 | 31 | builder.append("["); 32 | int hours = calendar.get(Calendar.HOUR_OF_DAY); 33 | padAndLimit(builder, Integer.toString(hours), '0', 2); 34 | builder.append(":"); 35 | int minutes = calendar.get(Calendar.MINUTE); 36 | padAndLimit(builder, Integer.toString(minutes), '0', 2); 37 | builder.append("]["); 38 | padAndLimit(builder, record.getLevel().getName(), ' ', 5); 39 | builder.append("]["); 40 | String name = record.getLoggerName(); 41 | if (name == null) { 42 | name = "AnonLogger"; 43 | } 44 | padAndLimit(builder, name, ' ', 10); 45 | builder.append("]: "); 46 | builder.append(formatMessage(record)); 47 | builder.append('\n'); 48 | return builder.toString(); 49 | } 50 | 51 | private static void padAndLimit(StringBuilder builder, String val, char padding, int length) { 52 | if (val.length() >= length) { 53 | builder.append(val, 0, length); 54 | return; 55 | } 56 | int count = length - val.length(); 57 | for (int i = 0; i < count; i++) { 58 | builder.append(padding); 59 | } 60 | builder.append(val); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/Prismarine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine; 18 | 19 | import uk.co.thinkofdeath.prismarine.block.BlockRegistry; 20 | import uk.co.thinkofdeath.prismarine.item.ItemRegistry; 21 | import uk.co.thinkofdeath.prismarine.network.NetworkManager; 22 | 23 | public abstract class Prismarine { 24 | 25 | private static Prismarine instance; 26 | 27 | private final BlockRegistry blockRegistry = new BlockRegistry(); 28 | private final ItemRegistry itemRegistry = new ItemRegistry(); 29 | private NetworkManager networkManager = new NetworkManager(this); 30 | 31 | protected Prismarine() { 32 | if (instance != null) { 33 | throw new RuntimeException("Only a single Prismarine instance can exist"); 34 | } 35 | instance = this; 36 | } 37 | 38 | protected void init() { 39 | 40 | } 41 | 42 | public NetworkManager getNetworkManager() { 43 | return networkManager; 44 | } 45 | 46 | public BlockRegistry getBlockRegistry() { 47 | return blockRegistry; 48 | } 49 | 50 | public ItemRegistry getItemRegistry() { 51 | return itemRegistry; 52 | } 53 | 54 | public static Prismarine getInstance() { 55 | return instance; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/block/Block.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.block; 18 | 19 | public class Block { 20 | } 21 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/block/BlockRegistry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.block; 18 | 19 | import uk.co.thinkofdeath.prismarine.registry.Registry; 20 | 21 | public class BlockRegistry extends Registry { 22 | 23 | public BlockRegistry() { 24 | super(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/chat/ChatSerializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.chat; 18 | 19 | import com.google.gson.Gson; 20 | import com.google.gson.GsonBuilder; 21 | 22 | public class ChatSerializer { 23 | 24 | private static final Gson gson = attach(new GsonBuilder()) 25 | .create(); 26 | 27 | public static String toString(Component component) { 28 | return gson.toJson(component, Component.class); 29 | } 30 | 31 | public static Component fromString(String str) { 32 | return gson.fromJson(str, Component.class); 33 | } 34 | 35 | public static GsonBuilder attach(GsonBuilder builder) { 36 | builder.registerTypeAdapter(Component.class, new ComponentSerializer()); 37 | return builder; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/chat/Color.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.chat; 18 | 19 | public enum Color { 20 | BLACK, 21 | DARK_BLUE, 22 | DARK_GREEN, 23 | DARK_AQUA, 24 | DARK_RED, 25 | DARK_PURPLE, 26 | GOLD, 27 | GRAY, 28 | DARK_GRAY, 29 | BLUE, 30 | GREEN, 31 | AQUA, 32 | RED, 33 | LIGHT_PURPLE, 34 | YELLOW, 35 | WHITE, 36 | } 37 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/chat/Component.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.chat; 18 | 19 | import uk.co.thinkofdeath.prismarine.util.Stringable; 20 | 21 | import java.util.ArrayList; 22 | import java.util.List; 23 | 24 | public abstract class Component implements Stringable { 25 | 26 | Component parent = RootComponent.INSTANCE; 27 | List subComponents; 28 | Boolean bold; 29 | Boolean italic; 30 | Boolean underlined; 31 | Boolean strikethrough; 32 | Boolean obfuscated; 33 | Color color; 34 | 35 | Component() { 36 | 37 | } 38 | 39 | public void addComponent(Component component) { 40 | if (subComponents == null) { 41 | subComponents = new ArrayList<>(); 42 | } 43 | subComponents.add(component); 44 | component.parent = this; 45 | } 46 | 47 | public boolean getBold() { 48 | return bold == null ? parent.getBold() : bold; 49 | } 50 | 51 | public void setBold(boolean bold) { 52 | this.bold = bold; 53 | } 54 | 55 | public Boolean getItalic() { 56 | return italic == null ? parent.getItalic() : italic; 57 | } 58 | 59 | public void setItalic(boolean italic) { 60 | this.italic = italic; 61 | } 62 | 63 | public Boolean getUnderlined() { 64 | return underlined == null ? parent.getUnderlined() : underlined; 65 | } 66 | 67 | public void setUnderlined(boolean underlined) { 68 | this.underlined = underlined; 69 | } 70 | 71 | public Boolean getStrikethrough() { 72 | return strikethrough == null ? parent.getStrikethrough() : strikethrough; 73 | } 74 | 75 | public void setStrikethrough(boolean strikethrough) { 76 | this.strikethrough = strikethrough; 77 | } 78 | 79 | public Boolean getObfuscated() { 80 | return obfuscated == null ? parent.getObfuscated() : strikethrough; 81 | } 82 | 83 | public void setObfuscated(boolean obfuscated) { 84 | this.obfuscated = obfuscated; 85 | } 86 | 87 | public Color getColor() { 88 | return color == null ? parent.getColor() : color; 89 | } 90 | 91 | public void setColor(Color color) { 92 | this.color = color; 93 | } 94 | 95 | public boolean hasFormatting() { 96 | return color != null 97 | || bold != null 98 | || italic != null 99 | || underlined != null 100 | || strikethrough != null 101 | || obfuscated != null 102 | || (subComponents != null && !subComponents.isEmpty()); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/chat/ComponentSerializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.chat; 18 | 19 | import com.google.gson.*; 20 | 21 | import java.lang.reflect.Type; 22 | 23 | class ComponentSerializer implements JsonSerializer, JsonDeserializer { 24 | @Override 25 | public Component deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { 26 | if (jsonElement.isJsonArray()) { 27 | TextComponent component = new TextComponent(""); 28 | for (Component c : jsonDeserializationContext.deserialize(jsonElement.getAsJsonArray(), Component[].class)) { 29 | component.addComponent(c); 30 | } 31 | return component; 32 | } else if (jsonElement.isJsonObject()) { 33 | JsonObject object = jsonElement.getAsJsonObject(); 34 | Component component; 35 | if (object.has("text")) { 36 | component = new TextComponent(object.get("text").getAsString()); 37 | } else { 38 | throw new RuntimeException("Unhandled component"); 39 | } 40 | 41 | if (object.has("bold")) { 42 | component.bold = object.get("bold").getAsBoolean(); 43 | } 44 | if (object.has("italic")) { 45 | component.italic = object.get("italic").getAsBoolean(); 46 | } 47 | if (object.has("underlined")) { 48 | component.underlined = object.get("underlined").getAsBoolean(); 49 | } 50 | if (object.has("strikethrough")) { 51 | component.strikethrough = object.get("strikethrough").getAsBoolean(); 52 | } 53 | if (object.has("obfuscated")) { 54 | component.obfuscated = object.get("obfuscated").getAsBoolean(); 55 | } 56 | if (object.has("color")) { 57 | component.color = Color.valueOf(object.get("color").getAsString().toUpperCase()); 58 | } 59 | 60 | if (object.has("extra")) { 61 | for (Component c : jsonDeserializationContext.deserialize(object.getAsJsonArray("extra"), Component[].class)) { 62 | component.addComponent(c); 63 | } 64 | } 65 | } 66 | throw new RuntimeException("Unhandled component"); 67 | } 68 | 69 | @Override 70 | public JsonElement serialize(Component component, Type type, JsonSerializationContext jsonSerializationContext) { 71 | if (component instanceof TextComponent && !component.hasFormatting()) { 72 | return new JsonPrimitive(((TextComponent) component).getText()); 73 | } 74 | JsonObject jsonObject = new JsonObject(); 75 | if (component instanceof TextComponent) { 76 | jsonObject.addProperty("text", ((TextComponent) component).getText()); 77 | } 78 | 79 | if (component.bold != null) { 80 | jsonObject.addProperty("bold", component.bold); 81 | } 82 | if (component.italic != null) { 83 | jsonObject.addProperty("italic", component.italic); 84 | } 85 | if (component.underlined != null) { 86 | jsonObject.addProperty("underlined", component.underlined); 87 | } 88 | if (component.strikethrough != null) { 89 | jsonObject.addProperty("strikethrough", component.strikethrough); 90 | } 91 | if (component.obfuscated != null) { 92 | jsonObject.addProperty("obfuscated", component.obfuscated); 93 | } 94 | if (component.color != null) { 95 | jsonObject.addProperty("color", component.color.toString().toLowerCase()); 96 | } 97 | if (component.subComponents != null) { 98 | JsonArray array = new JsonArray(); 99 | for (Component c : component.subComponents) { 100 | array.add(jsonSerializationContext.serialize(c, Component.class)); 101 | } 102 | jsonObject.add("extra", array); 103 | } 104 | return jsonObject; 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/chat/RootComponent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.chat; 18 | 19 | class RootComponent extends Component { 20 | 21 | public final static RootComponent INSTANCE = new RootComponent(); 22 | 23 | RootComponent() { 24 | bold = false; 25 | italic = false; 26 | underlined = false; 27 | strikethrough = false; 28 | obfuscated = false; 29 | color = Color.WHITE; 30 | parent = null; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/chat/TextComponent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.chat; 18 | 19 | public class TextComponent extends Component { 20 | 21 | private String text; 22 | 23 | public TextComponent(String text) { 24 | this.text = text; 25 | } 26 | 27 | public String getText() { 28 | return text; 29 | } 30 | 31 | public void setText(String text) { 32 | this.text = text; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/entity/Entity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.entity; 18 | 19 | import uk.co.thinkofdeath.prismarine.entity.metadata.MetaKey; 20 | 21 | public class Entity { 22 | 23 | public static MetaKey ENTITY_FLAGS = MetaKey.define(0, (byte) 0); 24 | } 25 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/entity/metadata/EntityMetadata.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.entity.metadata; 18 | 19 | import io.netty.handler.codec.DecoderException; 20 | import uk.co.thinkofdeath.prismarine.item.ItemStack; 21 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 22 | import uk.co.thinkofdeath.prismarine.util.IntMap; 23 | 24 | import java.util.HashMap; 25 | import java.util.Map; 26 | 27 | public class EntityMetadata { 28 | 29 | private static final Map, Integer> classIdMap = new HashMap<>(); 30 | private IntMap map = new IntMap<>(32); 31 | 32 | public void register(MetaKey key) { 33 | register(key, key.getDefault()); 34 | } 35 | 36 | public void register(MetaKey key, T value) { 37 | if (map.contains(key.getId())) { 38 | throw new IllegalArgumentException(key.getId() + " is already in use"); 39 | } 40 | map.put(key.getId(), new MetaEntry(key.getId(), value)); 41 | } 42 | 43 | public void update(MetaKey key, T value) { 44 | MetaEntry entry = map.get(key.getId()); 45 | if (entry.value == null || !entry.equals(value)) { 46 | entry.value = value; 47 | entry.dirty = true; 48 | } 49 | } 50 | 51 | @SuppressWarnings("unchecked") 52 | public T get(MetaKey key) { 53 | return (T) map.get(key.getId()).value; 54 | } 55 | 56 | public byte getByte(MetaKey key) { 57 | return (byte) map.get(key.getId()).value; 58 | } 59 | 60 | public short getShort(MetaKey key) { 61 | return (short) map.get(key.getId()).value; 62 | } 63 | 64 | public int getInt(MetaKey key) { 65 | return (int) map.get(key.getId()).value; 66 | } 67 | 68 | public float getFloat(MetaKey key) { 69 | return (float) map.get(key.getId()).value; 70 | } 71 | 72 | public void write(MCByteBuf buf) { 73 | map.forEach((v, k) -> { 74 | int type = classIdMap.get(v.getClass()); 75 | buf.writeByte(k | (type << 5)); 76 | switch (type) { 77 | case 0: 78 | buf.writeByte((byte) v.value); 79 | break; 80 | case 1: 81 | buf.writeShort((short) v.value); 82 | break; 83 | case 2: 84 | buf.writeInt((int) v.value); 85 | break; 86 | case 3: 87 | buf.writeFloat((float) v.value); 88 | break; 89 | case 4: 90 | buf.writeString((String) v.value); 91 | break; 92 | case 5: 93 | buf.writeItemStack((ItemStack) v.value); 94 | break; 95 | } 96 | }); 97 | buf.writeByte(0x7F); 98 | } 99 | 100 | public void read(MCByteBuf buf) { 101 | while (true) { 102 | int item = buf.readUnsignedByte(); 103 | if (item == 0x7F) { 104 | break; 105 | } 106 | int id = item & 0x1F; 107 | int type = item >> 5; 108 | 109 | Object val; 110 | switch (type) { 111 | case 0: 112 | val = buf.readByte(); 113 | break; 114 | case 1: 115 | val = buf.readShort(); 116 | break; 117 | case 2: 118 | val = buf.readInt(); 119 | break; 120 | case 3: 121 | val = buf.readFloat(); 122 | break; 123 | case 4: 124 | val = buf.readString(Short.MAX_VALUE); 125 | break; 126 | case 5: 127 | val = buf.readItemStack(); 128 | break; 129 | default: 130 | throw new DecoderException("Unknown metadata type " + type); 131 | } 132 | MetaEntry entry; 133 | if (map.contains(id)) { 134 | entry = map.get(id); 135 | } else { 136 | entry = new MetaEntry(id, val); 137 | map.put(id, entry); 138 | } 139 | entry.value = val; 140 | entry.dirty = true; 141 | } 142 | } 143 | 144 | static { 145 | classIdMap.put(Byte.class, 0); 146 | classIdMap.put(Short.class, 1); 147 | classIdMap.put(Integer.class, 2); 148 | classIdMap.put(Float.class, 3); 149 | classIdMap.put(String.class, 4); 150 | classIdMap.put(ItemStack.class, 5); 151 | } 152 | } 153 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/entity/metadata/MetaEntry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.entity.metadata; 18 | 19 | class MetaEntry { 20 | 21 | final int id; 22 | Object value; 23 | boolean dirty; 24 | 25 | MetaEntry(int id, Object value) { 26 | this.id = id; 27 | this.value = value; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/entity/metadata/MetaKey.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.entity.metadata; 18 | 19 | public class MetaKey { 20 | 21 | private final int id; 22 | private final T def; 23 | 24 | private MetaKey(int id, T def) { 25 | this.id = id; 26 | this.def = def; 27 | } 28 | 29 | public static MetaKey define(int id, T def) { 30 | return new MetaKey<>(id, def); 31 | } 32 | 33 | int getId() { 34 | return id; 35 | } 36 | 37 | T getDefault() { 38 | return def; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/game/Difficulty.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.game; 18 | 19 | public enum Difficulty { 20 | PEACEFUL, 21 | EASY, 22 | NORMAL, 23 | HARD 24 | } 25 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/game/Dimension.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.game; 18 | 19 | public enum Dimension { 20 | OVERWORLD(0), 21 | NETHER(-1), 22 | END(1); 23 | 24 | private final int id; 25 | 26 | Dimension(int id) { 27 | this.id = id; 28 | } 29 | 30 | public int getId() { 31 | return id; 32 | } 33 | 34 | public static Dimension byId(int b) { 35 | for (Dimension dimension : values()) { 36 | if (dimension.getId() == b) { 37 | return dimension; 38 | } 39 | } 40 | return null; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/game/GameMode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.game; 18 | 19 | public enum GameMode { 20 | SURVIVAL, 21 | CREATIVE, 22 | ADVENTURE, 23 | SPECTATOR, 24 | } 25 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/game/Position.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.game; 18 | 19 | public class Position { 20 | 21 | private final int x; 22 | private final int y; 23 | private final int z; 24 | 25 | public Position(int x, int y, int z) { 26 | this.x = x; 27 | this.y = y; 28 | this.z = z; 29 | } 30 | 31 | public static Position fromLong(long val) { 32 | return new Position( 33 | (int) (val >> 38), 34 | (int) (val << 26 >> 52), 35 | (int) (val << 38 >> 38) 36 | ); 37 | } 38 | 39 | public int getX() { 40 | return x; 41 | } 42 | 43 | public int getY() { 44 | return y; 45 | } 46 | 47 | public int getZ() { 48 | return z; 49 | } 50 | 51 | public long toLong() { 52 | return ((long) (x & 0x3FFFFFF) << 38) | ((long) (y & 0xFFF) << 26) | (long) (z & 0x3FFFFFF); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/item/Item.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.item; 18 | 19 | public class Item { 20 | } 21 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/item/ItemRegistry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.item; 18 | 19 | import uk.co.thinkofdeath.prismarine.registry.Registry; 20 | 21 | import java.util.HashMap; 22 | import java.util.Map; 23 | 24 | public class ItemRegistry extends Registry { 25 | 26 | private Map nameMap = new HashMap<>(); 27 | 28 | public ItemRegistry() { 29 | 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/item/ItemStack.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.item; 18 | 19 | public class ItemStack { 20 | 21 | private Item item; 22 | private int damage; 23 | private int count; 24 | 25 | public ItemStack(Item item, int damage, int count) { 26 | this.item = item; 27 | this.damage = damage; 28 | this.count = count; 29 | } 30 | 31 | public ItemStack duplicate() { 32 | return new ItemStack(item, damage, count); 33 | } 34 | 35 | public Item getItem() { 36 | return item; 37 | } 38 | 39 | public void setItem(Item item) { 40 | this.item = item; 41 | } 42 | 43 | public int getDamage() { 44 | return damage; 45 | } 46 | 47 | public void setDamage(int damage) { 48 | this.damage = damage; 49 | } 50 | 51 | public int getCount() { 52 | return count; 53 | } 54 | 55 | public void setCount(int count) { 56 | this.count = count; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/log/LogUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.log; 18 | 19 | import java.util.logging.Logger; 20 | 21 | public class LogUtil { 22 | 23 | public static Logger get(Class clazz) { 24 | return Logger.getLogger(clazz.getSimpleName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/CipherCodec.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import io.netty.channel.ChannelHandlerContext; 21 | import io.netty.handler.codec.ByteToMessageCodec; 22 | 23 | import javax.crypto.Cipher; 24 | import javax.crypto.NoSuchPaddingException; 25 | import javax.crypto.SecretKey; 26 | import javax.crypto.spec.IvParameterSpec; 27 | import java.security.InvalidAlgorithmParameterException; 28 | import java.security.InvalidKeyException; 29 | import java.security.NoSuchAlgorithmException; 30 | import java.util.List; 31 | 32 | /** 33 | * Ciphers the stream with a AES/CFB8/NoPadding cipher, using the 34 | * secret key provided 35 | */ 36 | public class CipherCodec extends ByteToMessageCodec { 37 | 38 | private Cipher cipherEncrypt; 39 | private Cipher cipherDecrypt; 40 | 41 | private byte[] encryptBuffer = new byte[8192]; 42 | private byte[] dataBuffer = new byte[8192]; 43 | private byte[] deDataBuffer = new byte[8192]; 44 | 45 | /** 46 | * Creates a CipherCodec using the provided secret key 47 | * 48 | * @param secretKey 49 | * the secret key 50 | */ 51 | public CipherCodec(SecretKey secretKey) { 52 | try { 53 | cipherEncrypt = Cipher.getInstance("AES/CFB8/NoPadding"); 54 | cipherEncrypt.init(Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(secretKey.getEncoded())); 55 | 56 | cipherDecrypt = Cipher.getInstance("AES/CFB8/NoPadding"); 57 | cipherDecrypt.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(secretKey.getEncoded())); 58 | } catch (NoSuchAlgorithmException 59 | | NoSuchPaddingException 60 | | InvalidAlgorithmParameterException 61 | | InvalidKeyException e) { 62 | throw new RuntimeException(e); 63 | } 64 | } 65 | 66 | @Override 67 | protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception { 68 | byte[] data; 69 | int offset = 0; 70 | int dataSize; 71 | if (!msg.isDirect()) { 72 | data = msg.array(); 73 | offset = msg.arrayOffset(); 74 | dataSize = msg.readableBytes(); 75 | msg.skipBytes(msg.readableBytes()); 76 | } else { 77 | dataSize = msg.readableBytes(); 78 | if (dataBuffer.length < dataSize) { 79 | dataBuffer = new byte[dataSize]; 80 | } 81 | msg.readBytes(dataBuffer, 0, dataSize); 82 | data = dataBuffer; 83 | } 84 | int size = cipherEncrypt.getOutputSize(msg.readableBytes()); 85 | if (encryptBuffer.length < size) { 86 | encryptBuffer = new byte[size]; 87 | } 88 | int count = cipherEncrypt.update(data, offset, dataSize, encryptBuffer); 89 | out.writeBytes(encryptBuffer, 0, count); 90 | } 91 | 92 | @Override 93 | protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { 94 | byte[] data; 95 | int offset = 0; 96 | int dataSize = in.readableBytes(); 97 | if (!in.isDirect()) { 98 | data = in.array(); 99 | offset = in.arrayOffset(); 100 | in.skipBytes(in.readableBytes()); 101 | } else { 102 | if (deDataBuffer.length < dataSize) { 103 | deDataBuffer = new byte[dataSize]; 104 | } 105 | in.readBytes(deDataBuffer, 0, dataSize); 106 | data = deDataBuffer; 107 | } 108 | 109 | int size = cipherDecrypt.getOutputSize(dataSize); 110 | ByteBuf buf = ctx.alloc().heapBuffer(size); 111 | buf.writerIndex(cipherDecrypt.update(data, offset, dataSize, buf.array(), buf.arrayOffset())); 112 | out.add(buf); 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/CompressionCodec.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import io.netty.channel.ChannelHandlerContext; 21 | import io.netty.handler.codec.ByteToMessageCodec; 22 | 23 | import java.util.List; 24 | import java.util.zip.Deflater; 25 | import java.util.zip.Inflater; 26 | 27 | /** 28 | * The compression codec will compress/decompress packets that fall within 29 | * the set threshold 30 | */ 31 | public class CompressionCodec extends ByteToMessageCodec { 32 | 33 | private int threshold; 34 | private ThreadLocal info = new ThreadLocal() { 35 | 36 | @Override 37 | protected CompressionInfo initialValue() { 38 | return new CompressionInfo(); 39 | } 40 | }; 41 | 42 | @Override 43 | protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception { 44 | MCByteBuf buf = new MCByteBuf(out); 45 | if (msg.readableBytes() < threshold) { 46 | buf.writeVarInt(0); 47 | buf.writeBytes(msg); 48 | } else { 49 | CompressionInfo ci = info.get(); 50 | 51 | byte[] data; 52 | int offset = 0; 53 | int dataSize; 54 | // Handle both direct and heap buffers 55 | // heap buffers are faster in this case as they 56 | // do not require a copy 57 | if (!msg.isDirect()) { 58 | data = msg.array(); 59 | offset = msg.arrayOffset(); 60 | dataSize = msg.readableBytes(); 61 | msg.skipBytes(msg.readableBytes()); 62 | } else { 63 | dataSize = msg.readableBytes(); 64 | if (ci.dataBuffer.length < dataSize) { 65 | ci.dataBuffer = new byte[dataSize]; 66 | } 67 | msg.readBytes(ci.dataBuffer, 0, dataSize); 68 | data = ci.dataBuffer; 69 | } 70 | 71 | buf.writeVarInt(dataSize); 72 | 73 | ci.deflater.setInput(data, offset, dataSize); 74 | ci.deflater.finish(); 75 | while (!ci.deflater.finished()) { 76 | int count = ci.deflater.deflate(ci.compBuffer); 77 | out.writeBytes(ci.compBuffer, 0, count); 78 | } 79 | ci.deflater.reset(); 80 | } 81 | } 82 | 83 | @Override 84 | protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { 85 | MCByteBuf buf = new MCByteBuf(in); 86 | int size = buf.readVarInt(); 87 | if (size == 0) { 88 | out.add(buf.readBytes(buf.readableBytes())); 89 | } else { 90 | CompressionInfo ci = info.get(); 91 | if (ci.decompBuffer.length < in.readableBytes()) { 92 | ci.decompBuffer = new byte[in.readableBytes()]; 93 | } 94 | int count = in.readableBytes(); 95 | in.readBytes(ci.decompBuffer, 0, count); 96 | ci.inflater.setInput(ci.decompBuffer, 0, count); 97 | 98 | // Use heap buffers so we can just access the internal array 99 | ByteBuf oBuf = ctx.alloc().heapBuffer(size); 100 | oBuf.writerIndex(ci.inflater.inflate(oBuf.array(), oBuf.arrayOffset(), size)); 101 | out.add(oBuf); 102 | ci.inflater.reset(); 103 | } 104 | } 105 | 106 | /** 107 | * Gets the current threshold 108 | * 109 | * @return the threshold 110 | */ 111 | public int getThreshold() { 112 | return threshold; 113 | } 114 | 115 | /** 116 | * Sets the new threshold for the codec 117 | * 118 | * @param threshold 119 | * the new threshold 120 | */ 121 | public void setThreshold(int threshold) { 122 | this.threshold = threshold; 123 | } 124 | 125 | // Reusable buffers 126 | private static class CompressionInfo { 127 | 128 | public Inflater inflater = new Inflater(); 129 | public Deflater deflater = new Deflater(); 130 | public byte[] dataBuffer = new byte[8192]; 131 | public byte[] compBuffer = new byte[8192]; 132 | public byte[] decompBuffer = new byte[8192]; 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/ConnectionInitializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network; 18 | 19 | import io.netty.channel.ChannelInitializer; 20 | import io.netty.channel.ChannelPipeline; 21 | import io.netty.channel.socket.SocketChannel; 22 | import io.netty.handler.timeout.ReadTimeoutHandler; 23 | import uk.co.thinkofdeath.prismarine.log.LogUtil; 24 | import uk.co.thinkofdeath.prismarine.network.protocol.PacketHandler; 25 | import uk.co.thinkofdeath.prismarine.network.protocol.Protocol; 26 | 27 | import java.util.function.Supplier; 28 | import java.util.logging.Logger; 29 | 30 | /** 31 | * Initializes the connection for new clients 32 | */ 33 | public class ConnectionInitializer extends ChannelInitializer { 34 | 35 | private static final Logger logger = LogUtil.get(ConnectionInitializer.class); 36 | private final NetworkManager networkManager; 37 | private final Supplier initialHandler; 38 | 39 | /** 40 | * Creates a ConnectionInitializer which will setup the pipeline 41 | * for the channel. The PacketCodec will use the handler created 42 | * by the initialHandler supplier 43 | * 44 | * @param networkManager 45 | * the network manager which owns this 46 | * @param initialHandler 47 | * the supplier which creates the initial packet handler 48 | */ 49 | public ConnectionInitializer(NetworkManager networkManager, Supplier initialHandler) { 50 | this.networkManager = networkManager; 51 | this.initialHandler = initialHandler; 52 | } 53 | 54 | @Override 55 | protected void initChannel(SocketChannel ch) throws Exception { 56 | logger.info("Connection(" + ch.remoteAddress() + ")"); 57 | 58 | ChannelPipeline pipeline = ch.pipeline(); 59 | pipeline.addLast("timeout", new ReadTimeoutHandler(30)); 60 | pipeline.addLast("frame-codec", new VarIntFrameCodec()); 61 | pipeline.addLast("packet-codec", new PacketCodec(Protocol.HANDSHAKING, networkManager.getIncomingPacketType())); 62 | pipeline.addLast("handler", new NetworkHandler(networkManager, ch, initialHandler.get())); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/Constants.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network; 18 | 19 | /** 20 | * Useful network constants 21 | */ 22 | public class Constants { 23 | /** 24 | * The currently supported Minecraft version 25 | */ 26 | public static final String MINECRAFT_VERSION = "14w34b"; 27 | /** 28 | * The currently supported protocol version 29 | */ 30 | public static final int PROTOCOL_VERSION = 41; 31 | } 32 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/NetworkManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network; 18 | 19 | import io.netty.bootstrap.ServerBootstrap; 20 | import io.netty.buffer.PooledByteBufAllocator; 21 | import io.netty.channel.Channel; 22 | import io.netty.channel.ChannelOption; 23 | import io.netty.channel.EventLoopGroup; 24 | import io.netty.channel.nio.NioEventLoopGroup; 25 | import io.netty.channel.socket.nio.NioServerSocketChannel; 26 | import uk.co.thinkofdeath.prismarine.Prismarine; 27 | import uk.co.thinkofdeath.prismarine.log.LogUtil; 28 | import uk.co.thinkofdeath.prismarine.network.protocol.PacketHandler; 29 | import uk.co.thinkofdeath.prismarine.network.protocol.ProtocolDirection; 30 | 31 | import java.security.KeyPair; 32 | import java.security.KeyPairGenerator; 33 | import java.security.NoSuchAlgorithmException; 34 | import java.util.function.Supplier; 35 | import java.util.logging.Level; 36 | import java.util.logging.Logger; 37 | 38 | /** 39 | * The core network handler. This handles listening for connections 40 | * (for a server) or connecting to a server (for clients) 41 | */ 42 | public class NetworkManager { 43 | 44 | private static final Logger logger = LogUtil.get(NetworkManager.class); 45 | private final Prismarine prismarine; 46 | private Channel channel; 47 | 48 | private boolean onlineMode = true; 49 | private KeyPair networkKeyPair; 50 | private ProtocolDirection incomingPacketType; 51 | 52 | /** 53 | * Creates a new Network Manager 54 | * 55 | * @param prismarine 56 | * the prismarine instance 57 | */ 58 | public NetworkManager(Prismarine prismarine) { 59 | this.prismarine = prismarine; 60 | } 61 | 62 | /** 63 | * Start listening on the specified address & port. The initial handler for 64 | * each connection will be created from the supplier 65 | * 66 | * @param address 67 | * the address of the server 68 | * @param port 69 | * the port of the server 70 | * @param initialHandler 71 | * the supplier which creates the initial packet handler 72 | */ 73 | public void listen(String address, int port, Supplier initialHandler) { 74 | // Listening == Server 75 | incomingPacketType = ProtocolDirection.SERVERBOUND; 76 | if (isOnlineMode()) { 77 | // Encryption is only enabled in online mode 78 | logger.info("Generating encryption keys"); 79 | try { 80 | KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA"); 81 | generator.initialize(1024); 82 | networkKeyPair = generator.generateKeyPair(); 83 | } catch (NoSuchAlgorithmException e) { 84 | logger.info("Failed to generate encryption keys"); 85 | throw new RuntimeException(e); 86 | } 87 | } 88 | 89 | logger.log(Level.INFO, "Starting on {0}:{1,number,#}", 90 | new Object[]{address, port}); 91 | 92 | final EventLoopGroup group = new NioEventLoopGroup(); 93 | ServerBootstrap bootstrap = new ServerBootstrap(); 94 | bootstrap.group(group) 95 | .channel(NioServerSocketChannel.class) 96 | .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) 97 | .childHandler(new ConnectionInitializer(this, initialHandler)) 98 | .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) 99 | .childOption(ChannelOption.TCP_NODELAY, true); 100 | 101 | channel = bootstrap.bind(address, port) 102 | .channel(); 103 | channel.closeFuture() 104 | .addListener(future -> group.shutdownGracefully()); 105 | 106 | } 107 | 108 | /** 109 | * Disconnects the server/client 110 | */ 111 | public void close() { 112 | logger.info("Disconnecting..."); 113 | channel.close().awaitUninterruptibly(); 114 | } 115 | 116 | /** 117 | * Returns whether the manager is in online mode or not 118 | * 119 | * @return whether this is in online mode 120 | */ 121 | public boolean isOnlineMode() { 122 | return onlineMode; 123 | } 124 | 125 | /** 126 | * Enables/Disables online mode for the manager 127 | * 128 | * @param onlineMode 129 | * the new online mode state 130 | */ 131 | public void setOnlineMode(boolean onlineMode) { 132 | this.onlineMode = onlineMode; 133 | } 134 | 135 | /** 136 | * Returns the key pair used for encryption. 137 | * This is null in offline mode 138 | * 139 | * @return the encryption key pair or null 140 | */ 141 | public KeyPair getNetworkKeyPair() { 142 | return networkKeyPair; 143 | } 144 | 145 | /** 146 | * Returns the incoming packet direction for the manager. 147 | * This is null if a connection hasn't been started yet 148 | * 149 | * @return the incoming packet direction or null 150 | */ 151 | public ProtocolDirection getIncomingPacketType() { 152 | return incomingPacketType; 153 | } 154 | 155 | public Prismarine getPrismarine() { 156 | return prismarine; 157 | } 158 | } 159 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/NullHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.protocol.PacketHandler; 20 | 21 | /** 22 | * For packets which don't need to be handled 23 | */ 24 | public class NullHandler implements PacketHandler { 25 | 26 | @Override 27 | public void setNetworkHandler(NetworkHandler handler) { 28 | 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/PacketCodec.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import io.netty.channel.ChannelHandlerContext; 21 | import io.netty.handler.codec.ByteToMessageCodec; 22 | import io.netty.handler.codec.DecoderException; 23 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 24 | import uk.co.thinkofdeath.prismarine.network.protocol.Protocol; 25 | import uk.co.thinkofdeath.prismarine.network.protocol.ProtocolDirection; 26 | 27 | import java.util.List; 28 | 29 | /** 30 | * Reads and writes Minecraft packets. Each packet in the Minecraft 31 | * protocol as a varint packet id at the front, this codec handles 32 | * selecting the right packet based on the id as well as prepending 33 | * the id to outgoing packets 34 | */ 35 | public class PacketCodec extends ByteToMessageCodec { 36 | 37 | private Protocol protocol; 38 | private final ProtocolDirection incomingPacketType; 39 | 40 | /** 41 | * Creates a PacketCodec which handles the set sub-protocol 42 | * (which may be changed later). The incomingPacketType is used 43 | * to know whether this is a client or a server. 44 | * 45 | * @param protocol 46 | * the initial sub-protocol 47 | * @param incomingPacketType 48 | * the direction of incoming packets 49 | */ 50 | public PacketCodec(Protocol protocol, ProtocolDirection incomingPacketType) { 51 | this.protocol = protocol; 52 | this.incomingPacketType = incomingPacketType; 53 | } 54 | 55 | @Override 56 | protected void encode(ChannelHandlerContext ctx, Packet msg, ByteBuf out) throws Exception { 57 | MCByteBuf buf = new MCByteBuf(out); 58 | buf.writeVarInt(protocol.getPacketId(msg)); 59 | msg.write(buf); 60 | } 61 | 62 | @Override 63 | protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { 64 | MCByteBuf buf = new MCByteBuf(in); 65 | int id = buf.readVarInt(); 66 | Packet packet = protocol.create(incomingPacketType, id); 67 | if (packet != null) { 68 | packet.read(buf); 69 | out.add(packet); 70 | if (in.readableBytes() > 0) { 71 | throw new DecoderException("Failed to read all bytes from " + id); 72 | } 73 | } else { 74 | // TODO: We need to handle all packets 75 | buf.skipBytes(buf.readableBytes()); 76 | } 77 | } 78 | 79 | /** 80 | * Sets the sub-protocol that this packet codec handles 81 | * 82 | * @param protocol 83 | * the sub-protocol 84 | */ 85 | public void setProtocol(Protocol protocol) { 86 | this.protocol = protocol; 87 | } 88 | 89 | /** 90 | * Returns the current protocol being used by this 91 | * packet codec 92 | * 93 | * @return the sub-protocol 94 | */ 95 | public Protocol getProtocol() { 96 | return protocol; 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/VarIntFrameCodec.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import io.netty.channel.ChannelHandlerContext; 21 | import io.netty.handler.codec.ByteToMessageCodec; 22 | import io.netty.handler.codec.DecoderException; 23 | 24 | import java.util.List; 25 | 26 | /** 27 | * A ByteToMessageCodec which reads a varint a the beginning of a packet 28 | * which states the packet's length. The codec also handles outgoing 29 | * packets by prepending a varint containing the packet's length 30 | */ 31 | public class VarIntFrameCodec extends ByteToMessageCodec { 32 | 33 | @Override 34 | protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception { 35 | MCByteBuf buf = new MCByteBuf(out); 36 | // Preallocate the required space 37 | buf.ensureWritable(sizeOf(msg.readableBytes()) + msg.readableBytes()); 38 | buf.writeVarInt(msg.readableBytes()); 39 | buf.writeBytes(msg); 40 | } 41 | 42 | @Override 43 | protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { 44 | // Return back to this point if the varint isn't complete 45 | in.markReaderIndex(); 46 | 47 | int val = 0; 48 | int bytes = 0; 49 | while (true) { 50 | if (!in.isReadable()) { 51 | in.resetReaderIndex(); 52 | return; 53 | } 54 | 55 | int b = in.readByte(); 56 | val |= (b & 0b01111111) << (bytes++ * 7); 57 | if (bytes >= 3) { // Smaller limit for packets 58 | throw new DecoderException("VarInt too big"); 59 | } 60 | 61 | // If the 8th bit is set then the varint continues 62 | if ((b & 0x80) == 0) { 63 | break; 64 | } 65 | } 66 | 67 | if (!in.isReadable(val)) { 68 | // Packet isn't complete yet 69 | in.resetReaderIndex(); 70 | return; 71 | } 72 | out.add(in.readBytes(val)); 73 | } 74 | 75 | /** 76 | * Returns the number of bytes required to represent the 77 | * integer as a varint 78 | * 79 | * @param i 80 | * the integer to find the size of 81 | * @return The number of bytes required 82 | */ 83 | private static int sizeOf(int i) { 84 | if ((i & ~0b1111111) == 0) return 1; 85 | if ((i & ~0b111111111111111) == 0) return 2; 86 | if ((i & ~0b11111111111111111111111) == 0) return 3; 87 | if ((i & ~0b1111111111111111111111111111111) == 0) return 4; 88 | return 5; 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/login/LoginResponse.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.login; 18 | 19 | import java.util.Arrays; 20 | 21 | public class LoginResponse { 22 | 23 | private String id; 24 | private String name; 25 | private Property[] properties; 26 | 27 | public String getId() { 28 | return id; 29 | } 30 | 31 | public String getName() { 32 | return name; 33 | } 34 | 35 | public Property[] getProperties() { 36 | return properties; 37 | } 38 | 39 | @Override 40 | public String toString() { 41 | return "LoginResponse{" + 42 | "id='" + id + '\'' + 43 | ", name='" + name + '\'' + 44 | ", properties=" + Arrays.toString(properties) + 45 | '}'; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/login/Property.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.login; 18 | 19 | public class Property { 20 | 21 | private String name; 22 | private String value; 23 | private String signature; 24 | 25 | public String getName() { 26 | return name; 27 | } 28 | 29 | public String getValue() { 30 | return value; 31 | } 32 | 33 | public String getSignature() { 34 | return signature; 35 | } 36 | 37 | @Override 38 | public String toString() { 39 | return "Property{" + 40 | "name='" + name + '\'' + 41 | ", value='" + value + '\'' + 42 | ", signature='" + signature + '\'' + 43 | '}'; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/ping/Ping.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.ping; 18 | 19 | import uk.co.thinkofdeath.prismarine.chat.Component; 20 | 21 | public class Ping { 22 | 23 | private final PingVersion version = new PingVersion(); 24 | private final PingPlayers players = new PingPlayers(); 25 | private Component description; 26 | 27 | public Component getDescription() { 28 | return description; 29 | } 30 | 31 | public void setDescription(Component description) { 32 | this.description = description; 33 | } 34 | 35 | public PingVersion getVersion() { 36 | return version; 37 | } 38 | 39 | public PingPlayers getPlayers() { 40 | return players; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/ping/PingPlayers.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.ping; 18 | 19 | public class PingPlayers { 20 | 21 | private int max; 22 | private int online; 23 | 24 | public int getMax() { 25 | return max; 26 | } 27 | 28 | public void setMax(int max) { 29 | this.max = max; 30 | } 31 | 32 | public int getOnline() { 33 | return online; 34 | } 35 | 36 | public void setOnline(int online) { 37 | this.online = online; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/ping/PingVersion.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.ping; 18 | 19 | public class PingVersion { 20 | 21 | private String name; 22 | private int protocol; 23 | 24 | public String getName() { 25 | return name; 26 | } 27 | 28 | public void setName(String name) { 29 | this.name = name; 30 | } 31 | 32 | public int getProtocol() { 33 | return protocol; 34 | } 35 | 36 | public void setProtocol(int protocol) { 37 | this.protocol = protocol; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/IHandshakingHandlerClientbound.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol; 18 | 19 | public interface IHandshakingHandlerClientbound extends PacketHandler { 20 | } 21 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/IHandshakingHandlerServerbound.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.protocol.handshaking.Handshake; 20 | 21 | public interface IHandshakingHandlerServerbound extends PacketHandler { 22 | void handle(Handshake handshake); 23 | } 24 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/ILoginHandlerClientbound.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.protocol.login.EncryptionRequest; 20 | import uk.co.thinkofdeath.prismarine.network.protocol.login.LoginDisconnect; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.login.LoginSuccess; 22 | import uk.co.thinkofdeath.prismarine.network.protocol.login.SetInitialCompression; 23 | 24 | public interface ILoginHandlerClientbound extends PacketHandler { 25 | void handle(EncryptionRequest encryptionRequest); 26 | 27 | void handle(LoginDisconnect loginDisconnect); 28 | 29 | void handle(LoginSuccess loginSuccess); 30 | 31 | void handle(SetInitialCompression setInitialCompression); 32 | } 33 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/ILoginHandlerServerbound.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.protocol.login.EncryptionResponse; 20 | import uk.co.thinkofdeath.prismarine.network.protocol.login.LoginStart; 21 | 22 | public interface ILoginHandlerServerbound extends PacketHandler { 23 | void handle(EncryptionResponse encryptionResponse); 24 | 25 | void handle(LoginStart loginStart); 26 | } 27 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/IPlayHandlerClientbound.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.protocol.play.*; 20 | 21 | public interface IPlayHandlerClientbound extends PacketHandler { 22 | void handle(JoinGame joinGame); 23 | 24 | void handle(KeepAlivePing keepAlivePing); 25 | 26 | void handle(PlayerTeleport playerTeleport); 27 | 28 | void handle(ServerMessage serverMessage); 29 | 30 | void handle(TimeUpdate timeUpdate); 31 | 32 | void handle(EntityEquipment entityEquipment); 33 | 34 | void handle(SpawnPosition spawnPosition); 35 | 36 | void handle(UpdateHealth updateHealth); 37 | 38 | void handle(Respawn respawn); 39 | 40 | void handle(SetHeldItem setHeldItem); 41 | 42 | void handle(UseBed useBed); 43 | 44 | void handle(Animation animation); 45 | 46 | void handle(SpawnPlayer spawnPlayer); 47 | 48 | void handle(CollectItem collectItem); 49 | 50 | void handle(SpawnObject spawnObject); 51 | 52 | void handle(SpawnLivingEntity spawnLivingEntity); 53 | 54 | void handle(SpawnPainting spawnPainting); 55 | 56 | void handle(SpawnExperienceOrb spawnExperienceOrb); 57 | 58 | void handle(EntityVelocity entityVelocity); 59 | 60 | void handle(DestroyEntities destroyEntities); 61 | 62 | void handle(Entity entity); 63 | 64 | void handle(EntityMove entityMove); 65 | 66 | void handle(EntityLook entityLook); 67 | 68 | void handle(EntityMoveLook entityMoveLook); 69 | 70 | void handle(EntityTeleport entityTeleport); 71 | 72 | void handle(EntityHeadLook entityHeadLook); 73 | 74 | void handle(EntityStatus entityStatus); 75 | 76 | void handle(EntityAttach entityAttach); 77 | 78 | void handle(EntitySetMetadata entitySetMetadata); 79 | 80 | void handle(EntityEffect entityEffect); 81 | 82 | void handle(EntityRemoveEffect entityRemoveEffect); 83 | 84 | void handle(SetExperience setExperience); 85 | } 86 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/IPlayHandlerServerbound.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.protocol.play.ChatMessage; 20 | import uk.co.thinkofdeath.prismarine.network.protocol.play.KeepAlivePong; 21 | 22 | public interface IPlayHandlerServerbound extends PacketHandler { 23 | void handle(KeepAlivePong keepAlivePong); 24 | 25 | void handle(ChatMessage chatMessage); 26 | } 27 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/IStatusHandlerClientbound.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.protocol.status.StatusPong; 20 | import uk.co.thinkofdeath.prismarine.network.protocol.status.StatusReponse; 21 | 22 | public interface IStatusHandlerClientbound extends PacketHandler { 23 | void handle(StatusPong statusPong); 24 | 25 | void handle(StatusReponse statusReponse); 26 | } 27 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/IStatusHandlerServerbound.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.protocol.status.StatusPing; 20 | import uk.co.thinkofdeath.prismarine.network.protocol.status.StatusRequest; 21 | 22 | public interface IStatusHandlerServerbound extends PacketHandler { 23 | void handle(StatusPing statusPing); 24 | 25 | void handle(StatusRequest statusRequest); 26 | } 27 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/Packet.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 20 | import uk.co.thinkofdeath.prismarine.util.Stringable; 21 | 22 | public interface Packet extends Stringable { 23 | 24 | void read(MCByteBuf buf); 25 | 26 | void write(MCByteBuf buf); 27 | 28 | void handle(H handler); 29 | } 30 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/PacketHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.NetworkHandler; 20 | 21 | public interface PacketHandler { 22 | 23 | void setNetworkHandler(NetworkHandler handler); 24 | } 25 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/ProtocolDirection.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol; 18 | 19 | public enum ProtocolDirection { 20 | SERVERBOUND, 21 | CLIENTBOUND 22 | } 23 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/handshaking/Handshake.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.handshaking; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 20 | import uk.co.thinkofdeath.prismarine.network.protocol.IHandshakingHandlerServerbound; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 22 | import uk.co.thinkofdeath.prismarine.network.protocol.Protocol; 23 | 24 | public class Handshake implements Packet { 25 | 26 | private int protocolVersion; 27 | private String address; 28 | private int port; 29 | private Protocol next; 30 | 31 | public Handshake() { 32 | } 33 | 34 | public Handshake(int protocolVersion, String address, int port, Protocol next) { 35 | this.protocolVersion = protocolVersion; 36 | this.address = address; 37 | this.port = port; 38 | this.next = next; 39 | } 40 | 41 | @Override 42 | public void read(MCByteBuf buf) { 43 | protocolVersion = buf.readVarInt(); 44 | address = buf.readString(255); 45 | port = buf.readUnsignedShort(); 46 | int n = buf.readVarInt(); 47 | for (Protocol protocol : Protocol.values()) { 48 | if (protocol.getId() == n) { 49 | next = protocol; 50 | break; 51 | } 52 | } 53 | } 54 | 55 | @Override 56 | public void write(MCByteBuf buf) { 57 | buf.writeVarInt(protocolVersion); 58 | buf.writeString(address); 59 | buf.writeShort(port); 60 | buf.writeVarInt(next.getId()); 61 | } 62 | 63 | @Override 64 | public void handle(IHandshakingHandlerServerbound handler) { 65 | handler.handle(this); 66 | } 67 | 68 | public int getProtocolVersion() { 69 | return protocolVersion; 70 | } 71 | 72 | public String getAddress() { 73 | return address; 74 | } 75 | 76 | public int getPort() { 77 | return port; 78 | } 79 | 80 | public Protocol getNext() { 81 | return next; 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/login/EncryptionRequest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.login; 18 | 19 | import io.netty.handler.codec.DecoderException; 20 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.ILoginHandlerClientbound; 22 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 23 | 24 | import java.security.KeyFactory; 25 | import java.security.NoSuchAlgorithmException; 26 | import java.security.PublicKey; 27 | import java.security.spec.InvalidKeySpecException; 28 | import java.security.spec.X509EncodedKeySpec; 29 | 30 | public class EncryptionRequest implements Packet { 31 | 32 | private String serverID; 33 | private PublicKey publicKey; 34 | private byte[] verifyToken; 35 | 36 | public EncryptionRequest() { 37 | } 38 | 39 | public EncryptionRequest(String serverID, PublicKey publicKey, byte[] verifyToken) { 40 | this.serverID = serverID; 41 | this.publicKey = publicKey; 42 | this.verifyToken = verifyToken; 43 | } 44 | 45 | @Override 46 | public void read(MCByteBuf buf) { 47 | serverID = buf.readString(40); 48 | try { 49 | publicKey = KeyFactory.getInstance("RSA") 50 | .generatePublic(new X509EncodedKeySpec(buf.readByteArray(Short.MAX_VALUE))); 51 | } catch (InvalidKeySpecException | NoSuchAlgorithmException e) { 52 | throw new DecoderException(e); 53 | } 54 | verifyToken = buf.readByteArray(Short.MAX_VALUE); 55 | } 56 | 57 | @Override 58 | public void write(MCByteBuf buf) { 59 | buf.writeString(serverID); 60 | buf.writeByteArray(publicKey.getEncoded()); 61 | buf.writeByteArray(verifyToken); 62 | } 63 | 64 | @Override 65 | public void handle(ILoginHandlerClientbound handler) { 66 | handler.handle(this); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/login/EncryptionResponse.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.login; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 20 | import uk.co.thinkofdeath.prismarine.network.protocol.ILoginHandlerServerbound; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 22 | 23 | public class EncryptionResponse implements Packet { 24 | 25 | private byte[] secretKey; 26 | private byte[] verifyToken; 27 | 28 | public EncryptionResponse() { 29 | } 30 | 31 | public EncryptionResponse(byte[] secretKey, byte[] verifyToken) { 32 | this.secretKey = secretKey; 33 | this.verifyToken = verifyToken; 34 | } 35 | 36 | @Override 37 | public void read(MCByteBuf buf) { 38 | secretKey = buf.readByteArray(128); 39 | verifyToken = buf.readByteArray(128); 40 | } 41 | 42 | @Override 43 | public void write(MCByteBuf buf) { 44 | buf.writeByteArray(secretKey); 45 | buf.writeByteArray(verifyToken); 46 | } 47 | 48 | @Override 49 | public void handle(ILoginHandlerServerbound handler) { 50 | handler.handle(this); 51 | } 52 | 53 | public byte[] getSecretKey() { 54 | return secretKey; 55 | } 56 | 57 | public byte[] getVerifyToken() { 58 | return verifyToken; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/login/LoginDisconnect.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.login; 18 | 19 | import uk.co.thinkofdeath.prismarine.chat.Component; 20 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.ILoginHandlerClientbound; 22 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 23 | 24 | public class LoginDisconnect implements Packet { 25 | 26 | private Component reason; 27 | 28 | public LoginDisconnect() { 29 | } 30 | 31 | public LoginDisconnect(Component reason) { 32 | this.reason = reason; 33 | } 34 | 35 | @Override 36 | public void read(MCByteBuf buf) { 37 | reason = buf.readChat(); 38 | } 39 | 40 | @Override 41 | public void write(MCByteBuf buf) { 42 | buf.writeChat(reason); 43 | } 44 | 45 | @Override 46 | public void handle(ILoginHandlerClientbound handler) { 47 | handler.handle(this); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/login/LoginStart.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.login; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 20 | import uk.co.thinkofdeath.prismarine.network.protocol.ILoginHandlerServerbound; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 22 | 23 | public class LoginStart implements Packet { 24 | 25 | private String username; 26 | 27 | public LoginStart() { 28 | } 29 | 30 | public LoginStart(String username) { 31 | this.username = username; 32 | } 33 | 34 | @Override 35 | public void read(MCByteBuf buf) { 36 | username = buf.readString(16); 37 | } 38 | 39 | @Override 40 | public void write(MCByteBuf buf) { 41 | buf.writeString(username); 42 | } 43 | 44 | @Override 45 | public void handle(ILoginHandlerServerbound handler) { 46 | handler.handle(this); 47 | } 48 | 49 | public String getUsername() { 50 | return username; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/login/LoginSuccess.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.login; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 20 | import uk.co.thinkofdeath.prismarine.network.protocol.ILoginHandlerClientbound; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 22 | 23 | public class LoginSuccess implements Packet { 24 | 25 | private String uuid; 26 | private String username; 27 | 28 | public LoginSuccess() { 29 | } 30 | 31 | public LoginSuccess(String uuid, String username) { 32 | this.uuid = uuid; 33 | this.username = username; 34 | } 35 | 36 | @Override 37 | public void read(MCByteBuf buf) { 38 | uuid = buf.readString(40); 39 | username = buf.readString(16); 40 | } 41 | 42 | @Override 43 | public void write(MCByteBuf buf) { 44 | buf.writeString(uuid); 45 | buf.writeString(username); 46 | } 47 | 48 | @Override 49 | public void handle(ILoginHandlerClientbound handler) { 50 | handler.handle(this); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/login/SetInitialCompression.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.login; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 20 | import uk.co.thinkofdeath.prismarine.network.protocol.ILoginHandlerClientbound; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 22 | 23 | public class SetInitialCompression implements Packet { 24 | 25 | private int threshold; 26 | 27 | public SetInitialCompression() { 28 | } 29 | 30 | public SetInitialCompression(int threshold) { 31 | this.threshold = threshold; 32 | } 33 | 34 | @Override 35 | public void read(MCByteBuf buf) { 36 | threshold = buf.readVarInt(); 37 | } 38 | 39 | @Override 40 | public void write(MCByteBuf buf) { 41 | buf.writeVarInt(threshold); 42 | } 43 | 44 | @Override 45 | public void handle(ILoginHandlerClientbound handler) { 46 | handler.handle(this); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/play/Animation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.play; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 20 | import uk.co.thinkofdeath.prismarine.network.protocol.IPlayHandlerClientbound; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 22 | 23 | public class Animation implements Packet { 24 | 25 | private int entityId; 26 | private int animation; 27 | 28 | public Animation() { 29 | } 30 | 31 | public Animation(int entityId, int animation) { 32 | this.entityId = entityId; 33 | this.animation = animation; 34 | } 35 | 36 | @Override 37 | public void read(MCByteBuf buf) { 38 | entityId = buf.readVarInt(); 39 | animation = buf.readUnsignedByte(); 40 | } 41 | 42 | @Override 43 | public void write(MCByteBuf buf) { 44 | buf.writeVarInt(entityId); 45 | buf.writeByte(animation); 46 | } 47 | 48 | @Override 49 | public void handle(IPlayHandlerClientbound handler) { 50 | handler.handle(this); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/play/ChatMessage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.play; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 20 | import uk.co.thinkofdeath.prismarine.network.protocol.IPlayHandlerServerbound; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 22 | 23 | public class ChatMessage implements Packet { 24 | 25 | private String message; 26 | 27 | public ChatMessage() { 28 | } 29 | 30 | public ChatMessage(String message) { 31 | this.message = message; 32 | } 33 | 34 | @Override 35 | public void read(MCByteBuf buf) { 36 | message = buf.readString(100); 37 | } 38 | 39 | @Override 40 | public void write(MCByteBuf buf) { 41 | buf.writeString(message); 42 | } 43 | 44 | @Override 45 | public void handle(IPlayHandlerServerbound handler) { 46 | handler.handle(this); 47 | } 48 | 49 | public String getMessage() { 50 | return message; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/play/CollectItem.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.play; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 20 | import uk.co.thinkofdeath.prismarine.network.protocol.IPlayHandlerClientbound; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 22 | 23 | public class CollectItem implements Packet { 24 | 25 | private int collectedId; 26 | private int collectorId; 27 | 28 | public CollectItem() { 29 | } 30 | 31 | public CollectItem(int collectedId, int collectorId) { 32 | this.collectedId = collectedId; 33 | this.collectorId = collectorId; 34 | } 35 | 36 | @Override 37 | public void read(MCByteBuf buf) { 38 | collectedId = buf.readVarInt(); 39 | collectorId = buf.readVarInt(); 40 | } 41 | 42 | @Override 43 | public void write(MCByteBuf buf) { 44 | buf.writeVarInt(collectedId); 45 | buf.writeVarInt(collectorId); 46 | } 47 | 48 | @Override 49 | public void handle(IPlayHandlerClientbound handler) { 50 | handler.handle(this); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/play/DestroyEntities.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.play; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 20 | import uk.co.thinkofdeath.prismarine.network.protocol.IPlayHandlerClientbound; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 22 | 23 | public class DestroyEntities implements Packet { 24 | 25 | private int[] ids; 26 | 27 | public DestroyEntities() { 28 | } 29 | 30 | public DestroyEntities(int[] ids) { 31 | this.ids = ids; 32 | } 33 | 34 | @Override 35 | public void read(MCByteBuf buf) { 36 | int count = buf.readVarInt(); 37 | ids = new int[count]; 38 | for (int i = 0; i < count; i++) { 39 | ids[i] = buf.readVarInt(); 40 | } 41 | } 42 | 43 | @Override 44 | public void write(MCByteBuf buf) { 45 | buf.writeVarInt(ids.length); 46 | for (int id : ids) { 47 | buf.writeVarInt(id); 48 | } 49 | } 50 | 51 | @Override 52 | public void handle(IPlayHandlerClientbound handler) { 53 | handler.handle(this); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/play/Entity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.play; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 20 | import uk.co.thinkofdeath.prismarine.network.protocol.IPlayHandlerClientbound; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 22 | 23 | public class Entity implements Packet { 24 | 25 | private int id; 26 | 27 | public Entity() { 28 | } 29 | 30 | public Entity(int id) { 31 | this.id = id; 32 | } 33 | 34 | @Override 35 | public void read(MCByteBuf buf) { 36 | id = buf.readVarInt(); 37 | } 38 | 39 | @Override 40 | public void write(MCByteBuf buf) { 41 | buf.writeVarInt(id); 42 | } 43 | 44 | @Override 45 | public void handle(IPlayHandlerClientbound handler) { 46 | handler.handle(this); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/play/EntityAttach.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.play; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 20 | import uk.co.thinkofdeath.prismarine.network.protocol.IPlayHandlerClientbound; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 22 | 23 | public class EntityAttach implements Packet { 24 | 25 | private int entityId; 26 | private int vehicleID; 27 | private boolean leash; 28 | 29 | public EntityAttach() { 30 | } 31 | 32 | public EntityAttach(int entityId, int vehicleID, boolean leash) { 33 | this.entityId = entityId; 34 | this.vehicleID = vehicleID; 35 | this.leash = leash; 36 | } 37 | 38 | @Override 39 | public void read(MCByteBuf buf) { 40 | entityId = buf.readInt(); 41 | vehicleID = buf.readInt(); 42 | leash = buf.readBoolean(); 43 | } 44 | 45 | @Override 46 | public void write(MCByteBuf buf) { 47 | buf.writeInt(entityId); 48 | buf.writeInt(vehicleID); 49 | buf.writeBoolean(leash); 50 | } 51 | 52 | @Override 53 | public void handle(IPlayHandlerClientbound handler) { 54 | handler.handle(this); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/play/EntityEffect.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.play; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 20 | import uk.co.thinkofdeath.prismarine.network.protocol.IPlayHandlerClientbound; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 22 | 23 | public class EntityEffect implements Packet { 24 | 25 | private int entityId; 26 | private int effectId; 27 | private int amplifier; 28 | private int duration; 29 | private boolean hideParticles; 30 | 31 | public EntityEffect() { 32 | } 33 | 34 | public EntityEffect(int entityId, int effectId, int amplifier, int duration, boolean hideParticles) { 35 | this.entityId = entityId; 36 | this.effectId = effectId; 37 | this.amplifier = amplifier; 38 | this.duration = duration; 39 | this.hideParticles = hideParticles; 40 | } 41 | 42 | @Override 43 | public void read(MCByteBuf buf) { 44 | entityId = buf.readVarInt(); 45 | effectId = buf.readByte(); 46 | amplifier = buf.readByte(); 47 | duration = buf.readVarInt(); 48 | hideParticles = buf.readBoolean(); 49 | } 50 | 51 | @Override 52 | public void write(MCByteBuf buf) { 53 | buf.writeVarInt(entityId); 54 | buf.writeByte(effectId); 55 | buf.writeByte(amplifier); 56 | buf.writeVarInt(duration); 57 | buf.writeBoolean(hideParticles); 58 | } 59 | 60 | @Override 61 | public void handle(IPlayHandlerClientbound handler) { 62 | handler.handle(this); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/play/EntityEquipment.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.play; 18 | 19 | import uk.co.thinkofdeath.prismarine.item.ItemStack; 20 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.IPlayHandlerClientbound; 22 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 23 | 24 | public class EntityEquipment implements Packet { 25 | 26 | private int entityId; 27 | private int slot; 28 | private ItemStack itemStack; 29 | 30 | public EntityEquipment() { 31 | } 32 | 33 | public EntityEquipment(int entityId, int slot, ItemStack itemStack) { 34 | this.entityId = entityId; 35 | this.slot = slot; 36 | this.itemStack = itemStack.duplicate(); 37 | } 38 | 39 | @Override 40 | public void read(MCByteBuf buf) { 41 | entityId = buf.readVarInt(); 42 | slot = buf.readShort(); 43 | itemStack = buf.readItemStack(); 44 | } 45 | 46 | @Override 47 | public void write(MCByteBuf buf) { 48 | buf.writeVarInt(entityId); 49 | buf.writeShort(slot); 50 | buf.writeItemStack(itemStack); 51 | } 52 | 53 | @Override 54 | public void handle(IPlayHandlerClientbound handler) { 55 | handler.handle(this); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/play/EntityHeadLook.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.play; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 20 | import uk.co.thinkofdeath.prismarine.network.protocol.IPlayHandlerClientbound; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 22 | 23 | public class EntityHeadLook implements Packet { 24 | 25 | private int entityId; 26 | private int yaw; 27 | 28 | public EntityHeadLook() { 29 | } 30 | 31 | public EntityHeadLook(int entityId, int yaw) { 32 | this.entityId = entityId; 33 | this.yaw = yaw; 34 | } 35 | 36 | @Override 37 | public void read(MCByteBuf buf) { 38 | entityId = buf.readVarInt(); 39 | yaw = buf.readByte(); 40 | } 41 | 42 | @Override 43 | public void write(MCByteBuf buf) { 44 | buf.writeVarInt(entityId); 45 | buf.writeByte(yaw); 46 | } 47 | 48 | @Override 49 | public void handle(IPlayHandlerClientbound handler) { 50 | handler.handle(this); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/play/EntityLook.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.play; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 20 | import uk.co.thinkofdeath.prismarine.network.protocol.IPlayHandlerClientbound; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 22 | 23 | public class EntityLook implements Packet { 24 | 25 | private int entityId; 26 | private int yaw; 27 | private int pitch; 28 | private boolean onGround; 29 | 30 | public EntityLook() { 31 | } 32 | 33 | public EntityLook(int entityId, int yaw, int pitch, boolean onGround) { 34 | this.entityId = entityId; 35 | this.yaw = yaw; 36 | this.pitch = pitch; 37 | this.onGround = onGround; 38 | } 39 | 40 | @Override 41 | public void read(MCByteBuf buf) { 42 | entityId = buf.readVarInt(); 43 | yaw = buf.readByte(); 44 | pitch = buf.readByte(); 45 | onGround = buf.readBoolean(); 46 | } 47 | 48 | @Override 49 | public void write(MCByteBuf buf) { 50 | buf.writeVarInt(entityId); 51 | buf.writeByte(yaw); 52 | buf.writeByte(pitch); 53 | buf.writeBoolean(onGround); 54 | } 55 | 56 | @Override 57 | public void handle(IPlayHandlerClientbound handler) { 58 | handler.handle(this); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/play/EntityMove.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.play; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 20 | import uk.co.thinkofdeath.prismarine.network.protocol.IPlayHandlerClientbound; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 22 | 23 | public class EntityMove implements Packet { 24 | 25 | private int entityId; 26 | private int dx; 27 | private int dy; 28 | private int dz; 29 | private boolean onGround; 30 | 31 | public EntityMove() { 32 | } 33 | 34 | public EntityMove(int entityId, int dx, int dy, int dz, boolean onGround) { 35 | this.entityId = entityId; 36 | this.dx = dx; 37 | this.dy = dy; 38 | this.dz = dz; 39 | this.onGround = onGround; 40 | } 41 | 42 | @Override 43 | public void read(MCByteBuf buf) { 44 | entityId = buf.readVarInt(); 45 | dx = buf.readByte(); 46 | dy = buf.readByte(); 47 | dz = buf.readByte(); 48 | onGround = buf.readBoolean(); 49 | } 50 | 51 | @Override 52 | public void write(MCByteBuf buf) { 53 | buf.writeVarInt(entityId); 54 | buf.writeByte(dx); 55 | buf.writeByte(dy); 56 | buf.writeByte(dz); 57 | buf.writeBoolean(onGround); 58 | } 59 | 60 | @Override 61 | public void handle(IPlayHandlerClientbound handler) { 62 | handler.handle(this); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/play/EntityMoveLook.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.play; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 20 | import uk.co.thinkofdeath.prismarine.network.protocol.IPlayHandlerClientbound; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 22 | 23 | public class EntityMoveLook implements Packet { 24 | 25 | private int entityId; 26 | private int dx; 27 | private int dy; 28 | private int dz; 29 | private int yaw; 30 | private int pitch; 31 | private boolean onGround; 32 | 33 | public EntityMoveLook() { 34 | } 35 | 36 | public EntityMoveLook(int entityId, int dx, int dy, int dz, int yaw, int pitch, boolean onGround) { 37 | this.entityId = entityId; 38 | this.dx = dx; 39 | this.dy = dy; 40 | this.dz = dz; 41 | this.yaw = yaw; 42 | this.pitch = pitch; 43 | this.onGround = onGround; 44 | } 45 | 46 | @Override 47 | public void read(MCByteBuf buf) { 48 | entityId = buf.readVarInt(); 49 | dx = buf.readByte(); 50 | dy = buf.readByte(); 51 | dz = buf.readByte(); 52 | yaw = buf.readByte(); 53 | pitch = buf.readByte(); 54 | onGround = buf.readBoolean(); 55 | } 56 | 57 | @Override 58 | public void write(MCByteBuf buf) { 59 | buf.writeVarInt(entityId); 60 | buf.writeByte(dx); 61 | buf.writeByte(dy); 62 | buf.writeByte(dz); 63 | buf.writeByte(yaw); 64 | buf.writeByte(pitch); 65 | buf.writeBoolean(onGround); 66 | } 67 | 68 | @Override 69 | public void handle(IPlayHandlerClientbound handler) { 70 | handler.handle(this); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/play/EntityRemoveEffect.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.play; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 20 | import uk.co.thinkofdeath.prismarine.network.protocol.IPlayHandlerClientbound; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 22 | 23 | public class EntityRemoveEffect implements Packet { 24 | 25 | private int entityId; 26 | private int effectId; 27 | 28 | public EntityRemoveEffect() { 29 | } 30 | 31 | public EntityRemoveEffect(int entityId, int effectId) { 32 | this.entityId = entityId; 33 | this.effectId = effectId; 34 | } 35 | 36 | @Override 37 | public void read(MCByteBuf buf) { 38 | entityId = buf.readVarInt(); 39 | effectId = buf.readByte(); 40 | } 41 | 42 | @Override 43 | public void write(MCByteBuf buf) { 44 | buf.writeVarInt(entityId); 45 | buf.writeByte(effectId); 46 | } 47 | 48 | @Override 49 | public void handle(IPlayHandlerClientbound handler) { 50 | handler.handle(this); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/play/EntitySetMetadata.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.play; 18 | 19 | import uk.co.thinkofdeath.prismarine.entity.metadata.EntityMetadata; 20 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.IPlayHandlerClientbound; 22 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 23 | 24 | public class EntitySetMetadata implements Packet { 25 | 26 | private int entityId; 27 | private EntityMetadata metadata; 28 | 29 | public EntitySetMetadata() { 30 | } 31 | 32 | public EntitySetMetadata(int entityId, EntityMetadata metadata) { 33 | this.entityId = entityId; 34 | this.metadata = metadata; 35 | } 36 | 37 | @Override 38 | public void read(MCByteBuf buf) { 39 | entityId = buf.readVarInt(); 40 | metadata = new EntityMetadata(); 41 | metadata.read(buf); 42 | } 43 | 44 | @Override 45 | public void write(MCByteBuf buf) { 46 | buf.writeVarInt(entityId); 47 | metadata.write(buf); 48 | } 49 | 50 | @Override 51 | public void handle(IPlayHandlerClientbound handler) { 52 | handler.handle(this); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/play/EntityStatus.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.play; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 20 | import uk.co.thinkofdeath.prismarine.network.protocol.IPlayHandlerClientbound; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 22 | 23 | public class EntityStatus implements Packet { 24 | 25 | private int entityId; 26 | private int status; 27 | 28 | public EntityStatus() { 29 | } 30 | 31 | public EntityStatus(int entityId, int status) { 32 | this.entityId = entityId; 33 | this.status = status; 34 | } 35 | 36 | @Override 37 | public void read(MCByteBuf buf) { 38 | entityId = buf.readInt(); 39 | status = buf.readByte(); 40 | } 41 | 42 | @Override 43 | public void write(MCByteBuf buf) { 44 | buf.writeInt(entityId); 45 | buf.writeByte(status); 46 | } 47 | 48 | @Override 49 | public void handle(IPlayHandlerClientbound handler) { 50 | handler.handle(this); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/play/EntityTeleport.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.play; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 20 | import uk.co.thinkofdeath.prismarine.network.protocol.IPlayHandlerClientbound; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 22 | 23 | public class EntityTeleport implements Packet { 24 | 25 | private int entityId; 26 | private int x; 27 | private int y; 28 | private int z; 29 | private int yaw; 30 | private int pitch; 31 | private boolean onGround; 32 | 33 | public EntityTeleport() { 34 | } 35 | 36 | public EntityTeleport(int entityId, int x, int y, int z, int yaw, int pitch, boolean onGround) { 37 | this.entityId = entityId; 38 | this.x = x; 39 | this.y = y; 40 | this.z = z; 41 | this.yaw = yaw; 42 | this.pitch = pitch; 43 | this.onGround = onGround; 44 | } 45 | 46 | @Override 47 | public void read(MCByteBuf buf) { 48 | entityId = buf.readVarInt(); 49 | x = buf.readInt(); 50 | y = buf.readInt(); 51 | z = buf.readInt(); 52 | yaw = buf.readByte(); 53 | pitch = buf.readByte(); 54 | onGround = buf.readBoolean(); 55 | } 56 | 57 | @Override 58 | public void write(MCByteBuf buf) { 59 | buf.writeVarInt(entityId); 60 | buf.writeInt(x); 61 | buf.writeInt(y); 62 | buf.writeInt(z); 63 | buf.writeByte(yaw); 64 | buf.writeByte(pitch); 65 | buf.writeBoolean(onGround); 66 | } 67 | 68 | @Override 69 | public void handle(IPlayHandlerClientbound handler) { 70 | handler.handle(this); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/play/EntityVelocity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.play; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 20 | import uk.co.thinkofdeath.prismarine.network.protocol.IPlayHandlerClientbound; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 22 | 23 | public class EntityVelocity implements Packet { 24 | 25 | private int entityId; 26 | private int velX; 27 | private int velY; 28 | private int velZ; 29 | 30 | public EntityVelocity() { 31 | } 32 | 33 | public EntityVelocity(int entityId, int velX, int velY, int velZ) { 34 | this.entityId = entityId; 35 | this.velX = velX; 36 | this.velY = velY; 37 | this.velZ = velZ; 38 | } 39 | 40 | @Override 41 | public void read(MCByteBuf buf) { 42 | entityId = buf.readVarInt(); 43 | velX = buf.readShort(); 44 | velY = buf.readShort(); 45 | velZ = buf.readShort(); 46 | } 47 | 48 | @Override 49 | public void write(MCByteBuf buf) { 50 | buf.writeVarInt(entityId); 51 | buf.writeShort(velX); 52 | buf.writeShort(velY); 53 | buf.writeShort(velZ); 54 | } 55 | 56 | @Override 57 | public void handle(IPlayHandlerClientbound handler) { 58 | handler.handle(this); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/play/JoinGame.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.play; 18 | 19 | import uk.co.thinkofdeath.prismarine.game.Difficulty; 20 | import uk.co.thinkofdeath.prismarine.game.Dimension; 21 | import uk.co.thinkofdeath.prismarine.game.GameMode; 22 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 23 | import uk.co.thinkofdeath.prismarine.network.protocol.IPlayHandlerClientbound; 24 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 25 | 26 | public class JoinGame implements Packet { 27 | 28 | private int entityId; 29 | private GameMode gameMode; 30 | private boolean hardcore; 31 | private Dimension dimension; 32 | private Difficulty difficulty; 33 | private int maxPlayers; 34 | private String levelType; 35 | private boolean reducedDebug; 36 | 37 | public JoinGame() { 38 | } 39 | 40 | public JoinGame(int entityId, GameMode gameMode, boolean hardcore, 41 | Dimension dimension, Difficulty difficulty, 42 | int maxPlayers, String levelType, boolean reducedDebug) { 43 | this.entityId = entityId; 44 | this.gameMode = gameMode; 45 | this.hardcore = hardcore; 46 | this.dimension = dimension; 47 | this.difficulty = difficulty; 48 | this.maxPlayers = maxPlayers; 49 | this.levelType = levelType; 50 | this.reducedDebug = reducedDebug; 51 | } 52 | 53 | @Override 54 | public void read(MCByteBuf buf) { 55 | entityId = buf.readInt(); 56 | int mode = buf.readUnsignedByte(); 57 | if ((mode & 0x8) != 0) { 58 | hardcore = true; 59 | } 60 | gameMode = GameMode.values()[mode & 0b111]; 61 | dimension = Dimension.byId(buf.readByte()); 62 | difficulty = Difficulty.values()[buf.readUnsignedByte()]; 63 | maxPlayers = buf.readUnsignedByte(); 64 | levelType = buf.readString(255); 65 | reducedDebug = buf.readBoolean(); 66 | } 67 | 68 | @Override 69 | public void write(MCByteBuf buf) { 70 | buf.writeInt(entityId); 71 | buf.writeByte(gameMode.ordinal() | (hardcore ? 0x8 : 0x0)); 72 | buf.writeByte(dimension.getId()); 73 | buf.writeByte(difficulty.ordinal()); 74 | buf.writeByte(maxPlayers); 75 | buf.writeString(levelType); 76 | buf.writeBoolean(reducedDebug); 77 | } 78 | 79 | @Override 80 | public void handle(IPlayHandlerClientbound handler) { 81 | handler.handle(this); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/play/KeepAlivePing.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.play; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 20 | import uk.co.thinkofdeath.prismarine.network.protocol.IPlayHandlerClientbound; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 22 | 23 | public class KeepAlivePing implements Packet { 24 | 25 | private int id; 26 | 27 | public KeepAlivePing() { 28 | } 29 | 30 | public KeepAlivePing(int id) { 31 | this.id = id; 32 | } 33 | 34 | @Override 35 | public void read(MCByteBuf buf) { 36 | id = buf.readVarInt(); 37 | } 38 | 39 | @Override 40 | public void write(MCByteBuf buf) { 41 | buf.writeVarInt(id); 42 | } 43 | 44 | @Override 45 | public void handle(IPlayHandlerClientbound handler) { 46 | handler.handle(this); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/play/KeepAlivePong.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.play; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 20 | import uk.co.thinkofdeath.prismarine.network.protocol.IPlayHandlerServerbound; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 22 | 23 | public class KeepAlivePong implements Packet { 24 | 25 | private int id; 26 | 27 | public KeepAlivePong() { 28 | } 29 | 30 | public KeepAlivePong(int id) { 31 | this.id = id; 32 | } 33 | 34 | @Override 35 | public void read(MCByteBuf buf) { 36 | id = buf.readVarInt(); 37 | } 38 | 39 | @Override 40 | public void write(MCByteBuf buf) { 41 | buf.writeVarInt(id); 42 | } 43 | 44 | @Override 45 | public void handle(IPlayHandlerServerbound handler) { 46 | handler.handle(this); 47 | } 48 | 49 | public int getId() { 50 | return id; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/play/PlayerTeleport.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.play; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 20 | import uk.co.thinkofdeath.prismarine.network.protocol.IPlayHandlerClientbound; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 22 | 23 | public class PlayerTeleport implements Packet { 24 | 25 | private double x; 26 | private double y; 27 | private double z; 28 | private float yaw; 29 | private float pitch; 30 | private int flags; 31 | 32 | public PlayerTeleport() { 33 | } 34 | 35 | public PlayerTeleport(double x, double y, double z, float yaw, float pitch, int flags) { 36 | this.x = x; 37 | this.y = y; 38 | this.z = z; 39 | this.yaw = yaw; 40 | this.pitch = pitch; 41 | this.flags = flags; 42 | } 43 | 44 | @Override 45 | public void read(MCByteBuf buf) { 46 | x = buf.readDouble(); 47 | y = buf.readDouble(); 48 | z = buf.readDouble(); 49 | yaw = buf.readFloat(); 50 | pitch = buf.readFloat(); 51 | flags = buf.readUnsignedByte(); 52 | } 53 | 54 | @Override 55 | public void write(MCByteBuf buf) { 56 | buf.writeDouble(x); 57 | buf.writeDouble(y); 58 | buf.writeDouble(z); 59 | buf.writeFloat(yaw); 60 | buf.writeFloat(pitch); 61 | buf.writeByte(flags); 62 | } 63 | 64 | @Override 65 | public void handle(IPlayHandlerClientbound handler) { 66 | handler.handle(this); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/play/Respawn.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.play; 18 | 19 | import uk.co.thinkofdeath.prismarine.game.Difficulty; 20 | import uk.co.thinkofdeath.prismarine.game.Dimension; 21 | import uk.co.thinkofdeath.prismarine.game.GameMode; 22 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 23 | import uk.co.thinkofdeath.prismarine.network.protocol.IPlayHandlerClientbound; 24 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 25 | 26 | public class Respawn implements Packet { 27 | 28 | private Dimension dimension; 29 | private Difficulty difficulty; 30 | private GameMode gameMode; 31 | private String levelType; 32 | 33 | public Respawn() { 34 | } 35 | 36 | public Respawn(Dimension dimension, Difficulty difficulty, GameMode gameMode, String levelType) { 37 | this.dimension = dimension; 38 | this.difficulty = difficulty; 39 | this.gameMode = gameMode; 40 | this.levelType = levelType; 41 | } 42 | 43 | @Override 44 | public void read(MCByteBuf buf) { 45 | dimension = Dimension.byId(buf.readInt()); 46 | gameMode = GameMode.values()[buf.readUnsignedByte()]; 47 | difficulty = Difficulty.values()[buf.readUnsignedByte()]; 48 | levelType = buf.readString(255); 49 | } 50 | 51 | @Override 52 | public void write(MCByteBuf buf) { 53 | buf.writeInt(dimension.getId()); 54 | buf.writeByte(gameMode.ordinal()); 55 | buf.writeByte(difficulty.ordinal()); 56 | buf.writeString(levelType); 57 | } 58 | 59 | @Override 60 | public void handle(IPlayHandlerClientbound handler) { 61 | handler.handle(this); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/play/ServerMessage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.play; 18 | 19 | import uk.co.thinkofdeath.prismarine.chat.Component; 20 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.IPlayHandlerClientbound; 22 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 23 | 24 | public class ServerMessage implements Packet { 25 | 26 | private Component message; 27 | private Type type; 28 | 29 | public ServerMessage() { 30 | } 31 | 32 | public ServerMessage(Component message, Type type) { 33 | this.message = message; 34 | this.type = type; 35 | } 36 | 37 | @Override 38 | public void read(MCByteBuf buf) { 39 | message = buf.readChat(); 40 | type = Type.values()[buf.readByte()]; 41 | } 42 | 43 | @Override 44 | public void write(MCByteBuf buf) { 45 | buf.writeChat(message); 46 | buf.writeByte(type.ordinal()); 47 | } 48 | 49 | @Override 50 | public void handle(IPlayHandlerClientbound handler) { 51 | handler.handle(this); 52 | } 53 | 54 | public static enum Type { 55 | CHAT, 56 | SYSTEM, 57 | NOTIFY 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/play/SetExperience.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.play; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 20 | import uk.co.thinkofdeath.prismarine.network.protocol.IPlayHandlerClientbound; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 22 | 23 | public class SetExperience implements Packet { 24 | 25 | private float barPosition; 26 | private int level; 27 | private int totalExp; 28 | 29 | public SetExperience() { 30 | } 31 | 32 | public SetExperience(float barPosition, int level, int totalExp) { 33 | this.barPosition = barPosition; 34 | this.level = level; 35 | this.totalExp = totalExp; 36 | } 37 | 38 | @Override 39 | public void read(MCByteBuf buf) { 40 | barPosition = buf.readFloat(); 41 | level = buf.readVarInt(); 42 | totalExp = buf.readVarInt(); 43 | } 44 | 45 | @Override 46 | public void write(MCByteBuf buf) { 47 | buf.writeFloat(barPosition); 48 | buf.writeVarInt(level); 49 | buf.writeVarInt(totalExp); 50 | } 51 | 52 | @Override 53 | public void handle(IPlayHandlerClientbound handler) { 54 | handler.handle(this); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/play/SetHeldItem.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.play; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 20 | import uk.co.thinkofdeath.prismarine.network.protocol.IPlayHandlerClientbound; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 22 | 23 | public class SetHeldItem implements Packet { 24 | 25 | private int slot; 26 | 27 | public SetHeldItem() { 28 | } 29 | 30 | public SetHeldItem(int slot) { 31 | this.slot = slot; 32 | } 33 | 34 | @Override 35 | public void read(MCByteBuf buf) { 36 | slot = buf.readByte(); 37 | } 38 | 39 | @Override 40 | public void write(MCByteBuf buf) { 41 | buf.writeByte(slot); 42 | } 43 | 44 | @Override 45 | public void handle(IPlayHandlerClientbound handler) { 46 | handler.handle(this); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/play/SpawnExperienceOrb.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.play; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 20 | import uk.co.thinkofdeath.prismarine.network.protocol.IPlayHandlerClientbound; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 22 | 23 | public class SpawnExperienceOrb implements Packet { 24 | 25 | private int entityId; 26 | private int x; 27 | private int y; 28 | private int z; 29 | private int count; 30 | 31 | public SpawnExperienceOrb() { 32 | } 33 | 34 | public SpawnExperienceOrb(int entityId, int x, int y, int z, int count) { 35 | this.entityId = entityId; 36 | this.x = x; 37 | this.y = y; 38 | this.z = z; 39 | this.count = count; 40 | } 41 | 42 | @Override 43 | public void read(MCByteBuf buf) { 44 | entityId = buf.readVarInt(); 45 | x = buf.readInt(); 46 | y = buf.readInt(); 47 | z = buf.readInt(); 48 | count = buf.readUnsignedShort(); 49 | } 50 | 51 | @Override 52 | public void write(MCByteBuf buf) { 53 | buf.writeVarInt(entityId); 54 | buf.writeInt(x); 55 | buf.writeInt(y); 56 | buf.writeInt(z); 57 | buf.writeShort(count); 58 | } 59 | 60 | @Override 61 | public void handle(IPlayHandlerClientbound handler) { 62 | handler.handle(this); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/play/SpawnLivingEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.play; 18 | 19 | import uk.co.thinkofdeath.prismarine.entity.metadata.EntityMetadata; 20 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.IPlayHandlerClientbound; 22 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 23 | 24 | public class SpawnLivingEntity implements Packet { 25 | 26 | private int entityId; 27 | private int type; 28 | private int x; 29 | private int y; 30 | private int z; 31 | private byte yaw; 32 | private byte pitch; 33 | private byte headYaw; 34 | private short velX; 35 | private short velY; 36 | private short velZ; 37 | private EntityMetadata metadata; 38 | 39 | public SpawnLivingEntity() { 40 | } 41 | 42 | public SpawnLivingEntity(int entityId, int type, int x, int y, int z, 43 | byte yaw, byte pitch, byte headYaw, 44 | short velX, short velY, short velZ, EntityMetadata metadata) { 45 | this.entityId = entityId; 46 | this.type = type; 47 | this.x = x; 48 | this.y = y; 49 | this.z = z; 50 | this.yaw = yaw; 51 | this.pitch = pitch; 52 | this.headYaw = headYaw; 53 | this.velX = velX; 54 | this.velY = velY; 55 | this.velZ = velZ; 56 | this.metadata = metadata; 57 | } 58 | 59 | @Override 60 | public void read(MCByteBuf buf) { 61 | entityId = buf.readVarInt(); 62 | type = buf.readUnsignedByte(); 63 | x = buf.readInt(); 64 | y = buf.readInt(); 65 | z = buf.readInt(); 66 | yaw = buf.readByte(); 67 | pitch = buf.readByte(); 68 | headYaw = buf.readByte(); 69 | velX = buf.readShort(); 70 | velY = buf.readShort(); 71 | velZ = buf.readShort(); 72 | metadata = new EntityMetadata(); 73 | metadata.read(buf); 74 | } 75 | 76 | @Override 77 | public void write(MCByteBuf buf) { 78 | buf.writeVarInt(entityId); 79 | buf.writeByte(type); 80 | buf.writeInt(x); 81 | buf.writeInt(y); 82 | buf.writeInt(z); 83 | buf.writeByte(yaw); 84 | buf.writeByte(pitch); 85 | buf.writeByte(headYaw); 86 | buf.writeShort(velX); 87 | buf.writeShort(velY); 88 | buf.writeShort(velZ); 89 | metadata.write(buf); 90 | } 91 | 92 | @Override 93 | public void handle(IPlayHandlerClientbound handler) { 94 | handler.handle(this); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/play/SpawnObject.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.play; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 20 | import uk.co.thinkofdeath.prismarine.network.protocol.IPlayHandlerClientbound; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 22 | 23 | public class SpawnObject implements Packet { 24 | 25 | private int entityId; 26 | private int type; 27 | private int x; 28 | private int y; 29 | private int z; 30 | private byte pitch; 31 | private byte yaw; 32 | private int objectData; 33 | private short velX; 34 | private short velY; 35 | private short velZ; 36 | 37 | public SpawnObject() { 38 | } 39 | 40 | public SpawnObject(int entityId, int type, int x, int y, int z, 41 | byte pitch, byte yaw, int objectData, 42 | short velX, short velY, short velZ) { 43 | this.entityId = entityId; 44 | this.type = type; 45 | this.x = x; 46 | this.y = y; 47 | this.z = z; 48 | this.pitch = pitch; 49 | this.yaw = yaw; 50 | this.objectData = objectData; 51 | this.velX = velX; 52 | this.velY = velY; 53 | this.velZ = velZ; 54 | } 55 | 56 | @Override 57 | public void read(MCByteBuf buf) { 58 | entityId = buf.readVarInt(); 59 | type = buf.readUnsignedByte(); 60 | x = buf.readInt(); 61 | y = buf.readInt(); 62 | z = buf.readInt(); 63 | pitch = buf.readByte(); 64 | yaw = buf.readByte(); 65 | objectData = buf.readInt(); 66 | if (objectData > 0) { 67 | velX = buf.readShort(); 68 | velY = buf.readShort(); 69 | velZ = buf.readShort(); 70 | } 71 | } 72 | 73 | @Override 74 | public void write(MCByteBuf buf) { 75 | buf.writeVarInt(entityId); 76 | buf.writeByte(type); 77 | buf.writeInt(x); 78 | buf.writeInt(y); 79 | buf.writeInt(z); 80 | buf.writeByte(pitch); 81 | buf.writeByte(yaw); 82 | buf.writeInt(objectData); 83 | if (objectData > 0) { 84 | buf.writeShort(velX); 85 | buf.writeShort(velY); 86 | buf.writeShort(velZ); 87 | } 88 | } 89 | 90 | @Override 91 | public void handle(IPlayHandlerClientbound handler) { 92 | handler.handle(this); 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/play/SpawnPainting.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.play; 18 | 19 | import uk.co.thinkofdeath.prismarine.game.Position; 20 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.IPlayHandlerClientbound; 22 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 23 | 24 | public class SpawnPainting implements Packet { 25 | 26 | private int entityId; 27 | private String title; 28 | private Position position; 29 | private int direction; 30 | 31 | public SpawnPainting() { 32 | } 33 | 34 | public SpawnPainting(int entityId, String title, Position position, int direction) { 35 | this.entityId = entityId; 36 | this.title = title; 37 | this.position = position; 38 | this.direction = direction; 39 | } 40 | 41 | @Override 42 | public void read(MCByteBuf buf) { 43 | entityId = buf.readVarInt(); 44 | title = buf.readString(13); 45 | position = buf.readPosition(); 46 | direction = buf.readUnsignedByte(); 47 | } 48 | 49 | @Override 50 | public void write(MCByteBuf buf) { 51 | buf.writeVarInt(entityId); 52 | buf.writeString(title); 53 | buf.writePosition(position); 54 | buf.writeByte(direction); 55 | } 56 | 57 | @Override 58 | public void handle(IPlayHandlerClientbound handler) { 59 | handler.handle(this); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/play/SpawnPlayer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.play; 18 | 19 | import uk.co.thinkofdeath.prismarine.Prismarine; 20 | import uk.co.thinkofdeath.prismarine.entity.metadata.EntityMetadata; 21 | import uk.co.thinkofdeath.prismarine.item.Item; 22 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 23 | import uk.co.thinkofdeath.prismarine.network.protocol.IPlayHandlerClientbound; 24 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 25 | 26 | import java.util.UUID; 27 | 28 | public class SpawnPlayer implements Packet { 29 | 30 | private int entityId; 31 | private UUID uuid; 32 | private int x; 33 | private int y; 34 | private int z; 35 | private byte yaw; 36 | private byte pitch; 37 | private Item currentItem; 38 | private EntityMetadata metadata; 39 | 40 | public SpawnPlayer() { 41 | } 42 | 43 | public SpawnPlayer(int entityId, UUID uuid, 44 | int x, int y, int z, byte yaw, byte pitch, 45 | Item currentItem, EntityMetadata metadata) { 46 | this.entityId = entityId; 47 | this.uuid = uuid; 48 | this.x = x; 49 | this.y = y; 50 | this.z = z; 51 | this.yaw = yaw; 52 | this.pitch = pitch; 53 | this.currentItem = currentItem; 54 | this.metadata = metadata; 55 | } 56 | 57 | @Override 58 | public void read(MCByteBuf buf) { 59 | entityId = buf.readVarInt(); 60 | uuid = buf.readUUID(); 61 | x = buf.readInt(); 62 | y = buf.readInt(); 63 | z = buf.readInt(); 64 | yaw = buf.readByte(); 65 | pitch = buf.readByte(); 66 | int item = buf.readUnsignedShort(); 67 | if (item != 0) { 68 | currentItem = Prismarine.getInstance().getItemRegistry().get(item); 69 | } 70 | metadata = new EntityMetadata(); 71 | metadata.read(buf); 72 | } 73 | 74 | @Override 75 | public void write(MCByteBuf buf) { 76 | buf.writeVarInt(entityId); 77 | buf.writeUUID(uuid); 78 | buf.writeInt(x); 79 | buf.writeInt(y); 80 | buf.writeInt(z); 81 | buf.writeByte(yaw); 82 | buf.writeByte(pitch); 83 | if (currentItem == null) { 84 | buf.writeShort(0); 85 | } else { 86 | buf.writeShort(Prismarine.getInstance().getItemRegistry().getId(currentItem)); 87 | } 88 | metadata.write(buf); 89 | } 90 | 91 | @Override 92 | public void handle(IPlayHandlerClientbound handler) { 93 | handler.handle(this); 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/play/SpawnPosition.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.play; 18 | 19 | import uk.co.thinkofdeath.prismarine.game.Position; 20 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.IPlayHandlerClientbound; 22 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 23 | 24 | public class SpawnPosition implements Packet { 25 | 26 | private Position position; 27 | 28 | public SpawnPosition() { 29 | } 30 | 31 | public SpawnPosition(Position position) { 32 | this.position = position; 33 | } 34 | 35 | @Override 36 | public void read(MCByteBuf buf) { 37 | position = buf.readPosition(); 38 | } 39 | 40 | @Override 41 | public void write(MCByteBuf buf) { 42 | buf.writePosition(position); 43 | } 44 | 45 | @Override 46 | public void handle(IPlayHandlerClientbound handler) { 47 | handler.handle(this); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/play/TimeUpdate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.play; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 20 | import uk.co.thinkofdeath.prismarine.network.protocol.IPlayHandlerClientbound; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 22 | 23 | public class TimeUpdate implements Packet { 24 | 25 | private long worldAge; 26 | private long time; 27 | 28 | public TimeUpdate() { 29 | } 30 | 31 | public TimeUpdate(long worldAge, long time) { 32 | this.worldAge = worldAge; 33 | this.time = time; 34 | } 35 | 36 | @Override 37 | public void read(MCByteBuf buf) { 38 | worldAge = buf.readLong(); 39 | time = buf.readLong(); 40 | } 41 | 42 | @Override 43 | public void write(MCByteBuf buf) { 44 | buf.writeLong(worldAge); 45 | buf.writeLong(time); 46 | } 47 | 48 | @Override 49 | public void handle(IPlayHandlerClientbound handler) { 50 | handler.handle(this); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/play/UpdateHealth.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.play; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 20 | import uk.co.thinkofdeath.prismarine.network.protocol.IPlayHandlerClientbound; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 22 | 23 | public class UpdateHealth implements Packet { 24 | 25 | private float health; 26 | private int food; 27 | private float saturation; 28 | 29 | public UpdateHealth() { 30 | } 31 | 32 | public UpdateHealth(float health, int food, float saturation) { 33 | this.health = health; 34 | this.food = food; 35 | this.saturation = saturation; 36 | } 37 | 38 | @Override 39 | public void read(MCByteBuf buf) { 40 | health = buf.readFloat(); 41 | food = buf.readVarInt(); 42 | saturation = buf.readFloat(); 43 | } 44 | 45 | @Override 46 | public void write(MCByteBuf buf) { 47 | buf.writeFloat(health); 48 | buf.writeVarInt(food); 49 | buf.writeFloat(saturation); 50 | } 51 | 52 | @Override 53 | public void handle(IPlayHandlerClientbound handler) { 54 | handler.handle(this); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/play/UseBed.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.play; 18 | 19 | import uk.co.thinkofdeath.prismarine.game.Position; 20 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.IPlayHandlerClientbound; 22 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 23 | 24 | public class UseBed implements Packet { 25 | 26 | private int entityId; 27 | private Position position; 28 | 29 | public UseBed() { 30 | } 31 | 32 | public UseBed(int entityId, Position position) { 33 | this.entityId = entityId; 34 | this.position = position; 35 | } 36 | 37 | @Override 38 | public void read(MCByteBuf buf) { 39 | entityId = buf.readVarInt(); 40 | position = buf.readPosition(); 41 | } 42 | 43 | @Override 44 | public void write(MCByteBuf buf) { 45 | buf.writeVarInt(entityId); 46 | buf.writePosition(position); 47 | } 48 | 49 | @Override 50 | public void handle(IPlayHandlerClientbound handler) { 51 | handler.handle(this); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/status/StatusPing.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.status; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 20 | import uk.co.thinkofdeath.prismarine.network.protocol.IStatusHandlerServerbound; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 22 | 23 | public class StatusPing implements Packet { 24 | 25 | private long time; 26 | 27 | public StatusPing() { 28 | } 29 | 30 | public StatusPing(long time) { 31 | this.time = time; 32 | } 33 | 34 | @Override 35 | public void read(MCByteBuf buf) { 36 | time = buf.readLong(); 37 | } 38 | 39 | @Override 40 | public void write(MCByteBuf buf) { 41 | buf.writeLong(time); 42 | } 43 | 44 | @Override 45 | public void handle(IStatusHandlerServerbound handler) { 46 | handler.handle(this); 47 | } 48 | 49 | public long getTime() { 50 | return time; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/status/StatusPong.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.status; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 20 | import uk.co.thinkofdeath.prismarine.network.protocol.IStatusHandlerClientbound; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 22 | 23 | public class StatusPong implements Packet { 24 | 25 | private long time; 26 | 27 | public StatusPong() { 28 | } 29 | 30 | public StatusPong(long time) { 31 | this.time = time; 32 | } 33 | 34 | @Override 35 | public void read(MCByteBuf buf) { 36 | time = buf.readLong(); 37 | } 38 | 39 | @Override 40 | public void write(MCByteBuf buf) { 41 | buf.writeLong(time); 42 | } 43 | 44 | @Override 45 | public void handle(IStatusHandlerClientbound handler) { 46 | handler.handle(this); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/status/StatusReponse.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.status; 18 | 19 | import com.google.gson.Gson; 20 | import com.google.gson.GsonBuilder; 21 | import uk.co.thinkofdeath.prismarine.chat.ChatSerializer; 22 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 23 | import uk.co.thinkofdeath.prismarine.network.ping.Ping; 24 | import uk.co.thinkofdeath.prismarine.network.protocol.IStatusHandlerClientbound; 25 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 26 | 27 | public class StatusReponse implements Packet { 28 | 29 | private static final Gson gson = ChatSerializer.attach(new GsonBuilder()) 30 | .create(); 31 | private Ping response; 32 | 33 | public StatusReponse() { 34 | } 35 | 36 | public StatusReponse(Ping response) { 37 | this.response = response; 38 | } 39 | 40 | @Override 41 | public void read(MCByteBuf buf) { 42 | response = gson.fromJson(buf.readString(Short.MAX_VALUE), Ping.class); 43 | } 44 | 45 | @Override 46 | public void write(MCByteBuf buf) { 47 | buf.writeString(gson.toJson(response)); 48 | } 49 | 50 | @Override 51 | public void handle(IStatusHandlerClientbound handler) { 52 | handler.handle(this); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/network/protocol/status/StatusRequest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.network.protocol.status; 18 | 19 | import uk.co.thinkofdeath.prismarine.network.MCByteBuf; 20 | import uk.co.thinkofdeath.prismarine.network.protocol.IStatusHandlerServerbound; 21 | import uk.co.thinkofdeath.prismarine.network.protocol.Packet; 22 | 23 | public class StatusRequest implements Packet { 24 | 25 | public StatusRequest() { 26 | } 27 | 28 | @Override 29 | public void read(MCByteBuf buf) { 30 | 31 | } 32 | 33 | @Override 34 | public void write(MCByteBuf buf) { 35 | 36 | } 37 | 38 | @Override 39 | public void handle(IStatusHandlerServerbound handler) { 40 | handler.handle(this); 41 | } 42 | 43 | 44 | } 45 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/registry/Registry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.registry; 18 | 19 | import uk.co.thinkofdeath.prismarine.util.IntMap; 20 | 21 | import java.util.HashMap; 22 | import java.util.Map; 23 | 24 | public abstract class Registry { 25 | 26 | protected IntMap idMap = new IntMap<>(256); 27 | protected Map reverseIdMap = new HashMap<>(); 28 | private int nextId = 0; 29 | 30 | protected Registry() { 31 | 32 | } 33 | 34 | public T get(int id) { 35 | return idMap.get(id); 36 | } 37 | 38 | public int getId(T value) { 39 | return reverseIdMap.containsKey(value) ? reverseIdMap.get(value) : -1; 40 | } 41 | 42 | public void put(int id, T value) { 43 | idMap.put(id, value); 44 | reverseIdMap.put(value, id); 45 | } 46 | 47 | public void add(T value) { 48 | put(nextId++, value); 49 | } 50 | 51 | public void setNextId(int id) { 52 | nextId = id; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/util/IntMap.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.util; 18 | 19 | import java.util.function.ObjIntConsumer; 20 | 21 | public class IntMap { 22 | 23 | private static final Object NULL = new Object(); 24 | private Object[] values; 25 | 26 | public IntMap() { 27 | this(16); 28 | } 29 | 30 | public IntMap(int initialSize) { 31 | if (initialSize < 16) { 32 | initialSize = 16; 33 | } 34 | values = new Object[initialSize]; 35 | } 36 | 37 | public void put(int key, T value) { 38 | if (key >= values.length) { 39 | Object[] nv = new Object[values.length << 1]; 40 | System.arraycopy(values, 0, nv, 0, values.length); 41 | values = nv; 42 | } 43 | if (value == null) { 44 | values[key] = NULL; 45 | } else { 46 | values[key] = value; 47 | } 48 | } 49 | 50 | @SuppressWarnings("unchecked") 51 | public T get(int key) { 52 | if (key >= values.length) { 53 | return null; 54 | } 55 | Object val = values[key]; 56 | if (val == NULL || val == null) { 57 | return null; 58 | } 59 | return (T) val; 60 | } 61 | 62 | public boolean contains(int key) { 63 | return key < values.length && values[key] != null; 64 | } 65 | 66 | @SuppressWarnings("unchecked") 67 | public T remove(int key) { 68 | if (key >= values.length) { 69 | return null; 70 | } 71 | Object val = values[key]; 72 | values[key] = null; 73 | if (val == NULL || val == null) { 74 | return null; 75 | } 76 | return (T) val; 77 | } 78 | 79 | public void forEach(ObjIntConsumer func) { 80 | for (int i = 0; i < values.length; i++) { 81 | if (contains(i)) { 82 | func.accept(get(i), i); 83 | } 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /shared/src/main/java/uk/co/thinkofdeath/prismarine/util/Stringable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Matthew Collins 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.co.thinkofdeath.prismarine.util; 18 | 19 | import java.lang.reflect.Field; 20 | import java.lang.reflect.Modifier; 21 | import java.util.Arrays; 22 | import java.util.Objects; 23 | 24 | public interface Stringable { 25 | 26 | default String asString() { 27 | StringBuilder builder = new StringBuilder(); 28 | builder.append(getClass().getSimpleName()); 29 | builder.append("{"); 30 | StringHelper.appendFields(builder, this, getClass()); 31 | if (builder.charAt(builder.length() - 1) == ',') { 32 | builder.deleteCharAt(builder.length() - 1); 33 | } 34 | builder.append("}"); 35 | return builder.toString(); 36 | } 37 | 38 | } 39 | 40 | class StringHelper { 41 | 42 | public static void appendFields(StringBuilder builder, Object instance, Class clazz) { 43 | for (Field field : clazz.getDeclaredFields()) { 44 | if ((field.getModifiers() & Modifier.STATIC) != 0) { 45 | continue; 46 | } 47 | field.setAccessible(true); 48 | String print; 49 | try { 50 | Object value = field.get(instance); 51 | if (value == null) { 52 | print = "null"; 53 | } else if (value.getClass().isArray()) { 54 | if (value instanceof byte[]) { 55 | print = Arrays.toString((byte[]) value); 56 | } else if (value instanceof boolean[]) { 57 | print = Arrays.toString((boolean[]) value); 58 | } else if (value instanceof int[]) { 59 | print = Arrays.toString((int[]) value); 60 | } else if (value instanceof short[]) { 61 | print = Arrays.toString((short[]) value); 62 | } else if (value instanceof char[]) { 63 | print = Arrays.toString((char[]) value); 64 | } else if (value instanceof long[]) { 65 | print = Arrays.toString((long[]) value); 66 | } else if (value instanceof float[]) { 67 | print = Arrays.toString((float[]) value); 68 | } else if (value instanceof double[]) { 69 | print = Arrays.toString((double[]) value); 70 | } else { 71 | print = Arrays.deepToString((Object[]) value); 72 | } 73 | } else if (value instanceof String) { 74 | print = "\"" + value + "\""; 75 | } else if (value instanceof Stringable) { 76 | print = ((Stringable) value).asString(); 77 | } else { 78 | print = Objects.toString(value); 79 | } 80 | } catch (IllegalAccessException e) { 81 | throw new RuntimeException(e); 82 | } 83 | 84 | builder.append(field.getName()) 85 | .append("=") 86 | .append(print) 87 | .append(","); 88 | } 89 | if (!clazz.getSuperclass().equals(Object.class)) { 90 | appendFields(builder, instance, clazz.getSuperclass()); 91 | } 92 | } 93 | } --------------------------------------------------------------------------------