38 Commits

Author SHA1 Message Date
skippyall 948741f663 Overlays 2026-05-31 15:06:34 +02:00
skippyall d3c84ac000 More Errors 2026-05-30 14:34:43 +02:00
skippyall 7ed686d8d9 Error & Nullability Spam 2026-05-26 13:45:14 +02:00
skippyall f8eb1578b2 Back to the Future
Fun is still suspended though
2026-05-09 17:04:27 +02:00
skippyall 48a38b87c5 Rename .java to .kt 2026-05-09 17:04:27 +02:00
skippyall bd87a15cf2 You have no choice 2026-05-02 23:33:45 +02:00
skippyall f3c934c619 Gehen Sie zu Kotlin. Gehen Sie nicht über Los und ziehen Sie nicht 200 RM ein.
Migrate some classes to Kotlin, let's see if I will regret
2026-05-02 10:48:13 +02:00
skippyall 71016f9e70 Rename .java to .kt 2026-05-02 10:48:13 +02:00
skippyall 08f9763b83 Go Back! 2026-04-30 23:56:21 +02:00
skippyall e117139a63 Port to 26.1 2026-04-29 17:20:13 +02:00
skippyall f5202a4264 Mojang Mappings 2026-04-29 08:51:37 +02:00
skippyall cc9fedd63b Converted 2026-04-29 00:51:31 +02:00
skippyall 324fea04a9 Convert even more 2026-04-05 01:17:53 +02:00
skippyall 7acd083e79 Cast, Convert & more 2026-03-16 23:01:58 +01:00
skippyall 9b61dba4c7 Minion Config² 2026-02-07 23:44:50 +01:00
skippyall 0382798a5d Configured 2026-01-31 15:46:40 +01:00
skippyall 1e430ef506 Swap item instruction (for now) & freezin 2026-01-29 21:44:52 +01:00
skippyall 5bd7c08f83 Refactoring 2026-01-26 00:03:57 +01:00
skippyall 017b2ec590 Listening in both directions 2026-01-25 22:28:09 +01:00
skippyall 382b394523 Fixing & Listening 2026-01-18 00:52:13 +01:00
skippyall 3f2a52fd0a Listen up! 2026-01-05 13:12:40 +01:00
skippyall c5b3c883ca MinionTriggerBlock & More GUI 2025-12-09 20:00:57 +01:00
skippyall b413592abf Runtime & MinionTriggerBlock start 2025-11-06 22:48:20 +01:00
skippyall cc1320d5c6 Nuke some generics 2025-10-11 00:41:47 +02:00
skippyall da4351cff2 Commiting before Dingenskirchen 2025-10-08 00:04:21 +02:00
skippyall 640e6f801a Things 2025-09-24 20:27:40 +02:00
skippyall bcfb406547 Moving and stopping 2025-09-13 02:02:56 +02:00
skippyall b6f7bfc57b Redo everything 2025-09-12 00:59:57 +02:00
skippyall e27b1698e4 Urlaub 2025-09-12 00:57:15 +02:00
skippyall 216855d9c9 Merge remote-tracking branch 'origin/feature/command_rework' into feature/command_rework
# Conflicts:
#	src/main/java/io/github/skippyall/minions/gui/ModuleInventory.java
2025-07-06 14:50:08 +02:00
skippyall e42e3adf84 Update 2025-07-06 14:48:20 +02:00
skippyall 66cbad38cb Fixes 2025-07-06 12:49:16 +02:00
skippyall 6640f95291 Fixes 2025-07-02 00:27:49 +02:00
skippyall 362bf15a19 Update to 1.21.6 2025-06-21 00:52:12 +02:00
skippyall 2d64426496 ModuleInventory optimization 2025-06-20 23:39:13 +02:00
skippyall f5e0c1d426 Start Command rework 2025-06-11 11:28:43 +02:00
skippyall ec31e090b9 Creative Tab and Commands 2025-04-25 15:33:17 +02:00
skippyall 935b2225c9 Port to 1.21.5 2025-04-25 13:53:11 +02:00
301 changed files with 9929 additions and 2903 deletions
+2
View File
@@ -1,5 +1,7 @@
# User-specific stuff # User-specific stuff
.idea/ .idea/
urlaub/
.kotlin/
*.iml *.iml
*.ipr *.ipr
+1 -1
View File
@@ -1,6 +1,6 @@
The MIT License (MIT) The MIT License (MIT)
Copyright (c) 2024 Copyright (c) 2024 skippyall
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
+10
View File
@@ -0,0 +1,10 @@
Module ohne Instruction nicht bei Instruction Auswahl vorschlagen ✓
Mob Spawning durch Instruction an/aus
Referenz kopieren als Item, das beim linken verschwindet ✓
zurück-Button (Jain)
Instructions stoppen / einfrieren bei Entfernung des Moduls ✓
Verbindung mit Minion anzeigen ✓
Schlagen auf Entities fixen ✓
Signaländerung bei Trigger nach Tod des Minions mit Minion synchronisieren ✓
Menüführung für Argumente verbessern (Nö)
direkt Wert zeigen ✓
+35
View File
@@ -0,0 +1,35 @@
- Module:
- Bewegungsmodul ✓
- Angriffsmodul ✓
- Abbaumodul ✓
- Platziermodul / Verwendungsmodul ✓
- Werfmodul
- Inventarmanagementmodul (Noch nicht wirklich)
- Inventaropenmodul (Nä)
- Detektoren:
- Blockdetektor
- Entitydetektor
- Dimensiondetektor
- Positiondetektor
- Inventardetektor
- Programmieren: Nö
- Variablen Nö
- (Listen) Nö
- Warten Nö
- Wiederholen Nö
- Wiederhole bis/während Nö
- Wiederhole x mal/(für jedes Element) Nö
- Bedingung Nö
- VariableTypen:
- Integer ✓
- String ✓
- Position (Nö)
- Block (weisned)
- BlockType
- (NBT) Nö
- (World) Nö
- (Chunk) Nö
- Inventory (weisned)
-124
View File
@@ -1,124 +0,0 @@
plugins {
id 'fabric-loom' version '1.8-SNAPSHOT'
id 'maven-publish'
}
version = project.mod_version
group = project.maven_group
loom {
accessWidenerPath = file("src/main/resources/minions.accesswidener")
}
base {
archivesName = project.archives_base_name
}
loom {
accessWidenerPath = file("src/main/resources/minions.accesswidener")
}
repositories {
// Add repositories to retrieve artifacts from in here.
// You should only use this when depending on other mods because
// Loom adds the essential maven repositories to download Minecraft and libraries from automatically.
// See https://docs.gradle.org/current/userguide/declaring_repositories.html
// for more information about repositories.
maven { url 'https://maven.nucleoid.xyz' }
exclusiveContent {
forRepository {
maven {
name = "Modrinth"
url = "https://api.modrinth.com/maven"
}
}
filter {
includeGroup "maven.modrinth"
}
}
}
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. This is technically optional, but you probably want it anyway.
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
modImplementation "eu.pb4:polymer-core:${project.polymer_version}"
modImplementation include("eu.pb4:sgui:${project.sgui_version}")
modImplementation include("xyz.nucleoid:server-translations-api:${project.server_translations_version}")
modCompileOnly "maven.modrinth:universal-graves:${project.universal_graves_version}"
}
processResources {
inputs.property "version", project.version
inputs.property "minecraft_version", project.minecraft_version
inputs.property "loader_version", project.loader_version
filteringCharset "UTF-8"
filesMatching("fabric.mod.json") {
expand "version": project.version,
"minecraft_version": project.minecraft_version,
"loader_version": project.loader_version
}
}
def targetJavaVersion = 21
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"
if (targetJavaVersion >= 10 || JavaVersion.current().isJava10Compatible()) {
it.options.release.set(targetJavaVersion)
}
}
java {
def javaVersion = JavaVersion.toVersion(targetJavaVersion)
if (JavaVersion.current() < javaVersion) {
toolchain.languageVersion = JavaLanguageVersion.of(targetJavaVersion)
}
// 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 {
minions (MavenPublication) {
from components.java
}
}
// See https://docs.gradle.org/current/userguide/publishing_maven.html for information on how to set up publishing.
repositories {
// Add repositories to publish to here.
// Notice: This block does NOT have the same function as the block in the top level.
// The repositories here will be used for publishing your artifact, not for
// retrieving dependencies.
maven {
def release = "https://maven.foxgalaxy.de/private-releases"
def snapshot = "https://maven.foxgalaxy.de/private-snapshot"
url = version.endsWith('SNAPSHOT') ? snapshot : release
credentials {
username = "${maven_user}"
password = "${maven_password}"
}
}
}
}
+128
View File
@@ -0,0 +1,128 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
plugins {
id("net.fabricmc.fabric-loom")
`maven-publish`
id("org.jetbrains.kotlin.jvm") version "2.3.21"
}
version = providers.gradleProperty("mod_version").get()
group = providers.gradleProperty("maven_group").get()
repositories {
// Add repositories to retrieve artifacts from in here.
// You should only use this when depending on other mods because
// Loom adds the essential maven repositories to download Minecraft and libraries from automatically.
// See https://docs.gradle.org/current/userguide/declaring_repositories.html
// for more information about repositories.
maven("https://maven.nucleoid.xyz")
exclusiveContent {
forRepository {
maven ("https://api.modrinth.com/maven")
}
filter {
includeGroup("maven.modrinth")
}
}
}
loom {
splitEnvironmentSourceSets()
mods {
register("minions") {
sourceSet(sourceSets.main.get())
sourceSet(sourceSets.getByName("client"))
}
}
for (settings in runs) {
settings.vmArg("-XX:+AllowEnhancedClassRedefinition")
}
accessWidenerPath = file("src/main/resources/minions.classtweaker")
}
dependencies {
// To change the versions see the gradle.properties file
minecraft("com.mojang:minecraft:${providers.gradleProperty("minecraft_version").get()}")
implementation("net.fabricmc:fabric-loader:${providers.gradleProperty("loader_version").get()}")
// Fabric API. This is technically optional, but you probably want it anyway.
implementation("net.fabricmc.fabric-api:fabric-api:${providers.gradleProperty("fabric_api_version").get()}")
implementation("net.fabricmc:fabric-language-kotlin:${providers.gradleProperty("fabric_kotlin_version").get()}")
val polymer_version = providers.gradleProperty("polymer_version").get()
implementation("eu.pb4:polymer-core:${polymer_version}")
implementation("eu.pb4:polymer-virtual-entity:${polymer_version}")
implementation("eu.pb4:polymer-resource-pack:${polymer_version}")
localRuntime("eu.pb4:polymer-autohost:${polymer_version}")
implementation("eu.pb4:sgui:${providers.gradleProperty("sgui_version").get()}")
implementation("xyz.nucleoid:server-translations-api:${providers.gradleProperty("server_translations_version").get()}")
implementation("com.electronwill.night-config:toml:${providers.gradleProperty("night_config_version").get()}")
compileOnly("maven.modrinth:universal-graves:${providers.gradleProperty("universal_graves_version").get()}")
}
tasks.processResources {
val version = version
inputs.property("version", version)
filesMatching("fabric.mod.json") {
expand("version" to version)
}
}
tasks.withType<JavaCompile>().configureEach {
options.release = 25
}
kotlin {
compilerOptions {
jvmTarget = JvmTarget.JVM_25
}
}
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()
sourceCompatibility = JavaVersion.VERSION_25
targetCompatibility = JavaVersion.VERSION_25
}
tasks.jar {
val projectName = project.name
inputs.property("projectName", projectName)
from("LICENSE") {
rename { "${it}_$projectName" }
}
}
// configure the maven publication
publishing {
publications {
register<MavenPublication>("mavenJava") {
from(components["java"])
}
}
// See https://docs.gradle.org/current/userguide/publishing_maven.html for information on how to set up publishing.
repositories {
// Add repositories to publish to here.
// Notice: This block does NOT have the same function as the block in the top level.
// The repositories here will be used for publishing your artifact, not for
// retrieving dependencies.
maven("https://maven.foxgalaxy.de/private") {
name = "foxgalaxy"
credentials(PasswordCredentials::class.java)
}
}
}
+12 -9
View File
@@ -3,21 +3,24 @@ org.gradle.jvmargs=-Xmx1G
# Fabric Properties # Fabric Properties
# check these on https://modmuss50.me/fabric.html # check these on https://modmuss50.me/fabric.html
minecraft_version=1.21.4 loom_version=1.16-SNAPSHOT
loader_version=0.16.13 minecraft_version=26.1.2
yarn_mappings=1.21.4+build.8 loader_version=0.19.2
# Mod Properties # Mod Properties
mod_version = 0.1.2-SNAPSHOT mod_version = 1.0.0-TEST-1
maven_group = io.github.skippyall maven_group = io.github.skippyall
archives_base_name = Minions archives_base_name = Minions
# Dependencies # Dependencies
# check this on https://modmuss50.me/fabric.html # check this on https://modmuss50.me/fabric.html
fabric_version=0.115.1+1.21.4 fabric_api_version=0.147.0+26.1.2
fabric_kotlin_version=1.13.11+kotlin.2.3.21
polymer_version=0.11.5+1.21.4 polymer_version=0.16.3+26.1.2
sgui_version=1.8.2+1.21.4 sgui_version=2.0.0+26.1
server_translations_version=2.4.0+1.21.2-rc1 server_translations_version=3.0.3+26.1
universal_graves_version=3.5.0+1.21.2 night_config_version=3.8.3
universal_graves_version=3.11.0+26.1.2
Binary file not shown.
+7 -1
View File
@@ -1 +1,7 @@
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-9.4.1-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Vendored Executable
+248
View File
@@ -0,0 +1,248 @@
#!/bin/sh
#
# Copyright © 2015 the original 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.
#
# SPDX-License-Identifier: Apache-2.0
#
##############################################################################
#
# Gradle start up script for POSIX generated by Gradle.
#
# Important for running:
#
# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
# noncompliant, but you have some other compliant shell such as ksh or
# bash, then to run this script, type that shell name before the whole
# command line, like:
#
# ksh Gradle
#
# Busybox and similar reduced shells will NOT work, because this script
# requires all of these POSIX shell features:
# * functions;
# * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
# «${var#prefix}», «${var%suffix}», and «$( cmd )»;
# * compound commands having a testable exit status, especially «case»;
# * various built-in commands including «command», «set», and «ulimit».
#
# Important for patching:
#
# (2) This script targets any POSIX shell, so it avoids extensions provided
# by Bash, Ksh, etc; in particular arrays are avoided.
#
# The "traditional" practice of packing multiple parameters into a
# space-separated string is a well documented source of bugs and security
# problems, so this is (mostly) avoided, by progressively accumulating
# options in "$@", and eventually passing that to Java.
#
# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
# see the in-line comments for details.
#
# There are tweaks for specific operating systems such as AIX, CygWin,
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/2d6327017519d23b96af35865dc997fcb544fb40/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
#
##############################################################################
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
app_path=$0
# Need this for daisy-chained symlinks.
while
APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
[ -h "$app_path" ]
do
ls=$( ls -ld "$app_path" )
link=${ls#*' -> '}
case $link in #(
/*) app_path=$link ;; #(
*) app_path=$APP_HOME$link ;;
esac
done
# This is normally unused
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
warn () {
echo "$*"
} >&2
die () {
echo
echo "$*"
echo
exit 1
} >&2
# 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 ;; #(
MSYS* | MINGW* ) msys=true ;; #(
NONSTOP* ) nonstop=true ;;
esac
# 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
if ! command -v java >/dev/null 2>&1
then
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
fi
# Increase the maximum file descriptors if we can.
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC2039,SC3045
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC2039,SC3045
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
fi
# Collect all arguments for the java command, stacking in reverse order:
# * args from the command line
# * the main class name
# * -classpath
# * -D...appname settings
# * --module-path (only if needed)
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
# For Cygwin or MSYS, switch paths to Windows format before running java
if "$cygwin" || "$msys" ; then
APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
JAVACMD=$( cygpath --unix "$JAVACMD" )
# Now convert the arguments - kludge to limit ourselves to /bin/sh
for arg do
if
case $arg in #(
-*) false ;; # don't mess with options #(
/?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
[ -e "$t" ] ;; #(
*) false ;;
esac
then
arg=$( cygpath --path --ignore --mixed "$arg" )
fi
# Roll the args list around exactly as many times as the number of
# args, so each arg winds up back in the position where it started, but
# possibly modified.
#
# NB: a `for` loop captures its iteration list before it begins, so
# changing the positional parameters here affects neither the number of
# iterations, nor the values presented in `arg`.
shift # remove old arg
set -- "$@" "$arg" # push replacement arg
done
fi
# 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"'
# Collect all arguments for the java command:
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
# and any embedded shellness will be escaped.
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
# treated as '${Hostname}' itself on the command line.
set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
-jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \
"$@"
# Stop when "xargs" is not available.
if ! command -v xargs >/dev/null 2>&1
then
die "xargs is not available"
fi
# Use "xargs" to parse quoted args.
#
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
#
# In Bash we could simply go:
#
# readarray ARGS < <( xargs -n1 <<<"$var" ) &&
# set -- "${ARGS[@]}" "$@"
#
# but POSIX shell has neither arrays nor command substitution, so instead we
# post-process each arg (as a line of input to sed) to backslash-escape any
# character that might be a shell metacharacter, then use eval to reverse
# that process (while maintaining the separation between arguments), and wrap
# the whole thing up as a single "set" statement.
#
# This will of course break if any of these variables contains a newline or
# an unmatched quote.
#
eval "set -- $(
printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
xargs -n1 |
sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
tr '\n' ' '
)" '"$@"'
exec "$JAVACMD" "$@"
Vendored
+93
View File
@@ -0,0 +1,93 @@
@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
@rem SPDX-License-Identifier: Apache-2.0
@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=.
@rem This is normally unused
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% equ 0 goto execute
echo. 1>&2
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto execute
echo. 1>&2
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2
goto fail
:execute
@rem Setup the command line
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %*
:end
@rem End local scope for the variables with windows NT shell
if %ERRORLEVEL% equ 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!
set EXIT_CODE=%ERRORLEVEL%
if %EXIT_CODE% equ 0 set EXIT_CODE=1
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
exit /b %EXIT_CODE%
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega
+2
View File
@@ -0,0 +1,2 @@
#!/bin/bash
bash gradlew publish -PfoxgalaxyPassword=$(read -s -p "Enter Password: ")
-9
View File
@@ -1,9 +0,0 @@
pluginManagement {
repositories {
maven {
name = 'Fabric'
url = 'https://maven.fabricmc.net/'
}
gradlePluginPortal()
}
}
+17
View File
@@ -0,0 +1,17 @@
pluginManagement {
repositories {
maven {
name = "Fabric"
url = uri("https://maven.fabricmc.net/")
}
mavenCentral()
gradlePluginPortal()
}
plugins {
id("net.fabricmc.fabric-loom") version providers.gradleProperty("loom_version")
}
}
// Should match your modid
rootProject.name = "minions"
@@ -0,0 +1,12 @@
package io.github.skippyall.minions.client;
import eu.pb4.polymer.networking.api.client.PolymerClientNetworking;
import io.github.skippyall.minions.polymer.VersionSync;
import net.fabricmc.api.ClientModInitializer;
public class MinionsClient implements ClientModInitializer {
@Override
public void onInitializeClient() {
PolymerClientNetworking.registerCommonHandler(VersionSync.VersionSyncPayload.class, (client, handler, payload) -> {});
}
}
@@ -0,0 +1,49 @@
package io.github.skippyall.minions;
import net.fabricmc.loader.api.FabricLoader;
import org.jspecify.annotations.Nullable;
import org.objectweb.asm.tree.ClassNode;
import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin;
import org.spongepowered.asm.mixin.extensibility.IMixinInfo;
import java.util.List;
import java.util.Set;
public class MinionMixinConfigPlugin implements IMixinConfigPlugin {
@Override
public void onLoad(String mixinPackage) {
}
@Override
@Nullable
public String getRefMapperConfig() {
return null;
}
@Override
public boolean shouldApplyMixin(String targetClassName, String mixinClassName) {
if(mixinClassName.startsWith("io.github.skippyall.minions.mixins.compat.universal_graves.")) {
return MinionsConfig.get().compat.enableGravesCompat && FabricLoader.getInstance().isModLoaded("universal-graves");
}
if(mixinClassName.startsWith("io.github.skippyall.minions.mixins.antimobcap.")) {
return MinionsConfig.get().minion.enableMobCapHacks;
}
return true;
}
@Override
public void acceptTargets(Set<String> myTargets, Set<String> otherTargets) {}
@Override
@Nullable
public List<String> getMixins() {
return null;
}
@Override
public void preApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) {}
@Override
public void postApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) {}
}
@@ -1,70 +1,43 @@
package io.github.skippyall.minions; package io.github.skippyall.minions;
import eu.pb4.polymer.core.api.entity.PolymerEntityUtils; import io.github.skippyall.minions.command.MinionsCommand;
import eu.pb4.polymer.core.api.item.SimplePolymerItem; import io.github.skippyall.minions.docs.DocsManager;
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
import io.github.skippyall.minions.minion.MinionData;
import io.github.skippyall.minions.minion.MinionItem;
import io.github.skippyall.minions.minion.MinionPersistentState; import io.github.skippyall.minions.minion.MinionPersistentState;
import io.github.skippyall.minions.module.Modules; import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
import io.github.skippyall.minions.polymer.PolymerRegistration;
import io.github.skippyall.minions.registration.MinionRegistration;
import net.fabricmc.api.ModInitializer; import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; import net.fabricmc.fabric.api.resource.v1.ResourceLoader;
import net.minecraft.component.DataComponentTypes; import net.minecraft.resources.Identifier;
import net.minecraft.component.type.DamageResistantComponent; import net.minecraft.server.packs.PackType;
import net.minecraft.entity.boss.WitherEntity;
import net.minecraft.entity.damage.DamageType;
import net.minecraft.item.Item;
import net.minecraft.item.Items;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.tag.TagKey;
import net.minecraft.server.world.ChunkTicketManager;
import net.minecraft.util.Identifier;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
public class Minions implements ModInitializer { public class Minions implements ModInitializer {
public static final String MOD_ID = "minions"; public static final String MOD_ID = "minions";
public static final TagKey<DamageType> MINION_ITEM_RESISTS = TagKey.of(RegistryKeys.DAMAGE_TYPE, Identifier.of(MOD_ID, "minion_item_resists"));
public static final MinionItem MINION_ITEM = registerItem(Identifier.of(MOD_ID, "minion"), settings -> new MinionItem(settings.component(DataComponentTypes.DAMAGE_RESISTANT, new DamageResistantComponent(MINION_ITEM_RESISTS))));
public static final SimplePolymerItem BASIC_UPGRADE_BASE = registerItem(Identifier.of(MOD_ID, "basic_upgrade_base"), settings -> new SimplePolymerItem(settings, Items.NETHERITE_UPGRADE_SMITHING_TEMPLATE));
public static final SimplePolymerItem ADVANCED_UPGRADE_BASE = registerItem(Identifier.of(MOD_ID, "advanced_upgrade_base"), settings -> new SimplePolymerItem(settings, Items.NETHERITE_UPGRADE_SMITHING_TEMPLATE));
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID); public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
@Override @Override
public void onInitialize() { public void onInitialize() {
MinionData.register(); MinionsConfig.get();
PolymerEntityUtils.registerType();
MinionRegistration.register();
ServerLifecycleEvents.SERVER_STARTED.register(server -> { ServerLifecycleEvents.SERVER_STARTED.register(server -> {
MinionPersistentState.create(server); MinionPersistentState.get(server).getMinionData().forEach((uuid, data) -> {
MinionPersistentState.INSTANCE.getMinionData().forEach((uuid, data) -> {
if(data.isSpawned()) { if(data.isSpawned()) {
MinionFakePlayer.spawnMinion(data, server.getOverworld(), null, null); MinionFakePlayer.spawnMinion(data, server.overworld(), null, null, true);
} }
}); });
}); });
CommandRegistrationCallback.EVENT.register((commandDispatcher, commandRegistryAccess, registrationEnvironment) -> { CommandRegistrationCallback.EVENT.register(MinionsCommand::register);
MobCapCommand.registerCommand(commandDispatcher);
});
Modules.register(); PolymerRegistration.register();
}
private static <T extends Item> T registerItem(Identifier identifier, Function<Item.Settings, T> constructor, Item.Settings settings) { ResourceLoader.get(PackType.SERVER_DATA).registerReloadListener(Identifier.fromNamespaceAndPath(Minions.MOD_ID, "docs"), new DocsManager());
return Registry.register(Registries.ITEM, identifier, constructor.apply(settings.registryKey(RegistryKey.of(RegistryKeys.ITEM, identifier))));
}
private static <T extends Item> T registerItem(Identifier identifier, Function<Item.Settings, T> constructor) {
return registerItem(identifier, constructor, new Item.Settings());
} }
} }
@@ -0,0 +1,87 @@
package io.github.skippyall.minions;
import com.electronwill.nightconfig.core.CommentedConfig;
import com.electronwill.nightconfig.core.io.ParsingException;
import com.electronwill.nightconfig.core.io.WritingException;
import com.electronwill.nightconfig.core.io.WritingMode;
import com.electronwill.nightconfig.core.serde.ObjectDeserializer;
import com.electronwill.nightconfig.core.serde.ObjectSerializer;
import com.electronwill.nightconfig.core.serde.SerdeException;
import com.electronwill.nightconfig.core.serde.annotations.SerdeComment;
import com.electronwill.nightconfig.toml.TomlFormat;
import com.electronwill.nightconfig.toml.TomlParser;
import net.fabricmc.loader.api.FabricLoader;
import org.jspecify.annotations.Nullable;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class MinionsConfig {
private static @Nullable MinionsConfig INSTANCE;
public Minion minion = new Minion();
public static class Minion {
@SerdeComment("The prefix for all minion names")
public String minionPrefix = "+";
@SerdeComment("Makes minions not raise the mob cap if they can't spawn mobs.")
@SerdeComment("Might cause incompatibilities.")
public boolean enableMobCapHacks = true;
}
public Compat compat = new Compat();
public static class Compat {
@SerdeComment("Enables compat with Universal Graves, which allows everyone to pick up graves from minions")
public boolean enableGravesCompat = true;
}
private static Path getPath() {
Path minionsDir = FabricLoader.getInstance().getConfigDir().resolve("minions");
if(!Files.isDirectory(minionsDir)) {
try {
Files.createDirectory(minionsDir);
} catch (IOException e) {
Minions.LOGGER.error("Could not create config dir", e);
}
}
return minionsDir.resolve(Minions.MOD_ID + ".toml");
}
public static MinionsConfig get() {
if(INSTANCE == null) {
loadConfig();
}
return INSTANCE;
}
public static void loadConfig() {
try {
CommentedConfig defaultConfig = ObjectSerializer.standard().serializeFields(new MinionsConfig(), TomlFormat::newConfig);
CommentedConfig config = new TomlParser().parse(getPath(), (file, configFormat) -> {
configFormat.createWriter().write(defaultConfig, file, WritingMode.REPLACE);
return true;
});
//Always use default values when entries are missing
config.addAll(defaultConfig);
INSTANCE = ObjectDeserializer.standard().deserializeFields(config, MinionsConfig::new);
} catch (SerdeException | ParsingException | WritingException e) {
Minions.LOGGER.error("Error while reading config", e);
INSTANCE = new MinionsConfig();
}
}
/*public static void saveConfig() {
try {
CommentedConfig config = ObjectSerializer.standard().serializeFields(INSTANCE, TomlFormat::newConfig);
new TomlWriter().write(config, getPath(), WritingMode.REPLACE);
} catch (SerdeException | ParsingException | WritingException e) {
System.out.println("[minions] Error while writing config");
e.printStackTrace();
}
}*/
}
@@ -1,27 +0,0 @@
package io.github.skippyall.minions;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MinionsTickExecutor {
private static final List<Runnable> executeOnNextTick = Collections.synchronizedList(new ArrayList<>());
public static void register() {
ServerTickEvents.START_SERVER_TICK.register(server -> {
synchronized (executeOnNextTick) {
for (Runnable run:executeOnNextTick) {
run.run();
}
executeOnNextTick.clear();
}
});
}
public static void addExecuteOnNextTick(Runnable run) {
executeOnNextTick.add(run);
}
}
@@ -1,24 +0,0 @@
package io.github.skippyall.minions;
import com.mojang.brigadier.CommandDispatcher;
import io.github.skippyall.minions.mixinhelper.ChunkTicketManager$DistanceFromNearestPlayerTrackerAccessor;
import io.github.skippyall.minions.mixinhelper.ChunkTicketManagerAccessor;
import io.github.skippyall.minions.mixins.antimobcap.ServerChunkManagerAccessor;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.world.ChunkTicketManager;
import net.minecraft.text.Text;
import static net.minecraft.server.command.CommandManager.literal;
public class MobCapCommand {
public static void registerCommand(CommandDispatcher<ServerCommandSource> dispatcher) {
dispatcher.register(literal("mobcapdebug")
.executes(context -> {
ChunkTicketManager ticketManager = ((ServerChunkManagerAccessor)context.getSource().getWorld().getChunkManager()).getTicketManager();
int tickedChunkCount = ((ChunkTicketManager$DistanceFromNearestPlayerTrackerAccessor)((ChunkTicketManagerAccessor)ticketManager).getMinionless()).minions$getTickedChunkCount();
context.getSource().sendFeedback(() -> Text.of(String.valueOf(tickedChunkCount)), false);
return 0;
})
);
}
}
@@ -0,0 +1,24 @@
package io.github.skippyall.minions.block.input;
import io.github.skippyall.minions.clipboard.ClipboardItem;
import net.minecraft.core.BlockPos;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
public class AnalogInputBlock extends Block {
public AnalogInputBlock(Properties settings) {
super(settings);
}
@Override
protected InteractionResult useWithoutItem(BlockState state, Level world, BlockPos pos, Player player, BlockHitResult hit) {
if(!world.isClientSide()) {
player.getInventory().placeItemBackInInventory(ClipboardItem.createBlockPosReference(world, pos), true);
}
return InteractionResult.SUCCESS;
}
}
@@ -0,0 +1,4 @@
@NullMarked
package io.github.skippyall.minions.block.input;
import org.jspecify.annotations.NullMarked;
@@ -0,0 +1,65 @@
package io.github.skippyall.minions.block.instruction_bound;
import io.github.skippyall.minions.block.miniontrigger.MinionTriggerBlockEntity;
import io.github.skippyall.minions.clipboard.InstructionClipboard;
import io.github.skippyall.minions.minion.MinionPersistentState;
import io.github.skippyall.minions.registration.MinionBlocks;
import io.github.skippyall.minions.registration.MinionComponentTypes;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.game.ClientboundSoundPacket;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
public abstract class InstructionBoundBlock extends Block implements EntityBlock {
public InstructionBoundBlock(Properties settings) {
super(settings);
}
protected abstract BlockEntityType<? extends InstructionBoundBlockEntity<?>> getBlockEntityType();
@Override
protected InteractionResult useItemOn(ItemStack stack, BlockState state, Level world, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if(stack.get(MinionComponentTypes.REFERENCE) instanceof InstructionClipboard instruction && player instanceof ServerPlayer serverPlayer) {
world.getBlockEntity(pos, getBlockEntityType()).ifPresent(be -> {
be.setInstruction(instruction.selectedMinion(), instruction.selectedInstruction());
serverPlayer.connection.send(new ClientboundSoundPacket(SoundEvents.NOTE_BLOCK_CHIME, SoundSource.BLOCKS, pos.getX(), pos.getY(), pos.getZ(), 1, 1, 0));
stack.shrink(1);
});
return InteractionResult.SUCCESS;
}
return super.useItemOn(stack, state, world, pos, player, hand, hit);
}
@Override
protected InteractionResult useWithoutItem(BlockState state, Level world, BlockPos pos, Player player, BlockHitResult hit) {
if(world.isClientSide()) {
return InteractionResult.CONSUME;
}
world.getBlockEntity(pos, getBlockEntityType()).ifPresent(be -> {
String name = MinionPersistentState.get(world.getServer()).getMinionData(be.getMinionUuid()).getName();
player.sendSystemMessage(Component.translatable("minions.reference.instruction.tooltip", be.getInstructionName(), name));
});
return InteractionResult.SUCCESS;
}
@Override
protected void affectNeighborsAfterRemoval(BlockState state, ServerLevel world, BlockPos pos, boolean moved) {
super.affectNeighborsAfterRemoval(state, world, pos, moved);
world.getBlockEntity(pos, MinionBlocks.MINION_TRIGGER_BE_TYPE).ifPresent(MinionTriggerBlockEntity::removeListener);
}
}
@@ -0,0 +1,79 @@
package io.github.skippyall.minions.block.instruction_bound;
import io.github.skippyall.minions.listener.BlockEntityMinionListener;
import io.github.skippyall.minions.minion.MinionRuntime;
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
import io.github.skippyall.minions.program.instruction.ConfiguredInstruction;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import org.jspecify.annotations.Nullable;
import java.util.Optional;
import java.util.UUID;
public abstract class InstructionBoundBlockEntity<L extends BlockEntityMinionListener<?>> extends BlockEntity {
protected @Nullable UUID minionUuid;
protected String instructionName = "";
public InstructionBoundBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) {
super(type, pos, state);
}
protected abstract L createListener();
protected abstract Class<L> getListenerClass();
public void removeListener() {
if(level instanceof ServerLevel serverWorld) {
L listener = getListener();
if(listener != null) {
listener.remove(serverWorld.getServer());
}
}
}
public void addListener() {
if(level instanceof ServerLevel serverWorld) {
L listener = createListener();
listener.add(serverWorld.getServer());
}
}
public void setInstruction(UUID minionUuid, String instructionName) {
removeListener();
this.minionUuid = minionUuid;
this.instructionName = instructionName;
addListener();
setChanged();
}
public Optional<MinionFakePlayer> getMinion() {
if(minionUuid != null && level != null && level.getPlayerByUUID(minionUuid) instanceof MinionFakePlayer minion) {
return Optional.of(minion);
}
return Optional.empty();
}
public @Nullable UUID getMinionUuid() {
return minionUuid;
}
public String getInstructionName() {
return instructionName;
}
public Optional<ConfiguredInstruction<MinionRuntime>> getInstruction(MinionFakePlayer minion) {
return Optional.ofNullable(minion.getInstructionManager().getInstruction(instructionName));
}
public Optional<ConfiguredInstruction<MinionRuntime>> getInstruction() {
return getMinion().flatMap(this::getInstruction);
}
public @Nullable L getListener() {
return BlockEntityMinionListener.getListener(level, worldPosition, minionUuid, getListenerClass());
}
}
@@ -0,0 +1,4 @@
@NullMarked
package io.github.skippyall.minions.block.instruction_bound;
import org.jspecify.annotations.NullMarked;
@@ -0,0 +1,90 @@
package io.github.skippyall.minions.block.miniontrigger;
import com.mojang.serialization.MapCodec;
import io.github.skippyall.minions.block.instruction_bound.InstructionBoundBlock;
import io.github.skippyall.minions.registration.MinionBlocks;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.SupportType;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.redstone.Orientation;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
public class MinionTriggerBlock extends InstructionBoundBlock {
public static final MapCodec<MinionTriggerBlock> CODEC = simpleCodec(MinionTriggerBlock::new);
public static final BooleanProperty POWERED = BooleanProperty.create("powered");
public static final VoxelShape SHAPE = Block.column(16.0, 0.0, 2.0);
public MinionTriggerBlock(Properties settings) {
super(settings);
registerDefaultState(defaultBlockState().setValue(POWERED, false));
}
@Override
protected VoxelShape getShape(BlockState state, BlockGetter world, BlockPos pos, CollisionContext context) {
return SHAPE;
}
@Override
protected boolean canSurvive(BlockState state, LevelReader world, BlockPos pos) {
BlockPos blockPos = pos.below();
return this.canPlaceAbove(world, blockPos, world.getBlockState(blockPos));
}
protected boolean canPlaceAbove(LevelReader world, BlockPos pos, BlockState state) {
return state.isFaceSturdy(world, pos, Direction.UP, SupportType.RIGID);
}
@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(POWERED);
}
@Override
@NullMarked
protected void neighborChanged(BlockState state, Level world, BlockPos pos, Block sourceBlock, @Nullable Orientation wireOrientation, boolean notify) {
if(!canSurvive(state, world, pos)) {
dropResources(state, world, pos);
world.removeBlock(pos, false);
return;
}
boolean newPower = world.hasNeighborSignal(pos);
if(state.getValue(POWERED) != newPower) {
world.setBlockAndUpdate(pos, state.setValue(POWERED, newPower));
world.getBlockEntity(pos, MinionBlocks.MINION_TRIGGER_BE_TYPE).ifPresent(MinionTriggerBlockEntity::updatePower);
}
}
@Override
protected boolean hasAnalogOutputSignal(BlockState state) {
return true;
}
@Override
protected int getAnalogOutputSignal(BlockState state, Level world, BlockPos pos, Direction direction) {
return world.getBlockEntity(pos, MinionBlocks.MINION_TRIGGER_BE_TYPE).map(MinionTriggerBlockEntity::getComparatorOutput).orElse(0);
}
@Override
public @Nullable BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new MinionTriggerBlockEntity(pos, state);
}
@Override
protected BlockEntityType<MinionTriggerBlockEntity> getBlockEntityType() {
return MinionBlocks.MINION_TRIGGER_BE_TYPE;
}
}
@@ -0,0 +1,66 @@
package io.github.skippyall.minions.block.miniontrigger;
import io.github.skippyall.minions.block.instruction_bound.InstructionBoundBlockEntity;
import io.github.skippyall.minions.registration.MinionBlocks;
import net.minecraft.core.BlockPos;
import net.minecraft.core.UUIDUtil;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.storage.ValueInput;
import net.minecraft.world.level.storage.ValueOutput;
public class MinionTriggerBlockEntity extends InstructionBoundBlockEntity<MinionTriggerMinionListener> {
public MinionTriggerBlockEntity(BlockPos pos, BlockState state) {
super(MinionBlocks.MINION_TRIGGER_BE_TYPE, pos, state);
}
@Override
protected MinionTriggerMinionListener createListener() {
return new MinionTriggerMinionListener(level.dimension(), worldPosition, minionUuid, instructionName);
}
@Override
protected Class<MinionTriggerMinionListener> getListenerClass() {
return MinionTriggerMinionListener.class;
}
public void updatePower() {
boolean powered = getBlockState().getValue(MinionTriggerBlock.POWERED);
MinionTriggerMinionListener listener = getListener();
if(listener != null) {
listener.incomingPowerCache = powered;
}
getMinion().ifPresent(minion -> {
getInstruction().ifPresent(instruction -> {
if(powered) {
instruction.run(minion.getInstructionManager());
} else {
instruction.stop(minion.getInstructionManager());
}
});
});
}
public int getComparatorOutput() {
MinionTriggerMinionListener listener = getListener();
if(listener != null && listener.runningCache) {
return 15;
}
return 0;
}
@Override
protected void loadAdditional(ValueInput view) {
minionUuid = view.read("minionUuid", UUIDUtil.AUTHLIB_CODEC).orElse(null);
instructionName = view.getStringOr("instructionName", "");
}
@Override
protected void saveAdditional(ValueOutput view) {
if(minionUuid != null) {
view.store("minionUuid", UUIDUtil.AUTHLIB_CODEC, minionUuid);
}
view.putString("instructionName", instructionName);
}
}
@@ -0,0 +1,114 @@
package io.github.skippyall.minions.block.miniontrigger;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import io.github.skippyall.minions.Minions;
import io.github.skippyall.minions.listener.BlockEntityMinionInstructionListener;
import io.github.skippyall.minions.minion.MinionRuntime;
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
import io.github.skippyall.minions.program.instruction.ConfiguredInstruction;
import io.github.skippyall.minions.program.instruction.ConfiguredInstructionListener;
import io.github.skippyall.minions.registration.MinionBlocks;
import net.minecraft.core.BlockPos;
import net.minecraft.core.UUIDUtil;
import net.minecraft.resources.Identifier;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.level.Level;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
public class MinionTriggerMinionListener extends BlockEntityMinionInstructionListener<MinionTriggerBlockEntity> {
public static final Codec<MinionTriggerMinionListener> CODEC = RecordCodecBuilder.create(instance ->
instance.group(
Level.RESOURCE_KEY_CODEC.fieldOf("world").forGetter(listener -> listener.worldKey),
BlockPos.CODEC.fieldOf("pos").forGetter(listener -> listener.pos),
UUIDUtil.AUTHLIB_CODEC.fieldOf("minionUuid").forGetter(listener -> listener.minionUuid),
Codec.STRING.fieldOf("instructionName").forGetter(listener -> listener.instructionName)
).apply(instance, MinionTriggerMinionListener::new));
String instructionName;
final TriggerInstructionListener listener = new TriggerInstructionListener();
boolean runningCache;
boolean incomingPowerCache;
MinionTriggerMinionListener(ResourceKey<Level> worldKey, BlockPos pos, UUID minionUuid, String instructionName) {
super(worldKey, pos, minionUuid, MinionBlocks.MINION_TRIGGER_BE_TYPE);
this.instructionName = Objects.requireNonNull(instructionName);
}
@Override
protected Map<String, ConfiguredInstructionListener> getInstructionListeners() {
return Map.of(instructionName, listener);
}
@Override
public void onMinionSpawn(MinionFakePlayer minion) {
super.onMinionSpawn(minion);
runningCache = minion.getInstructionManager().getInstruction(instructionName).isRunning();
updateComparatorsIfLoaded(minion.getServer());
ConfiguredInstruction<MinionRuntime> instruction = minion.getInstructionManager().getInstruction(instructionName);
if(instruction.isRunning() && !incomingPowerCache) {
instruction.stop(minion.getInstructionManager());
} else if (!instruction.isRunning() && incomingPowerCache) {
instruction.run(minion.getInstructionManager());
}
}
@Override
public void onMinionRemove(MinionFakePlayer minion) {
super.onMinionRemove(minion);
runningCache = false;
updateComparatorsIfLoaded(minion.getServer());
}
@Override
public void onInstructionRename(MinionFakePlayer minion, ConfiguredInstruction<?> instruction, String oldName, String newName) {
super.onInstructionRename(minion, instruction, oldName, newName);
if(instructionName.equals(oldName)) {
instructionName = newName;
}
}
@Override
public void add(MinecraftServer server) {
super.add(server);
runningCache = minion.getInstructionManager().getInstruction(instructionName).isRunning();
updateComparatorsIfLoaded(server);
}
@Override
public Optional<Identifier> getCodecId() {
return Optional.of(Identifier.fromNamespaceAndPath(Minions.MOD_ID, "minion_trigger"));
}
public void updateComparatorsIfLoaded(MinecraftServer server) {
Level world = server.getLevel(worldKey);
if(world.isLoaded(pos)) {
world.updateNeighbourForOutputSignal(pos, MinionBlocks.MINION_TRIGGER_BLOCK);
}
}
public boolean isRunning() {
return runningCache;
}
public class TriggerInstructionListener implements ConfiguredInstructionListener {
@Override
public void onRun(ConfiguredInstruction<?> instruction) {
runningCache = true;
updateComparatorsIfLoaded(minion.getServer());
}
@Override
public void onStop(ConfiguredInstruction<?> instruction) {
runningCache = false;
updateComparatorsIfLoaded(minion.getServer());
}
}
}
@@ -0,0 +1,4 @@
@NullMarked
package io.github.skippyall.minions.block.miniontrigger;
import org.jspecify.annotations.NullMarked;
@@ -0,0 +1,4 @@
@NullMarked
package io.github.skippyall.minions.block;
import org.jspecify.annotations.NullMarked;
@@ -0,0 +1,33 @@
package io.github.skippyall.minions.clipboard;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.core.BlockPos;
import net.minecraft.core.component.DataComponentGetter;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.Level;
import java.util.function.Consumer;
public record BlockPosClipboard(ResourceKey<Level> world, BlockPos pos) implements Clipboard {
public static final MapCodec<BlockPosClipboard> CODEC = RecordCodecBuilder.mapCodec(instance ->
instance.group(
Level.RESOURCE_KEY_CODEC.fieldOf("world").forGetter(BlockPosClipboard::world),
BlockPos.CODEC.fieldOf("pos").forGetter(BlockPosClipboard::pos)
).apply(instance, BlockPosClipboard::new)
);
@Override
public MapCodec<BlockPosClipboard> getCodec() {
return CODEC;
}
@Override
public void addToTooltip(Item.TooltipContext context, Consumer<Component> textConsumer, TooltipFlag type, DataComponentGetter components) {
textConsumer.accept(Component.translatable("minions.reference.block.tooltip", pos.toString()));
}
}
@@ -0,0 +1,14 @@
package io.github.skippyall.minions.clipboard;
import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import io.github.skippyall.minions.registration.MinionRegistries;
import net.minecraft.world.item.component.TooltipProvider;
import java.util.function.Function;
public interface Clipboard extends TooltipProvider {
Codec<Clipboard> CODEC = MinionRegistries.CLIPBOARD_TYPES.byNameCodec().dispatch(Clipboard::getCodec, Function.identity());
MapCodec<? extends Clipboard> getCodec();
}
@@ -0,0 +1,23 @@
package io.github.skippyall.minions.clipboard;
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
import io.github.skippyall.minions.registration.MinionComponentTypes;
import io.github.skippyall.minions.registration.MinionItems;
import net.minecraft.core.BlockPos;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
public class ClipboardItem {
public static ItemStack createInstructionReference(MinionFakePlayer minion, String instructionName) {
ItemStack stack = new ItemStack(MinionItems.REFERENCE_ITEM);
stack.set(MinionComponentTypes.REFERENCE, new InstructionClipboard(minion.getUUID(), instructionName, minion.getGameProfile().name()));
return stack;
}
public static ItemStack createBlockPosReference(Level world, BlockPos pos) {
ItemStack stack = new ItemStack(MinionItems.REFERENCE_ITEM);
stack.set(MinionComponentTypes.REFERENCE, new BlockPosClipboard(world.dimension(), pos));
return stack;
}
}
@@ -0,0 +1,32 @@
package io.github.skippyall.minions.clipboard;
import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.core.UUIDUtil;
import net.minecraft.core.component.DataComponentGetter;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.TooltipFlag;
import java.util.UUID;
import java.util.function.Consumer;
public record InstructionClipboard(UUID selectedMinion, String selectedInstruction, String visualMinionName) implements Clipboard {
public static final MapCodec<InstructionClipboard> CODEC = RecordCodecBuilder.mapCodec(instance ->
instance.group(
UUIDUtil.AUTHLIB_CODEC.fieldOf("selectedMinion").forGetter(InstructionClipboard::selectedMinion),
Codec.STRING.fieldOf("selectedInstruction").forGetter(InstructionClipboard::selectedInstruction),
Codec.STRING.fieldOf("visualMinionName").forGetter(InstructionClipboard::visualMinionName)
).apply(instance, InstructionClipboard::new));
@Override
public MapCodec<? extends Clipboard> getCodec() {
return CODEC;
}
@Override
public void addToTooltip(Item.TooltipContext context, Consumer<Component> textConsumer, TooltipFlag type, DataComponentGetter components) {
textConsumer.accept(Component.translatable("minions.reference.instruction.tooltip", selectedInstruction, visualMinionName));
}
}
@@ -0,0 +1,4 @@
@NullMarked
package io.github.skippyall.minions.clipboard;
import org.jspecify.annotations.NullMarked;
@@ -0,0 +1,36 @@
package io.github.skippyall.minions.command;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import io.github.skippyall.minions.docs.DocsManager;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.arguments.IdentifierArgument;
import net.minecraft.resources.Identifier;
import java.util.concurrent.CompletableFuture;
import static net.minecraft.commands.Commands.argument;
import static net.minecraft.commands.Commands.literal;
public class DocsSubcommand {
public static final LiteralArgumentBuilder<CommandSourceStack> DOCS = literal("docs")
.then(
argument("docName", IdentifierArgument.id())
.suggests(DocsSubcommand::getSuggestions)
.executes(DocsSubcommand::execute)
);
public static CompletableFuture<Suggestions> getSuggestions(CommandContext<CommandSourceStack> context, SuggestionsBuilder builder) {
DocsManager.getDocsEntryIds().forEach(id -> builder.suggest(id.toString()));
return CompletableFuture.completedFuture(builder.build());
}
public static int execute(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
Identifier id = IdentifierArgument.getId(context, "docName");
DocsManager.showDocsEntry(context.getSource().getPlayerOrException(), id);
return 1;
}
}
@@ -0,0 +1,25 @@
package io.github.skippyall.minions.command;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import io.github.skippyall.minions.minion.MinionData;
import io.github.skippyall.minions.minion.MinionPersistentState;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.network.chat.Component;
import java.util.Collection;
import static net.minecraft.commands.Commands.literal;
public class ListSubcommand {
public static final LiteralArgumentBuilder<CommandSourceStack> LIST = literal("list")
.executes(ListSubcommand::list);
public static int list(CommandContext<CommandSourceStack> context) {
Collection<MinionData> minions = MinionPersistentState.get(context.getSource().getServer()).getMinionData().values();
for (MinionData minion : minions) {
context.getSource().sendSuccess(() -> Component.literal(minion.getName() + "(" + minion.getUuid() + "):" + minion.isSpawned()), false);
}
return 0;
}
}
@@ -0,0 +1,50 @@
package io.github.skippyall.minions.command;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import com.mojang.brigadier.suggestion.SuggestionProvider;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import io.github.skippyall.minions.minion.MinionData;
import io.github.skippyall.minions.minion.MinionPersistentState;
import io.github.skippyall.minions.minion.MinionProfileUtils;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.network.chat.Component;
import net.minecraft.server.MinecraftServer;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
public class MinionArgument {
public static final SimpleCommandExceptionType MINION_NOT_PRESENT = new SimpleCommandExceptionType(Component.translatable("minions.command.minion.not_present"));
public static final MinionSuggestionProvider SUGGESTION_PROVIDER = new MinionSuggestionProvider();
public static MinionData parse(MinecraftServer server, String argument) throws CommandSyntaxException {
Optional<MinionData> data = Optional.empty();
if(argument.startsWith(MinionProfileUtils.getPrefix())) {
data = MinionPersistentState.get(server).getMinionWithName(argument);
} else {
try {
data = Optional.ofNullable(MinionPersistentState.get(server).getMinionData(UUID.fromString(argument)));
} catch (IllegalArgumentException ignored) {}
}
if(data.isEmpty()) {
throw MINION_NOT_PRESENT.create();
}
return data.get();
}
public static class MinionSuggestionProvider implements SuggestionProvider<CommandSourceStack> {
@Override
public CompletableFuture<Suggestions> getSuggestions(CommandContext<CommandSourceStack> context, SuggestionsBuilder builder) throws CommandSyntaxException {
for (MinionData data : MinionPersistentState.get(context.getSource().getServer()).getMinionDataList()) {
builder.suggest(data.getName());
}
return builder.buildFuture();
}
}
}
@@ -0,0 +1,28 @@
package io.github.skippyall.minions.command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import io.github.skippyall.minions.MinionsConfig;
import net.minecraft.commands.CommandBuildContext;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import static net.minecraft.commands.Commands.literal;
public class MinionsCommand {
public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext access, Commands.CommandSelection environment) {
LiteralArgumentBuilder<CommandSourceStack> builder = literal("minions")
.then(SpawnSubcommand.SPAWN)
.then(ListSubcommand.LIST)
.then(DocsSubcommand.DOCS)
.then(TestSubcommand.TEST);
if(MinionsConfig.get().minion.enableMobCapHacks) {
builder.then(MobCapDebugSubcommand.MOB_CAP_DEBUG);
}
dispatcher.register(
builder
);
}
}
@@ -0,0 +1,24 @@
package io.github.skippyall.minions.command;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import io.github.skippyall.minions.mixinhelper.antimobcap.ChunkLevelManager$DistanceFromNearestPlayerTrackerAccessor;
import io.github.skippyall.minions.mixinhelper.antimobcap.ChunkLevelManagerAccessor;
import io.github.skippyall.minions.mixins.antimobcap.ServerChunkCacheAccessor;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.DistanceManager;
import static net.minecraft.commands.Commands.literal;
public class MobCapDebugSubcommand {
public static final LiteralArgumentBuilder<CommandSourceStack> MOB_CAP_DEBUG = literal("mobcapdebug")
.executes(MobCapDebugSubcommand::mobcapdebugCommand);
public static int mobcapdebugCommand(CommandContext<CommandSourceStack> context) {
DistanceManager levelManager = ((ServerChunkCacheAccessor)context.getSource().getLevel().getChunkSource()).getDistanceManager();
int tickedChunkCount = ((ChunkLevelManager$DistanceFromNearestPlayerTrackerAccessor)((ChunkLevelManagerAccessor)levelManager).minions$getMinionless()).minions$getTickedChunkCount();
context.getSource().sendSuccess(() -> Component.nullToEmpty(String.valueOf(tickedChunkCount)), false);
return 0;
}
}
@@ -0,0 +1,57 @@
package io.github.skippyall.minions.command;
import com.mojang.brigadier.arguments.BoolArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import io.github.skippyall.minions.minion.MinionData;
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.arguments.coordinates.Coordinates;
import net.minecraft.commands.arguments.coordinates.Vec3Argument;
import net.minecraft.server.permissions.Permissions;
import org.jspecify.annotations.Nullable;
import static net.minecraft.commands.Commands.argument;
import static net.minecraft.commands.Commands.literal;
public class SpawnSubcommand {
public static final LiteralArgumentBuilder<CommandSourceStack> SPAWN = literal("spawn")
.requires(source -> source.permissions().hasPermission(Permissions.COMMANDS_GAMEMASTER))
.then(argument("minion", StringArgumentType.word())
.suggests(MinionArgument.SUGGESTION_PROVIDER)
.then(argument("pos", Vec3Argument.vec3())
.executes(context ->
spawnCommand(
context.getSource(),
StringArgumentType.getString(context, "minion"),
Vec3Argument.getCoordinates(context, "pos"),
false
)
)
.then(argument("force", BoolArgumentType.bool())
.executes(context ->
spawnCommand(
context.getSource(),
StringArgumentType.getString(context, "minion"),
Vec3Argument.getCoordinates(context, "pos"),
BoolArgumentType.getBool(context, "force")
)
)
)
)
.executes(context ->
spawnCommand(
context.getSource(),
StringArgumentType.getString(context, "minion"),
null,
false
))
);
public static int spawnCommand(CommandSourceStack source, String minion, @Nullable Coordinates pos, boolean force) throws CommandSyntaxException {
MinionData data = MinionArgument.parse(source.getServer(), minion);
MinionFakePlayer.spawnMinion(data, source.getLevel(), pos != null ? pos.getPosition(source) : null, pos != null ? pos.getRotation(source) : null, force);
return 0;
}
}
@@ -0,0 +1,78 @@
package io.github.skippyall.minions.command;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import io.github.skippyall.minions.Minions;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.arguments.coordinates.BlockPosArgument;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.network.chat.Component;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Blocks;
import java.util.Collection;
import java.util.HashSet;
import static net.minecraft.commands.Commands.argument;
import static net.minecraft.commands.Commands.literal;
public class TestSubcommand {
public static LiteralArgumentBuilder<CommandSourceStack> TEST = literal("test")
.then(argument("pos", BlockPosArgument.blockPos())
.executes(TestSubcommand::execute)
);
private static int execute(CommandContext<CommandSourceStack> context) {
try {
BlockPos pos = BlockPosArgument.getBlockPos(context, "pos");
Collection<BlockPos> result = findInputs(context.getSource().getLevel(), pos);
for (BlockPos resultPos : result) {
context.getSource().sendSuccess(() -> Component.literal(resultPos.toShortString()), false);
}
} catch (Throwable e) {
Minions.LOGGER.error("Error", e);
}
return 0;
}
private static Collection<BlockPos> findInputs(Level world, BlockPos pos) {
//positions that are already processed
Collection<BlockPos> visitedPositions = new HashSet<>();
//positions we are currently looking at
Collection<BlockPos> currentPositions = new HashSet<>();
//new positions are added here
Collection<BlockPos> newPositions = new HashSet<>();
//resulting machines
Collection<BlockPos> machines = new HashSet<>();
currentPositions.add(pos);
while(!currentPositions.isEmpty()) {
for(BlockPos currentPosition : currentPositions) {
for(Direction dir : Direction.values()) {
//check each neighbor of the current positions
BlockPos newPos = currentPosition.relative(dir);
//Do not check blocks that were already checked
if(!visitedPositions.contains(newPos)) {
if (world.getBlockState(newPos).getBlock() == Blocks.REDSTONE_BLOCK) {
//Add pipes to positions for the next iteration
newPositions.add(newPos);
} else if (world.getBlockState(newPos).getBlock() == Blocks.IRON_BLOCK) {
//Add machines to output set
machines.add(newPos);
}
//Add checked blocks here so that they are not checked again
visitedPositions.add(newPos);
}
}
}
//Check the new positions in the next iteration
currentPositions = newPositions;
newPositions = new HashSet<>();
}
return machines;
}
}
@@ -0,0 +1,4 @@
@NullMarked
package io.github.skippyall.minions.command;
import org.jspecify.annotations.NullMarked;
@@ -0,0 +1,29 @@
package io.github.skippyall.minions.docs;
import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import io.github.skippyall.minions.registration.MinionRegistries;
import net.minecraft.core.RegistryAccess;
import net.minecraft.server.dialog.body.DialogBody;
import java.util.List;
import java.util.function.Function;
public interface DocsEntry {
Codec<DocsEntry> CODEC = MinionRegistries.DOCS_ENTRY_TYPES.byNameCodec().dispatch(DocsEntry::getCodec, Function.identity());
Metadata getMetadata();
List<DialogBody> getDialog(RegistryAccess manager);
MapCodec<? extends DocsEntry> getCodec();
record Metadata(String titleKey) {
public static final MapCodec<Metadata> CODEC = RecordCodecBuilder.mapCodec(instance ->
instance.group(
Codec.STRING.fieldOf("title").forGetter(Metadata::titleKey)
).apply(instance, Metadata::new)
);
}
}
@@ -0,0 +1,140 @@
package io.github.skippyall.minions.docs;
import com.google.gson.JsonParseException;
import com.mojang.serialization.JsonOps;
import io.github.skippyall.minions.Minions;
import net.fabricmc.fabric.api.resource.v1.reloader.SimpleReloadListener;
import net.minecraft.core.Holder;
import net.minecraft.network.chat.ClickEvent;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.Identifier;
import net.minecraft.server.dialog.ActionButton;
import net.minecraft.server.dialog.CommonButtonData;
import net.minecraft.server.dialog.CommonDialogData;
import net.minecraft.server.dialog.DialogAction;
import net.minecraft.server.dialog.MultiActionDialog;
import net.minecraft.server.dialog.action.StaticAction;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.packs.resources.Resource;
import net.minecraft.util.StrictJsonParser;
import net.minecraft.util.Tuple;
import org.jspecify.annotations.Nullable;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public class DocsManager extends SimpleReloadListener<Tuple<Map<Identifier, DocsEntry>, DocsTree>> {
private static @Nullable Map<Identifier, DocsEntry> docs;
private static @Nullable DocsTree tree;
public static @Nullable DocsTree getTree() {
return tree;
}
public static void showDocsEntry(ServerPlayer player, Identifier id) {
DocsEntry entry = getDocsEntry(id);
if(entry == null) {
return;
}
List<ActionButton> buttons = new ArrayList<>();
if(tree != null) {
DocsTree.DocElement element = tree.getElement(id);
if (element.previous() != null) {
Identifier previousId = element.previous().getId();
buttons.add(getDialogButton(Component.literal("<- ").append(Component.translatable(getDocsEntry(previousId).getMetadata().titleKey())), previousId.toString()));
}
if (element.next() != null) {
Identifier nextId = element.next().getId();
buttons.add(getDialogButton(Component.translatable(getDocsEntry(nextId).getMetadata().titleKey()).append(Component.literal(" ->")), nextId.toString()));
}
}
buttons.add(new ActionButton(
new CommonButtonData(Component.translatable("gui.ok"), 100),
Optional.empty()
));
player.openDialog(Holder.direct(new MultiActionDialog(
new CommonDialogData(
Component.translatable(entry.getMetadata().titleKey()),
Optional.empty(),
true,
false,
DialogAction.CLOSE,
entry.getDialog(player.registryAccess()),
List.of()
),
buttons,
Optional.empty(),
2
)));
}
private static ActionButton getDialogButton(Component text, String dialogToOpen) {
return new ActionButton(
new CommonButtonData(
text, 100
),
Optional.of(new StaticAction(new ClickEvent.RunCommand("/minions docs " + dialogToOpen)))
);
}
public static @Nullable DocsEntry getDocsEntry(Identifier id) {
if(docs != null) {
return docs.get(id);
} else {
return null;
}
}
public static @Nullable Collection<Identifier> getDocsEntryIds() {
if (docs != null) {
return docs.keySet();
} else {
return null;
}
}
@Override
public Tuple<Map<Identifier, DocsEntry>, DocsTree> prepare(SharedState state) {
Map<Identifier, Resource> resources = state.resourceManager().listResources("docs", id -> id.getNamespace().equals(Minions.MOD_ID) && id.getPath().endsWith(".json"));
final DocsTree. @Nullable BranchElement[] root = {null};
Map<Identifier, DocsEntry> docsEntries = new HashMap<>();
resources.forEach((id, resource) -> {
try(Reader reader = resource.openAsReader()) {
if(id.getPath().equals("docs/tree.json")) {
DocsTree.BranchElement.CODEC.decode(JsonOps.INSTANCE, StrictJsonParser.parse(reader))
.ifSuccess(entry -> root[0] = entry.getFirst())
.ifError(error -> Minions.LOGGER.warn("Could not parse docs tree {}: {}", id, error.message()));
} else {
Identifier docId = Identifier.fromNamespaceAndPath(id.getNamespace(), id.getPath().substring("docs/".length(), id.getPath().length() - ".json".length()));
DocsEntry.CODEC.decode(JsonOps.INSTANCE, StrictJsonParser.parse(reader))
.ifSuccess(entry -> docsEntries.put(docId, entry.getFirst()))
.ifError(error -> Minions.LOGGER.warn("Could not parse docs entry {}: {}", id, error.message()));
}
} catch (IOException | JsonParseException e) {
Minions.LOGGER.warn("Could not read file {}", id, e);
}
});
if(root[0] != null) {
DocsTree tree = new DocsTree(root[0]);
return new Tuple<>(docsEntries, tree);
} else {
return new Tuple<>(docsEntries, null);
}
}
@Override
public void apply(Tuple<Map<Identifier, DocsEntry>, DocsTree> o, SharedState state) {
docs = o.getA();
tree = o.getB();
}
}
@@ -0,0 +1,155 @@
package io.github.skippyall.minions.docs;
import com.mojang.datafixers.util.Either;
import com.mojang.serialization.Codec;
import net.minecraft.resources.Identifier;
import org.jspecify.annotations.Nullable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
public class DocsTree {
private final BranchElement root;
private final Map<Identifier, DocElement> entries = new HashMap<>();
public DocsTree(BranchElement root) {
this.root = root;
initEntries();
}
private void initEntries() {
DocElement element = root.first();
do {
entries.put(element.id, element);
element = element.next();
} while (element != null);
}
public BranchElement getRoot() {
return root;
}
public DocElement getElement(Identifier id) {
return entries.get(id);
}
public static abstract sealed class Element {
private @Nullable BranchElement parent;
public @Nullable BranchElement getParent() {
return parent;
}
void setParent(BranchElement parent) {
this.parent = parent;
}
public @Nullable DocElement next() {
if (parent != null) {
return parent.next(this);
} else {
return null;
}
}
public @Nullable DocElement previous() {
if (parent != null) {
return parent.previous(this);
} else {
return null;
}
}
}
public static final class DocElement extends Element {
public static final Codec<DocElement> CODEC = Identifier.CODEC.xmap(DocElement::new, DocElement::getId);
private final Identifier id;
public DocElement(Identifier element) {
this.id = element;
}
public Identifier getId() {
return id;
}
}
public static final class BranchElement extends Element {
public static final Codec<BranchElement> CODEC = Codec.<BranchElement>recursive("DocsBranch", codec ->
Codec.either(codec, DocElement.CODEC)
.xmap(
Either::unwrap,
element -> switch (element) {
case BranchElement branch -> Either.left(branch);
case DocElement doc -> Either.right(doc);
}
).listOf()
.xmap(BranchElement::new, branch -> branch.e)
).xmap(BranchElement::setParents, Function.identity());
private final List<Element> e;
public BranchElement(List<Element> elements) {
e = elements;
}
public DocElement first() {
return switch (e.getFirst()) {
case BranchElement branch -> branch.first();
case DocElement doc -> doc;
};
}
public DocElement last() {
return switch (e.getLast()) {
case BranchElement branch -> branch.last();
case DocElement doc -> doc;
};
}
public @Nullable DocElement next(Element current) {
int nextIndex = e.indexOf(current) + 1;
if(nextIndex < e.size()) {
return switch (e.get(nextIndex)) {
case BranchElement branch -> branch.first();
case DocElement doc -> doc;
};
} else {
if(getParent() != null) {
return getParent().next(this);
} else {
return null;
}
}
}
public @Nullable DocElement previous(Element current) {
int previousIndex = e.indexOf(current) - 1;
if(previousIndex >= 0) {
return switch (e.get(previousIndex)) {
case BranchElement branch -> branch.first();
case DocElement doc -> doc;
};
} else {
if(getParent() != null) {
return getParent().previous(this);
} else {
return null;
}
}
}
private BranchElement setParents() {
for(Element element : e) {
element.setParent(this);
if(element instanceof BranchElement branch) {
branch.setParents();
}
}
return this;
}
}
}
@@ -0,0 +1,82 @@
package io.github.skippyall.minions.docs;
import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import io.github.skippyall.minions.gui.GuiDisplay;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.ComponentSerialization;
import net.minecraft.resources.Identifier;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.dialog.body.DialogBody;
import net.minecraft.server.dialog.body.ItemBody;
import net.minecraft.server.dialog.body.PlainMessage;
import net.minecraft.world.item.Item;
import org.jspecify.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public record ReferenceEntry(Metadata metadata, ResourceKey<?> object, Component shortDescription, Component longDescription) implements DocsEntry {
private static final Codec<ResourceKey<?>> REGISTRY_KEY_CODEC = RecordCodecBuilder.create(instance ->
instance.group(
Identifier.CODEC.fieldOf("registry").forGetter(ResourceKey::registry),
Identifier.CODEC.fieldOf("value").forGetter(ResourceKey::identifier)
).apply(instance, (registry, value) -> ResourceKey.create(ResourceKey.createRegistryKey(registry), value))
);
public static final MapCodec<ReferenceEntry> CODEC = RecordCodecBuilder.mapCodec(instance ->
instance.group(
Metadata.CODEC.fieldOf("metadata").forGetter(ReferenceEntry::getMetadata),
REGISTRY_KEY_CODEC.fieldOf("object").forGetter(ReferenceEntry::object),
ComponentSerialization.CODEC.fieldOf("shortDescription").forGetter(ReferenceEntry::shortDescription),
ComponentSerialization.CODEC.fieldOf("longDescription").forGetter(ReferenceEntry::longDescription)
).apply(instance, ReferenceEntry::new));
@Override
public Metadata getMetadata() {
return metadata;
}
@Override
public List<DialogBody> getDialog(RegistryAccess manager) {
List<DialogBody> bodyElements = new ArrayList<>();
GuiDisplay display = getObjectDisplay(manager);
if(display != null) {
bodyElements.add(new ItemBody(display.createItemStackTemplate(), Optional.empty(), false, false, 16, 16));
}
bodyElements.add(new PlainMessage(Component.translatable(object.identifier().toLanguageKey(object.registry().getPath())), 200));
bodyElements.add(new PlainMessage(longDescription, 200));
return bodyElements;
}
public @Nullable GuiDisplay getObjectDisplay(RegistryAccess manager) {
GuiDisplay display = GuiDisplay.DEFAULT_DISPLAY;
if(object.isFor(Registries.ITEM) || object.isFor(Registries.BLOCK)) {
Item item;
if(object.isFor(Registries.ITEM)) {
item = BuiltInRegistries.ITEM.getValue(object.identifier());
} else {
item = BuiltInRegistries.BLOCK.getValue(object.identifier()).asItem();
}
if(item != null) {
display = new GuiDisplay.ItemBased(item);
}
} else {
Identifier displayId = object.identifier().withPrefix(object.registry().getPath() + "/");
display = GuiDisplay.getGuiDisplay(displayId, manager);
}
return display;
}
@Override
public MapCodec<? extends DocsEntry> getCodec() {
return CODEC;
}
}
@@ -0,0 +1,4 @@
@NullMarked
package io.github.skippyall.minions.docs;
import org.jspecify.annotations.NullMarked;
@@ -1,52 +0,0 @@
package io.github.skippyall.minions.gui;
import eu.pb4.sgui.api.elements.GuiElementBuilder;
import eu.pb4.sgui.api.gui.SimpleGui;
import io.github.skippyall.minions.module.command.Command;
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
import io.github.skippyall.minions.module.ModuleItem;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import java.util.List;
public class CommandsGui {
public static void openServerModuleCommandGui(ServerPlayerEntity player, MinionFakePlayer minion) {
List<ModuleItem> modules = minion.getModuleInventory().getModuleItems();
SimpleGui gui = new SimpleGui(ScreenHandlerType.GENERIC_9X3, player, false);
gui.setTitle(Text.translatable("minions.gui.module_commands.title"));
for (int i = 0; i < modules.size(); i++) {
ModuleItem module = modules.get(i);
gui.setSlot(i, new GuiElementBuilder()
.setItem(module.asItem())
.setCallback(() -> openServerCommandGui(player, minion, module))
);
}
gui.open();
}
public static void openServerCommandGui(ServerPlayerEntity player, MinionFakePlayer minion, ModuleItem module) {
List<Command> commands = module.getCommands();
SimpleGui commandGui = new SimpleGui(ScreenHandlerType.GENERIC_9X3, player, false);
commandGui.setTitle(Text.translatable("minions.gui.commands.title", module.asItem().getName()));
for(int j = 0; j < commands.size(); j++) {
Command command = commands.get(j);
commandGui.setSlot(j, new GuiElementBuilder()
.setItem(command.getItemRepresentation())
.setName(command.getName())
.addLoreLine(command.getDescription())
.setCallback(() -> command.execute(player, minion))
);
}
commandGui.open();
}
}
@@ -0,0 +1,5 @@
package io.github.skippyall.minions.gui;
public interface Displayable {
GuiDisplay getDisplay();
}
@@ -0,0 +1,164 @@
package io.github.skippyall.minions.gui;
import com.mojang.serialization.Codec;
import io.github.skippyall.minions.registration.MinionRegistries;
import io.github.skippyall.minions.util.TranslationUtil;
import net.minecraft.ChatFormatting;
import net.minecraft.core.Registry;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.UUIDUtil;
import net.minecraft.core.component.DataComponentPatch;
import net.minecraft.core.component.DataComponentType;
import net.minecraft.core.component.DataComponents;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.Identifier;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.ItemStackTemplate;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.Rarity;
import net.minecraft.world.item.component.ResolvableProfile;
import net.minecraft.world.item.component.TooltipDisplay;
import java.util.LinkedHashSet;
import java.util.UUID;
public interface GuiDisplay {
Codec<GuiDisplay> CODEC = MinionRegistries.GUI_DISPLAY_TYPE.byNameCodec().dispatch(GuiDisplay::getCodec, codec -> codec.fieldOf("data"));
GuiDisplay DEFAULT_DISPLAY = new ItemBased(Items.BARRIER);
TooltipDisplay TOOLTIP_HIDE_ALL_COMPONENTS = createHideAllTooltip();
private static TooltipDisplay createHideAllTooltip() {
LinkedHashSet<DataComponentType<?>> set = new LinkedHashSet<>();
for(DataComponentType<?> type : BuiltInRegistries.DATA_COMPONENT_TYPE) {
if(type != DataComponents.LORE) {
set.add(type);
}
}
return new TooltipDisplay(false, set);
}
static GuiDisplay getGuiDisplay(Identifier id, RegistryAccess manager) {
return manager.lookup(MinionRegistries.GUI_DISPLAY).map(registry -> registry.getValue(id)).orElse(DEFAULT_DISPLAY);
}
static <T> GuiDisplay getGuiDisplayFor(Registry<T> registry, T element, RegistryAccess manager) {
Identifier elementId = registry.getKey(element);
if(elementId == null) {
return DEFAULT_DISPLAY;
}
Identifier displayId = elementId.withPrefix(registry.key().identifier().getPath() + "/");
return getGuiDisplay(displayId, manager);
}
static <T> ItemStack getDisplayStack(Registry<T> registry, T element, RegistryAccess manager) {
return getGuiDisplayFor(registry, element, manager).createItemStack();
}
static <T> ItemStack getDisplayStackWithName(Registry<T> registry, T element, RegistryAccess manager) {
ItemStack stack = getDisplayStack(registry, element, manager);
stack.set(DataComponents.CUSTOM_NAME, Component.translatable(TranslationUtil.getTranslationKey(element, registry)).withStyle(style -> style.withItalic(false).withColor(ChatFormatting.WHITE)));
return stack;
}
ItemStackTemplate createItemStackTemplate();
default ItemStack createItemStack() {
return createItemStackTemplate().create();
}
Codec<? extends GuiDisplay> getCodec();
class ModelBased implements GuiDisplay {
public static final Codec<ModelBased> CODEC = Identifier.CODEC.xmap(ModelBased::new, display -> display.model);
private final Identifier model;
public ModelBased(Identifier model) {
this.model = model;
}
@Override
public ItemStackTemplate createItemStackTemplate() {
return new ItemStackTemplate(Items.BARRIER, DataComponentPatch.builder()
.set(DataComponents.ITEM_MODEL, model)
.build()
);
}
@Override
public Codec<? extends GuiDisplay> getCodec() {
return CODEC;
}
}
class ItemBased implements GuiDisplay {
public static final Codec<ItemBased> CODEC = BuiltInRegistries.ITEM.byNameCodec().xmap(ItemBased::new, display -> display.item);
private final Item item;
public ItemBased(Item item) {
this.item = item;
}
@Override
public ItemStackTemplate createItemStackTemplate() {
return new ItemStackTemplate(item, DataComponentPatch.builder()
.set(DataComponents.TOOLTIP_DISPLAY, TOOLTIP_HIDE_ALL_COMPONENTS)
.set(DataComponents.RARITY, Rarity.COMMON)
.build());
}
@Override
public Codec<? extends GuiDisplay> getCodec() {
return CODEC;
}
}
class HeadBased implements GuiDisplay {
public static final Codec<HeadBased> CODEC = UUIDUtil.AUTHLIB_CODEC.xmap(HeadBased::new, display -> display.uuid);
private final UUID uuid;
public HeadBased(UUID uuid) {
this.uuid = uuid;
}
@Override
public ItemStackTemplate createItemStackTemplate() {
return new ItemStackTemplate(Items.PLAYER_HEAD, DataComponentPatch.builder()
.set(DataComponents.PROFILE, ResolvableProfile.createUnresolved(uuid))
.set(DataComponents.TOOLTIP_DISPLAY, TOOLTIP_HIDE_ALL_COMPONENTS)
.build()
);
}
@Override
public Codec<? extends GuiDisplay> getCodec() {
return CODEC;
}
}
class StackBased implements GuiDisplay {
public static final Codec<StackBased> CODEC = ItemStackTemplate.CODEC.xmap(StackBased::new, StackBased::createItemStackTemplate);
private final ItemStackTemplate template;
public StackBased(ItemStackTemplate template) {
this.template = template;
}
@Override
public ItemStackTemplate createItemStackTemplate() {
return template;
}
@Override
public Codec<? extends GuiDisplay> getCodec() {
return CODEC;
}
}
}
@@ -1,77 +0,0 @@
package io.github.skippyall.minions.gui;
import eu.pb4.sgui.api.elements.GuiElementBuilder;
import eu.pb4.sgui.api.gui.SimpleGui;
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
import net.minecraft.item.Items;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.screen.slot.Slot;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
public class MinionGui {
public static void openInventory(ServerPlayerEntity player, MinionFakePlayer minion) {
openServerSideInventory(player, minion);
}
public static void openServerSideInventory(ServerPlayerEntity player, MinionFakePlayer minion) {
SimpleGui gui = new SimpleGui(ScreenHandlerType.GENERIC_3X3, player, false);
gui.setTitle(minion.getName());
gui.setSlot(1, new GuiElementBuilder()
.setItem(Items.COMMAND_BLOCK)
.setName(Text.translatable("minions.gui.main.commands"))
.setCallback((i, clickType, slotActionType) -> {
openCommandsGui(player, minion);
})
);
if(minion.isProgrammable()) {
gui.setSlot(4, new GuiElementBuilder()
.setItem(Items.REDSTONE)
.setName(Text.translatable("minions.gui.main.programming"))
.setCallback(() -> {
openProgrammingInventory(player, minion);
})
);
}
gui.setSlot(3, new GuiElementBuilder()
.setItem(Items.NETHERITE_UPGRADE_SMITHING_TEMPLATE)
.setName(Text.translatable("minions.gui.main.modules"))
.setCallback(() -> {
ModuleInventory.openModuleInventory(player, minion);
})
);
gui.setSlot(5, new GuiElementBuilder()
.setItem(Items.CHEST)
.setName(Text.translatable("minions.gui.main.inventory"))
.setCallback(() -> {
openMinionInventory(player, minion);
})
);
gui.setSlot(7, new GuiElementBuilder()
.setItem(Items.BARRIER)
.setName(Text.translatable("minions.gui.main.pickup"))
.setCallback(() -> {
minion.kill(minion.getServerWorld());
})
);
gui.open();
}
public static void openCommandsGui(ServerPlayerEntity player, MinionFakePlayer minion) {
CommandsGui.openServerModuleCommandGui(player, minion);
}
public static void openProgrammingInventory(ServerPlayerEntity player, MinionFakePlayer minion) {
}
public static void openMinionInventory(ServerPlayerEntity player, MinionFakePlayer minion) {
SimpleGui gui = new SimpleGui(ScreenHandlerType.GENERIC_9X5, player, false);
gui.setTitle(Text.translatable("minions.gui.inventory.title", minion.getName()));
for (int i = 0; i < minion.getInventory().size(); i++) {
gui.setSlotRedirect(i, new Slot(minion.getInventory(), i, 0, 0));
}
gui.open();
}
}
@@ -1,95 +0,0 @@
package io.github.skippyall.minions.gui;
import eu.pb4.sgui.api.elements.GuiElementBuilder;
import eu.pb4.sgui.api.gui.SimpleGui;
import io.github.skippyall.minions.input.TextInput;
import io.github.skippyall.minions.minion.MinionData;
import io.github.skippyall.minions.minion.MinionItem;
import io.github.skippyall.minions.minion.MinionProfileUtils;
import io.github.skippyall.minions.minion.skin.SkinProvider;
import io.github.skippyall.minions.minion.skin.SkinProviders;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.ProfileComponent;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import java.util.Optional;
public class MinionLookGui extends SimpleGui {
private ItemStack minionItem;
private SkinProvider currentSkinProvider;
public MinionLookGui(ServerPlayerEntity player, ItemStack minionItem) {
super(ScreenHandlerType.GENERIC_9X3, player, false);
this.minionItem = minionItem;
this.currentSkinProvider = SkinProviders.NAME;
}
public void update() {
updateName();
updateSkin();
updateSkinProvider();
}
private void updateName() {
setSlot(10, new GuiElementBuilder()
.setItem(Items.OAK_SIGN)
.setName(Text.literal(getData().name()))
.setCallback(() -> {
openRenameGui(player, minionItem);
})
);
}
private void updateSkin() {
GuiElementBuilder builder = new GuiElementBuilder()
.setItem(Items.PLAYER_HEAD)
.setCallback(() -> currentSkinProvider.openSkinMenu(player).thenAccept(skin -> {
MinionItem.setData(getData().withSkin(skin), minionItem);
}));
if(MinionItem.getData(minionItem) != null && MinionItem.getData(minionItem).skin().isPresent()) {
builder.setComponent(DataComponentTypes.PROFILE, new ProfileComponent(Optional.empty(), Optional.empty(), getData().skin().get()));
}
setSlot(16, builder);
}
private void cycleSkinProvider() {
int currentId = SkinProviders.SKIN_PROVIDERS.getRawId(currentSkinProvider);
currentId++;
if(SkinProviders.SKIN_PROVIDERS.size() == currentId) {
currentId = 0;
}
currentSkinProvider = SkinProviders.SKIN_PROVIDERS.get(currentId);
updateSkinProvider();
}
private void updateSkinProvider() {
setSlot(25, new GuiElementBuilder()
.setItem(Items.GREEN_STAINED_GLASS_PANE)
.setComponent(DataComponentTypes.CUSTOM_NAME, currentSkinProvider.getDisplayName())
.setCallback(this::cycleSkinProvider)
);
}
private MinionData getData() {
return MinionItem.getDataOrDefault(minionItem);
}
public static void open(ServerPlayerEntity player, ItemStack minionItem) {
MinionLookGui gui = new MinionLookGui(player, minionItem);
gui.update();
gui.open();
}
public void openRenameGui(ServerPlayerEntity player, ItemStack minionItem) {
TextInput.inputSync(player, Text.translatable("minions.gui.look.rename.title"), "Minion", MinionProfileUtils::checkMinionNameWithoutPrefix)
.thenAccept(name -> {
MinionItem.setData(getData().withName(MinionProfileUtils.PREFIX + name), minionItem);
open();
});
}
}
@@ -0,0 +1,131 @@
package io.github.skippyall.minions.gui
import com.mojang.authlib.GameProfile
import eu.pb4.sgui.api.elements.GuiElementBuilder
import eu.pb4.sgui.api.gui.SimpleGui
import io.github.skippyall.minions.gui.input.TextInput
import io.github.skippyall.minions.minion.MinionData
import io.github.skippyall.minions.minion.MinionItem
import io.github.skippyall.minions.minion.MinionProfileUtils
import io.github.skippyall.minions.minion.skin.SkinProvider
import io.github.skippyall.minions.registration.MinionRegistries
import io.github.skippyall.minions.registration.SkinProviders
import kotlinx.coroutines.future.await
import kotlinx.coroutines.launch
import net.fabricmc.fabric.api.entity.FakePlayer
import net.minecraft.core.component.DataComponents
import net.minecraft.network.chat.Component
import net.minecraft.server.level.ServerPlayer
import net.minecraft.world.inventory.MenuType
import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.Items
import net.minecraft.world.item.component.ResolvableProfile
import java.util.*
class MinionLookGui(
viewer: ServerPlayer,
private val minionItem: ItemStack
) : MinionsGui(viewer) {
private lateinit var gui: SimpleGui
private var currentSkinProvider: SkinProvider = SkinProviders.NAME
private val data: MinionData
get() = MinionItem.getDataOrDefault(viewer.level().server, minionItem)
init {
open()
}
override fun open() {
gui = object : SimpleGui(MenuType.GENERIC_9x3, viewer, false) {
override fun onPlayerClose(success: Boolean) {
onBackingClosed()
}
}
update()
gui.open()
}
override fun closeBacking() {
gui.close()
}
fun update() {
updateName()
updateSkin()
updateSkinProvider()
}
private fun updateName() {
gui.setSlot(
10, GuiElementBuilder()
.setItem(Items.OAK_SIGN)
.setName(Component.literal(this.data.name))
.setCallback(this::openRenameGui)
)
}
private fun updateSkin() {
val builder = GuiElementBuilder()
.setItem(Items.PLAYER_HEAD)
.setCallback(this::openSkinGui)
if (MinionItem.getData(viewer.level().server, minionItem)?.skin?.isPresent ?: false) {
builder.setComponent(
DataComponents.PROFILE,
ResolvableProfile.createResolved(GameProfile(FakePlayer.DEFAULT_UUID, "", this.data.skin.get()))
)
}
gui.setSlot(16, builder)
}
private fun updateSkinProvider() {
gui.setSlot(
25, GuiElementBuilder()
.setItem(Items.GREEN_STAINED_GLASS_PANE)
.setComponent(DataComponents.CUSTOM_NAME, currentSkinProvider.getDisplayName())
.setCallback(Runnable { this.cycleSkinProvider() })
)
}
fun openSkinGui() {
scope.launch {
val profile = currentSkinProvider.openSkinMenu(this@MinionLookGui).await()
val skin = profile?.resolveProfile(viewer.level().server.services().profileResolver())?.await()
data.skin = Optional.ofNullable(skin?.properties())
updateSkin()
}
}
private fun cycleSkinProvider() {
var currentId = MinionRegistries.SKIN_PROVIDERS.getId(currentSkinProvider)
currentId++
if (MinionRegistries.SKIN_PROVIDERS.size() == currentId) {
currentId = 0
}
currentSkinProvider = MinionRegistries.SKIN_PROVIDERS.byId(currentId)!!
updateSkinProvider()
}
fun openRenameGui() {
scope.launch {
val newName = TextInput.input(
this@MinionLookGui,
Component.translatable("minions.gui.look.rename.title"),
"Minion",
) { name ->
MinionProfileUtils.checkMinionNameWithoutPrefix(viewer.level().server, name)
}.await()
if(newName != null) {
data.name = newName
updateName()
}
}
}
}
@@ -0,0 +1,84 @@
package io.github.skippyall.minions.gui
import eu.pb4.sgui.api.elements.GuiElementBuilder
import kotlinx.coroutines.CompletableJob
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import net.minecraft.network.chat.Component
import net.minecraft.server.level.ServerPlayer
import net.minecraft.world.item.Items
abstract class MinionsGui {
protected val parent: MinionsGui?
@JvmField
val viewer: ServerPlayer
protected var child: MinionsGui? = null
private var open = true
private val job: CompletableJob
val scope: CoroutineScope
constructor(parent: MinionsGui) {
this.viewer = parent.viewer
this.parent = parent
parent.child = this
job = Job(parent.job)
scope = CoroutineScope(Dispatchers.Unconfined.plus(CoroutineName("MinionsGui")).plus(job))
}
constructor(viewer: ServerPlayer) {
this.viewer = viewer
this.parent = null
job = Job(null)
scope = CoroutineScope(Dispatchers.Unconfined.plus(CoroutineName("MinionsGui")).plus(job))
}
protected abstract fun open()
protected open fun reopen() {
open()
}
fun onBackingClosed() {
if (child != null && child!!.open) {
return
}
close(true)
}
@JvmOverloads
fun close(alreadyClosed: Boolean = false) {
if (open) {
scope.cancel(null)
open = false
if (child != null) {
child!!.close(alreadyClosed)
} else if (!alreadyClosed) {
closeBacking()
}
}
}
fun goBack() {
if (parent != null) {
open = false
parent.child = null
parent.reopen()
close(true)
} else {
close(false)
}
}
fun backButton(): GuiElementBuilder? {
return GuiElementBuilder(Items.MANGROVE_DOOR)
.setName(Component.translatable("gui.back"))
.setCallback(Runnable { this.goBack() })
}
protected abstract fun closeBacking()
}
@@ -1,84 +0,0 @@
package io.github.skippyall.minions.gui;
import io.github.skippyall.minions.module.command.Command;
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
import io.github.skippyall.minions.module.ModuleItem;
import io.github.skippyall.minions.program.block.CodeBlock;
import net.minecraft.inventory.Inventories;
import net.minecraft.inventory.SimpleInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.screen.SimpleNamedScreenHandlerFactory;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import java.util.ArrayList;
import java.util.List;
public class ModuleInventory extends SimpleInventory {
public ModuleInventory() {
super(27);
}
public static void openModuleInventory(ServerPlayerEntity player, MinionFakePlayer minion) {
player.openHandledScreen(new SimpleNamedScreenHandlerFactory((syncId, playerInventory, player2) -> new ModuleInventoryScreenHandler(syncId, playerInventory, minion.getModuleInventory()), Text.translatable("minions.gui.modules.title", minion.getName())));
}
@Override
public int getMaxCountPerStack() {
return 1;
}
@Override
public boolean isValid(int slot, ItemStack stack) {
return (stack.getCount() <= getMaxCountPerStack()) && stack.getItem() instanceof ModuleItem;
}
public void readNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup lookup) {
Inventories.readNbt(nbt, heldStacks, lookup);
}
public NbtCompound writeNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup lookup) {
return Inventories.writeNbt(nbt, heldStacks, lookup);
}
public boolean hasModule(ModuleItem module) {
for(ItemStack stack : heldStacks) {
if(stack.getItem() instanceof ModuleItem module2 && module2 == module) {
return true;
}
}
return false;
}
public List<ModuleItem> getModuleItems() {
ArrayList<ModuleItem> modules = new ArrayList<>();
for(ItemStack stack : heldStacks) {
if(stack.getItem() instanceof ModuleItem module) {
modules.add(module);
}
}
return modules;
}
public List<Command> getAllCommands() {
ArrayList<Command> commands = new ArrayList<>();
for(ItemStack stack : heldStacks) {
if(stack.getItem() instanceof ModuleItem module) {
commands.addAll(module.getCommands());
}
}
return commands;
}
public List<CodeBlock<?,?>> getAllCodeBlocks() {
ArrayList<CodeBlock<?,?>> commands = new ArrayList<>();
for(ItemStack stack : heldStacks) {
if(stack.getItem() instanceof ModuleItem module) {
commands.addAll(module.getCodeBlocks());
}
}
return commands;
}
}
@@ -1,80 +0,0 @@
package io.github.skippyall.minions.gui;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.GenericContainerScreenHandler;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.screen.slot.Slot;
public class ModuleInventoryScreenHandler extends ScreenHandler {
private final int rows = 3;
private final ModuleInventory inventory;
public ModuleInventoryScreenHandler(int syncId, ModuleInventory inventory) {
super(ScreenHandlerType.GENERIC_9X3, syncId);
this.inventory = inventory;
}
public ModuleInventoryScreenHandler(int syncId, PlayerInventory playerInventory, ModuleInventory inventory) {
super(ScreenHandlerType.GENERIC_9X3, syncId);
int k;
int j;
GenericContainerScreenHandler.checkSize(inventory, 3 * 9);
this.inventory = inventory;
inventory.onOpen(playerInventory.player);
int i = (rows - 4) * 18;
for (j = 0; j < rows; ++j) {
for (k = 0; k < 9; ++k) {
this.addSlot(new Slot(inventory, k + j * 9, 8 + k * 18, 18 + j * 18) {
@Override
public boolean canInsert(ItemStack stack) {
return super.canInsert(stack) && inventory.isValid(getIndex(), stack);
}
});
}
}
for (j = 0; j < 3; ++j) {
for (k = 0; k < 9; ++k) {
this.addSlot(new Slot(playerInventory, k + j * 9 + 9, 8 + k * 18, 103 + j * 18 + i));
}
}
for (j = 0; j < 9; ++j) {
this.addSlot(new Slot(playerInventory, j, 8 + j * 18, 161 + i));
}
}
@Override
public boolean canUse(PlayerEntity player) {
return this.inventory.canPlayerUse(player);
}
@Override
public ItemStack quickMove(PlayerEntity player, int slot) {
ItemStack itemStack = ItemStack.EMPTY;
Slot slot2 = this.slots.get(slot);
if (slot2 != null && slot2.hasStack()) {
ItemStack itemStack2 = slot2.getStack();
itemStack = itemStack2.copy();
if (slot < this.rows * 9 ? !this.insertItem(itemStack2, this.rows * 9, this.slots.size(), true) : !this.insertItem(itemStack2, 0, this.rows * 9, false)) {
return ItemStack.EMPTY;
}
if (itemStack2.isEmpty()) {
slot2.setStack(ItemStack.EMPTY);
} else {
slot2.markDirty();
}
}
return itemStack;
}
@Override
public void onClosed(PlayerEntity player) {
super.onClosed(player);
this.inventory.onClose(player);
}
public ModuleInventory getInventory() {
return inventory;
}
}
@@ -0,0 +1,110 @@
package io.github.skippyall.minions.gui;
import eu.pb4.sgui.api.elements.GuiElementBuilder;
import eu.pb4.sgui.api.gui.SimpleGui;
import net.minecraft.core.IdMap;
import net.minecraft.network.chat.Component;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.item.Items;
import org.jspecify.annotations.Nullable;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiFunction;
import java.util.function.Function;
public class PaginatedList extends MinionsGui {
private int page = 0;
private SimpleGui gui;
private final Component title;
private final int size;
private final BiFunction<Integer, PaginatedList, GuiElementBuilder> display;
private @Nullable Runnable onClose = null;
public PaginatedList(MinionsGui parent, Component title, int size, BiFunction<Integer, PaginatedList, GuiElementBuilder> display) {
super(parent);
this.title = title;
this.size = size;
this.display = display;
open();
}
public PaginatedList(MinionsGui parent, Component title, int size, BiFunction<Integer, PaginatedList, GuiElementBuilder> display, Runnable onClose) {
this(parent, title, size, display);
this.onClose = onClose;
}
@Override
protected void open() {
gui = new SimpleGui(MenuType.GENERIC_9x6, viewer, false) {
@Override
public void onPlayerClose(boolean success) {
onBackingClosed();
if(onClose != null) {
onClose.run();
}
}
};
gui.setTitle(title);
gui.setSlot(8, backButton());
addItems();
gui.open();
}
@Override
protected void closeBacking() {
gui.close();
}
public static <T> void createList(MinionsGui parent, Component title, List<T> list, BiFunction<T, PaginatedList, GuiElementBuilder> display) {
new PaginatedList(parent, title, list.size(), (i, gui) -> display.apply(list.get(i), gui));
}
public static <T> void createList(MinionsGui parent, Component title, IdMap<T> list, BiFunction<T, PaginatedList, GuiElementBuilder> display) {
new PaginatedList(parent, title, list.size(), (i, gui) -> display.apply(list.byId(i), gui));
}
public static <T> CompletableFuture<T> createListFuture(MinionsGui parent, Component title, List<T> list, Function<T, GuiElementBuilder> display) {
CompletableFuture<T> future = new CompletableFuture<>();
new PaginatedList(parent, title, list.size(), (i, me) -> display.apply(list.get(i))
.setCallback(() -> {
future.complete(list.get(i));
me.goBack();
})
);
return future;
}
private void addItems() {
int slot = 9;
for(int i = 36 * page; i < Math.min(36 * (page + 1), size); i++) {
gui.setSlot(slot, display.apply(i, this));
slot++;
}
if(page > 0) {
gui.setSlot(48, new GuiElementBuilder(Items.SPECTRAL_ARROW)
.setItemName(Component.translatable("book.page_button.previous"))
.setCallback(() -> {
page--;
addItems();
})
);
} else {
gui.clearSlot(48);
}
if(27 * (page + 1) < size) {
gui.setSlot(50, new GuiElementBuilder(Items.ARROW)
.setItemName(Component.translatable("book.page_button.next"))
.setCallback(() -> {
page++;
addItems();
})
);
} else {
gui.clearSlot(50);
}
}
}
@@ -0,0 +1,55 @@
package io.github.skippyall.minions.gui.input
import eu.pb4.sgui.api.elements.GuiElementBuilder
import eu.pb4.sgui.api.gui.SimpleGui
import io.github.skippyall.minions.gui.MinionsGui
import io.github.skippyall.minions.gui.minion.SimpleMinionsGui
import net.minecraft.network.chat.Component
import net.minecraft.world.inventory.MenuType
import net.minecraft.world.item.Items
import java.util.concurrent.CompletableFuture
object BooleanInput {
@JvmStatic
@JvmOverloads
fun confirm(
parent: MinionsGui,
title: Component,
falseText: Component = Component.translatable("minions.gui.abort"),
trueText: Component = Component.translatable("minions.gui.confirm")
): CompletableFuture<Boolean> {
val future = CompletableFuture<Boolean>()
SimpleMinionsGui(parent) { onClose: Runnable, me: SimpleMinionsGui ->
val gui: SimpleGui = object : SimpleGui(MenuType.GENERIC_3x3, parent.viewer, false) {
override fun onPlayerClose(success: Boolean) {
future.complete(false)
onClose.run()
}
}
gui.setTitle(title)
gui.setSlot(
3, GuiElementBuilder(Items.REDSTONE_BLOCK)
.setName(falseText)
.setCallback(Runnable {
future.complete(false)
me.goBack()
})
)
gui.setSlot(
5, GuiElementBuilder(Items.EMERALD_BLOCK)
.setName(trueText)
.setCallback(Runnable {
future.complete(true)
me.goBack()
})
)
gui.open()
gui
}
return future
}
}
@@ -0,0 +1,183 @@
package io.github.skippyall.minions.gui.input;
import org.jspecify.annotations.Nullable;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
public sealed interface Result<T, E> permits Result.Success, Result.Error {
static <T> Result<T, String> wrap(UnsafeOperation<T> toWrap) {
return wrapCustomError(toWrap, Exception::getMessage);
}
static <T, E> Result<T, E> wrapCustomError(UnsafeOperation<T> toWrap, E error) {
return wrapCustomError(toWrap, e -> error);
}
static <T, E> Result<T, E> wrapCustomError(UnsafeOperation<T> toWrap, Function<Exception, E> errorTransformer) {
try {
return new Result.Success<>(toWrap.run());
} catch (Exception e) {
return new Result.Error<>(errorTransformer.apply(e));
}
}
static <T, E> Result<T, E> ofNullable(@Nullable T value, E error) {
if(value != null) {
return new Success<T, E>(value);
} else {
return new Error<>(error);
}
}
static <T, E> Result<T, E> ofNullable(@Nullable T value, Supplier<E> error) {
if(value != null) {
return new Success<T, E>(value);
} else {
return new Error<>(error.get());
}
}
boolean isSuccess();
T getOrDefault(T defaultValue);
T getOrThrow();
E getErrorOrThrow();
Optional<T> getOptional();
Optional<E> getOptionalError();
void ifSuccess(Consumer<T> handler);
void ifError(Consumer<Error<T, E>> handler);
<U> Result<U,E> map(Function<T, U> mapper);
<U> Result<U,E> flatMap(Function<T, Result<U, E>> mapper);
<U> Result<T,U> mapError(Function<E, U> mapper);
record Success<T, E>(T result) implements Result<T, E> {
@Override
public boolean isSuccess() {
return true;
}
@Override
public T getOrDefault(T defaultValue) {
return result;
}
@Override
public Optional<E> getOptionalError() {
return Optional.empty();
}
@Override
public T getOrThrow() {
return result;
}
@Override
public E getErrorOrThrow() {
throw new RuntimeException("Result was not an Error");
}
@Override
public Optional<T> getOptional() {
return Optional.ofNullable(result);
}
@Override
public void ifSuccess(Consumer<T> handler) {
handler.accept(result);
}
@Override
public void ifError(Consumer<Error<T, E>> handler) {
}
@Override
public <U> Result<U, E> map(Function<T, U> mapper) {
return new Success<>(mapper.apply(result));
}
@Override
public <U> Result<U, E> flatMap(Function<T, Result<U, E>> mapper) {
return mapper.apply(result);
}
@Override
public <U> Result<T, U> mapError(Function<E, U> mapper) {
return new Success<>(result);
}
}
record Error<T, E>(E message) implements Result<T, E> {
@Override
public boolean isSuccess() {
return false;
}
@Override
public T getOrDefault(
T defaultValue) {
return defaultValue;
}
@Override
public T getOrThrow() {
throw new RuntimeException("Result was an error: " + message);
}
@Override
public E getErrorOrThrow() {
return message;
}
@Override
public Optional<T> getOptional() {
return Optional.empty();
}
@Override
public Optional<E> getOptionalError() {
return Optional.ofNullable(message);
}
@Override
public void ifSuccess(Consumer<T> handler) {
}
@Override
public void ifError(Consumer<Error<T, E>> handler) {
handler.accept(this);
}
@Override
public <U> Result<U, E> map(Function<T, U> mapper) {
return new Error<>(message);
}
@Override
public <U> Result<U, E> flatMap(Function<T, Result<U, E>> mapper) {
return new Error<>(message);
}
@Override
public <U> Result<T, U> mapError(Function<E, U> mapper) {
return new Error<>(mapper.apply(message));
}
}
interface UnsafeOperation<T> {
T run() throws Exception;
}
}
@@ -0,0 +1,149 @@
package io.github.skippyall.minions.gui.input
import eu.pb4.sgui.api.elements.GuiElementBuilder
import eu.pb4.sgui.api.gui.AnvilInputGui
import io.github.skippyall.minions.gui.MinionsGui
import kotlinx.coroutines.launch
import net.minecraft.network.chat.Component
import net.minecraft.world.inventory.AnvilMenu
import net.minecraft.world.item.Items
import java.util.concurrent.CompletableFuture
class TextInput<T : Any>(
parent: MinionsGui,
val title: Component,
val defaultValue: String,
val parser: suspend (String) -> Result<T, Component>
) : MinionsGui(parent) {
private val valid: GuiElementBuilder = GuiElementBuilder()
.setItem(Items.EMERALD_BLOCK)
.setName(Component.literal("OK"))
.setCallback(Runnable { this.onConfirm() })
private val invalid: GuiElementBuilder = GuiElementBuilder()
.setItem(Items.REDSTONE_BLOCK)
private lateinit var gui: AnvilInputGui
private var result: Result<T, Component>? = null
val future = CompletableFuture<T?>()
init {
open()
}
override fun open() {
gui = object : AnvilInputGui(viewer, false) {
override fun onInput(input: String) {
updateConfirmButton(input)
}
override fun onPlayerClose(success: Boolean) {
onBackingClosed()
if (!future.isDone) {
future.complete(null)
}
}
}
gui.setTitle(title)
gui.setDefaultInputValue(defaultValue)
updateConfirmButton(defaultValue)
gui.open()
}
override fun closeBacking() {
gui.close()
}
fun updateConfirmButton(input: String) {
scope.launch {
val result = parser(input)
this@TextInput.result = result
if (result.isSuccess()) {
gui.setSlot(AnvilMenu.RESULT_SLOT, valid)
} else {
val text = result.getErrorOrThrow()
gui.setSlot(AnvilMenu.RESULT_SLOT, invalid.setName(text))
}
}
}
fun onConfirm() {
result?.ifSuccess { success: T ->
future.complete(success)
goBack()
}
}
companion object {
@JvmStatic
fun <T : Any>input(
gui: MinionsGui,
title: Component,
defaultValue: String,
parser: suspend (String) -> Result<T, Component>,
): CompletableFuture<T?> {
val input = TextInput(
parent = gui,
title = title,
defaultValue = defaultValue,
parser = parser,
)
return input.future
}
@JvmStatic
fun inputString(
gui: MinionsGui,
title: Component,
defaultValue: String,
): CompletableFuture<String?> {
return input<String>(
gui = gui,
title = title,
defaultValue = defaultValue,
parser = { result -> Result.Success(result) },
)
}
@JvmStatic
fun inputLong(
gui: MinionsGui,
title: Component,
defaultValue: Long,
): CompletableFuture<Long?> {
return input<Long>(
gui = gui,
title = title,
defaultValue = defaultValue.toString(),
parser = { string ->
Result.wrapCustomError<Long, Component>(
{ string.toLong() },
Component.translatable("value_type.minions.long.not_long")
)
},
)
}
@JvmStatic
fun inputDouble(
gui: MinionsGui,
title: Component,
defaultValue: Double,
): CompletableFuture<Double?> {
return input<Double>(
gui = gui,
title = title,
defaultValue = defaultValue.toString(),
parser = { string ->
Result.wrapCustomError<Double, Component>(
{ string.toDouble() },
Component.translatable("value_type.minions.long.not_long")
)
},
)
}
}
}
@@ -0,0 +1,4 @@
@NullMarked
package io.github.skippyall.minions.gui.input;
import org.jspecify.annotations.NullMarked;
@@ -0,0 +1,154 @@
package io.github.skippyall.minions.gui.instruction;
import eu.pb4.sgui.api.elements.GuiElementBuilder;
import eu.pb4.sgui.api.gui.SimpleGui;
import io.github.skippyall.minions.gui.GuiDisplay;
import io.github.skippyall.minions.gui.MinionsGui;
import io.github.skippyall.minions.gui.PaginatedList;
import io.github.skippyall.minions.gui.minion.GuiContext;
import io.github.skippyall.minions.minion.MinionRuntime;
import io.github.skippyall.minions.program.instruction.ConfiguredInstruction;
import io.github.skippyall.minions.program.supplier.Parameter;
import io.github.skippyall.minions.program.supplier.ValueSupplier;
import io.github.skippyall.minions.program.supplier.ValueSupplierList;
import io.github.skippyall.minions.program.supplier.ValueSupplierType;
import io.github.skippyall.minions.registration.MinionRegistries;
import io.github.skippyall.minions.util.TranslationUtil;
import net.minecraft.network.chat.Component;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import org.jspecify.annotations.Nullable;
public class ArgumentGui extends MinionsGui {
private final GuiContext.ValueSupplier context;
private final ConfiguredInstruction<MinionRuntime> instruction;
private final Parameter<?> parameter;
private SimpleGui gui;
private @Nullable ValueSupplierType<MinionRuntime> argumentType;
private ValueSupplierList. @Nullable ValueSupplierEntry<?, MinionRuntime> entry;
public ArgumentGui(MinionsGui parent, GuiContext.ValueSupplier context) {
super(parent);
instruction = context.getInstruction();
this.parameter = context.getParameter();
this.context = context;
this.entry = instruction.getArguments().getEntry(parameter);
this.argumentType = entry.getSupplier().getType();
open();
}
public String getInstructionName() {
return context.getName();
}
public @Nullable ValueSupplier<?, MinionRuntime> getArgument() {
if(entry != null) {
return entry.getSupplier();
}
return null;
}
@Override
protected void open() {
gui = new SimpleGui(MenuType.GENERIC_3x3, viewer, false) {
@Override
public void onPlayerClose(boolean success) {
onBackingClosed();
}
};
gui.setTitle(Component.translatable(
"minions.gui.instruction.argument.title",
parameter.name(),
Component.translatable(TranslationUtil.getTranslationKey(parameter.type(), MinionRegistries.VALUE_TYPES))
));
gui.setSlot(2, backButton());
updateTypeConfiguration();
updateArgumentConfiguration();
updateConverterConfiguration();
gui.open();
}
private void updateTypeConfiguration() {
ItemStack displayStack;
if(argumentType != null) {
displayStack = GuiDisplay.getDisplayStack(MinionRegistries.VALUE_SUPPLIER_TYPES, argumentType, viewer.registryAccess());
} else {
displayStack = new ItemStack(Items.BARRIER);
}
gui.setSlot(3, new GuiElementBuilder(displayStack)
.setName(Component.translatable("minions.gui.instruction.argument.configure.type"))
.addLoreLine(Component.translatable(TranslationUtil.getTranslationKey(
argumentType,
MinionRegistries.VALUE_SUPPLIER_TYPES,
"minions.gui.not_set"
)))
.setCallback(this::selectArgumentType)
);
}
private void updateArgumentConfiguration() {
if(argumentType != null) {
gui.setSlot(4, new GuiElementBuilder(Items.STRUCTURE_VOID)
.setName(Component.translatable("minions.gui.instruction.argument.configure.data"))
.addLoreLine(getArgument() != null ? getArgument().getDisplayText() : Component.translatable("minions.gui.not_set"))
.setCallback(() -> argumentType.openConfiguration(this, parameter.type(), getArgument())
.thenAccept(this::setArgument)
)
);
}
}
private void updateConverterConfiguration() {
if(entry != null) {
gui.setSlot(5, new GuiElementBuilder(Items.CRAFTER)
.setName(Component.translatable("minions.gui.instruction.converters"))
.setCallback(this::configureConvertersMenu)
);
}
}
@Override
protected void closeBacking() {
gui.close();
}
public void setArgumentType(ValueSupplierType<MinionRuntime> type) {
this.argumentType = type;
if(entry != null && getArgument().getType() != argumentType) {
instruction.getArguments().removeEntry(parameter);
entry = null;
}
updateTypeConfiguration();
}
public void setArgument(ValueSupplier<?, MinionRuntime> argument) {
if(entry != null) {
entry.setSupplier(argument);
} else {
entry = instruction.getArguments().createEntry(parameter, argument);
}
}
public void selectArgumentType() {
PaginatedList.createList(this, Component.translatable("minions.gui.instruction.argument.configure.type.title"), MinionRegistries.VALUE_SUPPLIER_TYPES, (type, me) ->
new GuiElementBuilder(GuiDisplay.getDisplayStackWithName(MinionRegistries.VALUE_SUPPLIER_TYPES, type, viewer.registryAccess()))
.setCallback(() -> {
setArgumentType(type);
me.goBack();
})
);
}
public void configureConvertersMenu() {
if(entry != null) {
new ConverterListGui(this, entry.getConverters(), entry.getSupplier().getValueType(), entry.getParameter().type());
}
}
}
@@ -0,0 +1,172 @@
package io.github.skippyall.minions.gui.instruction;
import eu.pb4.sgui.api.elements.GuiElementBuilder;
import eu.pb4.sgui.api.gui.SimpleGui;
import io.github.skippyall.minions.clipboard.ClipboardItem;
import io.github.skippyall.minions.gui.MinionsGui;
import io.github.skippyall.minions.gui.input.BooleanInput;
import io.github.skippyall.minions.gui.minion.GuiContext;
import io.github.skippyall.minions.minion.MinionListener;
import io.github.skippyall.minions.minion.MinionRuntime;
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
import io.github.skippyall.minions.program.instruction.ConfiguredInstruction;
import io.github.skippyall.minions.program.instruction.ConfiguredInstructionListener;
import io.github.skippyall.minions.program.supplier.Parameter;
import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.game.ClientboundSoundEntityPacket;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.item.Items;
import java.util.List;
public class ConfigureInstructionGui extends MinionsGui implements ConfiguredInstructionListener, MinionListener {
private String name;
private final ConfiguredInstruction<MinionRuntime> instruction;
private final MinionFakePlayer minion;
private final GuiContext.Instruction context;
private SimpleGui gui;
public ConfigureInstructionGui(MinionsGui parent, GuiContext.Instruction context) {
super(parent);
this.name = context.getName();
this.instruction = context.getInstruction();
this.minion = context.getMinion();
this.context = context;
minion.addMinionListener(this);
instruction.addListener(this);
open();
}
@Override
protected void open() {
gui = new SimpleGui(MenuType.GENERIC_9x3, viewer, false) {
@Override
public void onPlayerClose(boolean success) {
onBackingClosed();
}
};
gui.setTitle(Component.literal(name));
gui.setSlot(6, new GuiElementBuilder(Items.ANVIL)
.setName(Component.translatable("minions.gui.instruction.configure.rename"))
.setCallback(() -> InstructionGui.inputInstructionName(this, context, name).thenAccept(newName -> {
if(newName != null) {
minion.getInstructionManager().setInstructionName(name, newName);
}
reopen();
}))
);
gui.setSlot(7, new GuiElementBuilder(Items.LAVA_BUCKET)
.setName(Component.translatable("minions.gui.instruction.configure.delete"))
.setCallback(() -> BooleanInput.confirm(this, Component.translatable("minions.gui.instruction.configure.delete.confirm", name))
.thenAccept((confirmed) -> {
if(confirmed) {
minion.getInstructionManager().removeInstruction(name);
goBack();
}
}))
);
gui.setSlot(8, backButton());
updateSuppliers();
gui.setSlot(13, InstructionGui.createInstructionElement(instruction.getInstruction(), viewer.registryAccess()));
gui.setSlot(25, new GuiElementBuilder(Items.FEATHER)
.setName(Component.translatable("minions.gui.instruction.configure.copy"))
.addLoreLine(Component.translatable("minions.gui.instruction.configure.copy.description"))
.setCallback(() -> {
viewer.getInventory().placeItemBackInInventory(ClipboardItem.createInstructionReference(minion, name), true);
viewer.connection.send(new ClientboundSoundEntityPacket(SoundEvents.NOTE_BLOCK_CHIME, SoundSource.BLOCKS, viewer, 1, 1, 0));
})
);
updateLastError();
updateRunSlot();
gui.open();
}
@Override
protected void closeBacking() {
gui.close();
minion.removeMinionListener(this);
instruction.removeListener(this);
}
@Override
public void onInstructionRename(MinionFakePlayer minion, ConfiguredInstruction<?> instruction, String oldName, String newName) {
gui.setTitle(Component.literal(newName));
name = newName;
context.setName(newName);
}
@Override
public void onRun(ConfiguredInstruction<?> instruction) {
updateRunSlot();
updateLastError();
}
@Override
public void onStop(ConfiguredInstruction<?> instruction) {
updateRunSlot();
}
@Override
public void onSupplierChange(ConfiguredInstruction<?> instruction, Parameter<?> parameter) {
updateSuppliers();
}
private void updateRunSlot() {
List<Component> errors = instruction.preCheck();
if(errors.isEmpty()) {
if (!instruction.isRunning()) {
gui.setSlot(26, new GuiElementBuilder(Items.ARROW)
.setName(Component.translatable("minions.gui.instruction.run"))
.setCallback(() -> instruction.run(minion.getInstructionManager()))
);
} else {
gui.setSlot(26, new GuiElementBuilder(Items.BARRIER)
.setName(Component.translatable("minions.gui.instruction.stop"))
.setCallback(() -> instruction.stop(minion.getInstructionManager()))
);
}
} else {
GuiElementBuilder builder = new GuiElementBuilder(Items.RED_WOOL)
.setName(Component.translatable("minions.gui.instruction.errors"));
for(Component error : errors) {
builder.addLoreLine(error);
}
gui.setSlot(26, builder);
}
}
private void updateLastError() {
List<Component> errors = instruction.getLastErrors();
if(!errors.isEmpty()) {
GuiElementBuilder builder = new GuiElementBuilder(Items.RED_WOOL)
.setName(Component.translatable("minions.gui.instruction.last_errors"));
for(Component error : errors) {
builder.addLoreLine(error);
}
gui.setSlot(17, builder);
}
}
private void updateSuppliers() {
int slot = 12;
for(Parameter<?> parameter : instruction.getInstruction().getParameters().reversed()) {
gui.setSlot(slot, InstructionGui.createParameterElement(parameter, instruction.getArguments().getArgument(parameter), viewer.registryAccess())
.setCallback(() -> new ArgumentGui(this, GuiContext.ValueSupplier.create(context, parameter)))
);
slot--;
}
}
}
@@ -0,0 +1,123 @@
package io.github.skippyall.minions.gui.instruction;
import eu.pb4.sgui.api.elements.GuiElementBuilder;
import eu.pb4.sgui.api.gui.SimpleGui;
import io.github.skippyall.minions.gui.GuiDisplay;
import io.github.skippyall.minions.gui.MinionsGui;
import io.github.skippyall.minions.gui.PaginatedList;
import io.github.skippyall.minions.program.conversion.ConverterList;
import io.github.skippyall.minions.program.conversion.ValueConverter;
import io.github.skippyall.minions.program.conversion.ValueConverterType;
import io.github.skippyall.minions.program.value.ValueType;
import io.github.skippyall.minions.registration.MinionRegistries;
import net.minecraft.core.RegistryAccess;
import net.minecraft.network.chat.Component;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.item.Items;
import org.jspecify.annotations.Nullable;
public class ConverterGui extends MinionsGui {
private @Nullable ValueConverterType<?> valueConverterType;
private @Nullable ValueConverter<?,?> converter;
private ConverterList list;
private ValueType<?> from, to;
private SimpleGui gui;
private boolean isNew;
private int index;
public ConverterGui(MinionsGui parent, @Nullable ValueConverter<?,?> converter, ValueType<?> from, ValueType<?> to, ConverterList list, boolean isNew, int index) {
super(parent);
this.converter = converter;
if(converter != null) {
this.valueConverterType = converter.getType();
}
this.from = from;
this.to = to;
this.list = list;
this.isNew = isNew;
this.index = index;
open();
}
@Override
protected void open() {
gui = new SimpleGui(MenuType.GENERIC_3x3, viewer, false) {
@Override
public void onPlayerClose(boolean success) {
onBackingClosed();
}
};
gui.setTitle(Component.translatable("minions.gui.instruction.converter.title"));
updateTypeDisplay();
updateConverterDisplay();
gui.setSlot(8, backButton());
gui.open();
}
private void updateTypeDisplay() {
gui.setSlot(3, new GuiElementBuilder(GuiDisplay.getDisplayStackWithName(MinionRegistries.VALUE_CONVERTER_TYPES, valueConverterType, viewer.registryAccess()))
.setCallback(this::configureType)
);
}
private void updateConverterDisplay() {
gui.setSlot(5, new GuiElementBuilder(Items.STRUCTURE_VOID)
.setName(Component.translatable("minions.gui.instruction.converter.title"))
.addLoreLine(converter == null ? Component.translatable("minions.gui.not_set") : converter.getDisplayText())
.setCallback(this::configureData)
);
}
@Override
protected void closeBacking() {
gui.close();
}
private void setType(ValueConverterType<?> valueConverterType) {
this.valueConverterType = valueConverterType;
updateTypeDisplay();
}
public void setConverter(ValueConverter<?,?> converter) {
this.converter = converter;
if(isNew) {
list.getConverters().add(index, converter);
isNew = false;
} else {
list.getConverters().set(index, converter);
}
updateConverterDisplay();
}
private void configureType() {
PaginatedList.createList(
this,
Component.translatable("minions.gui.instruction.converter.type.title"),
MinionRegistries.VALUE_CONVERTER_TYPES,
(type, me) -> new GuiElementBuilder(
GuiDisplay.getDisplayStackWithName(MinionRegistries.VALUE_CONVERTER_TYPES, type, viewer.registryAccess())
).setCallback(() -> {
setType(type);
me.goBack();
})
);
}
private void configureData() {
if(valueConverterType != null) {
valueConverterType.configure(this, from, to, converter)
.thenAccept(this::setConverter);
}
}
public static GuiElementBuilder createConverterElement(ValueConverter<?,?> converter, RegistryAccess manager) {
GuiElementBuilder builder = new GuiElementBuilder(GuiDisplay.getDisplayStack(MinionRegistries.VALUE_CONVERTER_TYPES, converter.getType(), manager));
builder.addLoreLine(converter.getDisplayText());
return builder;
}
}
@@ -0,0 +1,180 @@
package io.github.skippyall.minions.gui.instruction;
import eu.pb4.sgui.api.elements.GuiElementBuilder;
import eu.pb4.sgui.api.gui.SimpleGui;
import io.github.skippyall.minions.gui.GuiDisplay;
import io.github.skippyall.minions.gui.MinionsGui;
import io.github.skippyall.minions.program.conversion.ConverterList;
import io.github.skippyall.minions.program.conversion.ValueConverter;
import io.github.skippyall.minions.program.value.ValueType;
import io.github.skippyall.minions.registration.MinionRegistries;
import io.github.skippyall.minions.util.TranslationUtil;
import net.minecraft.network.chat.Component;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.item.Items;
import org.jspecify.annotations.Nullable;
public class ConverterListGui extends MinionsGui {
private ConverterList converters;
private SimpleGui gui;
private int page = 0;
private ValueType<?> inputType;
private ValueType<?> outputType;
public ConverterListGui(MinionsGui parent, ConverterList converters, ValueType<?> inputType, ValueType<?> outputType) {
super(parent);
this.converters = converters;
this.inputType = inputType;
this.outputType = outputType;
open();
}
@Override
protected void open() {
gui = new SimpleGui(MenuType.GENERIC_9x3, viewer, false) {
@Override
public void onPlayerClose(boolean success) {
onBackingClosed();
}
};
gui.setTitle(Component.translatable("minions.gui.instruction.converters"));
updateConverters();
gui.setSlot(21, new GuiElementBuilder(Items.SPECTRAL_ARROW)
.setCallback(() -> {
if(page > 0) {
page--;
updateConverters();
}
})
);
gui.setSlot(23, new GuiElementBuilder(Items.ARROW)
.setCallback(() -> {
if(page * 4 + 4 < converters.getConverters().size() + 1) {
page++;
updateConverters();
}
})
);
gui.setSlot(26, backButton());
gui.open();
}
private ValueType<?> getInputTypeOfConverter(int converterIndex) {
if(converterIndex < converters.getConverters().size() && converterIndex >= 0) {
return converters.getConverters().get(converterIndex).getFrom();
} else if(converterIndex == converters.getConverters().size()) {
return outputType;
} else {
throw new IndexOutOfBoundsException("Can't get input type of converter " + converterIndex + ", list size " + converters.getConverters().size());
}
}
private ValueType<?> getOutputTypeOfConverter(int converterIndex) {
if(converterIndex < converters.getConverters().size() && converterIndex >= 0) {
return converters.getConverters().get(converterIndex).getTo();
} else if(converterIndex == -1) {
return inputType;
} else {
throw new IndexOutOfBoundsException("Can't get output type of converter " + converterIndex + ", list size " + converters.getConverters().size());
}
}
public void updateConverters() {
for(int slot = 0; slot < 18; slot++) {
gui.clearSlot(slot);
}
int lastConverter = Math.min(5, converters.getConverters().size() + 2 - page * 4);
for(int i = 0; i < lastConverter; i++) {
//Each page has 5 converters, but the last is displayed on the next page as well
//The input element index is -1, the output element index is the list size
int converterIndex = page * 4 + i - 1;
int slot = 9 + 2 * i;
if(converterIndex == converters.getConverters().size()) {
gui.setSlot(slot, new GuiElementBuilder(Items.HOPPER));
} else {
ValueType<?> thisConverterOutput = getOutputTypeOfConverter(converterIndex);
ValueType<?> nextConverterInput = getInputTypeOfConverter(converterIndex + 1);
//What should be shown at the converter's slot
if (converterIndex == -1) {
gui.setSlot(slot, new GuiElementBuilder(Items.DROPPER));
} else {
ValueType<?> previousConverterOutput = getOutputTypeOfConverter(converterIndex - 1);
ValueConverter<?, ?> converter = converters.getConverters().get(converterIndex);
gui.setSlot(slot, createConverterDisplay(converter, previousConverterOutput, nextConverterInput, converterIndex));
GuiElementBuilder warning = createConverterWarning(converter);
if (warning != null) {
gui.setSlot(slot - 8, warning);
}
}
//show cast in the next slot
if (i != 4 && converterIndex != converters.getConverters().size()) {
gui.setSlot(slot + 1, createCastDisplay(thisConverterOutput, nextConverterInput, converterIndex + 1));
GuiElementBuilder warning = createCastWarning(thisConverterOutput, nextConverterInput);
if (warning != null) {
gui.setSlot(slot - 8, warning);
}
}
}
}
}
private GuiElementBuilder createConverterDisplay(ValueConverter<?, ?> converter, ValueType<?> fromType, ValueType<?> toType, int converterIndex) {
return new GuiElementBuilder(GuiDisplay.getDisplayStackWithName(MinionRegistries.VALUE_CONVERTER_TYPES, converter.getType(), viewer.registryAccess()))
.addLoreLine(converter.getDisplayText())
.setCallback(() -> new ConverterGui(this, converter, fromType, toType, converters, false, converterIndex));
}
private GuiElementBuilder createCastDisplay(ValueType<?> fromType, ValueType<?> toType, int newConverterIndex) {
return new GuiElementBuilder(Items.MAGENTA_GLAZED_TERRACOTTA)
.setName(Component.translatable(
"minions.gui.instruction.converters.cast",
Component.translatable(TranslationUtil.getTranslationKey(fromType, MinionRegistries.VALUE_TYPES)),
Component.translatable(TranslationUtil.getTranslationKey(toType, MinionRegistries.VALUE_TYPES))
))
.setCallback(() -> new ConverterGui(this, null, fromType, toType, converters, true, newConverterIndex));
}
private @Nullable GuiElementBuilder createCastWarning(ValueType<?> fromType, ValueType<?> toType) {
Component warning = ConverterList.createCastWarning(fromType, toType);
if(warning != null) {
return new GuiElementBuilder(Items.RED_BANNER)
.setName(Component.translatable("minions.generic.error"))
.addLoreLine(warning);
} else {
return null;
}
}
private @Nullable GuiElementBuilder createConverterWarning(ValueConverter<?,?> converter) {
Component warning = ConverterList.createConverterWarning(converter);
if(warning != null) {
return new GuiElementBuilder(Items.RED_BANNER)
.setName(Component.translatable("minions.generic.error"))
.addLoreLine(warning);
} else {
return null;
}
}
@Override
protected void closeBacking() {
gui.close();
}
}
@@ -0,0 +1,185 @@
package io.github.skippyall.minions.gui.instruction;
import eu.pb4.sgui.api.elements.GuiElementBuilder;
import eu.pb4.sgui.api.gui.SimpleGui;
import io.github.skippyall.minions.gui.GuiDisplay;
import io.github.skippyall.minions.gui.MinionsGui;
import io.github.skippyall.minions.gui.input.Result;
import io.github.skippyall.minions.gui.input.TextInput;
import io.github.skippyall.minions.gui.minion.GuiContext;
import io.github.skippyall.minions.gui.minion.SimpleMinionsGui;
import io.github.skippyall.minions.minion.MinionRuntime;
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
import io.github.skippyall.minions.module.MinionModule;
import io.github.skippyall.minions.program.instruction.ConfiguredInstruction;
import io.github.skippyall.minions.program.instruction.InstructionType;
import io.github.skippyall.minions.program.supplier.Parameter;
import io.github.skippyall.minions.program.supplier.ValueSupplier;
import io.github.skippyall.minions.registration.MinionComponentTypes;
import io.github.skippyall.minions.registration.MinionRegistries;
import io.github.skippyall.minions.util.TranslationUtil;
import net.minecraft.core.RegistryAccess;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import org.jspecify.annotations.Nullable;
import java.util.NoSuchElementException;
import java.util.concurrent.CompletableFuture;
public class InstructionGui {
public static void openInstructionMainMenu(MinionsGui parent, GuiContext.Minion context) {
new SimpleMinionsGui(parent, (onClose, me) -> {
ServerPlayer player = parent.viewer;
SimpleGui gui = new SimpleGui(MenuType.GENERIC_3x3, player, false) {
@Override
public void onPlayerClose(boolean success) {
onClose.run();
}
};
gui.setTitle(Component.translatable("minions.gui.instruction.title"));
gui.setSlot(2, me.backButton());
gui.setSlot(3, new GuiElementBuilder()
.setItem(Items.BOOK)
.setName(Component.translatable("minions.gui.instruction.list"))
.setCallback(() -> new InstructionListGui(me, context))
);
gui.setSlot(5, new GuiElementBuilder()
.setItem(Items.WRITABLE_BOOK)
.setName(Component.translatable("minions.gui.instruction.create"))
.setCallback(() -> createNewInstruction(me, context))
);
gui.open();
return gui;
});
}
public static void createNewInstruction(MinionsGui parent, GuiContext.Minion context) {
MinionFakePlayer minion = context.getMinion();
ServerPlayer viewer = parent.viewer;
selectInstructionModuleMenu(parent, context).thenAccept(instructionType ->
inputInstructionName(parent, context, "Instruction").thenAccept(name -> {
if (!minion.isRemoved() && !minion.hasDisconnected() && name != null) {
ConfiguredInstruction<MinionRuntime> configuredInstruction = minion.getInstructionManager().createInstruction(name, instructionType);
new ConfigureInstructionGui(parent, GuiContext.Instruction.create(context, configuredInstruction, name));
}
})
);
}
public static CompletableFuture<@Nullable String> inputInstructionName(MinionsGui parent, GuiContext.Minion context, String defaultValue) {
return TextInput.input(parent, Component.translatable("minions.gui.instruction.enter_name"), defaultValue, (name, _) -> {
if (context.getMinion().getInstructionManager().hasInstruction(name)) {
return new Result.Error<>(Component.translatable("minions.gui.instruction.name_already_used"));
}
return new Result.Success<>(name);
});
}
public static boolean checkInstructionExists(String name, ConfiguredInstruction<?> instruction, MinionFakePlayer minion, ServerPlayer player) {
boolean stillExists = !minion.isRemoved() && !minion.hasDisconnected() && minion.getInstructionManager().getInstruction(name) == instruction;
if (!stillExists) {
player.closeContainer();
player.sendSystemMessage(Component.translatable("minions.gui.instruction.removed"));
}
return stillExists;
}
public static CompletableFuture<InstructionType<MinionRuntime>> selectInstructionModuleMenu(MinionsGui parent, GuiContext.Minion context) {
MinionFakePlayer minion = context.getMinion();
ServerPlayer viewer = parent.viewer;
if (minion.getModuleInventory().getModules().isEmpty()) {
viewer.sendSystemMessage(Component.translatable("minions.gui.instruction.no_modules"));
return CompletableFuture.failedFuture(new NoSuchElementException("No modules"));
}
CompletableFuture<InstructionType<MinionRuntime>> future = new CompletableFuture<>();
new SimpleMinionsGui(parent, (closeHandler, me) -> {
SimpleGui gui = new SimpleGui(MenuType.GENERIC_9x4, viewer, false) {
@Override
public void onPlayerClose(boolean success) {
if (!future.isDone()) {
future.cancel(false);
}
closeHandler.run();
}
};
gui.setTitle(Component.translatable("minions.gui.instruction.select_instruction"));
gui.setSlot(8, me.backButton());
for (int i = 0; i < minion.getModuleInventory().getContainerSize(); i++) {
ItemStack moduleItem = minion.getModuleInventory().getItem(i);
MinionModule module = moduleItem.get(MinionComponentTypes.MODULE);
if (module != null && !module.instructions().isEmpty()) {
gui.setSlot(i + 9, new GuiElementBuilder(moduleItem)
.setCallback(() -> selectInstructionMenu(parent, context, module)
.thenApply(future::complete)
)
);
}
}
gui.open();
return gui;
});
return future;
}
public static CompletableFuture<InstructionType<MinionRuntime>> selectInstructionMenu(MinionsGui parent, GuiContext.Minion context, MinionModule module) {
CompletableFuture<InstructionType<MinionRuntime>> future = new CompletableFuture<>();
new SimpleMinionsGui(parent, (closeHandler, me) -> {
SimpleGui gui = new SimpleGui(MenuType.GENERIC_9x4, parent.viewer, false) {
@Override
public void onPlayerClose(boolean success) {
if (!future.isDone()) {
future.cancel(false);
}
closeHandler.run();
}
};
gui.setTitle(Component.translatable("minions.gui.instruction.select_instruction"));
gui.setSlot(8, me.backButton());
int slot = 9;
for (InstructionType<MinionRuntime> instructionType : module.instructions()) {
gui.setSlot(slot, createInstructionElement(instructionType, parent.viewer.registryAccess())
.setCallback(() -> future.complete(instructionType))
);
slot++;
}
gui.open();
return gui;
});
return future;
}
public static GuiElementBuilder createInstructionElement(@Nullable InstructionType<MinionRuntime> instructionType, RegistryAccess manager) {
GuiElementBuilder instructionBuilder;
if (instructionType != null) {
instructionBuilder = new GuiElementBuilder(GuiDisplay.getDisplayStackWithName(MinionRegistries.INSTRUCTION_TYPES, instructionType, manager));
} else {
instructionBuilder = new GuiElementBuilder(Items.RED_WOOL)
.setName(Component.translatable("minions.gui.instruction.no_instruction_set"));
}
return instructionBuilder;
}
public static GuiElementBuilder createParameterElement(Parameter<?> parameter, @Nullable ValueSupplier<?,?> valueSupplier, RegistryAccess manager) {
GuiElementBuilder builder = new GuiElementBuilder(GuiDisplay.getDisplayStack(MinionRegistries.VALUE_TYPES, parameter.type(), manager))
.setName(Component.translatable("minions.gui.instruction.parameter", parameter.name(), Component.translatable(TranslationUtil.getTranslationKey(parameter.type(), MinionRegistries.VALUE_TYPES))));
if(valueSupplier != null) {
builder.addLoreLine(Component.translatable("minions.gui.instruction.argument", valueSupplier.getDisplayText()));
}
return builder;
}
}
@@ -0,0 +1,66 @@
package io.github.skippyall.minions.gui.instruction;
import eu.pb4.sgui.api.elements.GuiElementBuilder;
import eu.pb4.sgui.api.gui.SimpleGui;
import io.github.skippyall.minions.gui.GuiDisplay;
import io.github.skippyall.minions.gui.MinionsGui;
import io.github.skippyall.minions.gui.minion.GuiContext;
import io.github.skippyall.minions.minion.MinionListener;
import io.github.skippyall.minions.minion.MinionRuntime;
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
import io.github.skippyall.minions.program.instruction.ConfiguredInstruction;
import io.github.skippyall.minions.registration.MinionRegistries;
import net.minecraft.network.chat.Component;
import net.minecraft.world.inventory.MenuType;
public class InstructionListGui extends MinionsGui implements MinionListener {
private final GuiContext.Minion context;
private final MinionFakePlayer minion;
private SimpleGui gui;
public InstructionListGui(MinionsGui parent, GuiContext.Minion context) {
super(parent);
this.context = context;
this.minion = context.getMinion();
open();
}
@Override
public void onInstructionsUpdate(MinionFakePlayer minion) {
resetInstructionList();
}
@Override
protected void open() {
minion.addMinionListener(this);
gui = new SimpleGui(MenuType.GENERIC_9x4, viewer, false) {
@Override
public void onPlayerClose(boolean success) {
onBackingClosed();
}
};
gui.setTitle(Component.translatable("minions.gui.instruction.title"));
gui.setSlot(8, backButton());
resetInstructionList();
gui.open();
}
@Override
protected void closeBacking() {
minion.removeMinionListener(this);
gui.close();
}
private void resetInstructionList() {
int i = 9;
for (String instructionName : minion.getInstructionManager().getInstructionNames()) {
ConfiguredInstruction<MinionRuntime> instruction = minion.getInstructionManager().getInstruction(instructionName);
gui.setSlot(i, new GuiElementBuilder(GuiDisplay.getGuiDisplayFor(MinionRegistries.INSTRUCTION_TYPES, instruction.getInstruction(), viewer.registryAccess()).createItemStack())
.setName(Component.literal(instructionName))
.setCallback(() -> new ConfigureInstructionGui(this, GuiContext.Instruction.create(context, instruction, instructionName)))
);
i++;
}
}
}
@@ -0,0 +1,4 @@
@NullMarked
package io.github.skippyall.minions.gui.instruction;
import org.jspecify.annotations.NullMarked;
@@ -0,0 +1,66 @@
package io.github.skippyall.minions.gui.minion
import io.github.skippyall.minions.gui.minion.GuiContextImpl.InstructionImpl
import io.github.skippyall.minions.gui.minion.GuiContextImpl.MinionImpl
import io.github.skippyall.minions.gui.minion.GuiContextImpl.ValueSupplierImpl
import io.github.skippyall.minions.minion.MinionRuntime
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer
import io.github.skippyall.minions.program.instruction.ConfiguredInstruction
import io.github.skippyall.minions.program.supplier.Parameter
import net.minecraft.server.level.ServerPlayer
interface GuiContext {
val viewer: ServerPlayer
companion object {
@JvmStatic
fun create(viewer: ServerPlayer): GuiContext {
return GuiContextImpl(viewer)
}
}
interface Minion : GuiContext {
val minion: MinionFakePlayer
companion object {
@JvmStatic
fun create(context: GuiContext, minion: MinionFakePlayer): Minion {
return MinionImpl(
if(context is MinionImpl) context.context else context,
minion
)
}
}
}
interface Instruction : Minion {
val instruction: ConfiguredInstruction<MinionRuntime>
var name: String
companion object {
@JvmStatic
fun create(context: Minion, instruction: ConfiguredInstruction<MinionRuntime>, name: String): Instruction {
return InstructionImpl(
if(context is InstructionImpl) context.context else context,
instruction,
name
)
}
}
}
interface ValueSupplier : Instruction {
val parameter: Parameter<*>
companion object {
@JvmStatic
fun create(context: Instruction, parameter: Parameter<*>): ValueSupplier {
return ValueSupplierImpl(
if(context is ValueSupplierImpl) context.context else context,
parameter
)
}
}
}
}
@@ -0,0 +1,26 @@
package io.github.skippyall.minions.gui.minion
import io.github.skippyall.minions.minion.MinionRuntime
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer
import io.github.skippyall.minions.program.instruction.ConfiguredInstruction
import io.github.skippyall.minions.program.supplier.Parameter
import net.minecraft.server.level.ServerPlayer
//Thank you kotlin
class GuiContextImpl(override val viewer: ServerPlayer) : GuiContext {
class MinionImpl(
val context: GuiContext,
override val minion: MinionFakePlayer
) : GuiContext by context, GuiContext.Minion
class InstructionImpl(
val context: GuiContext.Minion,
override val instruction: ConfiguredInstruction<MinionRuntime>,
override var name: String
) : GuiContext.Minion by context, GuiContext.Instruction
class ValueSupplierImpl(
val context: GuiContext.Instruction,
override val parameter: Parameter<*>
) : GuiContext.Instruction by context, GuiContext.ValueSupplier
}
@@ -0,0 +1,78 @@
package io.github.skippyall.minions.gui.minion;
import eu.pb4.sgui.api.elements.GuiElementBuilder;
import eu.pb4.sgui.api.gui.SimpleGui;
import io.github.skippyall.minions.gui.MinionsGui;
import io.github.skippyall.minions.gui.instruction.InstructionGui;
import io.github.skippyall.minions.minion.MinionListener;
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
import io.github.skippyall.minions.module.ModuleInventory;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.item.Items;
public class MinionGui extends MinionsGui implements MinionListener {
private final MinionFakePlayer minion;
private SimpleGui gui;
public MinionGui(ServerPlayer viewer, MinionFakePlayer minion) {
super(viewer);
this.minion = minion;
minion.addMinionListener(this);
open();
}
public MinionFakePlayer getMinion() {
return minion;
}
@Override
protected void open() {
gui = new SimpleGui(MenuType.GENERIC_3x3, viewer, false) {
@Override
public void onPlayerClose(boolean success) {
onBackingClosed();
}
};
gui.setTitle(minion.getName());
gui.setSlot(1, new GuiElementBuilder()
.setItem(Items.COMMAND_BLOCK)
.setName(Component.translatable("minions.gui.main.instructions"))
.setCallback(() -> {
InstructionGui.openInstructionMainMenu(this, GuiContext.Minion.create(GuiContext.create(viewer), minion));
})
);
gui.setSlot(3, new GuiElementBuilder()
.setItem(Items.NETHERITE_UPGRADE_SMITHING_TEMPLATE)
.setName(Component.translatable("minions.gui.main.modules"))
.setCallback(() -> {
ModuleInventory.openModuleInventory(viewer, minion);
})
);
gui.setSlot(5, new GuiElementBuilder()
.setItem(Items.CHEST)
.setName(Component.translatable("minions.gui.main.inventory"))
.setCallback(() -> new MinionInventoryGui(this))
);
gui.setSlot(7, new GuiElementBuilder()
.setItem(Items.BARRIER)
.setName(Component.translatable("minions.gui.main.pickup"))
.setCallback(() -> minion.kill(minion.level()))
);
gui.open();
}
@Override
protected void closeBacking() {
gui.close();
minion.removeMinionListener(this);
}
@Override
public void onMinionRemove(MinionFakePlayer minion) {
close();
}
}
@@ -0,0 +1,70 @@
package io.github.skippyall.minions.gui.minion;
import eu.pb4.sgui.api.gui.SimpleGui;
import io.github.skippyall.minions.gui.MinionsGui;
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.ArmorSlot;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
public class MinionInventoryGui extends MinionsGui {
protected final MinionGui parent;
private final MinionFakePlayer minion;
private SimpleGui gui;
public MinionInventoryGui(MinionGui parent) {
super(parent);
this.parent = parent;
this.minion = parent.getMinion();
open();
}
@Override
protected void open() {
gui = new SimpleGui(MenuType.GENERIC_9x6, viewer, false) {
@Override
public void onPlayerClose(boolean success) {
onBackingClosed();
}
};
gui.setTitle(Component.translatable("minions.gui.inventory.title"));
for(int i = 0; i < 18; i++) {
gui.setSlot(i, new ItemStack(Items.BARRIER));
}
gui.setSlot(2, new ItemStack(Items.LEATHER_HELMET));
gui.setSlot(3, new ItemStack(Items.LEATHER_CHESTPLATE));
gui.setSlot(4, new ItemStack(Items.LEATHER_LEGGINGS));
gui.setSlot(5, new ItemStack(Items.LEATHER_BOOTS));
gui.setSlot(6, new ItemStack(Items.SHIELD));
gui.setSlot(8, backButton());
gui.setSlot(2 + 9, new ArmorSlot(minion.getInventory(), minion, EquipmentSlot.HEAD, EquipmentSlot.HEAD.getIndex(Inventory.INVENTORY_SIZE), 0, 0, null));
gui.setSlot(3 + 9, new ArmorSlot(minion.getInventory(), minion, EquipmentSlot.CHEST, EquipmentSlot.CHEST.getIndex(Inventory.INVENTORY_SIZE), 0, 0, null));
gui.setSlot(4 + 9, new ArmorSlot(minion.getInventory(), minion, EquipmentSlot.LEGS, EquipmentSlot.LEGS.getIndex(Inventory.INVENTORY_SIZE), 0, 0, null));
gui.setSlot(5 + 9, new ArmorSlot(minion.getInventory(), minion, EquipmentSlot.FEET, EquipmentSlot.FEET.getIndex(Inventory.INVENTORY_SIZE), 0, 0, null));
gui.setSlot(6 + 9, new Slot(minion.getInventory(), Inventory.SLOT_OFFHAND, 0, 0));
for (int i = Inventory.SELECTION_SIZE; i < Inventory.INVENTORY_SIZE; i++) {
gui.setSlot(i + 9, new Slot(minion.getInventory(), i, 0, 0));
}
for (int i = 0; i < Inventory.SELECTION_SIZE; i++) {
gui.setSlot(i + 45, new Slot(minion.getInventory(), i, 0, 0));
}
gui.open();
}
@Override
protected void closeBacking() {
gui.close();
}
}
@@ -0,0 +1,27 @@
package io.github.skippyall.minions.gui.minion;
import eu.pb4.sgui.api.gui.GuiLike;
import io.github.skippyall.minions.gui.MinionsGui;
import java.util.function.BiFunction;
public class SimpleMinionsGui extends MinionsGui {
private GuiLike gui;
private final BiFunction<Runnable, SimpleMinionsGui, GuiLike> guiFactory;
public SimpleMinionsGui(MinionsGui parent, BiFunction<Runnable, SimpleMinionsGui, GuiLike> guiFactory) {
super(parent);
this.guiFactory = guiFactory;
open();
}
@Override
protected void open() {
gui = guiFactory.apply(this::onBackingClosed, this);
}
@Override
protected void closeBacking() {
gui.close();
}
}
@@ -0,0 +1,4 @@
@NullMarked
package io.github.skippyall.minions.gui.minion;
import org.jspecify.annotations.NullMarked;
@@ -0,0 +1,4 @@
@NullMarked
package io.github.skippyall.minions.gui;
import org.jspecify.annotations.NullMarked;
@@ -1,117 +0,0 @@
package io.github.skippyall.minions.input;
import org.jetbrains.annotations.NotNull;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
public interface Result<T, E> {
static <T> Result<T, String> wrap(UnsafeOperation<T> toWrap) {
return wrapCustomError(toWrap, Exception::getMessage);
}
static <T, E> Result<T, E> wrapCustomError(UnsafeOperation<T> toWrap, E error) {
return wrapCustomError(toWrap, e -> error);
}
static <T, E> Result<T, E> wrapCustomError(UnsafeOperation<T> toWrap, Function<Exception, E> errorTransformer) {
try {
return new Result.Success<>(toWrap.run());
} catch (Exception e) {
return new Result.Error<>(errorTransformer.apply(e));
}
}
boolean isSuccess();
@NotNull T getOrDefault(@NotNull T defaultValue);
@NotNull T getOrThrow();
@NotNull E getErrorOrThrow();
@NotNull Optional<T> getOptional();
void ifSuccess(@NotNull Consumer<T> handler);
void ifError(@NotNull Consumer<Error<T, E>> handler);
record Success<T, E>(@NotNull T result) implements Result<T, E> {
@Override
public boolean isSuccess() {
return true;
}
@Override
public @NotNull T getOrDefault(@NotNull T defaultValue) {
return result;
}
@Override
public @NotNull T getOrThrow() {
return result;
}
@Override
public @NotNull E getErrorOrThrow() {
throw new RuntimeException("Result was not an Error");
}
@Override
public @NotNull Optional<T> getOptional() {
return Optional.of(result);
}
@Override
public void ifSuccess(@NotNull Consumer<T> handler) {
handler.accept(result);
}
@Override
public void ifError(@NotNull Consumer<Error<T, E>> handler) {
}
}
record Error<T, E>(@NotNull E message) implements Result<T, E> {
@Override
public boolean isSuccess() {
return false;
}
@Override
public @NotNull T getOrDefault(@NotNull T defaultValue) {
return defaultValue;
}
@Override
public @NotNull T getOrThrow() {
throw new RuntimeException("Result was an error: " + message.toString());
}
@Override
public @NotNull E getErrorOrThrow() {
return message;
}
@Override
public @NotNull Optional<T> getOptional() {
return Optional.empty();
}
@Override
public void ifSuccess(@NotNull Consumer<T> handler) {
}
@Override
public void ifError(Consumer<Error<T, E>> handler) {
handler.accept(this);
}
}
interface UnsafeOperation<T> {
T run() throws Exception;
}
}
@@ -1,90 +0,0 @@
package io.github.skippyall.minions.input;
import eu.pb4.sgui.api.elements.GuiElementBuilder;
import eu.pb4.sgui.api.gui.AnvilInputGui;
import net.minecraft.item.Items;
import net.minecraft.screen.AnvilScreenHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import java.util.function.Predicate;
public class TextInput<T> extends AnvilInputGui {
private final GuiElementBuilder valid = new GuiElementBuilder()
.setItem(Items.EMERALD_BLOCK)
.setName(Text.literal("OK"))
.setCallback(this::onConfirm);
private final GuiElementBuilder invalid = new GuiElementBuilder()
.setItem(Items.REDSTONE_BLOCK);
private final Function<String, CompletableFuture<Result<T, Text>>> parser;
private final CompletableFuture<T> future;
private Result<T, Text> result;
public TextInput(ServerPlayerEntity player, Text title, String defaultValue, Function<String, CompletableFuture<Result<T, Text>>> parser, CompletableFuture<T> future) {
super(player, false);
setTitle(title);
setDefaultInputValue(defaultValue);
this.parser = parser;
this.future = future;
updateConfirmButton(defaultValue);
}
public static <T> CompletableFuture<T> inputSync(ServerPlayerEntity player, Text title, String defaultValue, Function<String, Result<T, Text>> parser) {
return input(player, title, defaultValue, (String string) -> CompletableFuture.completedFuture(parser.apply(string)));
}
public static <T> CompletableFuture<T> input(ServerPlayerEntity player, Text title, String defaultValue, Function<String, CompletableFuture<Result<T, Text>>> parser) {
CompletableFuture<T> future = new CompletableFuture<>();
new TextInput<>(player, title, defaultValue, parser, future).open();
return future;
}
public static CompletableFuture<String> inputString(ServerPlayerEntity player, Text title, String defaultValue) {
return inputSync(player, title, defaultValue, Result.Success::new);
}
public static CompletableFuture<Integer> inputInt(ServerPlayerEntity player, Text title, String defaultValue) {
return inputSync(player, title, defaultValue, string -> Result.wrapCustomError(() -> Integer.valueOf(string), Text.translatable("minions.command.input.int.fail")));
}
public static CompletableFuture<Float> inputFloat(ServerPlayerEntity player, Text title, String defaultValue) {
return inputSync(player, title, defaultValue, string -> Result.wrapCustomError(() -> Float.valueOf(string), Text.translatable("minions.command.input.float.fail")));
}
@Override
public void onInput(String input) {
updateConfirmButton(input);
}
public void updateConfirmButton(String input) {
parser.apply(input).thenAccept(result -> {
this.result = result;
if(result.isSuccess()) {
setSlot(AnvilScreenHandler.OUTPUT_ID, valid);
} else {
Text text = result.getErrorOrThrow();
setSlot(AnvilScreenHandler.OUTPUT_ID, invalid.setName(text));
}
});
}
@Override
public void onClose() {
if(!future.isDone()) {
future.cancel(false);
}
}
public void onConfirm() {
if(result != null) {
result.ifSuccess(success -> {
future.complete(success);
close();
});
}
}
}
@@ -0,0 +1,54 @@
package io.github.skippyall.minions.listener;
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
import io.github.skippyall.minions.program.instruction.ConfiguredInstructionListener;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import java.util.Map;
import java.util.UUID;
public abstract class BlockEntityMinionInstructionListener<E extends BlockEntity> extends BlockEntityMinionListener<E> {
protected BlockEntityMinionInstructionListener(ResourceKey<Level> worldKey, BlockPos pos, UUID minionUuid, BlockEntityType<E> type) {
super(worldKey, pos, minionUuid, type);
}
protected abstract Map<String, ConfiguredInstructionListener> getInstructionListeners();
@Override
public void onMinionSpawn(MinionFakePlayer minion) {
super.onMinionSpawn(minion);
registerInstructionListeners();
}
@Override
public void add(MinecraftServer server) {
super.add(server);
if(minion != null) {
registerInstructionListeners();
}
}
@Override
public void remove(MinecraftServer server) {
super.remove(server);
if(minion != null) {
removeInstructionListeners();
}
}
public void registerInstructionListeners() {
for(Map.Entry<String, ConfiguredInstructionListener> listener : getInstructionListeners().entrySet()) {
minion.getInstructionManager().getInstruction(listener.getKey()).addListener(listener.getValue());
}
}
public void removeInstructionListeners() {
for(Map.Entry<String, ConfiguredInstructionListener> listener : getInstructionListeners().entrySet()) {
minion.getInstructionManager().getInstruction(listener.getKey()).removeListener(listener.getValue());
}
}
}
@@ -0,0 +1,113 @@
package io.github.skippyall.minions.listener;
import com.mojang.datafixers.util.Function3;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import io.github.skippyall.minions.minion.MinionListener;
import io.github.skippyall.minions.minion.MinionPersistentState;
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
import net.minecraft.core.BlockPos;
import net.minecraft.core.UUIDUtil;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import org.jspecify.annotations.Nullable;
import java.util.Optional;
import java.util.UUID;
public abstract class BlockEntityMinionListener<E extends BlockEntity> implements MinionListener {
protected ResourceKey<Level> worldKey;
protected BlockPos pos;
protected UUID minionUuid;
protected BlockEntityType<E> type;
protected @Nullable MinionFakePlayer minion;
protected BlockEntityMinionListener(ResourceKey<Level> worldKey, BlockPos pos, UUID minionUuid, BlockEntityType<E> type) {
this.worldKey = worldKey;
this.pos = pos;
this.minionUuid = minionUuid;
this.type = type;
}
@Override
public void onMinionSpawn(MinionFakePlayer minion) {
MinionListener.super.onMinionSpawn(minion);
this.minion = minion;
removeIfBeRemoved(minion.getServer());
}
@Override
public void onMinionRemove(MinionFakePlayer minion) {
MinionListener.super.onMinionRemove(minion);
this.minion = null;
}
public static <T extends BlockEntityMinionListener<?>> @Nullable T getListener(Level world, BlockPos pos, @Nullable UUID minionUuid, Class<T> clazz) {
if(minionUuid != null) {
for (MinionListener listener : MinionPersistentState.get(world.getServer()).getMinionData(minionUuid).getListeners()) {
if (listener instanceof BlockEntityMinionListener<?> tl && tl.pos.equals(pos) && tl.worldKey.equals(world.dimension()) && clazz.isInstance(tl)) {
return clazz.cast(tl);
}
}
}
return null;
}
public static <L extends BlockEntityMinionListener<?>> Codec<L> getCodec(Function3<ResourceKey<Level>, BlockPos, UUID, L> constructor) {
return RecordCodecBuilder.create(instance ->
instance.group(
Level.RESOURCE_KEY_CODEC.fieldOf("world").forGetter(listener -> listener.worldKey),
BlockPos.CODEC.fieldOf("pos").forGetter(listener -> listener.pos),
UUIDUtil.AUTHLIB_CODEC.fieldOf("minionUuid").forGetter(listener -> listener.minionUuid)
).apply(instance, constructor));
}
private BlockEntityState getBlockEntityState(MinecraftServer server) {
Level world = server.getLevel(worldKey);
if(world == null || !world.isLoaded(pos)) {
return BlockEntityState.UNLOADED;
}
if(world.getBlockEntity(pos, type).isPresent()) {
return BlockEntityState.LOADED;
} else {
return BlockEntityState.REMOVED;
}
}
public Optional<E> getBlockEntity(MinecraftServer server) {
Level world = server.getLevel(worldKey);
if(world != null && world.isLoaded(pos)) {
return world.getBlockEntity(pos, type);
}
return Optional.empty();
}
public boolean removeIfBeRemoved(MinecraftServer server) {
if(getBlockEntityState(server) == BlockEntityState.REMOVED) {
remove(server);
return true;
}
return false;
}
public void add(MinecraftServer server) {
MinionPersistentState.get(server).getMinionData(minionUuid).getListeners().addListener(this);
MinionPersistentState.get(server).setDirty();
this.minion = (MinionFakePlayer) server.getPlayerList().getPlayer(minionUuid);
}
public void remove(MinecraftServer server) {
MinionPersistentState.get(server).getMinionData(minionUuid).getListeners().removeListener(this);
MinionPersistentState.get(server).setDirty();
}
public enum BlockEntityState {
LOADED,
REMOVED,
UNLOADED
}
}
@@ -0,0 +1,29 @@
package io.github.skippyall.minions.listener
import java.util.concurrent.CopyOnWriteArraySet
open class ListenerManager<T>(
protected val listeners: MutableSet<T> = CopyOnWriteArraySet(),
val onChange: () -> Unit = {},
) : MutableIterable<T> by listeners {
fun addListener(listener: T) {
listeners.add(listener)
onChange()
}
fun removeListener(listener: T) {
listeners.remove(listener)
onChange()
}
override fun iterator(): MutableIterator<T> {
val iterator = listeners.iterator()
return object : MutableIterator<T> by iterator {
override fun remove() {
iterator.remove()
onChange()
}
}
}
}
@@ -0,0 +1,47 @@
package io.github.skippyall.minions.listener
import com.mojang.serialization.Codec
import com.mojang.serialization.MapCodec
import io.github.skippyall.minions.listener.SerializableListenerManager.SerializableListener
import net.minecraft.core.Registry
import net.minecraft.resources.Identifier
import java.util.*
import java.util.concurrent.CopyOnWriteArraySet
class SerializableListenerManager<T : SerializableListener>(
listeners: MutableSet<T> = CopyOnWriteArraySet(),
onChange: () -> Unit = {},
) : ListenerManager<T>(listeners, onChange) {
interface SerializableListener {
val codecId: Optional<Identifier>
get() = Optional.empty<Identifier>()
}
companion object {
@JvmStatic
@JvmOverloads
fun <T : SerializableListener> getCodec(
registry: Registry<Codec<out T>>,
onChange: () -> Unit = {},
): Codec<SerializableListenerManager<T>> {
return registry.byNameCodec().dispatch(
{ listener -> listener.codecId.map(registry::getValue)
.orElseGet { MapCodec.unitCodec(null) }
},
{ codec -> codec.fieldOf("data") }
).listOf().xmap(
{ list -> SerializableListenerManager<T>(CopyOnWriteArraySet<T>(list), onChange) },
{ manager ->
val serializableListeners: MutableList<T> = mutableListOf()
for (listener in manager.listeners) {
if (listener.codecId.isPresent) {
serializableListeners.add(listener)
}
}
return@xmap serializableListeners
}
)
}
}
}
@@ -0,0 +1,4 @@
@NullMarked
package io.github.skippyall.minions.listener;
import org.jspecify.annotations.NullMarked;
@@ -0,0 +1,63 @@
package io.github.skippyall.minions.minion;
import com.mojang.serialization.Codec;
import io.github.skippyall.minions.registration.MinionRegistries;
import net.minecraft.resources.Identifier;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
public class MinionConfig {
public static final Codec<MinionConfig> CODEC = Codec.<Option<?>, Object>dispatchedMap(
MinionRegistries.MINION_CONFIG_OPTIONS.byNameCodec(),
Option::codec
).xmap(MinionConfig::new, config -> config.values);
private final Map<Option<?>, Object> values;
public MinionConfig() {
values = new HashMap<>();
}
private MinionConfig(Map<Option<?>, Object> values) {
this.values = new HashMap<>(values);
}
public <T> T getOption(Option<T> option) {
if(values.containsKey(option)) {
//noinspection unchecked
return (T) values.get(option);
} else {
return option.defaultValue();
}
}
@Override
public boolean equals(Object o) {
if (!(o instanceof MinionConfig that)) return false;
return Objects.equals(values, that.values);
}
@Override
public int hashCode() {
return Objects.hashCode(values);
}
public static Option<Boolean> booleanOption(Identifier key, boolean defaultValue) {
return new Option<>(key, defaultValue, Codec.BOOL);
}
public record Option<T>(Identifier key, T defaultValue, Codec<T> codec) {
@Override
public boolean equals(Object o) {
if (!(o instanceof Option<?> option)) return false;
return Objects.equals(key, option.key);
}
@Override
public int hashCode() {
return Objects.hashCode(key);
}
}
}
@@ -1,65 +0,0 @@
package io.github.skippyall.minions.minion;
import com.mojang.authlib.properties.PropertyMap;
import com.mojang.datafixers.util.Pair;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import eu.pb4.polymer.core.api.other.PolymerComponent;
import io.github.skippyall.minions.Minions;
import net.minecraft.component.ComponentType;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtOps;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.util.Identifier;
import net.minecraft.util.Uuids;
import net.minecraft.util.dynamic.Codecs;
import org.jetbrains.annotations.Nullable;
import java.util.Optional;
import java.util.UUID;
public record MinionData(UUID uuid, String name, Optional<PropertyMap> skin, boolean isSpawned) {
public static final Codec<MinionData> CODEC = RecordCodecBuilder.create(instance ->
instance.group(
Uuids.CODEC.fieldOf("uuid").forGetter(MinionData::uuid),
Codec.STRING.fieldOf("name").forGetter(MinionData::name),
Codecs.GAME_PROFILE_PROPERTY_MAP.optionalFieldOf("skin").forGetter(MinionData::skin),
Codec.BOOL.optionalFieldOf("isSpawned", false).forGetter(MinionData::isSpawned)
).apply(instance, MinionData::new)
);
public static final ComponentType<UUID> COMPONENT = Registry.register(Registries.DATA_COMPONENT_TYPE, Identifier.of(Minions.MOD_ID, "minion_data"), ComponentType.<UUID>builder().codec(Uuids.CODEC).build());
public static MinionData createDefault() {
return new MinionData(UUID.randomUUID(), MinionProfileUtils.newDefaultMinionName(), Optional.empty(), false);
}
public MinionData withUuid(UUID uuid) {
return new MinionData(uuid, name, skin, isSpawned);
}
public MinionData withName(String name) {
return new MinionData(uuid, name, skin, isSpawned);
}
public MinionData withSkin(Optional<PropertyMap> skin) {
return new MinionData(uuid, name, skin, isSpawned);
}
public MinionData withSpawned(boolean isSpawned) {
return new MinionData(uuid, name, skin, isSpawned);
}
public NbtCompound writeNbt() {
return (NbtCompound) MinionData.CODEC.encode(this, NbtOps.INSTANCE, null).result().orElse(new NbtCompound());
}
public static MinionData readNbt(NbtCompound nbt) {
return MinionData.CODEC.decode(NbtOps.INSTANCE, nbt).resultOrPartial().map(Pair::getFirst).orElseGet(MinionData::createDefault);
}
public static void register() {
PolymerComponent.registerDataComponent(COMPONENT);
}
}
@@ -0,0 +1,81 @@
package io.github.skippyall.minions.minion
import com.mojang.authlib.properties.PropertyMap
import com.mojang.serialization.Codec
import com.mojang.serialization.codecs.RecordCodecBuilder
import io.github.skippyall.minions.listener.SerializableListenerManager
import io.github.skippyall.minions.registration.MinionRegistries
import net.minecraft.core.UUIDUtil
import net.minecraft.server.MinecraftServer
import net.minecraft.util.ExtraCodecs
import java.util.*
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
class MinionData(
initialUuid: UUID,
initialName: String,
initialSkin: Optional<PropertyMap>,
initialIsSpawned: Boolean,
val listeners: SerializableListenerManager<MinionListener>,
initialConfig: MinionConfig,
var onDirty: Runnable = {},
) {
var uuid by DirtyProperty(initialUuid, this::setDirty)
var name by DirtyProperty(initialName, this::setDirty)
var skin by DirtyProperty(initialSkin, this::setDirty)
var isSpawned by DirtyProperty(initialIsSpawned, this::setDirty)
var config by DirtyProperty(initialConfig, this::setDirty)
fun setDirty() {
this@MinionData.onDirty.run()
}
companion object {
@JvmField
val CODEC: Codec<MinionData> =
RecordCodecBuilder.create { instance ->
instance.group(
UUIDUtil.AUTHLIB_CODEC.fieldOf("uuid").forGetter(MinionData::uuid),
Codec.STRING.fieldOf("name").forGetter(MinionData::name),
ExtraCodecs.PROPERTY_MAP.optionalFieldOf("skin").forGetter(MinionData::skin),
Codec.BOOL.optionalFieldOf("isSpawned", false).forGetter(MinionData::isSpawned),
SerializableListenerManager.getCodec<MinionListener>(MinionRegistries.MINION_LISTENER_CODECS)
.optionalFieldOf("listeners").xmap(
{ optional -> optional.orElseGet { SerializableListenerManager() } },
Optional<SerializableListenerManager<MinionListener>>::of
).forGetter(MinionData::listeners),
MinionConfig.CODEC.optionalFieldOf("config", MinionConfig())
.forGetter(MinionData::config)
).apply(
instance,
::MinionData
)
}
@JvmStatic
fun createDefault(server: MinecraftServer): MinionData {
return MinionData(
UUID.randomUUID(),
MinionProfileUtils.newDefaultMinionName(server),
Optional.empty<PropertyMap>(),
false,
SerializableListenerManager(),
MinionConfig()
) {
MinionPersistentState.get(server).isDirty = true
}
}
}
class DirtyProperty<V>(var value: V, val onDirty: () -> Unit) : ReadWriteProperty<Any?, V> {
override fun getValue(thisRef: Any?, property: KProperty<*>): V {
return value
}
override fun setValue(thisRef: Any?, property: KProperty<*>, value: V) {
this.value = value
onDirty()
}
}
}
@@ -1,103 +1,78 @@
package io.github.skippyall.minions.minion; package io.github.skippyall.minions.minion;
import eu.pb4.polymer.core.api.item.PolymerItem;
import eu.pb4.polymer.core.api.item.PolymerItemUtils;
import io.github.skippyall.minions.gui.MinionLookGui; import io.github.skippyall.minions.gui.MinionLookGui;
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer; import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
import net.minecraft.component.DataComponentTypes; import io.github.skippyall.minions.registration.MinionComponentTypes;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.server.MinecraftServer;
import net.minecraft.item.Item; import net.minecraft.server.level.ServerLevel;
import net.minecraft.item.ItemStack; import net.minecraft.server.level.ServerPlayer;
import net.minecraft.item.ItemUsageContext; import net.minecraft.world.InteractionHand;
import net.minecraft.item.Items; import net.minecraft.world.InteractionResult;
import net.minecraft.item.tooltip.TooltipType; import net.minecraft.world.entity.player.Player;
import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.world.item.Item;
import net.minecraft.server.world.ServerWorld; import net.minecraft.world.item.ItemStack;
import net.minecraft.text.Text; import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.util.ActionResult; import net.minecraft.world.level.Level;
import net.minecraft.util.Hand; import net.minecraft.world.phys.Vec2;
import net.minecraft.util.Identifier; import org.jspecify.annotations.Nullable;
import net.minecraft.util.math.Vec2f;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;
import xyz.nucleoid.packettweaker.PacketContext;
import java.util.List; public class MinionItem extends Item {
public MinionItem(Properties settings) {
public class MinionItem extends Item implements PolymerItem {
public MinionItem(Settings settings) {
super(settings); super(settings);
} }
/*@Override
public void appendHoverText(ItemStack stack, TooltipContext context, TooltipDisplay component, Consumer<Component> tooltip, TooltipFlag type) {
//MinionData data = getData(stack);
//if(data != null) {
// tooltip.accept(Component.translatable("minions.minion_item.tooltip", data.name()));
//}
}*/
@Override @Override
public @Nullable Identifier getPolymerItemModel(ItemStack stack, PacketContext context) { public InteractionResult use(Level world, Player user, InteractionHand hand) {
return null; if(user instanceof ServerPlayer serverPlayer) {
ItemStack stack = user.getItemInHand(hand);
new MinionLookGui(serverPlayer, stack);
return InteractionResult.SUCCESS;
}
return InteractionResult.SUCCESS_SERVER;
} }
@Override @Override
public Item getPolymerItem(ItemStack itemStack, PacketContext player) { public InteractionResult useOn(UseOnContext context) {
return Items.ARMOR_STAND; if(!context.getLevel().isClientSide()) {
MinionData data = getDataOrDefault(context.getLevel().getServer(), context.getItemInHand());
MinionFakePlayer.spawnMinion(data, (ServerLevel) context.getLevel(), context.getClickedPos().getCenter().add(0,0.5,0), new Vec2(0, 0));
}
context.getItemInHand().shrink(1);
return InteractionResult.SUCCESS;
} }
@Override public static void setData(MinecraftServer server, MinionData data, ItemStack item) {
public ItemStack getPolymerItemStack(ItemStack stack, TooltipType tooltipType, PacketContext player) { item.set(MinionComponentTypes.MINION_DATA, data.getUuid());
ItemStack out = PolymerItemUtils.createItemStack(stack, tooltipType, player); MinionPersistentState.get(server).updateMinionData(data);
out.set(DataComponentTypes.ENCHANTMENT_GLINT_OVERRIDE, true);
return out;
}
@Override
public void appendTooltip(ItemStack stack, TooltipContext context, List<Text> tooltip, TooltipType type) {
MinionData data = getData(stack);
if(data != null) {
tooltip.add(Text.translatable("minions.minion_item.tooltip", data.name()));
}
}
@Override
public ActionResult use(World world, PlayerEntity user, Hand hand) {
if(user instanceof ServerPlayerEntity serverPlayer) {
ItemStack stack = user.getStackInHand(hand);
MinionLookGui.open(serverPlayer, stack);
return ActionResult.SUCCESS;
}
return ActionResult.SUCCESS_SERVER;
}
@Override
public ActionResult useOnBlock(ItemUsageContext context) {
if(!context.getWorld().isClient) {
MinionData data = getDataOrDefault(context.getStack());
MinionFakePlayer.spawnMinion(data, (ServerWorld) context.getWorld(), context.getBlockPos().toCenterPos().add(0,0.5,0), new Vec2f(0, 0));
}
context.getStack().decrement(1);
return ActionResult.SUCCESS;
}
public static void setData(MinionData data, ItemStack item) {
item.set(MinionData.COMPONENT, data.uuid());
MinionPersistentState.INSTANCE.updateMinionData(data);
} }
@Nullable @Nullable
public static MinionData getData(ItemStack item) { public static MinionData getData(MinecraftServer server, ItemStack item) {
if(item.contains(MinionData.COMPONENT)) { if(item.has(MinionComponentTypes.MINION_DATA)) {
return MinionPersistentState.INSTANCE.getMinionData(item.get(MinionData.COMPONENT)); return MinionPersistentState.get(server).getMinionData(item.get(MinionComponentTypes.MINION_DATA));
} }
return null; return null;
} }
public static MinionData getDataOrDefault(ItemStack item) { public static MinionData getDataOrDefault(MinecraftServer server, ItemStack item) {
MinionData data = getData(item); MinionData data = getData(server, item);
if(data == null) { if(data == null) {
data = MinionData.createDefault(); data = MinionData.createDefault(server);
setData(data, item); setData(server, data, item);
} }
return data; return data;
} }
public static boolean containsData(ItemStack item) { public static boolean containsData(ItemStack item) {
return item.contains(MinionData.COMPONENT); return item.has(MinionComponentTypes.MINION_DATA);
} }
} }
@@ -0,0 +1,48 @@
package io.github.skippyall.minions.minion;
import io.github.skippyall.minions.listener.SerializableListenerManager;
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
import io.github.skippyall.minions.program.instruction.ConfiguredInstruction;
import org.jspecify.annotations.Nullable;
public interface MinionListener extends SerializableListenerManager.SerializableListener {
default void onMinionSpawn(MinionFakePlayer minion) {}
default void onMinionRemove(MinionFakePlayer minion) {}
default void onInstructionsUpdate(MinionFakePlayer minion) {}
default void onInstructionRename(MinionFakePlayer minion, ConfiguredInstruction<?> instruction, String oldName, String newName) {}
interface Delegating extends MinionListener {
@Nullable MinionListener getBacking();
@Override
default void onMinionSpawn(MinionFakePlayer minion) {
if(getBacking() != null) {
getBacking().onMinionSpawn(minion);
}
}
@Override
default void onMinionRemove(MinionFakePlayer minion) {
if(getBacking() != null) {
getBacking().onMinionRemove(minion);
}
}
@Override
default void onInstructionsUpdate(MinionFakePlayer minion) {
if(getBacking() != null) {
getBacking().onInstructionsUpdate(minion);
}
}
@Override
default void onInstructionRename(MinionFakePlayer minion, ConfiguredInstruction<?> instruction, String oldName, String newName) {
if(getBacking() != null) {
getBacking().onInstructionRename(minion, instruction, oldName, newName);
}
}
}
}
@@ -1,48 +1,44 @@
package io.github.skippyall.minions.minion; package io.github.skippyall.minions.minion;
import net.minecraft.nbt.NbtCompound; import com.mojang.serialization.Codec;
import net.minecraft.nbt.NbtElement; import io.github.skippyall.minions.Minions;
import net.minecraft.nbt.NbtList; import net.minecraft.resources.Identifier;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.server.MinecraftServer; import net.minecraft.server.MinecraftServer;
import net.minecraft.world.PersistentState; import net.minecraft.world.level.Level;
import net.minecraft.world.World; import net.minecraft.world.level.saveddata.SavedData;
import net.minecraft.world.level.saveddata.SavedDataType;
import org.jspecify.annotations.Nullable;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import java.util.UUID; import java.util.UUID;
public class MinionPersistentState extends PersistentState { public class MinionPersistentState extends SavedData {
public static Type<MinionPersistentState> TYPE = new Type<>(MinionPersistentState::new, MinionPersistentState::read, null); public static final Codec<MinionPersistentState> CODEC = MinionData.CODEC.listOf().xmap(MinionPersistentState::new, MinionPersistentState::getMinionDataList);
public static MinionPersistentState INSTANCE; public static SavedDataType<MinionPersistentState> TYPE = new SavedDataType<>(
Identifier.fromNamespaceAndPath(Minions.MOD_ID, "minion"),
MinionPersistentState::new,
MinionPersistentState.CODEC,
null
);
private final Map<UUID, MinionData> minionData = new HashMap<>(); private final Map<UUID, MinionData> minionData = new HashMap<>();
@Override public MinionPersistentState() {
public NbtCompound writeNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLookup) {
NbtList list = new NbtList();
for(MinionData data : minionData.values()) {
list.add(data.writeNbt());
}
nbt.put("minions", list);
return nbt;
} }
public static MinionPersistentState read(NbtCompound compound, RegistryWrapper.WrapperLookup lookup) { public MinionPersistentState(List<MinionData> dataList) {
NbtList list = compound.getList("minions", NbtElement.COMPOUND_TYPE); for (MinionData data : dataList) {
MinionPersistentState instance = new MinionPersistentState(); data.setOnDirty(this::setDirty);
for(NbtElement element : list) { minionData.put(data.getUuid(), data);
if(element instanceof NbtCompound compound1) {
MinionData data = MinionData.readNbt(compound1);
instance.minionData.put(data.uuid(), data);
} }
} }
return instance;
}
public MinionData getMinionData(UUID uuid) { public @Nullable MinionData getMinionData(UUID uuid) {
return minionData.get(uuid); return minionData.get(uuid);
} }
@@ -50,9 +46,14 @@ public class MinionPersistentState extends PersistentState {
return minionData; return minionData;
} }
public List<MinionData> getMinionDataList() {
return List.copyOf(minionData.values());
}
public void updateMinionData(MinionData data) { public void updateMinionData(MinionData data) {
minionData.put(data.uuid(), data); minionData.put(data.getUuid(), data);
markDirty(); data.setOnDirty(this::setDirty);
setDirty();
} }
public boolean isMinion(UUID uuid) { public boolean isMinion(UUID uuid) {
@@ -60,10 +61,16 @@ public class MinionPersistentState extends PersistentState {
} }
public boolean isMinionNameTaken(String name) { public boolean isMinionNameTaken(String name) {
return minionData.values().stream().anyMatch(data -> data.name().equals(name)); return getMinionWithName(name).isPresent();
} }
public static void create(MinecraftServer server) { public Optional<MinionData> getMinionWithName(String name) {
INSTANCE = server.getWorld(World.OVERWORLD).getPersistentStateManager().getOrCreate(TYPE, "minion"); return minionData.values().stream()
.filter(data -> data.getName().equals(name))
.findFirst();
}
public static MinionPersistentState get(MinecraftServer server) {
return server.getLevel(Level.OVERWORLD).getDataStorage().computeIfAbsent(TYPE);
} }
} }
@@ -1,70 +1,65 @@
package io.github.skippyall.minions.minion; package io.github.skippyall.minions.minion;
import com.mojang.authlib.GameProfile; import com.mojang.authlib.GameProfile;
import com.mojang.authlib.ProfileLookupCallback;
import com.mojang.authlib.properties.PropertyMap; import com.mojang.authlib.properties.PropertyMap;
import com.mojang.authlib.yggdrasil.ProfileResult;
import com.mojang.brigadier.StringReader; import com.mojang.brigadier.StringReader;
import io.github.skippyall.minions.input.Result; import io.github.skippyall.minions.MinionsConfig;
import net.minecraft.block.entity.SkullBlockEntity; import io.github.skippyall.minions.gui.input.Result;
import net.minecraft.network.chat.Component;
import net.minecraft.server.MinecraftServer; import net.minecraft.server.MinecraftServer;
import net.minecraft.text.Text; import net.minecraft.util.StringUtil;
import net.minecraft.util.StringHelper; import org.jspecify.annotations.Nullable;
import org.jetbrains.annotations.Nullable;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ForkJoinPool;
import static io.github.skippyall.minions.Minions.LOGGER; import static io.github.skippyall.minions.Minions.LOGGER;
public class MinionProfileUtils { public class MinionProfileUtils {
public static final String PREFIX = "+"; public static String getPrefix() {
return MinionsConfig.get().minion.minionPrefix;
}
public static GameProfile makeNewMinionProfile(UUID uuidMinion, String username, PropertyMap skin) { public static GameProfile makeNewMinionProfile(@Nullable UUID uuidMinion, String username, @Nullable PropertyMap skin) {
if(uuidMinion == null) { if(uuidMinion == null) {
uuidMinion = UUID.randomUUID(); uuidMinion = UUID.randomUUID();
} }
GameProfile newProfile = new GameProfile(uuidMinion, username); GameProfile newProfile = new GameProfile(uuidMinion, username, skin != null ? skin : PropertyMap.EMPTY);
if (skin != null) {
newProfile.getProperties().putAll(skin);
}
LOGGER.info("Minion Profile: {}", newProfile); LOGGER.info("Minion Profile: {}", newProfile);
return newProfile; return newProfile;
} }
public static Result<String, Text> checkMinionNameWithoutPrefix(String name) { public static Result<String, Component> checkMinionNameWithoutPrefix(MinecraftServer server, String name) {
for(char c : name.toCharArray()) { for(char c : name.toCharArray()) {
if(!StringReader.isAllowedInUnquotedString(c)) { if(!StringReader.isAllowedInUnquotedString(c)) {
return new Result.Error<>(Text.translatable("minions.generic.name.invalid_char")); return new Result.Error<>(Component.translatable("minions.generic.name.invalid_char"));
} }
} }
if((PREFIX + name).length() > 16) { if((getPrefix() + name).length() > 16) {
return new Result.Error<>(Text.translatable("minions.generic.name.too_long")); return new Result.Error<>(Component.translatable("minions.generic.name.too_long"));
} }
if(!StringHelper.isValidPlayerName(PREFIX + name)) { if(!StringUtil.isValidPlayerName(getPrefix() + name)) {
return new Result.Error<>(Text.translatable("minions.generic.name.invalid")); return new Result.Error<>(Component.translatable("minions.generic.name.invalid"));
} }
if(MinionPersistentState.INSTANCE.isMinionNameTaken(PREFIX + name)) { if(MinionPersistentState.get(server).isMinionNameTaken(getPrefix() + name)) {
return new Result.Error<>(Text.translatable("minions.generic.name.taken")); return new Result.Error<>(Component.translatable("minions.generic.name.taken"));
} }
return new Result.Success<>(name); return new Result.Success<>(name);
} }
public static String newDefaultMinionName() { public static String newDefaultMinionName(MinecraftServer server) {
int i = 0; int i = 0;
while (MinionPersistentState.INSTANCE.isMinionNameTaken("+Minion" + i)) { while (MinionPersistentState.get(server).isMinionNameTaken("+Minion" + i)) {
i++; i++;
} }
return "+Minion" + i; return "+Minion" + i;
} }
public static boolean isMinion(UUID uuid) { public static boolean isMinion(MinecraftServer server, UUID uuid) {
return MinionPersistentState.INSTANCE.isMinion(uuid); return MinionPersistentState.get(server).isMinion(uuid);
} }
} }
@@ -0,0 +1,146 @@
package io.github.skippyall.minions.minion;
import io.github.skippyall.minions.Minions;
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
import io.github.skippyall.minions.program.InstructionRuntime;
import io.github.skippyall.minions.program.consumer.ValueConsumerType;
import io.github.skippyall.minions.program.instruction.ConfiguredInstruction;
import io.github.skippyall.minions.program.instruction.InstructionType;
import io.github.skippyall.minions.program.supplier.ValueSupplierType;
import io.github.skippyall.minions.registration.MinionRegistries;
import net.minecraft.core.Registry;
import net.minecraft.world.level.storage.ValueInput;
import net.minecraft.world.level.storage.ValueOutput;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
public class MinionRuntime implements InstructionRuntime<MinionRuntime> {
private final MinionFakePlayer minion;
private final Map<String, ConfiguredInstruction<MinionRuntime>> configuredInstructions = new HashMap<>();
public MinionRuntime(MinionFakePlayer minion) {
this.minion = minion;
}
public MinionFakePlayer getMinion() {
return minion;
}
public void tick() {
for (ConfiguredInstruction<MinionRuntime> instruction : configuredInstructions.values()) {
instruction.tick(this);
}
}
public void disableInstructionType(InstructionType<MinionRuntime> instructionType) {
updatePausedStatus(instructionType);
}
public void enableInstructionType(InstructionType<MinionRuntime> instructionType) {
updatePausedStatus(instructionType);
}
public void updatePausedStatus(InstructionType<MinionRuntime> instructionType) {
for(ConfiguredInstruction<MinionRuntime> instruction : configuredInstructions.values()) {
if(instruction.getInstruction() == instructionType) {
instruction.updatePauseStatus(this);
}
}
}
@Override
public boolean isInstructionEnabled(InstructionType<MinionRuntime> type) {
return minion.getModuleInventory().hasInstruction(type);
}
public Set<String> getInstructionNames() {
return configuredInstructions.keySet();
}
public ConfiguredInstruction<MinionRuntime> createInstruction(String name, InstructionType<MinionRuntime> instructionType) {
if(configuredInstructions.containsKey(name)) {
return null;
}
ConfiguredInstruction<MinionRuntime> instruction = new ConfiguredInstruction<>(instructionType);
configuredInstructions.put(name, instruction);
minion.forEachMinionListener(listener -> listener.onInstructionsUpdate(minion));
return instruction;
}
public void removeInstruction(String name) {
ConfiguredInstruction<MinionRuntime> instruction = getInstruction(name);
instruction.stop(this);
configuredInstructions.remove(name);
instruction.onInstructionRemove();
minion.forEachMinionListener(listener -> listener.onInstructionsUpdate(minion));
}
public ConfiguredInstruction<MinionRuntime> getInstruction(String name) {
return configuredInstructions.get(name);
}
public boolean hasInstruction(String name) {
return configuredInstructions.containsKey(name);
}
public void setInstructionName(String oldName, String newName) {
if(!configuredInstructions.containsKey(newName) && configuredInstructions.containsKey(oldName)) {
ConfiguredInstruction<MinionRuntime> instruction = configuredInstructions.get(oldName);
configuredInstructions.remove(oldName);
configuredInstructions.put(newName, instruction);
minion.forEachMinionListener(minionListener -> {
minionListener.onInstructionRename(minion, instruction, oldName, newName);
minionListener.onInstructionsUpdate(minion);
});
}
}
public void save(ValueOutput view) {
ValueOutput.ValueOutputList list = view.childrenList("configuredInstructions");
for (Map.Entry<String, ConfiguredInstruction<MinionRuntime>> instruction : configuredInstructions.entrySet()) {
ValueOutput inner = list.addChild();
inner.putString("name", instruction.getKey());
instruction.getValue().save(inner, this);
}
}
public void load(ValueInput view) {
ValueInput.ValueInputList list = view.childrenListOrEmpty("configuredInstructions");
for (ValueInput inner : list) {
Optional<String> name = inner.getString("name");
if(name.isEmpty()) {
Minions.LOGGER.error("Tried deserializing configured instruction without a name of minion \"{}\":", minion.getGameProfile().name());
continue;
}
try {
ConfiguredInstruction<MinionRuntime> instruction = ConfiguredInstruction.load(inner, this);
configuredInstructions.put(name.get(), instruction);
} catch (Exception e) {
Minions.LOGGER.error("Could not deserialize configured instruction \"{}\" of minion \"{}\":", name.get(), minion.getGameProfile().name(), e);
}
}
}
@Override
public Registry<ValueSupplierType<MinionRuntime>> getArgumentTypeRegistry() {
return MinionRegistries.VALUE_SUPPLIER_TYPES;
}
@Override
public Registry<InstructionType<MinionRuntime>> getInstructionTypeRegistry() {
return MinionRegistries.INSTRUCTION_TYPES;
}
@Override
public Registry<ValueConsumerType<MinionRuntime>> getValueConsumerTypeRegistry() {
return MinionRegistries.VALUE_CONSUMER_TYPES;
}
}
@@ -2,44 +2,44 @@
package io.github.skippyall.minions.minion.fakeplayer; package io.github.skippyall.minions.minion.fakeplayer;
import io.github.skippyall.minions.mixins.EntityAccessor;
import net.minecraft.commands.arguments.EntityAnchorArgument;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.network.protocol.game.ClientboundSetHeldSlotPacket;
import net.minecraft.network.protocol.game.ServerboundPlayerActionPacket;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.Mth;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.animal.equine.AbstractHorse;
import net.minecraft.world.entity.decoration.ItemFrame;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.vehicle.boat.Boat;
import net.minecraft.world.entity.vehicle.minecart.Minecart;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.EntityHitResult;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.Vec2;
import net.minecraft.world.phys.Vec3;
import java.util.EnumMap; import java.util.EnumMap;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import io.github.skippyall.minions.mixins.EntityAccessor;
import net.minecraft.block.BlockState;
import net.minecraft.command.argument.EntityAnchorArgumentType;
import net.minecraft.entity.Entity;
import net.minecraft.entity.decoration.ItemFrameEntity;
import net.minecraft.entity.passive.AbstractHorseEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.entity.vehicle.BoatEntity;
import net.minecraft.entity.vehicle.MinecartEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket;
import net.minecraft.network.packet.s2c.play.UpdateSelectedSlotS2CPacket;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.hit.EntityHitResult;
import net.minecraft.util.hit.HitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec2f;
import net.minecraft.util.math.Vec3d;
public class EntityPlayerActionPack public class EntityPlayerActionPack
{ {
private final ServerPlayerEntity player; private final MinionFakePlayer player;
private final Map<ActionType, Action> actions = new EnumMap<>(ActionType.class); private final Map<ActionType, Action> actions = new EnumMap<>(ActionType.class);
private BlockPos currentBlock; private BlockPos currentBlock;
private int blockHitDelay; public int blockHitDelay;
private boolean isHittingBlock; private boolean isHittingBlock;
private float curBlockDamageMP; private float curBlockDamageMP;
@@ -50,7 +50,7 @@ public class EntityPlayerActionPack
private int itemUseCooldown; private int itemUseCooldown;
public EntityPlayerActionPack(ServerPlayerEntity playerIn) public EntityPlayerActionPack(MinionFakePlayer playerIn)
{ {
player = playerIn; player = playerIn;
stopAll(); stopAll();
@@ -71,6 +71,14 @@ public class EntityPlayerActionPack
itemUseCooldown = other.itemUseCooldown; itemUseCooldown = other.itemUseCooldown;
} }
public Action getAction(ActionType type) {
return actions.get(type);
}
public boolean hasAction(ActionType type) {
return actions.containsKey(type);
}
public EntityPlayerActionPack start(ActionType type, Action action) public EntityPlayerActionPack start(ActionType type, Action action)
{ {
Action previous = actions.remove(type); Action previous = actions.remove(type);
@@ -92,7 +100,7 @@ public class EntityPlayerActionPack
public EntityPlayerActionPack setSneaking(boolean doSneak) public EntityPlayerActionPack setSneaking(boolean doSneak)
{ {
sneaking = doSneak; sneaking = doSneak;
player.setSneaking(doSneak); player.setShiftKeyDown(doSneak);
if (sprinting && sneaking) if (sprinting && sneaking)
setSprinting(false); setSprinting(false);
return this; return this;
@@ -124,35 +132,35 @@ public class EntityPlayerActionPack
case SOUTH -> look(0, 0); case SOUTH -> look(0, 0);
case EAST -> look(-90, 0); case EAST -> look(-90, 0);
case WEST -> look(90, 0); case WEST -> look(90, 0);
case UP -> look(player.getYaw(), -90); case UP -> look(player.getYRot(), -90);
case DOWN -> look(player.getYaw(), 90); case DOWN -> look(player.getYRot(), 90);
}; };
} }
public EntityPlayerActionPack look(Vec2f rotation) public EntityPlayerActionPack look(Vec2 rotation)
{ {
return look(rotation.x, rotation.y); return look(rotation.x, rotation.y);
} }
public EntityPlayerActionPack look(float yaw, float pitch) public EntityPlayerActionPack look(float yaw, float pitch)
{ {
player.setYaw(yaw % 360); //setYaw player.setYRot(yaw % 360); //setYaw
player.setPitch(MathHelper.clamp(pitch, -90, 90)); // setPitch player.setXRot(Mth.clamp(pitch, -90, 90)); // setPitch
// maybe player.moveTo(player.getX(), player.getY(), player.getZ(), yaw, Mth.clamp(pitch,-90.0F, 90.0F)); // maybe player.moveTo(player.getX(), player.getY(), player.getZ(), yaw, Mth.clamp(pitch,-90.0F, 90.0F));
return this; return this;
} }
public EntityPlayerActionPack lookAt(Vec3d position) public EntityPlayerActionPack lookAt(Vec3 position)
{ {
player.lookAt(EntityAnchorArgumentType.EntityAnchor.EYES, position); player.lookAt(EntityAnchorArgument.Anchor.EYES, position);
return this; return this;
} }
public EntityPlayerActionPack turn(float yaw, float pitch) public EntityPlayerActionPack turn(float yaw, float pitch)
{ {
return look(player.getYaw() + yaw, player.getPitch() + pitch); return look(player.getYRot() + yaw, player.getXRot() + pitch);
} }
public EntityPlayerActionPack turn(Vec2f rotation) public EntityPlayerActionPack turn(Vec2 rotation)
{ {
return turn(rotation.x, rotation.y); return turn(rotation.x, rotation.y);
} }
@@ -181,12 +189,12 @@ public class EntityPlayerActionPack
List<Entity> entities; List<Entity> entities;
if (onlyRideables) if (onlyRideables)
{ {
entities = player.getWorld().getOtherEntities(player, player.getBoundingBox().expand(3.0D, 1.0D, 3.0D), entities = player.level().getEntities(player, player.getBoundingBox().inflate(3.0D, 1.0D, 3.0D),
e -> (e instanceof MinecartEntity || e instanceof BoatEntity || e instanceof AbstractHorseEntity) && ((EntityAccessor)e).invokeCanAddPassenger(player)); e -> (e instanceof Minecart || e instanceof Boat || e instanceof AbstractHorse) && ((EntityAccessor)e).minions$canAddPassenger(player));
} }
else else
{ {
entities = player.getWorld().getOtherEntities(player, player.getBoundingBox().expand(3.0D, 1.0D, 3.0D)); entities = player.level().getEntities(player, player.getBoundingBox().inflate(3.0D, 1.0D, 3.0D));
} }
if (entities.size()==0) if (entities.size()==0)
return this; return this;
@@ -197,7 +205,7 @@ public class EntityPlayerActionPack
{ {
if (e == player || (currentVehicle == e)) if (e == player || (currentVehicle == e))
continue; continue;
double dd = player.squaredDistanceTo(e); double dd = player.distanceToSqr(e);
if (dd<distance) if (dd<distance)
{ {
distance = dd; distance = dd;
@@ -205,10 +213,10 @@ public class EntityPlayerActionPack
} }
} }
if (closest == null) return this; if (closest == null) return this;
if (closest instanceof AbstractHorseEntity && onlyRideables) if (closest instanceof AbstractHorse && onlyRideables)
((AbstractHorseEntity) closest).interactMob(player, Hand.MAIN_HAND); ((AbstractHorse) closest).mobInteract(player, InteractionHand.MAIN_HAND);
else else
player.startRiding(closest, !onlyRideables); player.startRiding(closest, !onlyRideables, true);
return this; return this;
} }
public EntityPlayerActionPack dismount() public EntityPlayerActionPack dismount()
@@ -248,48 +256,52 @@ public class EntityPlayerActionPack
float vel = sneaking?0.3F:1.0F; float vel = sneaking?0.3F:1.0F;
// The != 0.0F checks are needed given else real players can't control minecarts, however it works with fakes and else they don't stop immediately // The != 0.0F checks are needed given else real players can't control minecarts, however it works with fakes and else they don't stop immediately
if (forward != 0.0F || player instanceof MinionFakePlayer) { if (forward != 0.0F || player instanceof MinionFakePlayer) {
player.forwardSpeed = forward * vel; player.zza = forward * vel;
} }
if (strafing != 0.0F || player instanceof MinionFakePlayer) { if (strafing != 0.0F || player instanceof MinionFakePlayer) {
player.sidewaysSpeed = strafing * vel; player.xxa = strafing * vel;
}
if(blockHitDelay > 0) {
blockHitDelay--;
} }
} }
static HitResult getTarget(ServerPlayerEntity player) public static HitResult getTarget(ServerPlayer player)
{ {
double reach = player.interactionManager.isCreative() ? 5 : 4.5f; double reach = player.gameMode.isCreative() ? 5 : 4.5f;
return Tracer.rayTrace(player, 1, reach, false); return Tracer.rayTrace(player, 1, reach, false);
} }
private void dropItemFromSlot(int slot, boolean dropAll) private void dropItemFromSlot(int slot, boolean dropAll)
{ {
PlayerInventory inv = player.getInventory(); // getInventory; Inventory inv = player.getInventory(); // getInventory;
if (!inv.getStack(slot).isEmpty()) if (!inv.getItem(slot).isEmpty())
player.dropItem(inv.removeStack(slot, player.drop(inv.removeItem(slot,
dropAll ? inv.getStack(slot).getCount() : 1 dropAll ? inv.getItem(slot).getCount() : 1
), false, true); // scatter, keep owner ), false, true); // scatter, keep owner
} }
public void drop(int selectedSlot, boolean dropAll) public void drop(int selectedSlot, boolean dropAll)
{ {
PlayerInventory inv = player.getInventory(); // getInventory; Inventory inv = player.getInventory(); // getInventory;
if (selectedSlot == -2) // all if (selectedSlot == -2) // all
{ {
for (int i = inv.size(); i >= 0; i--) for (int i = inv.getContainerSize(); i >= 0; i--)
dropItemFromSlot(i, dropAll); dropItemFromSlot(i, dropAll);
} }
else // one slot else // one slot
{ {
if (selectedSlot == -1) if (selectedSlot == -1)
selectedSlot = inv.selectedSlot; selectedSlot = inv.getSelectedSlot();
dropItemFromSlot(selectedSlot, dropAll); dropItemFromSlot(selectedSlot, dropAll);
} }
} }
public void setSlot(int slot) public void setSlot(int slot)
{ {
player.getInventory().selectedSlot = slot-1; player.getInventory().setSelectedSlot(slot-1);
player.networkHandler.sendPacket(new UpdateSelectedSlotS2CPacket(slot-1)); player.connection.send(new ClientboundSetHeldSlotPacket(slot-1));
} }
public enum ActionType public enum ActionType
@@ -297,9 +309,9 @@ public class EntityPlayerActionPack
USE(true) USE(true)
{ {
@Override @Override
boolean execute(ServerPlayerEntity player, Action action) boolean execute(MinionFakePlayer player, Action action)
{ {
EntityPlayerActionPack ap = ((ServerPlayerInterface) player).minions$getActionPack(); EntityPlayerActionPack ap = player.getMinionActionPack();
if (ap.itemUseCooldown > 0) if (ap.itemUseCooldown > 0)
{ {
ap.itemUseCooldown--; ap.itemUseCooldown--;
@@ -310,23 +322,23 @@ public class EntityPlayerActionPack
return true; return true;
} }
HitResult hit = getTarget(player); HitResult hit = getTarget(player);
for (Hand hand : Hand.values()) for (InteractionHand hand : InteractionHand.values())
{ {
switch (hit.getType()) switch (hit.getType())
{ {
case BLOCK: case BLOCK:
{ {
player.updateLastActionTime(); player.resetLastActionTime();
ServerWorld world = player.getServerWorld(); ServerLevel world = player.level();
BlockHitResult blockHit = (BlockHitResult) hit; BlockHitResult blockHit = (BlockHitResult) hit;
BlockPos pos = blockHit.getBlockPos(); BlockPos pos = blockHit.getBlockPos();
Direction side = blockHit.getSide(); Direction side = blockHit.getDirection();
if (pos.getY() < player.getServerWorld().getTopYInclusive() - (side == Direction.UP ? 1 : 0) && world.canPlayerModifyAt(player, pos)) if (pos.getY() < player.level().getMaxY() - (side == Direction.UP ? 1 : 0) && world.mayInteract(player, pos))
{ {
ActionResult result = player.interactionManager.interactBlock(player, world, player.getStackInHand(hand), hand, blockHit); InteractionResult result = player.gameMode.useItemOn(player, world, player.getItemInHand(hand), hand, blockHit);
if (result instanceof ActionResult.Success success) if (result instanceof InteractionResult.Success success)
{ {
if (success.swingSource() == ActionResult.SwingSource.SERVER) player.swingHand(hand); if (success.swingSource() == InteractionResult.SwingSource.SERVER) player.swing(hand);
ap.itemUseCooldown = 3; ap.itemUseCooldown = 3;
return true; return true;
} }
@@ -335,19 +347,19 @@ public class EntityPlayerActionPack
} }
case ENTITY: case ENTITY:
{ {
player.updateLastActionTime(); player.resetLastActionTime();
EntityHitResult entityHit = (EntityHitResult) hit; EntityHitResult entityHit = (EntityHitResult) hit;
Entity entity = entityHit.getEntity(); Entity entity = entityHit.getEntity();
boolean handWasEmpty = player.getStackInHand(hand).isEmpty(); boolean handWasEmpty = player.getItemInHand(hand).isEmpty();
boolean itemFrameEmpty = (entity instanceof ItemFrameEntity) && ((ItemFrameEntity) entity).getHeldItemStack().isEmpty(); boolean itemFrameEmpty = (entity instanceof ItemFrame) && ((ItemFrame) entity).getItem().isEmpty();
Vec3d relativeHitPos = entityHit.getPos().subtract(entity.getX(), entity.getY(), entity.getZ()); Vec3 relativeHitPos = entityHit.getLocation().subtract(entity.getX(), entity.getY(), entity.getZ());
if (entity.interactAt(player, relativeHitPos, hand).isAccepted()) if (entity.interact(player, hand, relativeHitPos).consumesAction())
{ {
ap.itemUseCooldown = 3; ap.itemUseCooldown = 3;
return true; return true;
} }
// fix for SS itemframe always returns CONSUME even if no action is performed // fix for SS itemframe always returns CONSUME even if no action is performed
if (player.interact(entity, hand).isAccepted() && !(handWasEmpty && itemFrameEmpty)) if (player.interactOn(entity, hand, relativeHitPos).consumesAction() && !(handWasEmpty && itemFrameEmpty))
{ {
ap.itemUseCooldown = 3; ap.itemUseCooldown = 3;
return true; return true;
@@ -355,8 +367,8 @@ public class EntityPlayerActionPack
break; break;
} }
} }
ItemStack handItem = player.getStackInHand(hand); ItemStack handItem = player.getItemInHand(hand);
if (player.interactionManager.interactItem(player, player.getWorld(), handItem, hand).isAccepted()) if (player.gameMode.useItem(player, player.level(), handItem, hand).consumesAction())
{ {
ap.itemUseCooldown = 3; ap.itemUseCooldown = 3;
return true; return true;
@@ -366,50 +378,49 @@ public class EntityPlayerActionPack
} }
@Override @Override
void inactiveTick(ServerPlayerEntity player, Action action) void inactiveTick(MinionFakePlayer player, Action action)
{ {
EntityPlayerActionPack ap = ((ServerPlayerInterface) player).minions$getActionPack(); EntityPlayerActionPack ap = player.getMinionActionPack();
ap.itemUseCooldown = 0; ap.itemUseCooldown = 0;
player.stopUsingItem(); player.releaseUsingItem();
} }
}, },
ATTACK(true) { ATTACK(true) {
@Override @Override
boolean execute(ServerPlayerEntity player, Action action) { boolean execute(MinionFakePlayer player, Action action) {
HitResult hit = getTarget(player); HitResult hit = getTarget(player);
switch (hit.getType()) { switch (hit.getType()) {
case ENTITY: { case ENTITY: {
EntityHitResult entityHit = (EntityHitResult) hit; EntityHitResult entityHit = (EntityHitResult) hit;
if (!action.isContinuous) if (!action.isContinuous || action.first)
{ {
player.attack(entityHit.getEntity()); player.attack(entityHit.getEntity());
player.swingHand(Hand.MAIN_HAND); player.swing(InteractionHand.MAIN_HAND);
} }
player.resetLastAttackedTicks(); player.resetAttackStrengthTicker();
player.updateLastActionTime(); player.resetLastActionTime();
return true; return true;
} }
case BLOCK: { case BLOCK: {
EntityPlayerActionPack ap = ((ServerPlayerInterface) player).minions$getActionPack(); EntityPlayerActionPack ap = player.getMinionActionPack();
if (ap.blockHitDelay > 0) if (ap.blockHitDelay > 0)
{ {
ap.blockHitDelay--;
return false; return false;
} }
BlockHitResult blockHit = (BlockHitResult) hit; BlockHitResult blockHit = (BlockHitResult) hit;
BlockPos pos = blockHit.getBlockPos(); BlockPos pos = blockHit.getBlockPos();
Direction side = blockHit.getSide(); Direction side = blockHit.getDirection();
if (player.isBlockBreakingRestricted(player.getWorld(), pos, player.interactionManager.getGameMode())) return false; if (player.blockActionRestricted(player.level(), pos, player.gameMode.getGameModeForPlayer())) return false;
if (ap.currentBlock != null && player.getWorld().getBlockState(ap.currentBlock).isAir()) if (ap.currentBlock != null && player.level().getBlockState(ap.currentBlock).isAir())
{ {
ap.currentBlock = null; ap.currentBlock = null;
return false; return false;
} }
BlockState state = player.getWorld().getBlockState(pos); BlockState state = player.level().getBlockState(pos);
boolean blockBroken = false; boolean blockBroken = false;
if (player.interactionManager.getGameMode().isCreative()) if (player.gameMode.getGameModeForPlayer().isCreative())
{ {
player.interactionManager.processBlockBreakingAction(pos, PlayerActionC2SPacket.Action.START_DESTROY_BLOCK, side, player.getWorld().getTopYInclusive(), -1); player.gameMode.handleBlockBreakAction(pos, ServerboundPlayerActionPacket.Action.START_DESTROY_BLOCK, side, player.level().getMaxY(), -1);
ap.blockHitDelay = 5; ap.blockHitDelay = 5;
blockBroken = true; blockBroken = true;
} }
@@ -417,15 +428,15 @@ public class EntityPlayerActionPack
{ {
if (ap.currentBlock != null) if (ap.currentBlock != null)
{ {
player.interactionManager.processBlockBreakingAction(ap.currentBlock, PlayerActionC2SPacket.Action.ABORT_DESTROY_BLOCK, side, player.getWorld().getTopYInclusive(), -1); player.gameMode.handleBlockBreakAction(ap.currentBlock, ServerboundPlayerActionPacket.Action.ABORT_DESTROY_BLOCK, side, player.level().getMaxY(), -1);
} }
player.interactionManager.processBlockBreakingAction(pos, PlayerActionC2SPacket.Action.START_DESTROY_BLOCK, side, player.getWorld().getTopYInclusive(), -1); player.gameMode.handleBlockBreakAction(pos, ServerboundPlayerActionPacket.Action.START_DESTROY_BLOCK, side, player.level().getMaxY(), -1);
boolean notAir = !state.isAir(); boolean notAir = !state.isAir();
if (notAir && ap.curBlockDamageMP == 0) if (notAir && ap.curBlockDamageMP == 0)
{ {
state.onBlockBreakStart(player.getWorld(), pos, player); state.attack(player.level(), pos, player);
} }
if (notAir && state.calcBlockBreakingDelta(player, player.getWorld(), pos) >= 1) if (notAir && state.getDestroyProgress(player, player.level(), pos) >= 1)
{ {
ap.currentBlock = null; ap.currentBlock = null;
//instamine?? //instamine??
@@ -439,19 +450,19 @@ public class EntityPlayerActionPack
} }
else else
{ {
ap.curBlockDamageMP += state.calcBlockBreakingDelta(player, player.getWorld(), pos); ap.curBlockDamageMP += state.getDestroyProgress(player, player.level(), pos);
if (ap.curBlockDamageMP >= 1) if (ap.curBlockDamageMP >= 1)
{ {
player.interactionManager.processBlockBreakingAction(pos, PlayerActionC2SPacket.Action.STOP_DESTROY_BLOCK, side, player.getWorld().getTopYInclusive(), -1); player.gameMode.handleBlockBreakAction(pos, ServerboundPlayerActionPacket.Action.STOP_DESTROY_BLOCK, side, player.level().getMaxY(), -1);
ap.currentBlock = null; ap.currentBlock = null;
ap.blockHitDelay = 5; ap.blockHitDelay = 5;
blockBroken = true; blockBroken = true;
} }
player.getWorld().setBlockBreakingInfo(-1, pos, (int) (ap.curBlockDamageMP * 10)); player.level().destroyBlockProgress(-1, pos, (int) (ap.curBlockDamageMP * 10));
} }
player.updateLastActionTime(); player.resetLastActionTime();
player.swingHand(Hand.MAIN_HAND); player.swing(InteractionHand.MAIN_HAND);
return blockBroken; return blockBroken;
} }
} }
@@ -459,23 +470,24 @@ public class EntityPlayerActionPack
} }
@Override @Override
void inactiveTick(ServerPlayerEntity player, Action action) void inactiveTick(MinionFakePlayer player, Action action)
{ {
EntityPlayerActionPack ap = ((ServerPlayerInterface) player).minions$getActionPack(); EntityPlayerActionPack ap = player.getMinionActionPack();
if (ap.currentBlock == null) return; if (ap.currentBlock == null) return;
player.getWorld().setBlockBreakingInfo(-1, ap.currentBlock, -1); player.level().destroyBlockProgress(-1, ap.currentBlock, -1);
player.interactionManager.processBlockBreakingAction(ap.currentBlock, PlayerActionC2SPacket.Action.ABORT_DESTROY_BLOCK, Direction.DOWN, player.getWorld().getTopYInclusive(), -1); player.gameMode.handleBlockBreakAction(ap.currentBlock, ServerboundPlayerActionPacket.Action.ABORT_DESTROY_BLOCK, Direction.DOWN, player.level().getMaxY(), -1);
ap.currentBlock = null; ap.currentBlock = null;
} }
}, },
JUMP(true) JUMP(true)
{ {
@Override @Override
boolean execute(ServerPlayerEntity player, Action action) boolean execute(MinionFakePlayer player, Action action)
{ {
if (action.limit == 1) if (action.limit == 1)
{ {
if (player.isOnGround()) player.jump(); // onGround if (player.onGround()) player.jumpFromGround();
else if (!player.onClimbable()) player.tryToStartFallFlying();
} }
else else
{ {
@@ -485,7 +497,7 @@ public class EntityPlayerActionPack
} }
@Override @Override
void inactiveTick(ServerPlayerEntity player, Action action) void inactiveTick(MinionFakePlayer player, Action action)
{ {
player.setJumping(false); player.setJumping(false);
} }
@@ -493,32 +505,32 @@ public class EntityPlayerActionPack
DROP_ITEM(true) DROP_ITEM(true)
{ {
@Override @Override
boolean execute(ServerPlayerEntity player, Action action) boolean execute(MinionFakePlayer player, Action action)
{ {
player.updateLastActionTime(); player.resetLastActionTime();
player.dropSelectedItem(false); // dropSelectedItem player.drop(false); // dropSelectedItem
return false; return false;
} }
}, },
DROP_STACK(true) DROP_STACK(true)
{ {
@Override @Override
boolean execute(ServerPlayerEntity player, Action action) boolean execute(MinionFakePlayer player, Action action)
{ {
player.updateLastActionTime(); player.resetLastActionTime();
player.dropSelectedItem(true); // dropSelectedItem player.drop(true); // dropSelectedItem
return false; return false;
} }
}, },
SWAP_HANDS(true) SWAP_HANDS(true)
{ {
@Override @Override
boolean execute(ServerPlayerEntity player, Action action) boolean execute(MinionFakePlayer player, Action action)
{ {
player.updateLastActionTime(); player.resetLastActionTime();
ItemStack itemStack_1 = player.getStackInHand(Hand.OFF_HAND); ItemStack itemStack_1 = player.getItemInHand(InteractionHand.OFF_HAND);
player.setStackInHand(Hand.OFF_HAND, player.getStackInHand(Hand.MAIN_HAND)); player.setItemInHand(InteractionHand.OFF_HAND, player.getItemInHand(InteractionHand.MAIN_HAND));
player.setStackInHand(Hand.MAIN_HAND, itemStack_1); player.setItemInHand(InteractionHand.MAIN_HAND, itemStack_1);
return false; return false;
} }
}; };
@@ -530,10 +542,10 @@ public class EntityPlayerActionPack
this.preventSpectator = preventSpectator; this.preventSpectator = preventSpectator;
} }
void start(ServerPlayerEntity player, Action action) {} void start(MinionFakePlayer player, Action action) {}
abstract boolean execute(ServerPlayerEntity player, Action action); abstract boolean execute(MinionFakePlayer player, Action action);
void inactiveTick(ServerPlayerEntity player, Action action) {} void inactiveTick(MinionFakePlayer player, Action action) {}
void stop(ServerPlayerEntity player, Action action) void stop(MinionFakePlayer player, Action action)
{ {
inactiveTick(player, action); inactiveTick(player, action);
} }
@@ -548,8 +560,9 @@ public class EntityPlayerActionPack
private int count; private int count;
private int next; private int next;
private final boolean isContinuous; private final boolean isContinuous;
boolean first = true;
private Action(int limit, int interval, int offset, boolean continuous) private Action(int limit, int interval, int offset, boolean continuous, boolean first)
{ {
this.limit = limit; this.limit = limit;
this.interval = interval; this.interval = interval;
@@ -560,22 +573,27 @@ public class EntityPlayerActionPack
public static Action once() public static Action once()
{ {
return new Action(1, 1, 0, false); return new Action(1, 1, 0, false, false);
}
public static Action startContinuous()
{
return new Action(-1, 1, 0, true, true);
} }
public static Action continuous() public static Action continuous()
{ {
return new Action(-1, 1, 0, true); return new Action(-1, 1, 0, true, false);
} }
public static Action interval(int interval) public static Action interval(int interval)
{ {
return new Action(-1, interval, 0, false); return new Action(-1, interval, 0, false, false);
} }
public static Action interval(int interval, int offset) public static Action interval(int interval, int offset)
{ {
return new Action(-1, interval, offset, false); return new Action(-1, interval, offset, false, false);
} }
Boolean tick(EntityPlayerActionPack actionPack, ActionType type) Boolean tick(EntityPlayerActionPack actionPack, ActionType type)
@@ -606,6 +624,7 @@ public class EntityPlayerActionPack
return cancel; return cancel;
} }
next = interval; next = interval;
first = false;
} }
else else
{ {
@@ -1,14 +1,17 @@
//code from https://github.com/gnembon/fabric-carpet //code from https://github.com/gnembon/fabric-carpet
package io.github.skippyall.minions.minion.fakeplayer; package io.github.skippyall.minions.minion.fakeplayer;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import net.minecraft.network.ClientConnection; import net.minecraft.network.Connection;
import net.minecraft.network.NetworkSide; import net.minecraft.network.PacketListener;
import net.minecraft.network.NetworkState; import net.minecraft.network.ProtocolInfo;
import net.minecraft.network.listener.PacketListener; import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.PacketFlow;
import org.jspecify.annotations.Nullable;
public class FakeClientConnection extends ClientConnection { public class FakeClientConnection extends Connection {
public FakeClientConnection(NetworkSide p) public FakeClientConnection(PacketFlow p)
{ {
super(p); super(p);
// compat with adventure-platform-fabric. This does NOT trigger other vanilla handlers for establishing a channel // compat with adventure-platform-fabric. This does NOT trigger other vanilla handlers for establishing a channel
@@ -17,7 +20,12 @@ public class FakeClientConnection extends ClientConnection {
} }
@Override @Override
public void tryDisableAutoRead() public void send(Packet<?> packet, @Nullable ChannelFutureListener listener, boolean flush) {
}
@Override
public void setReadOnly()
{ {
} }
@@ -26,11 +34,11 @@ public class FakeClientConnection extends ClientConnection {
} }
@Override @Override
public void setInitialPacketListener(PacketListener packetListener) public void setListenerForServerboundHandshake(PacketListener packetListener)
{ {
} }
@Override @Override
public <T extends PacketListener> void transitionInbound(NetworkState<T> state, T packetListener) { public <T extends PacketListener> void setupInboundProtocol(ProtocolInfo<T> state, T packetListener) {
} }
} }
@@ -4,307 +4,337 @@ package io.github.skippyall.minions.minion.fakeplayer;
import com.mojang.authlib.GameProfile; import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.PropertyMap; import com.mojang.authlib.properties.PropertyMap;
import io.github.skippyall.minions.Minions; import io.github.skippyall.minions.Minions;
import io.github.skippyall.minions.MinionsTickExecutor; import io.github.skippyall.minions.gui.minion.MinionGui;
import io.github.skippyall.minions.listener.SerializableListenerManager;
import io.github.skippyall.minions.minion.MinionData; import io.github.skippyall.minions.minion.MinionData;
import io.github.skippyall.minions.gui.MinionGui;
import io.github.skippyall.minions.minion.MinionItem; import io.github.skippyall.minions.minion.MinionItem;
import io.github.skippyall.minions.minion.MinionListener;
import io.github.skippyall.minions.minion.MinionPersistentState; import io.github.skippyall.minions.minion.MinionPersistentState;
import io.github.skippyall.minions.minion.MinionProfileUtils; import io.github.skippyall.minions.minion.MinionProfileUtils;
import io.github.skippyall.minions.gui.ModuleInventory; import io.github.skippyall.minions.minion.MinionRuntime;
import io.github.skippyall.minions.program.runtime.MinionRuntime; import io.github.skippyall.minions.module.ModuleInventory;
import net.minecraft.block.BlockState; import io.github.skippyall.minions.registration.MinionConfigOptions;
import net.minecraft.block.EndPortalBlock; import io.github.skippyall.minions.registration.MinionItems;
import net.minecraft.entity.Entity; import io.github.skippyall.minions.registration.SpecialAbilities;
import net.minecraft.entity.EquipmentSlot; import net.fabricmc.fabric.impl.networking.context.PacketContextImpl;
import net.minecraft.entity.MovementType; import net.minecraft.core.BlockPos;
import net.minecraft.entity.attribute.EntityAttributes; import net.minecraft.network.DisconnectionDetails;
import net.minecraft.entity.damage.DamageSource; import net.minecraft.network.chat.Component;
import net.minecraft.entity.player.HungerManager; import net.minecraft.network.chat.contents.TranslatableContents;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.network.protocol.PacketFlow;
import net.minecraft.item.ItemStack; import net.minecraft.network.protocol.game.ClientboundEntityPositionSyncPacket;
import net.minecraft.nbt.NbtCompound; import net.minecraft.network.protocol.game.ClientboundRotateHeadPacket;
import net.minecraft.network.DisconnectionInfo; import net.minecraft.network.protocol.game.ServerboundClientCommandPacket;
import net.minecraft.network.NetworkSide;
import net.minecraft.network.packet.c2s.common.SyncedClientOptions;
import net.minecraft.network.packet.c2s.play.ClientStatusC2SPacket;
import net.minecraft.network.packet.s2c.play.EntityPositionSyncS2CPacket;
import net.minecraft.network.packet.s2c.play.EntitySetHeadYawS2CPacket;
import net.minecraft.server.MinecraftServer; import net.minecraft.server.MinecraftServer;
import net.minecraft.server.ServerTask; import net.minecraft.server.TickTask;
import net.minecraft.server.network.ConnectedClientData; import net.minecraft.server.level.ClientInformation;
import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.world.ServerWorld; import net.minecraft.server.level.ServerPlayer;
import net.minecraft.text.Text; import net.minecraft.server.network.CommonListenerCookie;
import net.minecraft.text.TranslatableTextContent; import net.minecraft.util.ProblemReporter;
import net.minecraft.util.ActionResult; import net.minecraft.world.InteractionHand;
import net.minecraft.util.Hand; import net.minecraft.world.InteractionResult;
import net.minecraft.util.math.BlockPos; import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.util.math.Vec2f; import net.minecraft.world.entity.Entity;
import net.minecraft.util.math.Vec3d; import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.GameMode; import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.TeleportTarget; import net.minecraft.world.entity.item.ItemEntity;
import org.jetbrains.annotations.Nullable; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.vehicle.boat.AbstractBoat;
import net.minecraft.world.food.FoodData;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.GameType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.portal.TeleportTransition;
import net.minecraft.world.level.storage.TagValueInput;
import net.minecraft.world.level.storage.ValueInput;
import net.minecraft.world.level.storage.ValueOutput;
import net.minecraft.world.phys.Vec2;
import net.minecraft.world.phys.Vec3;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.concurrent.CompletableFuture; import java.util.function.Consumer;
public class MinionFakePlayer extends ServerPlayerEntity { @NullMarked
public class MinionFakePlayer extends ServerPlayer {
public Runnable fixStartingPosition = () -> {}; public Runnable fixStartingPosition = () -> {};
private float moveForward; private EntityPlayerActionPack actionPack;
private float moveSideways;
private boolean programmable; private final ModuleInventory moduleInventory = new ModuleInventory(this);
private final ModuleInventory moduleInventory = new ModuleInventory(); private final MinionRuntime instructionManager = new MinionRuntime(this);
private final MinionRuntime runtime = new MinionRuntime(this);
private final MinionData data; public static void spawnMinion(MinionData data, ServerLevel level, @Nullable Vec3 pos, @Nullable Vec2 rot) {
spawnMinion(data, level, pos, rot, false);
}
public static void spawnMinion(MinionData data, ServerWorld level, @Nullable Vec3d pos, @Nullable Vec2f rot) { public static void spawnMinion(MinionData data, ServerLevel level, @Nullable Vec3 pos, @Nullable Vec2 rot, boolean force) {
if(!data.isSpawned() || force) {
MinecraftServer server = level.getServer(); MinecraftServer server = level.getServer();
PropertyMap skin = data.skin().orElse(null); PropertyMap skin = data.getSkin().orElse(null);
GameProfile profile = MinionProfileUtils.makeNewMinionProfile(data.uuid(), data.name(), skin);
doSpawn(data, profile, server, level, pos, rot);
GameProfile profile = MinionProfileUtils.makeNewMinionProfile(data.getUuid(), data.getName(), skin);
server.schedule(server.wrapRunnable(() -> doSpawn(data, profile, server, level, pos, rot)));
}
} }
private static void doSpawn(MinionData data, GameProfile profile, MinecraftServer server, ServerWorld level, @Nullable Vec3d pos, @Nullable Vec2f rot) { private static void doSpawn(MinionData data, GameProfile profile, MinecraftServer server, ServerLevel level, @Nullable Vec3 pos, @Nullable Vec2 rot) {
MinionFakePlayer instance = new MinionFakePlayer(server, level, profile, ClientInformation.createDefault());
MinionFakePlayer instance = new MinionFakePlayer(server, level, profile, SyncedClientOptions.createDefault(), data); data.setSpawned(true);
MinionPersistentState.INSTANCE.updateMinionData(data.withSpawned(true));
if(pos != null && rot != null) { if(pos != null && rot != null) {
instance.fixStartingPosition = () -> instance.refreshPositionAndAngles(pos.x, pos.y, pos.z, rot.x, rot.y); instance.fixStartingPosition = () -> instance.snapTo(pos.x, pos.y, pos.z, rot.x, rot.y);
} }
server.getPlayerManager().onPlayerConnect(new FakeClientConnection(NetworkSide.SERVERBOUND), instance, new ConnectedClientData(profile, 0, instance.getClientOptions(), false)); FakeClientConnection connection = new FakeClientConnection(PacketFlow.SERVERBOUND);
System.out.println(instance.getPos()); //noinspection UnstableApiUsage
connection.getPacketContext().set(PacketContextImpl.REGISTRY_ACCESS, server.registryAccess());
//noinspection UnstableApiUsage
connection.getPacketContext().set(PacketContextImpl.SERVER_INSTANCE, server);
//noinspection UnstableApiUsage
connection.getPacketContext().set(PacketContextImpl.GAME_PROFILE, profile);
server.getPlayerList().placeNewPlayer(connection, instance, new CommonListenerCookie(profile, 0, instance.clientInformation(), false));
loadPlayerData(instance);
instance.stopRiding(); // otherwise the created fake player will be on the vehicle
System.out.println(instance.position());
if(pos != null && rot != null) { if(pos != null && rot != null) {
instance.teleport(level, pos.x, pos.y, pos.z, Set.of(), rot.x, rot.y, true); instance.teleportTo(level, pos.x, pos.y, pos.z, Set.of(), rot.x, rot.y, true);
} }
instance.setVelocity(0,0,0); instance.setDeltaMovement(0,0,0);
instance.setHealth(20.0F); instance.setHealth(20.0F);
instance.unsetRemoved(); instance.unsetRemoved();
instance.getAttributeInstance(EntityAttributes.STEP_HEIGHT).setBaseValue(0.6F); instance.getAttribute(Attributes.STEP_HEIGHT).setBaseValue(0.6F);
instance.interactionManager.changeGameMode(GameMode.SURVIVAL); instance.getAttribute(Attributes.WAYPOINT_TRANSMIT_RANGE).setBaseValue(0);
server.getPlayerManager().sendToDimension(new EntitySetHeadYawS2CPacket(instance, (byte) (instance.headYaw * 256 / 360)), level.getRegistryKey());//instance.dimension); instance.gameMode.changeGameModeForPlayer(GameType.SURVIVAL);
server.getPlayerManager().sendToDimension(EntityPositionSyncS2CPacket.create(instance), level.getRegistryKey());//instance.dimension); server.getPlayerList().broadcastAll(new ClientboundRotateHeadPacket(instance, (byte) (instance.yHeadRot * 256 / 360)), level.dimension());
//instance.world.getChunkManager(). updatePosition(instance); server.getPlayerList().broadcastAll(ClientboundEntityPositionSyncPacket.of(instance), level.dimension());
instance.dataTracker.set(PLAYER_MODEL_PARTS, (byte) 0x7f); // show all model layers (incl. capes) instance.level().getChunkSource().move(instance);
instance.entityData.set(DATA_PLAYER_MODE_CUSTOMISATION, (byte) 0x7f); // show all model layers (incl. capes)
instance.getAbilities().flying = false; instance.getAbilities().flying = false;
instance.listeners().forEach(listener -> listener.onMinionSpawn(instance));
} }
public static MinionFakePlayer respawnFake(MinecraftServer server, ServerWorld level, GameProfile profile, SyncedClientOptions cli, MinionData data) private static void loadPlayerData(MinionFakePlayer player)
{ {
return new MinionFakePlayer(server, level, profile, cli, data); try (ProblemReporter.ScopedCollector scopedCollector = new ProblemReporter.ScopedCollector(player.problemPath(), Minions.LOGGER))
{
Optional<ValueInput> optional = player.level().getServer().getPlayerList().loadPlayerData(player.nameAndId()).map((compoundTag) -> TagValueInput.create(scopedCollector, player.registryAccess(), compoundTag));
optional.ifPresent( valueInput -> {
player.load(valueInput);
player.loadAndSpawnEnderPearls(valueInput);
player.loadAndSpawnParentVehicle(valueInput);
});
}
} }
private MinionFakePlayer(MinecraftServer server, ServerWorld worldIn, GameProfile profile, SyncedClientOptions cli, MinionData data) public static MinionFakePlayer respawnFake(MinecraftServer server, ServerLevel level, GameProfile profile, ClientInformation cli)
{
return new MinionFakePlayer(server, level, profile, cli);
}
private MinionFakePlayer(MinecraftServer server, ServerLevel worldIn, GameProfile profile, ClientInformation cli)
{ {
super(server, worldIn, profile, cli); super(server, worldIn, profile, cli);
this.data = data; actionPack = new EntityPlayerActionPack(this);
}
public boolean isProgrammable() {
return programmable;
}
public void setProgrammable(boolean programmable) {
this.programmable = programmable;
} }
public ModuleInventory getModuleInventory() { public ModuleInventory getModuleInventory() {
return moduleInventory; return moduleInventory;
} }
public MinionRuntime getRuntime() {
return runtime;
}
public EntityPlayerActionPack getMinionActionPack() { public EntityPlayerActionPack getMinionActionPack() {
return ((ServerPlayerInterface)this).minions$getActionPack(); return actionPack;
}
public MinionRuntime getInstructionManager() {
return instructionManager;
}
public MinionData getData() {
return MinionPersistentState.get(getServer()).getMinionData(getUUID());
}
public SerializableListenerManager<MinionListener> listeners() {
return getData().getListeners();
}
public void addMinionListener(MinionListener listener) {
listeners().addListener(listener);
}
public void removeMinionListener(MinionListener listener) {
listeners().removeListener(listener);
}
public void forEachMinionListener(Consumer<MinionListener> listenerConsumer) {
listeners().forEach(listenerConsumer);
}
public boolean canSpawnMobs() {
return moduleInventory.hasAbility(SpecialAbilities.MOB_SPAWNING) || getData().getConfig().getOption(MinionConfigOptions.spawnAndDespawnMobs);
}
public boolean canDespawnMobs() {
return canSpawnMobs();
}
public MinecraftServer getServer() {
return level().getServer();
} }
@Override @Override
public ActionResult interact(PlayerEntity player, Hand hand) { public InteractionResult interact(Player player, InteractionHand hand, Vec3 location) {
if(player instanceof ServerPlayerEntity spe) { if(player instanceof ServerPlayer spe) {
MinionGui.openInventory(spe, this); new MinionGui(spe, this);
} }
return ActionResult.CONSUME; return InteractionResult.CONSUME;
} }
@Override @Override
public ActionResult interactAt(PlayerEntity player, Vec3d hitPos, Hand hand) { public void onEquipItem(final EquipmentSlot slot, final ItemStack previous, final ItemStack stack)
return interact(player, hand); {
if (!isUsingItem()) super.onEquipItem(slot, previous, stack);
} }
@Override public void kill(Component reason)
public void onEquipStack(final EquipmentSlot slot, final ItemStack previous, final ItemStack stack)
{ {
if (!isUsingItem()) super.onEquipStack(slot, previous, stack); listeners().forEach(listener -> listener.onMinionRemove(this));
}
/*@Override
public void kill()
{
kill(Text.literal("Killed"));
}*/
public void kill(Text reason)
{
shakeOff(); shakeOff();
if (reason.getContent() instanceof TranslatableTextContent text && text.getKey().equals("multiplayer.disconnect.duplicate_login")) { if (reason.getContents() instanceof TranslatableContents text && text.getKey().equals("multiplayer.disconnect.duplicate_login")) {
this.networkHandler.onDisconnected(new DisconnectionInfo(reason)); this.connection.onDisconnect(new DisconnectionDetails(reason));
} else { } else {
this.server.send(new ServerTask(this.server.getTicks(), () -> { this.getServer().schedule(new TickTask(this.getServer().getTickCount(), () -> {
this.networkHandler.onDisconnected(new DisconnectionInfo(reason)); this.connection.onDisconnect(new DisconnectionDetails(reason));
})); }));
} }
MinionPersistentState.INSTANCE.updateMinionData(data.withSpawned(false)); getData().setSpawned(false);
} }
@Override @Override
public void tick() public void tick()
{ {
if (this.getServer().getTicks() % 10 == 0) actionPack.onUpdate();
if (this.getServer().getTickCount() % 10 == 0)
{ {
this.networkHandler.syncWithPlayerPosition(); this.connection.resetPosition();
this.getServerWorld().getChunkManager().updatePosition(this); this.level().getChunkSource().move(this);
} }
try try
{ {
super.tick(); super.tick();
this.playerTick(); this.doTick();
instructionManager.tick();
} }
catch (NullPointerException ignored) catch (NullPointerException ignored)
{ {
// happens with that paper port thingy - not sure what that would fix, but hey // happens with that paper port thingy - not sure what that would fix, but hey
// the game not gonna crash violently. // the game not gonna crash violently.
} }
runtime.tick();
} }
@Override
public boolean startRiding(Entity entityToRide, boolean force, boolean sendEventAndTriggers) {
if (super.startRiding(entityToRide, force, sendEventAndTriggers)) {
// from ClientPacketListener.handleSetEntityPassengersPacket
if (entityToRide instanceof AbstractBoat) {
this.yRotO = entityToRide.getYRot();
this.setYRot(entityToRide.getYRot());
this.setYHeadRot(entityToRide.getYHeadRot());
}
return true;
} else {
return false;
}
}
private void shakeOff() private void shakeOff()
{ {
if (getVehicle() instanceof PlayerEntity) stopRiding(); if (getVehicle() instanceof Player) stopRiding();
for (Entity passenger : getPassengersDeep()) for (Entity passenger : getIndirectPassengers())
{ {
if (passenger instanceof PlayerEntity) passenger.stopRiding(); if (passenger instanceof Player) passenger.stopRiding();
} }
} }
@Override @Override
public void onDeath(DamageSource cause) public void die(DamageSource cause)
{ {
shakeOff(); shakeOff();
super.onDeath(cause); super.die(cause);
setHealth(20); setHealth(20);
this.hungerManager = new HungerManager(); this.foodData = new FoodData();
kill(this.getDamageTracker().getDeathMessage()); kill(this.getCombatTracker().getDeathMessage());
} }
@Override @Override
public String getIp() public String getIpAddress()
{ {
return "127.0.0.1"; return "127.0.0.1";
} }
@Override @Override
public boolean allowsServerListing() { public boolean allowsListing() {
return false; return false;
} }
@Override @Override
protected void fall(double y, boolean onGround, BlockState state, BlockPos pos) { protected void checkFallDamage(double y, boolean onGround, BlockState state, BlockPos pos) {
handleFall(0.0, y, 0.0, onGround); doCheckFallDamage(0.0, y, 0.0, onGround);
} }
@Override @Override
public ServerPlayerEntity teleportTo(TeleportTarget target) public ServerPlayer teleport(TeleportTransition target)
{ {
super.teleportTo(target); super.teleport(target);
if (notInAnyWorld) { if (wonGame) {
ClientStatusC2SPacket p = new ClientStatusC2SPacket(ClientStatusC2SPacket.Mode.PERFORM_RESPAWN); ServerboundClientCommandPacket p = new ServerboundClientCommandPacket(ServerboundClientCommandPacket.Action.PERFORM_RESPAWN);
networkHandler.onClientStatus(p); connection.handleClientCommand(p);
} }
// If above branch was taken, *this* has been removed and replaced, the new instance has been set // If above branch was taken, *this* has been removed and replaced, the new instance has been set
// on 'our' connection (which is now theirs, but we still have a ref). // on 'our' connection (which is now theirs, but we still have a ref).
if (networkHandler.player.isInTeleportationState()) { if (connection.player.isChangingDimension()) {
networkHandler.player.onTeleportationDone(); connection.player.hasChangedDimension();
}
return networkHandler.player;
}
public void moveForward(float forward) {
this.moveForward += forward;
EntityPlayerActionPack actionPack = getMinionActionPack();
if (moveForward != 0) {
actionPack.setForward(moveForward > 0 ? 1 : -1);
}
}
public void moveSideways(float sideways) {
this.moveSideways += sideways;
EntityPlayerActionPack actionPack = getMinionActionPack();
if (moveSideways != 0) {
actionPack.setStrafing(moveSideways > 0 ? 1 : -1);
} }
return connection.player;
} }
@Override @Override
public void move(MovementType movementType, Vec3d movement) { public void dropAllDeathLoot(ServerLevel world, DamageSource damageSource) {
float newForward = (float) (moveForward - movement.z); super.dropAllDeathLoot(world, damageSource);
float newSideways = (float) (moveSideways - movement.x); ItemEntity entity = drop(toItemStack(world.getServer()), true, false);
Vec3d newMovement = movement; //noinspection ConstantValue (Wrong nullability of drop)
if ((newForward < 0 && moveForward > 0) || (newForward > 0 && moveForward < 0)) { if (entity != null) {
newMovement = new Vec3d(newMovement.x, newMovement.y, moveForward); entity.setUnlimitedLifetime();
moveForward = 0;
getMinionActionPack().setForward(0);
}else {
moveForward = newForward;
} }
if ((newSideways < 0 && moveSideways > 0) || (newSideways > 0 && moveSideways < 0)) {
newMovement = new Vec3d(newMovement.x, newMovement.y, moveSideways);
moveSideways = 0;
getMinionActionPack().setStrafing(0);
}else {
moveSideways = newSideways;
}
super.move(movementType, newMovement);
} }
@Override private ItemStack toItemStack(MinecraftServer server) {
public void drop(ServerWorld world, DamageSource damageSource) { ItemStack stack = new ItemStack(MinionItems.MINION_ITEM);
super.drop(world, damageSource); MinionItem.setData(server, getData(), stack);
dropStack(world, toItemStack()).setNeverDespawn();
}
private ItemStack toItemStack() {
ItemStack stack = new ItemStack(Minions.MINION_ITEM);
MinionItem.setData(data, stack);
return stack; return stack;
} }
public MinionData getData() { @Override
return data; public void addAdditionalSaveData(ValueOutput view) {
super.addAdditionalSaveData(view);
moduleInventory.writeData(view.child("modules"));
instructionManager.save(view.child("instructionManager"));
} }
@Override @Override
public void writeCustomDataToNbt(NbtCompound nbt) { public void readAdditionalSaveData(ValueInput view) {
super.writeCustomDataToNbt(nbt); super.readAdditionalSaveData(view);
nbt.put("modules", moduleInventory.writeNbt(new NbtCompound(), getRegistryManager())); moduleInventory.readData(view.childOrEmpty("modules"));
nbt.putBoolean("programmable", programmable); instructionManager.load(view.childOrEmpty("instructionManager"));
}
@Override
public void readCustomDataFromNbt(NbtCompound nbt) {
super.readCustomDataFromNbt(nbt);
moduleInventory.readNbt(nbt.getCompound("modules"), getRegistryManager());
programmable = nbt.getBoolean("programmable");
} }
} }
@@ -1,46 +1,41 @@
//code from https://github.com/gnembon/fabric-carpet //code from https://github.com/gnembon/fabric-carpet
package io.github.skippyall.minions.minion.fakeplayer; package io.github.skippyall.minions.minion.fakeplayer;
import net.minecraft.entity.player.PlayerPosition; import net.minecraft.network.Connection;
import net.minecraft.network.ClientConnection; import net.minecraft.network.chat.Component;
import net.minecraft.network.packet.Packet; import net.minecraft.network.chat.contents.TranslatableContents;
import net.minecraft.network.packet.s2c.play.PositionFlag;
import net.minecraft.server.MinecraftServer; import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ConnectedClientData; import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.network.ServerPlayNetworkHandler; import net.minecraft.server.network.CommonListenerCookie;
import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.network.ServerGamePacketListenerImpl;
import net.minecraft.text.Text; import net.minecraft.world.entity.PositionMoveRotation;
import net.minecraft.text.TranslatableTextContent; import net.minecraft.world.entity.Relative;
import java.util.Set; import java.util.Set;
public class NetHandlerPlayServerFake extends ServerPlayNetworkHandler public class NetHandlerPlayServerFake extends ServerGamePacketListenerImpl
{ {
public NetHandlerPlayServerFake(final MinecraftServer minecraftServer, final ClientConnection connection, final ServerPlayerEntity serverPlayer, final ConnectedClientData i) public NetHandlerPlayServerFake(final MinecraftServer minecraftServer, final Connection connection, final ServerPlayer serverPlayer, final CommonListenerCookie i)
{ {
super(minecraftServer, connection, serverPlayer, i); super(minecraftServer, connection, serverPlayer, i);
} }
@Override @Override
public void sendPacket(final Packet<?> packetIn) public void disconnect(Component message)
{ {
} if (message.getContents() instanceof TranslatableContents text && (text.getKey().equals("multiplayer.disconnect.idling") || text.getKey().equals("multiplayer.disconnect.duplicate_login")))
@Override
public void disconnect(Text message)
{
if (message.getContent() instanceof TranslatableTextContent text && (text.getKey().equals("multiplayer.disconnect.idling") || text.getKey().equals("multiplayer.disconnect.duplicate_login")))
{ {
((MinionFakePlayer) player).kill(message); ((MinionFakePlayer) player).kill(message);
} }
} }
@Override @Override
public void requestTeleport(PlayerPosition pos, Set<PositionFlag> set) public void teleport(PositionMoveRotation pos, Set<Relative> set)
{ {
super.requestTeleport(pos, set); super.teleport(pos, set);
if (player.getServerWorld().getPlayerByUuid(player.getUuid()) != null) { if (player.level().getPlayerByUUID(player.getUUID()) != null) {
syncWithPlayerPosition(); resetPosition();
player.getServerWorld().getChunkManager().updatePosition(player); player.level().getChunkSource().move(player);
} }
} }
@@ -1,7 +0,0 @@
//code from https://github.com/gnembon/fabric-carpet
package io.github.skippyall.minions.minion.fakeplayer;
public interface ServerPlayerInterface
{
EntityPlayerActionPack minions$getActionPack();
}
@@ -1,16 +1,17 @@
//code from https://github.com/gnembon/fabric-carpet //code from https://github.com/gnembon/fabric-carpet
package io.github.skippyall.minions.minion.fakeplayer; package io.github.skippyall.minions.minion.fakeplayer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.ClipContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.EntityHitResult;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.Vec3;
import java.util.Optional; import java.util.Optional;
import java.util.function.Predicate; import java.util.function.Predicate;
import net.minecraft.entity.Entity;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.hit.EntityHitResult;
import net.minecraft.util.hit.HitResult;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.RaycastContext;
import net.minecraft.world.World;
public class Tracer public class Tracer
{ {
@@ -20,7 +21,7 @@ public class Tracer
double maxSqDist = reach * reach; double maxSqDist = reach * reach;
if (blockHit != null) if (blockHit != null)
{ {
maxSqDist = blockHit.getPos().squaredDistanceTo(source.getCameraPosVec(partialTicks)); maxSqDist = blockHit.getLocation().distanceToSqr(source.getEyePosition(partialTicks));
} }
EntityHitResult entityHit = rayTraceEntities(source, partialTicks, reach, maxSqDist); EntityHitResult entityHit = rayTraceEntities(source, partialTicks, reach, maxSqDist);
return entityHit == null ? blockHit : entityHit; return entityHit == null ? blockHit : entityHit;
@@ -28,31 +29,31 @@ public class Tracer
public static BlockHitResult rayTraceBlocks(Entity source, float partialTicks, double reach, boolean fluids) public static BlockHitResult rayTraceBlocks(Entity source, float partialTicks, double reach, boolean fluids)
{ {
Vec3d pos = source.getCameraPosVec(partialTicks); Vec3 pos = source.getEyePosition(partialTicks);
Vec3d rotation = source.getRotationVec(partialTicks); Vec3 rotation = source.getViewVector(partialTicks);
Vec3d reachEnd = pos.add(rotation.x * reach, rotation.y * reach, rotation.z * reach); Vec3 reachEnd = pos.add(rotation.x * reach, rotation.y * reach, rotation.z * reach);
return source.getWorld().raycast(new RaycastContext(pos, reachEnd, RaycastContext.ShapeType.OUTLINE, fluids ? return source.level().clip(new ClipContext(pos, reachEnd, ClipContext.Block.OUTLINE, fluids ?
RaycastContext.FluidHandling.ANY : RaycastContext.FluidHandling.NONE, source)); ClipContext.Fluid.ANY : ClipContext.Fluid.NONE, source));
} }
public static EntityHitResult rayTraceEntities(Entity source, float partialTicks, double reach, double maxSqDist) public static EntityHitResult rayTraceEntities(Entity source, float partialTicks, double reach, double maxSqDist)
{ {
Vec3d pos = source.getCameraPosVec(partialTicks); Vec3 pos = source.getEyePosition(partialTicks);
Vec3d reachVec = source.getRotationVec(partialTicks).multiply(reach); Vec3 reachVec = source.getViewVector(partialTicks).scale(reach);
Box box = source.getBoundingBox().stretch(reachVec).expand(1); AABB box = source.getBoundingBox().expandTowards(reachVec).inflate(1);
return rayTraceEntities(source, pos, pos.add(reachVec), box, e -> !e.isSpectator() && e.canHit(), maxSqDist); return rayTraceEntities(source, pos, pos.add(reachVec), box, e -> !e.isSpectator() && e.isPickable(), maxSqDist);
} }
public static EntityHitResult rayTraceEntities(Entity source, Vec3d start, Vec3d end, Box box, Predicate<Entity> predicate, double maxSqDistance) public static EntityHitResult rayTraceEntities(Entity source, Vec3 start, Vec3 end, AABB box, Predicate<Entity> predicate, double maxSqDistance)
{ {
World world = source.getWorld(); Level world = source.level();
double targetDistance = maxSqDistance; double targetDistance = maxSqDistance;
Entity target = null; Entity target = null;
Vec3d targetHitPos = null; Vec3 targetHitPos = null;
for (Entity current : world.getOtherEntities(source, box, predicate)) for (Entity current : world.getEntities(source, box, predicate))
{ {
Box currentBox = current.getBoundingBox().expand(current.getTargetingMargin()); AABB currentBox = current.getBoundingBox().inflate(current.getPickRadius());
Optional<Vec3d> currentHit = currentBox.raycast(start, end); Optional<Vec3> currentHit = currentBox.clip(start, end);
if (currentBox.contains(start)) if (currentBox.contains(start))
{ {
if (targetDistance >= 0) if (targetDistance >= 0)
@@ -64,8 +65,8 @@ public class Tracer
} }
else if (currentHit.isPresent()) else if (currentHit.isPresent())
{ {
Vec3d currentHitPos = currentHit.get(); Vec3 currentHitPos = currentHit.get();
double currentDistance = start.squaredDistanceTo(currentHitPos); double currentDistance = start.distanceToSqr(currentHitPos);
if (currentDistance < targetDistance || targetDistance == 0) if (currentDistance < targetDistance || targetDistance == 0)
{ {
if (current.getRootVehicle() == source.getRootVehicle()) if (current.getRootVehicle() == source.getRootVehicle())
@@ -0,0 +1,4 @@
@NullMarked
package io.github.skippyall.minions.minion;
import org.jspecify.annotations.NullMarked;
@@ -0,0 +1,51 @@
package io.github.skippyall.minions.minion.program.instruction;
import io.github.skippyall.minions.minion.MinionRuntime;
import io.github.skippyall.minions.minion.fakeplayer.EntityPlayerActionPack;
import io.github.skippyall.minions.program.consumer.ValueConsumerList;
import io.github.skippyall.minions.program.instruction.execution.ContinuousInstructionExecution;
import io.github.skippyall.minions.program.supplier.ParameterValueList;
import net.minecraft.world.level.storage.ValueInput;
import net.minecraft.world.level.storage.ValueOutput;
public class ActionExecution implements ContinuousInstructionExecution<MinionRuntime> {
private final EntityPlayerActionPack.ActionType action;
public ActionExecution(EntityPlayerActionPack.ActionType action) {
this.action = action;
}
@Override
public void start(MinionRuntime minion) {
EntityPlayerActionPack ap = minion.getMinion().getMinionActionPack();
if(!ap.hasAction(action)) {
minion.getMinion().getMinionActionPack().start(action, EntityPlayerActionPack.Action.startContinuous());
}
}
@Override
public void stop(MinionRuntime minion, ValueConsumerList<MinionRuntime> valueConsumers) {
minion.getMinion().getMinionActionPack().stop(action);
}
@Override
public void pause(MinionRuntime runtime) {
runtime.getMinion().getMinionActionPack().stop(action);
}
@Override
public void resume(MinionRuntime runtime) {
runtime.getMinion().getMinionActionPack().start(action, EntityPlayerActionPack.Action.continuous());
}
@Override
public void readArguments(ParameterValueList parameters, MinionRuntime minion) {}
@Override
public void save(ValueOutput view, MinionRuntime minion) {}
@Override
public void load(ValueInput view, MinionRuntime runtime) {
runtime.getMinion().getMinionActionPack().start(action, EntityPlayerActionPack.Action.continuous());
}
}
@@ -0,0 +1,142 @@
//partially code from https://github.com/gnembon/fabric-carpet (EntityPlayerActionPack)
package io.github.skippyall.minions.minion.program.instruction;
import io.github.skippyall.minions.minion.MinionRuntime;
import io.github.skippyall.minions.minion.fakeplayer.EntityPlayerActionPack;
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
import io.github.skippyall.minions.program.consumer.ValueConsumerList;
import io.github.skippyall.minions.program.instruction.InstructionExecution;
import io.github.skippyall.minions.program.supplier.ParameterValueList;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.network.protocol.game.ServerboundPlayerActionPacket;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.storage.ValueInput;
import net.minecraft.world.level.storage.ValueOutput;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
import org.jspecify.annotations.Nullable;
public class MineBlockExecution implements InstructionExecution<MinionRuntime> {
private @Nullable BlockPos currentBlock;
private float currentBlockDamage = 0;
private boolean first = true;
private boolean done = false;
private boolean success = false;
@Override
public void start(MinionRuntime runtime) {
MinionFakePlayer player = runtime.getMinion();
if(EntityPlayerActionPack.getTarget(player) instanceof BlockHitResult hit) {
this.currentBlock = hit.getBlockPos();
EntityPlayerActionPack ap = player.getMinionActionPack();
if (ap.blockHitDelay > 0) {
ap.blockHitDelay--;
done = true;
return;
}
if (player.blockActionRestricted(player.level(), hit.getBlockPos(), player.gameMode.getGameModeForPlayer())) {
done = true;
}
} else {
done = true;
}
}
@Override
public void tick(MinionRuntime runtime) {
if(done) {
return;
}
MinionFakePlayer player = runtime.getMinion();
EntityPlayerActionPack ap = player.getMinionActionPack();
HitResult newHit = EntityPlayerActionPack.getTarget(player);
if(!(newHit instanceof BlockHitResult newBlockHit)) {
done = true;
return;
}
BlockPos newPos = newBlockHit.getBlockPos();
if(!newPos.equals(currentBlock)) {
done = true;
return;
}
if (player.level().getBlockState(currentBlock).isAir()) {
done = true;
return;
}
BlockState state = player.level().getBlockState(currentBlock);
boolean blockBroken = false;
if (first) {
first = false;
player.gameMode.handleBlockBreakAction(currentBlock, ServerboundPlayerActionPacket.Action.START_DESTROY_BLOCK, newBlockHit.getDirection(), player.level().getMaxY(), -1);
boolean notAir = !state.isAir();
if (notAir)
{
state.attack(player.level(), currentBlock, player);
}
if (notAir && state.getDestroyProgress(player, player.level(), currentBlock) >= 1)
{
//instamine??
blockBroken = true;
}
} else {
currentBlockDamage += state.getDestroyProgress(player, player.level(), currentBlock);
if (currentBlockDamage >= 1) {
player.gameMode.handleBlockBreakAction(currentBlock, ServerboundPlayerActionPacket.Action.STOP_DESTROY_BLOCK, newBlockHit.getDirection(), player.level().getMaxY(), -1);
ap.blockHitDelay = 5;
blockBroken = true;
}
player.level().destroyBlockProgress(-1, currentBlock, (int) (currentBlockDamage * 10));
}
player.resetLastActionTime();
player.swing(InteractionHand.MAIN_HAND);
if(blockBroken) {
done = true;
success = true;
}
}
@Override
public boolean isDone(MinionRuntime runtime) {
return done;
}
@Override
public void stop(MinionRuntime runtime, ValueConsumerList<MinionRuntime> valueConsumers) {
MinionFakePlayer player = runtime.getMinion();
EntityPlayerActionPack ap = player.getMinionActionPack();
if(currentBlock != null) {
player.level().destroyBlockProgress(-1, currentBlock, -1);
player.gameMode.handleBlockBreakAction(currentBlock, ServerboundPlayerActionPacket.Action.ABORT_DESTROY_BLOCK, Direction.DOWN, player.level().getMaxY(), -1);
}
}
@Override
public void readArguments(ParameterValueList arguments, MinionRuntime runtime) {
}
@Override
public void save(ValueOutput view, MinionRuntime runtime) {
view.store("currentBlock", BlockPos.CODEC, currentBlock);
view.putFloat("currentBlockDamage", currentBlockDamage);
}
@Override
public void load(ValueInput view, MinionRuntime runtime) {
currentBlock = view.read("currentBlock", BlockPos.CODEC).orElse(null);
currentBlockDamage = view.getFloatOr("currentBlockDamage", 0);
if(currentBlock == null) {
done = true;
}
}
}

Some files were not shown because too many files have changed in this diff Show More