├── .gitignore ├── .travis.yml ├── CONTRIBUTING.md ├── LICENSE ├── LICENSEHEADER.txt ├── README.md ├── pom.xml ├── scripts ├── build.sh ├── deploy.sh ├── javadoc.sh └── runtests.sh └── src ├── main ├── java │ └── net │ │ └── tridentsdk │ │ └── server │ │ ├── ImplementationProvider.java │ │ ├── TridentMain.java │ │ ├── TridentServer.java │ │ ├── command │ │ ├── DebugCommand.java │ │ ├── DeopCommand.java │ │ ├── HelpCommand.java │ │ ├── KickCommand.java │ │ ├── OpCommand.java │ │ ├── SayCommand.java │ │ ├── StopCommand.java │ │ ├── TeleportCommand.java │ │ └── TridentDummyCommandPlugin.java │ │ ├── concurrent │ │ ├── PoolSpec.java │ │ ├── ServerThreadPool.java │ │ ├── TridentScheduler.java │ │ └── TridentTick.java │ │ ├── config │ │ ├── ConcurrentLinkedStringMap.java │ │ ├── ConfigIo.java │ │ ├── OpsList.java │ │ ├── ServerConfig.java │ │ ├── TridentConfig.java │ │ └── TridentConfigSection.java │ │ ├── entity │ │ ├── TridentEntity.java │ │ └── meta │ │ │ ├── EntityMetaType.java │ │ │ ├── TridentEntityMeta.java │ │ │ └── TridentLivingEntityMeta.java │ │ ├── inventory │ │ ├── TridentInventory.java │ │ ├── TridentItem.java │ │ └── TridentPlayerInventory.java │ │ ├── logger │ │ ├── ColorizerLogger.java │ │ ├── DebugLogger.java │ │ ├── DefaultLogger.java │ │ ├── FileLogger.java │ │ ├── InfoLogger.java │ │ ├── LogMessageImpl.java │ │ ├── LoggerHandlers.java │ │ └── PipelinedLogger.java │ │ ├── net │ │ ├── EntityMetadata.java │ │ ├── InDecoder.java │ │ ├── NetChannelInit.java │ │ ├── NetClient.java │ │ ├── NetCrypto.java │ │ ├── NetData.java │ │ ├── NetEpollServer.java │ │ ├── NetNioServer.java │ │ ├── NetServer.java │ │ ├── OutEncoder.java │ │ └── Slot.java │ │ ├── packet │ │ ├── Packet.java │ │ ├── PacketIn.java │ │ ├── PacketOut.java │ │ ├── PacketRegistry.java │ │ ├── handshake │ │ │ ├── HandshakeIn.java │ │ │ └── LegacyHandshakeIn.java │ │ ├── login │ │ │ ├── Login.java │ │ │ ├── LoginInEncryptionResponse.java │ │ │ ├── LoginInStart.java │ │ │ ├── LoginOutCompression.java │ │ │ ├── LoginOutDisconnect.java │ │ │ ├── LoginOutEncryptionRequest.java │ │ │ ├── LoginOutSuccess.java │ │ │ └── Mojang.java │ │ ├── play │ │ │ ├── PlayInAnimation.java │ │ │ ├── PlayInBlockPlace.java │ │ │ ├── PlayInChat.java │ │ │ ├── PlayInClientSettings.java │ │ │ ├── PlayInClientStatus.java │ │ │ ├── PlayInCloseWindow.java │ │ │ ├── PlayInCreativeInventoryAction.java │ │ │ ├── PlayInEntityAction.java │ │ │ ├── PlayInKeepAlive.java │ │ │ ├── PlayInLook.java │ │ │ ├── PlayInPlayer.java │ │ │ ├── PlayInPlayerAbilities.java │ │ │ ├── PlayInPlayerDig.java │ │ │ ├── PlayInPluginMsg.java │ │ │ ├── PlayInPos.java │ │ │ ├── PlayInPosLook.java │ │ │ ├── PlayInSetSlot.java │ │ │ ├── PlayInTeleportConfirm.java │ │ │ ├── PlayInUseEntity.java │ │ │ ├── PlayInUseItem.java │ │ │ ├── PlayOutAnimation.java │ │ │ ├── PlayOutBlockChange.java │ │ │ ├── PlayOutBossBar.java │ │ │ ├── PlayOutChat.java │ │ │ ├── PlayOutChunk.java │ │ │ ├── PlayOutDestroyEntities.java │ │ │ ├── PlayOutDifficulty.java │ │ │ ├── PlayOutDisconnect.java │ │ │ ├── PlayOutEntityHeadLook.java │ │ │ ├── PlayOutEntityLook.java │ │ │ ├── PlayOutEntityLookAndRelativeMove.java │ │ │ ├── PlayOutEntityMetadata.java │ │ │ ├── PlayOutEntityRelativeMove.java │ │ │ ├── PlayOutEquipment.java │ │ │ ├── PlayOutGameState.java │ │ │ ├── PlayOutJoinGame.java │ │ │ ├── PlayOutKeepAlive.java │ │ │ ├── PlayOutLightning.java │ │ │ ├── PlayOutOpenWindow.java │ │ │ ├── PlayOutPlayerAbilities.java │ │ │ ├── PlayOutPlayerListHeaderAndFooter.java │ │ │ ├── PlayOutPluginMsg.java │ │ │ ├── PlayOutPosLook.java │ │ │ ├── PlayOutSlot.java │ │ │ ├── PlayOutSpawnPlayer.java │ │ │ ├── PlayOutSpawnPos.java │ │ │ ├── PlayOutTabListItem.java │ │ │ ├── PlayOutTeleport.java │ │ │ ├── PlayOutTime.java │ │ │ ├── PlayOutTitle.java │ │ │ ├── PlayOutUnloadChunk.java │ │ │ ├── PlayOutWindowItems.java │ │ │ └── PlayOutWorldBorder.java │ │ └── status │ │ │ ├── StatusInPing.java │ │ │ ├── StatusInRequest.java │ │ │ ├── StatusOutPong.java │ │ │ └── StatusOutResponse.java │ │ ├── player │ │ ├── RecipientSelector.java │ │ ├── TridentPlayer.java │ │ └── TridentPlayerMeta.java │ │ ├── plugin │ │ ├── EventDispatcher.java │ │ ├── TridentEventController.java │ │ ├── TridentPluginAllChannel.java │ │ └── TridentPluginChannel.java │ │ ├── ui │ │ ├── bossbar │ │ │ ├── AbstractBossBar.java │ │ │ ├── CustomBossBar.java │ │ │ └── DragonBossBar.java │ │ ├── tablist │ │ │ ├── TabListElement.java │ │ │ ├── TridentCustomTabList.java │ │ │ ├── TridentGlobalTabList.java │ │ │ └── TridentTabList.java │ │ └── title │ │ │ └── CustomTitle.java │ │ ├── util │ │ ├── BitUtils.java │ │ ├── Cache.java │ │ ├── Debug.java │ │ ├── JiraExceptionCatcher.java │ │ ├── Long2ReferenceOpenHashMap.java │ │ ├── NibbleArray.java │ │ ├── Reference2IntOpenHashMap.java │ │ ├── ShortArrayList.java │ │ ├── ShortOpenHashSet.java │ │ └── UncheckedCdl.java │ │ └── world │ │ ├── ChunkMap.java │ │ ├── ChunkSection.java │ │ ├── Region.java │ │ ├── TridentBlock.java │ │ ├── TridentChunk.java │ │ ├── TridentWorld.java │ │ ├── TridentWorldLoader.java │ │ ├── gen │ │ ├── FlatGeneratorProvider.java │ │ ├── FlatTerrainGenerator.java │ │ └── GeneratorContextImpl.java │ │ └── opt │ │ ├── GenOptImpl.java │ │ ├── WeatherImpl.java │ │ ├── WorldBorderImpl.java │ │ └── WorldOptImpl.java └── resources │ ├── commit_data │ └── server.hjson └── test └── java └── net └── tridentsdk └── server ├── BlockBenchmark.java ├── EventTest.java ├── IoTest.java ├── command └── LoggerTest.java └── packet └── InstantiationTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | ### Trident ### 2 | /logs/ 3 | /server.json 4 | 5 | 6 | ### Java ### 7 | *.class 8 | 9 | # Package Files # 10 | *.jar 11 | *.war 12 | *.ear 13 | 14 | # virtual machine crash logs 15 | hs_err_pid* 16 | 17 | ### Eclipse ### 18 | *.pydevproject 19 | .metadata 20 | .gradle 21 | bin/ 22 | tmp/ 23 | *.tmp 24 | *.bak 25 | *.swp 26 | *~.nib 27 | local.properties 28 | .settings/ 29 | .loadpath 30 | .classpath 31 | .project 32 | 33 | # External tool builders 34 | .externalToolBuilders/ 35 | 36 | # Locally stored "Eclipse launch configurations" 37 | *.launch 38 | 39 | # CDT-specific 40 | .cproject 41 | 42 | # PDT-specific 43 | .buildpath 44 | 45 | # sbteclipse plugin 46 | .target 47 | 48 | # TeXlipse plugin 49 | .texlipse 50 | 51 | ### Maven ### 52 | target/ 53 | pom.xml.tag 54 | pom.xml.releaseBackup 55 | pom.xml.versionsBackup 56 | pom.xml.next 57 | release.properties 58 | 59 | ## Directory-based project format 60 | .idea/ 61 | 62 | ## File-based project format 63 | *.ipr 64 | *.iws 65 | *.iml 66 | 67 | ## Additional for IntelliJ 68 | 69 | # generated by mpeltonen/sbt-idea plugin 70 | .idea_modules/ 71 | 72 | # generated by JIRA plugin 73 | atlassian-ide-plugin.xml 74 | 75 | # generated by Crashlytics plugin (for Android Studio and Intellij) 76 | com_crashlytics_export_strings.xml 77 | 78 | ### NetBeans ### 79 | nbproject/private/ 80 | build/ 81 | nbbuild/ 82 | dist/ 83 | nbdist/ 84 | nbactions.xml 85 | nb-configuration.xml 86 | 87 | # Editor preferences for a class etc 88 | *.ctxt 89 | 90 | # Duplicate of project file 91 | bluej.pkh 92 | 93 | ### Processing ### 94 | .DS_Store 95 | applet 96 | application.linux32 97 | application.linux64 98 | application.windows32 99 | application.windows64 100 | application.macosx 101 | 102 | ### Some others ### 103 | MANIFEST.MF 104 | dependency-reduced-pom.xml 105 | 106 | Thumbs.db 107 | 108 | !/gradle/**/* -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | 3 | dist: trusty 4 | 5 | jdk: 6 | - oraclejdk8 7 | - openjdk8 8 | 9 | before_install: 10 | - git clone -b travis `git config --get remote.origin.url` travis 11 | 12 | script: ./scripts/build.sh 13 | after_script: ./scripts/javadoc.sh 14 | 15 | install: true 16 | sudo: false 17 | 18 | deploy: 19 | provider: script 20 | script: scripts/deploy.sh 21 | on: 22 | branch: revamp 23 | jdk: openjdk8 24 | 25 | cache: 26 | directories: 27 | - .autoconf 28 | - $HOME/.m2 29 | 30 | env: 31 | global: 32 | - secure: Qv3lWydc65BlLDv2IGtRC1+NTOyytKOqAGOVSO5/NFvH1mX8VE1yBm+vop/S/Tj6iUxOSzxD47xDQAZt/T13SKKFUngqu83uQS4bwu8QK6aNVp2xxphLNwqprcxXIbvY4L5NxOd0lE050ITgJhyz+tVwW/0yfc7BTvw7t8iOkLI= 33 | - secure: R5fqbgn9y+uZ8sjQul0tIrZoqqOIFBe8Peqop9onDx4/ffczZJvPkkPlI/PIYEUVRNEteSP+y1RSTGxTFNmeiX1NLWfB5mdT8dmKharKkWFuDKWwMdaOuWbdWjALT/dx4yrgY3UHsM7GO2h7T67owPL3c3k83IDlPgBI7CKSryc= 34 | - secure: k5SUYYpfcBsvaWuIEtjKFk/7mOVwrxrcsx7Fba5GaO4oTHuomfDRc+NS6PI92Krkm6ZNV2HNSREwTbTSLRZ2L6iR8S2z3vbcg4ZvYnDJ94DxqSgBqc/b8En9mEGv16t8Q7g+vJfjKITpgO76hS3dEKD+RFpHbxz1m8gDalsA7M4= 35 | -------------------------------------------------------------------------------- /LICENSEHEADER.txt: -------------------------------------------------------------------------------- 1 | Trident - A Multithreaded Server Alternative 2 | Copyright 2017 The TridentSDK Team 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. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Trident [![Build Status](https://travis-ci.org/TridentSDK/Trident.svg?branch=revamp)](https://travis-ci.org/TridentSDK/Trident) 2 | ========= 3 | 4 | The Trident project, the implementation of the new generation of multi-threaded, high-performance, and cleanroom Minecraft servers. 5 | 6 | * [Website](https://tsdk.xyz) 7 | * [Chat](https://www.hipchat.com/g20bt22H2?v=2) 8 | * [Issues](https://tridentsdk.atlassian.net/projects/TRD/issues) 9 | * [Tech Document](https://tsdk.xyz/techdoc/) 10 | * [Wiki](https://tridentsdkwiki.atlassian.net/wiki/dashboard.action) 11 | * [Javadoc](https://tridentsdk.github.io/javadocs) 12 | * [Contributing](https://tridentsdkwiki.atlassian.net/wiki/display/DEV/Trident+Development) 13 | * [TridentSDK](https://github.com/TridentSDK/TridentSDK) 14 | 15 | ## Latest Release ## 16 | 17 | - [0.3-alpha DP](https://github.com/TridentSDK/Trident/releases/tag/0.3-alpha-DP) 18 | 19 | ## Getting the JAR ## 20 | 21 | ### Method one: Build it yourself ### 22 | 23 | If you have decided that our forms of distribution are questionable, or you would like to modify something before getting a JAR file, you want to build from the source directly. 24 | 25 | #### Prerequisites #### 26 | 27 | 1. A [computer](https://en.wikipedia.org/wiki/Computer) 28 | 2. [Git](https://git-scm.com/) 29 | 3. [Maven](https://maven.apache.org/) 30 | 4. [Java 8 JDK](http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html) 31 | 32 | #### Directions #### 33 | 34 | Type in the following into the command line: 35 | 36 | ```bash 37 | git clone -b [master|bleeding-edge] https://github.com/TridentSDK/Trident.git 38 | cd Trident 39 | mvn clean install 40 | ``` 41 | 42 | One would use master to ensure compatibility with the latest build of [TridentSDK](https://github.com/TridentSDK/TridentSDK). Otherwise, for the most up-to-date (and potentially breaking) build of Trident, one would use `bleeding-edge`. If you are building the JAR yourself, it is your responsibility to know which one is right for you. 43 | 44 | The built JAR will `./Trident/target/trident-*.jar` 45 | 46 | ### Method two: Download from Sonatype Nexus ### 47 | 48 | For a precompiled solution, one which has passed the tests we have wrote, as well as if you are too lazy to download 2 files and install the [Method one](#method-one-build-it-yourself) prerequeisites, you can download one yourself. 49 | 50 | #### Prerequisites #### 51 | 52 | 1. A [mouse](https://en.wikipedia.org/wiki/Mouse_(computing)) 53 | 2. A [web browser](https://en.wikipedia.org/wiki/Web_browser) 54 | 3. A [computer](https://en.wikipedia.org/wiki/Computer) 55 | 4. [Java 8 JRE/JDK](http://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html) 56 | 57 | #### Directions #### 58 | 59 | 1. [Click](https://oss.sonatype.org/service/local/artifact/maven/redirect?r=snapshots&g=net.tridentsdk&a=trident&v=0.3-SNAPSHOT&e=jar) 60 | 2. Click "Keep" 61 | 62 | ### Method three: Get one from us ### 63 | 64 | If we've released a JAR for you to use, it usually comes with a nice read. So if you like nice reads or the look of our [official website](https://tsdk.xyz), you can go rummage around the releases forum and look for a download link. 65 | 66 | #### Prerequisites #### 67 | 68 | 1. A [mouse](https://en.wikipedia.org/wiki/Mouse_(computing)) 69 | 2. A [web browser](https://en.wikipedia.org/wiki/Web_browser) 70 | 3. A [computer](https://en.wikipedia.org/wiki/Computer) 71 | 4. [Java 8 JRE/JDK](http://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html) 72 | 73 | #### Directions #### 74 | 75 | 1. Nagigate to [tsdk.xyz](https://tsdk.xyz) 76 | 2. Scroll down the News list to the first release post 77 | 3. Find the download link 78 | 4. Click the download link 79 | -------------------------------------------------------------------------------- /scripts/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo "Currently on branch: $TRAVIS_BRANCH" 4 | echo "Pull request: $TRAVIS_PULL_REQUEST" 5 | 6 | ORIGIN_BRANCH=$TRAVIS_BRANCH 7 | 8 | if [ "$TRAVIS_PULL_REQUEST" != "false" ]; 9 | then 10 | FULL_SLUG=$TRAVIS_PULL_REQUEST_SLUG 11 | FULL_SLUG+="SDK" 12 | 13 | echo $FULL_SLUG 14 | 15 | OWN_REPO=$(curl --write-out %{http_code} --silent --output /dev/null "https://github.com/$FULL_SLUG") 16 | 17 | echo "Own SDK Repo: $OWN_REPO" 18 | 19 | if [ "$OWN_REPO" != "404" ]; 20 | then 21 | git clone -b revamp "https://github.com/$FULL_SLUG.git" 22 | else 23 | git clone -b revamp https://github.com/TridentSDK/TridentSDK.git 24 | fi 25 | 26 | ORIGIN_BRANCH=$TRAVIS_PULL_REQUEST_BRANCH 27 | else 28 | git clone -b revamp https://github.com/TridentSDK/TridentSDK.git 29 | fi 30 | 31 | pushd TridentSDK 32 | 33 | REMOTE_REPO=$(git remote -v | grep fetch | awk '{print $2}') 34 | BRANCH_EXISTS=$(git ls-remote --heads "$REMOTE_REPO" "$ORIGIN_BRANCH") 35 | 36 | if [ "$BRANCH_EXISTS" ]; 37 | then 38 | git checkout $ORIGIN_BRANCH 39 | fi 40 | 41 | mvn clean install 42 | popd 43 | 44 | if [ "$TRAVIS_BRANCH" == "revamp" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ]; 45 | then 46 | echo "Compiling with deployment" 47 | mvn clean install deploy --settings travis/settings.xml 48 | 49 | # Ping docker hub 50 | curl -H "Content-Type: application/json" --data "'{\"build\": true}'" -X POST https://registry.hub.docker.com/u/tridentsdk/trident/trigger/${DOCKER_TRIGGER_TOKEN}/ 51 | else 52 | echo "Compiling without deployment" 53 | mvn clean install --settings travis/settings.xml 54 | fi 55 | -------------------------------------------------------------------------------- /scripts/deploy.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo "Currently on branch: $TRAVIS_BRANCH" 4 | 5 | if [ "$TRAVIS_BRANCH" == "revamp" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ]; 6 | then 7 | ./gradlew uploadArchives 8 | 9 | # Ping docker hub 10 | curl -H "Content-Type: application/json" --data "'{\"build\": true}'" -X POST https://registry.hub.docker.com/u/tridentsdk/trident/trigger/${DOCKER_TRIGGER_TOKEN}/ 11 | fi -------------------------------------------------------------------------------- /scripts/javadoc.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # This script was originally written by maxiaohao in the aws-mock GitHub project. 3 | # https://github.com/treelogic-swe/aws-mock/ 4 | 5 | if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then 6 | # Set it up 7 | git config --global user.email "woodyc40@gmail.com" 8 | git config --global user.name "AgentTroll" 9 | mkdir gh-pages 10 | 11 | # We're gonna make this a proper repo 12 | cd gh-pages 13 | git init 14 | cd .. 15 | 16 | # TridentSDK included with the javadoc 17 | git clone -b bleeding-edge https://github.com/TridentSDK/TridentSDK.git 18 | 19 | # Move the server directory into TridentSDK, else the last javadoc will not display the correct index 20 | cp -R src/main/java/net/tridentsdk/server TridentSDK/src/main/java/net/tridentsdk/ 21 | 22 | cd TridentSDK 23 | mvn clean javadoc:javadoc 24 | mv gh-pages/* ../gh-pages 25 | cd .. 26 | 27 | # Push! 28 | cd gh-pages 29 | git add . 30 | git commit -m "Auto-publishing Javadoc from Travis CI" 31 | git push -fq https://AgentTroll:${DOC_PASS}@github.com/TridentSDK/javadocs.git HEAD:gh-pages >/dev/null 2>/dev/null 32 | echo "Published JavaDoc.\n" # Done! 33 | fi 34 | -------------------------------------------------------------------------------- /scripts/runtests.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | cd src/test/jcstress 4 | mvn clean install -pl tests-custom -am 5 | java -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:-RestrictContended -jar tests-custom/target/jcstress.jar -c 16 -m tough -r results/tough -yield true 6 | java -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:-RestrictContended -jar tests-custom/target/jcstress.jar -c 16 -m stress -r results/stress -yield true 7 | 8 | 9 | # 16 threads 10 | # iterations 11 | # time (higher = better lower = faster) 12 | # verbose (print a lot) -v 13 | # deoptimize interval (deopt every x, lower = better higher = faster) 14 | # mode (sanity, quick, default, tough, stress) (may be preset, don't apply for flags) 15 | # stride (lower = better, higher = less overhead) 16 | # -r result output directory 17 | # best: 18 | # java -jar tests-custom/target/jcstress.jar -c 16 -f 10 -iter 1000000000 -time 5000 -deoptRatio 0 -yield true -minStride 0 -maxStride 0 -r /results/limit 19 | # 20 | # preferred: 21 | # java -jar tests-custom/target/jcstress.jar -c 16 -f 10 -iter 1000000 -time 100 -deoptRatio 0 -yield true -minStride 0 -maxStride 0 -r -r /results/pref -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/command/DeopCommand.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.command; 18 | 19 | import net.tridentsdk.command.*; 20 | import net.tridentsdk.command.annotation.AllowedSourceTypes; 21 | import net.tridentsdk.command.annotation.PermissionRequired; 22 | import net.tridentsdk.command.annotation.PlayerExactMatch; 23 | import net.tridentsdk.entity.living.Player; 24 | import net.tridentsdk.ui.chat.ChatColor; 25 | import net.tridentsdk.ui.chat.ChatComponent; 26 | 27 | public class DeopCommand implements CommandListener { 28 | 29 | @Command(name = "deop", help = "/deop ", desc = "Sets the player to a non-operator") 30 | @PermissionRequired("minecraft.deop") 31 | @AllowedSourceTypes({ CommandSourceType.PLAYER, CommandSourceType.CONSOLE }) 32 | public void deop(CommandSource source, String[] args, @PlayerExactMatch Player player) { 33 | if (player == null) { 34 | source.sendMessage(ChatComponent.create().setColor(ChatColor.RED).setText("No player by the name '" + args[1] + "' is online!")); 35 | } else { 36 | player.setOp(false); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/command/KickCommand.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.command; 18 | 19 | import net.tridentsdk.command.Command; 20 | import net.tridentsdk.command.CommandListener; 21 | import net.tridentsdk.command.CommandSource; 22 | import net.tridentsdk.command.annotation.PermissionRequired; 23 | import net.tridentsdk.entity.living.Player; 24 | import net.tridentsdk.server.TridentServer; 25 | import net.tridentsdk.ui.chat.ChatComponent; 26 | 27 | import javax.annotation.concurrent.Immutable; 28 | 29 | @Immutable 30 | public class KickCommand implements CommandListener { 31 | 32 | @Command(name = "kick", help = "/kick [reason]", desc = "Kicks a player from the server") 33 | @PermissionRequired("minecraft.kick") 34 | public void kick(CommandSource source, String[] args, Player player, String... reason) { 35 | if (player != null) { 36 | String reasonString = reason.length == 0 ? "Kicked by an operator." : String.join(" ", reason); 37 | player.kick(ChatComponent.text(reasonString)); 38 | TridentServer.getInstance().getLogger().log("Kicked player " + player.getName() + " for: " + reasonString); 39 | } else { 40 | source.sendMessage(ChatComponent.text("No player by the name '" + args[1] + "' is online.")); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/command/OpCommand.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.command; 18 | 19 | import net.tridentsdk.command.*; 20 | import net.tridentsdk.command.annotation.AllowedSourceTypes; 21 | import net.tridentsdk.command.annotation.PermissionRequired; 22 | import net.tridentsdk.command.annotation.PlayerExactMatch; 23 | import net.tridentsdk.entity.living.Player; 24 | import net.tridentsdk.ui.chat.ChatColor; 25 | import net.tridentsdk.ui.chat.ChatComponent; 26 | 27 | public class OpCommand implements CommandListener { 28 | 29 | @Command(name = "op", help = "/op ", desc = "Sets the player to an operator") 30 | @PermissionRequired("minecraft.op") 31 | @AllowedSourceTypes({ CommandSourceType.PLAYER, CommandSourceType.CONSOLE }) 32 | public void op(CommandSource source, String[] args, @PlayerExactMatch Player player) { 33 | if (player == null) { 34 | source.sendMessage(ChatComponent.create().setColor(ChatColor.RED).setText("No player by the name '" + args[1] + "' is online!")); 35 | } else { 36 | player.setOp(true); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/command/SayCommand.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.command; 18 | 19 | import net.tridentsdk.command.*; 20 | import net.tridentsdk.command.annotation.MinCount; 21 | import net.tridentsdk.command.annotation.PermissionRequired; 22 | import net.tridentsdk.server.TridentServer; 23 | import net.tridentsdk.server.player.TridentPlayer; 24 | import net.tridentsdk.ui.chat.ChatComponent; 25 | 26 | import net.tridentsdk.ui.chat.ClickAction; 27 | import net.tridentsdk.ui.chat.ClickEvent; 28 | 29 | public class SayCommand implements CommandListener { 30 | @Command(name = "say", help = "/say ", desc = "Broadcasts a message to all players") 31 | @PermissionRequired("minecraft.say") 32 | public void say(CommandSource source, String[] args, @MinCount(1) String... message) { 33 | StringBuilder builder = new StringBuilder(); 34 | for (String arg : message) { 35 | builder.append(' ').append(arg); 36 | } 37 | 38 | if (source.getCmdType() == CommandSourceType.PLAYER) { 39 | String name = ((TridentPlayer) source).getName(); 40 | String msg = '[' + name + "]"; 41 | ChatComponent cc = ChatComponent.create() 42 | .setText(msg) 43 | .setClickEvent(ClickEvent.of(ClickAction.SUGGEST_COMMAND, "/tell " + name + " ")) 44 | .addExtra(builder.toString()); 45 | for (TridentPlayer player : TridentPlayer.getPlayers().values()) { 46 | player.sendMessage(cc); 47 | } 48 | TridentServer.getInstance().getLogger().log(msg + builder); 49 | } else { 50 | String msg = "[Server]" + builder; 51 | for (TridentPlayer player : TridentPlayer.getPlayers().values()) { 52 | player.sendMessage(ChatComponent.create().setText(msg)); 53 | } 54 | TridentServer.getInstance().getLogger().log(msg); 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/command/StopCommand.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.command; 18 | 19 | import net.tridentsdk.command.Command; 20 | import net.tridentsdk.command.CommandListener; 21 | import net.tridentsdk.command.CommandSource; 22 | import net.tridentsdk.command.CommandSourceType; 23 | import net.tridentsdk.command.annotation.AllowedSourceTypes; 24 | import net.tridentsdk.command.annotation.PermissionRequired; 25 | import net.tridentsdk.server.TridentServer; 26 | 27 | public class StopCommand implements CommandListener { 28 | @Command(name = "stop", aliases = { "shutdown", "fuck" }, help = "/stop", desc = "Stops the server and shuts-down") 29 | @PermissionRequired("minecraft.stop") 30 | @AllowedSourceTypes({ CommandSourceType.CONSOLE, CommandSourceType.PLAYER }) 31 | public void stop(CommandSource source, String[] args) { 32 | TridentServer.getInstance().shutdown(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/command/TeleportCommand.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.command; 18 | 19 | import net.tridentsdk.base.Position; 20 | import net.tridentsdk.command.*; 21 | import net.tridentsdk.command.annotation.AllowedSourceTypes; 22 | import net.tridentsdk.command.annotation.MaxCount; 23 | import net.tridentsdk.command.annotation.PermissionRequired; 24 | import net.tridentsdk.command.annotation.PlayerExactMatch; 25 | import net.tridentsdk.entity.living.Player; 26 | import net.tridentsdk.ui.chat.ChatColor; 27 | import net.tridentsdk.ui.chat.ChatComponent; 28 | 29 | public class TeleportCommand implements CommandListener { 30 | 31 | @Command(name = "teleport", aliases = "tp", help = "/teleport [ ]", desc = "Teleports the given player to the given XYZ") 32 | @PermissionRequired("minecraft.tp") 33 | @AllowedSourceTypes(CommandSourceType.PLAYER) 34 | public void teleport(CommandSource source, String[] args, @PlayerExactMatch Player player, double x, double y, double z, @MaxCount(2) float... direction) { 35 | if (player == null) { 36 | source.sendMessage(ChatComponent.create().setColor(ChatColor.RED).setText("No player by the name '" + args[1] + "' is online")); 37 | } else { 38 | float pitch = direction.length > 0 ? direction[0] : 0; 39 | float yaw = direction.length > 1 ? direction[1] : 0; 40 | player.setPosition(new Position(player.getWorld(), x, y, z, pitch, yaw)); 41 | } 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/command/TridentDummyCommandPlugin.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.command; 18 | 19 | import net.tridentsdk.plugin.Plugin; 20 | import net.tridentsdk.plugin.PluginDesc; 21 | 22 | import java.lang.annotation.Annotation; 23 | import java.lang.reflect.Field; 24 | import java.util.Arrays; 25 | import java.util.concurrent.atomic.AtomicInteger; 26 | 27 | /** 28 | * @author Nick Robson 29 | */ 30 | public class TridentDummyCommandPlugin extends Plugin { 31 | 32 | private static final AtomicInteger used0 = new AtomicInteger(), used1 = new AtomicInteger(); 33 | public static final Plugin TRIDENT_INST = new TridentDummyCommandPlugin("trident", "Trident"); 34 | public static final Plugin MINECRAFT_INST = new TridentDummyCommandPlugin("minecraft", "Minecraft"); 35 | private TridentDummyCommandPlugin(String id, String display) { 36 | if (!Arrays.asList("trident", "minecraft").contains(id)) 37 | throw new IllegalArgumentException("only trident and minecraft are allowed ids"); 38 | if (!used0.compareAndSet(0, 1) && !used1.compareAndSet(0, 1)) { 39 | throw new IllegalArgumentException("invalid registration"); 40 | } 41 | PluginDesc pluginDesc = new PluginDesc() { 42 | @Override public boolean equals(Object obj) { return obj == this; } 43 | 44 | @Override 45 | public int hashCode() { 46 | return this.id().hashCode(); 47 | } 48 | 49 | @Override 50 | public String toString() { 51 | return this.id(); 52 | } 53 | @Override public Class annotationType() { return PluginDesc.class; } 54 | @Override public String id() { return id; } 55 | @Override public String name() { return display; } 56 | @Override public String version() { return "1.0.0"; } 57 | @Override public String author() { return "TridentSDK Team"; } 58 | @Override public String[] depends() { return new String[0]; } 59 | }; 60 | try { 61 | Field f = Plugin.class.getDeclaredField("description"); 62 | f.setAccessible(true); 63 | f.set(this, pluginDesc); 64 | } catch (ReflectiveOperationException ex) { 65 | ex.printStackTrace(); 66 | } 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/concurrent/TridentScheduler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.concurrent; 18 | 19 | import lombok.Getter; 20 | 21 | import javax.annotation.concurrent.Immutable; 22 | 23 | /** 24 | * Implementation of a scheduler which allows plugins and 25 | * the server to ask for tasks to be done in other threads 26 | * or with delays. 27 | */ 28 | @Immutable 29 | public final class TridentScheduler { 30 | private static final ServerThreadPool POOL = ServerThreadPool.forSpec(PoolSpec.SCHEDULER); 31 | 32 | @Getter 33 | private static final TridentScheduler instance = new TridentScheduler(); 34 | 35 | private TridentScheduler() { 36 | } 37 | 38 | public void tick() { 39 | } 40 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/concurrent/TridentTick.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.concurrent; 18 | 19 | import net.tridentsdk.logger.Logger; 20 | import net.tridentsdk.server.player.TridentPlayer; 21 | import net.tridentsdk.server.util.JiraExceptionCatcher; 22 | import net.tridentsdk.server.world.TridentWorld; 23 | import net.tridentsdk.server.world.TridentWorldLoader; 24 | 25 | import javax.annotation.concurrent.Immutable; 26 | import java.util.concurrent.TimeUnit; 27 | 28 | /** 29 | * This class represents the server heartbeat pulse called 30 | * "tick" which occurs every 1/20th of a second. 31 | */ 32 | @Immutable 33 | public final class TridentTick extends Thread { 34 | /** 35 | * The amount of time taken by a single tick 36 | */ 37 | private static final long TICK_MILLIS = TimeUnit.SECONDS.toMillis(1) / 20; 38 | /** 39 | * The logger for this server tick thread 40 | */ 41 | private final Logger logger; 42 | 43 | /** 44 | * Creates a new server ticker thread. 45 | * 46 | * @param logger the logger to log to 47 | */ 48 | public TridentTick(Logger logger) { 49 | super("TRD - Tick"); 50 | this.logger = logger; 51 | } 52 | 53 | @Override 54 | public void run() { 55 | while (true) { 56 | try { 57 | long start = System.currentTimeMillis(); 58 | 59 | // Tick worlds 60 | for (TridentWorld world : TridentWorldLoader.getInstance().worlds()) { 61 | world.tick(); 62 | } 63 | 64 | // Tick players 65 | for (TridentPlayer player : TridentPlayer.getPlayers().values()) { 66 | player.tick(); 67 | } 68 | 69 | // Tick the scheduler 70 | TridentScheduler.getInstance().tick(); 71 | 72 | // Timing mechanics 73 | long end = System.currentTimeMillis(); 74 | long elapsed = end - start; 75 | long waitTime = TICK_MILLIS - elapsed; 76 | if (waitTime < 0) { 77 | this.logger.debug("Server running behind " + 78 | -waitTime + "ms, skipped " + (-waitTime / TICK_MILLIS) + " ticks"); 79 | } else { 80 | Thread.sleep(waitTime); 81 | } 82 | } catch (InterruptedException e) { 83 | break; // Thread interrupted by server, 84 | // server must be shutting down 85 | } catch (Exception e) { 86 | JiraExceptionCatcher.serverException(e); 87 | break; 88 | } 89 | } 90 | } 91 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/config/ConcurrentLinkedStringMap.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.config; 18 | 19 | import javax.annotation.concurrent.ThreadSafe; 20 | import java.util.Collection; 21 | import java.util.LinkedHashMap; 22 | import java.util.Map; 23 | import java.util.Set; 24 | import java.util.concurrent.locks.Lock; 25 | import java.util.concurrent.locks.ReadWriteLock; 26 | import java.util.concurrent.locks.ReentrantReadWriteLock; 27 | import java.util.function.BiConsumer; 28 | 29 | /** 30 | * Yet another dumb class without any real purpose that is 31 | * made necessary simply for no apparent reason 32 | * 33 | * since I am lazy I don't intend on adding any unnecessary 34 | * bs code that won't be used, so this really isn't a real 35 | * "map" 36 | * 37 | * all these methods are self-explanatory, there is no use 38 | * for documenting them 39 | */ 40 | @ThreadSafe 41 | public class ConcurrentLinkedStringMap { 42 | /** 43 | * The lock which protects the map 44 | */ 45 | private final ReadWriteLock lock = new ReentrantReadWriteLock(); 46 | /** 47 | * The underlying map 48 | */ 49 | private final LinkedHashMap map = new LinkedHashMap<>(); 50 | 51 | public void put(String key, V value) { 52 | Lock lock = this.lock.writeLock(); 53 | lock.lock(); 54 | try { 55 | this.map.put(key, value); 56 | } finally { 57 | lock.unlock(); 58 | } 59 | } 60 | 61 | public V remove(String key) { 62 | Lock lock = this.lock.writeLock(); 63 | lock.lock(); 64 | try { 65 | return this.map.remove(key); 66 | } finally { 67 | lock.unlock(); 68 | } 69 | } 70 | 71 | public V get(String key) { 72 | Lock lock = this.lock.readLock(); 73 | lock.lock(); 74 | try { 75 | return this.map.get(key); 76 | } finally { 77 | lock.unlock(); 78 | } 79 | } 80 | 81 | public boolean containsKey(String key) { 82 | Lock lock = this.lock.readLock(); 83 | lock.lock(); 84 | try { 85 | return this.map.containsKey(key); 86 | } finally { 87 | lock.unlock(); 88 | } 89 | } 90 | 91 | public Set keySet() { 92 | Lock lock = this.lock.readLock(); 93 | lock.lock(); 94 | try { 95 | return this.map.keySet(); 96 | } finally { 97 | lock.unlock(); 98 | } 99 | } 100 | 101 | public Collection values() { 102 | Lock lock = this.lock.readLock(); 103 | lock.lock(); 104 | try { 105 | return this.map.values(); 106 | } finally { 107 | lock.unlock(); 108 | } 109 | } 110 | 111 | public void forEach(BiConsumer consumer) { 112 | Lock lock = this.lock.readLock(); 113 | lock.lock(); 114 | try { 115 | this.map.forEach(consumer); 116 | } finally { 117 | lock.unlock(); 118 | } 119 | } 120 | 121 | public Set> entrySet() { 122 | Lock lock = this.lock.readLock(); 123 | lock.lock(); 124 | try { 125 | return this.map.entrySet(); 126 | } finally { 127 | lock.unlock(); 128 | } 129 | } 130 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/config/TridentConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.config; 18 | 19 | 20 | import net.tridentsdk.config.Config; 21 | import net.tridentsdk.config.ConfigSection; 22 | import org.hjson.JsonObject; 23 | 24 | import javax.annotation.concurrent.Immutable; 25 | import java.io.File; 26 | import java.io.IOException; 27 | import java.nio.file.Path; 28 | import java.util.concurrent.ConcurrentHashMap; 29 | import java.util.concurrent.ConcurrentMap; 30 | 31 | /** 32 | * The default implementation of a configuration file that 33 | * is loaded into memory. 34 | * 35 | * @author TridentSDK 36 | * @since 0.5-alpha 37 | */ 38 | @Immutable 39 | public class TridentConfig extends TridentConfigSection implements Config { 40 | /** 41 | * The mapping of configs cached by the server. 42 | * 43 | * Configs should really only have one instance so this 44 | * cache holds configs indefinitely. 45 | */ 46 | private static final ConcurrentMap cachedConfigs = new ConcurrentHashMap<>(); 47 | 48 | /** 49 | * Releases the configuration file that may be cached 50 | * at the given location. 51 | * 52 | * @param path the path to evict the config 53 | */ 54 | public static void release(Path path) { 55 | cachedConfigs.remove(path); 56 | } 57 | 58 | /** 59 | * The path to the config file 60 | */ 61 | private final Path path; 62 | 63 | /** 64 | * Creates a new config from the given path 65 | * 66 | * @param path the path to the config file 67 | */ 68 | protected TridentConfig(Path path) { 69 | super("", null, null); 70 | this.path = path; 71 | } 72 | 73 | /** 74 | * Init safety static factory method; instance of this 75 | * class is published when creating new config sections 76 | * via the load method in TridentConfigSection 77 | * 78 | * @param path the path to config 79 | * @return the loaded Trident config 80 | */ 81 | public static TridentConfig load(Path path) { 82 | return cachedConfigs.computeIfAbsent(path, k -> { 83 | TridentConfig config = new TridentConfig(path); 84 | try { 85 | config.load(); 86 | } catch (IOException e) { 87 | throw new RuntimeException(e); 88 | } 89 | 90 | return config; 91 | }); 92 | } 93 | 94 | @Override 95 | public File getFile() { 96 | return this.path.toFile(); 97 | } 98 | 99 | @Override 100 | public Path getPath() { 101 | return this.path; 102 | } 103 | 104 | @Override 105 | public File getDirectory() { 106 | return this.path.getParent().toFile(); 107 | } 108 | 109 | @Override 110 | public ConfigSection getRoot() { 111 | return this; 112 | } 113 | 114 | @Override 115 | public ConfigSection getParent() { 116 | return this; 117 | } 118 | 119 | @Override 120 | public void load() throws IOException { 121 | JsonObject object = ConfigIo.readConfig(this.path); 122 | this.read(object); 123 | } 124 | 125 | @Override 126 | public void save() throws IOException { 127 | JsonObject object = this.write(); 128 | ConfigIo.writeConfig(this.path, object); 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/entity/meta/EntityMetaType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.entity.meta; 18 | 19 | import java.lang.annotation.ElementType; 20 | import java.lang.annotation.Retention; 21 | import java.lang.annotation.RetentionPolicy; 22 | import java.lang.annotation.Target; 23 | 24 | @Target(ElementType.TYPE) 25 | @Retention(RetentionPolicy.RUNTIME) 26 | public @interface EntityMetaType { 27 | 28 | Class value(); 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/entity/meta/TridentLivingEntityMeta.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.entity.meta; 18 | 19 | import net.tridentsdk.meta.entity.living.LivingEntityMeta; 20 | import net.tridentsdk.server.net.EntityMetadata; 21 | 22 | import javax.annotation.concurrent.ThreadSafe; 23 | 24 | /** 25 | * @author TridentSDK 26 | * @since 0.5-alpha 27 | */ 28 | @ThreadSafe 29 | public class TridentLivingEntityMeta extends TridentEntityMeta implements LivingEntityMeta { 30 | 31 | public TridentLivingEntityMeta(EntityMetadata metadata) { 32 | super(metadata); 33 | metadata.add(6, EntityMetadata.EntityMetadataType.BYTE, 0b10); 34 | metadata.add(7, EntityMetadata.EntityMetadataType.FLOAT, 20.0f); 35 | metadata.add(8, EntityMetadata.EntityMetadataType.VARINT, 0); 36 | metadata.add(9, EntityMetadata.EntityMetadataType.BOOLEAN, false); 37 | metadata.add(10, EntityMetadata.EntityMetadataType.VARINT, 0); 38 | } 39 | 40 | @Override 41 | public boolean isHandActive() { 42 | return this.getMetadata().get(6).asBit(0); 43 | } 44 | 45 | @Override 46 | public void setHandActive(boolean active) { 47 | this.getMetadata().get(6).setBit(0, active); 48 | } 49 | 50 | @Override 51 | public boolean isMainHandActive() { 52 | return !this.getMetadata().get(6).asBit(1); 53 | } 54 | 55 | @Override 56 | public void setMainHandActive(boolean mainHand) { 57 | this.getMetadata().get(6).setBit(1, !mainHand); 58 | } 59 | 60 | @Override 61 | public float getHealth() { 62 | return this.getMetadata().get(7).asFloat(); 63 | } 64 | 65 | @Override 66 | public void setHealth(float health) { 67 | this.getMetadata().get(7).set(health); 68 | } 69 | 70 | @Override 71 | public int getPotionEffectColor() { 72 | return this.getMetadata().get(8).asInt(); 73 | } 74 | 75 | @Override 76 | public void setPotionEffectColor(int potionEffectColor) { 77 | this.getMetadata().get(8).set(potionEffectColor); 78 | } 79 | 80 | @Override 81 | public boolean isPotionEffectAmbient() { 82 | return this.getMetadata().get(9).asBoolean(); 83 | } 84 | 85 | @Override 86 | public void setPotionEffectAmbient(boolean ambient) { 87 | this.getMetadata().get(9).set(ambient); 88 | } 89 | 90 | @Override 91 | public int getNumberOfArrowsInEntity() { 92 | return this.getMetadata().get(10).asInt(); 93 | } 94 | 95 | @Override 96 | public void setNumberOfArrowsInEntity(int arrows) { 97 | this.getMetadata().get(10).set(arrows); 98 | } 99 | 100 | } 101 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/inventory/TridentItem.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.inventory; 18 | 19 | import lombok.AllArgsConstructor; 20 | import lombok.EqualsAndHashCode; 21 | import lombok.Getter; 22 | import net.tridentsdk.base.Substance; 23 | import net.tridentsdk.inventory.Item; 24 | import net.tridentsdk.meta.ItemMeta; 25 | 26 | import javax.annotation.concurrent.Immutable; 27 | 28 | /** 29 | * Implementation of an inventory item. 30 | */ 31 | @Immutable 32 | @EqualsAndHashCode 33 | @Getter 34 | @AllArgsConstructor 35 | public class TridentItem implements Item { 36 | /** 37 | * An empty item 38 | */ 39 | public static final TridentItem EMPTY = new TridentItem(Substance.AIR, 0, (byte) 0, new ItemMeta()); 40 | 41 | /** 42 | * The item's substance type. 43 | */ 44 | private final Substance substance; 45 | /** 46 | * The amount of items in this stack. 47 | */ 48 | private final int count; 49 | /** 50 | * The 4-bit item data. 51 | */ 52 | private final byte damage; 53 | /** 54 | * The item metadata 55 | */ 56 | private final ItemMeta meta; 57 | 58 | @Override 59 | public boolean isEmpty() { 60 | return this.substance == Substance.AIR; 61 | } 62 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/logger/DebugLogger.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.logger; 18 | 19 | import javax.annotation.concurrent.Immutable; 20 | 21 | /** 22 | * Logger filter which prevents debug messages from being 23 | * passed on. 24 | */ 25 | @Immutable 26 | class NoDebugLogger extends PipelinedLogger { 27 | /** 28 | * Creates a new logger that handles debug messages 29 | * without printing it. 30 | * 31 | * @param next the next logger in the pipeline 32 | */ 33 | public NoDebugLogger(PipelinedLogger next) { 34 | super(next); 35 | } 36 | 37 | @Override 38 | public LogMessageImpl handle(LogMessageImpl msg) { 39 | return msg; 40 | } 41 | 42 | @Override 43 | public void debug(LogMessageImpl msg) { 44 | // No-op 45 | } 46 | } 47 | 48 | /** 49 | * A debug filter logger which allows debug messages to be 50 | * passed along the pipeline, useful for verbose mode. 51 | */ 52 | @Immutable 53 | public class DebugLogger extends PipelinedLogger { 54 | /** 55 | * Create a new logger which logs to the next 56 | * system specific logger. 57 | * 58 | * @param next the next logger in the pipeline 59 | */ 60 | protected DebugLogger(PipelinedLogger next) { 61 | super(next); 62 | } 63 | 64 | /** 65 | * Creates a verbose logger filter 66 | * 67 | * @param next the next logger to which 68 | * the filter will pass messages 69 | * @return a new instance of the logger filter 70 | */ 71 | public static PipelinedLogger verbose(PipelinedLogger next) { 72 | return new DebugLogger(next); 73 | } 74 | 75 | /** 76 | * Creates a non-verbose logger filter which removes 77 | * debug messages from the pipeline 78 | * 79 | * @param next the next logger to which 80 | * the filter will pass messages 81 | * @return a new instance of the logger filter 82 | */ 83 | public static PipelinedLogger noop(PipelinedLogger next) { 84 | return new NoDebugLogger(next); 85 | } 86 | 87 | @Override 88 | public LogMessageImpl handle(LogMessageImpl msg) { 89 | return msg; 90 | } 91 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/logger/DefaultLogger.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.logger; 18 | 19 | import org.fusesource.jansi.AnsiConsole; 20 | 21 | import javax.annotation.concurrent.Immutable; 22 | import java.io.OutputStream; 23 | import java.io.PrintStream; 24 | 25 | /** 26 | * This logger is normally regarded as the underlying 27 | * logger as it is designed to be the final logger in the 28 | * pipeline; it does not pass the logger messages any 29 | * further and logs its output directly to the output. 30 | * 31 | *

Thus such, this class contains the init code required 32 | * to setup the loggers.

33 | */ 34 | @Immutable 35 | public class DefaultLogger extends PipelinedLogger { 36 | /** 37 | * The underlying stream to which output is passed 38 | */ 39 | private final PrintStream stream; 40 | 41 | /** 42 | * Creates a new logger that prints messages to the out 43 | * stream at the bottom of the pipeline. 44 | */ 45 | public DefaultLogger() { 46 | super(null); 47 | this.stream = AnsiConsole.out; 48 | 49 | System.setOut(this.stream); 50 | System.setErr(this.stream); 51 | // force error stream to 52 | // lock on System.out 53 | } 54 | 55 | @Override 56 | public LogMessageImpl handle(LogMessageImpl msg) { 57 | return null; 58 | } 59 | 60 | @Override 61 | public void log(LogMessageImpl msg) { 62 | this.stream.println(msg.format(1)); 63 | } 64 | 65 | @Override 66 | public void success(LogMessageImpl msg) { 67 | this.stream.println(msg.format(1)); 68 | } 69 | 70 | @Override 71 | public void warn(LogMessageImpl msg) { 72 | this.stream.println(msg.format(1)); 73 | } 74 | 75 | @Override 76 | public void error(LogMessageImpl msg) { 77 | this.stream.println(msg.format(1)); 78 | } 79 | 80 | @Override 81 | public void debug(LogMessageImpl msg) { 82 | this.stream.println(msg.format(1)); 83 | } 84 | 85 | @Override 86 | public OutputStream out() { 87 | return this.stream; 88 | } 89 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/logger/LogMessageImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.logger; 18 | 19 | import net.tridentsdk.logger.LogMessage; 20 | 21 | import javax.annotation.concurrent.ThreadSafe; 22 | import java.time.ZonedDateTime; 23 | 24 | /** 25 | * This class represents a message sent by a logger 26 | */ 27 | @ThreadSafe 28 | public class LogMessageImpl implements LogMessage { 29 | /** 30 | * The logger that sent the message 31 | */ 32 | private final InfoLogger source; 33 | /** 34 | * The components to be added to the output 35 | */ 36 | private final String[] components; 37 | /** 38 | * The message that was passed to the logger 39 | */ 40 | private volatile String message; 41 | /** 42 | * The time at which this log message was created 43 | */ 44 | private final ZonedDateTime time; 45 | 46 | /** 47 | * Creates a new log message 48 | * 49 | * @param source the logger that created the message 50 | * @param components additional related info 51 | * @param message the message that was passed 52 | * @param time the time the message was created 53 | */ 54 | public LogMessageImpl(InfoLogger source, String[] components, 55 | String message, ZonedDateTime time) { 56 | this.source = source; 57 | this.components = components; 58 | this.message = message; 59 | this.time = time; 60 | } 61 | 62 | @Override 63 | public InfoLogger getLogger() { 64 | return this.source; 65 | } 66 | 67 | @Override 68 | public String[] getComponents() { 69 | return this.components; 70 | } 71 | 72 | @Override 73 | public String getMessage() { 74 | return this.message; 75 | } 76 | 77 | public void setMessage(String message) { 78 | this.message = message; 79 | } 80 | 81 | @Override 82 | public ZonedDateTime getTime() { 83 | return this.time; 84 | } 85 | 86 | /** 87 | * Appends all of the components and the message. 88 | * 89 | * @param start the start of the component index to add 90 | * @return the string 91 | */ 92 | public String format(int start) { 93 | StringBuilder builder = new StringBuilder(); 94 | for (int i = start; i < this.components.length; i++) { 95 | builder.append(this.components[i]).append(' '); 96 | } 97 | 98 | builder.append(this.message); 99 | return builder.toString(); 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/logger/LoggerHandlers.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.logger; 18 | 19 | import net.tridentsdk.logger.LogHandler; 20 | 21 | import javax.annotation.concurrent.ThreadSafe; 22 | import java.util.Collections; 23 | import java.util.Set; 24 | import java.util.concurrent.ConcurrentHashMap; 25 | 26 | /** 27 | * This class contains the handlers that plugins may use to 28 | * change the output of the logger or their loggers. 29 | */ 30 | @ThreadSafe 31 | public class LoggerHandlers extends PipelinedLogger { 32 | /** 33 | * The set of handlers that intercept all output 34 | */ 35 | private final Set handlers = Collections.newSetFromMap(new ConcurrentHashMap<>()); 36 | 37 | /** 38 | * Creates a new handler class for the all messages log 39 | * interceptors. 40 | * 41 | * @param next the next logger in the pipeline 42 | */ 43 | public LoggerHandlers(PipelinedLogger next) { 44 | super(next); 45 | } 46 | 47 | @Override 48 | public LogMessageImpl handle(LogMessageImpl msg) { 49 | boolean doLog = true; 50 | for (LogHandler handler : this.handlers) { 51 | if (!handler.handle(msg)) { 52 | doLog = false; 53 | } 54 | } 55 | return doLog ? msg : null; 56 | } 57 | 58 | /** 59 | * Obtains the all the handlers that are attached to 60 | * the 61 | * output. 62 | * 63 | * @return the logger handlers 64 | */ 65 | public Set handlers() { 66 | return this.handlers; 67 | } 68 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/net/NetChannelInit.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.net; 18 | 19 | import io.netty.channel.ChannelInitializer; 20 | import io.netty.channel.ChannelPipeline; 21 | import io.netty.channel.socket.SocketChannel; 22 | 23 | import javax.annotation.concurrent.Immutable; 24 | 25 | /** 26 | * Channel initializers register channel handlers to handle 27 | * incoming connections and packets, as well as handle 28 | * output. 29 | */ 30 | @Immutable 31 | public class NetChannelInit extends ChannelInitializer { 32 | @Override 33 | protected void initChannel(SocketChannel socketChannel) throws Exception { 34 | ChannelPipeline pipe = socketChannel.pipeline(); 35 | 36 | pipe.addLast(new InDecoder()); 37 | pipe.addLast(new OutEncoder()); 38 | } 39 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/net/NetEpollServer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.net; 18 | 19 | import io.netty.bootstrap.ServerBootstrap; 20 | import io.netty.channel.ChannelOption; 21 | import io.netty.channel.EventLoopGroup; 22 | import io.netty.channel.epoll.EpollEventLoopGroup; 23 | import io.netty.channel.epoll.EpollServerSocketChannel; 24 | import net.tridentsdk.server.concurrent.PoolSpec; 25 | 26 | import javax.annotation.concurrent.Immutable; 27 | 28 | /** 29 | * Netty native-transport (linux) over JNI implemented 30 | * sockets. 31 | */ 32 | @Immutable 33 | public class NetEpollServer extends NetServer { 34 | private final EventLoopGroup bossGroup = new EpollEventLoopGroup(0, PoolSpec.UNCAUGHT_FACTORY); 35 | private final EventLoopGroup workerGroup = new EpollEventLoopGroup(0, PoolSpec.UNCAUGHT_FACTORY); 36 | 37 | NetEpollServer(String ip, int port) { 38 | super(ip, port); 39 | } 40 | 41 | @Override 42 | public void setup() { 43 | ServerBootstrap b = new ServerBootstrap(); 44 | b.group(this.bossGroup, this.workerGroup) 45 | .channel(EpollServerSocketChannel.class) 46 | .childHandler(new NetChannelInit()) 47 | .option(ChannelOption.SO_BACKLOG, 128) 48 | .childOption(ChannelOption.SO_KEEPALIVE, true); 49 | b.bind(this.ip(), this.port()); 50 | } 51 | 52 | @Override 53 | public void shutdown() throws InterruptedException { 54 | this.bossGroup.shutdownGracefully(); 55 | this.workerGroup.shutdownGracefully(); 56 | } 57 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/net/NetNioServer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.net; 18 | 19 | import io.netty.bootstrap.ServerBootstrap; 20 | import io.netty.channel.ChannelOption; 21 | import io.netty.channel.EventLoopGroup; 22 | import io.netty.channel.nio.NioEventLoopGroup; 23 | import io.netty.channel.socket.nio.NioServerSocketChannel; 24 | import net.tridentsdk.server.concurrent.PoolSpec; 25 | 26 | import javax.annotation.concurrent.Immutable; 27 | 28 | /** 29 | * The default NetServer implementation that uses NIO 30 | * multi-threaded server socket handlers 31 | */ 32 | @Immutable 33 | public class NetNioServer extends NetServer { 34 | private final EventLoopGroup bossGroup = new NioEventLoopGroup(0, PoolSpec.UNCAUGHT_FACTORY); 35 | private final EventLoopGroup workerGroup = new NioEventLoopGroup(0, PoolSpec.UNCAUGHT_FACTORY); 36 | 37 | public NetNioServer(String ip, int port) { 38 | super(ip, port); 39 | } 40 | 41 | @Override 42 | public void setup() { 43 | ServerBootstrap b = new ServerBootstrap(); 44 | b.group(this.bossGroup, this.workerGroup) 45 | .channel(NioServerSocketChannel.class) 46 | .childHandler(new NetChannelInit()) 47 | .option(ChannelOption.SO_BACKLOG, 128) 48 | .childOption(ChannelOption.SO_KEEPALIVE, true); 49 | b.bind(this.ip(), this.port()); 50 | } 51 | 52 | @Override 53 | public void shutdown() throws InterruptedException { 54 | this.bossGroup.shutdownGracefully().await(); 55 | this.workerGroup.shutdownGracefully().await(); 56 | } 57 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/net/NetServer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.net; 18 | 19 | import javax.annotation.concurrent.Immutable; 20 | 21 | /** 22 | * This class handles the network connections for the 23 | * server 24 | * and manages the netty channels, packets, pipelines, etc. 25 | */ 26 | @Immutable 27 | public abstract class NetServer { 28 | /** 29 | * The server IP 30 | */ 31 | private final String ip; 32 | /** 33 | * The server port 34 | */ 35 | private final int port; 36 | 37 | /** 38 | * The net server superconstructor 39 | */ 40 | NetServer(String ip, int port) { 41 | this.ip = ip; 42 | this.port = port; 43 | } 44 | 45 | /** 46 | * Initializer code for server startup. 47 | * 48 | * @param ip the IP address of the interface to bind 49 | * @param port the port which to bind the interface 50 | * @param useNative whether to use native epoll 51 | * @return the new server net handler 52 | */ 53 | public static NetServer init(String ip, int port, boolean useNative) { 54 | boolean nativeCompat = System.getProperty("os.name").toLowerCase().contains("linux"); 55 | 56 | return nativeCompat && useNative ? 57 | new NetEpollServer(ip, port) : new NetNioServer(ip, port); 58 | } 59 | 60 | /** 61 | * Sets up the server. 62 | */ 63 | public abstract void setup(); 64 | 65 | /** 66 | * Shuts down the server. 67 | * 68 | * @throws InterruptedException no 69 | */ 70 | public abstract void shutdown() throws InterruptedException; 71 | 72 | /** 73 | * Obtains the address used to initialize the server. 74 | * 75 | * @return the server address 76 | */ 77 | public String ip() { 78 | return this.ip; 79 | } 80 | 81 | /** 82 | * Obtains the port used to initialize the server. 83 | * 84 | * @return the server port 85 | */ 86 | public int port() { 87 | return this.port; 88 | } 89 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/Packet.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet; 18 | 19 | import javax.annotation.concurrent.Immutable; 20 | 21 | /** 22 | * This class represents an abstraction of a data packet 23 | * that is used by the Minecraft protocol to communicate 24 | * units of information between the client(s) and the 25 | * server. 26 | */ 27 | @Immutable 28 | public class Packet { 29 | /** 30 | * The direction which a packet is sent towards. 31 | */ 32 | public enum Bound { 33 | /** 34 | * Client-bound, out packets. 35 | */ 36 | CLIENT, 37 | /** 38 | * Server-bound, in packets. 39 | */ 40 | SERVER; 41 | 42 | /** 43 | * Obtain the bound of the packet represented by 44 | * the 45 | * given class. 46 | * 47 | * @param cls the class to determine the bound 48 | * @return the bound of the packet 49 | */ 50 | public Bound of(Class cls) { 51 | if (cls.getSuperclass() == PacketIn.class) { 52 | return SERVER; 53 | } else { 54 | return CLIENT; 55 | } 56 | } 57 | } 58 | 59 | /** 60 | * The packet ID 61 | */ 62 | private final int id; 63 | 64 | /** 65 | * The constructor which polls the packet registry in 66 | * order to setup the initializing fields. 67 | * 68 | * @param cls the class of the packet to be registered 69 | */ 70 | public Packet(Class cls) { 71 | int info = PacketRegistry.packetInfo(cls); 72 | this.id = PacketRegistry.idOf(info); 73 | } 74 | 75 | /** 76 | * Obtains the packet ID which identifies the packet 77 | * once it has been sent to or from the client. 78 | * 79 | * @return the packet ID 80 | */ 81 | public int id() { 82 | return this.id; 83 | } 84 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/PacketIn.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.server.net.NetClient; 21 | 22 | import javax.annotation.concurrent.Immutable; 23 | 24 | /** 25 | * Represents a server-bound packet that a Minecraft client 26 | * sends to the server. 27 | */ 28 | @Immutable 29 | public abstract class PacketIn extends Packet { 30 | /** 31 | * The constructor which polls the packet registry in 32 | * order to setup the initializing fields. 33 | * 34 | * @param cls the class of the packet to be registered 35 | */ 36 | public PacketIn(Class cls) { 37 | super(cls); 38 | } 39 | 40 | /** 41 | * Reads the buf data that was sent by the injected 42 | * client. 43 | * 44 | * @param buf the buf of the packet 45 | * @param client the client 46 | */ 47 | public abstract void read(ByteBuf buf, NetClient client); 48 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/PacketOut.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | 21 | import javax.annotation.concurrent.Immutable; 22 | 23 | /** 24 | * Represents a client-bound packet that is sent by the 25 | * server. 26 | */ 27 | @Immutable 28 | public abstract class PacketOut extends Packet { 29 | /** 30 | * The constructor which polls the packet registry in 31 | * order to setup the initializing fields. 32 | * 33 | * @param cls the class of the packet to be registered 34 | */ 35 | public PacketOut(Class cls) { 36 | super(cls); 37 | } 38 | 39 | /** 40 | * Writes the buf of a client-bound packet. 41 | * 42 | *

Packet headers and compression are automatically 43 | * handled when the packet is encoded to the pipeline. 44 | *

45 | * 46 | * @param buf the buf to be written 47 | */ 48 | public abstract void write(ByteBuf buf); 49 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/handshake/HandshakeIn.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.handshake; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.server.net.NetClient; 21 | import net.tridentsdk.server.packet.PacketIn; 22 | import net.tridentsdk.server.packet.login.LoginOutDisconnect; 23 | import net.tridentsdk.server.packet.status.StatusOutResponse; 24 | import net.tridentsdk.ui.chat.ChatComponent; 25 | 26 | import javax.annotation.concurrent.Immutable; 27 | 28 | import static net.tridentsdk.server.net.NetData.rstr; 29 | import static net.tridentsdk.server.net.NetData.rvint; 30 | 31 | /** 32 | * Handshake packet. Sent for both ping and as a getState 33 | * trigger before login. 34 | * 35 | *

Packet is serverbound only

36 | */ 37 | @Immutable 38 | public final class HandshakeIn extends PacketIn { 39 | /** 40 | * Constructor which sets up the packet header details. 41 | */ 42 | public HandshakeIn() { 43 | super(HandshakeIn.class); 44 | } 45 | 46 | @Override 47 | public void read(ByteBuf buf, NetClient client) { 48 | // Schema: 49 | // VarInt:version, String:address, VarInt:port, 50 | // VarInt:nextState 51 | int version = rvint(buf); 52 | String address = rstr(buf); 53 | int port = buf.readUnsignedShort(); 54 | int nextState = rvint(buf); 55 | 56 | if (version != StatusOutResponse.PROTOCOL_VERSION) { 57 | client.sendPacket(new LoginOutDisconnect(ChatComponent.text("Client has the wrong protocol version: " + version))); 58 | } 59 | 60 | client.setState(NetClient.NetState.values()[nextState]); 61 | } 62 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/handshake/LegacyHandshakeIn.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.handshake; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.server.net.NetClient; 21 | import net.tridentsdk.server.packet.PacketIn; 22 | 23 | import javax.annotation.concurrent.Immutable; 24 | 25 | /** 26 | * Legacy handshake packet that is usually sent when the 27 | * client's regular ping fails. 28 | */ 29 | @Immutable 30 | public class LegacyHandshakeIn extends PacketIn { 31 | public LegacyHandshakeIn() { 32 | super(LegacyHandshakeIn.class); 33 | } 34 | 35 | @Override 36 | public void read(ByteBuf buf, NetClient client) { 37 | if (buf.readUnsignedByte() != 1) { 38 | throw new RuntimeException("Legacy handshake has the wrong schema"); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/login/Login.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.login; 18 | 19 | import net.tridentsdk.server.TridentServer; 20 | import net.tridentsdk.server.net.NetClient; 21 | import net.tridentsdk.server.player.TridentPlayer; 22 | import net.tridentsdk.server.util.Cache; 23 | 24 | import javax.annotation.concurrent.ThreadSafe; 25 | import java.util.UUID; 26 | import java.util.concurrent.atomic.AtomicInteger; 27 | import java.util.regex.Pattern; 28 | 29 | /** 30 | * Login utilities. 31 | */ 32 | @ThreadSafe 33 | public final class Login { 34 | /** 35 | * The amount of players that are queued to login 36 | */ 37 | private static final AtomicInteger LOGGING_IN = new AtomicInteger(); 38 | /** 39 | * Cache of UUIDs as Mojang doesn't like it when you 40 | * push the rate limit. 41 | */ 42 | private static final Cache UUID_CACHE = new Cache<>(1000 * 60 * 5); // 5 Minutes 43 | /** 44 | * The pattern used to add back dashes to the UUID 45 | */ 46 | private static final Pattern UUID_PATTERN = Pattern.compile("(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})"); 47 | 48 | // Prevent instantiation 49 | private Login() { 50 | } 51 | 52 | /** 53 | * Ensures that the given network connection is able to 54 | * login to the server. 55 | * 56 | * @param client the client to test 57 | * @return {@code true} if the client may login 58 | */ 59 | public static boolean canLogin(NetClient client) { 60 | if (LOGGING_IN.get() + TridentPlayer.getPlayers().size() >= 61 | TridentServer.cfg().maxPlayers()) { 62 | client.disconnect("Server is full"); 63 | return false; 64 | } 65 | 66 | String name = client.getName(); 67 | if (TridentPlayer.getPlayerNames().containsKey(name)) { 68 | client.disconnect("Player with name \"" + name + "\" already exists"); 69 | return false; 70 | } 71 | 72 | LOGGING_IN.incrementAndGet(); 73 | return true; 74 | } 75 | 76 | /** 77 | * Finishes a player login and removes a login spot. 78 | */ 79 | public static void finish() { 80 | LOGGING_IN.decrementAndGet(); 81 | } 82 | 83 | /** 84 | * Corrects input UUIDs that do not have dashes. 85 | * 86 | * @param name the name to cache 87 | * @param input the raw UUID from mojang 88 | * @return the dashed string 89 | */ 90 | public static UUID convert(String name, String input) { 91 | return UUID_CACHE.get(name, () -> UUID.fromString(UUID_PATTERN.matcher(input).replaceAll("$1-$2-$3-$4-$5"))); 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/login/LoginInStart.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.login; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.server.TridentServer; 21 | import net.tridentsdk.server.net.NetClient; 22 | import net.tridentsdk.server.net.NetCrypto; 23 | import net.tridentsdk.server.packet.PacketIn; 24 | import net.tridentsdk.server.player.TridentPlayer; 25 | 26 | import javax.annotation.concurrent.Immutable; 27 | 28 | import static net.tridentsdk.server.net.NetData.rstr; 29 | 30 | /** 31 | * Login start request made after status switch to 2 due to 32 | * handshake request. 33 | */ 34 | @Immutable 35 | public final class LoginInStart extends PacketIn { 36 | public LoginInStart() { 37 | super(LoginInStart.class); 38 | } 39 | 40 | @Override 41 | public void read(ByteBuf buf, NetClient client) { 42 | String name = rstr(buf); 43 | client.setName(name); 44 | 45 | if (!Login.canLogin(client)) { 46 | return; 47 | } 48 | 49 | if (name.length() > 16 || !name.matches("[a-zA-Z0-9_]+")) { 50 | client.disconnect("Invalid name"); 51 | return; 52 | } 53 | 54 | if (TridentServer.cfg().doAuth()) { 55 | NetCrypto crypto = client.initCrypto(); 56 | client.sendPacket(crypto.reqCrypto()); 57 | } else { 58 | LoginOutSuccess packet = new LoginOutSuccess(client); 59 | client.sendPacket(packet).addListener(future -> 60 | TridentPlayer.spawn(client, name, packet.getUuid(), packet.getTextures())); 61 | } 62 | } 63 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/login/LoginOutCompression.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.login; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.server.TridentServer; 21 | import net.tridentsdk.server.packet.PacketOut; 22 | 23 | import javax.annotation.concurrent.Immutable; 24 | 25 | import static net.tridentsdk.server.net.NetData.wvint; 26 | 27 | /** 28 | * Indicates to the client that the server requests to 29 | * compress packets. 30 | */ 31 | @Immutable 32 | public final class LoginOutCompression extends PacketOut { 33 | public LoginOutCompression() { 34 | super(LoginOutCompression.class); 35 | } 36 | 37 | @Override 38 | public void write(ByteBuf buf) { 39 | wvint(buf, TridentServer.cfg().compressionThresh()); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/login/LoginOutDisconnect.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.login; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.server.packet.PacketOut; 21 | import net.tridentsdk.ui.chat.ChatComponent; 22 | 23 | import static net.tridentsdk.server.net.NetData.wstr; 24 | 25 | /** 26 | * This packet is sent to the client in states LOGIN, PLAY 27 | * to indicate that the player will be disconnected from 28 | * the server. 29 | */ 30 | public final class LoginOutDisconnect extends PacketOut { 31 | /** 32 | * The message displayed on the screen once the player 33 | * is disconnected 34 | */ 35 | private final ChatComponent reason; 36 | 37 | public LoginOutDisconnect(ChatComponent reason) { 38 | super(LoginOutDisconnect.class); 39 | this.reason = reason; 40 | } 41 | 42 | @Override 43 | public void write(ByteBuf buf) { 44 | wstr(buf, this.reason.toString()); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/login/LoginOutEncryptionRequest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.login; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.server.packet.PacketOut; 21 | 22 | import javax.annotation.concurrent.Immutable; 23 | 24 | import static net.tridentsdk.server.net.NetData.wvint; 25 | 26 | /** 27 | * Packet sent by the server after {@link LoginInStart} to 28 | * attempt to request the client to use packet encryption. 29 | */ 30 | @Immutable 31 | public final class LoginOutEncryptionRequest extends PacketOut { 32 | /** 33 | * The encoded public key 34 | */ 35 | private final byte[] publicKey; 36 | /** 37 | * The token 38 | */ 39 | private final byte[] token; 40 | 41 | public LoginOutEncryptionRequest(byte[] publicKey, byte[] token) { 42 | super(LoginOutEncryptionRequest.class); 43 | this.publicKey = publicKey; 44 | this.token = token; 45 | } 46 | 47 | @Override 48 | public void write(ByteBuf buf) { 49 | wvint(buf, 0); // empty string 50 | 51 | wvint(buf, this.publicKey.length); 52 | buf.writeBytes(this.publicKey); 53 | wvint(buf, this.token.length); 54 | buf.writeBytes(this.token); 55 | } 56 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayInAnimation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.server.net.NetClient; 21 | import net.tridentsdk.server.net.NetData; 22 | import net.tridentsdk.server.packet.PacketIn; 23 | import net.tridentsdk.server.player.RecipientSelector; 24 | import net.tridentsdk.server.player.TridentPlayer; 25 | 26 | /** 27 | * @author TridentSDK 28 | * @since 0.5-alpha 29 | */ 30 | public class PlayInAnimation extends PacketIn { 31 | 32 | public PlayInAnimation() { 33 | super(PlayInAnimation.class); 34 | } 35 | 36 | @Override 37 | public void read(ByteBuf buf, NetClient client) { 38 | int animation = NetData.rvint(buf); 39 | 40 | TridentPlayer player = client.getPlayer(); 41 | PlayOutAnimation packet = new PlayOutAnimation(player, animation == 0 ? PlayOutAnimation.AnimationType.SWING_MAIN_ARM : PlayOutAnimation.AnimationType.SWING_OFFHAND); 42 | RecipientSelector.whoCanSee(player, true, packet); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayInChat.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.server.concurrent.PoolSpec; 21 | import net.tridentsdk.server.concurrent.ServerThreadPool; 22 | import net.tridentsdk.server.net.NetClient; 23 | import net.tridentsdk.server.packet.PacketIn; 24 | import net.tridentsdk.server.player.TridentPlayer; 25 | 26 | import javax.annotation.concurrent.Immutable; 27 | 28 | import static net.tridentsdk.server.net.NetData.rstr; 29 | 30 | /** 31 | * This packet is received by the server when a player 32 | * sends 33 | * a chat message. 34 | */ 35 | @Immutable 36 | public final class PlayInChat extends PacketIn { 37 | public PlayInChat() { 38 | super(PlayInChat.class); 39 | } 40 | 41 | @Override 42 | public void read(ByteBuf buf, NetClient client) { 43 | TridentPlayer player = client.getPlayer(); 44 | String msg = rstr(buf); 45 | 46 | if (msg.startsWith("/")) { 47 | ServerThreadPool.forSpec(PoolSpec.PLUGINS).execute(() -> player.runCommand(msg.replaceFirst("/", ""))); 48 | } else { 49 | player.chat(msg); 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayInClientSettings.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.server.net.NetClient; 21 | import net.tridentsdk.server.net.NetData; 22 | import net.tridentsdk.server.packet.PacketIn; 23 | import net.tridentsdk.server.player.TridentPlayer; 24 | import net.tridentsdk.server.player.TridentPlayerMeta; 25 | import net.tridentsdk.ui.chat.ClientChatMode; 26 | 27 | /** 28 | * @author TridentSDK 29 | * @since 0.5-alpha 30 | */ 31 | public class PlayInClientSettings extends PacketIn { 32 | public PlayInClientSettings() { 33 | super(PlayInClientSettings.class); 34 | } 35 | 36 | @Override 37 | public void read(ByteBuf buf, NetClient client) { 38 | String locale = NetData.rstr(buf); 39 | byte renderDistance = buf.readByte(); 40 | ClientChatMode chatMode = ClientChatMode.of(NetData.rvint(buf)); 41 | boolean chatColors = buf.readBoolean(); 42 | byte skinFlags = buf.readByte(); 43 | int mainHand = buf.readByte(); 44 | 45 | TridentPlayer player = client.getPlayer(); 46 | TridentPlayerMeta metadata = player.getMetadata(); 47 | player.setRenderDistance(renderDistance); 48 | player.setLocale(locale); 49 | player.setChatColors(chatColors); 50 | player.setChatMode(chatMode); 51 | metadata.setSkinFlags(skinFlags); 52 | metadata.setLeftHandMain(mainHand == 0); 53 | player.updateMetadata(); 54 | } 55 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayInClientStatus.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.server.net.NetClient; 21 | import net.tridentsdk.server.packet.PacketIn; 22 | 23 | import javax.annotation.concurrent.Immutable; 24 | 25 | import static net.tridentsdk.server.net.NetData.rvint; 26 | 27 | /** 28 | * Sent by the client to indicate status update (such as 29 | * finding stats or respawning). 30 | */ 31 | @Immutable 32 | public final class PlayInClientStatus extends PacketIn { 33 | public PlayInClientStatus() { 34 | super(PlayInClientStatus.class); 35 | } 36 | 37 | @Override 38 | public void read(ByteBuf buf, NetClient client) { 39 | int status = rvint(buf); 40 | } 41 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayInCloseWindow.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.server.inventory.TridentInventory; 21 | import net.tridentsdk.server.net.NetClient; 22 | import net.tridentsdk.server.packet.PacketIn; 23 | 24 | import javax.annotation.concurrent.Immutable; 25 | 26 | @Immutable 27 | public class PlayInCloseWindow extends PacketIn { 28 | public PlayInCloseWindow() { 29 | super(PlayInCloseWindow.class); 30 | } 31 | 32 | @Override 33 | public void read(ByteBuf buf, NetClient client) { 34 | short windowId = buf.readUnsignedByte(); 35 | if (windowId == 0) { 36 | return; // Player inventory, no close needed 37 | } 38 | 39 | TridentInventory.close(windowId, client.getPlayer()); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayInCreativeInventoryAction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.inventory.Item; 21 | import net.tridentsdk.server.inventory.TridentPlayerInventory; 22 | import net.tridentsdk.server.net.NetClient; 23 | import net.tridentsdk.server.net.Slot; 24 | import net.tridentsdk.server.packet.PacketIn; 25 | 26 | import javax.annotation.concurrent.Immutable; 27 | 28 | /** 29 | * Sent by the client whenever an inventory action occurs 30 | * in creative mode. 31 | */ 32 | @Immutable 33 | public class PlayInCreativeInventoryAction extends PacketIn { 34 | public PlayInCreativeInventoryAction() { 35 | super(PlayInCreativeInventoryAction.class); 36 | } 37 | 38 | @Override 39 | public void read(ByteBuf buf, NetClient client) { 40 | int slot = buf.readShort(); 41 | Slot item = Slot.read(buf); 42 | 43 | TridentPlayerInventory inventory = client.getPlayer().getInventory(); 44 | if (slot == -1) { 45 | Item drop = item.toItem(); 46 | // TODO drop it 47 | return; 48 | } 49 | 50 | if (item.getId() == -1) { 51 | inventory.remove(slot, Integer.MAX_VALUE); 52 | } else { 53 | inventory.add(slot, item.toItem(), item.getCount()); 54 | } 55 | } 56 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayInEntityAction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.server.net.NetClient; 21 | import net.tridentsdk.server.net.NetData; 22 | import net.tridentsdk.server.packet.PacketIn; 23 | import net.tridentsdk.server.player.TridentPlayer; 24 | 25 | /** 26 | * @author TridentSDK 27 | * @since 0.5-alpha 28 | */ 29 | public class PlayInEntityAction extends PacketIn { 30 | 31 | public PlayInEntityAction() { 32 | super(PlayInEntityAction.class); 33 | } 34 | 35 | @Override 36 | public void read(ByteBuf buf, NetClient client) { 37 | int entityId = NetData.rvint(buf); 38 | int actionId = NetData.rvint(buf); 39 | int jumpBoost = NetData.rvint(buf); 40 | 41 | TridentPlayer player = client.getPlayer(); 42 | switch (actionId) { 43 | case 0: // start crouching 44 | player.setCrouching(true); 45 | break; 46 | case 1: // stop crouching 47 | player.setCrouching(false); 48 | break; 49 | case 2: // leave bed 50 | // TODO 51 | break; 52 | case 3: // start sprinting 53 | player.setSprinting(true); 54 | break; 55 | case 4: // stop sprinting 56 | player.setSprinting(false); 57 | break; 58 | case 5: // start jump w/ horse 59 | // TODO 60 | break; 61 | case 6: // stop jump w/ horse 62 | // TODO 63 | break; 64 | case 7: // open horse inventory 65 | // TODO 66 | break; 67 | case 8: // start flying w/ elytra 68 | // TODO 69 | break; 70 | default: 71 | break; 72 | } 73 | 74 | player.updateMetadata(); 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayInKeepAlive.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.server.net.NetClient; 21 | import net.tridentsdk.server.packet.PacketIn; 22 | import net.tridentsdk.server.util.Cache; 23 | 24 | import javax.annotation.concurrent.Immutable; 25 | import java.util.concurrent.ThreadLocalRandom; 26 | 27 | import static net.tridentsdk.server.net.NetData.rvint; 28 | 29 | /** 30 | * Sent by the client in order to enusre that the 31 | * connection 32 | * remains active. 33 | */ 34 | @Immutable 35 | public final class PlayInKeepAlive extends PacketIn { 36 | /** 37 | * The keep alive time cache 38 | */ 39 | private static final Cache TICK_IDS = 40 | new Cache<>(NetClient.KEEP_ALIVE_KICK_NANOS / 1000000, (client, id) -> { 41 | client.disconnect("No KeepAlive response"); 42 | return true; 43 | }); 44 | 45 | /** 46 | * Obtains the next keep alive ID for the given net 47 | * client 48 | * 49 | * @param client the client 50 | * @return the next teleport ID 51 | */ 52 | public static int query(NetClient client) { 53 | return TICK_IDS.compute(client, (k, v) -> { 54 | if (v == null) { 55 | // retarded int limit on VarInt, idk 56 | return ThreadLocalRandom.current().nextInt(0xFFFFFFF); 57 | } else { 58 | client.disconnect("No KeepAlive response"); 59 | return null; 60 | } 61 | }); 62 | } 63 | 64 | public PlayInKeepAlive() { 65 | super(PlayInKeepAlive.class); 66 | } 67 | 68 | @Override 69 | public void read(ByteBuf buf, NetClient client) { 70 | int id = rvint(buf); 71 | Integer localId = TICK_IDS.get(client); 72 | 73 | if (localId != null && id != localId) { 74 | client.disconnect("Keep alive ID mismatch, actual:" + localId + " rcvd:" + id); 75 | return; 76 | } 77 | 78 | if (System.nanoTime() - client.lastKeepAlive() > NetClient.KEEP_ALIVE_KICK_NANOS) { 79 | client.disconnect("Timed out"); 80 | return; 81 | } 82 | 83 | TICK_IDS.compute(client, (k, v) -> null); 84 | } 85 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayInLook.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.server.net.NetClient; 21 | import net.tridentsdk.server.packet.PacketIn; 22 | import net.tridentsdk.server.player.TridentPlayer; 23 | 24 | import javax.annotation.concurrent.Immutable; 25 | 26 | /** 27 | * Sent by the player whenever the player turns or looks 28 | * up and down without moving. 29 | */ 30 | @Immutable 31 | public final class PlayInLook extends PacketIn { 32 | public PlayInLook() { 33 | super(PlayInLook.class); 34 | } 35 | 36 | @Override 37 | public void read(ByteBuf buf, NetClient client) { 38 | float yaw = buf.readFloat(); 39 | float pitch = buf.readFloat(); 40 | boolean onGround = buf.readBoolean(); 41 | 42 | TridentPlayer player = client.getPlayer(); 43 | if (player == null) return; 44 | player.setPosition(player.getPosition().setYaw(yaw).setPitch(pitch), false); 45 | player.setOnGround(onGround); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayInPlayer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.server.net.NetClient; 21 | import net.tridentsdk.server.packet.PacketIn; 22 | 23 | import javax.annotation.concurrent.Immutable; 24 | 25 | /** 26 | * Sent by the client upon joining the server in order to 27 | * update the player. 28 | */ 29 | @Immutable 30 | public final class PlayInPlayer extends PacketIn { 31 | public PlayInPlayer() { 32 | super(PlayInPlayer.class); 33 | } 34 | 35 | @Override 36 | public void read(ByteBuf buf, NetClient client) { 37 | boolean onGround = buf.readBoolean(); 38 | client.getPlayer().setOnGround(onGround); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayInPlayerAbilities.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.entity.living.Player; 21 | import net.tridentsdk.server.net.NetClient; 22 | import net.tridentsdk.server.packet.PacketIn; 23 | import net.tridentsdk.server.player.TridentPlayer; 24 | 25 | /** 26 | * @author Nick Robson 27 | */ 28 | public class PlayInPlayerAbilities extends PacketIn { 29 | 30 | public PlayInPlayerAbilities() { 31 | super(PlayInPlayerAbilities.class); 32 | } 33 | 34 | @Override 35 | public void read(ByteBuf buf, NetClient client) { 36 | byte flags = buf.readByte(); 37 | 38 | boolean isGod = (flags & 0x08) != 0; 39 | boolean canFly = (flags & 0x04) != 0; 40 | boolean isFlying = (flags & 0x02) != 0; 41 | boolean isCreative = (flags & 0x01) != 0; 42 | 43 | float flyingSpeed = buf.readFloat(); 44 | float walkingSpeed = buf.readFloat(); 45 | 46 | // NOTE: We have to be very careful here, since a hacked client can easily send these things. 47 | 48 | TridentPlayer player = client.getPlayer(); 49 | player.setSprinting(Double.compare(walkingSpeed, Player.DEFAULT_SPRINT_SPEED) == 0); 50 | 51 | if (player.canFly()) { 52 | player.setFlyingSpeed(flyingSpeed); 53 | player.setFlying(isFlying, false); 54 | } else { 55 | player.setFlying(false, false); 56 | } 57 | 58 | client.sendPacket(new PlayOutPlayerAbilities(player)); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayInPos.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.server.net.NetClient; 21 | import net.tridentsdk.server.packet.PacketIn; 22 | import net.tridentsdk.server.player.TridentPlayer; 23 | 24 | import javax.annotation.concurrent.Immutable; 25 | 26 | /** 27 | * Packet received by the server upon the client requesting 28 | * the player entity to move. 29 | */ 30 | @Immutable 31 | public final class PlayInPos extends PacketIn { 32 | public PlayInPos() { 33 | super(PlayInPos.class); 34 | } 35 | 36 | @Override 37 | public void read(ByteBuf buf, NetClient client) { 38 | double x = buf.readDouble(); 39 | double feetY = buf.readDouble(); 40 | double z = buf.readDouble(); 41 | boolean onGround = buf.readBoolean(); 42 | 43 | TridentPlayer player = client.getPlayer(); 44 | player.setPosition(player.getPosition().set(x, feetY, z), false); 45 | player.setOnGround(onGround); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayInPosLook.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.base.Position; 21 | import net.tridentsdk.server.net.NetClient; 22 | import net.tridentsdk.server.packet.PacketIn; 23 | import net.tridentsdk.server.player.TridentPlayer; 24 | 25 | import javax.annotation.concurrent.Immutable; 26 | 27 | /** 28 | * Client confirmation of the player's current position and 29 | * look direction prior to spawning. 30 | */ 31 | @Immutable 32 | public final class PlayInPosLook extends PacketIn { 33 | public PlayInPosLook() { 34 | super(PlayInPosLook.class); 35 | } 36 | 37 | @Override 38 | public void read(ByteBuf buf, NetClient client) { 39 | TridentPlayer player = client.getPlayer(); 40 | double x = buf.readDouble(); 41 | double y = buf.readDouble(); 42 | double z = buf.readDouble(); 43 | float yaw = buf.readFloat(); 44 | float pitch = buf.readFloat(); 45 | boolean isOnGround = buf.readBoolean(); 46 | 47 | player.setPosition(new Position(player.getWorld(), x, y, z, yaw, pitch), false); 48 | player.setOnGround(isOnGround); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayInSetSlot.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.server.net.NetClient; 21 | import net.tridentsdk.server.packet.PacketIn; 22 | 23 | import javax.annotation.concurrent.Immutable; 24 | 25 | /** 26 | * Sent by the client to indicate to the server that the 27 | * player has selected a new slot. 28 | */ 29 | @Immutable 30 | public class PlayInSetSlot extends PacketIn { 31 | public PlayInSetSlot() { 32 | super(PlayInSetSlot.class); 33 | } 34 | 35 | @Override 36 | public void read(ByteBuf buf, NetClient client) { 37 | client.getPlayer().getInventory().setSelectedSlot(buf.readShort()); 38 | } 39 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayInUseEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.server.net.NetClient; 21 | import net.tridentsdk.server.packet.PacketIn; 22 | 23 | import javax.annotation.concurrent.Immutable; 24 | 25 | import static net.tridentsdk.server.net.NetData.rvint; 26 | 27 | /** 28 | * Sent by the client to the server whenever a player 29 | * interacts with an entity. 30 | */ 31 | @Immutable 32 | public class PlayInUseEntity extends PacketIn { 33 | public PlayInUseEntity() { 34 | super(PlayInUseEntity.class); 35 | } 36 | 37 | @Override 38 | public void read(ByteBuf buf, NetClient client) { 39 | int target = rvint(buf); // eid I think 40 | int type = rvint(buf); // 0=interact 1=attack 2=inat 41 | float x = Float.NaN; 42 | float y = Float.NaN; 43 | float z = Float.NaN; 44 | int hand = -1; 45 | if (type == 2) { 46 | x = buf.readFloat(); 47 | y = buf.readFloat(); 48 | z = buf.readFloat(); 49 | } 50 | 51 | if (type == 0 || type == 2) { 52 | hand = rvint(buf); // 0=main 53 | } 54 | } 55 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayInUseItem.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.event.player.PlayerInteractEvent; 21 | import net.tridentsdk.inventory.Item; 22 | import net.tridentsdk.inventory.PlayerInventory; 23 | import net.tridentsdk.server.TridentServer; 24 | import net.tridentsdk.server.net.NetClient; 25 | import net.tridentsdk.server.packet.PacketIn; 26 | import net.tridentsdk.server.player.TridentPlayer; 27 | 28 | import javax.annotation.concurrent.Immutable; 29 | 30 | import static net.tridentsdk.server.net.NetData.rvint; 31 | 32 | @Immutable 33 | public final class PlayInUseItem extends PacketIn { 34 | public PlayInUseItem() { 35 | super(PlayInUseItem.class); 36 | } 37 | 38 | @Override 39 | public void read(ByteBuf buf, NetClient client) { 40 | TridentPlayer player = client.getPlayer(); 41 | PlayerInventory inventory = player.getInventory(); 42 | Item cur = rvint(buf) == 0 ? inventory.getHeldItem() : inventory.getOffHeldItem(); 43 | TridentServer.getInstance().getEventController().dispatch(new PlayerInteractEvent(player), e -> { 44 | if (!e.isCancelled()) { 45 | // TODO actions 46 | } 47 | }); 48 | } 49 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayOutAnimation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.server.net.NetData; 21 | import net.tridentsdk.server.packet.PacketOut; 22 | import net.tridentsdk.server.player.TridentPlayer; 23 | 24 | /** 25 | * @author TridentSDK 26 | * @since 0.5-alpha 27 | */ 28 | public class PlayOutAnimation extends PacketOut { 29 | 30 | private final TridentPlayer player; 31 | private final AnimationType animationType; 32 | 33 | public PlayOutAnimation(TridentPlayer player, AnimationType animationType) { 34 | super(PlayOutAnimation.class); 35 | this.player = player; 36 | this.animationType = animationType; 37 | } 38 | 39 | @Override 40 | public void write(ByteBuf buf) { 41 | NetData.wvint(buf, this.player.getId()); 42 | buf.writeByte(this.animationType.ordinal()); 43 | } 44 | 45 | public enum AnimationType { 46 | 47 | SWING_MAIN_ARM, 48 | TAKE_DAMAGE, 49 | LEAVE_BED, 50 | SWING_OFFHAND, 51 | CRITICAL_EFFECT, 52 | MAGIC_CRITICAL_EFFECT 53 | 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayOutBlockChange.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.base.Position; 21 | import net.tridentsdk.server.packet.PacketOut; 22 | 23 | import javax.annotation.concurrent.Immutable; 24 | 25 | import static net.tridentsdk.server.net.NetData.wvec; 26 | import static net.tridentsdk.server.net.NetData.wvint; 27 | 28 | /** 29 | * Sent by the server to indicate to the client that the 30 | * block at the given location has changed to the new 31 | * value. 32 | */ 33 | @Immutable 34 | public final class PlayOutBlockChange extends PacketOut { 35 | private final Position block; 36 | private final int newBlock; 37 | 38 | public PlayOutBlockChange(Position block, int newBlock) { 39 | super(PlayOutBlockChange.class); 40 | this.block = block; 41 | this.newBlock = newBlock; 42 | } 43 | 44 | @Override 45 | public void write(ByteBuf buf) { 46 | wvec(buf, this.block); 47 | wvint(buf, this.newBlock); 48 | } 49 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayOutChat.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import lombok.Getter; 21 | import net.tridentsdk.server.packet.PacketOut; 22 | import net.tridentsdk.ui.chat.ChatComponent; 23 | import net.tridentsdk.ui.chat.ChatType; 24 | 25 | import javax.annotation.concurrent.Immutable; 26 | 27 | import static net.tridentsdk.server.net.NetData.wstr; 28 | 29 | /** 30 | * This packet is sent by the server to broadcast an 31 | * incoming chat packet to the entire server, or to send 32 | * a message to certain players. 33 | */ 34 | @Immutable 35 | @Getter 36 | public final class PlayOutChat extends PacketOut { 37 | /** 38 | * The chat message being sent 39 | */ 40 | private final ChatComponent chat; 41 | /** 42 | * The type of chat being sent to the player 43 | */ 44 | private final ChatType type; 45 | /** 46 | * Whether or not chat colors is enabled 47 | */ 48 | private final boolean chatColors; 49 | 50 | public PlayOutChat(ChatComponent chat, ChatType type, boolean chatColors) { 51 | super(PlayOutChat.class); 52 | this.chat = chat; 53 | this.type = type; 54 | this.chatColors = chatColors; 55 | } 56 | 57 | @Override 58 | public void write(ByteBuf buf) { 59 | if (!this.chatColors) { 60 | wstr(buf, this.chat.stripColor().toString()); 61 | } else { 62 | wstr(buf, this.chat.toString()); 63 | } 64 | 65 | buf.writeByte(this.type.ordinal()); 66 | } 67 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayOutChunk.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.server.packet.PacketOut; 21 | import net.tridentsdk.server.world.TridentChunk; 22 | 23 | import javax.annotation.concurrent.Immutable; 24 | 25 | /** 26 | * Sends the client the data contained by the chunk. 27 | */ 28 | @Immutable 29 | public final class PlayOutChunk extends PacketOut { 30 | private final TridentChunk chunk; 31 | 32 | public PlayOutChunk(TridentChunk chunk) { 33 | super(PlayOutChunk.class); 34 | this.chunk = chunk; 35 | } 36 | 37 | @Override 38 | public void write(ByteBuf buf) { 39 | boolean doGUContinuous = true; 40 | 41 | buf.writeInt(this.chunk.getX()); 42 | buf.writeInt(this.chunk.getZ()); 43 | buf.writeBoolean(doGUContinuous); 44 | this.chunk.write(buf, doGUContinuous); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayOutDestroyEntities.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.entity.Entity; 21 | import net.tridentsdk.server.packet.PacketOut; 22 | 23 | import javax.annotation.concurrent.Immutable; 24 | import java.util.List; 25 | 26 | import static net.tridentsdk.server.net.NetData.wvint; 27 | 28 | @Immutable 29 | public final class PlayOutDestroyEntities extends PacketOut { 30 | 31 | private final List entities; 32 | 33 | public PlayOutDestroyEntities(List entities) { 34 | super(PlayOutDestroyEntities.class); 35 | this.entities = entities; 36 | } 37 | 38 | @Override 39 | public void write(ByteBuf buf) { 40 | wvint(buf, this.entities.size()); 41 | this.entities.forEach(entity -> wvint(buf, entity.getId())); 42 | } 43 | 44 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayOutDifficulty.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.server.packet.PacketOut; 21 | import net.tridentsdk.world.World; 22 | import net.tridentsdk.world.opt.Difficulty; 23 | 24 | import javax.annotation.concurrent.Immutable; 25 | 26 | /** 27 | * This packet is sent after {@link PlayOutPluginMsg} to 28 | * communicate the current difficulty on the player's 29 | * world. 30 | */ 31 | @Immutable 32 | public final class PlayOutDifficulty extends PacketOut { 33 | /** 34 | * The world difficulty 35 | */ 36 | private final Difficulty difficulty; 37 | 38 | public PlayOutDifficulty(World world) { 39 | super(PlayOutDifficulty.class); 40 | this.difficulty = world.getWorldOptions().getDifficulty(); 41 | } 42 | 43 | @Override 44 | public void write(ByteBuf buf) { 45 | buf.writeByte(this.difficulty.asByte()); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayOutDisconnect.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.server.packet.PacketOut; 21 | import net.tridentsdk.ui.chat.ChatComponent; 22 | 23 | import javax.annotation.concurrent.Immutable; 24 | 25 | import static net.tridentsdk.server.net.NetData.wstr; 26 | 27 | /** 28 | * Disconnect packet for PLAY. 29 | */ 30 | @Immutable 31 | public final class PlayOutDisconnect extends PacketOut { 32 | /** 33 | * The reason why the player is disconnected 34 | */ 35 | private final ChatComponent reason; 36 | 37 | public PlayOutDisconnect(ChatComponent reason) { 38 | super(PlayOutDisconnect.class); 39 | this.reason = reason; 40 | } 41 | 42 | @Override 43 | public void write(ByteBuf buf) { 44 | wstr(buf, this.reason.toString()); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayOutEntityHeadLook.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.entity.Entity; 21 | import net.tridentsdk.server.packet.PacketOut; 22 | 23 | import javax.annotation.concurrent.Immutable; 24 | 25 | import static net.tridentsdk.server.net.NetData.convertAngle; 26 | import static net.tridentsdk.server.net.NetData.wvint; 27 | 28 | @Immutable 29 | public final class PlayOutEntityHeadLook extends PacketOut { 30 | 31 | private final int id; 32 | private final float yaw; 33 | 34 | public PlayOutEntityHeadLook(Entity entity) { 35 | super(PlayOutEntityHeadLook.class); 36 | this.id = entity.getId(); 37 | this.yaw = entity.getPosition().getYaw(); 38 | } 39 | 40 | @Override 41 | public void write(ByteBuf buf) { 42 | wvint(buf, this.id); 43 | 44 | buf.writeByte(convertAngle(this.yaw)); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayOutEntityLook.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.entity.Entity; 21 | import net.tridentsdk.server.packet.PacketOut; 22 | 23 | import javax.annotation.concurrent.Immutable; 24 | 25 | import static net.tridentsdk.server.net.NetData.convertAngle; 26 | import static net.tridentsdk.server.net.NetData.wvint; 27 | 28 | @Immutable 29 | public final class PlayOutEntityLook extends PacketOut { 30 | 31 | private final int id; 32 | private final float yaw; 33 | private final float pitch; 34 | private final boolean onGround; 35 | 36 | public PlayOutEntityLook(Entity entity) { 37 | super(PlayOutEntityLook.class); 38 | this.id = entity.getId(); 39 | this.yaw = entity.getPosition().getYaw(); 40 | this.pitch = entity.getPosition().getPitch(); 41 | this.onGround = entity.isOnGround(); 42 | } 43 | 44 | @Override 45 | public void write(ByteBuf buf) { 46 | wvint(buf, this.id); 47 | 48 | buf.writeByte(convertAngle(this.yaw)); 49 | buf.writeByte(convertAngle(this.pitch)); 50 | 51 | buf.writeBoolean(this.onGround); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayOutEntityLookAndRelativeMove.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.base.Position; 21 | import net.tridentsdk.entity.Entity; 22 | import net.tridentsdk.server.packet.PacketOut; 23 | 24 | import javax.annotation.concurrent.Immutable; 25 | 26 | import static net.tridentsdk.server.net.NetData.wvint; 27 | 28 | @Immutable 29 | public final class PlayOutEntityLookAndRelativeMove extends PacketOut { 30 | 31 | private final int id; 32 | private final double yaw; 33 | private final double pitch; 34 | private final boolean onGround; 35 | private final Position delta; 36 | 37 | public PlayOutEntityLookAndRelativeMove(Entity entity, Position delta) { 38 | super(PlayOutEntityLookAndRelativeMove.class); 39 | this.id = entity.getId(); 40 | this.yaw = entity.getPosition().getYaw(); 41 | this.pitch = entity.getPosition().getPitch(); 42 | this.onGround = entity.isOnGround(); 43 | this.delta = delta.multiply(4096, 4096, 4096); 44 | } 45 | 46 | @Override 47 | public void write(ByteBuf buf) { 48 | wvint(buf, this.id); 49 | 50 | buf.writeShort((int) this.delta.getX()); 51 | buf.writeShort((int) this.delta.getY()); 52 | buf.writeShort((int) this.delta.getZ()); 53 | 54 | buf.writeByte((int) (this.yaw * 256 / 360)); 55 | buf.writeByte((int) (this.pitch / 1.4)); 56 | 57 | buf.writeBoolean(this.onGround); 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayOutEntityMetadata.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.server.entity.TridentEntity; 21 | import net.tridentsdk.server.net.NetData; 22 | import net.tridentsdk.server.packet.PacketOut; 23 | 24 | /** 25 | * @author TridentSDK 26 | * @since 0.5-alpha 27 | */ 28 | public class PlayOutEntityMetadata extends PacketOut { 29 | 30 | private final TridentEntity entity; 31 | 32 | public PlayOutEntityMetadata(TridentEntity entity) { 33 | super(PlayOutEntityMetadata.class); 34 | this.entity = entity; 35 | } 36 | 37 | @Override 38 | public void write(ByteBuf buf) { 39 | NetData.wvint(buf, this.entity.getId()); 40 | this.entity.getMetadata().getMetadata().write(buf); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayOutEntityRelativeMove.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.base.Position; 21 | import net.tridentsdk.entity.Entity; 22 | import net.tridentsdk.server.packet.PacketOut; 23 | 24 | import javax.annotation.concurrent.Immutable; 25 | 26 | import static net.tridentsdk.server.net.NetData.wvint; 27 | 28 | @Immutable 29 | public final class PlayOutEntityRelativeMove extends PacketOut { 30 | 31 | private final Entity entity; 32 | private final Position delta; 33 | 34 | public PlayOutEntityRelativeMove(Entity entity, Position delta) { 35 | super(PlayOutEntityRelativeMove.class); 36 | this.entity = entity; 37 | this.delta = delta.multiply(4096, 4096, 4096); 38 | } 39 | 40 | @Override 41 | public void write(ByteBuf buf) { 42 | wvint(buf, this.entity.getId()); 43 | 44 | buf.writeShort((short) this.delta.getX()); 45 | buf.writeShort((short) this.delta.getY()); 46 | buf.writeShort((short) this.delta.getZ()); 47 | 48 | buf.writeBoolean(this.entity.isOnGround()); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayOutEquipment.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.inventory.Item; 21 | import net.tridentsdk.server.entity.TridentEntity; 22 | import net.tridentsdk.server.net.Slot; 23 | import net.tridentsdk.server.packet.PacketOut; 24 | 25 | import javax.annotation.concurrent.Immutable; 26 | 27 | import static net.tridentsdk.server.net.NetData.wvint; 28 | 29 | /** 30 | * The packet is sent by the server to inform the client of 31 | * an entity's equipped armor and held item(s). 32 | */ 33 | @Immutable 34 | public class PlayOutEquipment extends PacketOut { 35 | /** 36 | * The entity with the given equipment 37 | */ 38 | private final TridentEntity entity; 39 | /** 40 | * The slot number 41 | * 42 | * - 0 = main 43 | * - 1 = off 44 | * - 2 = boots 45 | * ... 46 | * - 5 = helmet 47 | */ 48 | private final int slot; 49 | /** 50 | * The item to place in that slot 51 | */ 52 | private final Item item; 53 | 54 | public PlayOutEquipment(TridentEntity entity, int slot, Item item) { 55 | super(PlayOutEquipment.class); 56 | this.entity = entity; 57 | this.slot = slot; 58 | this.item = item; 59 | } 60 | 61 | @Override 62 | public void write(ByteBuf buf) { 63 | wvint(buf, this.entity.getId()); 64 | wvint(buf, this.slot); 65 | Slot.newSlot(this.item).write(buf); 66 | } 67 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayOutGameState.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.server.packet.PacketOut; 21 | 22 | import javax.annotation.concurrent.Immutable; 23 | 24 | /** 25 | * Sent by the server to the client whenever the indicated 26 | * game state should change, such as for an invalid bed, 27 | * raining, or game mode toggles. 28 | */ 29 | @Immutable 30 | public final class PlayOutGameState extends PacketOut { 31 | private final int reason; 32 | private final float val; 33 | 34 | public PlayOutGameState(int reason, float val) { 35 | super(PlayOutGameState.class); 36 | this.reason = reason; 37 | this.val = val; 38 | } 39 | 40 | @Override 41 | public void write(ByteBuf buf) { 42 | buf.writeByte(this.reason); 43 | buf.writeFloat(this.val); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayOutJoinGame.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.entity.living.Player; 21 | import net.tridentsdk.server.packet.PacketOut; 22 | import net.tridentsdk.world.World; 23 | import net.tridentsdk.world.opt.Dimension; 24 | import net.tridentsdk.world.opt.GameRule; 25 | import net.tridentsdk.world.opt.LevelType; 26 | import net.tridentsdk.world.opt.WorldOpts; 27 | 28 | import javax.annotation.concurrent.Immutable; 29 | 30 | import static net.tridentsdk.server.net.NetData.wstr; 31 | 32 | /** 33 | * Sent directly after login success, this provides the 34 | * player with information about the world they are 35 | * joining. 36 | */ 37 | @Immutable 38 | public final class PlayOutJoinGame extends PacketOut { 39 | /** 40 | * World data that is sent to the player 41 | */ 42 | private final WorldOpts opts; 43 | /** 44 | * The world's dimension 45 | */ 46 | private final Dimension dimension; 47 | /** 48 | * The level type 49 | */ 50 | private final LevelType type; 51 | /** 52 | * The player that is joining the game 53 | */ 54 | private final Player player; 55 | 56 | public PlayOutJoinGame(Player player, World world) { 57 | super(PlayOutJoinGame.class); 58 | this.player = player; 59 | this.dimension = world.getDimension(); 60 | this.opts = world.getWorldOptions(); 61 | this.type = world.getGeneratorOptions().getLevelType(); 62 | } 63 | 64 | @Override 65 | public void write(ByteBuf buf) { 66 | buf.writeInt(this.player.getId()); 67 | buf.writeByte(this.opts.getGameMode().asByte()); 68 | buf.writeInt(this.dimension.asByte()); 69 | buf.writeByte(this.opts.getDifficulty().asByte()); 70 | buf.writeByte(0); // ignored by client 71 | wstr(buf, this.type.toString()); 72 | buf.writeBoolean(this.opts.getGameRules().get(GameRule.REDUCE_DEBUG)); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayOutKeepAlive.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.server.net.NetClient; 21 | import net.tridentsdk.server.packet.PacketOut; 22 | 23 | import javax.annotation.concurrent.Immutable; 24 | 25 | import static net.tridentsdk.server.net.NetData.wvint; 26 | 27 | /** 28 | * Server keep alive packet sent to the client to ensure 29 | * proper response. 30 | */ 31 | @Immutable 32 | public final class PlayOutKeepAlive extends PacketOut { 33 | /** 34 | * The client being kept alive 35 | */ 36 | private final NetClient client; 37 | 38 | public PlayOutKeepAlive(NetClient client) { 39 | super(PlayOutKeepAlive.class); 40 | this.client = client; 41 | } 42 | 43 | @Override 44 | public void write(ByteBuf buf) { 45 | int query = PlayInKeepAlive.query(this.client); 46 | wvint(buf, query); 47 | } 48 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayOutLightning.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.base.AbstractVector; 21 | import net.tridentsdk.server.entity.TridentEntity; 22 | import net.tridentsdk.server.net.NetData; 23 | import net.tridentsdk.server.packet.PacketOut; 24 | 25 | import javax.annotation.concurrent.Immutable; 26 | 27 | /** 28 | * Sent by the server to spawn a lightning packet for a 29 | * particular player. 30 | */ 31 | @Immutable 32 | public final class PlayOutLightning extends PacketOut { 33 | private final double x; 34 | private final double y; 35 | private final double z; 36 | 37 | public PlayOutLightning(AbstractVector vector) { 38 | super(PlayOutLightning.class); 39 | this.x = vector.getX(); 40 | this.y = vector.getY(); 41 | this.z = vector.getZ(); 42 | } 43 | 44 | @Override 45 | public void write(ByteBuf buf) { 46 | NetData.wvint(buf, TridentEntity.EID_COUNTER.incrementAndGet()); 47 | buf.writeByte(1); // apparently the only useful value 48 | buf.writeDouble(this.x); 49 | buf.writeDouble(this.y); 50 | buf.writeDouble(this.z); 51 | } 52 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayOutOpenWindow.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.entity.Entity; 21 | import net.tridentsdk.inventory.InventoryType; 22 | import net.tridentsdk.server.inventory.TridentInventory; 23 | import net.tridentsdk.server.packet.PacketOut; 24 | 25 | import javax.annotation.concurrent.Immutable; 26 | 27 | import static net.tridentsdk.server.net.NetData.wstr; 28 | 29 | /** 30 | * Opens an inventory window for the client. 31 | */ 32 | @Immutable 33 | public class PlayOutOpenWindow extends PacketOut { 34 | /** 35 | * The inventory window to open 36 | */ 37 | private final TridentInventory inventory; 38 | /** 39 | * The horse, if this inventory is a horse inventory 40 | */ 41 | private final Entity entity; 42 | 43 | public PlayOutOpenWindow(TridentInventory inventory, Entity entity) { 44 | super(PlayOutOpenWindow.class); 45 | this.inventory = inventory; 46 | this.entity = entity; 47 | } 48 | 49 | @Override 50 | public void write(ByteBuf buf) { 51 | buf.writeByte(this.inventory.getId()); 52 | wstr(buf, this.inventory.getType().toString()); 53 | wstr(buf, this.inventory.getTitle().toString()); 54 | buf.writeByte(this.inventory.getSize()); 55 | 56 | if (this.inventory.getType() == InventoryType.HORSE) { 57 | buf.writeInt(this.entity.getId()); // y no varint, mojang 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayOutPlayerAbilities.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.server.packet.PacketOut; 21 | import net.tridentsdk.server.player.TridentPlayer; 22 | import net.tridentsdk.world.opt.GameMode; 23 | 24 | import javax.annotation.concurrent.ThreadSafe; 25 | 26 | /** 27 | * Sent after {@link PlayOutSpawnPos} to communicate to the 28 | * client their abilities once joined. 29 | */ 30 | @ThreadSafe 31 | public final class PlayOutPlayerAbilities extends PacketOut { 32 | private final boolean isGod; 33 | private final boolean isFlying; 34 | private final boolean canFly; 35 | private final GameMode gameMode; 36 | private final float flyingSpeed; 37 | private final float walkingSpeed; 38 | 39 | private volatile boolean doubleJump; 40 | 41 | public PlayOutPlayerAbilities(TridentPlayer player) { 42 | super(PlayOutPlayerAbilities.class); 43 | this.isGod = player.isGodMode(); 44 | this.isFlying = player.isFlying(); 45 | this.canFly = player.canFly(); 46 | this.gameMode = player.getGameMode(); 47 | this.flyingSpeed = player.getFlyingSpeed(); 48 | this.walkingSpeed = player.getWalkingSpeed(); 49 | this.doubleJump = false; 50 | } 51 | 52 | public void setDoubleJumpInsteadOfFlying() { 53 | this.doubleJump = this.canFly; 54 | } 55 | 56 | @Override 57 | public void write(ByteBuf buf) { 58 | byte abilities = 0x00; 59 | abilities |= this.isGod ? 0x01 : 0x00; // invuln 60 | abilities |= this.isFlying && !this.doubleJump ? 0x02 : 0; // flying 61 | abilities |= this.canFly ? 0x04 : 0; // can fly 62 | abilities |= this.gameMode == GameMode.CREATIVE ? 0x08 : 0; // creative 63 | 64 | buf.writeByte(abilities); 65 | buf.writeFloat(this.flyingSpeed); 66 | buf.writeFloat(this.walkingSpeed); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayOutPlayerListHeaderAndFooter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.server.packet.PacketOut; 21 | import net.tridentsdk.ui.chat.ChatComponent; 22 | 23 | import javax.annotation.concurrent.Immutable; 24 | 25 | import static net.tridentsdk.server.net.NetData.wstr; 26 | 27 | @Immutable 28 | public final class PlayOutPlayerListHeaderAndFooter extends PacketOut { 29 | 30 | private final ChatComponent header; 31 | private final ChatComponent footer; 32 | 33 | public PlayOutPlayerListHeaderAndFooter(ChatComponent header, ChatComponent footer) { 34 | super(PlayOutPlayerListHeaderAndFooter.class); 35 | this.header = header; 36 | this.footer = footer; 37 | } 38 | 39 | @Override 40 | public void write(ByteBuf buf) { 41 | wstr(buf, this.header == null ? ChatComponent.empty().toString() : this.header.toString()); 42 | wstr(buf, this.header == null ? ChatComponent.empty().toString() : this.footer.toString()); 43 | } 44 | 45 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayOutPluginMsg.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.server.packet.PacketOut; 21 | 22 | import javax.annotation.concurrent.Immutable; 23 | 24 | import static net.tridentsdk.server.net.NetData.NET_CHARSET; 25 | import static net.tridentsdk.server.net.NetData.wstr; 26 | 27 | /** 28 | * Plugin message packet, used to send the brand after the 29 | * {@link PlayOutJoinGame} packet has been sent. 30 | */ 31 | @Immutable 32 | public final class PlayOutPluginMsg extends PacketOut { 33 | /** 34 | * The branding packet 35 | */ 36 | public static final PlayOutPluginMsg BRAND = 37 | new PlayOutPluginMsg("MC|Brand", "tridentsdk".getBytes(NET_CHARSET)); 38 | /** 39 | * The channel name 40 | */ 41 | private final String channel; 42 | /** 43 | * The message payload 44 | */ 45 | private final byte[] data; 46 | 47 | public PlayOutPluginMsg(String channel, byte[] data) { 48 | super(PlayOutPluginMsg.class); 49 | if (data.length >= Short.MAX_VALUE) { 50 | throw new ArrayIndexOutOfBoundsException("Data must have len < Short.MAX_VALUE"); 51 | } 52 | 53 | this.channel = channel; 54 | this.data = data; 55 | } 56 | 57 | @Override 58 | public void write(ByteBuf buf) { 59 | wstr(buf, this.channel); 60 | buf.writeBytes(this.data); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayOutPosLook.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.base.Position; 21 | import net.tridentsdk.server.packet.PacketOut; 22 | import net.tridentsdk.server.player.TridentPlayer; 23 | 24 | import javax.annotation.concurrent.Immutable; 25 | 26 | import static net.tridentsdk.server.net.NetData.wvint; 27 | 28 | /** 29 | * Sent after receiving {@link PlayInClientSettings} from the 30 | * client, at which point this packet will be sent with the 31 | * player's position and the client will leave the login 32 | * screen. 33 | */ 34 | @Immutable 35 | public final class PlayOutPosLook extends PacketOut { 36 | /** 37 | * The player to move 38 | */ 39 | private final TridentPlayer player; 40 | /** 41 | * The position which to move the player 42 | */ 43 | private final Position pos; 44 | 45 | public PlayOutPosLook(TridentPlayer player) { 46 | super(PlayOutPosLook.class); 47 | this.player = player; 48 | this.pos = player.getPosition(); 49 | } 50 | 51 | public PlayOutPosLook(TridentPlayer player, Position pos) { 52 | super(PlayOutPosLook.class); 53 | this.player = player; 54 | this.pos = pos; 55 | } 56 | 57 | @Override 58 | public void write(ByteBuf buf) { 59 | buf.writeDouble(this.pos.getX()); 60 | buf.writeDouble(this.pos.getY()); 61 | buf.writeDouble(this.pos.getZ()); 62 | buf.writeFloat(this.pos.getYaw()); 63 | buf.writeFloat(this.pos.getPitch()); 64 | buf.writeByte(0); // all absolute 65 | wvint(buf, PlayInTeleportConfirm.query(this.player.net())); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayOutSlot.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.server.net.Slot; 21 | import net.tridentsdk.server.packet.PacketOut; 22 | 23 | import javax.annotation.concurrent.Immutable; 24 | 25 | /** 26 | * Sets a slot in the given position of the given window 27 | * to the given slot data. 28 | */ 29 | @Immutable 30 | public class PlayOutSlot extends PacketOut { 31 | /** 32 | * The window to set the slot, 0 for player inventory 33 | */ 34 | private final int window; 35 | /** 36 | * The slot to place the item into 37 | */ 38 | private final int pos; 39 | /** 40 | * The slot data 41 | */ 42 | private final Slot slot; 43 | 44 | public PlayOutSlot(int window, int pos, Slot slot) { 45 | super(PlayOutSlot.class); 46 | this.window = window; 47 | this.pos = pos; 48 | this.slot = slot; 49 | } 50 | 51 | @Override 52 | public void write(ByteBuf buf) { 53 | buf.writeByte(this.window); 54 | buf.writeShort(this.pos); 55 | this.slot.write(buf); 56 | } 57 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayOutSpawnPlayer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.base.Position; 21 | import net.tridentsdk.entity.living.Player; 22 | import net.tridentsdk.server.entity.meta.TridentEntityMeta; 23 | import net.tridentsdk.server.packet.PacketOut; 24 | 25 | import javax.annotation.concurrent.Immutable; 26 | 27 | import static net.tridentsdk.server.net.NetData.convertAngle; 28 | import static net.tridentsdk.server.net.NetData.wvint; 29 | 30 | @Immutable 31 | public final class PlayOutSpawnPlayer extends PacketOut { 32 | 33 | private final Player player; 34 | 35 | public PlayOutSpawnPlayer(Player player) { 36 | super(PlayOutSpawnPlayer.class); 37 | this.player = player; 38 | } 39 | 40 | @Override 41 | public void write(ByteBuf buf) { 42 | wvint(buf, this.player.getId()); 43 | 44 | buf.writeLong(this.player.getUuid().getMostSignificantBits()); 45 | buf.writeLong(this.player.getUuid().getLeastSignificantBits()); 46 | 47 | Position pos = this.player.getPosition(); 48 | buf.writeDouble(pos.getX()); 49 | buf.writeDouble(pos.getY()); 50 | buf.writeDouble(pos.getZ()); 51 | 52 | buf.writeByte(convertAngle(pos.getYaw())); 53 | buf.writeByte(convertAngle(pos.getPitch())); 54 | 55 | ((TridentEntityMeta) this.player.getMetadata()).getMetadata().write(buf); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayOutSpawnPos.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.base.Vector; 21 | import net.tridentsdk.server.packet.PacketOut; 22 | 23 | import javax.annotation.concurrent.Immutable; 24 | 25 | import static net.tridentsdk.server.net.NetData.wvec; 26 | 27 | /** 28 | * This packet is sent to indicate to the client where 29 | * their 30 | * bed/compass will point towards. 31 | */ 32 | @Immutable 33 | public final class PlayOutSpawnPos extends PacketOut { 34 | /** 35 | * The default spawn position 36 | */ 37 | public static final Vector VEC = new Vector(); 38 | 39 | public PlayOutSpawnPos() { 40 | super(PlayOutSpawnPos.class); 41 | } 42 | 43 | @Override 44 | public void write(ByteBuf buf) { 45 | wvec(buf, VEC); 46 | } 47 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayOutTeleport.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.base.Position; 21 | import net.tridentsdk.server.entity.TridentEntity; 22 | import net.tridentsdk.server.packet.PacketOut; 23 | 24 | import javax.annotation.concurrent.Immutable; 25 | 26 | import static net.tridentsdk.server.net.NetData.convertAngle; 27 | import static net.tridentsdk.server.net.NetData.wvint; 28 | 29 | /** 30 | * Sent by the server whenever a player needs to be moved 31 | * more than 4 blocks. 32 | */ 33 | @Immutable 34 | public class PlayOutTeleport extends PacketOut { 35 | private final int eid; 36 | private final Position position; 37 | private final boolean onGround; 38 | 39 | public PlayOutTeleport(TridentEntity entity, Position teleport) { 40 | super(PlayOutTeleport.class); 41 | this.eid = entity.getId(); 42 | this.position = teleport; 43 | this.onGround = entity.isOnGround(); 44 | } 45 | 46 | @Override 47 | public void write(ByteBuf buf) { 48 | wvint(buf, this.eid); 49 | buf.writeDouble(this.position.getX()); 50 | buf.writeDouble(this.position.getY()); 51 | buf.writeDouble(this.position.getZ()); 52 | buf.writeByte(convertAngle(this.position.getYaw())); 53 | buf.writeByte(convertAngle(this.position.getPitch())); 54 | buf.writeBoolean(this.onGround); 55 | } 56 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayOutTime.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.server.packet.PacketOut; 21 | 22 | import javax.annotation.concurrent.Immutable; 23 | 24 | /** 25 | * Players update the server time on the client. This packet 26 | * should only be used to resync the world time with the 27 | * client time. 28 | */ 29 | @Immutable 30 | public final class PlayOutTime extends PacketOut { 31 | private final long age; 32 | private final long currentTime; 33 | 34 | public PlayOutTime(long age, long currentTime) { 35 | super(PlayOutTime.class); 36 | this.age = age; 37 | this.currentTime = currentTime; 38 | } 39 | 40 | @Override 41 | public void write(ByteBuf buf) { 42 | buf.writeLong(this.age); 43 | buf.writeLong(this.currentTime); 44 | } 45 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayOutUnloadChunk.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.server.packet.PacketOut; 21 | 22 | import javax.annotation.concurrent.Immutable; 23 | 24 | /** 25 | * Sent to players to have the client unload the chunk at 26 | * the given coordinates on the client side. 27 | */ 28 | @Immutable 29 | public class PlayOutUnloadChunk extends PacketOut { 30 | /** 31 | * The chunk x 32 | */ 33 | private final int x; 34 | /** 35 | * The chunk z 36 | */ 37 | private final int z; 38 | 39 | public PlayOutUnloadChunk(int x, int z) { 40 | super(PlayOutUnloadChunk.class); 41 | this.x = x; 42 | this.z = z; 43 | } 44 | 45 | @Override 46 | public void write(ByteBuf buf) { 47 | buf.writeInt(this.x); 48 | buf.writeInt(this.z); 49 | } 50 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/play/PlayOutWindowItems.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.play; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.server.inventory.TridentInventory; 21 | import net.tridentsdk.server.net.Slot; 22 | import net.tridentsdk.server.packet.PacketOut; 23 | 24 | import javax.annotation.concurrent.Immutable; 25 | 26 | /** 27 | * Sent to the client for whenever the server wants to 28 | * populate the opened window with items. 29 | */ 30 | @Immutable 31 | public class PlayOutWindowItems extends PacketOut { 32 | /** 33 | * The inventory to obtain the items to send 34 | */ 35 | private final TridentInventory inventory; 36 | 37 | public PlayOutWindowItems(TridentInventory inventory) { 38 | super(PlayOutWindowItems.class); 39 | this.inventory = inventory; 40 | } 41 | 42 | @Override 43 | public void write(ByteBuf buf) { 44 | buf.writeByte(this.inventory.getId()); 45 | buf.writeShort(this.inventory.getSize()); 46 | for (int i = 0; i < this.inventory.getSize(); i++) { 47 | Slot.newSlot(this.inventory.get(i)).write(buf); 48 | } 49 | } 50 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/status/StatusInPing.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.status; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.server.net.NetClient; 21 | import net.tridentsdk.server.packet.PacketIn; 22 | 23 | import javax.annotation.concurrent.Immutable; 24 | 25 | /** 26 | * Ping packet sent by the client in order to determine the 27 | * server latency (if needed). 28 | */ 29 | @Immutable 30 | public final class StatusInPing extends PacketIn { 31 | public StatusInPing() { 32 | super(StatusInPing.class); 33 | } 34 | 35 | @Override 36 | public void read(ByteBuf buf, NetClient client) { 37 | long endStamp = System.currentTimeMillis(); 38 | long time = buf.readLong(); 39 | 40 | client.getPing().set(endStamp - time); 41 | client.sendPacket(new StatusOutPong(time)); 42 | } 43 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/status/StatusInRequest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.status; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.event.server.ServerPingEvent; 21 | import net.tridentsdk.server.TridentServer; 22 | import net.tridentsdk.server.concurrent.PoolSpec; 23 | import net.tridentsdk.server.concurrent.ServerThreadPool; 24 | import net.tridentsdk.server.config.ServerConfig; 25 | import net.tridentsdk.server.net.NetClient; 26 | import net.tridentsdk.server.packet.PacketIn; 27 | import net.tridentsdk.server.player.TridentPlayer; 28 | import net.tridentsdk.ui.chat.ChatComponent; 29 | 30 | import javax.annotation.concurrent.Immutable; 31 | import java.net.InetSocketAddress; 32 | import java.util.Collection; 33 | 34 | import static net.tridentsdk.server.packet.status.StatusOutResponse.MC_VERSION; 35 | import static net.tridentsdk.server.packet.status.StatusOutResponse.PROTOCOL_VERSION; 36 | 37 | /** 38 | * This packet represents a status request that is sent 39 | * when 40 | * the server is pinged and after the server receives a 41 | * handshake packet with a next status set to 1. 42 | */ 43 | @Immutable 44 | public final class StatusInRequest extends PacketIn { 45 | public StatusInRequest() { 46 | super(StatusInRequest.class); 47 | } 48 | 49 | @Override 50 | public void read(ByteBuf buf, NetClient client) { 51 | ServerConfig cfg = TridentServer.cfg(); 52 | 53 | Collection players = TridentPlayer.getPlayers().values(); 54 | int onlinePlayers = players.size(); 55 | ServerPingEvent.ServerPingResponseSample[] sample = new ServerPingEvent.ServerPingResponseSample[onlinePlayers]; 56 | int i = 0; 57 | for (TridentPlayer player : players) { 58 | sample[i++] = new ServerPingEvent.ServerPingResponseSample(player.getName(), player.getUuid()); 59 | } 60 | 61 | ServerPingEvent.ServerPingResponse response = new ServerPingEvent.ServerPingResponse( 62 | new ServerPingEvent.ServerPingResponseVersion(MC_VERSION, PROTOCOL_VERSION), 63 | new ServerPingEvent.ServerPingResponsePlayers(onlinePlayers, cfg.maxPlayers(), sample), 64 | ChatComponent.text(cfg.motd()), 65 | StatusOutResponse.b64icon.get() 66 | ); 67 | InetSocketAddress pinger = (InetSocketAddress) client.getChannel().remoteAddress(); 68 | ServerPingEvent event = new ServerPingEvent(pinger, response); 69 | ServerThreadPool.forSpec(PoolSpec.PLUGINS) 70 | .submit(() -> TridentServer.getInstance().getEventController() 71 | .dispatch(event, e -> client.sendPacket(new StatusOutResponse(e))) 72 | ); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/packet/status/StatusOutPong.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet.status; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import net.tridentsdk.server.packet.PacketOut; 21 | 22 | /** 23 | * Server response to {@link StatusInPing}. 24 | */ 25 | public final class StatusOutPong extends PacketOut { 26 | /** 27 | * The time that was sent by the ping packet 28 | */ 29 | private final long time; 30 | 31 | public StatusOutPong(long time) { 32 | super(StatusOutPong.class); 33 | this.time = time; 34 | } 35 | 36 | @Override 37 | public void write(ByteBuf buf) { 38 | // Schema: 39 | // long:time 40 | // fucking mojang won't make up their mind with 41 | // varlong and long and bs like that 42 | buf.writeLong(this.time); 43 | } 44 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/plugin/EventDispatcher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.plugin; 18 | 19 | import com.esotericsoftware.reflectasm.MethodAccess; 20 | import lombok.Getter; 21 | import net.tridentsdk.event.DispatchOrder; 22 | import net.tridentsdk.event.Event; 23 | 24 | import javax.annotation.concurrent.Immutable; 25 | import java.lang.reflect.Method; 26 | 27 | /** 28 | * A container class that links the controller to the event 29 | * listener in order to dispatch events to it. 30 | */ 31 | @Immutable 32 | public final class EventDispatcher implements Comparable { 33 | private final MethodAccess access; 34 | @Getter 35 | private final Object container; 36 | private final int idx; 37 | private final DispatchOrder order; 38 | 39 | /** 40 | * Creates a new event dispatcher. 41 | * @param access the accessor to the class methods 42 | * @param inst the instance of the listener 43 | * @param method the method to invoke 44 | * @param order the order which to invoke the listener 45 | */ 46 | public EventDispatcher(MethodAccess access, Object inst, Method method, DispatchOrder order) { 47 | this.access = access; 48 | this.container = inst; 49 | this.order = order; 50 | 51 | this.idx = access.getIndex(method.getName(), method.getParameterTypes()); 52 | } 53 | 54 | /** 55 | * Fires the event handler, causing it to invoke the 56 | * method which processes the given event object. 57 | * 58 | *

It is up to the responsibility of the bookkeeping 59 | * class to ensure type safety.

60 | * 61 | * @param event the event to pass to the handler 62 | * @return the event that was passed to this handler 63 | */ 64 | public Event fire(Event event) { 65 | this.access.invoke(this.container, this.idx, event); 66 | return event; 67 | } 68 | 69 | /** 70 | * Checks to determine whether or not the event handler 71 | * that is represented by this invoker is contained by 72 | * the given class. 73 | * 74 | * @param cls the class to check 75 | * @return {@code true} if the given class contains this 76 | * event handler 77 | */ 78 | public boolean isContainedBy(Class cls) { 79 | return this.container.getClass().equals(cls); 80 | } 81 | 82 | /** 83 | * Obtains whether this listener is the last to be 84 | * invoked on dispatch of an event. 85 | * 86 | * @return {@code true} if this is the last listener 87 | */ 88 | public boolean isLast() { 89 | return this.order == DispatchOrder.LAST; 90 | } 91 | 92 | @Override 93 | public int compareTo(EventDispatcher o) { 94 | // Since we're dealing with an ordered set, we need 95 | // to return 1 if equal to retain the last listener 96 | if (this.order == o.order) { 97 | return 1; 98 | } 99 | 100 | return this.order.compareTo(o.order); 101 | } 102 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/ui/bossbar/AbstractBossBar.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.ui.bossbar; 18 | 19 | import net.tridentsdk.ui.bossbar.BossBar; 20 | 21 | import java.util.Collections; 22 | import java.util.Set; 23 | import java.util.UUID; 24 | import java.util.concurrent.ConcurrentHashMap; 25 | 26 | /** 27 | * @author TridentSDK 28 | * @since 0.5-alpha 29 | */ 30 | public abstract class AbstractBossBar implements BossBar { 31 | /** 32 | * Set of used UUIDs to prevent conflicts 33 | */ 34 | private static final Set uuids = Collections.newSetFromMap(new ConcurrentHashMap<>()); 35 | 36 | /** 37 | * The UUID of the boss bar 38 | */ 39 | private final UUID uuid; 40 | 41 | /** 42 | * Creates a new boss bar and assigns a UUID 43 | */ 44 | public AbstractBossBar() { 45 | while (true) { 46 | UUID uuid = UUID.randomUUID(); 47 | if (uuids.add(uuid)) { 48 | this.uuid = uuid; 49 | break; 50 | } 51 | } 52 | } 53 | 54 | @Override 55 | public final UUID getUuid() { 56 | return this.uuid; 57 | } 58 | 59 | @Override 60 | public abstract AbstractBossBar clone(); 61 | 62 | @Override 63 | public boolean equals(Object obj) { 64 | return obj != null && obj instanceof AbstractBossBar && 65 | this.uuid.equals(((AbstractBossBar) obj).getUuid()); 66 | } 67 | 68 | @Override 69 | public int hashCode() { 70 | return this.uuid.hashCode(); 71 | } 72 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/ui/bossbar/DragonBossBar.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.ui.bossbar; 18 | 19 | import net.tridentsdk.ui.bossbar.BossBarColor; 20 | import net.tridentsdk.ui.bossbar.BossBarDivision; 21 | import net.tridentsdk.ui.chat.ChatComponent; 22 | 23 | /** 24 | * @author TridentSDK 25 | * @since 0.5-alpha 26 | */ 27 | public class DragonBossBar extends AbstractBossBar { 28 | 29 | private final BossBarColor color; 30 | private final BossBarDivision division; 31 | 32 | public DragonBossBar(/* EnderDragon dragon */) { 33 | this.color = BossBarColor.PINK; 34 | this.division = BossBarDivision.NO_DIVISION; 35 | } 36 | 37 | @Override 38 | public ChatComponent getTitle() { 39 | return ChatComponent.create().setText("Just a default boss bar because #yolo"); 40 | } 41 | 42 | @Override 43 | public void setTitle(ChatComponent title) { 44 | throw new IllegalStateException("Cannot change a default bossbar"); 45 | } 46 | 47 | @Override 48 | public float getHealth() { 49 | // return boss's health as a percentage 50 | return 0; 51 | } 52 | 53 | @Override 54 | public void setHealth(float health) { 55 | // cannot be changed 56 | } 57 | 58 | @Override 59 | public BossBarColor getColor() { 60 | return this.color; // TODO - perhaps this needs to be changed 61 | } 62 | 63 | @Override 64 | public void setColor(BossBarColor color) { 65 | // cannot be changed 66 | } 67 | 68 | @Override 69 | public BossBarDivision getDivision() { 70 | return this.division; 71 | } 72 | 73 | @Override 74 | public void setDivision(BossBarDivision division) { 75 | throw new IllegalStateException("Cannot change a default bossbar"); 76 | } 77 | 78 | @Override 79 | public boolean isDarkenSky() { 80 | return true; // TODO - do we darken the sky? idk 81 | } 82 | 83 | @Override 84 | public void setDarkenSky(boolean darkenSky) { 85 | throw new IllegalStateException("Cannot change a default bossbar"); 86 | } 87 | 88 | @Override 89 | public boolean isDragonBar() { 90 | return true; 91 | } 92 | 93 | @Override 94 | public void setDragonBar(boolean dragonBar) { 95 | throw new IllegalStateException("Cannot change a default bossbar"); 96 | } 97 | 98 | @Override 99 | public DragonBossBar clone() { 100 | return this; 101 | } 102 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/ui/tablist/TabListElement.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.ui.tablist; 18 | 19 | import lombok.Data; 20 | import lombok.NonNull; 21 | import lombok.RequiredArgsConstructor; 22 | import net.tridentsdk.doc.Policy; 23 | import net.tridentsdk.server.player.TridentPlayer; 24 | import net.tridentsdk.ui.chat.ChatComponent; 25 | import net.tridentsdk.world.opt.GameMode; 26 | 27 | import javax.annotation.concurrent.ThreadSafe; 28 | import java.util.Collections; 29 | import java.util.List; 30 | import java.util.UUID; 31 | 32 | /** 33 | * A tab list element. 34 | */ 35 | @Data 36 | @ThreadSafe 37 | public class TabListElement { 38 | /** 39 | * The UUID of the player at this tab list element 40 | */ 41 | private final UUID uuid; 42 | /** 43 | * The game mode 44 | */ 45 | private volatile GameMode gameMode; 46 | /** 47 | * The name of the player 48 | */ 49 | private volatile String name; 50 | /** 51 | * The player's ping 52 | */ 53 | private volatile int ping; 54 | /** 55 | * The value that is displayed on the scoreboard 56 | */ 57 | private volatile ChatComponent displayName; 58 | /** 59 | * Whether or not this element is blank/empty 60 | */ 61 | private volatile boolean blank; 62 | /** 63 | * The tab list properties (such as skin textures) 64 | */ 65 | @Policy("Single update") 66 | private final List properties; 67 | 68 | /** 69 | * Creates a new tab list element which can be filled 70 | * with custom fields. 71 | */ 72 | public TabListElement() { 73 | this.uuid = UUID.randomUUID(); 74 | this.properties = Collections.emptyList(); 75 | } 76 | 77 | /** 78 | * Creates a new tab list element which is filled with 79 | * data from the player's properties. 80 | * 81 | * @param player the player to fill the element 82 | */ 83 | public TabListElement(TridentPlayer player) { 84 | this.uuid = player.getUuid(); 85 | this.name = player.getName(); 86 | this.displayName = player.getTabListName(); 87 | this.ping = player.net().getPing().intValue(); 88 | this.gameMode = player.getGameMode(); 89 | 90 | TabListElement.PlayerProperty textures = player.getSkinTextures(); 91 | if (textures != null) { 92 | this.properties = Collections.singletonList(textures); 93 | } else { 94 | this.properties = Collections.emptyList(); 95 | } 96 | } 97 | 98 | @Data 99 | @RequiredArgsConstructor 100 | public static class PlayerProperty { 101 | private final String name; 102 | @NonNull 103 | private final String value; 104 | private final String signature; 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/ui/tablist/TridentGlobalTabList.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.ui.tablist; 18 | 19 | import lombok.Getter; 20 | import net.tridentsdk.doc.Policy; 21 | import net.tridentsdk.entity.living.Player; 22 | import net.tridentsdk.server.packet.play.PlayOutTabListItem; 23 | import net.tridentsdk.server.player.TridentPlayer; 24 | import net.tridentsdk.ui.chat.ChatComponent; 25 | 26 | import javax.annotation.concurrent.ThreadSafe; 27 | import java.util.Comparator; 28 | 29 | /** 30 | * Implementation of a global tablist, which contains all 31 | * players on the server. 32 | */ 33 | @Policy("singleton") 34 | @ThreadSafe 35 | public class TridentGlobalTabList extends TridentTabList { 36 | /** 37 | * The instance of the global tab list 38 | */ 39 | @Getter 40 | private static final TridentGlobalTabList instance = new TridentGlobalTabList(); 41 | 42 | /** 43 | * Creates an initializes a new global tab list 44 | */ 45 | private TridentGlobalTabList() { 46 | } 47 | 48 | @Override 49 | public void setElement(int slot, ChatComponent value) { 50 | throw new RuntimeException("Attempted to edit global tablist!"); 51 | } 52 | 53 | @Override 54 | public ChatComponent getElement(int slot) { 55 | throw new RuntimeException("Attempted to grab element on global tablist"); 56 | } 57 | 58 | @Override 59 | public void subscribe(Player player) { 60 | this.users.add((TridentPlayer) player); 61 | 62 | PlayOutTabListItem.RemovePlayer removeAll = PlayOutTabListItem.removePlayerPacket(); 63 | PlayOutTabListItem.AddPlayer addAll = PlayOutTabListItem.addPlayerPacket(); 64 | synchronized (this.lock) { 65 | for (TabListElement element : this.elements) { 66 | removeAll.removePlayer(element.getUuid()); 67 | } 68 | 69 | this.elements.clear(); 70 | 71 | TridentPlayer.getPlayers().values(). 72 | stream(). 73 | sorted(Comparator.comparing(p -> p.getTabListName().getText())). 74 | forEach(p -> { 75 | TabListElement e = new TabListElement(p); 76 | this.elements.add(e); 77 | addAll.addPlayer(e); 78 | }); 79 | } 80 | 81 | for (TridentPlayer p : this.users) { 82 | p.net().sendPacket(removeAll); 83 | p.net().sendPacket(addAll); 84 | } 85 | } 86 | 87 | @Override 88 | public void unsubscribe(Player player) { 89 | super.unsubscribe(player); 90 | } 91 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/ui/title/CustomTitle.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.ui.title; 18 | 19 | import lombok.Getter; 20 | import net.tridentsdk.ui.chat.ChatComponent; 21 | import net.tridentsdk.ui.title.Title; 22 | 23 | import javax.annotation.concurrent.NotThreadSafe; 24 | 25 | /** 26 | * The representation of a title that is sent to the player. 27 | */ 28 | @NotThreadSafe 29 | @Getter 30 | public class CustomTitle implements Title { 31 | private ChatComponent header; 32 | private ChatComponent subtitle; 33 | private int fadeIn = Title.DEFAULT_FADE_IN; 34 | private int stay = Title.DEFAULT_STAY; 35 | private int fadeOut = Title.DEFAULT_FADE_OUT; 36 | 37 | @Override 38 | public Title setHeader(ChatComponent title) { 39 | this.header = title; 40 | return this; 41 | } 42 | 43 | @Override 44 | public Title setSubtitle(ChatComponent subtitle) { 45 | this.subtitle = subtitle; 46 | return this; 47 | } 48 | 49 | @Override 50 | public Title setFadeIn(int fadeIn) { 51 | this.fadeIn = fadeIn; 52 | return this; 53 | } 54 | 55 | @Override 56 | public Title setStay(int stay) { 57 | this.stay = stay; 58 | return this; 59 | } 60 | 61 | @Override 62 | public Title setFadeOut(int fadeOut) { 63 | this.fadeOut = fadeOut; 64 | return this; 65 | } 66 | 67 | @Override 68 | public boolean isDefaultFadeTimes() { 69 | return this.fadeIn == Title.DEFAULT_FADE_IN && 70 | this.stay == Title.DEFAULT_STAY && 71 | this.fadeOut == Title.DEFAULT_FADE_OUT; 72 | } 73 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/util/Debug.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.util; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import lombok.AccessLevel; 21 | import lombok.NoArgsConstructor; 22 | 23 | import javax.annotation.concurrent.ThreadSafe; 24 | 25 | /** 26 | * Debug helper class which contains useful debug functions 27 | * for different purposes of testing the server. 28 | * 29 | * @author TridentSDK 30 | * @since 0.5-alpha 31 | */ 32 | @ThreadSafe 33 | @NoArgsConstructor(access = AccessLevel.PRIVATE) 34 | public final class Debug { 35 | public static volatile boolean IS_DEBUGGING; 36 | 37 | /** 38 | * Checks to see if the current thread that should be 39 | * running according to the 40 | * {@link net.tridentsdk.doc.Policy} annotation is 41 | * actually using the plugin thread. This is a very 42 | * crude name check and is prone to breaking should the 43 | * name change, but oh well. Debugging. 44 | */ 45 | public static void tryCheckThread() { 46 | if (IS_DEBUGGING) { 47 | String name = Thread.currentThread().getName(); 48 | if (!name.equals("TRD - Plugins")) { 49 | throw new IllegalStateException("Wrong thread: " + name); 50 | } 51 | } 52 | } 53 | 54 | /** 55 | * Hexdumps the contents of the given buffer. 56 | * 57 | * @param buf the buffer to dump 58 | * @return the hex dump 59 | */ 60 | public static String debugBuffer(ByteBuf buf) { 61 | return debugBuffer(buf, false); 62 | } 63 | 64 | /** 65 | * Dumps the contents of the given buffer, with the 66 | * option to convert the dump to decimal instead of 67 | * hex. 68 | * 69 | * @param buf the buffer to dump 70 | * @param decimal {@code true} for decimal dump 71 | * @return the content dump 72 | */ 73 | public static String debugBuffer(ByteBuf buf, boolean decimal) { 74 | int index = buf.readerIndex(); 75 | int readableBytes = buf.readableBytes(); 76 | String response = buf.getClass().getSimpleName() + "(" + readableBytes + "): ["; 77 | 78 | for (int i = 0; i < readableBytes; i++) { 79 | if (i > 0) { 80 | response += ", "; 81 | } 82 | 83 | byte b = buf.readByte(); 84 | response += String.format("%02x", b); 85 | 86 | if (decimal) { 87 | response += "(" + (0xff & b) + ")"; 88 | } 89 | } 90 | 91 | buf.readerIndex(index); 92 | return response + "]"; 93 | } 94 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/util/JiraExceptionCatcher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.util; 18 | 19 | import net.tridentsdk.Server; 20 | 21 | import java.io.PrintStream; 22 | 23 | /** 24 | * Catches exceptions in the server process that are 25 | * unrecoverable and pastes a link for creating an issue 26 | * with details pulled from the environment. 27 | */ 28 | public final class JiraExceptionCatcher { 29 | // Prevent instantiation 30 | private JiraExceptionCatcher() { 31 | } 32 | 33 | /** 34 | * Catches the given exception and reformats it, 35 | * pulling 36 | * in the stacktrace information and providing a post 37 | * link to the TridentSDK JIRA. 38 | * 39 | * @param e the exception to catch 40 | */ 41 | public static void serverException(Exception e) { 42 | String url = "https://github.com/TridentSDK/Trident/issues/new"; 43 | 44 | String environment = "Trident Version: " + Server.VERSION + "\n" + 45 | "Operating System: " + System.getProperty("os.name") + " (" + System.getProperty("os.version") + ")\n" + 46 | "System Architecture: " + System.getProperty("os.arch") + "\n" + 47 | "Java Version: " + System.getProperty("java.version") + " (" + System.getProperty("java.vendor") + ")"; 48 | 49 | PrintStream o = System.err; 50 | o.println(); 51 | o.println("Unhandled Exception occurred while starting the server."); 52 | o.println("This was not intended to happen."); 53 | o.println("Please report this at " + url); 54 | o.println(); 55 | o.println("SYSTEM INFO:"); 56 | o.println("============"); 57 | o.println(environment); 58 | o.println(); 59 | o.println("STACKTRACE"); 60 | o.println("=========="); 61 | e.printStackTrace(); 62 | } 63 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/util/UncheckedCdl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.util; 18 | 19 | import java.util.concurrent.CountDownLatch; 20 | 21 | /** 22 | * CountDownLatch that does not throw a checked exception, 23 | * useful for preventing boilerplate in lambdas and places 24 | * where an exception should be rethrown by a 25 | * {@link RuntimeException}. 26 | */ 27 | public class UncheckedCdl extends CountDownLatch { 28 | public UncheckedCdl(int count) { 29 | super(count); 30 | } 31 | 32 | @Override 33 | public void await() { 34 | try { 35 | super.await(); 36 | } catch (InterruptedException e) { 37 | throw new RuntimeException(e); 38 | } 39 | } 40 | } -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/world/gen/FlatGeneratorProvider.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.world.gen; 18 | 19 | import net.tridentsdk.world.World; 20 | import net.tridentsdk.world.gen.*; 21 | 22 | import javax.annotation.Nonnull; 23 | import javax.annotation.concurrent.Immutable; 24 | import java.util.Collections; 25 | import java.util.Set; 26 | 27 | /** 28 | * A generator provider that provides the proper generators 29 | * for a flat world level type. 30 | */ 31 | @Immutable 32 | public class FlatGeneratorProvider implements GeneratorProvider { 33 | /** 34 | * The singleton instance of this provider 35 | */ 36 | public static final FlatGeneratorProvider INSTANCE = new FlatGeneratorProvider(); 37 | 38 | @Override 39 | public TerrainGenerator getTerrainGenerator(World world) { 40 | return FlatTerrainGenerator.INSTANCE; 41 | } 42 | 43 | @Nonnull 44 | @Override 45 | public Set getFeatureGenerators(World world) { 46 | return Collections.emptySet(); 47 | } 48 | 49 | @Nonnull 50 | @Override 51 | public Set getPropGenerators(World world) { 52 | return Collections.emptySet(); 53 | } 54 | 55 | @Override 56 | public GenContainer getGenerationContainer() { 57 | return GenContainer.ARBITRARY; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/net/tridentsdk/server/world/gen/FlatTerrainGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.world.gen; 18 | 19 | import net.tridentsdk.world.gen.GeneratorContext; 20 | import net.tridentsdk.world.gen.TerrainGenerator; 21 | 22 | import javax.annotation.concurrent.Immutable; 23 | 24 | /** 25 | * This generator generates the base chunk layer used for 26 | * generating flat worlds. 27 | */ 28 | @Immutable 29 | public class FlatTerrainGenerator implements TerrainGenerator { 30 | /** 31 | * Instance of this generator 32 | */ 33 | public static final FlatTerrainGenerator INSTANCE = new FlatTerrainGenerator(); 34 | 35 | @Override 36 | public void generate(int chunkX, int chunkZ, GeneratorContext context) { 37 | for (int x = 0; x < 16; x++) { 38 | for (int z = 0; z < 16; z++) { 39 | context.set(x, 0, z, 7, (byte) 0); 40 | context.set(x, 1, z, 3, (byte) 0); 41 | context.set(x, 2, z, 3, (byte) 0); 42 | context.set(x, 3, z, 2, (byte) 0); 43 | } 44 | } 45 | } 46 | } -------------------------------------------------------------------------------- /src/main/resources/commit_data: -------------------------------------------------------------------------------- 1 | ${build.commit.hash} 2 | ${build.commit.message} 3 | -------------------------------------------------------------------------------- /src/main/resources/server.hjson: -------------------------------------------------------------------------------- 1 | { 2 | // Address used by the server 3 | address: 0.0.0.0 4 | 5 | // The server port 6 | port: 25565 7 | 8 | // "true" to use linux natives when available 9 | use-native: true 10 | 11 | // The amount of bytes before compressing packets 12 | // Use -1 to disable 13 | compression-threshold: 256 14 | 15 | // Whether to use Mojang auth to check players 16 | online-mode: true 17 | 18 | // The max players that can be online at once 19 | max-players: 20 20 | 21 | // The MOTD message shown in the server list 22 | motd: Just another TridentSDK server... 23 | 24 | // Whether to check for netty memory leaks during runtime 25 | netty-leak-detector: false 26 | } -------------------------------------------------------------------------------- /src/test/java/net/tridentsdk/server/EventTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server; 18 | 19 | import net.tridentsdk.Impl; 20 | import net.tridentsdk.event.DispatchOrder; 21 | import net.tridentsdk.event.Event; 22 | import net.tridentsdk.event.Listener; 23 | import net.tridentsdk.event.ListenerOpts; 24 | import net.tridentsdk.server.logger.PipelinedLogger; 25 | import net.tridentsdk.server.concurrent.ServerThreadPool; 26 | import net.tridentsdk.server.plugin.TridentEventController; 27 | import net.tridentsdk.server.util.UncheckedCdl; 28 | import org.openjdk.jmh.annotations.*; 29 | import org.openjdk.jmh.runner.Runner; 30 | import org.openjdk.jmh.runner.options.Options; 31 | import org.openjdk.jmh.runner.options.OptionsBuilder; 32 | 33 | import java.util.concurrent.TimeUnit; 34 | 35 | @State(Scope.Benchmark) 36 | public class EventTest implements Listener { 37 | public static void main(String[] args) throws Exception { 38 | Impl.setImpl(new ImplementationProvider(PipelinedLogger.init(true))); 39 | 40 | TridentEventController controller = TridentEventController.getInstance(); 41 | controller.register(new EventTest()); 42 | 43 | TestEvent event = new TestEvent(); 44 | int its = 10; 45 | 46 | for (int i = 0; i < its; i++) { 47 | controller.dispatch(event, e -> {}); 48 | } 49 | 50 | controller.dispatch(event, e -> System.out.println(e.counter)); 51 | 52 | Options options = new OptionsBuilder() 53 | .include(".*" + EventTest.class.getSimpleName() + ".*") 54 | .timeUnit(TimeUnit.NANOSECONDS) 55 | .mode(Mode.AverageTime) 56 | .warmupIterations(20) 57 | .measurementIterations(5) 58 | .forks(0) 59 | .threads(1) 60 | .build(); 61 | 62 | new Runner(options).run(); 63 | } 64 | 65 | private TestEvent event; 66 | 67 | @Setup(Level.Iteration) 68 | public void setup() { 69 | this.event = new TestEvent(); 70 | } 71 | 72 | @TearDown 73 | public void tearDown() { 74 | ServerThreadPool.shutdownAll(); 75 | } 76 | 77 | @Benchmark 78 | public void run() { 79 | UncheckedCdl cdl = new UncheckedCdl(1); 80 | TridentEventController.getInstance().dispatch(this.event, e -> cdl.countDown()); 81 | cdl.await(); 82 | } 83 | 84 | @ListenerOpts(order = DispatchOrder.LAST) 85 | public void testLastOverride(TestEvent event) { 86 | event.increment(); 87 | } 88 | 89 | public void fireFirst(TestEvent event) { 90 | event.increment(); 91 | } 92 | 93 | @ListenerOpts(order = DispatchOrder.LAST) 94 | public void fireLast(TestEvent event) { 95 | event.increment(); 96 | } 97 | 98 | public static class TestEvent implements Event { 99 | private int counter; 100 | 101 | public void increment() { 102 | this.counter++; 103 | } 104 | } 105 | } -------------------------------------------------------------------------------- /src/test/java/net/tridentsdk/server/command/LoggerTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.command; 18 | 19 | import net.tridentsdk.logger.Logger; 20 | import net.tridentsdk.server.logger.*; 21 | import org.fusesource.jansi.AnsiConsole; 22 | import org.openjdk.jmh.annotations.*; 23 | import org.openjdk.jmh.runner.Runner; 24 | import org.openjdk.jmh.runner.RunnerException; 25 | import org.openjdk.jmh.runner.options.Options; 26 | import org.openjdk.jmh.runner.options.OptionsBuilder; 27 | 28 | import java.io.ByteArrayOutputStream; 29 | import java.io.PrintStream; 30 | import java.util.concurrent.Executor; 31 | import java.util.concurrent.Executors; 32 | import java.util.concurrent.ThreadLocalRandom; 33 | import java.util.concurrent.TimeUnit; 34 | 35 | @State(Scope.Benchmark) 36 | public class LoggerTest { 37 | public static void main(String[] args) throws RunnerException, InterruptedException { 38 | run(); 39 | // run1(); 40 | } 41 | 42 | public static void run1() throws InterruptedException { 43 | LoggerTest test = new LoggerTest(); 44 | Executor e = Executors.newFixedThreadPool(1); 45 | while (true) { 46 | // Introduce breakpoints for profilers 47 | Thread.sleep(10); 48 | if (ThreadLocalRandom.current().nextInt(50) == 1) { 49 | test.setup(); 50 | } 51 | 52 | e.execute(test::log); 53 | } 54 | } 55 | 56 | public static void run() throws RunnerException { 57 | Options options = new OptionsBuilder() 58 | .include(".*" + LoggerTest.class.getSimpleName() + ".*") 59 | .timeUnit(TimeUnit.SECONDS) 60 | .mode(Mode.Throughput) 61 | .warmupIterations(20) 62 | .measurementIterations(5) 63 | .forks(1) 64 | .threads(4) 65 | .build(); 66 | 67 | new Runner(options).run(); 68 | } 69 | 70 | private final Logger logger; 71 | private final ByteArrayOutputStream out = new ByteArrayOutputStream(); 72 | 73 | public LoggerTest() { 74 | AnsiConsole.systemInstall(); 75 | System.setOut(new PrintStream(this.out)); 76 | System.setErr(System.out); 77 | 78 | PipelinedLogger underlying = new DefaultLogger(); 79 | PipelinedLogger colorizer = new ColorizerLogger(underlying); 80 | PipelinedLogger debugger = DebugLogger.noop(colorizer); 81 | PipelinedLogger handler = new LoggerHandlers(debugger); 82 | PipelinedLogger file = null; 83 | try { 84 | file = FileLogger.init(handler); 85 | } catch (Exception e) { 86 | e.printStackTrace(); 87 | } 88 | 89 | this.logger = InfoLogger.get(file, "Bench"); 90 | } 91 | 92 | private volatile String print; 93 | 94 | @Setup(Level.Iteration) 95 | public void setup() { 96 | byte[] barray = new byte[64]; 97 | ThreadLocalRandom.current().nextBytes(barray); 98 | this.print = new String(barray); 99 | } 100 | 101 | @TearDown(Level.Iteration) 102 | public void tearDown() { 103 | this.out.reset(); 104 | } 105 | 106 | @Benchmark 107 | public void log() { 108 | this.logger.log(this.print); 109 | } 110 | } -------------------------------------------------------------------------------- /src/test/java/net/tridentsdk/server/packet/InstantiationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Trident - A Multithreaded Server Alternative 3 | * Copyright 2017 The TridentSDK Team 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.tridentsdk.server.packet; 18 | 19 | import com.esotericsoftware.reflectasm.ConstructorAccess; 20 | import org.openjdk.jmh.annotations.Benchmark; 21 | import org.openjdk.jmh.annotations.Mode; 22 | import org.openjdk.jmh.annotations.Scope; 23 | import org.openjdk.jmh.annotations.State; 24 | import org.openjdk.jmh.runner.Runner; 25 | import org.openjdk.jmh.runner.RunnerException; 26 | import org.openjdk.jmh.runner.options.Options; 27 | import org.openjdk.jmh.runner.options.OptionsBuilder; 28 | 29 | import java.util.concurrent.TimeUnit; 30 | import java.util.function.Supplier; 31 | 32 | @State(Scope.Benchmark) 33 | public class InstantiationTest { 34 | private final ConstructorAccess testClassConstructorAccess = ConstructorAccess.get(TestClass.class); 35 | private final Supplier supplier = TestClass::new; 36 | 37 | public static void main(String[] args) throws RunnerException { 38 | Options options = new OptionsBuilder() 39 | .include(".*" + InstantiationTest.class.getSimpleName() + ".*") 40 | .timeUnit(TimeUnit.SECONDS) 41 | .mode(Mode.Throughput) 42 | .warmupIterations(20) 43 | .measurementIterations(5) 44 | .forks(1) 45 | .threads(4) 46 | .build(); 47 | 48 | new Runner(options).run(); 49 | } 50 | 51 | @Benchmark 52 | public void baseLine() { 53 | } 54 | 55 | @Benchmark 56 | public TestClass normalInstantiation() { 57 | return new TestClass(); 58 | } 59 | 60 | @Benchmark 61 | public TestClass funcInstantiation() { 62 | return this.supplier.get(); 63 | } 64 | 65 | @Benchmark 66 | public TestClass reflectionInstantiation() { 67 | return this.testClassConstructorAccess.newInstance(); 68 | } 69 | 70 | public static class TestClass { 71 | } 72 | } --------------------------------------------------------------------------------