├── .github ├── FUNDING.yml └── workflows │ └── gradle-publish.yml ├── .gitignore ├── LICENSE ├── README.md ├── build.gradle ├── cmd.bat ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle.kts └── src └── main ├── java └── fr │ └── anarchick │ └── skriptpacket │ ├── Logging.java │ ├── Metrics.java │ ├── SkriptPacket.java │ ├── elements │ ├── CustomComparators.java │ ├── CustomConverters.java │ ├── Types.java │ ├── conditions │ │ ├── IsBukkitMaterial.java │ │ ├── IsFromClient.java │ │ ├── IsFromServer.java │ │ ├── IsJavaList.java │ │ └── IsNMS.java │ ├── deprecated │ │ ├── ExprArrayList.java │ │ ├── ExprBaseComponent.java │ │ ├── ExprBukkitChatComponent.java │ │ ├── ExprBukkitChunk.java │ │ ├── ExprBukkitEntity.java │ │ ├── ExprBukkitItemStack.java │ │ ├── ExprBukkitMaterial.java │ │ ├── ExprBukkitWorld.java │ │ ├── ExprJavaUUID.java │ │ ├── ExprLocationFromNMS.java │ │ ├── ExprNMSBiomeID.java │ │ ├── ExprNMSBlockPosition.java │ │ ├── ExprNMSText.java │ │ ├── ExprNewPJSON.java │ │ ├── ExprNewWatchableObject.java │ │ ├── ExprPJSON.java │ │ ├── ExprReadableJson.java │ │ ├── ExprWatchableObjectIndex.java │ │ ├── ExprWatchableObjectValue.java │ │ └── PJSON.java │ ├── effects │ │ ├── EffReceivePacket.java │ │ ├── EffSendPacket.java │ │ └── EffUpdateEntity.java │ ├── events │ │ └── EvtPacket.java │ └── expressions │ │ ├── datawatcher │ │ ├── DataWatcher.java │ │ ├── ExprDataWatcherIndex.java │ │ ├── ExprDataWatcherIndexes.java │ │ └── ExprNewDataWatcher.java │ │ ├── nms │ │ ├── ExprNMS.java │ │ └── ExprPair.java │ │ ├── packet │ │ ├── ExprNewPacket.java │ │ ├── ExprPacketClone.java │ │ ├── ExprPacketField.java │ │ ├── ExprPacketFields.java │ │ ├── ExprPacketFieldsClasses.java │ │ ├── ExprPacketMeta.java │ │ └── ExprPacketType.java │ │ └── utility │ │ ├── ExprBukkitMaterial.java │ │ ├── ExprEntityFromID.java │ │ ├── ExprEntityID.java │ │ ├── ExprEnum.java │ │ ├── ExprItemFromMaterial.java │ │ ├── ExprNumberAs.java │ │ └── ExprNumbersAsArray.java │ ├── packets │ ├── BukkitPacketEvent.java │ ├── PacketManager.java │ ├── SPPacketAdapter.java │ └── SkriptPacketEventListener.java │ ├── sections │ └── DoSection.java │ └── util │ ├── ArrayUtils.java │ ├── ClassUtils.java │ ├── NumberEnums.java │ ├── NumberUtils.java │ ├── Scheduling.java │ ├── Utils.java │ └── converters │ ├── Converter.java │ ├── ConverterLogic.java │ ├── ConverterToBukkit.java │ ├── ConverterToNMS.java │ ├── ConverterToUtility.java │ └── ConverterType.java └── resources ├── config.yml ├── packet.sk └── plugin.yml /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | ko_fi: Anarchick -------------------------------------------------------------------------------- /.github/workflows/gradle-publish.yml: -------------------------------------------------------------------------------- 1 | # This workflow uses actions that are not certified by GitHub. 2 | # They are provided by a third-party and are governed by 3 | # separate terms of service, privacy policy, and support 4 | # documentation. 5 | # This workflow will build a package using Gradle and then publish it to GitHub packages when a release is created 6 | # For more information see: https://github.com/actions/setup-java/blob/main/docs/advanced-usage.md#Publishing-using-gradle 7 | 8 | name: Gradle Package 9 | 10 | on: 11 | release: 12 | types: [created] 13 | 14 | jobs: 15 | build: 16 | 17 | runs-on: ubuntu-latest 18 | permissions: 19 | contents: read 20 | packages: write 21 | 22 | steps: 23 | - uses: actions/checkout@v4 24 | - name: Set up JDK 17 25 | uses: actions/setup-java@v4 26 | with: 27 | java-version: '17' 28 | distribution: 'temurin' 29 | server-id: github # Value of the distributionManagement/repository/id field of the pom.xml 30 | settings-path: ${{ github.workspace }} # location for the settings.xml file 31 | 32 | - name: Setup Gradle 33 | uses: gradle/actions/setup-gradle@417ae3ccd767c252f5661f1ace9f835f9654f2b5 # v3.1.0 34 | 35 | - name: Build with Gradle 36 | run: ./gradlew build 37 | 38 | # The USERNAME and TOKEN need to correspond to the credentials environment variables used in 39 | # the publishing section of your build.gradle 40 | - name: Publish to GitHub Packages 41 | run: ./gradlew publish 42 | env: 43 | USERNAME: ${{ github.actor }} 44 | TOKEN: ${{ secrets.GITHUB_TOKEN }} 45 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | 22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 23 | hs_err_pid* 24 | /bin/ 25 | /target/ 26 | /.gradle/ 27 | 28 | # Ignore Gradle project-specific cache directory 29 | gradle/ 30 | .gradle 31 | .bat 32 | settings.gradle 33 | gradlew 34 | 35 | # Ignore Gradle build output directory 36 | build 37 | 38 | 39 | # Ignore Eclipse specific 40 | .project 41 | .settings/ 42 | .classpath 43 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Anarchick 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![SkriptHubViewTheDocs](http://skripthub.net/static/addon/ViewTheDocsButton.png)](http://skripthub.net/docs/?addon=skript-packet) 2 | [![Discord Banner 2](https://discordapp.com/api/guilds/138464183946575874/widget.png?style=banner2)](https://discord.com/channels/138464183946575874/860221632852393996) 3 | [![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/R6R3VYU8L) 4 | 5 | # Skript-Packet 6 | A Skript packet addon to replace ThatPacketAddon (which is not updated) with Skript 2.5.2+ 7 | 8 | # CAUTIONS 9 | 10 | ⚠️ Skript-Packet **is not** a fork of TPA, syntaxes may change ⚠️ 11 | ⚠️ This plugin **is not** for beginners ⚠️ 12 | ⚠️ You may have to use java NMS which is not an API ⚠️ 13 | ⚠️ You **should not** use packet for the first solution if possible, do not try to use this plugin to do ScoreBoard, BossBars, Particles, or existing things ⚠️ 14 | 15 | # Requirements 16 | - Recent version of Skript 2.10.0+ 17 | - Stable [ProtocolLib dev build](https://ci.dmulloy2.net/job/ProtocolLib/) (does not work with v-5.1.0) 18 | - Java21+ 19 | - It's highly recommended to use skript-reflect 20 | - I have only tested mc 1.20.1 and 1.21.4 **but** should work in a lot of mc versions 21 | 22 | # What is a packet 23 | The Minecraft server and your Minecraft client share information called "packets". 24 | Example of a packet: 25 | - Any movement made by an entity 26 | - Opening a gui to a player 27 | - When the player use an item 28 | 29 | The interest of manipulation of packets is to send fake information to a specific group of players, like display a fake diamond block (client side) instead of a tnt (server side). 30 | You can do a lot of things with packets, but it's really hard to understand how to use them ... 31 | 32 | - This link can help you to identify the content of a packet: https://wiki.vg/Protocol 33 | - This link can help you to identify the arguments of a packet: https://minidigger.github.io/MiniMappingViewer/ 34 | - The wiki is a good start : https://github.com/Anarchick/skript-packet/wiki/Examples 35 | 36 | # Example of code using skript-packet 37 | 38 | ```applescript 39 | packet event play_client_held_item_slot: 40 | broadcast "slot changed" 41 | 42 | on packet event play_server_chat: 43 | cancel event if "%field 0 of event-packet%" contain "block.minecraft.set_spawn" 44 | ``` 45 | 46 |
47 | Example of 1.16.X high level coding (does not work in other versions) 48 | 49 | ```applescript 50 | function BiomeStorage(biome: biome) :: object: 51 | set {_id} to nms biome id of {_biome} 52 | if {BiomeStorage::%{_id}%} is not set: 53 | loop 1024 times: 54 | set {_biomeId::%loop-value%} to {_id} 55 | set {BiomeStorage::%{_id}%} to {_biomeId::*} as primitive int array 56 | return {BiomeStorage::%{_id}%} 57 | 58 | import: 59 | net.minecraft.server.v1_16_R3.PacketPlayOutMapChunk 60 | 61 | effect change client side biome of [chunk] %chunk% to %biome% for %players% : 62 | trigger: 63 | await 0.toString() # Async 64 | set {_chunk} to expression-1 65 | set {_MapChunk} to new PacketPlayOutMapChunk(nms chunk of {_chunk}, 65535) 66 | set {_packet} to new play_server_map_chunk packet 67 | set field 0 of {_packet} to {_chunk}.getX() 68 | set field 1 of {_packet} to {_chunk}.getZ() 69 | set field 2 of {_packet} to {_MapChunk}.c # int 70 | set field 3 of {_packet} to {_MapChunk}.d # NBTTagCompound 71 | set field 4 of {_packet} to BiomeStorage(expression-2) 72 | set {_byte::*} to ...{_MapChunk}.f 73 | set field 5 of {_packet} to {_byte::*} # Primitive byte array 74 | set field 6 of {_packet} to {_MapChunk}.g # Represent all Tiles Entities 75 | set field 7 of {_packet} to true # Represent a full chunk, biomes are store only if true 76 | set field 8 of {_packet} to true 77 | set field 9 of {_packet} to {_} # Empty ArrayList 78 | send packet {_packet} to expression-3 without calling event 79 | 80 | command /biome [] []: 81 | permission: fakebiome.cmd 82 | trigger: 83 | delete {biome} 84 | if arg-1 is set: 85 | set {biome} to arg-1 86 | BiomeStorage({biome}) 87 | send "Fake biome set to %{biome}%" to sender 88 | 89 | on async packet event play_server_map_chunk: 90 | {biome} is set 91 | field 7 of event-packet is true # Represent a full chunk, biomes are store only if true 92 | set field 4 of event-packet to BiomeStorage({biome}) 93 | ``` 94 |
95 | 96 | **More examples on the [wiki](https://github.com/Anarchick/skript-packet/wiki/Examples)** 97 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id("com.gradleup.shadow") version "8.3.3" 3 | id ("base") 4 | id("java-library") 5 | id("maven-publish") 6 | id("io.papermc.paperweight.userdev") version "2.0.0-beta.14" 7 | } 8 | 9 | repositories { 10 | maven { url 'https://hub.spigotmc.org/nexus/content/repositories/snapshots' } // Spigot 11 | maven { url 'https://repo.dmulloy2.net/nexus/repository/public/' } // ProtocolLib 12 | maven { url 'https://mvnrepository.com/artifact/org.json/json' } // DataWatcher 13 | 14 | maven { url 'https://repo.skriptlang.org/releases' } // Skript 15 | maven { url 'https://jitpack.io' } 16 | 17 | maven { url 'https://oss.sonatype.org/content/repositories/snapshots' } 18 | maven { url 'https://oss.sonatype.org/content/repositories/central' } 19 | maven { 20 | name = "jeffMediaPublic" 21 | url = uri("https://repo.jeff-media.com/public") 22 | } 23 | mavenCentral() 24 | maven { url 'https://libraries.minecraft.net' } // must be last to avoid conflicts 25 | } 26 | 27 | base { 28 | archivesName = "Skript-Packet" 29 | setVersion("3.0.1") 30 | setGroup("fr.anarchick.skriptpacket") 31 | } 32 | 33 | //version = '3.0.0' 34 | //group = 'fr.anarchick.skriptpacket' 35 | 36 | def mcVersion = '1.21' 37 | def mcSubVersion = '.4' 38 | def skriptVersion = '2.10.0' 39 | def protocolLibVersion = '5.3.0' 40 | def skriptReflectVersion = '2.4-dev1' 41 | 42 | dependencies { 43 | compileOnly (group: 'com.github.SkriptLang', name: 'Skript', version: skriptVersion) { 44 | exclude group: 'com.sk89q.worldguard', module: 'worldguard-legacy' 45 | exclude group: 'net.milkbowl.vault', module: 'Vault' 46 | } 47 | compileOnly group: 'net.md-5', name: 'bungeecord-api', version: mcVersion + '-R0.1-SNAPSHOT' 48 | compileOnly group: 'com.github.SkriptLang', name: 'skript-reflect', version: skriptReflectVersion 49 | compileOnly group: 'org.eclipse.jdt', name: 'org.eclipse.jdt.annotation', version: '2.2.600' 50 | compileOnly group: 'com.mojang', name: 'datafixerupper', version: '1.0.20' // ExprPair 51 | compileOnly group: "com.comphenix.protocol", name: "ProtocolLib", version: protocolLibVersion 52 | compileOnly group: 'org.json', name: 'json', version: '20231013' 53 | implementation("com.jeff_media:SpigotUpdateChecker:3.0.3") 54 | paperweight.paperDevBundle(mcVersion + mcSubVersion +'-R0.1-SNAPSHOT') 55 | } 56 | 57 | java { 58 | toolchain.languageVersion = JavaLanguageVersion.of(21) 59 | } 60 | 61 | tasks { 62 | compileJava { 63 | // Set the release flag. This configures what version bytecode the compiler will emit, as well as what JDK APIs are usable. 64 | // See https://openjdk.java.net/jeps/247 for more information. 65 | options.release = 21 66 | } 67 | } 68 | 69 | tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { 70 | relocate 'com.jeff_media.updatechecker', 'fr.anarchick.skriptpacket.updatechecker' 71 | } 72 | 73 | sourceSets { 74 | main { 75 | java { 76 | exclude 'fr/anarchick/skriptpacket/elements/deprecated/**' 77 | } 78 | } 79 | } 80 | 81 | processResources { 82 | def props = [version: version] 83 | inputs.properties props 84 | filteringCharset 'UTF-8' 85 | filesMatching('plugin.yml') { 86 | expand props 87 | } 88 | // Ensure filtering is applied correctly 89 | filter(org.apache.tools.ant.filters.ReplaceTokens, tokens: props) 90 | } 91 | 92 | [compileJava, compileTestJava]*.options*.encoding = 'UTF-8' 93 | 94 | publishing { 95 | publications { 96 | mavenJava(MavenPublication) { 97 | from components.java 98 | } 99 | } 100 | } 101 | 102 | tasks.register('export') { 103 | group = "other" 104 | description = "build gradle into external folder" 105 | def userHome = System.getenv('USERPROFILE').replace('\\', '/') 106 | def fromFile = 'build/libs/' + base.archivesName.get() + '-' + version + '-all.jar' 107 | // List of version folder to export the jar 108 | def versions = ['1.20.1', '1.21.1', '1.21.4'] 109 | doLast { 110 | versions.each { ver -> 111 | def destDir = file(userHome + '/Documents/minecraft/SkriptPacket/' + ver + '/plugins') 112 | if (destDir.exists()) { 113 | copy { 114 | from fromFile 115 | into destDir 116 | rename { String fileName -> 117 | fileName.replace('-all.jar', '.jar') 118 | } 119 | } 120 | } else { 121 | println "The folder '" + destDir + "' does not exist" 122 | } 123 | } 124 | } 125 | } 126 | 127 | tasks.build.finalizedBy(tasks.export) 128 | tasks.shadowJar.finalizedBy(tasks.export) -------------------------------------------------------------------------------- /cmd.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | cmd.exe 3 | gradle build && xcopy C:\Users\aeim\git\skript-packet\build\libs\Skript-Packet-2.1.0.jar C:\Users\aeim\Documents\minecraft\eclipse1.17\plugins\skript-packet.jar 4 | pause -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Anarchick/skript-packet/157b5284c2d901395a079c99e2567d054366a8c6/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Fri Oct 11 16:25:39 CEST 2024 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.12-bin.zip 5 | zipStoreBase=GRADLE_USER_HOME 6 | zipStorePath=wrapper/dists 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # 4 | # Copyright © 2015-2021 the original authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | # 21 | # Gradle start up script for POSIX generated by Gradle. 22 | # 23 | # Important for running: 24 | # 25 | # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is 26 | # noncompliant, but you have some other compliant shell such as ksh or 27 | # bash, then to run this script, type that shell name before the whole 28 | # command line, like: 29 | # 30 | # ksh Gradle 31 | # 32 | # Busybox and similar reduced shells will NOT work, because this script 33 | # requires all of these POSIX shell features: 34 | # * functions; 35 | # * expansions «$var», «${var}», «${var:-default}», «${var+SET}», 36 | # «${var#prefix}», «${var%suffix}», and «$( cmd )»; 37 | # * compound commands having a testable exit status, especially «case»; 38 | # * various built-in commands including «command», «set», and «ulimit». 39 | # 40 | # Important for patching: 41 | # 42 | # (2) This script targets any POSIX shell, so it avoids extensions provided 43 | # by Bash, Ksh, etc; in particular arrays are avoided. 44 | # 45 | # The "traditional" practice of packing multiple parameters into a 46 | # space-separated string is a well documented source of bugs and security 47 | # problems, so this is (mostly) avoided, by progressively accumulating 48 | # options in "$@", and eventually passing that to Java. 49 | # 50 | # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, 51 | # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; 52 | # see the in-line comments for details. 53 | # 54 | # There are tweaks for specific operating systems such as AIX, CygWin, 55 | # Darwin, MinGW, and NonStop. 56 | # 57 | # (3) This script is generated from the Groovy template 58 | # https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt 59 | # within the Gradle project. 60 | # 61 | # You can find Gradle at https://github.com/gradle/gradle/. 62 | # 63 | ############################################################################## 64 | 65 | # Attempt to set APP_HOME 66 | 67 | # Resolve links: $0 may be a link 68 | app_path=$0 69 | 70 | # Need this for daisy-chained symlinks. 71 | while 72 | APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path 73 | [ -h "$app_path" ] 74 | do 75 | ls=$( ls -ld "$app_path" ) 76 | link=${ls#*' -> '} 77 | case $link in #( 78 | /*) app_path=$link ;; #( 79 | *) app_path=$APP_HOME$link ;; 80 | esac 81 | done 82 | 83 | APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit 84 | 85 | APP_NAME="Gradle" 86 | APP_BASE_NAME=${0##*/} 87 | 88 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 89 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 90 | 91 | # Use the maximum available, or set MAX_FD != -1 to use that value. 92 | MAX_FD=maximum 93 | 94 | warn () { 95 | echo "$*" 96 | } >&2 97 | 98 | die () { 99 | echo 100 | echo "$*" 101 | echo 102 | exit 1 103 | } >&2 104 | 105 | # OS specific support (must be 'true' or 'false'). 106 | cygwin=false 107 | msys=false 108 | darwin=false 109 | nonstop=false 110 | case "$( uname )" in #( 111 | CYGWIN* ) cygwin=true ;; #( 112 | Darwin* ) darwin=true ;; #( 113 | MSYS* | MINGW* ) msys=true ;; #( 114 | NONSTOP* ) nonstop=true ;; 115 | esac 116 | 117 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 118 | 119 | 120 | # Determine the Java command to use to start the JVM. 121 | if [ -n "$JAVA_HOME" ] ; then 122 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 123 | # IBM's JDK on AIX uses strange locations for the executables 124 | JAVACMD=$JAVA_HOME/jre/sh/java 125 | else 126 | JAVACMD=$JAVA_HOME/bin/java 127 | fi 128 | if [ ! -x "$JAVACMD" ] ; then 129 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 130 | 131 | Please set the JAVA_HOME variable in your environment to match the 132 | location of your Java installation." 133 | fi 134 | else 135 | JAVACMD=java 136 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 137 | 138 | Please set the JAVA_HOME variable in your environment to match the 139 | location of your Java installation." 140 | fi 141 | 142 | # Increase the maximum file descriptors if we can. 143 | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then 144 | case $MAX_FD in #( 145 | max*) 146 | MAX_FD=$( ulimit -H -n ) || 147 | warn "Could not query maximum file descriptor limit" 148 | esac 149 | case $MAX_FD in #( 150 | '' | soft) :;; #( 151 | *) 152 | ulimit -n "$MAX_FD" || 153 | warn "Could not set maximum file descriptor limit to $MAX_FD" 154 | esac 155 | fi 156 | 157 | # Collect all arguments for the java command, stacking in reverse order: 158 | # * args from the command line 159 | # * the main class name 160 | # * -classpath 161 | # * -D...appname settings 162 | # * --module-path (only if needed) 163 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. 164 | 165 | # For Cygwin or MSYS, switch paths to Windows format before running java 166 | if "$cygwin" || "$msys" ; then 167 | APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) 168 | CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) 169 | 170 | JAVACMD=$( cygpath --unix "$JAVACMD" ) 171 | 172 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 173 | for arg do 174 | if 175 | case $arg in #( 176 | -*) false ;; # don't mess with options #( 177 | /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath 178 | [ -e "$t" ] ;; #( 179 | *) false ;; 180 | esac 181 | then 182 | arg=$( cygpath --path --ignore --mixed "$arg" ) 183 | fi 184 | # Roll the args list around exactly as many times as the number of 185 | # args, so each arg winds up back in the position where it started, but 186 | # possibly modified. 187 | # 188 | # NB: a `for` loop captures its iteration list before it begins, so 189 | # changing the positional parameters here affects neither the number of 190 | # iterations, nor the values presented in `arg`. 191 | shift # remove old arg 192 | set -- "$@" "$arg" # push replacement arg 193 | done 194 | fi 195 | 196 | # Collect all arguments for the java command; 197 | # * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of 198 | # shell script including quotes and variable substitutions, so put them in 199 | # double quotes to make sure that they get re-expanded; and 200 | # * put everything else in single quotes, so that it's not re-expanded. 201 | 202 | set -- \ 203 | "-Dorg.gradle.appname=$APP_BASE_NAME" \ 204 | -classpath "$CLASSPATH" \ 205 | org.gradle.wrapper.GradleWrapperMain \ 206 | "$@" 207 | 208 | # Use "xargs" to parse quoted args. 209 | # 210 | # With -n1 it outputs one arg per line, with the quotes and backslashes removed. 211 | # 212 | # In Bash we could simply go: 213 | # 214 | # readarray ARGS < <( xargs -n1 <<<"$var" ) && 215 | # set -- "${ARGS[@]}" "$@" 216 | # 217 | # but POSIX shell has neither arrays nor command substitution, so instead we 218 | # post-process each arg (as a line of input to sed) to backslash-escape any 219 | # character that might be a shell metacharacter, then use eval to reverse 220 | # that process (while maintaining the separation between arguments), and wrap 221 | # the whole thing up as a single "set" statement. 222 | # 223 | # This will of course break if any of these variables contains a newline or 224 | # an unmatched quote. 225 | # 226 | 227 | eval "set -- $( 228 | printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | 229 | xargs -n1 | 230 | sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | 231 | tr '\n' ' ' 232 | )" '"$@"' 233 | 234 | exec "$JAVACMD" "$@" 235 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto execute 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto execute 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :execute 68 | @rem Setup the command line 69 | 70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 71 | 72 | 73 | @rem Execute Gradle 74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 75 | 76 | :end 77 | @rem End local scope for the variables with windows NT shell 78 | if "%ERRORLEVEL%"=="0" goto mainEnd 79 | 80 | :fail 81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 82 | rem the _cmd.exe /c_ return code! 83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 84 | exit /b 1 85 | 86 | :mainEnd 87 | if "%OS%"=="Windows_NT" endlocal 88 | 89 | :omega 90 | -------------------------------------------------------------------------------- /settings.gradle.kts: -------------------------------------------------------------------------------- 1 | rootProject.name = "Skript-Packet" 2 | 3 | pluginManagement { 4 | repositories { 5 | gradlePluginPortal() 6 | maven("https://repo.papermc.io/repository/maven-public/") 7 | } 8 | } -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/Logging.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket; 2 | 3 | import java.util.logging.Logger; 4 | 5 | import org.bukkit.Bukkit; 6 | 7 | import net.md_5.bungee.api.ChatColor; 8 | import org.bukkit.command.ConsoleCommandSender; 9 | 10 | public class Logging { 11 | 12 | private static final String name = "&7[&bSkript-Packet&7] "; 13 | private static final ConsoleCommandSender LOGGER = Bukkit.getConsoleSender(); // Allow colored messages 14 | 15 | public static void info(String msg) { 16 | LOGGER.sendMessage(colored(name + "&7" + msg)); 17 | } 18 | 19 | public static void warn(String msg) { 20 | LOGGER.sendMessage(colored(name + "&e" + msg)); 21 | } 22 | 23 | public static void severe(String msg) { 24 | LOGGER.sendMessage(colored(name + "&c" + msg)); 25 | } 26 | 27 | private static String colored(String input) { 28 | return ChatColor.translateAlternateColorCodes('&', input); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/SkriptPacket.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket; 2 | 3 | import ch.njol.skript.ScriptLoader; 4 | import ch.njol.skript.Skript; 5 | import ch.njol.skript.SkriptAddon; 6 | import ch.njol.skript.lang.Expression; 7 | import ch.njol.skript.util.Version; 8 | import com.jeff_media.updatechecker.UpdateCheckSource; 9 | import com.jeff_media.updatechecker.UpdateChecker; 10 | import fr.anarchick.skriptpacket.elements.Types; 11 | import fr.anarchick.skriptpacket.packets.PacketManager; 12 | import fr.anarchick.skriptpacket.packets.SkriptPacketEventListener; 13 | import fr.anarchick.skriptpacket.util.Utils; 14 | import fr.anarchick.skriptpacket.util.converters.ConverterLogic; 15 | import org.bukkit.Bukkit; 16 | import org.bukkit.event.Event; 17 | import org.bukkit.plugin.PluginManager; 18 | import org.bukkit.plugin.java.JavaPlugin; 19 | 20 | public class SkriptPacket extends JavaPlugin { 21 | 22 | private static SkriptPacket INSTANCE; 23 | 24 | public static boolean isReflectAddon = false; 25 | 26 | public static final PluginManager pluginManager = Bukkit.getServer().getPluginManager(); 27 | 28 | public static final Version MINIMUM_PROTOCOLLIB_VERSION = new Version(5, 0, 0); 29 | public static final Version PROTOCOLLIB_VERSION = 30 | new Version(pluginManager.getPlugin("ProtocolLib").getDescription().getVersion()); 31 | 32 | public static final Version MINIMUM_SKRIPT_VERSION = new Version(2, 7, 0); 33 | public static Version SKRIPT_VERSION; 34 | public static Version VERSION; 35 | 36 | // You have to fork Skript-Packet to enable this !! 37 | public static boolean enableDeprecated = false; 38 | 39 | @Override 40 | public void onEnable() { 41 | INSTANCE = this; 42 | VERSION = new Version(getDescription().getVersion()); 43 | 44 | final PluginManager pluginManager = Bukkit.getPluginManager(); 45 | isReflectAddon = pluginManager.isPluginEnabled("skript-reflect"); 46 | 47 | if (isReflectAddon) { 48 | Logging.info("Support of skript-reflect wrapper enabled"); 49 | } 50 | 51 | SKRIPT_VERSION = Skript.getVersion(); 52 | 53 | if (SKRIPT_VERSION.isSmallerThan(MINIMUM_SKRIPT_VERSION)) { 54 | Logging.info("Your version of Skript is " + SKRIPT_VERSION); 55 | Logging.info("Skript-Packet requires that you run at least version " + MINIMUM_SKRIPT_VERSION + " of Skript"); 56 | // Does not disable the plugin, cause some syntaxes can still works 57 | } 58 | 59 | if (PROTOCOLLIB_VERSION.isSmallerThan(MINIMUM_PROTOCOLLIB_VERSION)) { 60 | Logging.info("Your version of ProtocolLib is " + PROTOCOLLIB_VERSION); 61 | Logging.info("Skript-Packet requires that you run at least version " + MINIMUM_PROTOCOLLIB_VERSION + " of ProtocolLib"); 62 | // Does not disable the plugin, cause some syntaxes can still works 63 | } 64 | 65 | try { 66 | 67 | if (Skript.isAcceptRegistrations()) { 68 | final SkriptAddon ADDON = Skript.registerAddon(this); 69 | Class.forName(Types.class.getName()); // Load first 70 | ADDON.loadClasses("fr.anarchick.skriptpacket", "elements"); 71 | //ADDON.loadClasses("fr.anarchick.skriptpacket", "sections"); 72 | } 73 | 74 | } catch ( Exception e ) { 75 | e.printStackTrace(); 76 | pluginManager.disablePlugin(this); 77 | return; 78 | } 79 | 80 | ConverterLogic.loadBiomeID(); 81 | 82 | // New Script Event API since https://github.com/SkriptLang/Skript/commit/751c1027d5770079a9242475a86c1bec904f0c33 83 | ScriptLoader.eventRegistry().register(ScriptLoader.ScriptPreInitEvent.class, SkriptPacketEventListener::beforeReload); 84 | 85 | int pluginId = 10270; 86 | Metrics metrics = new Metrics(this, pluginId); 87 | metrics.addCustomChart(new Metrics.SimplePie("skript_version", () -> 88 | SKRIPT_VERSION.toString())); 89 | metrics.addCustomChart(new Metrics.SimplePie("protocollib_version", 90 | () -> Utils.regexGroup("^((\\d+\\.?)+(-\\w+)?)", PROTOCOLLIB_VERSION.toString(), 1))); 91 | metrics.addCustomChart(new Metrics.SimplePie("skript-reflect_support", () -> 92 | String.valueOf(isReflectAddon))); 93 | 94 | Logging.info("is enable! Enjoy packets :D"); 95 | checkUpdate(); 96 | } 97 | 98 | @Override 99 | public void onDisable() { 100 | PacketManager.removeListeners(); 101 | PacketManager.removeAsyncListeners(); 102 | } 103 | 104 | public static SkriptPacket getInstance() { 105 | return INSTANCE; 106 | } 107 | 108 | private void checkUpdate() { 109 | new UpdateChecker(this, UpdateCheckSource.GITHUB_RELEASE_TAG, "Anarchick/skript-packet") 110 | .setDownloadLink("https://github.com/Anarchick/skript-packet/releases") 111 | .checkNow(); 112 | } 113 | 114 | @SuppressWarnings({"unchecked" }) 115 | public static boolean isCurrentEvent(Expression expr, String error, Class... clazz) { 116 | boolean result = expr.getParser().isCurrentEvent(clazz); 117 | 118 | if (!result) { 119 | Skript.error(error); 120 | } 121 | 122 | return result; 123 | } 124 | 125 | } 126 | -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/CustomComparators.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements; 2 | 3 | import ch.njol.skript.aliases.ItemType; 4 | import ch.njol.skript.util.slot.Slot; 5 | import fr.anarchick.skriptpacket.Logging; 6 | import org.bukkit.Material; 7 | import org.bukkit.block.Block; 8 | import org.bukkit.inventory.ItemStack; 9 | import org.skriptlang.skript.lang.comparator.Comparators; 10 | import org.skriptlang.skript.lang.comparator.Relation; 11 | 12 | public class CustomComparators { 13 | 14 | static { 15 | 16 | Logging.info("Register Skript comparators"); 17 | 18 | Comparators.registerComparator(Block.class, Material.class, 19 | (o1, o2) -> Relation.get(o1.getType().equals(o2))); 20 | 21 | Comparators.registerComparator(ItemStack.class, Material.class, 22 | (o1, o2) -> Relation.get(o1.getType().equals(o2))); 23 | 24 | Comparators.registerComparator(ItemType.class, Material.class, 25 | (o1, o2) -> Relation.get(o1.getMaterial().equals(o2))); 26 | 27 | Comparators.registerComparator(Slot.class, Material.class, 28 | (o1, o2) -> { 29 | 30 | if (o1.getItem() == null) { 31 | return Relation.get(Material.AIR.equals(o2)); 32 | } else { 33 | return Relation.get(o1.getItem().getType().equals(o2)); 34 | } 35 | 36 | }); 37 | 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/CustomConverters.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements; 2 | 3 | import fr.anarchick.skriptpacket.Logging; 4 | 5 | public class CustomConverters { 6 | 7 | static { 8 | 9 | Logging.info("Register Skript converters"); 10 | /* 11 | Converters.registerConverter(ItemStack.class, Material.class, ItemStack::getType); 12 | */ 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/Types.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements; 2 | 3 | import ch.njol.skript.classes.ClassInfo; 4 | import ch.njol.skript.classes.Parser; 5 | import ch.njol.skript.classes.Serializer; 6 | import ch.njol.skript.expressions.base.EventValueExpression; 7 | import ch.njol.skript.lang.ParseContext; 8 | import ch.njol.skript.registrations.Classes; 9 | import ch.njol.yggdrasil.Fields; 10 | import com.comphenix.protocol.PacketType; 11 | import com.comphenix.protocol.events.PacketContainer; 12 | import fr.anarchick.skriptpacket.Logging; 13 | import fr.anarchick.skriptpacket.elements.expressions.datawatcher.DataWatcher; 14 | import fr.anarchick.skriptpacket.packets.PacketManager; 15 | import org.eclipse.jdt.annotation.Nullable; 16 | import org.jetbrains.annotations.NotNull; 17 | 18 | import java.io.StreamCorruptedException; 19 | 20 | public class Types { 21 | 22 | static { 23 | 24 | Logging.info("Register Skript types"); 25 | 26 | if (Classes.getClassInfoNoError("packettype") == null) { 27 | Classes.registerClass(new ClassInfo<>(PacketType.class, "packettype") 28 | .user("packet ?types?") 29 | .name("PacketType") 30 | .since("1.0") 31 | .description("Represents the type of a packet from ProtocolLib") 32 | .usage(PacketManager.getAllPacketTypeNames()) 33 | .examples("broadcast all packettypes") 34 | .defaultExpression(new EventValueExpression<>(PacketType.class)) 35 | .supplier(PacketManager.PACKET_TYPES) 36 | .parser(new Parser<>() { 37 | 38 | @Override 39 | public boolean canParse(final @NotNull ParseContext context) { 40 | return true; 41 | } 42 | 43 | @Override 44 | @Nullable 45 | public PacketType parse(final @NotNull String name, final @NotNull ParseContext context) { 46 | return PacketManager.getPacketType(name); 47 | } 48 | 49 | @Override 50 | public @NotNull String toVariableNameString(PacketType packetType) { 51 | return PacketManager.getPacketName(packetType); 52 | } 53 | 54 | @Override 55 | public @NotNull String toString(PacketType packetType, int flags) { 56 | return PacketManager.getPacketName(packetType); 57 | } 58 | 59 | }) 60 | .serializer(new Serializer<>() { 61 | 62 | @Override 63 | public @NotNull Fields serialize(PacketType packetType) { 64 | final Fields f = new Fields(); 65 | f.putObject("packetType", PacketManager.getPacketName(packetType)); 66 | return f; 67 | } 68 | 69 | @Override 70 | public void deserialize(PacketType packetType, @NotNull Fields f) { 71 | assert false; 72 | } 73 | 74 | @Override 75 | public PacketType deserialize(@NotNull Fields f) throws StreamCorruptedException { 76 | final String name = (String) f.getObject("packetType"); 77 | 78 | if (name != null) { 79 | return PacketManager.getPacketType(name); 80 | } 81 | 82 | return null; 83 | } 84 | 85 | @Override 86 | @Nullable 87 | public PacketType deserialize(final @NotNull String s) { 88 | return PacketManager.getPacketType(s); 89 | } 90 | 91 | @Override 92 | public boolean mustSyncDeserialization() { 93 | return false; 94 | } 95 | 96 | @Override 97 | protected boolean canBeInstantiated() { 98 | return false; 99 | } 100 | 101 | }) 102 | ); 103 | } 104 | 105 | if (Classes.getClassInfoNoError("packet") == null) { 106 | Classes.registerClass(new ClassInfo<>(PacketContainer.class, "packet") 107 | .user("packets?") 108 | .name("Packet") 109 | .since("1.0") 110 | .description("Represents a packet from ProtocolLib") 111 | .usage("") 112 | .examples("") 113 | .defaultExpression(new EventValueExpression<>(PacketContainer.class)) 114 | .parser(new Parser<>() { 115 | 116 | @Override 117 | public boolean canParse(final @NotNull ParseContext context) { 118 | return false; 119 | } 120 | 121 | @Override 122 | @Nullable 123 | public PacketContainer parse(final @NotNull String packet, final @NotNull ParseContext context) { 124 | return null; 125 | } 126 | 127 | @Override 128 | public @NotNull String toVariableNameString(PacketContainer packet) { 129 | return packet.toString(); 130 | } 131 | 132 | @Override 133 | public @NotNull String toString(PacketContainer packet, int flags) { 134 | String str; 135 | 136 | try { 137 | str = packet.toString(); 138 | } catch (Exception e) { 139 | str = "PacketContainer[type=" + packet.getType() + ", structureModifier=INVALID_DATA]"; 140 | } 141 | 142 | return str; 143 | } 144 | 145 | }) 146 | ); 147 | } 148 | 149 | if (Classes.getClassInfoNoError("datawatcher") == null) { 150 | Classes.registerClass(new ClassInfo<>(DataWatcher.class, "datawatcher") 151 | .user("datawatchers?") 152 | .name("Datawatcher") 153 | .since("2.0") 154 | .description("A data watcher is a list of index (=integer) associate with a value (=object)") 155 | .usage("") 156 | .examples("") 157 | .parser(new Parser<>() { 158 | 159 | @Override 160 | public boolean canParse(final @NotNull ParseContext context) { 161 | return false; 162 | } 163 | 164 | @Override 165 | @Nullable 166 | public DataWatcher parse(final @NotNull String data, final @NotNull ParseContext context) { 167 | return null; 168 | } 169 | 170 | @Override 171 | public @NotNull String toVariableNameString(DataWatcher data) { 172 | return data.toJSON().toString(); 173 | } 174 | 175 | @Override 176 | public @NotNull String toString(DataWatcher data, int flags) { 177 | return data.toJSON().toString(); 178 | } 179 | 180 | }) 181 | ); 182 | } 183 | 184 | } 185 | 186 | } 187 | -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/conditions/IsBukkitMaterial.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.conditions; 2 | 3 | import ch.njol.skript.conditions.base.PropertyCondition; 4 | import ch.njol.skript.doc.Description; 5 | import ch.njol.skript.doc.Examples; 6 | import ch.njol.skript.doc.Name; 7 | import ch.njol.skript.doc.Since; 8 | import org.bukkit.Material; 9 | import org.jetbrains.annotations.NotNull; 10 | 11 | @Name("Is Bukkit Material") 12 | @Description("Check if a given object is an instance of org.bukkit.Material") 13 | @Examples("if {_something} is Bukkit Material:") 14 | @Since("2.2.0") 15 | 16 | public class IsBukkitMaterial extends PropertyCondition { 17 | 18 | static { 19 | register(IsBukkitMaterial.class, "Bukkit Material", "objects"); 20 | } 21 | 22 | @Override 23 | public boolean check(Object value) { 24 | return value instanceof Material; 25 | } 26 | 27 | @Override 28 | protected @NotNull String getPropertyName() { 29 | return "Bukkit Material"; 30 | } 31 | 32 | } -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/conditions/IsFromClient.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.conditions; 2 | 3 | import ch.njol.skript.conditions.base.PropertyCondition; 4 | import ch.njol.skript.doc.Description; 5 | import ch.njol.skript.doc.Examples; 6 | import ch.njol.skript.doc.Name; 7 | import ch.njol.skript.doc.Since; 8 | import com.comphenix.protocol.events.PacketContainer; 9 | import org.jetbrains.annotations.NotNull; 10 | 11 | @Name("Is from client") 12 | @Description("Check if a given packet is sent by the client to the server") 13 | @Examples("if {_packet} is from client:") 14 | @Since("2.2.0") 15 | 16 | public class IsFromClient extends PropertyCondition { 17 | 18 | static { 19 | register(IsFromClient.class, "from client", "packets"); 20 | } 21 | 22 | @Override 23 | public boolean check(PacketContainer value) { 24 | return value.getType().isClient(); 25 | } 26 | 27 | @Override 28 | protected @NotNull String getPropertyName() { 29 | return "from client"; 30 | } 31 | 32 | } -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/conditions/IsFromServer.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.conditions; 2 | 3 | import ch.njol.skript.conditions.base.PropertyCondition; 4 | import ch.njol.skript.doc.Description; 5 | import ch.njol.skript.doc.Examples; 6 | import ch.njol.skript.doc.Name; 7 | import ch.njol.skript.doc.Since; 8 | import com.comphenix.protocol.events.PacketContainer; 9 | import org.jetbrains.annotations.NotNull; 10 | 11 | @Name("Is from server") 12 | @Description("Check if a given packet is sent by the server to the client") 13 | @Examples("if {_packet} is from server:") 14 | @Since("2.2.0") 15 | 16 | public class IsFromServer extends PropertyCondition { 17 | 18 | static { 19 | register(IsFromServer.class, "from server", "packets"); 20 | } 21 | 22 | @Override 23 | public boolean check(PacketContainer value) { 24 | return value.getType().isServer(); 25 | } 26 | 27 | @Override 28 | protected @NotNull String getPropertyName() { 29 | return "from server"; 30 | } 31 | 32 | } -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/conditions/IsJavaList.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.conditions; 2 | 3 | import ch.njol.skript.conditions.base.PropertyCondition; 4 | import ch.njol.skript.doc.Description; 5 | import ch.njol.skript.doc.Examples; 6 | import ch.njol.skript.doc.Name; 7 | import ch.njol.skript.doc.Since; 8 | import org.jetbrains.annotations.NotNull; 9 | 10 | import java.util.List; 11 | 12 | @Name("Is Java List") 13 | @Description("Check if a given object is an instance of java.util.List") 14 | @Examples("if {_something} is Java List:") 15 | @Since("2.2.0") 16 | 17 | public class IsJavaList extends PropertyCondition { 18 | 19 | static { 20 | register(IsJavaList.class, "Java List", "objects"); 21 | } 22 | 23 | @Override 24 | public boolean check(Object value) { 25 | return value instanceof List; 26 | } 27 | 28 | @Override 29 | protected @NotNull String getPropertyName() { 30 | return "Java List"; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/conditions/IsNMS.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.conditions; 2 | 3 | import ch.njol.skript.conditions.base.PropertyCondition; 4 | import ch.njol.skript.doc.Description; 5 | import ch.njol.skript.doc.Examples; 6 | import ch.njol.skript.doc.Name; 7 | import ch.njol.skript.doc.Since; 8 | import com.comphenix.protocol.utility.MinecraftReflection; 9 | import org.jetbrains.annotations.NotNull; 10 | 11 | @Name("Is MMS") 12 | @Description("Check if a given object can be found within the package net.minecraft.server") 13 | @Examples("if {_something} is NMS:") 14 | @Since("2.2.0") 15 | 16 | public class IsNMS extends PropertyCondition { 17 | 18 | static { 19 | register(IsNMS.class, "NMS", "objects"); 20 | } 21 | 22 | @Override 23 | public boolean check(Object value) { 24 | return MinecraftReflection.isMinecraftObject(value); 25 | } 26 | 27 | @Override 28 | protected @NotNull String getPropertyName() { 29 | return "NMS"; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/deprecated/ExprArrayList.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.deprecated; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.List; 6 | 7 | import org.bukkit.event.Event; 8 | import org.eclipse.jdt.annotation.Nullable; 9 | 10 | import com.btk5h.skriptmirror.ObjectWrapper; 11 | 12 | import ch.njol.skript.Skript; 13 | import ch.njol.skript.doc.Description; 14 | import ch.njol.skript.doc.Examples; 15 | import ch.njol.skript.doc.Name; 16 | import ch.njol.skript.doc.Since; 17 | import ch.njol.skript.lang.Expression; 18 | import ch.njol.skript.lang.ExpressionType; 19 | import ch.njol.skript.lang.SkriptParser.ParseResult; 20 | import ch.njol.skript.lang.util.SimpleExpression; 21 | import ch.njol.util.Kleenean; 22 | import ch.njol.util.coll.CollectionUtils; 23 | import fr.anarchick.skriptpacket.SkriptPacket; 24 | 25 | @Name("ArrayList") 26 | @Description("Create a java ArrayList from objects") 27 | @Examples({ 28 | "set {_arrayList} to all players as arraylist", 29 | "set {_emptyArrayList} to {_} as arraylist" 30 | }) 31 | @Since("1.0") 32 | 33 | public class ExprArrayList extends SimpleExpression { 34 | 35 | private Expression expr; 36 | 37 | static { 38 | if (SkriptPacket.enableDeprecated) Skript.registerExpression(ExprArrayList.class, Object.class, ExpressionType.SIMPLE, 39 | "%objects% as arraylist"); 40 | } 41 | 42 | @Override 43 | @SuppressWarnings("unchecked") 44 | public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parser) { 45 | expr = (Expression) exprs[0]; 46 | return true; 47 | } 48 | 49 | @Override 50 | @Nullable 51 | protected Object[] get(Event e) { 52 | ArrayList Array = new ArrayList(); 53 | if (SkriptPacket.isReflectAddon) { 54 | for (Object _expr : expr.getAll(e)) { 55 | Array.add(ObjectWrapper.unwrapIfNecessary(_expr)); 56 | } 57 | } else { 58 | List list = Arrays.asList(expr.getAll(e)); 59 | Array.addAll(list); 60 | } 61 | return CollectionUtils.array(Array); 62 | } 63 | 64 | @Override 65 | public boolean isSingle() { 66 | return true; 67 | } 68 | 69 | @Override 70 | public Class getReturnType() { 71 | return Object.class; 72 | } 73 | 74 | @Override 75 | public String toString(@Nullable Event e, boolean debug) { 76 | return "%Objects% as arraylist"; 77 | } 78 | 79 | } -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/deprecated/ExprBaseComponent.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.deprecated; 2 | 3 | import org.bukkit.event.Event; 4 | import org.eclipse.jdt.annotation.Nullable; 5 | 6 | import com.comphenix.protocol.wrappers.ComponentConverter; 7 | import com.comphenix.protocol.wrappers.WrappedChatComponent; 8 | 9 | import ch.njol.skript.Skript; 10 | import ch.njol.skript.doc.Description; 11 | import ch.njol.skript.doc.Examples; 12 | import ch.njol.skript.doc.Name; 13 | import ch.njol.skript.doc.Since; 14 | import ch.njol.skript.lang.Expression; 15 | import ch.njol.skript.lang.ExpressionType; 16 | import ch.njol.skript.lang.SkriptParser.ParseResult; 17 | import ch.njol.skript.lang.util.SimpleExpression; 18 | import ch.njol.util.Kleenean; 19 | import fr.anarchick.skriptpacket.SkriptPacket; 20 | 21 | @Name("BaseComponent") 22 | @Description("Get net.md_5.bungee.api.chat.BaseComponent from a string") 23 | @Examples({ 24 | "set {_basecomponent} to basecomponent from text \"Connected\"", 25 | "set {_textcomponent} to basecomponent from json \"{text:'test',color:'red'}\"" 26 | }) 27 | @Since("1.2") 28 | 29 | public class ExprBaseComponent extends SimpleExpression { 30 | 31 | private Expression stringExpr; 32 | private int pattern; 33 | private final static String[] patterns; 34 | 35 | static { 36 | patterns = new String[] { 37 | "BaseComponent from text %string%", 38 | "BaseComponent from json %string%" 39 | }; 40 | if (SkriptPacket.enableDeprecated) Skript.registerExpression(ExprBaseComponent.class, Object.class, ExpressionType.SIMPLE, patterns); 41 | } 42 | 43 | @Override 44 | @SuppressWarnings("unchecked") 45 | public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parser) { 46 | pattern = matchedPattern; 47 | stringExpr = (Expression) exprs[0]; 48 | return true; 49 | } 50 | 51 | @Override 52 | @Nullable 53 | protected Object[] get(Event e) { 54 | final String text = stringExpr.getSingle(e); 55 | WrappedChatComponent wrapper = WrappedChatComponent.fromText(""); 56 | if (!text.isEmpty() || text != null) { 57 | switch (pattern) { 58 | case 0: 59 | wrapper = WrappedChatComponent.fromText(text); 60 | break; 61 | case 1: 62 | wrapper = WrappedChatComponent.fromJson(text); 63 | break; 64 | default: 65 | break; 66 | } 67 | } 68 | return new Object[] {ComponentConverter.fromWrapper(wrapper)}; 69 | } 70 | 71 | @Override 72 | public boolean isSingle() { 73 | return true; 74 | } 75 | 76 | @Override 77 | public Class getReturnType() { 78 | return Object.class; 79 | } 80 | 81 | @Override 82 | public String toString(@Nullable Event e, boolean debug) { 83 | return patterns[pattern]; 84 | } 85 | 86 | } -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/deprecated/ExprBukkitChatComponent.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.deprecated; 2 | 3 | import org.bukkit.event.Event; 4 | import org.eclipse.jdt.annotation.Nullable; 5 | 6 | import com.comphenix.protocol.utility.MinecraftReflection; 7 | import com.comphenix.protocol.wrappers.WrappedChatComponent; 8 | 9 | import ch.njol.skript.Skript; 10 | import ch.njol.skript.doc.Description; 11 | import ch.njol.skript.doc.Examples; 12 | import ch.njol.skript.doc.Name; 13 | import ch.njol.skript.doc.Since; 14 | import ch.njol.skript.lang.Expression; 15 | import ch.njol.skript.lang.ExpressionType; 16 | import ch.njol.skript.lang.SkriptParser.ParseResult; 17 | import ch.njol.skript.lang.util.SimpleExpression; 18 | import ch.njol.util.Kleenean; 19 | import fr.anarchick.skriptpacket.SkriptPacket; 20 | 21 | @Name("Bukkit ChatComponent") 22 | @Description("Convert an NMS (net.minecraft.server) ChatComponentText to a Json string") 23 | @Examples("set {_json} to chatcomponent from nms {_nmsChatComponent}") 24 | @Since("1.2") 25 | 26 | public class ExprBukkitChatComponent extends SimpleExpression { 27 | 28 | private Expression nmsExpr; 29 | 30 | static { 31 | if (SkriptPacket.enableDeprecated) Skript.registerExpression(ExprBukkitChatComponent.class, String.class, ExpressionType.SIMPLE, 32 | "chatcomponent from NMS %object%"); 33 | } 34 | 35 | @Override 36 | @SuppressWarnings("unchecked") 37 | public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parser) { 38 | nmsExpr = (Expression) exprs[0]; 39 | return true; 40 | } 41 | 42 | @Override 43 | @Nullable 44 | protected String[] get(Event e) { 45 | Object nms = nmsExpr.getSingle(e); 46 | if (MinecraftReflection.isMinecraftObject(nms, "ChatComponentText")) { 47 | return new String[] {WrappedChatComponent.fromHandle(nms).getJson()}; 48 | } 49 | return new String[0]; 50 | } 51 | 52 | @Override 53 | public boolean isSingle() { 54 | return true; 55 | } 56 | 57 | @Override 58 | public Class getReturnType() { 59 | return String.class; 60 | } 61 | 62 | @Override 63 | public String toString(@Nullable Event e, boolean debug) { 64 | return "chatcomponent from NMS " + nmsExpr.toString(e, debug); 65 | } 66 | 67 | } -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/deprecated/ExprBukkitChunk.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.deprecated; 2 | 3 | import java.lang.reflect.InvocationTargetException; 4 | 5 | import org.bukkit.Chunk; 6 | import org.bukkit.event.Event; 7 | import org.eclipse.jdt.annotation.Nullable; 8 | 9 | import com.comphenix.protocol.utility.MinecraftReflection; 10 | 11 | import ch.njol.skript.Skript; 12 | import ch.njol.skript.doc.Description; 13 | import ch.njol.skript.doc.Examples; 14 | import ch.njol.skript.doc.Name; 15 | import ch.njol.skript.doc.Since; 16 | import ch.njol.skript.lang.Expression; 17 | import ch.njol.skript.lang.ExpressionType; 18 | import ch.njol.skript.lang.SkriptParser.ParseResult; 19 | import ch.njol.skript.lang.util.SimpleExpression; 20 | import ch.njol.util.Kleenean; 21 | import fr.anarchick.skriptpacket.SkriptPacket; 22 | 23 | @Name("Bukkit Chunk") 24 | @Description("Convert an NMS (net.minecraft.server) Chunk to his Bukkit equivalent") 25 | @Examples("set {_chunk} to chunk from nms {_nmsChunk}") 26 | @Since("1.2") 27 | 28 | public class ExprBukkitChunk extends SimpleExpression { 29 | 30 | private Expression nmsExpr; 31 | 32 | static { 33 | if (SkriptPacket.enableDeprecated) Skript.registerExpression(ExprBukkitChunk.class, Chunk.class, ExpressionType.SIMPLE, 34 | "chunk from NMS %object%"); 35 | } 36 | 37 | @Override 38 | @SuppressWarnings("unchecked") 39 | public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parser) { 40 | nmsExpr = (Expression) exprs[0]; 41 | return true; 42 | } 43 | 44 | @Override 45 | @Nullable 46 | protected Chunk[] get(Event e) { 47 | Object nms = nmsExpr.getSingle(e); 48 | if (MinecraftReflection.isMinecraftObject(nms, "Chunk") ) { 49 | Chunk chunk; 50 | try { 51 | chunk = (Chunk) nms.getClass().getMethod("getBukkitChunk").invoke(nms); 52 | return new Chunk[] {chunk}; 53 | } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException 54 | | NoSuchMethodException | SecurityException e1) { 55 | Skript.exception(e1); 56 | } 57 | } 58 | return new Chunk[0]; 59 | } 60 | 61 | @Override 62 | public boolean isSingle() { 63 | return true; 64 | } 65 | 66 | @Override 67 | public Class getReturnType() { 68 | return Chunk.class; 69 | } 70 | 71 | @Override 72 | public String toString(@Nullable Event e, boolean debug) { 73 | return "chunk from NMS " + nmsExpr.toString(e, debug); 74 | } 75 | 76 | } -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/deprecated/ExprBukkitEntity.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.deprecated; 2 | 3 | import org.bukkit.entity.Entity; 4 | import org.bukkit.event.Event; 5 | import org.eclipse.jdt.annotation.Nullable; 6 | 7 | import com.comphenix.protocol.utility.MinecraftReflection; 8 | 9 | import ch.njol.skript.Skript; 10 | import ch.njol.skript.doc.Description; 11 | import ch.njol.skript.doc.Examples; 12 | import ch.njol.skript.doc.Name; 13 | import ch.njol.skript.doc.Since; 14 | import ch.njol.skript.lang.Expression; 15 | import ch.njol.skript.lang.ExpressionType; 16 | import ch.njol.skript.lang.SkriptParser.ParseResult; 17 | import ch.njol.skript.lang.util.SimpleExpression; 18 | import ch.njol.util.Kleenean; 19 | import fr.anarchick.skriptpacket.SkriptPacket; 20 | 21 | @Name("Bukkit Entity") 22 | @Description("Convert an NMS (net.minecraft.server) Entity to his Bukkit equivalent") 23 | @Examples({ 24 | "set {_player} to entity from nms {_nmsPlayer}", 25 | "set {_entity} to entity from nms {_nmsEntity}" 26 | }) 27 | @Since("1.2") 28 | 29 | public class ExprBukkitEntity extends SimpleExpression { 30 | 31 | private Expression nmsExpr; 32 | 33 | static { 34 | if (SkriptPacket.enableDeprecated) Skript.registerExpression(ExprBukkitEntity.class, Entity.class, ExpressionType.SIMPLE, 35 | "entity from NMS %object%"); 36 | } 37 | 38 | @Override 39 | @SuppressWarnings("unchecked") 40 | public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parser) { 41 | nmsExpr = (Expression) exprs[0]; 42 | return true; 43 | } 44 | 45 | @Override 46 | @Nullable 47 | protected Entity[] get(Event e) { 48 | Object nms = nmsExpr.getSingle(e); 49 | if (MinecraftReflection.isMinecraftEntity(nms) ) { // include Player 50 | return new Entity[] {(Entity) MinecraftReflection.getBukkitEntity(nms)}; 51 | } 52 | return new Entity[0]; 53 | } 54 | 55 | @Override 56 | public boolean isSingle() { 57 | return true; 58 | } 59 | 60 | @Override 61 | public Class getReturnType() { 62 | return Entity.class; 63 | } 64 | 65 | @Override 66 | public String toString(@Nullable Event e, boolean debug) { 67 | return "entity from NMS " + nmsExpr.toString(e, debug); 68 | } 69 | 70 | } -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/deprecated/ExprBukkitItemStack.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.deprecated; 2 | 3 | import org.bukkit.event.Event; 4 | import org.bukkit.inventory.ItemStack; 5 | import org.eclipse.jdt.annotation.Nullable; 6 | 7 | import com.comphenix.protocol.utility.MinecraftReflection; 8 | 9 | import ch.njol.skript.Skript; 10 | import ch.njol.skript.doc.Description; 11 | import ch.njol.skript.doc.Examples; 12 | import ch.njol.skript.doc.Name; 13 | import ch.njol.skript.doc.Since; 14 | import ch.njol.skript.lang.Expression; 15 | import ch.njol.skript.lang.ExpressionType; 16 | import ch.njol.skript.lang.SkriptParser.ParseResult; 17 | import ch.njol.skript.lang.util.SimpleExpression; 18 | import ch.njol.util.Kleenean; 19 | import fr.anarchick.skriptpacket.SkriptPacket; 20 | 21 | @Name("Bukkit ItemStack") 22 | @Description("Convert an NMS (net.minecraft.server) Entity to his Bukkit equivalent") 23 | @Examples("set {_item} to itemstack from nms {_nmsItemStack}") 24 | @Since("1.2") 25 | 26 | public class ExprBukkitItemStack extends SimpleExpression { 27 | 28 | private Expression nmsExpr; 29 | 30 | static { 31 | if (SkriptPacket.enableDeprecated) Skript.registerExpression(ExprBukkitItemStack.class, ItemStack.class, ExpressionType.SIMPLE, 32 | "itemstack from NMS %object%"); 33 | } 34 | 35 | @Override 36 | @SuppressWarnings("unchecked") 37 | public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parser) { 38 | nmsExpr = (Expression) exprs[0]; 39 | return true; 40 | } 41 | 42 | @Override 43 | @Nullable 44 | protected ItemStack[] get(Event e) { 45 | Object nms = nmsExpr.getSingle(e); 46 | if (MinecraftReflection.isItemStack(nms) ) { 47 | return new ItemStack[] {MinecraftReflection.getBukkitItemStack(nms)}; 48 | } 49 | return new ItemStack[0]; 50 | } 51 | 52 | @Override 53 | public boolean isSingle() { 54 | return true; 55 | } 56 | 57 | @Override 58 | public Class getReturnType() { 59 | return ItemStack.class; 60 | } 61 | 62 | @Override 63 | public String toString(@Nullable Event e, boolean debug) { 64 | return "itemstack from NMS " + nmsExpr.toString(e, debug); 65 | } 66 | 67 | } -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/deprecated/ExprBukkitMaterial.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.deprecated; 2 | 3 | import java.lang.reflect.InvocationTargetException; 4 | 5 | import org.bukkit.Material; 6 | import org.bukkit.event.Event; 7 | import org.eclipse.jdt.annotation.Nullable; 8 | 9 | import com.comphenix.protocol.utility.MinecraftReflection; 10 | import com.comphenix.protocol.wrappers.BukkitConverters; 11 | 12 | import ch.njol.skript.Skript; 13 | import ch.njol.skript.doc.Description; 14 | import ch.njol.skript.doc.Examples; 15 | import ch.njol.skript.doc.Name; 16 | import ch.njol.skript.doc.Since; 17 | import ch.njol.skript.lang.Expression; 18 | import ch.njol.skript.lang.ExpressionType; 19 | import ch.njol.skript.lang.SkriptParser.ParseResult; 20 | import ch.njol.skript.lang.util.SimpleExpression; 21 | import ch.njol.util.Kleenean; 22 | import fr.anarchick.skriptpacket.SkriptPacket; 23 | 24 | @Name("Bukkit Material") 25 | @Description("Convert an NMS (net.minecraft.server) IBlockData or ItemStack to his Bukkit Material") 26 | @Examples("set {_material} to material from nms {_nmsIBlockData}") 27 | @Since("1.2") 28 | 29 | public class ExprBukkitMaterial extends SimpleExpression { 30 | 31 | private Expression nmsExpr; 32 | 33 | static { 34 | if (SkriptPacket.enableDeprecated) Skript.registerExpression(ExprBukkitMaterial.class, Material.class, ExpressionType.SIMPLE, 35 | "material from NMS %object%"); 36 | } 37 | 38 | @Override 39 | @SuppressWarnings("unchecked") 40 | public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parser) { 41 | nmsExpr = (Expression) exprs[0]; 42 | return true; 43 | } 44 | 45 | @Override 46 | @Nullable 47 | protected Material[] get(Event e) { 48 | Object nms = nmsExpr.getSingle(e); 49 | Material material = null; 50 | if (MinecraftReflection.isMinecraftObject(nms)) { 51 | if (MinecraftReflection.isMinecraftObject(nms, "IBlockData") ) { 52 | try { 53 | material = (Material) nms.getClass().getMethod("getBukkitMaterial").invoke(nms); 54 | } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException 55 | | NoSuchMethodException | SecurityException e1) { 56 | Skript.exception(e1); 57 | } 58 | } else if (MinecraftReflection.isMinecraftObject(nms, "Block") ) { 59 | material = BukkitConverters.getBlockConverter().getSpecific(nms); 60 | } else if (MinecraftReflection.isItemStack(nms) ) { 61 | material = MinecraftReflection.getBukkitItemStack(nms).getType(); 62 | } 63 | } 64 | return new Material[] {material}; 65 | } 66 | 67 | @Override 68 | public boolean isSingle() { 69 | return true; 70 | } 71 | 72 | @Override 73 | public Class getReturnType() { 74 | return Material.class; 75 | } 76 | 77 | @Override 78 | public String toString(@Nullable Event e, boolean debug) { 79 | return "material from NMS " + nmsExpr.toString(e, debug); 80 | } 81 | 82 | } -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/deprecated/ExprBukkitWorld.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.deprecated; 2 | 3 | import java.lang.reflect.InvocationTargetException; 4 | 5 | import org.bukkit.World; 6 | import org.bukkit.event.Event; 7 | import org.eclipse.jdt.annotation.Nullable; 8 | 9 | import com.comphenix.protocol.utility.MinecraftReflection; 10 | 11 | import ch.njol.skript.Skript; 12 | import ch.njol.skript.doc.Description; 13 | import ch.njol.skript.doc.Examples; 14 | import ch.njol.skript.doc.Name; 15 | import ch.njol.skript.doc.Since; 16 | import ch.njol.skript.lang.Expression; 17 | import ch.njol.skript.lang.ExpressionType; 18 | import ch.njol.skript.lang.SkriptParser.ParseResult; 19 | import ch.njol.skript.lang.util.SimpleExpression; 20 | import ch.njol.util.Kleenean; 21 | import fr.anarchick.skriptpacket.SkriptPacket; 22 | 23 | @Name("Bukkit World") 24 | @Description("Convert an NMS (net.minecraft.server) WorldServer to his Bukkit equivalent") 25 | @Examples("set {_world} to world from nms {_nmsWorld}") 26 | @Since("1.2") 27 | 28 | public class ExprBukkitWorld extends SimpleExpression { 29 | 30 | private Expression nmsExpr; 31 | 32 | static { 33 | if (SkriptPacket.enableDeprecated) Skript.registerExpression(ExprBukkitWorld.class, World.class, ExpressionType.SIMPLE, 34 | "world from NMS %object%"); 35 | } 36 | 37 | @Override 38 | @SuppressWarnings("unchecked") 39 | public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parser) { 40 | nmsExpr = (Expression) exprs[0]; 41 | return true; 42 | } 43 | 44 | @Override 45 | @Nullable 46 | protected World[] get(Event e) { 47 | Object nms = nmsExpr.getSingle(e); 48 | if (MinecraftReflection.isMinecraftObject(nms, "WorldServer") ) { 49 | World world; 50 | try { 51 | world = (World) nms.getClass().getMethod("getWorld").invoke(nms); 52 | return new World[] {world}; 53 | } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException 54 | | NoSuchMethodException | SecurityException e1) { 55 | Skript.exception(e1); 56 | } 57 | } 58 | return new World[0]; 59 | } 60 | 61 | @Override 62 | public boolean isSingle() { 63 | return true; 64 | } 65 | 66 | @Override 67 | public Class getReturnType() { 68 | return World.class; 69 | } 70 | 71 | @Override 72 | public String toString(@Nullable Event e, boolean debug) { 73 | return "world from NMS " + nmsExpr.toString(e, debug); 74 | } 75 | 76 | } -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/deprecated/ExprJavaUUID.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.deprecated; 2 | 3 | import java.util.UUID; 4 | import java.util.regex.Matcher; 5 | import java.util.regex.Pattern; 6 | 7 | import org.bukkit.event.Event; 8 | import org.eclipse.jdt.annotation.Nullable; 9 | 10 | import ch.njol.skript.Skript; 11 | import ch.njol.skript.doc.Description; 12 | import ch.njol.skript.doc.Examples; 13 | import ch.njol.skript.doc.Name; 14 | import ch.njol.skript.doc.Since; 15 | import ch.njol.skript.lang.Expression; 16 | import ch.njol.skript.lang.ExpressionType; 17 | import ch.njol.skript.lang.SkriptParser.ParseResult; 18 | import ch.njol.skript.lang.util.SimpleExpression; 19 | import ch.njol.util.Kleenean; 20 | import fr.anarchick.skriptpacket.SkriptPacket; 21 | 22 | @Name("Java UUID") 23 | @Description("Get a java.util.UUID from a String UUID or create new one") 24 | @Examples({ 25 | "set {_uuid} to java uuid of (uuid of player)", 26 | "set {_nms} to a new java uuid" 27 | }) 28 | @Since("1.2") 29 | 30 | public class ExprJavaUUID extends SimpleExpression { 31 | 32 | 33 | private final static Pattern regex = Pattern.compile("^[\\da-f]{8}-([\\da-f]{4}-){3}[\\da-f]{12}$", Pattern.CASE_INSENSITIVE); 34 | private Expression uuidExpr; 35 | private int pattern; 36 | private final static String[] patterns = new String[] { 37 | "[the] java uuid (from|of) %string%", 38 | "[a] new java uuid" 39 | }; 40 | 41 | static { 42 | if (SkriptPacket.enableDeprecated) Skript.registerExpression(ExprJavaUUID.class, UUID.class, ExpressionType.SIMPLE, patterns); 43 | } 44 | 45 | @Override 46 | @SuppressWarnings("unchecked") 47 | public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parser) { 48 | pattern = matchedPattern; 49 | if (pattern == 0) uuidExpr = (Expression) exprs[0]; 50 | return true; 51 | } 52 | 53 | @Override 54 | @Nullable 55 | protected UUID[] get(Event e) { 56 | if (pattern == 0) { 57 | String uuid = uuidExpr.getSingle(e); 58 | if (uuid != null) { 59 | Matcher matcher = regex.matcher(uuid); 60 | if (matcher.find()) return new UUID[] {UUID.fromString(uuid)}; 61 | } 62 | return new UUID[0]; 63 | } 64 | return new UUID[] {UUID.randomUUID()}; 65 | } 66 | 67 | @Override 68 | public boolean isSingle() { 69 | return true; 70 | } 71 | 72 | @Override 73 | public Class getReturnType() { 74 | return UUID.class; 75 | } 76 | 77 | @Override 78 | public String toString(@Nullable Event e, boolean debug) { 79 | return patterns[pattern]; 80 | } 81 | 82 | } -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/deprecated/ExprLocationFromNMS.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.deprecated; 2 | 3 | import org.bukkit.Location; 4 | import org.bukkit.World; 5 | import org.bukkit.event.Event; 6 | import org.eclipse.jdt.annotation.Nullable; 7 | 8 | import com.comphenix.protocol.utility.MinecraftReflection; 9 | import com.comphenix.protocol.wrappers.BlockPosition; 10 | 11 | import ch.njol.skript.Skript; 12 | import ch.njol.skript.doc.Description; 13 | import ch.njol.skript.doc.Examples; 14 | import ch.njol.skript.doc.Name; 15 | import ch.njol.skript.doc.Since; 16 | import ch.njol.skript.lang.Expression; 17 | import ch.njol.skript.lang.ExpressionType; 18 | import ch.njol.skript.lang.SkriptParser.ParseResult; 19 | import ch.njol.skript.lang.util.SimpleExpression; 20 | import ch.njol.util.Kleenean; 21 | import fr.anarchick.skriptpacket.SkriptPacket; 22 | 23 | @Name("Location From NMS") 24 | @Description("Convert an NMS (net.minecraft.server) BlockPosition to a Bukkit Location") 25 | @Examples("set {_loc} to location from nms {_nmsblockPosition} in world of player") 26 | @Since("1.2") 27 | 28 | public class ExprLocationFromNMS extends SimpleExpression { 29 | 30 | private Expression nmsExpr; 31 | private Expression worldExpr; 32 | 33 | static { 34 | if (SkriptPacket.enableDeprecated) Skript.registerExpression(ExprLocationFromNMS.class, Location.class, ExpressionType.SIMPLE, 35 | "location from NMS %object% in %world%"); 36 | } 37 | 38 | @Override 39 | @SuppressWarnings("unchecked") 40 | public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parser) { 41 | nmsExpr = (Expression) exprs[0]; 42 | worldExpr = (Expression) exprs[1]; 43 | return true; 44 | } 45 | 46 | @Override 47 | @Nullable 48 | protected Location[] get(Event e) { 49 | Object nms = nmsExpr.getSingle(e); 50 | World world = worldExpr.getSingle(e); 51 | if (MinecraftReflection.isBlockPosition(nms) ) { 52 | return new Location[] { BlockPosition.getConverter().getSpecific(nms).toLocation(world)}; 53 | } else if (nms instanceof Location) { 54 | return new Location[] {(Location) nms}; 55 | } 56 | return new Location[0]; 57 | } 58 | 59 | @Override 60 | public boolean isSingle() { 61 | return true; 62 | } 63 | 64 | @Override 65 | public Class getReturnType() { 66 | return Location.class; 67 | } 68 | 69 | @Override 70 | public String toString(@Nullable Event e, boolean debug) { 71 | return "location from NMS " + nmsExpr.toString(e, debug) +" in "+ worldExpr.toString(e, debug); 72 | } 73 | 74 | } -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/deprecated/ExprNMSBiomeID.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.deprecated; 2 | 3 | import fr.anarchick.skriptpacket.util.converters.ConverterLogic; 4 | import org.bukkit.block.Biome; 5 | 6 | import ch.njol.skript.doc.Description; 7 | import ch.njol.skript.doc.Examples; 8 | import ch.njol.skript.doc.Name; 9 | import ch.njol.skript.doc.Since; 10 | import ch.njol.skript.expressions.base.SimplePropertyExpression; 11 | import fr.anarchick.skriptpacket.SkriptPacket; 12 | import org.jetbrains.annotations.NotNull; 13 | 14 | @Name("NMS Biome ID") 15 | @Description({"Get the NMS ID of biome. This method return a number", 16 | "Not guaranted to be up to date. Refer to https://minecraft.gamepedia.com/Biome/ID" 17 | }) 18 | @Examples("broadcast \"%nms biome id of crimson forest%\"") 19 | @Since("1.1 (mc1.16)") 20 | 21 | public class ExprNMSBiomeID extends SimplePropertyExpression{ 22 | 23 | static { 24 | if (SkriptPacket.enableDeprecated) register(ExprNMSBiomeID.class, Number.class, "nms biome id", "biome"); 25 | } 26 | 27 | @Override 28 | public Number convert(Biome biome) { 29 | return ConverterLogic.getBiomeID(biome); 30 | } 31 | 32 | @Override 33 | public @NotNull Class getReturnType() { 34 | return Number.class; 35 | } 36 | 37 | @Override 38 | protected @NotNull String getPropertyName() { 39 | return "nms biome id of %biome%"; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/deprecated/ExprNMSBlockPosition.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.deprecated; 2 | 3 | import ch.njol.skript.Skript; 4 | import ch.njol.skript.doc.Description; 5 | import ch.njol.skript.doc.Examples; 6 | import ch.njol.skript.doc.Name; 7 | import ch.njol.skript.doc.Since; 8 | import ch.njol.skript.lang.Expression; 9 | import ch.njol.skript.lang.ExpressionType; 10 | import ch.njol.skript.lang.SkriptParser.ParseResult; 11 | import ch.njol.skript.lang.util.SimpleExpression; 12 | import ch.njol.util.Kleenean; 13 | import fr.anarchick.skriptpacket.SkriptPacket; 14 | import fr.anarchick.skriptpacket.util.converters.ConverterToNMS; 15 | import org.bukkit.Location; 16 | import org.bukkit.event.Event; 17 | import org.bukkit.util.Vector; 18 | import org.eclipse.jdt.annotation.Nullable; 19 | 20 | @Name("NMS Block Position") 21 | @Description("Get the NMS (net.minecraft.server) BlockPosition from a location or a vector") 22 | @Examples({ 23 | "set {_nmsBlockPosition} to nms block position from location of player", 24 | "set {_nmsBlockPosition} to nms block position from new vector 0, 0, 0" 25 | }) 26 | @Since("1.0") 27 | 28 | public class ExprNMSBlockPosition extends SimpleExpression { 29 | 30 | private Expression loc; 31 | private Expression vector; 32 | private int pattern; 33 | private final static String[] patterns; 34 | 35 | static { 36 | patterns = new String[] { 37 | "NMS block[ ]position from %location%", 38 | "NMS block[ ]position from %vector%" 39 | }; 40 | if (SkriptPacket.enableDeprecated) Skript.registerExpression(ExprNMSBlockPosition.class, Object.class, ExpressionType.SIMPLE, patterns); 41 | } 42 | 43 | @Override 44 | @SuppressWarnings("unchecked") 45 | public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parser) { 46 | pattern = matchedPattern; 47 | if (pattern == 0) loc = (Expression) exprs[0]; 48 | if (pattern == 1) vector = (Expression) exprs[0]; 49 | return true; 50 | } 51 | 52 | @Override 53 | @Nullable 54 | protected Object[] get(Event e) { 55 | if (pattern == 0) { // Location 56 | Location _loc = loc.getSingle(e); 57 | return new Object[] {ConverterToNMS.RELATED_TO_NMS_BLOCKPOSITION.convert(_loc)}; 58 | } else if (pattern == 1) { // Vector 59 | Vector _vec = vector.getSingle(e); 60 | return new Object[] {ConverterToNMS.RELATED_TO_NMS_BLOCKPOSITION.convert(_vec)}; 61 | } 62 | return new Object[] {null}; 63 | } 64 | 65 | @Override 66 | public boolean isSingle() { 67 | return true; 68 | } 69 | 70 | @Override 71 | public Class getReturnType() { 72 | return Object.class; 73 | } 74 | 75 | @Override 76 | public String toString(@Nullable Event e, boolean debug) { 77 | return patterns[pattern]; 78 | } 79 | 80 | } -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/deprecated/ExprNMSText.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.deprecated; 2 | 3 | import org.bukkit.event.Event; 4 | import org.eclipse.jdt.annotation.Nullable; 5 | import org.json.JSONObject; 6 | 7 | import com.comphenix.protocol.wrappers.WrappedChatComponent; 8 | 9 | import ch.njol.skript.Skript; 10 | import ch.njol.skript.doc.Description; 11 | import ch.njol.skript.doc.Examples; 12 | import ch.njol.skript.doc.Name; 13 | import ch.njol.skript.doc.Since; 14 | import ch.njol.skript.lang.Expression; 15 | import ch.njol.skript.lang.ExpressionType; 16 | import ch.njol.skript.lang.SkriptParser.ParseResult; 17 | import ch.njol.skript.lang.util.SimpleExpression; 18 | import ch.njol.util.Kleenean; 19 | import ch.njol.util.coll.CollectionUtils; 20 | import fr.anarchick.skriptpacket.SkriptPacket; 21 | 22 | @Name("NMS TextComponent") 23 | @Description("Get the NMS (net.minecraft.server) textcomponent from a string") 24 | @Examples({ 25 | "set {_textcomponent} to nms textcomponent from text \"Connected\"", 26 | "set {_textcomponent} to nms textcomponent from json \"{text:'test',color:'red'}\"" 27 | }) 28 | @Since("1.2") 29 | 30 | public class ExprNMSText extends SimpleExpression { 31 | 32 | private static final Object[] empty = CollectionUtils.array(WrappedChatComponent.fromText("").getHandle()); 33 | private Expression stringExpr; 34 | private final static String[] patterns = new String[] { 35 | "NMS TextComponent from text %string%" 36 | }; 37 | 38 | static { 39 | if (SkriptPacket.enableDeprecated) Skript.registerExpression(ExprNMSText.class, Object.class, ExpressionType.SIMPLE, patterns); 40 | } 41 | 42 | @Override 43 | @SuppressWarnings("unchecked") 44 | public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parser) { 45 | stringExpr = (Expression) exprs[0]; 46 | return true; 47 | } 48 | 49 | @Override 50 | @Nullable 51 | protected Object[] get(Event e) { 52 | String text = stringExpr.getSingle(e); 53 | if (text == null ||text.isEmpty()) return empty; 54 | if (text.startsWith("{") && text.endsWith("}")) { 55 | try { 56 | @SuppressWarnings("unused") 57 | JSONObject json = new JSONObject(text); 58 | return CollectionUtils.array(WrappedChatComponent.fromJson(text).getHandle()); 59 | } catch (Exception ex) { 60 | return null; 61 | } 62 | } 63 | return CollectionUtils.array(WrappedChatComponent.fromText(text).getHandle()); 64 | // Object[] handle = Arrays.stream(WrappedChatComponent.fromChatMessage(text)) 65 | // .map(WrappedChatComponent::getHandle) 66 | // .toArray(Object[]::new); 67 | // return CollectionUtils.array(handle); 68 | } 69 | 70 | @Override 71 | public boolean isSingle() { 72 | return true; 73 | } 74 | 75 | @Override 76 | public Class getReturnType() { 77 | return Object.class; 78 | } 79 | 80 | @Override 81 | public String toString(@Nullable Event e, boolean debug) { 82 | return "NMS TextComponent from text " + stringExpr.toString(e, debug); 83 | } 84 | 85 | } -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/deprecated/ExprNewPJSON.java: -------------------------------------------------------------------------------- 1 | //package fr.anarchick.skriptpacket.elements.deprecated; 2 | // 3 | //import org.bukkit.event.Event; 4 | //import org.eclipse.jdt.annotation.Nullable; 5 | // 6 | //import ch.njol.skript.Skript; 7 | //import ch.njol.skript.lang.Expression; 8 | //import ch.njol.skript.lang.ExpressionType; 9 | //import ch.njol.skript.lang.SkriptParser.ParseResult; 10 | //import ch.njol.skript.lang.util.SimpleExpression; 11 | //import ch.njol.util.Kleenean; 12 | //import ch.njol.util.coll.CollectionUtils; 13 | //import fr.anarchick.skriptpacket.SkriptPacket; 14 | // 15 | //public class ExprNewPJSON extends SimpleExpression { 16 | // 17 | // private Expression objExpr; 18 | // private int pattern; 19 | // private static final String[] patterns = new String[] { 20 | // "new pjson [from %-object%]" 21 | // }; 22 | // 23 | // static { 24 | // if (SkriptPacket.enableDeprecated) Skript.registerExpression(ExprNewPJSON.class, PJSON.class, ExpressionType.SIMPLE, patterns); 25 | // } 26 | // 27 | // @Override 28 | // public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { 29 | // pattern = matchedPattern; 30 | // objExpr =(Expression) exprs[0]; 31 | // return true; 32 | // } 33 | // 34 | // @Override 35 | // protected PJSON[] get(Event e) { 36 | // if (objExpr == null) return CollectionUtils.array(new PJSON()); 37 | // Object obj = objExpr.getSingle(e); 38 | // return CollectionUtils.array(PJSON.create(obj)); 39 | // } 40 | // 41 | // @Override 42 | // public boolean isSingle() { 43 | // return true; 44 | // } 45 | // 46 | // @Override 47 | // public Class getReturnType() { 48 | // return PJSON.class; 49 | // } 50 | // 51 | // @Override 52 | // public String toString(@Nullable Event e, boolean debug) { 53 | // return patterns[pattern]; 54 | // } 55 | // 56 | //} 57 | -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/deprecated/ExprNewWatchableObject.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.deprecated; 2 | 3 | import org.bukkit.event.Event; 4 | import org.eclipse.jdt.annotation.Nullable; 5 | 6 | import com.comphenix.protocol.wrappers.WrappedWatchableObject; 7 | 8 | import ch.njol.skript.Skript; 9 | import ch.njol.skript.lang.Expression; 10 | import ch.njol.skript.lang.ExpressionType; 11 | import ch.njol.skript.lang.SkriptParser.ParseResult; 12 | import ch.njol.skript.lang.util.SimpleExpression; 13 | import ch.njol.util.Kleenean; 14 | import fr.anarchick.skriptpacket.SkriptPacket; 15 | 16 | public class ExprNewWatchableObject extends SimpleExpression { 17 | 18 | private Expression indexExpr; 19 | private Expression objExpr; 20 | private int pattern; 21 | private static final String[] patterns = new String[] { 22 | "new watchable [object] from NMS %object%", 23 | "new watchable [object] with index %number% and value %object%" 24 | }; 25 | 26 | static { 27 | if (SkriptPacket.enableDeprecated) Skript.registerExpression(ExprNewWatchableObject.class, WrappedWatchableObject.class, ExpressionType.SIMPLE, patterns); 28 | } 29 | 30 | @Override 31 | @SuppressWarnings("unchecked") 32 | public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { 33 | pattern = matchedPattern; 34 | if (pattern == 0) { 35 | objExpr = (Expression) exprs[0]; 36 | } else { 37 | indexExpr = (Expression) exprs[0]; 38 | objExpr = (Expression) exprs[1]; 39 | } 40 | return true; 41 | } 42 | 43 | @Override 44 | protected WrappedWatchableObject[] get(Event event) { 45 | Object obj = objExpr.getSingle(event); 46 | if (obj == null) return new WrappedWatchableObject[0]; 47 | WrappedWatchableObject wrapper = null; 48 | if (pattern == 0) { 49 | wrapper = new WrappedWatchableObject(obj); 50 | } else { 51 | Number index = indexExpr.getSingle(event); 52 | if (index == null) return new WrappedWatchableObject[0]; 53 | wrapper = new WrappedWatchableObject(index.intValue(), obj); 54 | } 55 | return new WrappedWatchableObject[] {wrapper}; 56 | } 57 | 58 | @Override 59 | public boolean isSingle() { 60 | return true; 61 | } 62 | 63 | @Override 64 | public Class getReturnType() { 65 | return WrappedWatchableObject.class; 66 | } 67 | 68 | @Override 69 | public String toString(@Nullable Event e, boolean debug) { 70 | return patterns[pattern]; 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/deprecated/ExprPJSON.java: -------------------------------------------------------------------------------- 1 | //package fr.anarchick.skriptpacket.elements.deprecated; 2 | // 3 | //import org.bukkit.event.Event; 4 | //import org.eclipse.jdt.annotation.Nullable; 5 | // 6 | //import ch.njol.skript.Skript; 7 | //import ch.njol.skript.classes.Changer.ChangeMode; 8 | //import ch.njol.skript.lang.Expression; 9 | //import ch.njol.skript.lang.ExpressionType; 10 | //import ch.njol.skript.lang.SkriptParser.ParseResult; 11 | //import ch.njol.skript.lang.util.SimpleExpression; 12 | //import ch.njol.util.Kleenean; 13 | //import ch.njol.util.coll.CollectionUtils; 14 | //import fr.anarchick.skriptpacket.SkriptPacket; 15 | // 16 | //public class ExprPJSON extends SimpleExpression { 17 | // 18 | // private Expression keyExpr; 19 | // private Expression pjsonExpr; 20 | // 21 | // static { 22 | // if (SkriptPacket.enableDeprecated) Skript.registerExpression(ExprPJSON.class, Object.class, ExpressionType.SIMPLE, "[pjson] key %string% of %pjson%"); 23 | // } 24 | // 25 | // @Override 26 | // @SuppressWarnings("unchecked") 27 | // public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { 28 | // keyExpr = (Expression) exprs[0]; 29 | // pjsonExpr = (Expression) exprs[1]; 30 | // return true; 31 | // } 32 | // 33 | // @Override 34 | // protected Object[] get(Event event) { 35 | // String key = keyExpr.getSingle(event); 36 | // PJSON pjson = pjsonExpr.getSingle(event); 37 | // if (key == null || pjson == null) return null; 38 | // return new Object[] {pjson.get(key)}; 39 | // } 40 | // 41 | // @Override 42 | // public Class[] acceptChange(ChangeMode mode) { 43 | // if (mode == ChangeMode.SET || mode == ChangeMode.DELETE) { 44 | // return CollectionUtils.array(Object.class); 45 | // } 46 | // return null; 47 | // } 48 | // 49 | // @Override 50 | // public void change(Event e, Object[] delta, ChangeMode mode) { 51 | // String key = keyExpr.getSingle(e); 52 | // PJSON pjson = pjsonExpr.getSingle(e); 53 | // if (key == null || pjson == null || delta[0] == null) return; 54 | // if (mode == ChangeMode.SET) { 55 | // pjson.put(key, delta[0]); 56 | // } else if (mode == ChangeMode.DELETE) { 57 | // pjson.remove(key); 58 | // } 59 | // } 60 | // 61 | // @Override 62 | // public boolean isSingle() { 63 | // return true; 64 | // } 65 | // 66 | // @Override 67 | // public Class getReturnType() { 68 | // return Object.class; 69 | // } 70 | // 71 | // @Override 72 | // public String toString(@Nullable Event e, boolean debug) { 73 | // return "pjson key " + keyExpr.toString(e, debug) + " of " + pjsonExpr.toString(e, debug); 74 | // } 75 | // 76 | //} 77 | -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/deprecated/ExprReadableJson.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.deprecated; 2 | 3 | import org.bukkit.event.Event; 4 | import org.eclipse.jdt.annotation.Nullable; 5 | 6 | import ch.njol.skript.Skript; 7 | import ch.njol.skript.doc.Description; 8 | import ch.njol.skript.doc.Examples; 9 | import ch.njol.skript.doc.Name; 10 | import ch.njol.skript.doc.Since; 11 | import ch.njol.skript.lang.Expression; 12 | import ch.njol.skript.lang.ExpressionType; 13 | import ch.njol.skript.lang.SkriptParser.ParseResult; 14 | import ch.njol.skript.lang.util.SimpleExpression; 15 | import ch.njol.util.Kleenean; 16 | import fr.anarchick.skriptpacket.SkriptPacket; 17 | import net.md_5.bungee.api.chat.BaseComponent; 18 | import net.md_5.bungee.chat.ComponentSerializer; 19 | 20 | @Name("Readable Json") 21 | @Description("Get a readable text from a Json string") 22 | @Examples("set {_text} to json \"{Text:Hello,Color:red}\" as readable text") 23 | @Since("1.2") 24 | 25 | public class ExprReadableJson extends SimpleExpression { 26 | 27 | private Expression jsonExpr; 28 | 29 | static { 30 | if (SkriptPacket.enableDeprecated) Skript.registerExpression(ExprReadableJson.class, String.class, ExpressionType.SIMPLE, 31 | "json %string% as readable text"); 32 | } 33 | 34 | @Override 35 | @SuppressWarnings("unchecked") 36 | public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parser) { 37 | jsonExpr = (Expression) exprs[0]; 38 | return true; 39 | } 40 | 41 | @Override 42 | @Nullable 43 | protected String[] get(Event e) { 44 | String json = jsonExpr.getSingle(e); 45 | return new String[] {BaseComponent.toLegacyText(ComponentSerializer.parse(json))}; 46 | } 47 | 48 | @Override 49 | public boolean isSingle() { 50 | return true; 51 | } 52 | 53 | @Override 54 | public Class getReturnType() { 55 | return String.class; 56 | } 57 | 58 | @Override 59 | public String toString(@Nullable Event e, boolean debug) { 60 | return "json '" + jsonExpr.toString(e, debug) + "' as readable text"; 61 | } 62 | 63 | } -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/deprecated/ExprWatchableObjectIndex.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.deprecated; 2 | 3 | import com.comphenix.protocol.wrappers.WrappedWatchableObject; 4 | 5 | import ch.njol.skript.expressions.base.SimplePropertyExpression; 6 | import fr.anarchick.skriptpacket.SkriptPacket; 7 | 8 | public class ExprWatchableObjectIndex extends SimplePropertyExpression{ 9 | 10 | static { 11 | if (SkriptPacket.enableDeprecated) register(ExprWatchableObjectIndex.class, Number.class, "watched index", "watcheritem"); 12 | } 13 | 14 | @Override 15 | public Number convert(WrappedWatchableObject watcher) { 16 | return watcher.getIndex(); 17 | } 18 | 19 | @Override 20 | public Class getReturnType() { 21 | return Number.class; 22 | } 23 | 24 | @Override 25 | protected String getPropertyName() { 26 | return "watched index"; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/deprecated/ExprWatchableObjectValue.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.deprecated; 2 | 3 | import org.bukkit.event.Event; 4 | 5 | import com.comphenix.protocol.wrappers.WrappedWatchableObject; 6 | 7 | import ch.njol.skript.classes.Changer.ChangeMode; 8 | import ch.njol.skript.expressions.base.SimplePropertyExpression; 9 | import ch.njol.util.coll.CollectionUtils; 10 | import fr.anarchick.skriptpacket.SkriptPacket; 11 | 12 | public class ExprWatchableObjectValue extends SimplePropertyExpression{ 13 | 14 | static { 15 | if (SkriptPacket.enableDeprecated) register(ExprWatchableObjectValue.class, Object.class, "watched value", "watcheritem"); 16 | } 17 | 18 | @Override 19 | public Object convert(WrappedWatchableObject watcher) { 20 | return watcher.getValue(); 21 | } 22 | 23 | @Override 24 | public Class[] acceptChange(final ChangeMode mode) { 25 | return (mode == ChangeMode.SET) ? CollectionUtils.array(Object.class) : null; 26 | } 27 | 28 | @Override 29 | public void change(Event e, Object[] delta, ChangeMode mode){ 30 | if (delta != null && mode == ChangeMode.SET) { 31 | WrappedWatchableObject watcher = getExpr().getSingle(e); 32 | watcher.setValue(delta[0]); 33 | } 34 | } 35 | 36 | @Override 37 | public Class getReturnType() { 38 | return Object.class; 39 | } 40 | 41 | @Override 42 | protected String getPropertyName() { 43 | return "watched value"; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/deprecated/PJSON.java: -------------------------------------------------------------------------------- 1 | //package fr.anarchick.skriptpacket.elements.deprecated; 2 | // 3 | //import java.util.HashMap; 4 | //import java.util.List; 5 | //import java.util.Map; 6 | // 7 | //import javax.annotation.Nonnull; 8 | // 9 | //import org.json.JSONException; 10 | //import org.json.JSONObject; 11 | // 12 | //import com.comphenix.protocol.utility.MinecraftReflection; 13 | //import com.comphenix.protocol.wrappers.WrappedDataWatcher; 14 | //import com.comphenix.protocol.wrappers.WrappedWatchableObject; 15 | //import com.comphenix.protocol.wrappers.WrappedDataWatcher.Serializer; 16 | //import com.google.common.collect.ImmutableMap; 17 | // 18 | //import fr.anarchick.skriptpacket.util.converters.ConverterLogic.Auto; 19 | // 20 | //public class PJSON { 21 | // 22 | // private Map map = new HashMap<>(); 23 | // 24 | // public PJSON() {} 25 | // 26 | // public PJSON(Map map) { 27 | // this.map = map; 28 | // } 29 | // 30 | // public PJSON(String jsonString) { 31 | // if (jsonString != null) { 32 | // try { 33 | // JSONObject json = new JSONObject(jsonString); 34 | // map = json.toMap(); 35 | // } catch (JSONException ex) {} 36 | // } 37 | // } 38 | // @SuppressWarnings("unchecked") 39 | // public static PJSON create(Object obj) { 40 | // if (MinecraftReflection.isMinecraftObject(obj)) 41 | // return fromNMS(obj); 42 | // if (obj instanceof String) 43 | // return new PJSON((String) obj); 44 | // if (obj instanceof WrappedWatchableObject) 45 | // return fromDataWatcher((WrappedWatchableObject) obj); 46 | // if (obj instanceof WrappedDataWatcher) 47 | // return fromDataWatcher((WrappedDataWatcher) obj); 48 | // if (obj instanceof Map) 49 | // return new PJSON((Map) obj); 50 | // return null; 51 | // } 52 | // 53 | // 54 | // public static PJSON fromNMS(Object nms) { 55 | // if (MinecraftReflection.isMinecraftObject(nms, "DataWatcher$Item")) { 56 | // return fromDataWatcher(new WrappedWatchableObject(nms)); 57 | // } else if (MinecraftReflection.isMinecraftObject(nms, "DataWatcher")) { 58 | // return fromDataWatcher(new WrappedDataWatcher(nms)); 59 | // } 60 | // return null; 61 | // } 62 | // 63 | // public static PJSON fromDataWatcher(@Nonnull WrappedWatchableObject datawatcher) { 64 | // Map map = new HashMap<>(); 65 | // String index = String.valueOf(datawatcher.getIndex()); 66 | // map.put(index, datawatcher.getValue()); 67 | // return new PJSON(map); 68 | // } 69 | // 70 | // public static PJSON fromDataWatcher(@Nonnull WrappedDataWatcher datawatcher) { 71 | // Map map = new HashMap<>(); 72 | // for (int i : datawatcher.getIndexes()) { 73 | // map.put(String.valueOf(i), datawatcher.getObject(i)); 74 | // } 75 | // return new PJSON(map); 76 | // } 77 | // 78 | // public PJSON merge(PJSON from) { 79 | // Map map1 = getMap(); 80 | // Map map2 = from.getMap(); 81 | // Map map = new HashMap<>(map1); 82 | // map.putAll(map2); 83 | // return new PJSON(map); 84 | // } 85 | // 86 | // public void put(String key, Object value) { 87 | // map.put(key, value); 88 | // } 89 | // 90 | // @SuppressWarnings("unchecked") 91 | // public T get(String key) { 92 | // return (T) map.get(key); 93 | // } 94 | // 95 | // @SuppressWarnings("unchecked") 96 | // public T remove(String key) { 97 | // return (T) map.remove(key); 98 | // } 99 | // 100 | // public String toString() { 101 | // JSONObject json = toJSON(); 102 | // return (json != null) ? json.toString() : null; 103 | // 104 | // } 105 | // 106 | // public Object toMojangson() { 107 | // return Auto.STRING_TO_MOJANGSON.convert(toString()); 108 | // } 109 | // 110 | // public WrappedDataWatcher toDataWatcher() { 111 | // WrappedDataWatcher dw = new WrappedDataWatcher(); 112 | // for (String str : map.keySet()) { 113 | // Integer index = Integer.valueOf(str); 114 | // if (index != null) { 115 | // Object value = map.get(str); 116 | // if (value != null) { 117 | // Serializer serializer = WrappedDataWatcher.Registry.get(value.getClass()); 118 | // dw.setObject(index, serializer, value); 119 | // } 120 | // } 121 | // } 122 | // return dw; 123 | // } 124 | // 125 | // public List toDataWatcherItems() { 126 | // WrappedDataWatcher dw = toDataWatcher(); 127 | // return dw.getWatchableObjects(); 128 | // } 129 | // 130 | // public WrappedWatchableObject toDataWatcherItem(Integer index) { 131 | // String str = index.toString(); 132 | // if (!map.containsKey(str)) return null; 133 | // return new WrappedWatchableObject(index, map.get(str)); 134 | // } 135 | // 136 | // public JSONObject toJSON() { 137 | // try { 138 | // return new JSONObject(map); 139 | // } catch (Exception ex) { 140 | // return null; 141 | // } 142 | // } 143 | // 144 | // private Map getMap() { 145 | // return ImmutableMap.copyOf(map); 146 | // } 147 | // 148 | //} 149 | -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/effects/EffReceivePacket.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.effects; 2 | 3 | import org.bukkit.entity.Player; 4 | import org.bukkit.event.Event; 5 | 6 | import com.comphenix.protocol.events.PacketContainer; 7 | 8 | import ch.njol.skript.Skript; 9 | import ch.njol.skript.doc.Description; 10 | import ch.njol.skript.doc.Examples; 11 | import ch.njol.skript.doc.Name; 12 | import ch.njol.skript.doc.Since; 13 | import ch.njol.skript.lang.Effect; 14 | import ch.njol.skript.lang.Expression; 15 | import ch.njol.skript.lang.SkriptParser.ParseResult; 16 | import ch.njol.util.Kleenean; 17 | import fr.anarchick.skriptpacket.packets.PacketManager; 18 | import org.jetbrains.annotations.NotNull; 19 | 20 | import java.util.Arrays; 21 | 22 | @Name("Receive Packet") 23 | @Description("Makes the server simulate receiving the specified packet(s) from the specified player(s)") 24 | @Examples({ 25 | "function serverSideSlot(players: players, slot: number):", 26 | "\tset {_packet} to new play_client_held_item_slot packet", 27 | "\tset field 0 of packet {_packet} to {_slot}", 28 | "\treceive {_players::*} packet {_packet}" 29 | }) 30 | @Since("1.1") 31 | 32 | public class EffReceivePacket extends Effect{ 33 | 34 | private Expression packetsExpr; 35 | private Expression playersExpr; 36 | private boolean bypassEvent = false; 37 | 38 | static { 39 | Skript.registerEffect(EffReceivePacket.class, "rec(ei|ie)ve packet[s] %packets% from %players% [without calling event]", 40 | "rec(ei|ie)ve %players% packet[s] %packets% [without calling event]"); 41 | } 42 | 43 | @Override 44 | @SuppressWarnings("unchecked") 45 | public boolean init(Expression[] expr, int i, @NotNull Kleenean kleenean, ParseResult parseResult) { 46 | packetsExpr = (Expression) expr[i]; 47 | playersExpr = (Expression) expr[(i + 1) % 2]; 48 | bypassEvent = parseResult.expr.toLowerCase().endsWith(" without calling event"); 49 | return true; 50 | } 51 | 52 | @Override 53 | protected void execute(@NotNull Event e) { 54 | final PacketContainer[] packets = packetsExpr.getAll(e); // Size of 1 in most of the cases 55 | 56 | for (PacketContainer packet : packets) { 57 | 58 | if (bypassEvent) { 59 | packet.setMeta("bypassEvent", true); 60 | } 61 | 62 | PacketManager.receivePacket(packet, playersExpr.getAll(e)); 63 | } 64 | 65 | } 66 | 67 | @Override 68 | public @NotNull String toString(Event e, boolean b) { 69 | return "receive packet " + Arrays.toString(packetsExpr.getAll(e)) + " from " + Arrays.toString(playersExpr.getAll(e)); 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/effects/EffSendPacket.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.effects; 2 | 3 | import org.bukkit.entity.Player; 4 | import org.bukkit.event.Event; 5 | 6 | import com.comphenix.protocol.events.PacketContainer; 7 | 8 | import ch.njol.skript.Skript; 9 | import ch.njol.skript.doc.Description; 10 | import ch.njol.skript.doc.Examples; 11 | import ch.njol.skript.doc.Name; 12 | import ch.njol.skript.doc.Since; 13 | import ch.njol.skript.lang.Effect; 14 | import ch.njol.skript.lang.Expression; 15 | import ch.njol.skript.lang.SkriptParser.ParseResult; 16 | import ch.njol.util.Kleenean; 17 | import fr.anarchick.skriptpacket.packets.PacketManager; 18 | import org.jetbrains.annotations.NotNull; 19 | 20 | import java.util.Arrays; 21 | 22 | @Name("Send Packet") 23 | @Description("Sends the specified packet(s) to the specified player(s).") 24 | @Examples({ 25 | "function unloadChunk(x: number, z: number, players: players):", 26 | "\tset {_packet} to new play_server_unload_chunk packet", 27 | "\tset field 0 of packet {_packet} to {_x}", 28 | "\tset field 1 of packet {_packet} to {_z}", 29 | "\tsend {_players::*} packet {_packet}" 30 | }) 31 | @Since("1.0, 1.1 (without calling event)") 32 | 33 | public class EffSendPacket extends Effect { 34 | 35 | private Expression packetsExpr; 36 | private Expression playersExpr; 37 | private boolean bypassEvent = false; 38 | 39 | static { 40 | Skript.registerEffect(EffSendPacket.class, 41 | "(dispatch|send) packet[s] %packets% to %players% [without calling event]", 42 | "(dispatch|send) %players% packet[s] %packets% [without calling event]"); 43 | } 44 | 45 | @Override 46 | @SuppressWarnings("unchecked") 47 | public boolean init(Expression[] expr, int i, @NotNull Kleenean kleenean, ParseResult parseResult) { 48 | packetsExpr = (Expression) expr[i]; 49 | playersExpr = (Expression) expr[(i + 1) % 2]; 50 | bypassEvent = parseResult.expr.toLowerCase().endsWith(" without calling event"); 51 | return true; 52 | } 53 | 54 | @Override 55 | protected void execute(@NotNull Event e) { 56 | final PacketContainer[] packets = packetsExpr.getAll(e); // Size of 1 in most of the cases 57 | 58 | for (PacketContainer packet : packets) { 59 | 60 | if (bypassEvent) { 61 | packet.setMeta("bypassEvent", true); 62 | } 63 | 64 | PacketManager.sendPacket(packet, playersExpr.getAll(e)); 65 | } 66 | 67 | } 68 | 69 | @Override 70 | public @NotNull String toString(Event e, boolean b) { 71 | return "send packet " + Arrays.toString(packetsExpr.getAll(e)) + " to " + Arrays.toString(playersExpr.getAll(e)); 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/effects/EffUpdateEntity.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.effects; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import org.bukkit.entity.Entity; 7 | import org.bukkit.entity.Player; 8 | import org.bukkit.event.Event; 9 | 10 | import com.comphenix.protocol.ProtocolLibrary; 11 | import ch.njol.skript.Skript; 12 | import ch.njol.skript.doc.Description; 13 | import ch.njol.skript.doc.Examples; 14 | import ch.njol.skript.doc.Name; 15 | import ch.njol.skript.doc.Since; 16 | import ch.njol.skript.lang.Effect; 17 | import ch.njol.skript.lang.Expression; 18 | import ch.njol.skript.lang.SkriptParser.ParseResult; 19 | import ch.njol.util.Kleenean; 20 | import org.jetbrains.annotations.NotNull; 21 | 22 | @Name("Packet Update Entity") 23 | @Description("Force the update of an entity to a specific player(s)") 24 | @Examples("packet update {_entity} for all players in world of {_entity}") 25 | @Since("1.1") 26 | 27 | public class EffUpdateEntity extends Effect{ 28 | 29 | private Expression entitiesExpr; 30 | private Expression playersExpr; 31 | 32 | static { 33 | Skript.registerEffect(EffUpdateEntity.class, 34 | "packet update %entities% for %players%"); 35 | } 36 | 37 | @Override 38 | @SuppressWarnings("unchecked") 39 | public boolean init(Expression[] expr, int i, @NotNull Kleenean kleenean, @NotNull ParseResult parseResult) { 40 | entitiesExpr = (Expression) expr[0]; 41 | playersExpr = (Expression) expr[1]; 42 | return true; 43 | } 44 | 45 | @Override 46 | protected void execute(@NotNull Event e) { 47 | final Player[] players = playersExpr.getAll(e); 48 | final List list = Arrays.asList(players); 49 | 50 | for (Entity entity : entitiesExpr.getAll(e)) { 51 | ProtocolLibrary.getProtocolManager().updateEntity(entity, list); 52 | } 53 | 54 | } 55 | 56 | @Override 57 | public @NotNull String toString(Event e, boolean b) { 58 | return "packet update " + Arrays.toString(entitiesExpr.getAll(e)) + " to " + Arrays.toString(playersExpr.getAll(e)); 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/events/EvtPacket.java: -------------------------------------------------------------------------------- 1 | 2 | package fr.anarchick.skriptpacket.elements.events; 3 | 4 | 5 | import ch.njol.skript.Skript; 6 | import ch.njol.skript.lang.Literal; 7 | import ch.njol.skript.lang.SkriptEvent; 8 | import ch.njol.skript.lang.SkriptParser.ParseResult; 9 | import ch.njol.skript.registrations.EventValues; 10 | import com.comphenix.protocol.PacketType; 11 | import com.comphenix.protocol.events.ListenerPriority; 12 | import com.comphenix.protocol.events.PacketContainer; 13 | import fr.anarchick.skriptpacket.packets.BukkitPacketEvent; 14 | import fr.anarchick.skriptpacket.packets.PacketManager; 15 | import fr.anarchick.skriptpacket.packets.PacketManager.Mode; 16 | import fr.anarchick.skriptpacket.packets.SkriptPacketEventListener; 17 | import org.bukkit.World; 18 | import org.bukkit.entity.Player; 19 | import org.bukkit.event.Event; 20 | import org.eclipse.jdt.annotation.Nullable; 21 | import org.jetbrains.annotations.NotNull; 22 | 23 | import java.util.Objects; 24 | 25 | public class EvtPacket extends SkriptEvent { 26 | 27 | private Mode mode = Mode.DEFAULT; 28 | private Literal packetTypeLit; 29 | private ListenerPriority priority; 30 | 31 | static { 32 | Skript.registerEvent("Packet Event - Skript-Packet", EvtPacket.class, BukkitPacketEvent.class, 33 | "[(sync|async)] packet event %packettype%") 34 | .description("Called when a packet of one of the specified types is being sent or received.") 35 | .examples("packet event play_server_entity_equipments:", 36 | "\tbroadcast \"equipment changed\"") 37 | .since("1.0, 2.0 (sync/async)"); 38 | 39 | // event-packet 40 | EventValues.registerEventValue(BukkitPacketEvent.class, PacketContainer.class, BukkitPacketEvent::getPacket); 41 | // event-player 42 | EventValues.registerEventValue(BukkitPacketEvent.class, Player.class, BukkitPacketEvent::getPlayer); 43 | // event-world 44 | EventValues.registerEventValue(BukkitPacketEvent.class, World.class, (e) -> e.getPlayer().getWorld()); 45 | } 46 | 47 | @Override 48 | @SuppressWarnings("unchecked") 49 | public boolean init(Literal[] literal, int matchedPattern, ParseResult parser) { 50 | packetTypeLit = (Literal) literal[0]; 51 | switch (getEventPriority()) { 52 | case LOWEST -> priority = ListenerPriority.LOWEST; 53 | case LOW -> priority = ListenerPriority.LOW; 54 | case HIGH -> priority = ListenerPriority.HIGH; 55 | case HIGHEST -> priority = ListenerPriority.HIGHEST; 56 | case MONITOR -> priority = ListenerPriority.MONITOR; 57 | default -> priority = ListenerPriority.NORMAL; 58 | } 59 | 60 | final PacketType packetType = packetTypeLit.getSingle(); 61 | 62 | if (parser.expr.startsWith("async")) { 63 | mode = Mode.ASYNC; 64 | } else if (parser.expr.startsWith("sync")) { 65 | mode = Mode.SYNC; 66 | 67 | if (packetType.isAsyncForced()) { 68 | Skript.error("The packettype '"+PacketManager.getPacketName(packetType)+"' can't be use in SYNC"); 69 | return false; 70 | } 71 | 72 | } 73 | 74 | if (!packetType.isSupported()) { 75 | Skript.error("The packettype '"+PacketManager.getPacketName(packetType)+"' is not supported by your server"); 76 | return false; 77 | } 78 | 79 | final String scriptName = getScriptName(); 80 | SkriptPacketEventListener.register(packetTypeLit.getAll(), priority, mode, scriptName); 81 | return true; 82 | } 83 | 84 | private String getScriptName() { 85 | return getParser().getCurrentScript().getConfig().getFileName(); 86 | } 87 | 88 | @Override 89 | public boolean check(@NotNull Event event) { 90 | 91 | if (event instanceof BukkitPacketEvent e) { 92 | 93 | if (Objects.equals(packetTypeLit.getSingle(event), e.getPacketType()) 94 | && priority.equals(e.getPriority()) 95 | && mode.equals(e.getMode()) ) { 96 | return e.getPacket().getMeta("bypassEvent").isEmpty(); 97 | } 98 | 99 | } 100 | 101 | return false; 102 | } 103 | 104 | @Override 105 | public boolean canExecuteAsynchronously() { 106 | return mode == Mode.ASYNC; 107 | } 108 | 109 | @Override 110 | public @NotNull String toString(@Nullable Event e, boolean debug) { 111 | return mode.name() + " packet event " + packetTypeLit.toString(e, debug) + " with " + priority.name() + " priority"; 112 | } 113 | 114 | } -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/expressions/datawatcher/DataWatcher.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.expressions.datawatcher; 2 | 3 | import com.comphenix.protocol.events.PacketContainer; 4 | import com.comphenix.protocol.wrappers.WrappedDataValue; 5 | import com.comphenix.protocol.wrappers.WrappedDataWatcher; 6 | import fr.anarchick.skriptpacket.util.NumberEnums; 7 | import fr.anarchick.skriptpacket.util.Utils; 8 | import fr.anarchick.skriptpacket.util.converters.Converter; 9 | import fr.anarchick.skriptpacket.util.converters.ConverterLogic; 10 | import fr.anarchick.skriptpacket.util.converters.ConverterToUtility; 11 | import org.bukkit.util.Vector; 12 | import org.json.JSONObject; 13 | 14 | import javax.annotation.Nonnull; 15 | import javax.annotation.Nullable; 16 | import java.util.*; 17 | 18 | public record DataWatcher(PacketContainer packet) { 19 | 20 | public DataWatcher(@Nonnull PacketContainer packet) { 21 | this.packet = packet; 22 | } 23 | 24 | public List getWrappedDataValues() { 25 | return packet.getDataValueCollectionModifier().readSafely(0); 26 | } 27 | 28 | public List getWrappedDataValuesWithout(int index) { 29 | final List wrappedDataValues = getWrappedDataValues(); 30 | 31 | for (int i = 0; i < wrappedDataValues.size(); i++) { 32 | 33 | if (wrappedDataValues.get(i).getIndex() == index) { 34 | wrappedDataValues.remove(i); 35 | break; 36 | } 37 | 38 | } 39 | return wrappedDataValues; 40 | } 41 | 42 | public void set(Number index, Object value) { 43 | if (index == null || value == null) { 44 | return; 45 | } 46 | 47 | WrappedDataWatcher.Serializer serializer = getSerializer(index.intValue()); 48 | 49 | if (value instanceof Vector vector) { 50 | value = ConverterToUtility.BUKKIT_VECTOR_TO_PROTOCOLLIB_VECTOR3F.convert(vector); 51 | } 52 | 53 | if (serializer == null) { 54 | serializer = WrappedDataWatcher.Registry.get(value.getClass()); 55 | } else if (!serializer.getType().isInstance(value)) { 56 | final Class targetClass = serializer.getType(); 57 | 58 | if (NumberEnums.isNumber(targetClass) && value instanceof Number number) { 59 | value = NumberEnums.convert(serializer.getType(), number); 60 | } else if (targetClass.isEnum() && value instanceof String enumName) { 61 | value = Utils.getEnum(targetClass, enumName); 62 | } else { 63 | final Converter converter = ConverterLogic.getConverter(value.getClass(), targetClass); 64 | value = converter.convert(value); 65 | } 66 | 67 | } 68 | 69 | set(index.intValue(), serializer, value); 70 | } 71 | 72 | public void set(int index, @Nonnull WrappedDataWatcher.Serializer serializer, Object value) { 73 | 74 | try { 75 | final WrappedDataValue wrappedDataValue = new WrappedDataValue(index, serializer, value); 76 | final List wrappedDataValues = getWrappedDataValuesWithout(index); 77 | wrappedDataValues.add(wrappedDataValue); 78 | packet.getDataValueCollectionModifier().writeSafely(0, wrappedDataValues); 79 | } catch (Exception e) { 80 | e.printStackTrace(); 81 | } 82 | 83 | } 84 | 85 | @Nullable 86 | public WrappedDataValue get(int index) { 87 | for (WrappedDataValue wrappedDataValue : getWrappedDataValues()) { 88 | 89 | if (wrappedDataValue.getIndex() == index) { 90 | return wrappedDataValue; 91 | } 92 | 93 | } 94 | 95 | return null; 96 | } 97 | 98 | @Nullable 99 | public Object getValue(int index) { 100 | @Nullable WrappedDataValue wrappedDataValue = get(index); 101 | return (wrappedDataValue == null) ? null : wrappedDataValue.getValue(); 102 | } 103 | 104 | @Nullable 105 | public WrappedDataWatcher.Serializer getSerializer(int index) { 106 | @Nullable WrappedDataValue wrappedDataValue = get(index); 107 | return (wrappedDataValue == null) ? null : wrappedDataValue.getSerializer(); 108 | } 109 | 110 | public void remove(int index) { 111 | packet.getDataValueCollectionModifier().writeSafely(0, getWrappedDataValuesWithout(index)); 112 | } 113 | 114 | public Set getIndexes() { 115 | final Set indexes = new HashSet<>(); 116 | 117 | for (WrappedDataValue wrappedDataValue : getWrappedDataValues()) { 118 | indexes.add(wrappedDataValue.getIndex()); 119 | } 120 | 121 | return indexes; 122 | } 123 | 124 | public List toList() { 125 | return getWrappedDataValues(); 126 | } 127 | 128 | @Nonnull 129 | public Map toMap() { 130 | final Map map = new HashMap<>(); 131 | 132 | for (WrappedDataValue wrappedDataValue : getWrappedDataValues()) { 133 | map.put(wrappedDataValue.getIndex(), wrappedDataValue.getValue()); 134 | } 135 | 136 | return map; 137 | } 138 | 139 | public JSONObject toJSON() { 140 | try { 141 | return new JSONObject(toMap()); 142 | } catch (Exception ex) { 143 | return new JSONObject(); 144 | } 145 | } 146 | 147 | @Override 148 | public String toString() { 149 | return toJSON().toString(0); 150 | } 151 | 152 | } 153 | -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/expressions/datawatcher/ExprDataWatcherIndex.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.expressions.datawatcher; 2 | 3 | import ch.njol.skript.doc.Examples; 4 | import org.bukkit.event.Event; 5 | import org.eclipse.jdt.annotation.Nullable; 6 | 7 | import ch.njol.skript.Skript; 8 | import ch.njol.skript.classes.Changer.ChangeMode; 9 | import ch.njol.skript.doc.Description; 10 | import ch.njol.skript.doc.Name; 11 | import ch.njol.skript.doc.Since; 12 | import ch.njol.skript.lang.Expression; 13 | import ch.njol.skript.lang.ExpressionType; 14 | import ch.njol.skript.lang.SkriptParser.ParseResult; 15 | import ch.njol.skript.lang.util.SimpleExpression; 16 | import ch.njol.util.Kleenean; 17 | import ch.njol.util.coll.CollectionUtils; 18 | import org.jetbrains.annotations.NotNull; 19 | 20 | @Name("DataWatcher Index") 21 | @Description("Get or set a datawatcher's index. Used for metadata packet. More information at https://github.com/Anarchick/skript-packet/wiki/Data-Watcher") 22 | @Examples({ 23 | "set {_dw} to data watcher from {_packet}", 24 | "data watcher index 0 of {_dw} is set", 25 | "set data watcher index 0 of {_dw} to 64" 26 | }) 27 | @Since("2.0") 28 | 29 | public class ExprDataWatcherIndex extends SimpleExpression { 30 | 31 | private Expression indexExpr; 32 | private Expression dataWatcherExpr; 33 | 34 | static { 35 | Skript.registerExpression(ExprDataWatcherIndex.class, Object.class, ExpressionType.COMBINED, 36 | "[the] data[ ]watcher index %number% of %datawatcher%"); 37 | } 38 | 39 | @Override 40 | @SuppressWarnings("unchecked") 41 | public boolean init(Expression[] exprs, int matchedPattern, @NotNull Kleenean isDelayed, @NotNull ParseResult parseResult) { 42 | indexExpr = (Expression) exprs[0]; 43 | dataWatcherExpr = (Expression) exprs[1]; 44 | return true; 45 | } 46 | 47 | @Override 48 | protected Object @NotNull [] get(@NotNull Event event) { 49 | final Number index = indexExpr.getSingle(event); 50 | final DataWatcher dataWatcher = dataWatcherExpr.getSingle(event); 51 | 52 | if (index == null || dataWatcher == null) { 53 | return new Object[0]; 54 | } 55 | 56 | return new Object[] {dataWatcher.getValue(index.intValue())}; 57 | } 58 | 59 | @Override 60 | public Class @NotNull [] acceptChange(@NotNull ChangeMode mode) { 61 | if (mode == ChangeMode.SET || mode == ChangeMode.DELETE) { 62 | return CollectionUtils.array(Object.class); 63 | } 64 | 65 | return super.acceptChange(mode); 66 | } 67 | 68 | @Override 69 | public void change(@NotNull Event e, Object @NotNull [] delta, @NotNull ChangeMode mode) { 70 | final Number index = indexExpr.getSingle(e); 71 | final DataWatcher dataWatcher = dataWatcherExpr.getSingle(e); 72 | 73 | if (index == null || dataWatcher == null) { 74 | return; 75 | } 76 | 77 | if (mode == ChangeMode.SET) { 78 | dataWatcher.set(index.intValue(), delta[0]); 79 | } else if (mode == ChangeMode.DELETE) { 80 | dataWatcher.remove(index.intValue()); 81 | } 82 | } 83 | 84 | @Override 85 | public boolean isSingle() { 86 | return true; 87 | } 88 | 89 | @Override 90 | public @NotNull Class getReturnType() { 91 | return Object.class; 92 | } 93 | 94 | @Override 95 | public @NotNull String toString(@Nullable Event e, boolean debug) { 96 | return "datawatcher index" + indexExpr.toString(e, debug) + " of " + dataWatcherExpr.toString(e, debug); 97 | } 98 | 99 | } 100 | -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/expressions/datawatcher/ExprDataWatcherIndexes.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.expressions.datawatcher; 2 | 3 | import java.util.Iterator; 4 | 5 | import org.bukkit.event.Event; 6 | import org.eclipse.jdt.annotation.Nullable; 7 | 8 | import ch.njol.skript.Skript; 9 | import ch.njol.skript.doc.Description; 10 | import ch.njol.skript.doc.Name; 11 | import ch.njol.skript.doc.Since; 12 | import ch.njol.skript.lang.Expression; 13 | import ch.njol.skript.lang.ExpressionType; 14 | import ch.njol.skript.lang.SkriptParser.ParseResult; 15 | import ch.njol.skript.lang.util.SimpleExpression; 16 | import ch.njol.util.Kleenean; 17 | import ch.njol.util.coll.iterator.EmptyIterator; 18 | import org.jetbrains.annotations.NotNull; 19 | 20 | @Name("DataWatcher Indexes") 21 | @Description("Get all indexes of a data watcher") 22 | //@Examples("") 23 | @Since("2.0") 24 | 25 | public class ExprDataWatcherIndexes extends SimpleExpression { 26 | 27 | private Expression dataWatcherExpr; 28 | 29 | private static final String[] patterns = new String[] { 30 | "[all] data[ ]watcher (indexes|indices) of %datawatcher%", 31 | "[all] %datawatcher%'s data[ ]watcher (indexes|indices)" 32 | }; 33 | 34 | static { 35 | Skript.registerExpression(ExprDataWatcherIndexes.class, Number.class, ExpressionType.PROPERTY, patterns); 36 | } 37 | 38 | @Override 39 | @SuppressWarnings("unchecked") 40 | public boolean init(Expression[] exprs, int matchedPattern, @NotNull Kleenean isDelayed, @NotNull ParseResult parseResult) { 41 | dataWatcherExpr = (Expression) exprs[0]; 42 | return true; 43 | } 44 | 45 | @Override 46 | protected Number @NotNull [] get(@NotNull Event event) { 47 | final DataWatcher dataWatcher = dataWatcherExpr.getSingle(event); 48 | 49 | if (dataWatcher == null) { 50 | return new Number[0]; 51 | } 52 | 53 | return dataWatcher.getIndexes().toArray(Number[]::new); 54 | } 55 | 56 | @Override 57 | public Iterator iterator(@NotNull Event event) { 58 | final DataWatcher dataWatcher = dataWatcherExpr.getSingle(event); 59 | 60 | if (dataWatcher == null) { 61 | return new EmptyIterator<>(); 62 | } 63 | 64 | return dataWatcher.getIndexes().iterator(); 65 | } 66 | 67 | @Override 68 | public boolean isSingle() { 69 | return false; 70 | } 71 | 72 | @Override 73 | public @NotNull Class getReturnType() { 74 | return Number.class; 75 | } 76 | 77 | @Override 78 | public @NotNull String toString(@Nullable Event e, boolean debug) { 79 | return "all data watcher indexes of " + dataWatcherExpr.toString(e, debug); 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/expressions/datawatcher/ExprNewDataWatcher.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.expressions.datawatcher; 2 | 3 | import ch.njol.skript.Skript; 4 | import ch.njol.skript.doc.Description; 5 | import ch.njol.skript.doc.Examples; 6 | import ch.njol.skript.doc.Name; 7 | import ch.njol.skript.doc.Since; 8 | import ch.njol.skript.lang.Expression; 9 | import ch.njol.skript.lang.ExpressionType; 10 | import ch.njol.skript.lang.SkriptParser.ParseResult; 11 | import ch.njol.skript.lang.util.SimpleExpression; 12 | import ch.njol.util.Kleenean; 13 | import com.comphenix.protocol.events.PacketContainer; 14 | import org.bukkit.event.Event; 15 | import org.eclipse.jdt.annotation.Nullable; 16 | import org.jetbrains.annotations.NotNull; 17 | 18 | @Name("DataWatcher") 19 | @Description({ 20 | "Create a new datawatcher. Used for metadata packet.", 21 | "The data watcher is linked to the packet and you don't need to set the packet field to it", 22 | "More information at https://github.com/Anarchick/skript-packet/wiki/Data-Watcher" 23 | }) 24 | @Examples({ 25 | "on packet event play_server_entity_metadata:", 26 | "\tset {_dw} to new data watcher from event-packet", 27 | "\tdatawatcher index 0 of {_dw} exist" 28 | }) 29 | @Since("2.0, 2.2.0 need a packet") 30 | 31 | public class ExprNewDataWatcher extends SimpleExpression { 32 | 33 | private Expression packetExpr; 34 | private int pattern; 35 | private static final String[] patterns = new String[] { 36 | "new data[ ]watcher from %packet%" 37 | }; 38 | 39 | static { 40 | Skript.registerExpression(ExprNewDataWatcher.class, DataWatcher.class, ExpressionType.SIMPLE, patterns); 41 | } 42 | 43 | @Override 44 | @SuppressWarnings("unchecked") 45 | public boolean init(Expression[] exprs, int matchedPattern, @NotNull Kleenean isDelayed, @NotNull ParseResult parseResult) { 46 | packetExpr = (Expression) exprs[0]; 47 | pattern = matchedPattern; 48 | return true; 49 | } 50 | 51 | @Override 52 | protected DataWatcher @NotNull [] get(@NotNull Event e) { 53 | final PacketContainer packet = packetExpr.getSingle(e); 54 | 55 | if (packet == null) { 56 | return new DataWatcher[0]; 57 | } 58 | 59 | return new DataWatcher[] {new DataWatcher(packet)}; 60 | } 61 | 62 | @Override 63 | public boolean isSingle() { 64 | return true; 65 | } 66 | 67 | @Override 68 | public @NotNull Class getReturnType() { 69 | return DataWatcher.class; 70 | } 71 | 72 | @Override 73 | public @NotNull String toString(@Nullable Event e, boolean debug) { 74 | return patterns[pattern]; 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/expressions/nms/ExprNMS.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.expressions.nms; 2 | 3 | import fr.anarchick.skriptpacket.util.converters.ConverterLogic; 4 | import org.bukkit.Location; 5 | import org.bukkit.World; 6 | import org.bukkit.event.Event; 7 | import org.eclipse.jdt.annotation.Nullable; 8 | 9 | import ch.njol.skript.Skript; 10 | import ch.njol.skript.doc.Description; 11 | import ch.njol.skript.doc.Examples; 12 | import ch.njol.skript.doc.Name; 13 | import ch.njol.skript.doc.Since; 14 | import ch.njol.skript.lang.Expression; 15 | import ch.njol.skript.lang.ExpressionType; 16 | import ch.njol.skript.lang.SkriptParser.ParseResult; 17 | import ch.njol.skript.lang.util.SimpleExpression; 18 | import ch.njol.skript.registrations.EventValues; 19 | import ch.njol.util.Kleenean; 20 | import ch.njol.util.coll.CollectionUtils; 21 | import org.jetbrains.annotations.NotNull; 22 | 23 | @Name("NMS") 24 | @Description({ 25 | "Get the NMS (net.minecraft.server) from a convertible object or invert", 26 | "More information here https://github.com/Anarchick/skript-packet/wiki/ExprNMS" 27 | }) 28 | @Examples({ 29 | "set {_nms} to nms of player", 30 | "set {_bukkit} to wrap from {_nms}" 31 | }) 32 | @Since("2.0, 2.2.0 -%chunk% +%block data/material/slot/string% + wrap pattern modified") 33 | 34 | public class ExprNMS extends SimpleExpression { 35 | 36 | private Expression expr; 37 | private int pattern; 38 | private final static String[] patterns = new String[] { 39 | "NMS (of|from) %location/block/blockdata/itemtype/itemstack/material/slot/biome/entity/world/vector/string%", 40 | // fix issue https://github.com/SkriptLang/Skript/issues/6541 41 | // have to use : nms of type of pig 42 | "NMS (of|from) %entitytype%", 43 | "(convert|wrap) from nms %object%", 44 | }; 45 | private Object result = null; 46 | 47 | static { 48 | Skript.registerExpression(ExprNMS.class, Object.class, ExpressionType.COMBINED, patterns); 49 | } 50 | 51 | @Override 52 | public boolean init(Expression[] exprs, int matchedPattern, @NotNull Kleenean isDelayed, @NotNull ParseResult parser) { 53 | pattern = matchedPattern; 54 | expr = exprs[0]; 55 | return true; 56 | } 57 | 58 | @Override 59 | @Nullable 60 | protected Object @NotNull [] get(@NotNull Event e) { 61 | final Object obj = expr.getSingle(e); 62 | 63 | if (obj == null) { 64 | return new Object[0]; 65 | } 66 | 67 | switch (pattern) { 68 | case 0, 1 -> result = ConverterLogic.toNMS(obj); 69 | case 2 -> { 70 | result = ConverterLogic.toBukkit(obj); 71 | 72 | if (result instanceof Location) { 73 | @Nullable final World world = EventValues.getEventValue(e, World.class, 0); 74 | 75 | if (world != null) { 76 | ((Location) result).setWorld(world); 77 | } 78 | } 79 | 80 | } 81 | default -> { 82 | } 83 | } 84 | 85 | return CollectionUtils.array(result); 86 | } 87 | 88 | @Override 89 | public boolean isSingle() { 90 | return true; 91 | } 92 | 93 | @Override 94 | public @NotNull Class getReturnType() { 95 | return (result == null) ? Object.class : result.getClass(); 96 | } 97 | 98 | @Override 99 | public @NotNull String toString(@Nullable Event e, boolean debug) { 100 | return patterns[pattern]; 101 | } 102 | 103 | } -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/expressions/nms/ExprPair.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.expressions.nms; 2 | 3 | import org.bukkit.event.Event; 4 | import org.eclipse.jdt.annotation.Nullable; 5 | 6 | import com.btk5h.skriptmirror.ObjectWrapper; 7 | import com.mojang.datafixers.util.Pair; 8 | 9 | import ch.njol.skript.Skript; 10 | import ch.njol.skript.doc.Description; 11 | import ch.njol.skript.doc.Examples; 12 | import ch.njol.skript.doc.Name; 13 | import ch.njol.skript.doc.Since; 14 | import ch.njol.skript.lang.Expression; 15 | import ch.njol.skript.lang.ExpressionType; 16 | import ch.njol.skript.lang.SkriptParser.ParseResult; 17 | import ch.njol.skript.lang.util.SimpleExpression; 18 | import ch.njol.util.Kleenean; 19 | import fr.anarchick.skriptpacket.SkriptPacket; 20 | import org.jetbrains.annotations.NotNull; 21 | 22 | @Name("NMS Pair") 23 | @Description("Pair object A with object B uning com.mojang.datafixers.util.Pair") 24 | @Examples("set {_pair} to pair {_itemSlot} with {_item}") 25 | @Since("1.0") 26 | 27 | public class ExprPair extends SimpleExpression{ 28 | 29 | private Expression firstExpr; 30 | private Expression secondExpr; 31 | 32 | static { 33 | Skript.registerExpression(ExprPair.class, Object.class, ExpressionType.PATTERN_MATCHES_EVERYTHING, 34 | "pair %object% (with|and) %object%"); 35 | } 36 | 37 | @Override 38 | @SuppressWarnings("unchecked") 39 | public boolean init(Expression[] exprs, int matchedPattern, @NotNull Kleenean isDelayed, @NotNull ParseResult parser) { 40 | firstExpr = (Expression) exprs[0]; 41 | secondExpr = (Expression) exprs[1]; 42 | return true; 43 | } 44 | 45 | @Override 46 | @Nullable 47 | protected Object @NotNull [] get(@NotNull Event e) { 48 | final Object first = firstExpr.getSingle(e); 49 | final Object second = secondExpr.getSingle(e); 50 | 51 | if (SkriptPacket.isReflectAddon) { 52 | return new Object[] {new Pair<>(ObjectWrapper.unwrapIfNecessary(first), 53 | ObjectWrapper.unwrapIfNecessary(second) 54 | )}; 55 | } 56 | 57 | return new Object[] {new Pair<>(first, second)}; 58 | } 59 | 60 | @Override 61 | public boolean isSingle() { 62 | return true; 63 | } 64 | 65 | @Override 66 | public @NotNull Class getReturnType() { 67 | return Object.class; 68 | } 69 | 70 | @Override 71 | public @NotNull String toString(@Nullable Event e, boolean debug) { 72 | return "pair %object% with %object%"; 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/expressions/packet/ExprNewPacket.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.expressions.packet; 2 | 3 | import org.bukkit.event.Event; 4 | 5 | import com.comphenix.protocol.PacketType; 6 | import com.comphenix.protocol.ProtocolLibrary; 7 | import com.comphenix.protocol.events.PacketContainer; 8 | 9 | import ch.njol.skript.Skript; 10 | import ch.njol.skript.doc.Description; 11 | import ch.njol.skript.doc.Examples; 12 | import ch.njol.skript.doc.Name; 13 | import ch.njol.skript.doc.Since; 14 | import ch.njol.skript.lang.Expression; 15 | import ch.njol.skript.lang.ExpressionType; 16 | import ch.njol.skript.lang.SkriptParser.ParseResult; 17 | import ch.njol.skript.lang.util.SimpleExpression; 18 | import ch.njol.util.Kleenean; 19 | import org.jetbrains.annotations.NotNull; 20 | 21 | @Name("New Packet") 22 | @Description("Create a new packet from a ProtocolLib's packettype") 23 | @Examples("set {_packet} to new play_server_block_break_animation packet") 24 | @Since("1.0, 2.0 (default)") 25 | 26 | public class ExprNewPacket extends SimpleExpression { 27 | 28 | private Expression packetTypeExpr; 29 | private boolean hasDefault = false; 30 | 31 | static { 32 | Skript.registerExpression(ExprNewPacket.class, PacketContainer.class, ExpressionType.COMBINED, 33 | "new %packettype% packet [(1¦with default values)]"); 34 | } 35 | 36 | @Override 37 | @SuppressWarnings("unchecked") 38 | public boolean init(Expression[] exprs, int i, @NotNull Kleenean isDelayed, ParseResult parser) { 39 | packetTypeExpr = (Expression) exprs[0]; 40 | hasDefault = (parser.mark == 1); 41 | return true; 42 | } 43 | 44 | @Override 45 | protected PacketContainer @NotNull [] get(@NotNull Event e) { 46 | return new PacketContainer[]{ProtocolLibrary.getProtocolManager().createPacket(packetTypeExpr.getSingle(e), hasDefault)}; 47 | } 48 | 49 | @Override 50 | public boolean isSingle() { 51 | return true; 52 | } 53 | 54 | @Override 55 | public @NotNull Class getReturnType() { 56 | return PacketContainer.class; 57 | } 58 | 59 | @Override 60 | public @NotNull String toString(Event e, boolean debug) { 61 | String str = "new " + packetTypeExpr.toString(e, debug) + "packet"; 62 | 63 | if (hasDefault) { 64 | str += " with default values"; 65 | } 66 | 67 | return str; 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/expressions/packet/ExprPacketClone.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.expressions.packet; 2 | 3 | import ch.njol.skript.util.LiteralUtils; 4 | import com.comphenix.protocol.events.PacketContainer; 5 | 6 | import ch.njol.skript.doc.Description; 7 | import ch.njol.skript.doc.Examples; 8 | import ch.njol.skript.doc.Name; 9 | import ch.njol.skript.doc.Since; 10 | import ch.njol.skript.expressions.base.SimplePropertyExpression; 11 | import ch.njol.skript.lang.Expression; 12 | import ch.njol.skript.lang.SkriptParser.ParseResult; 13 | import ch.njol.util.Kleenean; 14 | import org.jetbrains.annotations.NotNull; 15 | 16 | @Name("Clone of Packet") 17 | @Description("Get a full copy (=deep) or a fast copy (=shallow) of a packet") 18 | @Examples("set {_copy} to deep clone of event-packet") 19 | @Since("1.1") 20 | 21 | public class ExprPacketClone extends SimplePropertyExpression{ 22 | 23 | private int mark; 24 | private String pattern; 25 | 26 | static { 27 | register(ExprPacketClone.class, PacketContainer.class, 28 | "[packet] (0¦deep|1¦shallow) (clone|copy)", "packet"); 29 | } 30 | 31 | @Override 32 | public boolean init(Expression @NotNull [] exprs, int matchedPattern, @NotNull Kleenean isDelayed, ParseResult parser) { 33 | mark = parser.mark; 34 | pattern = parser.expr; 35 | 36 | if (LiteralUtils.hasUnparsedLiteral(exprs[0])) { 37 | setExpr(LiteralUtils.defendExpression(exprs[0])); 38 | return LiteralUtils.canInitSafely(getExpr()); 39 | } 40 | 41 | setExpr((Expression) exprs[0]); 42 | return true; 43 | } 44 | 45 | @Override 46 | public PacketContainer convert(PacketContainer packet) { 47 | return (mark == 0) ? packet.deepClone() : packet.shallowClone(); 48 | } 49 | 50 | @Override 51 | public @NotNull Class getReturnType() { 52 | return PacketContainer.class; 53 | } 54 | 55 | @Override 56 | protected @NotNull String getPropertyName() { 57 | return pattern; 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/expressions/packet/ExprPacketField.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.expressions.packet; 2 | 3 | import ch.njol.skript.Skript; 4 | import ch.njol.skript.classes.Changer.ChangeMode; 5 | import ch.njol.skript.doc.Description; 6 | import ch.njol.skript.doc.Examples; 7 | import ch.njol.skript.doc.Name; 8 | import ch.njol.skript.doc.Since; 9 | import ch.njol.skript.lang.Expression; 10 | import ch.njol.skript.lang.ExpressionType; 11 | import ch.njol.skript.lang.Literal; 12 | import ch.njol.skript.lang.SkriptParser.ParseResult; 13 | import ch.njol.skript.lang.util.SimpleExpression; 14 | import ch.njol.util.Kleenean; 15 | import com.comphenix.protocol.events.PacketContainer; 16 | import com.comphenix.protocol.reflect.StructureModifier; 17 | import fr.anarchick.skriptpacket.SkriptPacket; 18 | import fr.anarchick.skriptpacket.packets.BukkitPacketEvent; 19 | import fr.anarchick.skriptpacket.packets.PacketManager; 20 | import fr.anarchick.skriptpacket.util.ArrayUtils; 21 | import fr.anarchick.skriptpacket.util.converters.ConverterLogic; 22 | import org.bukkit.event.Event; 23 | import org.eclipse.jdt.annotation.Nullable; 24 | import org.jetbrains.annotations.NotNull; 25 | 26 | @Name("Packet Field") 27 | @Description({ 28 | "Get or set a packet field", 29 | "Field id start from 0 and increase by 1 for each existent field", 30 | "This expression has an auto-converter. More information on the wiki https://github.com/Anarchick/skript-packet/wiki" 31 | }) 32 | @Examples({ 33 | "set field 0 of packet {_packet} to 5", 34 | "set field 1 of packet {_packet} to id of player" 35 | }) 36 | @Since("1.0, 1.2 (optional packet), 2.2.0(wrap option)") 37 | 38 | public class ExprPacketField extends SimpleExpression { 39 | 40 | private Expression indexExpr; 41 | private Expression packetExpr; 42 | private boolean shouldWrap; 43 | 44 | private boolean isSingle = true; 45 | 46 | static { 47 | Skript.registerExpression(ExprPacketField.class, Object.class, ExpressionType.COMBINED, 48 | "[packet] field %number% [of %-packet%]", 49 | "(convert|wrap) [packet] field %number% [of %-packet%]" 50 | ); 51 | } 52 | 53 | @Override 54 | @SuppressWarnings("unchecked") 55 | public boolean init(Expression[] exprs, int matchedPattern, @NotNull Kleenean isDelayed, @NotNull ParseResult parser) { 56 | shouldWrap = ( matchedPattern == 1 ); 57 | indexExpr = (Expression) exprs[0]; 58 | 59 | if (indexExpr instanceof Literal litIndex) { 60 | int index = litIndex.getSingle().intValue(); 61 | 62 | if (index < 0) { 63 | Skript.error("Indexes starts from 0"); 64 | return false; 65 | } 66 | 67 | } 68 | 69 | if (exprs[1] != null) { 70 | packetExpr = (Expression) exprs[1]; 71 | return true; 72 | } 73 | // ScriptLoader must be replaced with getParser() with Skript 2.6+ 74 | return SkriptPacket.isCurrentEvent(this,"A field expression can only be used with a packet", BukkitPacketEvent.class); 75 | } 76 | 77 | @Override 78 | @Nullable 79 | protected Object @NotNull [] get(@NotNull Event e) { 80 | final PacketContainer packet; 81 | 82 | if (packetExpr == null) { 83 | packet = ((BukkitPacketEvent) e).getPacket(); 84 | } else { 85 | packet = packetExpr.getSingle(e); 86 | } 87 | 88 | final Number index = indexExpr.getSingle(e); 89 | 90 | if ((packet != null) && (index != null)) { 91 | int i = index.intValue(); 92 | final StructureModifier modifier = packet.getModifier(); 93 | int size = modifier.size(); 94 | 95 | if ((i < 0 ) || (i >= size)) { 96 | Skript.error("Available indexes for the packketype '"+PacketManager.getPacketName(packet.getType())+"' are from 0 to "+(size -1)); 97 | return new Object[0]; 98 | } 99 | 100 | final Object field = modifier.readSafely(i); 101 | 102 | if (field == null) { 103 | return new Object[0]; 104 | } 105 | 106 | if (field.getClass().isArray()) { 107 | isSingle = false; 108 | return ArrayUtils.unknownToObject(field); 109 | } 110 | 111 | Object obj = ConverterLogic.toObject(field); 112 | 113 | if (shouldWrap) { 114 | obj = ConverterLogic.toBukkit(obj); 115 | } 116 | 117 | return new Object[] {obj}; 118 | } 119 | 120 | return new Object[0]; 121 | } 122 | 123 | @Override 124 | @Nullable 125 | public Class @NotNull [] acceptChange(final @NotNull ChangeMode mode) { 126 | if ( mode == ChangeMode.SET ) { 127 | return new Class[] {Number[].class, Object[].class}; 128 | } 129 | 130 | return new Class[0]; 131 | } 132 | 133 | @Override 134 | public void change(@NotNull Event e, Object @NotNull [] delta, @NotNull ChangeMode mode) { 135 | if (mode != ChangeMode.SET) { 136 | return; 137 | } 138 | 139 | final PacketContainer packet; 140 | 141 | if (packetExpr == null) { 142 | packet = ((BukkitPacketEvent) e).getPacket(); 143 | } else { 144 | packet = packetExpr.getSingle(e); 145 | } 146 | 147 | final Number index = indexExpr.getSingle(e); 148 | 149 | if ((packet != null) && (index != null)) { 150 | PacketManager.setField(packet, index.intValue(), ArrayUtils.toArray(delta)); 151 | } 152 | } 153 | 154 | @Override 155 | public boolean isSingle() { 156 | return isSingle; 157 | } 158 | 159 | @Override 160 | public @NotNull Class getReturnType() { 161 | return Object.class; 162 | } 163 | 164 | @Override 165 | public @NotNull String toString(@Nullable Event e, boolean debug) { 166 | 167 | if (shouldWrap) { 168 | return "(convert|wrap) [packet] field %number% [of %-packet%]"; 169 | } else { 170 | return "[packet] field %number% [of %-packet%]"; 171 | } 172 | 173 | } 174 | 175 | } -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/expressions/packet/ExprPacketFields.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.expressions.packet; 2 | 3 | import fr.anarchick.skriptpacket.util.converters.ConverterLogic; 4 | import org.bukkit.event.Event; 5 | import org.eclipse.jdt.annotation.Nullable; 6 | 7 | import com.comphenix.protocol.events.PacketContainer; 8 | import com.comphenix.protocol.reflect.StructureModifier; 9 | 10 | import ch.njol.skript.Skript; 11 | import ch.njol.skript.doc.Description; 12 | import ch.njol.skript.doc.Examples; 13 | import ch.njol.skript.doc.Name; 14 | import ch.njol.skript.doc.Since; 15 | import ch.njol.skript.lang.Expression; 16 | import ch.njol.skript.lang.ExpressionType; 17 | import ch.njol.skript.lang.SkriptParser.ParseResult; 18 | import ch.njol.skript.lang.util.SimpleExpression; 19 | import ch.njol.util.Kleenean; 20 | import fr.anarchick.skriptpacket.SkriptPacket; 21 | import fr.anarchick.skriptpacket.packets.BukkitPacketEvent; 22 | import fr.anarchick.skriptpacket.util.ArrayUtils; 23 | import org.jetbrains.annotations.NotNull; 24 | 25 | @Name("Packet Fields") 26 | @Description("Get all packet fields, can't be set") 27 | @Examples("set {_fields::*} to all fields of event-packet") 28 | @Since("1.0, 2.2.0(wrap option)") 29 | 30 | public class ExprPacketFields extends SimpleExpression { 31 | 32 | private static Expression packetExpr; 33 | private boolean shouldWrap; 34 | 35 | static { 36 | Skript.registerExpression(ExprPacketFields.class, Object.class, ExpressionType.SIMPLE, 37 | "[all] [packet] fields [of %-packet%]", 38 | "[all] (convert|wrap) [packet] fields [of %-packet%]"); 39 | } 40 | 41 | @Override 42 | @SuppressWarnings("unchecked") 43 | public boolean init(Expression[] exprs, int matchedPattern, @NotNull Kleenean isDelayed, @NotNull ParseResult parser) { 44 | shouldWrap = ( matchedPattern == 1 ); 45 | 46 | if (exprs[0] != null) { 47 | packetExpr = (Expression) exprs[0]; 48 | return true; 49 | } 50 | // ScriptLoader must be replaced with getParser() with Skript 2.6+ 51 | return SkriptPacket.isCurrentEvent(this, "A field expression can only be used with a packet", BukkitPacketEvent.class); 52 | } 53 | 54 | @Override 55 | @Nullable 56 | protected Object @NotNull [] get(@NotNull Event e) { 57 | final PacketContainer packet; 58 | 59 | if (packetExpr == null) { 60 | packet = ((BukkitPacketEvent) e).getPacket(); 61 | } else { 62 | packet = packetExpr.getSingle(e); 63 | } 64 | 65 | if (packet != null) { 66 | final StructureModifier modifier = packet.getModifier(); 67 | int size = modifier.getValues().size(); 68 | final Object[] values = new Object[size]; 69 | 70 | for (int i = 0; i < size ; i++) { 71 | final Object field = modifier.readSafely(i); 72 | 73 | if (field == null) { 74 | values[i] = null; 75 | } else if (field.getClass().isArray()) { 76 | values[i] = ArrayUtils.unknownToObject(field); 77 | } else { 78 | values[i] = ConverterLogic.toObject(field); 79 | } 80 | 81 | if (shouldWrap) { 82 | values[i] = ConverterLogic.toBukkit(values[i]); 83 | } 84 | 85 | } 86 | 87 | return values; 88 | } 89 | 90 | return new Object[0]; 91 | } 92 | 93 | @Override 94 | public boolean isSingle() { 95 | return false; 96 | } 97 | 98 | @Override 99 | public @NotNull Class getReturnType() { 100 | return Object.class; 101 | } 102 | 103 | @Override 104 | public @NotNull String toString(@Nullable Event e, boolean debug) { 105 | 106 | if (shouldWrap) { 107 | return "all wrap packet fields of " + packetExpr.toString(e, debug); 108 | } else { 109 | return "all packet fields of " + packetExpr.toString(e, debug); 110 | } 111 | 112 | } 113 | 114 | } -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/expressions/packet/ExprPacketFieldsClasses.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.expressions.packet; 2 | 3 | import fr.anarchick.skriptpacket.util.converters.Converter; 4 | import fr.anarchick.skriptpacket.util.converters.ConverterLogic; 5 | import fr.anarchick.skriptpacket.util.converters.ConverterToUtility; 6 | import org.bukkit.event.Event; 7 | import org.eclipse.jdt.annotation.Nullable; 8 | 9 | import com.comphenix.protocol.events.PacketContainer; 10 | import com.comphenix.protocol.reflect.StructureModifier; 11 | 12 | import ch.njol.skript.Skript; 13 | import ch.njol.skript.doc.Description; 14 | import ch.njol.skript.doc.Examples; 15 | import ch.njol.skript.doc.Name; 16 | import ch.njol.skript.doc.Since; 17 | import ch.njol.skript.lang.Expression; 18 | import ch.njol.skript.lang.ExpressionType; 19 | import ch.njol.skript.lang.SkriptParser.ParseResult; 20 | import ch.njol.skript.lang.util.SimpleExpression; 21 | import ch.njol.util.Kleenean; 22 | import fr.anarchick.skriptpacket.SkriptPacket; 23 | import fr.anarchick.skriptpacket.packets.BukkitPacketEvent; 24 | import org.jetbrains.annotations.NotNull; 25 | 26 | @Name("Packet Fields Classes") 27 | @Description({ 28 | "Get all fields's classes of a packet.", 29 | "This is not intended to be use on your final code,", 30 | "it's only to help you to know what is inside a packet" 31 | }) 32 | @Examples({ 33 | "set {_packet} to new play_server_block_break_animation packet", 34 | "broadcast \"%all wrap fields classes of packet {_packet}%\"" 35 | }) 36 | @Since("1.0, 2.2.0(wrap option)") 37 | 38 | public class ExprPacketFieldsClasses extends SimpleExpression { 39 | 40 | private static Expression packetExpr; 41 | private boolean shouldWrap; 42 | 43 | static { 44 | Skript.registerExpression(ExprPacketFieldsClasses.class, String.class, ExpressionType.PROPERTY, 45 | "[all] [packet] fields class[es] [of %-packet%]", 46 | "[all] (convert|wrap) [packet] fields class[es] [of %-packet%]"); 47 | } 48 | 49 | @Override 50 | @SuppressWarnings("unchecked") 51 | public boolean init(Expression[] exprs, int matchedPattern, @NotNull Kleenean isDelayed, @NotNull ParseResult parser) { 52 | shouldWrap = ( matchedPattern == 1 ); 53 | 54 | if (exprs[0] != null) { 55 | packetExpr = (Expression) exprs[0]; 56 | return true; 57 | } 58 | // ScriptLoader must be replaced with getParser() with Skript 2.6+ 59 | return SkriptPacket.isCurrentEvent(this, "A field expression can only be used with a packet", BukkitPacketEvent.class); 60 | } 61 | 62 | @Override 63 | protected String @NotNull [] get(@NotNull Event e) { 64 | final PacketContainer packet; 65 | 66 | if (packetExpr == null) { 67 | packet = ((BukkitPacketEvent) e).getPacket(); 68 | } else { 69 | packet = packetExpr.getSingle(e); 70 | } 71 | 72 | if (packet == null) { 73 | return new String[0]; 74 | } 75 | 76 | final StructureModifier modifier = packet.getModifier(); 77 | final String[] classNames = new String[modifier.size()]; 78 | 79 | for (int i = 0; i < classNames.length ; i++) { 80 | Class fieldClass = modifier.getField(i).getType(); 81 | 82 | if (shouldWrap) { 83 | final Converter converter = ConverterLogic.getConverterToBukkit(fieldClass); 84 | 85 | if (ConverterToUtility.HIMSELF != converter 86 | && converter.getOutputType() != Object.class ) { 87 | fieldClass = converter.getOutputType(); 88 | } 89 | 90 | } 91 | 92 | classNames[i] = fieldClass.getName(); 93 | } 94 | return classNames; 95 | } 96 | 97 | @Override 98 | public boolean isSingle() { 99 | return false; 100 | } 101 | 102 | @Override 103 | public @NotNull Class getReturnType() { 104 | return String.class; 105 | } 106 | 107 | @Override 108 | public @NotNull String toString(@Nullable Event e, boolean debug) { 109 | 110 | if (shouldWrap) { 111 | return "all wrap packets fields classes of " + packetExpr.toString(e, debug); 112 | } else { 113 | return "all packets fields classes of " + packetExpr.toString(e, debug); 114 | } 115 | 116 | } 117 | 118 | } -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/expressions/packet/ExprPacketMeta.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.expressions.packet; 2 | 3 | import java.util.Optional; 4 | 5 | import org.bukkit.event.Event; 6 | import org.eclipse.jdt.annotation.Nullable; 7 | 8 | import com.comphenix.protocol.events.PacketContainer; 9 | 10 | import ch.njol.skript.Skript; 11 | import ch.njol.skript.classes.Changer.ChangeMode; 12 | import ch.njol.skript.doc.Description; 13 | import ch.njol.skript.doc.Examples; 14 | import ch.njol.skript.doc.Name; 15 | import ch.njol.skript.doc.Since; 16 | import ch.njol.skript.lang.Expression; 17 | import ch.njol.skript.lang.ExpressionType; 18 | import ch.njol.skript.lang.SkriptParser.ParseResult; 19 | import ch.njol.skript.lang.util.SimpleExpression; 20 | import ch.njol.util.Kleenean; 21 | import org.jetbrains.annotations.NotNull; 22 | 23 | @Name("Packet Meta") 24 | @Description({ 25 | "Get or set a packet meta", 26 | "Meta are extra-datas that can be added to a packet" 27 | }) 28 | @Examples({ 29 | "set meta \"meta_name\" of packet {_packet} to (1, 2 and 3)", 30 | "set {_meta::*} to meta \"meta_name\" of packet {_packet}" 31 | }) 32 | @Since("1.1") 33 | 34 | public class ExprPacketMeta extends SimpleExpression { 35 | 36 | private Expression metaExpr; 37 | private Expression packetExpr; 38 | 39 | static { 40 | Skript.registerExpression(ExprPacketMeta.class, Object.class, ExpressionType.COMBINED, 41 | "[the] meta %string% of [packet] %packet%"); 42 | } 43 | 44 | @Override 45 | @SuppressWarnings("unchecked") 46 | public boolean init(Expression[] exprs, int matchedPattern, @NotNull Kleenean isDelayed, @NotNull ParseResult parser) { 47 | metaExpr = (Expression) exprs[0]; 48 | packetExpr = (Expression) exprs[1]; 49 | return true; 50 | } 51 | 52 | @Override 53 | protected Object @NotNull [] get(@NotNull Event e) { 54 | final String metaID = metaExpr.getSingle(e); 55 | final PacketContainer packet = packetExpr.getSingle(e); 56 | 57 | if ((packet != null) && (metaID != null)) { 58 | final Optional meta = packet.getMeta(metaID); 59 | 60 | if (meta.isPresent()) { 61 | return (@Nullable Object[]) meta.get(); 62 | } 63 | 64 | } 65 | 66 | return new Object[0]; 67 | } 68 | 69 | @Override 70 | public Class @NotNull [] acceptChange(final @NotNull ChangeMode mode) { 71 | return switch (mode) { 72 | case SET, DELETE, RESET -> new Class[]{Object[].class}; 73 | default -> super.acceptChange(mode); 74 | }; 75 | } 76 | 77 | @Override 78 | public void change(@NotNull Event e, Object @NotNull [] delta, @NotNull ChangeMode mode){ 79 | final PacketContainer packet = packetExpr.getSingle(e); 80 | final String metaID = metaExpr.getSingle(e); 81 | 82 | if ((packet != null) && (metaID != null)) { 83 | 84 | if (mode == ChangeMode.SET) { 85 | packet.setMeta(metaID, delta); 86 | } else if (mode == ChangeMode.DELETE || mode == ChangeMode.RESET){ 87 | packet.removeMeta(metaID); 88 | } 89 | 90 | } 91 | 92 | } 93 | 94 | @Override 95 | public boolean isSingle() { 96 | return false; 97 | } 98 | 99 | @Override 100 | public @NotNull Class getReturnType() { 101 | return Object.class; 102 | } 103 | 104 | @Override 105 | public @NotNull String toString(@Nullable Event e, boolean debug) { 106 | return "[the] meta %string% of [packet] %packet%"; 107 | } 108 | 109 | } -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/expressions/packet/ExprPacketType.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.expressions.packet; 2 | 3 | import com.comphenix.protocol.PacketType; 4 | import com.comphenix.protocol.events.PacketContainer; 5 | 6 | import ch.njol.skript.doc.Description; 7 | import ch.njol.skript.doc.Examples; 8 | import ch.njol.skript.doc.Name; 9 | import ch.njol.skript.doc.Since; 10 | import ch.njol.skript.expressions.base.SimplePropertyExpression; 11 | import org.jetbrains.annotations.NotNull; 12 | 13 | @Name("Packettype") 14 | @Description("Return the packet type of a %packet%") 15 | @Examples("set {_packettype} to packetttype of event-packet") 16 | @Since("1.1") 17 | 18 | public class ExprPacketType extends SimplePropertyExpression{ 19 | 20 | static { 21 | register(ExprPacketType.class, PacketType.class, "packettype", "packet"); 22 | } 23 | 24 | @Override 25 | public PacketType convert(final PacketContainer packet) { 26 | return packet.getType(); 27 | } 28 | 29 | @Override 30 | public @NotNull Class getReturnType() { 31 | return PacketType.class; 32 | } 33 | 34 | @Override 35 | protected @NotNull String getPropertyName() { 36 | return "packettype of %packet%"; 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/expressions/utility/ExprBukkitMaterial.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.expressions.utility; 2 | 3 | import ch.njol.skript.doc.Description; 4 | import ch.njol.skript.doc.Examples; 5 | import ch.njol.skript.doc.Name; 6 | import ch.njol.skript.doc.Since; 7 | import ch.njol.skript.expressions.base.SimplePropertyExpression; 8 | import fr.anarchick.skriptpacket.util.converters.ConverterToBukkit; 9 | import org.bukkit.Material; 10 | import org.eclipse.jdt.annotation.Nullable; 11 | import org.jetbrains.annotations.NotNull; 12 | 13 | @Name("Bukkit Material") 14 | @Description("Get the Material of an item or block") 15 | @Examples({ 16 | "broadcast material of tool", 17 | "broadcast material of (1 of stone)", 18 | "broadcast {_block}'s material", 19 | "broadcast material of \"STONE\"", 20 | }) 21 | @Since("2.2.0") 22 | 23 | public class ExprBukkitMaterial extends SimplePropertyExpression { 24 | 25 | static { 26 | register(ExprBukkitMaterial.class, Material.class, 27 | "[Bukkit] Material", "itemstack/itemtype/block/blockdata/string" 28 | ); 29 | } 30 | 31 | @Override 32 | public @Nullable Material convert(Object from) { 33 | return (Material) ConverterToBukkit.RELATED_TO_BUKKIT_MATERIAL.convert(from); 34 | } 35 | 36 | @Override 37 | protected @NotNull String getPropertyName() { 38 | return "Bukkit Material"; 39 | } 40 | 41 | @Override 42 | public @NotNull Class getReturnType() { 43 | return Material.class; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/expressions/utility/ExprEntityFromID.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.expressions.utility; 2 | 3 | import org.bukkit.World; 4 | import org.bukkit.entity.Entity; 5 | import org.bukkit.event.Event; 6 | import org.eclipse.jdt.annotation.Nullable; 7 | 8 | import com.comphenix.protocol.ProtocolLibrary; 9 | 10 | 11 | import ch.njol.skript.Skript; 12 | import ch.njol.skript.doc.Description; 13 | import ch.njol.skript.doc.Examples; 14 | import ch.njol.skript.doc.Name; 15 | import ch.njol.skript.doc.Since; 16 | import ch.njol.skript.lang.Expression; 17 | import ch.njol.skript.lang.ExpressionType; 18 | import ch.njol.skript.lang.SkriptParser.ParseResult; 19 | import ch.njol.skript.lang.util.SimpleExpression; 20 | import ch.njol.util.Kleenean; 21 | import ch.njol.util.coll.CollectionUtils; 22 | import org.jetbrains.annotations.NotNull; 23 | 24 | @Name("Entity From ID") 25 | @Description("Get the entity related to his ID in specified world") 26 | @Examples("set {_entity} to entity from id 39 in world of player") 27 | @Since("1.1") 28 | 29 | public class ExprEntityFromID extends SimpleExpression { 30 | 31 | private Expression idExpr; 32 | private Expression worldExpr; 33 | 34 | static { 35 | Skript.registerExpression(ExprEntityFromID.class, Entity.class, ExpressionType.COMBINED, 36 | "entity from id %number% in %world%"); 37 | } 38 | 39 | @Override 40 | @SuppressWarnings("unchecked") 41 | public boolean init(Expression[] exprs, int matchedPattern, @NotNull Kleenean isDelayed, @NotNull ParseResult parser) { 42 | idExpr = (Expression) exprs[0]; 43 | worldExpr = (Expression) exprs[1]; 44 | return true; 45 | } 46 | 47 | @Override 48 | @Nullable 49 | protected Entity @NotNull [] get(@NotNull Event e) { 50 | final Number id = idExpr.getSingle(e); 51 | final World world = worldExpr.getSingle(e); 52 | 53 | if (id == null || world == null) { 54 | return new Entity[0]; 55 | } 56 | 57 | return CollectionUtils.array(ProtocolLibrary.getProtocolManager().getEntityFromID(world, id.intValue())); 58 | } 59 | 60 | @Override 61 | public boolean isSingle() { 62 | return true; 63 | } 64 | 65 | @Override 66 | public @NotNull Class getReturnType() { 67 | return Entity.class; 68 | } 69 | 70 | @Override 71 | public @NotNull String toString(@Nullable Event e, boolean debug) { 72 | return "entity from id %number% in %world%"; 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/expressions/utility/ExprEntityID.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.expressions.utility; 2 | 3 | import org.bukkit.entity.Entity; 4 | 5 | import ch.njol.skript.doc.Description; 6 | import ch.njol.skript.doc.Examples; 7 | import ch.njol.skript.doc.Name; 8 | import ch.njol.skript.doc.Since; 9 | import ch.njol.skript.expressions.base.SimplePropertyExpression; 10 | import org.jetbrains.annotations.NotNull; 11 | 12 | @Name("ID of Entity") 13 | @Description("Get the ID of an entity. This method return a number and not the UUID") 14 | @Examples({"broadcast \"%entity id of player%\""}) 15 | @Since("1.0") 16 | 17 | public class ExprEntityID extends SimplePropertyExpression{ 18 | 19 | static { 20 | register(ExprEntityID.class, Number.class, "[entity] id", "entity"); 21 | } 22 | 23 | @Override 24 | public Number convert(Entity ent) { 25 | return ent.getEntityId(); 26 | } 27 | 28 | @Override 29 | public @NotNull Class getReturnType() { 30 | return Number.class; 31 | } 32 | 33 | @Override 34 | protected @NotNull String getPropertyName() { 35 | return "entity id"; 36 | } 37 | 38 | } -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/expressions/utility/ExprEnum.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.expressions.utility; 2 | 3 | import java.io.File; 4 | import java.net.URL; 5 | import java.nio.file.Paths; 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | import java.util.jar.JarFile; 9 | import java.util.zip.ZipEntry; 10 | 11 | import org.bukkit.event.Event; 12 | import org.eclipse.jdt.annotation.Nullable; 13 | 14 | import com.comphenix.protocol.utility.MinecraftReflection; 15 | 16 | import ch.njol.skript.Skript; 17 | import ch.njol.skript.doc.Description; 18 | import ch.njol.skript.doc.Examples; 19 | import ch.njol.skript.doc.Name; 20 | import ch.njol.skript.doc.Since; 21 | import ch.njol.skript.lang.Expression; 22 | import ch.njol.skript.lang.ExpressionType; 23 | import ch.njol.skript.lang.SkriptParser.ParseResult; 24 | import ch.njol.skript.lang.util.SimpleExpression; 25 | import ch.njol.util.Kleenean; 26 | import ch.njol.util.coll.CollectionUtils; 27 | import fr.anarchick.skriptpacket.util.Utils; 28 | import org.jetbrains.annotations.NotNull; 29 | 30 | @Name("Enum") 31 | @Description("Get an Enum from a class") 32 | @Examples({ 33 | "set {_enum} to enum \"GAME_INFO\" from class \"net.minecraft.server.v1_16_R3.ChatMessageType\"", 34 | "set {_enum} to enum \"GAME_INFO\" from nms class \"ChatMessageType\"" 35 | }) 36 | @Since("1.2") 37 | //!send enum "HEAD" from nms class "world.entity.EquipmentSlot" 38 | public class ExprEnum extends SimpleExpression { 39 | 40 | private Expression enumExpr; 41 | private Expression classExpr; 42 | private Expression objExpr; 43 | private int pattern; 44 | private static final String[] patterns = new String[] { 45 | "enum %string% from class %string%", 46 | "enum %string% from nms class %string%", 47 | "enum %string% from class of %object%" 48 | }; 49 | private static final List packages = new ArrayList<>(); 50 | 51 | static { 52 | Skript.registerExpression(ExprEnum.class, Object.class, ExpressionType.COMBINED, patterns); 53 | try { 54 | final Class minecraftVersionClass = MinecraftReflection.getMinecraftClass("CrashReport"); 55 | // If you use PAPER , it returns the file from '/cache/patched_1.17.1.jar' 56 | final URL url = minecraftVersionClass.getProtectionDomain().getCodeSource().getLocation(); 57 | final File file = Paths.get(url.toURI()).toFile(); 58 | final JarFile jar = new JarFile(file); 59 | jar.stream() 60 | .map(ZipEntry::getName) 61 | // If you use PAPER, a lot of more packages are included 62 | .filter(name -> (name.startsWith("net/minecraft") && name.endsWith(".class"))) 63 | .map(name -> name 64 | .substring(0, name.lastIndexOf('/')) 65 | .replace('/', '.') 66 | .replace("net.minecraft.", "") 67 | ) 68 | .distinct() 69 | .forEach(packages::add); 70 | jar.close(); 71 | } catch (Exception e) { 72 | e.printStackTrace(); 73 | } 74 | 75 | } 76 | 77 | @Override 78 | @SuppressWarnings("unchecked") 79 | public boolean init(Expression[] exprs, int matchedPattern, @NotNull Kleenean isDelayed, @NotNull ParseResult parser) { 80 | pattern = matchedPattern; 81 | enumExpr = (Expression) exprs[0]; 82 | 83 | if (pattern <= 1) { 84 | classExpr = (Expression) exprs[1]; 85 | } else { 86 | objExpr = (Expression) exprs[1]; 87 | } 88 | 89 | return true; 90 | } 91 | 92 | @Override 93 | protected Object @NotNull [] get(@NotNull Event e) { 94 | String targetClassName = ""; 95 | 96 | if (pattern <= 1) { 97 | targetClassName = classExpr.getSingle(e); 98 | 99 | if (targetClassName == null || targetClassName.isEmpty()) { 100 | return new Object[0]; 101 | } 102 | 103 | } 104 | 105 | Class clazz = null; 106 | 107 | switch (pattern) { 108 | case 0: 109 | try { 110 | clazz = Class.forName(targetClassName); 111 | } catch (ClassNotFoundException e1) { 112 | Skript.error("Failed to find class '" + targetClassName + "'"); 113 | } 114 | break; 115 | case 1: 116 | final String className = targetClassName.replaceFirst("net\\.minecraft", ""); 117 | final String[] aliases = className.split("\\."); 118 | final String alias = aliases[aliases.length -1]; 119 | 120 | try { 121 | clazz = MinecraftReflection.getMinecraftClass(className, alias); 122 | } catch (RuntimeException e1) { 123 | 124 | for (String str : packages) { 125 | String path = str+"."+alias; 126 | 127 | try { 128 | clazz = MinecraftReflection.getMinecraftClass(path); 129 | 130 | if (clazz != null) { 131 | Skript.error("You should replace '"+targetClassName+"' by '"+path+"' for better performances"); 132 | break; 133 | } 134 | 135 | } catch (RuntimeException ignored) {} 136 | 137 | } 138 | } 139 | 140 | if (clazz == null) { 141 | Skript.error("Failed to find NMS class '" + className + "'"); 142 | } 143 | 144 | break; 145 | case 2: 146 | final Object obj = objExpr.getSingle(e); 147 | 148 | if (obj != null) { 149 | clazz = obj.getClass(); 150 | } 151 | 152 | break; 153 | default: 154 | break; 155 | } 156 | 157 | final String enumStr = enumExpr.getSingle(e); 158 | return CollectionUtils.array(Utils.getEnum(clazz, enumStr)); 159 | } 160 | 161 | @Override 162 | public boolean isSingle() { 163 | return true; 164 | } 165 | 166 | @Override 167 | public @NotNull Class getReturnType() { 168 | return Object.class; 169 | } 170 | 171 | @Override 172 | public @NotNull String toString(@Nullable Event e, boolean debug) { 173 | return patterns[pattern]; 174 | } 175 | 176 | } 177 | -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/expressions/utility/ExprItemFromMaterial.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.expressions.utility; 2 | 3 | import ch.njol.skript.Skript; 4 | import ch.njol.skript.doc.Description; 5 | import ch.njol.skript.doc.Examples; 6 | import ch.njol.skript.doc.Name; 7 | import ch.njol.skript.doc.Since; 8 | import ch.njol.skript.lang.Expression; 9 | import ch.njol.skript.lang.ExpressionType; 10 | import ch.njol.skript.lang.SkriptParser.ParseResult; 11 | import ch.njol.skript.lang.util.SimpleExpression; 12 | import ch.njol.util.Kleenean; 13 | import org.bukkit.Material; 14 | import org.bukkit.event.Event; 15 | import org.bukkit.inventory.ItemStack; 16 | import org.eclipse.jdt.annotation.Nullable; 17 | import org.jetbrains.annotations.NotNull; 18 | 19 | @Name("Item from Material") 20 | @Description("Get the ItemStack from a Material") 21 | @Examples({ 22 | "give 1 of item from {_material} to player" 23 | }) 24 | @Since("2.2.0") 25 | 26 | public class ExprItemFromMaterial extends SimpleExpression { 27 | 28 | private Expression exprMaterial; 29 | private Expression exprAmount; 30 | 31 | static { 32 | Skript.registerExpression(ExprItemFromMaterial.class, ItemStack.class, ExpressionType.COMBINED, 33 | "%integer% of item[s] from %material%"); 34 | } 35 | 36 | @Override 37 | public boolean init(Expression[] exprs, int matchedPattern, @NotNull Kleenean isDelayed, @NotNull ParseResult parser) { 38 | exprAmount = (Expression) exprs[0]; 39 | exprMaterial = (Expression) exprs[1]; 40 | return true; 41 | } 42 | 43 | @Override 44 | @Nullable 45 | protected ItemStack @NotNull [] get(@NotNull Event e) { 46 | if (exprMaterial == null || exprAmount == null) { 47 | return new ItemStack[0]; 48 | } 49 | 50 | final Material material = exprMaterial.getSingle(e); 51 | final int amount = exprAmount.getSingle(e).intValue(); 52 | return new ItemStack[] { new ItemStack(material, amount) }; 53 | } 54 | 55 | @Override 56 | public boolean isSingle() { 57 | return true; 58 | } 59 | 60 | @Override 61 | public @NotNull Class getReturnType() { 62 | return ItemStack.class; 63 | } 64 | 65 | @Override 66 | public @NotNull String toString(@Nullable Event e, boolean debug) { 67 | return exprAmount.getSingle(e) + " of item from " + exprMaterial.getSingle(e); 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/expressions/utility/ExprNumberAs.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.expressions.utility; 2 | 3 | import fr.anarchick.skriptpacket.util.NumberEnums; 4 | import org.bukkit.event.Event; 5 | import org.eclipse.jdt.annotation.Nullable; 6 | 7 | import ch.njol.skript.Skript; 8 | import ch.njol.skript.doc.Description; 9 | import ch.njol.skript.doc.Examples; 10 | import ch.njol.skript.doc.Name; 11 | import ch.njol.skript.doc.Since; 12 | import ch.njol.skript.lang.Expression; 13 | import ch.njol.skript.lang.ExpressionType; 14 | import ch.njol.skript.lang.SkriptParser.ParseResult; 15 | import ch.njol.skript.lang.util.SimpleExpression; 16 | import ch.njol.util.Kleenean; 17 | import ch.njol.util.coll.CollectionUtils; 18 | import org.jetbrains.annotations.NotNull; 19 | 20 | import java.util.Objects; 21 | 22 | @Name("Number As") 23 | @Description("Convert a %number% to int/float/long/double/short/byte") 24 | @Examples("set {_byte} to 5 as byte") 25 | @Since("1.0") 26 | 27 | public class ExprNumberAs extends SimpleExpression { 28 | 29 | private Expression NumberExpr; 30 | private int mark; 31 | 32 | static { 33 | Skript.registerExpression(ExprNumberAs.class, Number.class, ExpressionType.COMBINED, 34 | "%number% as [primitive] (0¦int[eger]|1¦float|2¦long|3¦double|4¦short|5¦byte)"); 35 | } 36 | 37 | @Override 38 | public boolean isSingle() { 39 | return true; 40 | } 41 | 42 | @Override 43 | public @NotNull Class getReturnType() { 44 | return Number.class; 45 | } 46 | 47 | @Override 48 | @SuppressWarnings("unchecked") 49 | public boolean init(Expression[] exprs, int matchedPattern, @NotNull Kleenean isDelayed, ParseResult parser) { 50 | NumberExpr = (Expression) exprs[0]; 51 | mark = parser.mark; 52 | return true; 53 | } 54 | 55 | @Override 56 | @Nullable 57 | protected Number @NotNull [] get(@NotNull Event e) { 58 | final Number number = NumberExpr.getSingle(e); 59 | 60 | if (number == null) { 61 | return new Number[0]; 62 | } 63 | 64 | return CollectionUtils.array( 65 | Objects.requireNonNull(NumberEnums.get(mark)).toSingle(number) 66 | ); 67 | } 68 | 69 | @Override 70 | public @NotNull String toString(@Nullable Event e, boolean debug) { 71 | return NumberExpr.getSingle(e) + " as " + Objects.requireNonNull(NumberEnums.get(mark)).name(); 72 | } 73 | 74 | } -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/elements/expressions/utility/ExprNumbersAsArray.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.elements.expressions.utility; 2 | 3 | import fr.anarchick.skriptpacket.util.NumberEnums; 4 | import org.bukkit.event.Event; 5 | import org.eclipse.jdt.annotation.Nullable; 6 | 7 | import ch.njol.skript.Skript; 8 | import ch.njol.skript.doc.Description; 9 | import ch.njol.skript.doc.Examples; 10 | import ch.njol.skript.doc.Name; 11 | import ch.njol.skript.doc.Since; 12 | import ch.njol.skript.lang.Expression; 13 | import ch.njol.skript.lang.ExpressionType; 14 | import ch.njol.skript.lang.SkriptParser.ParseResult; 15 | import ch.njol.skript.lang.util.SimpleExpression; 16 | import ch.njol.util.Kleenean; 17 | import org.jetbrains.annotations.NotNull; 18 | 19 | import java.util.Arrays; 20 | 21 | @Name("Number As Array") 22 | @Description({ 23 | "Convert %numbers% to int/float/long/double/short/byte array", 24 | "Support conversion to primitive array" 25 | }) 26 | @Examples({ 27 | "set {_int::*} to 5, 3 and 1 as primitive int array # Return int[]", 28 | "set {_integer::*} to 5, 3 and 1 as int array # Return Integer[]" 29 | }) 30 | @Since("1.1") 31 | 32 | public class ExprNumbersAsArray extends SimpleExpression { 33 | 34 | private Expression NumbersExpr; 35 | private boolean toPrimitive = false; 36 | private NumberEnums numberEnum; 37 | 38 | static { 39 | String[] patterns = new String[] { 40 | "%numbers% as (0¦Int[eger]|1¦Float|2¦Long|3¦Double|4¦Short|5¦Byte) array", 41 | "%numbers% as primitive (0¦int|1¦float|2¦long|3¦double|4¦short|5¦byte) array" 42 | }; 43 | Skript.registerExpression(ExprNumbersAsArray.class, Object.class, ExpressionType.COMBINED, patterns); 44 | } 45 | 46 | @Override 47 | @SuppressWarnings("unchecked") 48 | public boolean init(Expression[] exprs, int matchedPattern, @NotNull Kleenean isDelayed, ParseResult parser) { 49 | NumbersExpr = (Expression) exprs[0]; 50 | toPrimitive = (matchedPattern == 1); 51 | numberEnum = NumberEnums.get(parser.mark); 52 | return true; 53 | } 54 | 55 | @Override 56 | protected Object @NotNull [] get(@NotNull Event e) { 57 | final Number[] numbers = NumbersExpr.getAll(e); 58 | 59 | if (toPrimitive) { 60 | return new Object[] {numberEnum.toPrimitiveArray(numbers)}; 61 | } else { 62 | return new Object[] {numberEnum.toArray(numbers)}; 63 | } 64 | 65 | } 66 | 67 | @Override 68 | public boolean isSingle() { 69 | return true; 70 | } 71 | 72 | @Override 73 | public @NotNull Class getReturnType() { 74 | return (toPrimitive) ? numberEnum.primitiveArrayClass : numberEnum.objectArrayClass; 75 | } 76 | 77 | @Override 78 | public @NotNull String toString(@Nullable Event e, boolean debug) { 79 | final String str = (toPrimitive) ? " as primitive " : " as "; 80 | return Arrays.toString(NumbersExpr.getAll(e)) + str + numberEnum.name() + " array"; 81 | } 82 | 83 | } -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/packets/BukkitPacketEvent.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.packets; 2 | 3 | import org.bukkit.entity.Player; 4 | import org.bukkit.event.Cancellable; 5 | import org.bukkit.event.Event; 6 | import org.bukkit.event.HandlerList; 7 | 8 | import com.comphenix.protocol.PacketType; 9 | import com.comphenix.protocol.events.ListenerPriority; 10 | import com.comphenix.protocol.events.PacketContainer; 11 | import com.comphenix.protocol.events.PacketEvent; 12 | import org.jetbrains.annotations.NotNull; 13 | 14 | public class BukkitPacketEvent extends Event implements Cancellable { 15 | 16 | private static final HandlerList handlers = new HandlerList(); 17 | private final PacketEvent packetEvent; 18 | private final ListenerPriority priority; 19 | private final PacketManager.Mode mode; 20 | 21 | public BukkitPacketEvent(PacketEvent event, ListenerPriority priority, PacketManager.Mode mode, boolean async) { 22 | super(async); 23 | this.packetEvent = event; 24 | this.priority = priority; 25 | this.mode = mode; 26 | } 27 | 28 | public PacketType getPacketType() { 29 | return this.packetEvent.getPacketType(); 30 | } 31 | 32 | public ListenerPriority getPriority() { 33 | return this.priority; 34 | } 35 | 36 | public PacketContainer getPacket() { 37 | return this.packetEvent.getPacket(); 38 | } 39 | 40 | public PacketManager.Mode getMode() { 41 | return this.mode; 42 | } 43 | 44 | public Player getPlayer() { 45 | return this.packetEvent.getPlayer(); 46 | } 47 | 48 | @NotNull 49 | @Override 50 | public HandlerList getHandlers() { 51 | return handlers; 52 | } 53 | 54 | public static HandlerList getHandlerList() { 55 | return handlers; 56 | } 57 | 58 | @Override 59 | public boolean isCancelled() { 60 | return this.packetEvent.isCancelled(); 61 | } 62 | 63 | @Override 64 | public void setCancelled(boolean b) { 65 | this.packetEvent.setCancelled(b); 66 | } 67 | 68 | @Override 69 | public String toString() { 70 | return String.format("BukkitPacketEvent[%s;%s;%s;%s]", packetEvent.getPacketType().name(), mode.name(), priority.name(), isAsynchronous()); 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/packets/SPPacketAdapter.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.packets; 2 | 3 | import ch.njol.skript.util.Task; 4 | import com.comphenix.protocol.PacketType; 5 | import com.comphenix.protocol.events.ListenerPriority; 6 | import com.comphenix.protocol.events.PacketAdapter; 7 | import com.comphenix.protocol.events.PacketEvent; 8 | import fr.anarchick.skriptpacket.SkriptPacket; 9 | import org.bukkit.plugin.java.JavaPlugin; 10 | 11 | public class SPPacketAdapter extends PacketAdapter { 12 | 13 | public static final JavaPlugin PLUGIN = SkriptPacket.getInstance(); 14 | public final ListenerPriority priority; 15 | public final PacketType packetType; 16 | public final PacketManager.Mode mode; 17 | private final boolean isServer, isAsync; 18 | 19 | public SPPacketAdapter(ListenerPriority priority, PacketType packetType, PacketManager.Mode mode) { 20 | super(PLUGIN, priority, packetType); 21 | this.priority = priority; 22 | this.packetType = packetType; 23 | this.mode = mode; 24 | this.isServer = packetType.isServer(); 25 | this.isAsync = PacketManager.Mode.ASYNC.equals(mode); 26 | } 27 | 28 | @Override 29 | public void onPacketSending(PacketEvent event) { 30 | if (event.getPacketType().equals(packetType) && isServer) { 31 | SkriptPacket.pluginManager.callEvent(new BukkitPacketEvent(event, priority, mode, isAsync)); 32 | } 33 | } 34 | 35 | @Override 36 | public void onPacketReceiving(PacketEvent event) { 37 | if (event.getPacketType().equals(packetType) && !isServer) { 38 | /* 39 | if (event.getPacket().getMeta("uuid").isEmpty()) { 40 | event.getPacket().setMeta("uuid", UUID.randomUUID()); 41 | } 42 | */ 43 | 44 | if (PacketManager.Mode.SYNC.equals(mode)) { 45 | // Can't use Bukkit scheduler https://discord.com/channels/135877399391764480/154927412394590208/1294976538865045547 46 | Task.callSync(() -> { 47 | SkriptPacket.pluginManager 48 | .callEvent(new BukkitPacketEvent(event, priority, mode, isAsync)); 49 | return null; 50 | }, SkriptPacket.getInstance()); 51 | } else { 52 | SkriptPacket.pluginManager.callEvent(new BukkitPacketEvent(event, priority, mode, isAsync)); 53 | } 54 | 55 | } 56 | } 57 | 58 | @Override 59 | public boolean equals(Object o) { 60 | if (o == null) { 61 | return false; 62 | } else if (o == this) { 63 | return true; 64 | } else { 65 | return o.hashCode() == hashCode(); 66 | } 67 | } 68 | 69 | @Override 70 | public int hashCode() { 71 | return mode.hashCode() + packetType.hashCode() + priority.hashCode(); 72 | } 73 | 74 | @Override 75 | public String toString() { 76 | return String.format("SPPacketAdapter[%s;%s;%s]", packetType.name(), mode.name(), priority.name()); 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/packets/SkriptPacketEventListener.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.packets; 2 | 3 | import ch.njol.skript.ScriptLoader; 4 | import ch.njol.skript.config.Config; 5 | import com.comphenix.protocol.PacketType; 6 | import com.comphenix.protocol.events.ListenerPriority; 7 | import fr.anarchick.skriptpacket.packets.PacketManager.Mode; 8 | 9 | import java.util.*; 10 | 11 | public class SkriptPacketEventListener { 12 | 13 | record Manager(String scriptName, Mode mode, 14 | PacketType packetType, ListenerPriority priority) { 15 | 16 | @Override 17 | public int hashCode() { 18 | return mode.hashCode() + packetType.hashCode() + priority.hashCode(); 19 | } 20 | 21 | @Override 22 | public String toString() { 23 | return String.format("[%s;%s;%s;%s]", scriptName, packetType.name(), mode.name(), priority.name()); 24 | } 25 | 26 | } 27 | 28 | private static final Map> MAP = new HashMap<>(); 29 | 30 | public static void register(PacketType[] packetTypes, ListenerPriority priority, Mode mode, String scriptName) { 31 | 32 | for (PacketType packetType : packetTypes) { 33 | final Manager manager = new Manager(scriptName, mode, packetType, priority); 34 | final Set managers = MAP.getOrDefault(scriptName, new HashSet<>()); 35 | managers.add(manager); 36 | MAP.put(scriptName, managers); // TODO Check if needed 37 | } 38 | 39 | update(scriptName); 40 | } 41 | 42 | public static List getAllScriptsNames() { 43 | return ScriptLoader 44 | .getLoadedScripts() 45 | .stream() 46 | .map(script -> script.getConfig().getFileName()) 47 | .toList(); 48 | } 49 | 50 | /** 51 | * Register only 1 time each combo of packettype + priority + mode 52 | * @param currentScriptName The name of the script which is currently reloading 53 | */ 54 | private static void update(final String currentScriptName) { 55 | final HashSet toRegister = new HashSet<>(); 56 | final List allScriptsNames = new ArrayList<>(getAllScriptsNames()); 57 | final Set registeredScriptsNames = new HashSet<>(MAP.keySet()); 58 | allScriptsNames.add(currentScriptName); 59 | 60 | for (String scriptName : registeredScriptsNames) { 61 | 62 | if (!allScriptsNames.contains(scriptName)) { 63 | MAP.remove(scriptName); 64 | } 65 | 66 | } 67 | 68 | for (Set managerSet : MAP.values()) { 69 | 70 | for (Manager managerToAdd : managerSet) { 71 | 72 | boolean canAdd = true; 73 | 74 | for (Manager registeredManager : toRegister) { 75 | 76 | if (managerToAdd.hashCode() == registeredManager.hashCode()) { 77 | canAdd = false; 78 | break; 79 | } 80 | 81 | } 82 | 83 | if (canAdd) { 84 | toRegister.add(managerToAdd); 85 | } 86 | 87 | } 88 | 89 | } 90 | 91 | PacketManager.removeListeners(); 92 | PacketManager.removeAsyncListeners(); 93 | 94 | for (Manager manager : toRegister) { 95 | PacketManager.onPacketEvent(manager.packetType(), manager.priority, manager.mode); 96 | } 97 | 98 | //PacketManager.debug(); 99 | 100 | } 101 | 102 | /** 103 | * Called once time before reload. 104 | * called for /sk reload scripts or /reload confirm 105 | * @param configs PreScriptLoadEvent 106 | */ 107 | public static void beforeReload(Collection configs) { 108 | for (Config config : configs) { 109 | final String scriptName = config.getFileName(); 110 | MAP.remove(scriptName); 111 | } 112 | } 113 | 114 | } 115 | -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/sections/DoSection.java: -------------------------------------------------------------------------------- 1 | /* 2 | package fr.anarchick.skriptpacket.sections; 3 | 4 | import java.util.List; 5 | 6 | import com.btk5h.skriptmirror.util.SkriptReflection; 7 | import org.bukkit.event.Event; 8 | import org.bukkit.event.HandlerList; 9 | import org.eclipse.jdt.annotation.Nullable; 10 | 11 | import ch.njol.skript.Skript; 12 | import ch.njol.skript.config.SectionNode; 13 | import ch.njol.skript.doc.NoDoc; 14 | import ch.njol.skript.effects.Delay; 15 | import ch.njol.skript.lang.Expression; 16 | import ch.njol.skript.lang.Section; 17 | import ch.njol.skript.lang.SkriptParser.ParseResult; 18 | import ch.njol.skript.variables.Variables; 19 | import ch.njol.skript.lang.TriggerItem; 20 | import ch.njol.skript.lang.TriggerSection; 21 | import ch.njol.util.Kleenean; 22 | import fr.anarchick.skriptpacket.util.Scheduling; 23 | 24 | // This is experimental 25 | 26 | @NoDoc 27 | public class DoSection extends Section { 28 | 29 | private enum Type { 30 | ASYNC, SYNC, PARALLEL 31 | } 32 | 33 | private boolean shouldWait = false; 34 | private Type type; 35 | private TriggerSection trigger; 36 | private int pattern; 37 | private static final String[] patterns = new String[] { 38 | "(async|do in background) [(1¦and wait)]", 39 | "(sync|do) [(1¦and wait)]", 40 | "(parallel|do in parallel) [(1¦and wait)]" 41 | }; 42 | private static final Type[] types = Type.values(); 43 | 44 | static { 45 | Skript.registerSection(DoSection.class, patterns); 46 | } 47 | 48 | @Override 49 | public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parser, 50 | SectionNode sectionNode, List triggerItems) { 51 | shouldWait = parser.mark == 1; 52 | pattern = matchedPattern; 53 | type = types[pattern]; 54 | trigger = new TriggerSection(sectionNode) { 55 | @Override 56 | public String toString(@Nullable Event e, boolean debug) { 57 | return e.toString(); 58 | } 59 | 60 | @Override 61 | protected @Nullable TriggerItem walk(Event e) { 62 | return walk(e, true); 63 | } 64 | }; 65 | SkriptReflection.setHasDelayBefore(Kleenean.TRUE); 66 | return true; 67 | } 68 | 69 | @Override 70 | protected @Nullable TriggerItem walk(Event e) { 71 | shouldWait = shouldWait && getNext() != null; 72 | Event event = (!type.equals(Type.PARALLEL)) ? e : new Event(e.isAsynchronous()) { 73 | private final HandlerList handlers = new HandlerList(); 74 | 75 | @Override 76 | public HandlerList getHandlers() { 77 | return this.handlers; 78 | } 79 | 80 | }; 81 | Object localVars = (shouldWait) ? Variables.removeLocals(e) : SkriptReflection.copyLocals(SkriptReflection.getLocals(e)); 82 | final Runnable runSection = () -> { 83 | if (localVars != null) Variables.setLocalVariables(event, localVars); 84 | TriggerItem.walk(trigger, event); 85 | if (shouldWait) { 86 | Runnable continuation = () -> { 87 | Object _localVars = SkriptReflection.copyLocals(SkriptReflection.getLocals(e)); 88 | Variables.setLocalVariables(e, _localVars); 89 | TriggerItem.walk(getNext(), e); 90 | }; 91 | 92 | runTask(continuation, e.isAsynchronous() ? Type.ASYNC : Type.SYNC); 93 | } 94 | }; 95 | if (shouldWait) 96 | Delay.addDelayedEvent(e); 97 | runTask(runSection, type); 98 | return shouldWait ? null : getNext(); 99 | } 100 | 101 | @Override 102 | public String toString(@Nullable Event e, boolean debug) { 103 | return patterns[pattern]; 104 | } 105 | 106 | private void runTask(Runnable runnable, Type type) { 107 | switch (type) { 108 | case ASYNC -> Scheduling.async(runnable); 109 | case SYNC -> Scheduling.sync(runnable); 110 | case PARALLEL -> runnable.run(); 111 | default -> { 112 | } 113 | } 114 | } 115 | } 116 | */ -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/util/ClassUtils.java: -------------------------------------------------------------------------------- 1 | // A small part of Apache lang3 library 2 | 3 | package fr.anarchick.skriptpacket.util; 4 | 5 | import java.util.HashMap; 6 | import java.util.Map; 7 | 8 | public class ClassUtils { 9 | 10 | private static final Map, Class> primitiveWrapperMap = new HashMap<>(); 11 | private static final Map, Class> wrapperPrimitiveMap = new HashMap<>(); 12 | 13 | static { 14 | primitiveWrapperMap.put(Boolean.TYPE, Boolean.class); 15 | primitiveWrapperMap.put(Byte.TYPE, Byte.class); 16 | primitiveWrapperMap.put(Character.TYPE, Character.class); 17 | primitiveWrapperMap.put(Short.TYPE, Short.class); 18 | primitiveWrapperMap.put(Integer.TYPE, Integer.class); 19 | primitiveWrapperMap.put(Long.TYPE, Long.class); 20 | primitiveWrapperMap.put(Double.TYPE, Double.class); 21 | primitiveWrapperMap.put(Float.TYPE, Float.class); 22 | primitiveWrapperMap.put(Void.TYPE, Void.TYPE); 23 | } 24 | 25 | static { 26 | for (final Map.Entry, Class> entry : primitiveWrapperMap.entrySet()) { 27 | final Class primitiveClass = entry.getKey(); 28 | final Class wrapperClass = entry.getValue(); 29 | 30 | if (!primitiveClass.equals(wrapperClass)) { 31 | wrapperPrimitiveMap.put(wrapperClass, primitiveClass); 32 | } 33 | 34 | } 35 | } 36 | 37 | public static Class wrapperToPrimitive(final Class cls) { 38 | return wrapperPrimitiveMap.get(cls); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/util/NumberEnums.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.util; 2 | 3 | import it.unimi.dsi.fastutil.ints.IntArrayList; 4 | import it.unimi.dsi.fastutil.ints.IntList; 5 | 6 | import java.lang.reflect.Array; 7 | import java.util.Arrays; 8 | 9 | /** 10 | * Replaced by NumberEnums since 2.1.0 11 | */ 12 | public enum NumberEnums { 13 | INTEGER(Integer.class, Integer.TYPE), 14 | FLOAT(Float.class, Float.TYPE), 15 | LONG(Long.class, Long.TYPE), 16 | DOUBLE(Double.class, Double.TYPE), 17 | SHORT(Short.class, Short.TYPE), 18 | BYTE(Byte.class, Byte.TYPE); 19 | 20 | public final Class objectClass; 21 | public final Class objectArrayClass; 22 | public final Class primitiveClass; 23 | public final Class primitiveArrayClass; 24 | 25 | @SuppressWarnings({"unchecked" }) 26 | NumberEnums(Class objectClass, Class primitiveClass) { 27 | this.objectClass = objectClass; 28 | this.objectArrayClass = (Class) objectClass.arrayType(); 29 | this.primitiveClass = primitiveClass; 30 | this.primitiveArrayClass = primitiveClass.arrayType(); 31 | } 32 | 33 | public static NumberEnums get(int ordinal) { 34 | for (NumberEnums e : values()) { 35 | 36 | if (e.ordinal() == ordinal) { 37 | return e; 38 | } 39 | 40 | } 41 | 42 | return null; 43 | } 44 | 45 | public static NumberEnums get(Class clazz) { 46 | final String className = (clazz.isArray()) ? clazz.getComponentType().getSimpleName().toUpperCase(): clazz.getSimpleName().toUpperCase(); 47 | 48 | return switch (className) { 49 | case "BYTE" -> BYTE; 50 | case "INT", "INTEGER", "INTLIST" -> INTEGER; 51 | case "LONG" -> LONG; 52 | case "SHORT" -> SHORT; 53 | case "FLOAT" -> FLOAT; 54 | case "DOUBLE" -> DOUBLE; 55 | default -> null; 56 | }; 57 | } 58 | 59 | public static boolean isNumber(Class clazz) { 60 | return get(clazz) != null; 61 | } 62 | 63 | @SuppressWarnings({"unchecked" }) 64 | public static T convert(Class targetClass, Number... input) { 65 | if (input == null || input.length == 0) { 66 | return null; 67 | } 68 | 69 | if (targetClass.equals(IntList.class)) { 70 | return (T) new IntArrayList((int[]) INTEGER.toPrimitiveArray(input)); 71 | } 72 | 73 | final NumberEnums target = get(targetClass); 74 | 75 | if (targetClass.isArray()) { 76 | return (targetClass.getComponentType().isPrimitive()) ? (T) target.toPrimitiveArray(input) : (T) target.toArray(input); 77 | } else { 78 | return (T) target.toSingle(input[0]); 79 | } 80 | } 81 | 82 | private T createPrimitiveArray(int length) { 83 | return switch (ordinal()) { 84 | case 0 -> (T) new int[length]; 85 | case 1 -> (T) new float[length]; 86 | case 2 -> (T) new long[length]; 87 | case 3 -> (T) new double[length]; 88 | case 4 -> (T) new short[length]; 89 | case 5 -> (T) new byte[length]; 90 | default -> null; 91 | }; 92 | } 93 | 94 | public Number toSingle(Number input) { 95 | return switch (ordinal()) { 96 | case 0 -> input.intValue(); 97 | case 1 -> input.floatValue(); 98 | case 2 -> input.longValue(); 99 | case 3 -> input.doubleValue(); 100 | case 4 -> input.shortValue(); 101 | case 5 -> input.byteValue(); 102 | default -> null; 103 | }; 104 | } 105 | 106 | public Number[] toArray(Number... input) { 107 | int length = (input == null) ? 0 : input.length; 108 | 109 | if (this.objectArrayClass.isInstance(input)) { 110 | return input; 111 | } 112 | 113 | final Number[] output = (Number[]) Array.newInstance(this.objectClass, length); 114 | Arrays.setAll(output, (i) -> toSingle(input[i])); 115 | return output; 116 | } 117 | 118 | public Object toPrimitiveArray(Number... input) { 119 | int length = (input == null) ? 0 : input.length; 120 | 121 | if (this.primitiveArrayClass.isInstance(input)) { 122 | return input; 123 | } 124 | 125 | final Object output = createPrimitiveArray(length); 126 | 127 | for (int i = 0; i < length; i++) { 128 | Array.set(output, i, toSingle(input[i])); 129 | } 130 | 131 | return output; 132 | } 133 | 134 | } 135 | -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/util/NumberUtils.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.util; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import it.unimi.dsi.fastutil.ints.IntArrayList; 7 | import it.unimi.dsi.fastutil.ints.IntList; 8 | 9 | public class NumberUtils { 10 | 11 | @SuppressWarnings("rawtypes") 12 | public static final List NUMBER = Arrays.asList(new Class[] { 13 | Integer.TYPE, 14 | Float.TYPE, 15 | Long.TYPE, 16 | Double.TYPE, 17 | Short.TYPE, 18 | Byte.TYPE, 19 | IntList.class}); 20 | 21 | @SuppressWarnings("rawtypes") 22 | public static final List OBJECT_NUMBER = Arrays.asList(new Class[] { 23 | Integer.class, 24 | Float.class, 25 | Long.class, 26 | Double.class, 27 | Short.class, 28 | Byte.class, 29 | Integer.TYPE, 30 | Float.TYPE, 31 | Long.TYPE, 32 | Double.TYPE, 33 | Short.TYPE, 34 | Byte.TYPE}); 35 | 36 | public static final List> OBJECT_NUMBER_ARRAY = Arrays.asList(new Class[] { 37 | Integer[].class, 38 | Float[].class, 39 | Long[].class, 40 | Double[].class, 41 | Short[].class, 42 | Byte[].class}); 43 | 44 | @SuppressWarnings("rawtypes") 45 | public static final List PRIMITIVE_NUMBER_ARRAY = Arrays.asList(new Class[] { 46 | int[].class, 47 | float[].class, 48 | long[].class, 49 | double[].class, 50 | short[].class, 51 | byte[].class}); 52 | 53 | @SuppressWarnings("unchecked") 54 | public static T convert(Class targetClass, Number ...array) { 55 | if (array == null || array.length == 0) { 56 | return null; 57 | } 58 | 59 | if (PRIMITIVE_NUMBER_ARRAY.contains(targetClass)) { 60 | return toPrimitiveArray(targetClass, array); 61 | } 62 | 63 | if (targetClass.equals(IntList.class)) { 64 | return (T) new IntArrayList(toPrimitiveIntArray(array)); 65 | } 66 | 67 | return (targetClass.isArray()) ? (T) toArray(targetClass, array) : (T) toSingle(targetClass, array[0]); 68 | } 69 | 70 | /** 71 | * Can produce an exception if the target class does not support a high value 72 | */ 73 | public static Number toSingle(Class targetClass, Number n) { 74 | return switch (OBJECT_NUMBER.indexOf(targetClass)) { 75 | case 6, 0 -> n.intValue(); 76 | case 7, 1 -> n.floatValue(); 77 | case 8, 2 -> n.longValue(); 78 | case 9, 3 -> n.doubleValue(); 79 | case 10, 4 -> n.shortValue(); 80 | case 11, 5 -> n.byteValue(); 81 | default -> null; 82 | }; 83 | } 84 | 85 | /** 86 | * Can produce an exception if the target class does not support a high value 87 | */ 88 | public static Number[] toArray(Class targetClass, Number ...array) { 89 | return switch (OBJECT_NUMBER_ARRAY.indexOf(targetClass)) { 90 | case 0 -> toIntegerArray(array); 91 | case 1 -> toFloatArray(array); 92 | case 2 -> toLongArray(array); 93 | case 3 -> toDoubleArray(array); 94 | case 4 -> toShortArray(array); 95 | case 5 -> toByteArray(array); 96 | default -> null; 97 | }; 98 | } 99 | 100 | public static T toPrimitiveArray(Class targetClass, Number ...array) { 101 | return switch (PRIMITIVE_NUMBER_ARRAY.indexOf(targetClass)) { 102 | case 0 -> (T) toPrimitiveIntArray(array); 103 | case 1 -> (T) toPrimitiveFloatArray(array); 104 | case 2 -> (T) toPrimitiveLongArray(array); 105 | case 3 -> (T) toPrimitiveDoubleArray(array); 106 | case 4 -> (T) toPrimitiveShortArray(array); 107 | case 5 -> (T) toPrimitiveByteArray(array); 108 | default -> null; 109 | }; 110 | } 111 | 112 | public static Integer[] toIntegerArray(final Number ...array) { 113 | final Integer[] result = new Integer[array.length]; 114 | 115 | for (int i = 0; i < array.length; i++) { 116 | result[i] = array[i].intValue(); 117 | } 118 | 119 | return result; 120 | } 121 | 122 | public static Float[] toFloatArray(final Number ...array) { 123 | final Float[] result = new Float[array.length]; 124 | 125 | for (int i = 0; i < array.length; i++) { 126 | result[i] = array[i].floatValue(); 127 | } 128 | 129 | return result; 130 | } 131 | 132 | public static Long[] toLongArray(final Number ...array) { 133 | final Long[] result = new Long[array.length]; 134 | 135 | for (int i = 0; i < array.length; i++) { 136 | result[i] = array[i].longValue(); 137 | } 138 | 139 | return result; 140 | } 141 | 142 | public static Short[] toShortArray(final Number ...array) { 143 | final Short[] result = new Short[array.length]; 144 | 145 | for (int i = 0; i < array.length; i++) { 146 | result[i] = array[i].shortValue(); 147 | } 148 | 149 | return result; 150 | } 151 | 152 | public static Double[] toDoubleArray(final Number ...array) { 153 | final Double[] result = new Double[array.length]; 154 | 155 | for (int i = 0; i < array.length; i++) { 156 | result[i] = array[i].doubleValue(); 157 | } 158 | 159 | return result; 160 | } 161 | 162 | public static Byte[] toByteArray(final Number ...array) { 163 | final Byte[] result = new Byte[array.length]; 164 | 165 | for (int i = 0; i < array.length; i++) { 166 | result[i] = array[i].byteValue(); 167 | } 168 | 169 | return result; 170 | } 171 | 172 | public static int[] toPrimitiveIntArray(final Number ...array) { 173 | return Arrays.stream(array).mapToInt(Number::intValue).toArray(); 174 | } 175 | public static float[] toPrimitiveFloatArray(final Number ...array) { 176 | final float[] result = new float[array.length]; 177 | 178 | for (int i = 0; i < array.length; i++) { 179 | result[i] = array[i].floatValue(); 180 | } 181 | 182 | return result; 183 | } 184 | public static long[] toPrimitiveLongArray(final Number ...array) { 185 | return Arrays.stream(array).mapToLong(Number::longValue).toArray(); 186 | } 187 | public static double[] toPrimitiveDoubleArray(final Number ...array) { 188 | return Arrays.stream(array).mapToDouble(Number::doubleValue).toArray(); 189 | } 190 | public static short[] toPrimitiveShortArray(final Number ...array) { 191 | final short[] result = new short[array.length]; 192 | 193 | for (int i = 0; i < array.length; i++) { 194 | result[i] = array[i].shortValue(); 195 | } 196 | 197 | return result; 198 | } 199 | public static byte[] toPrimitiveByteArray(final Number ...array) { 200 | final byte[] result = new byte[array.length]; 201 | 202 | for (int i = 0; i < array.length; i++) { 203 | result[i] = array[i].byteValue(); 204 | } 205 | 206 | return result; 207 | } 208 | 209 | public static boolean isNumber(Class clazz) { 210 | if (clazz == null) { 211 | return false; 212 | } 213 | 214 | if (clazz.isArray()) { 215 | clazz = clazz.getComponentType(); 216 | } 217 | 218 | return NUMBER.contains(clazz); 219 | } 220 | 221 | } -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/util/Scheduling.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.util; 2 | 3 | import org.bukkit.Bukkit; 4 | import org.bukkit.scheduler.BukkitScheduler; 5 | 6 | import fr.anarchick.skriptpacket.SkriptPacket; 7 | 8 | public class Scheduling { 9 | 10 | private static final BukkitScheduler scheduler = Bukkit.getScheduler(); 11 | private static final SkriptPacket plugin = SkriptPacket.getInstance(); 12 | 13 | public static void sync(Runnable runnable) { 14 | scheduler.runTask(plugin, runnable); 15 | } 16 | 17 | public static void async(Runnable runnable) { 18 | scheduler.runTaskAsynchronously(plugin, runnable); 19 | } 20 | 21 | public static void syncDelay(int ticks, Runnable runnable) { 22 | scheduler.runTaskLater(plugin, runnable, ticks); 23 | } 24 | 25 | public static void asyncDelay(int ticks, Runnable runnable) { 26 | scheduler.runTaskLaterAsynchronously(plugin, runnable, ticks); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/util/Utils.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.util; 2 | 3 | import java.util.regex.Matcher; 4 | import java.util.regex.Pattern; 5 | 6 | import org.bukkit.entity.Entity; 7 | 8 | import ch.njol.skript.Skript; 9 | 10 | public class Utils { 11 | 12 | public static Object getEnum(Class clazz, String enumStr) { 13 | if (enumStr == null || clazz == null || enumStr.isEmpty()) { 14 | return new Object[0]; 15 | } 16 | 17 | if (!clazz.isEnum()) { 18 | Skript.error(clazz + " is not an enum Class"); 19 | return null; 20 | } 21 | 22 | for (Object enumConstant : clazz.getEnumConstants()) { 23 | 24 | if ( ((Enum)enumConstant).name().equals(enumStr) ) { 25 | return enumConstant; 26 | } 27 | 28 | } 29 | 30 | Skript.error("Failed to find enum '"+enumStr+"' from "+clazz); 31 | Skript.error("Possible enums are : '"+getEnumsNames(clazz)+"'" ); 32 | return null; 33 | } 34 | 35 | public static String getEnumsNames(Class clazz) { 36 | Object[] values = new Object[0]; 37 | 38 | try { 39 | values = (Object[]) clazz.getDeclaredMethod("values").invoke(null); 40 | } catch (Exception ignored) {} 41 | 42 | if (values.length == 0) { 43 | return ""; 44 | } 45 | 46 | if (values.length > 1) { 47 | final StringBuilder builder = new StringBuilder(); 48 | 49 | for (int i = 0; i < values.length - 1; i++) { 50 | builder.append(values[i]); 51 | builder.append(", "); 52 | } 53 | 54 | builder.append(values[values.length - 1]); 55 | return builder.toString(); 56 | } else { 57 | return values[0].toString(); 58 | } 59 | } 60 | 61 | public static String regexGroup(String pattern, String matcher, int group) { 62 | final Pattern p = Pattern.compile(pattern); 63 | final Matcher m = p.matcher(matcher); 64 | 65 | if (!m.find()) { 66 | return ""; 67 | } 68 | 69 | int count = m.groupCount(); 70 | 71 | if (group <= 0 || group > count) { 72 | return ""; 73 | } 74 | 75 | return m.group(group); 76 | } 77 | 78 | public static Number[] EntitiesIDs(Entity[] entities) { 79 | final Number[] ids = new Number[entities.length]; 80 | 81 | for (int i = 0 ; i < entities.length ; i++ ) { 82 | final Entity ent = entities[i]; 83 | ids[i] = ent.getEntityId(); 84 | } 85 | 86 | return ids; 87 | } 88 | 89 | } 90 | -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/util/converters/Converter.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.util.converters; 2 | 3 | public interface Converter { 4 | 5 | Object convert(final Object single); 6 | 7 | default boolean isArrayInput() { 8 | return false; 9 | } 10 | 11 | default boolean returnArray() { 12 | return false; 13 | } 14 | 15 | Class getInputType(); 16 | Class getOutputType(); 17 | 18 | ConverterType getType(); 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/util/converters/ConverterToBukkit.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.util.converters; 2 | 3 | import ch.njol.skript.aliases.ItemType; 4 | import ch.njol.skript.util.BlockUtils; 5 | import com.comphenix.protocol.utility.MinecraftReflection; 6 | import com.comphenix.protocol.wrappers.BlockPosition; 7 | import com.comphenix.protocol.wrappers.BukkitConverters; 8 | import com.comphenix.protocol.wrappers.Vector3F; 9 | import net.kyori.adventure.text.Component; 10 | import org.bukkit.*; 11 | import org.bukkit.block.Block; 12 | import org.bukkit.block.data.BlockData; 13 | import org.bukkit.entity.Entity; 14 | import org.bukkit.inventory.ItemStack; 15 | import org.bukkit.util.Vector; 16 | 17 | import javax.annotation.Nonnull; 18 | import java.util.Optional; 19 | 20 | public enum ConverterToBukkit implements Converter { 21 | 22 | NMS_WORLD_TO_BUKKIT_WORLD { 23 | @Override 24 | public Object convert(final Object single) { 25 | return ConverterLogic.callMethod(single, "getWorld"); 26 | } 27 | 28 | @Override 29 | public Class getInputType() { 30 | return ConverterLogic.WorldServerClass; 31 | } 32 | 33 | @Override 34 | public Class getOutputType() { 35 | return World.class; 36 | } 37 | }, 38 | 39 | /** 40 | * NMS entity to Bukkit entity 41 | */ 42 | NMS_ENTITY_TO_BUKKIT_ENTITY { 43 | @Override 44 | public Object convert(final Object single) { 45 | return MinecraftReflection.getBukkitEntity(single); 46 | } 47 | 48 | @Override 49 | public Class getInputType() { 50 | return ConverterLogic.EntityClass; 51 | } 52 | 53 | @Override 54 | public Class getOutputType() { 55 | return Entity.class; 56 | } 57 | }, 58 | 59 | /** 60 | * NMS Chunk to Bukkit chunk 61 | * Not availaible anymore 62 | */ 63 | @Deprecated 64 | NMS_CHUNK_TO_BUKKIT_CHUNK { 65 | @Override 66 | public Object convert(final Object single) { 67 | return ConverterLogic.callMethod(single, "getBukkitChunk"); 68 | } 69 | 70 | @Override 71 | public Class getInputType() { 72 | return Object.class; 73 | } 74 | 75 | @Override 76 | public Class getOutputType() { 77 | return Chunk.class; 78 | } 79 | }, 80 | 81 | NMS_BLOCKPOSITION_TO_BUKKIT_LOCATION { 82 | @Override 83 | public Object convert(final Object single) { 84 | final World world = Bukkit.getWorlds().get(0); 85 | return BlockPosition.getConverter().getSpecific(single).toLocation(world); 86 | } 87 | 88 | @Override 89 | public Class getInputType() { 90 | return ConverterLogic.BlockPositionClass; 91 | } 92 | 93 | @Override 94 | public Class getOutputType() { 95 | return Location.class; 96 | } 97 | }, 98 | 99 | NMS_VEC3D_TO_BUKKIT_VECTOR { 100 | @Override 101 | public Object convert(final Object single) { 102 | return BukkitConverters.getVectorConverter().getSpecific(single); 103 | } 104 | 105 | @Override 106 | public Class getInputType() { 107 | return ConverterLogic.Vec3DClass; 108 | } 109 | 110 | @Override 111 | public Class getOutputType() { 112 | return Vector.class; 113 | } 114 | }, 115 | 116 | PROTOCOLLIB_VECTOR3F_TO_BUKKIT_VECTOR { 117 | @Override 118 | public Object convert(final Object single) { 119 | if (single instanceof Vector3F vec) { 120 | return new Vector(vec.getX(), vec.getY(), vec.getZ()); 121 | } 122 | 123 | return single; 124 | } 125 | 126 | @Override 127 | public Class getInputType() { 128 | return Vector3F.class; 129 | } 130 | 131 | @Override 132 | public Class getOutputType() { 133 | return Vector.class; 134 | } 135 | }, 136 | 137 | NMS_ITEMSTACK_TO_BUKKIT_ITEMSTACK { 138 | @Override 139 | public Object convert(final Object single) { 140 | return MinecraftReflection.getBukkitItemStack(single); 141 | } 142 | 143 | @Override 144 | public Class getInputType() { 145 | return ConverterLogic.ItemStackClass; 146 | } 147 | 148 | @Override 149 | public Class getOutputType() { 150 | return ItemStack.class; 151 | } 152 | }, 153 | 154 | // TODO to Skript ItemType ? 155 | 156 | NMS_BLOCK_TO_BUKKIT_MATERIAL { 157 | @Override 158 | public Object convert(final Object single) { 159 | return BukkitConverters.getBlockConverter().getSpecific(single); 160 | } 161 | 162 | @Override 163 | public Class getInputType() { 164 | return ConverterLogic.BlockClass; 165 | } 166 | 167 | @Override 168 | public Class getOutputType() { 169 | return Material.class; 170 | } 171 | }, 172 | 173 | NMS_IBLOCKDATA_TO_BUKKIT_BLOCKDATA { 174 | @Override 175 | public Object convert(final Object single) { 176 | String blockDataString = single.toString(); 177 | blockDataString = blockDataString.replace("Block{", ""); 178 | blockDataString = blockDataString.replace("}", ""); 179 | blockDataString = blockDataString.replace(",", ";"); 180 | return BlockUtils.createBlockData(blockDataString); 181 | } 182 | 183 | @Override 184 | public Class getInputType() { 185 | return ConverterLogic.IBlockDataClass; 186 | } 187 | 188 | @Override 189 | public Class getOutputType() { 190 | return BlockData.class; 191 | } 192 | }, 193 | 194 | // TODO to Skript ItemType ? 195 | RELATED_TO_BUKKIT_MATERIAL { 196 | @Nonnull 197 | @Override 198 | public Object convert(final Object obj) { 199 | 200 | if ( obj == null ) { 201 | return Material.AIR; 202 | } 203 | 204 | if ( obj instanceof ItemType item ) { 205 | return item.getMaterial(); 206 | } 207 | 208 | if ( obj instanceof ItemStack item ) { 209 | return item.getType(); 210 | } 211 | 212 | if ( obj instanceof Block block ) { 213 | return block.getType(); 214 | } 215 | 216 | if ( obj instanceof BlockData blockData ) { 217 | return blockData.getMaterial(); 218 | } 219 | 220 | if ( obj instanceof String str ) { 221 | 222 | try { 223 | return Material.valueOf(str.toUpperCase()); 224 | } catch ( IllegalArgumentException ignored) {} 225 | 226 | } 227 | 228 | return Material.AIR; 229 | } 230 | 231 | @Override 232 | public Class getInputType() { 233 | return Object.class; 234 | } 235 | 236 | @Override 237 | public Class getOutputType() { 238 | return Material.class; 239 | } 240 | }, 241 | 242 | STRING_TO_PAPER_COMPONENT { 243 | @Override 244 | public Object convert(final Object single) { 245 | final String text = Optional.ofNullable((String)single).orElse(""); 246 | return ConverterLogic.MINI_MESSAGE.deserialize(text); 247 | } 248 | 249 | @Override 250 | public Class getInputType() { 251 | return String.class; 252 | } 253 | 254 | @Override 255 | public Class getOutputType() { 256 | return Component.class; 257 | } 258 | }; 259 | 260 | 261 | @Override 262 | public ConverterType getType() { 263 | return ConverterType.TO_BUKKIT; 264 | } 265 | 266 | } 267 | -------------------------------------------------------------------------------- /src/main/java/fr/anarchick/skriptpacket/util/converters/ConverterType.java: -------------------------------------------------------------------------------- 1 | package fr.anarchick.skriptpacket.util.converters; 2 | 3 | public enum ConverterType { 4 | 5 | TO_BUKKIT,TO_NMS, TO_UTILITY 6 | 7 | } 8 | -------------------------------------------------------------------------------- /src/main/resources/config.yml: -------------------------------------------------------------------------------- 1 | missing-biome-check: true # If true, the plugin will try to check missing biomes and warn you in the console 2 | biome-ids: # https://minecraft.fandom.com/wiki/Biome/ID , Updated for mc 1.20.4 3 | minecraft-the_void: 0 4 | minecraft-plains: 1 5 | minecraft-sunflower_plains: 2 6 | minecraft-snowy_plains: 3 7 | minecraft-ice_spikes: 4 8 | minecraft-desert: 5 9 | minecraft-swamp: 6 10 | minecraft-mangrove_swamp: 7 11 | minecraft-forest: 8 12 | minecraft-flower_forest: 9 13 | minecraft-birch_forest: 10 14 | minecraft-dark_forest: 11 15 | minecraft-old_growth_birch_forest: 12 16 | minecraft-old_growth_pine_taiga: 13 17 | minecraft-old_growth_spruce_taiga: 14 18 | minecraft-taiga: 15 19 | minecraft-snowy_taiga: 16 20 | minecraft-savanna: 17 21 | minecraft-savanna_plateau: 18 22 | minecraft-windswept_hills: 19 23 | minecraft-windswept_gravelly_hills: 20 24 | minecraft-windswept_forest: 21 25 | minecraft-windswept_savanna: 22 26 | minecraft-jungle: 23 27 | minecraft-sparse_jungle: 24 28 | minecraft-bamboo_jungle: 25 29 | minecraft-badlands: 26 30 | minecraft-eroded_badlands: 27 31 | minecraft-wooded_badlands: 28 32 | minecraft-meadow: 29 33 | minecraft-cherry_grove: 30 34 | minecraft-grove: 31 35 | minecraft-snowy_slopes: 32 36 | minecraft-frozen_peaks: 33 37 | minecraft-jagged_peaks: 34 38 | minecraft-stony_peaks: 35 39 | minecraft-river: 36 40 | minecraft-frozen_river: 37 41 | minecraft-beach: 38 42 | minecraft-snowy_beach: 39 43 | minecraft-stony_shore: 40 44 | minecraft-warm_ocean: 41 45 | minecraft-lukewarm_ocean: 42 46 | minecraft-deep_lukewarm_ocean: 43 47 | minecraft-ocean: 44 48 | minecraft-deep_ocean: 45 49 | minecraft-cold_ocean: 46 50 | minecraft-deep_cold_ocean: 47 51 | minecraft-frozen_ocean: 48 52 | minecraft-deep_frozen_ocean: 49 53 | minecraft-mushroom_fields: 50 54 | minecraft-dripstone_caves: 51 55 | minecraft-lush_caves: 52 56 | minecraft-deep_dark: 53 57 | minecraft-nether_wastes: 54 58 | minecraft-warped_forest: 55 59 | minecraft-crimson_forest: 56 60 | minecraft-soul_sand_valley: 57 61 | minecraft-basalt_deltas: 58 62 | minecraft-the_end: 59 63 | minecraft-end_highlands: 60 64 | minecraft-end_midlands: 61 65 | minecraft-small_end_islands: 62 66 | minecraft-end_barrens: 63 -------------------------------------------------------------------------------- /src/main/resources/plugin.yml: -------------------------------------------------------------------------------- 1 | name: Skript-Packet 2 | author: Anarchick 3 | description: A skript addon for packets 4 | version: '${version}' 5 | api-version: 1.13 6 | main: fr.anarchick.skriptpacket.SkriptPacket 7 | depend: [Skript, ProtocolLib] 8 | softdepend: [skript-reflect] 9 | website: www.github.com/Anarchick/skript-packet 10 | libraries: 11 | - org.json:json:20210307 12 | --------------------------------------------------------------------------------