commit 3ba3eafffceab44fa37c3e6f74eae42773e2ec66 Author: Flytre Date: Wed Sep 8 06:42:41 2021 -0400 Initial commit Signed-off-by: Flytre diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3f11e34 --- /dev/null +++ b/.gitignore @@ -0,0 +1,41 @@ +# Gradle +.gradle/ +loom-cache/ + + +# VSCode +.settings/ +.vscode/ + +# Eclipse +.checkstyle +.classpath +.metadata +.settings +.project +*.launch + +# Intellij/Idea +.factorypath +.idea +*.iml +*.ipr +*.iws + +# Build artifacts +bin/ +build/ +jars/ +out/ +classes/ + +# Debug artifacts +run +*.log + +# Windows +*.db +$RECYCLE.BIN/ + +# Mac +.DS_Store \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..7f69ab0 --- /dev/null +++ b/LICENSE @@ -0,0 +1 @@ +All rights reserved \ No newline at end of file diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..b9fce96 --- /dev/null +++ b/build.gradle @@ -0,0 +1,83 @@ +plugins { + id 'fabric-loom' version '0.8-SNAPSHOT' + id 'maven-publish' +} + +sourceCompatibility = JavaVersion.VERSION_16 +targetCompatibility = JavaVersion.VERSION_16 + +archivesBaseName = project.archives_base_name +version = project.mod_version +group = project.maven_group + +repositories { + maven { url "https://jitpack.io" } + maven {url "https://mvnrepository.com/artifact/org.yaml/snakeyaml"} +} + +dependencies { + // To change the versions see the gradle.properties file + minecraft "com.mojang:minecraft:${project.minecraft_version}" + mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2" + modImplementation "net.fabricmc:fabric-loader:${project.loader_version}" + + // Fabric API. Optional. + modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" + + //Flytre Lib. Optional but you probably want it anyways. + modImplementation "com.github.Flytre.FlytreLib:flytre-lib:${project.flytre_lib_version}" + + implementation 'org.yaml:snakeyaml:1.8' +} + +processResources { + inputs.property "version", project.version + + filesMatching("fabric.mod.json") { + expand "version": project.version + } +} + +tasks.withType(JavaCompile).configureEach { + // ensure that the encoding is set to UTF-8, no matter what the system default is + // this fixes some edge cases with special characters not displaying correctly + // see http://yodaconditions.net/blog/fix-for-java-file-encoding-problems-with-gradle.html + // If Javadoc is generated, this must be specified in that task too. + it.options.encoding = "UTF-8" + + // Minecraft 1.17 (21w19a) upwards uses Java 16. + it.options.release = 16 +} + +java { + // Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task + // if it is present. + // If you remove this line, sources will not be generated. + withSourcesJar() +} + +jar { + from("LICENSE") { + rename { "${it}_${project.archivesBaseName}"} + } +} + +// configure the maven publication +publishing { + publications { + mavenJava(MavenPublication) { + // add all the jars that should be included when publishing to maven + artifact(remapJar) { + builtBy remapJar + } + artifact(sourcesJar) { + builtBy remapSourcesJar + } + } + } + + // See https://docs.gradle.org/current/userguide/publishing_maven.html for information on how to set up publishing. + repositories { + //repositories to publish to + } +} diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..962bd13 --- /dev/null +++ b/gradle.properties @@ -0,0 +1,16 @@ +# Done to increase the memory available to gradle. +org.gradle.jvmargs=-Xmx2G +# Fabric Properties +# check these on https://fabricmc.net/use +minecraft_version=1.17.1 +yarn_mappings=1.17.1+build.35 +loader_version=0.11.6 +# Mod Properties +mod_version=1.0.0 +maven_group=net.flytre +archives_base_name=example +# Dependencies +fabric_version=0.36.1+1.17 +flytre_lib_version=ae27701 + + diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..e708b1c Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..0f80bbf --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..4f906e0 --- /dev/null +++ b/gradlew @@ -0,0 +1,185 @@ +#!/usr/bin/env sh + +# +# Copyright 2015 the original author or authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin or MSYS, switch paths to Windows format before running java +if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=`expr $i + 1` + done + case $i in + 0) set -- ;; + 1) set -- "$args0" ;; + 2) set -- "$args0" "$args1" ;; + 3) set -- "$args0" "$args1" "$args2" ;; + 4) set -- "$args0" "$args1" "$args2" "$args3" ;; + 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=`save "$@"` + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..107acd3 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,89 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..f91a4fe --- /dev/null +++ b/settings.gradle @@ -0,0 +1,9 @@ +pluginManagement { + repositories { + maven { + name = 'Fabric' + url = 'https://maven.fabricmc.net/' + } + gradlePluginPortal() + } +} diff --git a/src/main/java/net/flytre/hoco_sg/Config.java b/src/main/java/net/flytre/hoco_sg/Config.java new file mode 100644 index 0000000..a44f138 --- /dev/null +++ b/src/main/java/net/flytre/hoco_sg/Config.java @@ -0,0 +1,29 @@ +package net.flytre.hoco_sg; + +import net.flytre.flytre_lib.api.config.ConfigEventAcceptor; +import net.flytre.flytre_lib.api.config.annotation.Description; +import net.minecraft.entity.player.PlayerEntity; + +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.stream.Collectors; + +public class Config implements ConfigEventAcceptor { + + + @Description("Admins will not have any information hidden. Place commentators on this list.") + public Set admins = Set.of(); + + @Description("Each entry contains the name of the group/team followed by a list of all members on that team. Members of the same team can see other members of that team more normally. Players not in any group can see no information.") + public Map> groups = Map.of("seniors", Set.of("flytre", "idil")); + + @Override + public void onReload() { + groups.replaceAll((k, v) -> v.stream().filter(Objects::nonNull).map(String::toLowerCase).collect(Collectors.toSet())); + } + + public static Set getGroup(PlayerEntity player) { + return HocoSgInitializer.HANDLER.getConfig().groups.values().stream().filter(i -> player != null && i.contains(player.getEntityName().toLowerCase())).findFirst().orElse(null); + } +} diff --git a/src/main/java/net/flytre/hoco_sg/HocoSgInitializer.java b/src/main/java/net/flytre/hoco_sg/HocoSgInitializer.java new file mode 100644 index 0000000..94b7e16 --- /dev/null +++ b/src/main/java/net/flytre/hoco_sg/HocoSgInitializer.java @@ -0,0 +1,40 @@ +package net.flytre.hoco_sg; + +import com.mojang.brigadier.arguments.IntegerArgumentType; +import net.fabricmc.api.ModInitializer; +import net.flytre.flytre_lib.api.config.ConfigHandler; +import net.flytre.flytre_lib.api.config.ConfigRegistry; +import net.flytre.flytre_lib.api.event.CommandRegistrationEvent; +import net.flytre.hoco_sg.game.Counter; +import net.minecraft.server.command.CommandManager; +import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.server.network.ServerPlayerEntity; + +import java.util.Collections; + +public class HocoSgInitializer implements ModInitializer { + + public static final ConfigHandler HANDLER = new ConfigHandler<>(new Config(), "hoco_sg"); + + @Override + public void onInitialize() { + + ConfigRegistry.registerServerConfig(HANDLER); + + + CommandRegistrationEvent.EVENT.register((dispatcher, dedicated) -> { + dispatcher.register(CommandManager + .literal("counter") + .then(CommandManager.argument("max", IntegerArgumentType.integer()) + .then(CommandManager.argument("bars", IntegerArgumentType.integer()) + .executes(context -> { + int max = IntegerArgumentType.getInteger(context,"max"); + int bars = IntegerArgumentType.getInteger(context,"bars"); + ServerCommandSource src = context.getSource(); + ServerPlayerEntity player = src.getPlayer(); + Counter.addActiveCounter(new Counter(Collections.singleton(player), max, bars,null)); + return 0; + })))); + }); + } +} diff --git a/src/main/java/net/flytre/hoco_sg/game/Counter.java b/src/main/java/net/flytre/hoco_sg/game/Counter.java new file mode 100644 index 0000000..7f2ca71 --- /dev/null +++ b/src/main/java/net/flytre/hoco_sg/game/Counter.java @@ -0,0 +1,85 @@ +package net.flytre.hoco_sg.game; + +import net.flytre.flytre_lib.api.event.ClientTickEvents; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.text.Text; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.ListIterator; +import java.util.function.Consumer; + +/** + * A counter is used to count down in players' actionbars Mineplex-style, then execute code when it counts down to 0 + */ +public class Counter { + + public static final String FORMAT = "%.2f"; + private static final List ACTIVE_COUNTERS = new ArrayList<>(); + + static { + ClientTickEvents.START_CLIENT_TICK.register((__) -> { + + ListIterator iter = ACTIVE_COUNTERS.listIterator(); + while (iter.hasNext()) { + Counter counter = iter.next(); + String label = counter.nextTick(); + + + counter.players.forEach(i -> i.sendMessage(Text.of(label), true)); + + if (label.equals("")) { + if (counter.callback != null) + counter.callback.accept(counter.players); + iter.remove(); + } + } + }); + } + + private final int max; + private final int bars; + private final Collection players; + private final @Nullable Consumer> callback; + private int time; + + public Counter(Collection players, int max, int bars, @Nullable Consumer> callback) { + this.players = players; + this.max = max; + this.bars = bars; + this.time = 0; + this.callback = callback; + } + + public static void addActiveCounter(Counter counter) { + ACTIVE_COUNTERS.add(counter); + } + + /** + * percent increases from 0 - 100; + */ + public String nextTick() { + + float percent = time / (20f * max); + + if (percent > 1) + return ""; + + int green = (int) (percent * bars); + int red = bars - green; + + time++; + + return "[" + + "§2" + + "■".repeat(green) + + "§c" + + "■".repeat(red) + + "§r] " + + String.format(FORMAT, (max - percent * max)); + } + + +} diff --git a/src/main/java/net/flytre/hoco_sg/mixin/HungerManagerMixin.java b/src/main/java/net/flytre/hoco_sg/mixin/HungerManagerMixin.java new file mode 100644 index 0000000..0d4750a --- /dev/null +++ b/src/main/java/net/flytre/hoco_sg/mixin/HungerManagerMixin.java @@ -0,0 +1,44 @@ +package net.flytre.hoco_sg.mixin; + +import net.minecraft.entity.player.HungerManager; +import net.minecraft.entity.player.PlayerEntity; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Constant; +import org.spongepowered.asm.mixin.injection.ModifyConstant; +import org.spongepowered.asm.mixin.injection.Redirect; + + +/** + * Reverts healing to 1.11 style + */ +@Mixin(HungerManager.class) +public abstract class HungerManagerMixin { + + + /** + * Removes rapid health regen at full hunger by making the conditions required for it always false + */ + @Redirect(method = "update", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/PlayerEntity;canFoodHeal()Z", ordinal = 0)) + public boolean hoco_sg$oldRegen(PlayerEntity player) { + return false; + } + + + /** + * Makes it take 5 seconds between regenerating one health instead of 4 + */ + @ModifyConstant(method = "update", constant = @Constant(intValue = 80)) + public int hoco_sg$slowRegen(int eighty) { + return 100; + } + + /** + * Makes it take 2/3 of the exhaustion to heal a health to combat food scarcity. + */ + @ModifyConstant(method = "update", constant = @Constant(floatValue = 6.0f)) + public float hoco_sg$lessExhaustion(float six) { + return 4f; + } +} + diff --git a/src/main/java/net/flytre/hoco_sg/mixin/ItemsMixin.java b/src/main/java/net/flytre/hoco_sg/mixin/ItemsMixin.java new file mode 100644 index 0000000..10779db --- /dev/null +++ b/src/main/java/net/flytre/hoco_sg/mixin/ItemsMixin.java @@ -0,0 +1,43 @@ +package net.flytre.hoco_sg.mixin; + + +import net.flytre.hoco_sg.pvp.Tools; +import net.minecraft.item.*; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Redirect; + +/** + * Revert items to 1.8 style by replacing their attack damage and attack speed + */ +@Mixin(Items.class) +public class ItemsMixin { + + + @Redirect(method = "", at = @At(value = "NEW", target = "net/minecraft/item/ShovelItem")) + private static ShovelItem hoco_sg$fixShovel(ToolMaterial material, float attackDamage, float attackSpeed, Item.Settings settings) { + return new ShovelItem(material, 1, 1020.0f, settings); + } + + @Redirect(method = "", at = @At(value = "NEW", target = "net/minecraft/item/PickaxeItem")) + private static PickaxeItem hoco_sg$fixPickaxe(ToolMaterial material, int attackDamage, float attackSpeed, Item.Settings settings) { + return new Tools.Pickaxe(material, 2, 1020.0f, settings); + } + + @Redirect(method = "", at = @At(value = "NEW", target = "net/minecraft/item/AxeItem")) + private static AxeItem hoco_sg$fixAxe(ToolMaterial material, float attackDamage, float attackSpeed, Item.Settings settings) { + return new Tools.Axe(material, 3, 1020.0f, settings); + } + + + @Redirect(method = "", at = @At(value = "NEW", target = "net/minecraft/item/HoeItem")) + private static HoeItem hoco_sg$fixHoe(ToolMaterial material, int attackDamage, float attackSpeed, Item.Settings settings) { + return new Tools.Hoe(material, 0, 1020.0f, settings); + } + + @Redirect(method = "", at = @At(value = "NEW", target = "net/minecraft/item/SwordItem")) + private static SwordItem hoco_sg$fixSword(ToolMaterial material, int attackDamage, float attackSpeed, Item.Settings settings) { + return new SwordItem(material, 4, 1020.0f, settings); + } + +} diff --git a/src/main/java/net/flytre/hoco_sg/mixin/PlayerEntityMixin.java b/src/main/java/net/flytre/hoco_sg/mixin/PlayerEntityMixin.java new file mode 100644 index 0000000..dcf7050 --- /dev/null +++ b/src/main/java/net/flytre/hoco_sg/mixin/PlayerEntityMixin.java @@ -0,0 +1,31 @@ +package net.flytre.hoco_sg.mixin; + + +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.util.math.Box; +import net.minecraft.world.World; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.Redirect; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import java.util.ArrayList; +import java.util.List; + +/** + * Disable sweeping edge + */ +@Mixin(PlayerEntity.class) +public class PlayerEntityMixin { + + @Redirect(method = "attack", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;getNonSpectatingEntities(Ljava/lang/Class;Lnet/minecraft/util/math/Box;)Ljava/util/List;")) + public List hoco_sg$noSweep(World world, Class entityClass, Box box) { + return new ArrayList<>(); + } + + @Inject(method = "spawnSweepAttackParticles", at = @At("HEAD"), cancellable = true) + public void hoco_sg$noSweepParticles(CallbackInfo ci) { + ci.cancel(); + } +} diff --git a/src/main/java/net/flytre/hoco_sg/mixin/PlayerListS2CPacketMixin.java b/src/main/java/net/flytre/hoco_sg/mixin/PlayerListS2CPacketMixin.java new file mode 100644 index 0000000..82d0456 --- /dev/null +++ b/src/main/java/net/flytre/hoco_sg/mixin/PlayerListS2CPacketMixin.java @@ -0,0 +1,67 @@ +package net.flytre.hoco_sg.mixin; + + +import com.mojang.authlib.GameProfile; +import net.flytre.hoco_sg.*; +import net.flytre.hoco_sg.skin.GameProfileBuilder; +import net.flytre.hoco_sg.skin.KnownRecipientPacket; +import net.flytre.hoco_sg.skin.Skins; +import net.minecraft.network.packet.s2c.play.PlayerListS2CPacket; +import net.minecraft.server.network.ServerPlayerEntity; +import org.jetbrains.annotations.Nullable; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; + +import java.util.List; +import java.util.ListIterator; + + +/** + * Modify player info packets sent to the client to convey custom skins / names + */ +@Mixin(value = PlayerListS2CPacket.class) +public abstract class PlayerListS2CPacketMixin implements KnownRecipientPacket { + + @Shadow + @Final + private List entries; + private ServerPlayerEntity recipient = null; + + @Shadow + public abstract PlayerListS2CPacket.Action getAction(); + + @Override + public void setRecipient(ServerPlayerEntity recipient) { + this.recipient = recipient; + } + + public void modifyForRecipient() { + + if (getAction() != PlayerListS2CPacket.Action.ADD_PLAYER) + return; + + if (HocoSgInitializer.HANDLER.getConfig().admins.contains(recipient.getEntityName().toLowerCase())) { + return; + } + + ListIterator iter = entries.listIterator(); + + + while (iter.hasNext()) { + PlayerListS2CPacket.Entry entry = iter.next(); + String packetPlayer = entry.getProfile().getName().toLowerCase(); + @Nullable var recipientGroup = Config.getGroup(recipient); + boolean same = (recipient != null && packetPlayer.equals(recipient.getEntityName().toLowerCase())) || (recipientGroup != null && recipientGroup.contains(packetPlayer)); + + if (!same) { //If not in group, remove the entry and replace it with an equivalent profile with the mystery skin + GameProfile cloned = GameProfileBuilder.cloneWithName(entry.getProfile(), "???"); + cloned.getProperties().removeAll("textures"); + cloned.getProperties().putAll("textures", Skins.MYSTERY); + iter.remove(); + iter.add(new PlayerListS2CPacket.Entry(cloned, entry.getLatency(), entry.getGameMode(), entry.getDisplayName())); + } + } + + } +} diff --git a/src/main/java/net/flytre/hoco_sg/mixin/PlayerManagerMixin.java b/src/main/java/net/flytre/hoco_sg/mixin/PlayerManagerMixin.java new file mode 100644 index 0000000..0b8ec93 --- /dev/null +++ b/src/main/java/net/flytre/hoco_sg/mixin/PlayerManagerMixin.java @@ -0,0 +1,265 @@ +package net.flytre.hoco_sg.mixin; + + +import com.mojang.authlib.GameProfile; +import com.mojang.serialization.DataResult; +import com.mojang.serialization.Dynamic; +import io.netty.buffer.Unpooled; +import net.flytre.hoco_sg.skin.KnownRecipientPacket; +import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityType; +import net.minecraft.entity.effect.StatusEffectInstance; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.nbt.NbtOps; +import net.minecraft.network.ClientConnection; +import net.minecraft.network.MessageType; +import net.minecraft.network.Packet; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.network.packet.s2c.play.*; +import net.minecraft.scoreboard.AbstractTeam; +import net.minecraft.scoreboard.ServerScoreboard; +import net.minecraft.scoreboard.Team; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.PlayerManager; +import net.minecraft.server.network.ServerPlayNetworkHandler; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.server.world.ServerWorld; +import net.minecraft.text.Text; +import net.minecraft.text.TranslatableText; +import net.minecraft.util.Formatting; +import net.minecraft.util.UserCache; +import net.minecraft.util.Util; +import net.minecraft.util.registry.DynamicRegistryManager; +import net.minecraft.util.registry.RegistryKey; +import net.minecraft.world.GameRules; +import net.minecraft.world.World; +import net.minecraft.world.WorldProperties; +import net.minecraft.world.biome.source.BiomeAccess; +import net.minecraft.world.dimension.DimensionType; +import org.apache.logging.log4j.Logger; +import org.jetbrains.annotations.Nullable; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Overwrite; +import org.spongepowered.asm.mixin.Shadow; + +import java.util.*; + +/** + * Overwrite the onPlayerConnect() method to send the modified custom packets (skins/name) rather than default ones + */ +@Mixin(value = PlayerManager.class, priority = 1) +public abstract class PlayerManagerMixin { + + @Shadow + @Final + private static Logger LOGGER; + @Shadow + @Final + private List players; + @Shadow + @Final + private MinecraftServer server; + @Shadow + @Final + private Map playerMap; + @Shadow + @Final + private DynamicRegistryManager.Impl registryManager; + @Shadow + private int viewDistance; + + @Shadow + @Nullable + public abstract NbtCompound loadPlayerData(ServerPlayerEntity player); + + @Shadow + public abstract void sendCommandTree(ServerPlayerEntity player); + + @Shadow + protected abstract void sendScoreboard(ServerScoreboard scoreboard, ServerPlayerEntity player); + + @Shadow + public abstract void broadcastChatMessage(Text message, MessageType type, UUID sender); + + @Shadow + public abstract void sendToAll(Packet packet); + + @Shadow + public abstract void sendWorldInfo(ServerPlayerEntity player, ServerWorld world); + + @Shadow + public abstract int getMaxPlayerCount(); + + @Shadow + public abstract MinecraftServer getServer(); + + /** + * @author Flytre + */ + @SuppressWarnings("ConstantConditions") + @Overwrite + public void onPlayerConnect(ClientConnection connection, ServerPlayerEntity player) { + GameProfile gameProfile = player.getGameProfile(); + UserCache userCache = this.server.getUserCache(); + Optional optional = userCache.getByUuid(gameProfile.getId()); + String string = optional.map(GameProfile::getName).orElse(gameProfile.getName()); + userCache.add(gameProfile); + NbtCompound nbtCompound = this.loadPlayerData(player); + RegistryKey var23; + if (nbtCompound != null) { + DataResult> var10000 = DimensionType.worldFromDimensionNbt(new Dynamic<>(NbtOps.INSTANCE, nbtCompound.get("Dimension"))); + Logger var10001 = LOGGER; + Objects.requireNonNull(var10001); + var23 = var10000.resultOrPartial(var10001::error).orElse(World.OVERWORLD); + } else { + var23 = World.OVERWORLD; + } + + RegistryKey registryKey = var23; + ServerWorld serverWorld = this.server.getWorld(registryKey); + ServerWorld serverWorld3; + if (serverWorld == null) { + LOGGER.warn("Unknown respawn dimension {}, defaulting to overworld", registryKey); + serverWorld3 = this.server.getOverworld(); + } else { + serverWorld3 = serverWorld; + } + + player.setWorld(serverWorld3); + String string2 = "local"; + if (connection.getAddress() != null) { + string2 = connection.getAddress().toString(); + } + + LOGGER.info("{}[{}] logged in with entity id {} at ({}, {}, {})", player.getName().getString(), string2, player.getId(), player.getX(), player.getY(), player.getZ()); + WorldProperties worldProperties = serverWorld3.getLevelProperties(); + player.setGameMode(nbtCompound); + ServerPlayNetworkHandler serverPlayNetworkHandler = new ServerPlayNetworkHandler(this.server, connection, player); + GameRules gameRules = serverWorld3.getGameRules(); + boolean bl = gameRules.getBoolean(GameRules.DO_IMMEDIATE_RESPAWN); + boolean bl2 = gameRules.getBoolean(GameRules.REDUCED_DEBUG_INFO); + serverPlayNetworkHandler.sendPacket(new GameJoinS2CPacket(player.getId(), player.interactionManager.getGameMode(), player.interactionManager.getPreviousGameMode(), BiomeAccess.hashSeed(serverWorld3.getSeed()), worldProperties.isHardcore(), this.server.getWorldRegistryKeys(), this.registryManager, serverWorld3.getDimension(), serverWorld3.getRegistryKey(), this.getMaxPlayerCount(), this.viewDistance, bl2, !bl, serverWorld3.isDebugWorld(), serverWorld3.isFlat())); + serverPlayNetworkHandler.sendPacket(new CustomPayloadS2CPacket(CustomPayloadS2CPacket.BRAND, (new PacketByteBuf(Unpooled.buffer())).writeString(this.getServer().getServerModName()))); + serverPlayNetworkHandler.sendPacket(new DifficultyS2CPacket(worldProperties.getDifficulty(), worldProperties.isDifficultyLocked())); + serverPlayNetworkHandler.sendPacket(new PlayerAbilitiesS2CPacket(player.getAbilities())); + serverPlayNetworkHandler.sendPacket(new UpdateSelectedSlotS2CPacket(player.getInventory().selectedSlot)); + serverPlayNetworkHandler.sendPacket(new SynchronizeRecipesS2CPacket(this.server.getRecipeManager().values())); + serverPlayNetworkHandler.sendPacket(new SynchronizeTagsS2CPacket(this.server.getTagManager().toPacket(this.registryManager))); + this.sendCommandTree(player); + player.getStatHandler().updateStatSet(); + player.getRecipeBook().sendInitRecipesPacket(player); + this.sendScoreboard(serverWorld3.getScoreboard(), player); + this.server.forcePlayerSampleUpdate(); + TranslatableText mutableText2; + if (player.getGameProfile().getName().equalsIgnoreCase(string)) { + mutableText2 = new TranslatableText("multiplayer.player.joined", player.getDisplayName()); + } else { + mutableText2 = new TranslatableText("multiplayer.player.joined.renamed", player.getDisplayName(), string); + } + + this.broadcastChatMessage(mutableText2.formatted(Formatting.YELLOW), MessageType.SYSTEM, Util.NIL_UUID); + serverPlayNetworkHandler.requestTeleport(player.getX(), player.getY(), player.getZ(), player.getYaw(), player.getPitch()); + this.players.add(player); + this.playerMap.put(player.getUuid(), player); + + + //Send new join to all players + { + for (ServerPlayerEntity serverPlayerEntity : this.players) { + Packet packet = new PlayerListS2CPacket(PlayerListS2CPacket.Action.ADD_PLAYER, player); + ((KnownRecipientPacket) packet).setRecipient(serverPlayerEntity); + ((KnownRecipientPacket) packet).modifyForRecipient(); + serverPlayerEntity.networkHandler.sendPacket(packet); + } + } + + + //Send new join info about all players + { + for (ServerPlayerEntity serverPlayerEntity : this.players) { + Packet packet = new PlayerListS2CPacket(PlayerListS2CPacket.Action.ADD_PLAYER, serverPlayerEntity); + ((KnownRecipientPacket) packet).setRecipient(player); + ((KnownRecipientPacket) packet).modifyForRecipient(); + player.networkHandler.sendPacket(packet); + } + } + + serverWorld3.onPlayerConnected(player); + this.server.getBossBarManager().onPlayerConnect(player); + this.sendWorldInfo(player, serverWorld3); + if (!this.server.getResourcePackUrl().isEmpty()) { + player.sendResourcePackUrl(this.server.getResourcePackUrl(), this.server.getResourcePackHash(), this.server.requireResourcePack(), this.server.getResourcePackPrompt()); + } + + for (StatusEffectInstance statusEffectInstance : player.getStatusEffects()) { + serverPlayNetworkHandler.sendPacket(new EntityStatusEffectS2CPacket(player.getId(), statusEffectInstance)); + } + + if (nbtCompound != null && nbtCompound.contains("RootVehicle", 10)) { + NbtCompound nbtCompound2 = nbtCompound.getCompound("RootVehicle"); + Entity entity = EntityType.loadEntityWithPassengers(nbtCompound2.getCompound("Entity"), serverWorld3, (vehicle) -> !serverWorld3.tryLoadEntity(vehicle) ? null : vehicle); + if (entity != null) { + UUID uUID2; + if (nbtCompound2.containsUuid("Attach")) { + uUID2 = nbtCompound2.getUuid("Attach"); + } else { + uUID2 = null; + } + + Iterator var21; + Entity entity3; + if (entity.getUuid().equals(uUID2)) { + player.startRiding(entity, true); + } else { + var21 = entity.getPassengersDeep().iterator(); + + while (var21.hasNext()) { + entity3 = (Entity) var21.next(); + if (entity3.getUuid().equals(uUID2)) { + player.startRiding(entity3, true); + break; + } + } + } + + if (!player.hasVehicle()) { + LOGGER.warn("Couldn't reattach entity to player"); + entity.discard(); + var21 = entity.getPassengersDeep().iterator(); + + while (var21.hasNext()) { + entity3 = (Entity) var21.next(); + entity3.discard(); + } + } + } + } + + player.onSpawn(); + + //Send new join to all players + { + for (ServerPlayerEntity serverPlayerEntity : this.players) { + Packet packet = new PlayerListS2CPacket(PlayerListS2CPacket.Action.ADD_PLAYER, player); + ((KnownRecipientPacket) packet).setRecipient(serverPlayerEntity); + ((KnownRecipientPacket) packet).modifyForRecipient(); + serverPlayerEntity.networkHandler.sendPacket(packet); + } + } + + + //hide nametags + { + ServerScoreboard scoreboard = server.getScoreboard(); + Team team = scoreboard.getTeam("hide"); + if (team == null) { + team = scoreboard.addTeam("hide"); + team.setNameTagVisibilityRule(AbstractTeam.VisibilityRule.NEVER); + scoreboard.addPlayerToTeam("???", team); + } + scoreboard.addPlayerToTeam(player.getEntityName(), team); + } + } + +} diff --git a/src/main/java/net/flytre/hoco_sg/mixin/ServerPlayerEntityMixin.java b/src/main/java/net/flytre/hoco_sg/mixin/ServerPlayerEntityMixin.java new file mode 100644 index 0000000..6e45d13 --- /dev/null +++ b/src/main/java/net/flytre/hoco_sg/mixin/ServerPlayerEntityMixin.java @@ -0,0 +1,68 @@ +package net.flytre.hoco_sg.mixin; + + +import com.mojang.authlib.GameProfile; +import net.flytre.hoco_sg.Config; +import net.minecraft.entity.damage.DamageSource; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.text.LiteralText; +import net.minecraft.text.Text; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import java.util.List; +import java.util.Set; + +/** + * Prevent grouping by damage players that have been grouped for too long + */ +@Mixin(ServerPlayerEntity.class) +public abstract class ServerPlayerEntityMixin extends PlayerEntity { + + + private int teamingCd = 0; + + public ServerPlayerEntityMixin(World world, BlockPos pos, float yaw, GameProfile profile) { + super(world, pos, yaw, profile); + } + + @Shadow + public abstract void sendMessage(Text message, boolean actionBar); + + @Shadow + public abstract boolean damage(DamageSource source, float amount); + + @Shadow private int joinInvulnerabilityTicks; + + @Inject(method = "tick", at = @At("HEAD")) + public void hocoSg$antiMob(CallbackInfo ci) { + List players = world.getEntitiesByClass(PlayerEntity.class, this.getBoundingBox().expand(12), i -> i != this && !i.isSpectator()); + Set group = Config.getGroup(this); + + boolean teaming = false; + if (group != null) { + long num = players.stream().filter(i -> group.contains(i.getEntityName().toLowerCase())).count(); + + if (num > 2) { + teaming = true; + teamingCd++; + sendMessage(new LiteralText("§cWarning! You are too close to too many teammates."), true); + + if (teamingCd > 200) { + this.setHealth(getHealth() - 0.04f); + this.joinInvulnerabilityTicks = 0; + this.timeUntilRegen = 0; + } + } + } + + if (!teaming && teamingCd >= 5) + teamingCd -= 5; + } +} diff --git a/src/main/java/net/flytre/hoco_sg/pvp/Tools.java b/src/main/java/net/flytre/hoco_sg/pvp/Tools.java new file mode 100644 index 0000000..8796e7e --- /dev/null +++ b/src/main/java/net/flytre/hoco_sg/pvp/Tools.java @@ -0,0 +1,33 @@ +package net.flytre.hoco_sg.pvp; + + +import net.minecraft.item.*; + +/** + * Used because some tools have protected constructors + */ +public class Tools { + + + public static class Pickaxe extends PickaxeItem { + + public Pickaxe(ToolMaterial material, int attackDamage, float attackSpeed, Settings settings) { + super(material, attackDamage, attackSpeed, settings); + } + } + + + public static class Axe extends AxeItem { + + public Axe(ToolMaterial material, float attackDamage, float attackSpeed, Settings settings) { + super(material, attackDamage, attackSpeed, settings); + } + } + + public static class Hoe extends HoeItem { + public Hoe(ToolMaterial material, int attackDamage, float attackSpeed, Settings settings) { + super(material, attackDamage, attackSpeed, settings); + } + } + +} diff --git a/src/main/java/net/flytre/hoco_sg/skin/GameProfileBuilder.java b/src/main/java/net/flytre/hoco_sg/skin/GameProfileBuilder.java new file mode 100644 index 0000000..12d3121 --- /dev/null +++ b/src/main/java/net/flytre/hoco_sg/skin/GameProfileBuilder.java @@ -0,0 +1,192 @@ +package net.flytre.hoco_sg.skin; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.lang.reflect.Type; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.*; +import java.util.Map.Entry; +import java.util.stream.Collectors; + +import biz.source_code.base64Coder.Base64Coder; +import com.google.gson.*; +import com.mojang.authlib.GameProfile; +import com.mojang.authlib.properties.Property; +import com.mojang.authlib.properties.PropertyMap; +import com.mojang.util.UUIDTypeAdapter; + +/** + * Copied from google. Mostly unused and is for experimental features. + * + * Has one useful method I added, which clones a game profile + */ +public class GameProfileBuilder { + + private static final String SERVICE_URL = "https://sessionserver.mojang.com/session/minecraft/profile/%s?unsigned=false"; + private static final String JSON_SKIN = "{\"timestamp\":%d,\"profileId\":\"%s\",\"profileName\":\"%s\",\"isPublic\":true,\"textures\":{\"SKIN\":{\"url\":\"%s\"}}}"; + private static final String JSON_CAPE = "{\"timestamp\":%d,\"profileId\":\"%s\",\"profileName\":\"%s\",\"isPublic\":true,\"textures\":{\"SKIN\":{\"url\":\"%s\"},\"CAPE\":{\"url\":\"%s\"}}}"; + + private static final Gson gson = new GsonBuilder().disableHtmlEscaping().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).registerTypeAdapter(GameProfile.class, new GameProfileSerializer()).registerTypeAdapter(PropertyMap.class, new PropertyMap.Serializer()).create(); + + private static final HashMap cache = new HashMap<>(); + private static long cacheTime = -1; + + /** + * Don't run in main thread! + *

+ * Fetches the GameProfile from the Mojang servers + * + * @param uuid The player uuid + * @return The GameProfile + * @throws IOException If something wents wrong while fetching + * @see GameProfile + */ + public static GameProfile fetch(UUID uuid) throws IOException { + return fetch(uuid, false); + } + + /** + * Don't run in main thread! + *

+ * Fetches the GameProfile from the Mojang servers + * + * @param uuid The player uuid + * @param forceNew If true the cache is ignored + * @return The GameProfile + * @throws IOException If something wents wrong while fetching + * @see GameProfile + */ + public static GameProfile fetch(UUID uuid, boolean forceNew) throws IOException { + if (!forceNew && cache.containsKey(uuid) && cache.get(uuid).isValid()) { + return cache.get(uuid).profile; + } else { + HttpURLConnection connection = (HttpURLConnection) new URL(String.format(SERVICE_URL, UUIDTypeAdapter.fromUUID(uuid))).openConnection(); + connection.setReadTimeout(5000); + + if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { + String json = new String(connection.getInputStream().readAllBytes(), StandardCharsets.UTF_8); + GameProfile result = gson.fromJson(json, GameProfile.class); + cache.put(uuid, new CachedProfile(result)); + return result; + } else { + if (!forceNew && cache.containsKey(uuid)) { + return cache.get(uuid).profile; + } + JsonObject error = (JsonObject) new JsonParser().parse(new BufferedReader(new InputStreamReader(connection.getErrorStream())).readLine()); + throw new IOException(error.get("error").getAsString() + ": " + error.get("errorMessage").getAsString()); + } + } + } + + /** + * Builds a GameProfile for the specified args + * + * @param uuid The uuid + * @param name The name + * @param skin The url from the skin image + * @return A GameProfile built from the arguments + * @see GameProfile + */ + public static GameProfile getProfile(UUID uuid, String name, String skin) { + return getProfile(uuid, name, skin, null); + } + + /** + * Builds a GameProfile for the specified args + * + * @param uuid The uuid + * @param name The name + * @param skinUrl Url from the skin image + * @param capeUrl Url from the cape image + * @return A GameProfile built from the arguments + * @see GameProfile + */ + public static GameProfile getProfile(UUID uuid, String name, String skinUrl, String capeUrl) { + GameProfile profile = new GameProfile(uuid, name); + boolean cape = capeUrl != null && !capeUrl.isEmpty(); + + List args = new ArrayList(); + args.add(System.currentTimeMillis()); + args.add(UUIDTypeAdapter.fromUUID(uuid)); + args.add(name); + args.add(skinUrl); + if (cape) args.add(capeUrl); + + profile.getProperties().put("textures", new Property("textures", Base64Coder.encodeString(String.format(cape ? JSON_CAPE : JSON_SKIN, args.toArray(new Object[args.size()]))))); + return profile; + } + + /** + * Sets the time as long as you want to keep the gameprofiles in cache (-1 = never remove it) + * + * @param time cache time (default = -1) + */ + public static void setCacheTime(long time) { + cacheTime = time; + } + + public static GameProfile clone(GameProfile profile) { + return cloneWithName(profile, profile.getName()); + } + + public static GameProfile cloneWithName(GameProfile profile, String name) { + + GameProfile clone = new GameProfile(profile.getId(), name); + + PropertyMap cloneMap = clone.getProperties(); + + PropertyMap properties = profile.getProperties(); + properties.asMap().forEach((key, value) -> { + Set set = value.stream().map(property -> new Property(property.getName(), property.getValue(), property.getSignature())).collect(Collectors.toSet()); + cloneMap.putAll(key, set); + }); + + return clone; + } + + private static class GameProfileSerializer implements JsonSerializer, JsonDeserializer { + + public GameProfile deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { + JsonObject object = (JsonObject) json; + UUID id = object.has("id") ? (UUID) context.deserialize(object.get("id"), UUID.class) : null; + String name = object.has("name") ? object.getAsJsonPrimitive("name").getAsString() : null; + GameProfile profile = new GameProfile(id, name); + + if (object.has("properties")) { + for (Entry prop : ((PropertyMap) context.deserialize(object.get("properties"), PropertyMap.class)).entries()) { + profile.getProperties().put(prop.getKey(), prop.getValue()); + } + } + return profile; + } + + public JsonElement serialize(GameProfile profile, Type type, JsonSerializationContext context) { + JsonObject result = new JsonObject(); + if (profile.getId() != null) + result.add("id", context.serialize(profile.getId())); + if (profile.getName() != null) + result.addProperty("name", profile.getName()); + if (!profile.getProperties().isEmpty()) + result.add("properties", context.serialize(profile.getProperties())); + return result; + } + + } + + private static class CachedProfile { + + private final long timestamp = System.currentTimeMillis(); + private final GameProfile profile; + + public CachedProfile(GameProfile profile) { + this.profile = profile; + } + + public boolean isValid() { + return cacheTime < 0 || (System.currentTimeMillis() - timestamp) < cacheTime; + } + } +} \ No newline at end of file diff --git a/src/main/java/net/flytre/hoco_sg/skin/KnownRecipientPacket.java b/src/main/java/net/flytre/hoco_sg/skin/KnownRecipientPacket.java new file mode 100644 index 0000000..2d8cdfa --- /dev/null +++ b/src/main/java/net/flytre/hoco_sg/skin/KnownRecipientPacket.java @@ -0,0 +1,15 @@ +package net.flytre.hoco_sg.skin; + +import net.minecraft.server.network.ServerPlayerEntity; + + +/** + * Duck interface for modifying player list packets + */ +public interface KnownRecipientPacket { + + void setRecipient(ServerPlayerEntity player); + + + void modifyForRecipient(); +} diff --git a/src/main/java/net/flytre/hoco_sg/skin/Skins.java b/src/main/java/net/flytre/hoco_sg/skin/Skins.java new file mode 100644 index 0000000..d6a4250 --- /dev/null +++ b/src/main/java/net/flytre/hoco_sg/skin/Skins.java @@ -0,0 +1,48 @@ +package net.flytre.hoco_sg.skin; + +import com.mojang.authlib.GameProfile; +import com.mojang.authlib.properties.Property; +import net.flytre.hoco_sg.skin.GameProfileBuilder; +import org.jetbrains.annotations.ApiStatus; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Collection; +import java.util.UUID; + +public class Skins { + + private static final Property MYSTERY_LAYER = new Property("textures", "ewogICJ0aW1lc3RhbXAiIDogMTYzMDk2MDU2MTc2NywKICAicHJvZmlsZUlkIiA6ICIxNmQ3YzkzYmVkODU0Y2Y0ODVlMjIxODI3MjIzNWFiNiIsCiAgInByb2ZpbGVOYW1lIiA6ICJMbGZlU3VwcG9ydCIsCiAgInNpZ25hdHVyZVJlcXVpcmVkIiA6IHRydWUsCiAgInRleHR1cmVzIiA6IHsKICAgICJTS0lOIiA6IHsKICAgICAgInVybCIgOiAiaHR0cDovL3RleHR1cmVzLm1pbmVjcmFmdC5uZXQvdGV4dHVyZS9mZDZmMjRkYjMxZjcxNzI3ZGUyYTk4NmExNTMyZDRlZDUzYjU2YjAxYTQyMzE3M2JhZDJhMjhmZTBkNmU4YjkwIgogICAgfQogIH0KfQ==", "eM/L4QhkDmXa+nMPWZKTmHKNqkjisr0kFHxoHjbwAye1jpUBRA+xDQG93gZuC4q6JF/bYROlU6V7Y1cwWTRrm2odK5MwU44r6LjAZutt7ZChJObpou7ezG9ERw2I7X5vwLScy4U9C0GIH8cGyqGYbD+luP9tUfcqE4CHclp+v+kUeAZMn8GpnSzAwH/L8RFLLAZJv41mxVDTzmmrp3saEt0RitCw5ZiYeLo37MlIb6cGkmcEfmocSH5mPO2wKxe0tf+YVzFeP2OjFUzZ8TdNVDiHRX5+ywypt+9Q8GL/iIThMpIVqlPDLlqjdTCa9bu01GZR+7xIqyYEUPnWVf62+wrJ2i2+9ImbOlyei0jMBVhC/NGRSWBMsCSqD6Jj6zkp3m3qR36fYpi0qI2dqJrpyEMtxnTB2HPsMEs5nfrsLPYl4QtYOMdoVxl1WsNNTFH3j6K6SHKK2IrsvPU1Cwjdut8sp3eVaYE+x3csW5gJMhxl/DgEcXKHoToXZgi2616INbl5IQRWWbK0i1gi7GP1C5L0iMsmCUSpc7jCRU0h1o3XJqasT0qsAoYUKhkkYzYXQfH8aN4KsqAVIdQ1NC5gcufAF7ynN99TftHcixw4RnH9aXat5KTAvE6SAShaUDX0nuABDHdXI1wGB0r5mpfWOU2fG74rQGQj2OycY3awuz0="); + public static final Collection MYSTERY = Arrays.asList(MYSTERY_LAYER, MYSTERY_LAYER); + + + /** + * Debug only + */ + @ApiStatus.Internal + public static Collection fromUUID(UUID uuid) { + GameProfile skinData; + + try { + skinData = GameProfileBuilder.fetch(uuid); + } catch (IOException e) { + throw new AssertionError(e); + } + + + + skinData.getProperties().get("textures").forEach(i -> { + System.out.printf(""" + name: %s, + value: %s + sig: %s + """, i.getName(), i.getValue(), i.getSignature()); + + System.out.printf(""" + = new Property("%s", "%s", "%s"); + """, i.getName(), i.getValue(), i.getSignature()); + }); + + return skinData.getProperties().get("textures"); + } +} diff --git a/src/main/java/net/flytre/hoco_sg/skin/UUIDFetcher.java b/src/main/java/net/flytre/hoco_sg/skin/UUIDFetcher.java new file mode 100644 index 0000000..bb6fd12 --- /dev/null +++ b/src/main/java/net/flytre/hoco_sg/skin/UUIDFetcher.java @@ -0,0 +1,107 @@ +package net.flytre.hoco_sg.skin; + +import net.minecraft.entity.player.PlayerEntity; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.URL; +import java.net.URLConnection; +import java.nio.charset.Charset; +import java.util.UUID; + +/** + * Helper-class for getting UUIDs of players. + */ +public final class UUIDFetcher { + + private static final String UUID_URL = "https://api.mojang.com/users" + + "/profiles/minecraft/"; + + private UUIDFetcher() { + throw new UnsupportedOperationException(); + } + + /** + * Returns the UUID of the searched player. + * + * @param player The player. + * @return The UUID of the given player. + */ + //Uncomment this if you want the helper method for BungeeCord: + /* + public static UUID getUUID(ProxiedPlayer player) { + return getUUID(player.getName()); + } + */ + + /** + * Returns the UUID of the searched player. + * + * @param player The player. + * @return The UUID of the given player. + */ + //Uncomment this if you want the helper method for Bukkit/Spigot: + public static UUID getUUID(PlayerEntity player) { + return getUUID(player.getEntityName()); + } + + + /** + * Returns the UUID of the searched player. + * + * @param playername The name of the player. + * @return The UUID of the given player. + */ + public static UUID getUUID(String playername) { + String output = callURL(UUID_URL + playername); + StringBuilder result = new StringBuilder(); + readData(output, result); + String u = result.toString(); + StringBuilder uuid = new StringBuilder(); + for (int i = 0; i <= 31; i++) { + uuid.append(u.charAt(i)); + if (i == 7 || i == 11 || i == 15 || i == 19) { + uuid.append('-'); + } + } + return UUID.fromString(uuid.toString()); + } + + private static void readData(String toRead, StringBuilder result) { + for (int i = toRead.length() - 3; i >= 0; i--) { + if (toRead.charAt(i) != '"') { + result.insert(0, toRead.charAt(i)); + } else { + break; + } + } + } + + private static String callURL(String urlStr) { + StringBuilder sb = new StringBuilder(); + URLConnection urlConn; + InputStreamReader in; + try { + URL url = new URL(urlStr); + urlConn = url.openConnection(); + if (urlConn != null) { + urlConn.setReadTimeout(60 * 1000); + } + if (urlConn != null && urlConn.getInputStream() != null) { + in = new InputStreamReader(urlConn.getInputStream(), + Charset.defaultCharset()); + BufferedReader bufferedReader = new BufferedReader(in); + int cp; + while ((cp = bufferedReader.read()) != -1) { + sb.append((char) cp); + } + bufferedReader.close(); + in.close(); + } + } catch (Exception e) { + e.printStackTrace(); + } + return sb.toString(); + } + +} diff --git a/src/main/resources/assets/hoco_sg/textures/mystery.png b/src/main/resources/assets/hoco_sg/textures/mystery.png new file mode 100644 index 0000000..accd8c1 Binary files /dev/null and b/src/main/resources/assets/hoco_sg/textures/mystery.png differ diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json new file mode 100644 index 0000000..86f9017 --- /dev/null +++ b/src/main/resources/fabric.mod.json @@ -0,0 +1,29 @@ +{ + "schemaVersion": 1, + "id": "hoco_sg", + "version": "${version}", + "name": "Server Side Logic for Hoco", + "description": "", + "authors": [ + "Flytre" + ], + "contact": { + "homepage": "https://flytre.net/" + }, + "license": "All Rights Reserved", + "icon": "assets/modid/icon.png", + "environment": "*", + "entrypoints": { + "main": [ + "net.flytre.hoco_sg.HocoSgInitializer" + ] + }, + "mixins": [ + "hoco_sg.mixins.json" + ], + "depends": { + "fabricloader": ">=0.10.8", + "minecraft": "1.17.x", + "java": ">=16" + } +} diff --git a/src/main/resources/hoco_sg.mixins.json b/src/main/resources/hoco_sg.mixins.json new file mode 100644 index 0000000..294a6c7 --- /dev/null +++ b/src/main/resources/hoco_sg.mixins.json @@ -0,0 +1,13 @@ +{ + "required": true, + "minVersion": "0.8", + "package": "net.flytre.hoco_sg.mixin", + "compatibilityLevel": "JAVA_16", + "mixins": [ + "HungerManagerMixin", "ItemsMixin", "PlayerEntityMixin", "PlayerListS2CPacketMixin", "PlayerManagerMixin", + "ServerPlayerEntityMixin" + ], + "injectors": { + "defaultRequire": 1 + } +}