53 Commits

Author SHA1 Message Date
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
skippyall cdd0a594f3 Merge branch '1.21.3' into 1.21.4
# Conflicts:
#	gradle.properties
2025-04-24 22:13:32 +02:00
skippyall a9d0b7fd94 Merge branch 'feature/skin_improvements' into 1.21.3
# Conflicts:
#	src/main/java/io/github/skippyall/minions/module/MoveModule.java
#	src/main/resources/minions.mixins.json
2025-04-24 22:11:34 +02:00
skippyall 1931848068 Skin Improvements 2025-04-24 22:09:38 +02:00
skippyall a2a189a0c1 Merge pull request #2 from Mineattack-Foxgalaxy/feature/mob-cap-fix
Bugfixes (Minion Mobcap)
2025-04-16 20:05:02 +02:00
Erfinderlabyrinth d2d96b3f67 Bugfixes (Minion Mobcap) 2025-04-16 15:29:16 +02:00
skippyall 08e7952e34 Mob Cap fix 2025-04-14 21:45:04 +02:00
skippyall cc69b22aba Turn Up/Down & Grave Compat 2025-04-12 19:08:25 +02:00
skippyall c7ff6de42b Skin Improvements 2025-04-07 18:39:05 +02:00
skippyall d3b25b595a Too simple 1.21.4 Port 2025-01-30 21:23:34 +01:00
skippyall 8007778c39 Interact and Attack 2025-01-21 19:21:18 +01:00
skippyall fc70b2fe5a I don't know... 2024-11-15 20:07:03 +01:00
skippyall 2f35493580 1.21.3 + Actions! 2024-10-31 14:03:43 +01:00
skippyall 6526f92a67 Actually start actions 2024-09-18 21:21:18 +02:00
skippyall d6a324a93f e 2024-09-17 10:46:43 +02:00
skippyall 1dd6914454 e 2024-09-17 09:55:43 +02:00
skippyall c497884884 e 2024-09-13 19:37:38 +02:00
skippyall a236abdd5c Recipe 2024-09-01 19:52:35 +02:00
skippyall b4d8df9e8e Merge spawnMinion and spawnMinionAt 2024-09-01 18:49:33 +02:00
skippyall 2ee3d946ec Merge pull request #1 from Mineattack-Foxgalaxy/master
ErfinderLabyrinth's changes
2024-09-01 17:35:12 +02:00
Erfinderlabyrinth ea5411450c Forgot to commit too 2024-09-01 16:34:47 +02:00
Erfinderlabyrinth f533ae8515 Minion Item(drop) contains Minion Data
Movement fixes
2024-09-01 16:34:15 +02:00
Erfinderlabyrinth d84190ee3c Fix PersistentState
Different Minion UUID
Add CustomThing
Bugfixes
2024-09-01 15:37:01 +02:00
skippyall ac4f58b9a2 1.21 and other things 2024-08-31 17:00:27 +02:00
265 changed files with 9974 additions and 2671 deletions
+1
View File
@@ -1,5 +1,6 @@
# User-specific stuff # User-specific stuff
.idea/ .idea/
urlaub/
*.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)
-91
View File
@@ -1,91 +0,0 @@
plugins {
id 'fabric-loom' version '1.5-SNAPSHOT'
id 'maven-publish'
}
version = project.mod_version
group = project.maven_group
base {
archivesName = project.archives_base_name
}
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' }
}
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 include("eu.pb4:polymer-core:${project.polymer_version}")
modImplementation include("eu.pb4:sgui:${project.sgui_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 = 17
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 {
mavenJava(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.
}
}
+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)
}
}
}
+13 -7
View File
@@ -3,18 +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.20.4 loom_version=1.16-SNAPSHOT
loader_version=0.15.6 minecraft_version=26.1.2
yarn_mappings=1.20.4+build.3 loader_version=0.19.2
# Mod Properties # Mod Properties
mod_version = 0.0.1 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.95.4+1.20.4 fabric_api_version=0.147.0+26.1.2
fabric_kotlin_version=1.13.11+kotlin.2.3.21
polymer_version=0.7.5+1.20.4 polymer_version=0.16.3+26.1.2
sgui_version=1.4.1+1.20.4 sgui_version=2.0.0+26.1
server_translations_version=3.0.3+26.1
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.5-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
@@ -1,46 +0,0 @@
package io.github.skippyall.minions;
import eu.pb4.sgui.api.ClickType;
import eu.pb4.sgui.api.elements.GuiElement;
import eu.pb4.sgui.api.elements.GuiElementBuilder;
import eu.pb4.sgui.api.elements.GuiElementInterface;
import eu.pb4.sgui.api.gui.SimpleGui;
import eu.pb4.sgui.api.gui.SlotGuiInterface;
import io.github.skippyall.minions.fakeplayer.MinionFakePlayer;
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;
public class MinionInventory {
public static void openInventory(ServerPlayer player, MinionFakePlayer minion) {
}
public static void openServerSideInventory(ServerPlayer player, MinionFakePlayer minion) {
SimpleGui gui = new SimpleGui(MenuType.GENERIC_3x3, player, false);
gui.setTitle(Component.literal("Minion"));
gui.setSlot(4, new GuiElementBuilder()
.setItem(Items.REDSTONE)
.setName(Component.literal("Programming"))
.setCallback((i, clickType, clickType1) -> {
openProgrammingInventory(player, minion);
})
);
gui.setSlot(3, new GuiElementBuilder()
.setItem(Items.NETHERITE_UPGRADE_SMITHING_TEMPLATE)
.setName(Component.literal("Modules and Detectors"))
.setCallback((i, clickType, clickType1) -> {
})
);
gui.open();
}
public static void openProgrammingInventory(ServerPlayer player, MinionFakePlayer minion) {
}
public static void open
}
@@ -1,56 +0,0 @@
package io.github.skippyall.minions;
import eu.pb4.polymer.core.api.item.PolymerItem;
import eu.pb4.polymer.core.api.item.PolymerItemUtils;
import io.github.skippyall.minions.fakeplayer.MinionFakePlayer;
import net.fabricmc.fabric.api.item.v1.FabricItemSettings;
import net.minecraft.network.chat.ComponentContents;
import net.minecraft.network.chat.contents.PlainTextContents;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.context.UseOnContext;
import org.jetbrains.annotations.Nullable;
public class MinionItem extends Item implements PolymerItem {
public MinionItem() {
super(new FabricItemSettings());
}
@Override
public Item getPolymerItem(ItemStack itemStack, @Nullable ServerPlayer player) {
return Items.PLAYER_HEAD;
}
@Override
public ItemStack getPolymerItemStack(ItemStack stack, TooltipFlag flag, ServerPlayer player) {
ItemStack out = PolymerItemUtils.createItemStack(stack, flag, player);
//CompoundTag tag = out.getOrCreateTag();
//PlayerHeadItem.TAG_SKULL_OWNER;
return out;
}
/*@Override
public int getPolymerCustomModelData(ItemStack itemStack, @Nullable ServerPlayer player) {
return 10;
}*/
public InteractionResult useOn(UseOnContext context) {
System.out.println("Minion spawned "+ context.getItemInHand().getDisplayName());
ComponentContents contents = context.getItemInHand().getHoverName().getContents();
String name;
if(contents instanceof PlainTextContents plainContents) {
name = plainContents.text();
} else {
name = "Minion";
}
if(!context.getLevel().isClientSide) {
MinionFakePlayer.createMinion(name, (ServerLevel) context.getLevel(), (ServerPlayer) context.getPlayer(), context.getClickedPos().getCenter().add(0,0.5,0), 0, 0);
}
return InteractionResult.SUCCESS;
}
}
@@ -1,27 +0,0 @@
package io.github.skippyall.minions;
import eu.pb4.polymer.core.api.entity.PolymerEntityUtils;
import io.github.skippyall.minions.networking.ClientToServerNetworking;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.networking.v1.ServerConfigurationNetworking;
import net.fabricmc.fabric.api.networking.v1.ServerLoginConnectionEvents;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerLoginPacketListenerImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Minions implements ModInitializer {
public static final String MOD_ID = "minions";
public static final MinionItem MINION_ITEM = Registry.register(BuiltInRegistries.ITEM, new ResourceLocation(MOD_ID, "minion"), new MinionItem());
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
@Override
public void onInitialize() {
PolymerEntityUtils.registerType();
}
}
@@ -1,16 +0,0 @@
package io.github.skippyall.minions.client;
import io.github.skippyall.minions.networking.ClientToServerNetworking;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationConnectionEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientLoginConnectionEvents;
public class MinionsClient implements ClientModInitializer {
/**
* Runs the mod initializer on the client environment.
*/
@Override
public void onInitializeClient() {
ClientConfigurationConnectionEvents.INIT.register(ClientToServerNetworking::onConfigurationInit);
}
}
@@ -1,201 +0,0 @@
package io.github.skippyall.minions.fakeplayer;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.yggdrasil.ProfileResult;
import net.minecraft.core.BlockPos;
import net.minecraft.core.UUIDUtil;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.contents.TranslatableContents;
import net.minecraft.network.protocol.PacketFlow;
import net.minecraft.network.protocol.game.ClientboundPlayerInfoUpdatePacket;
import net.minecraft.network.protocol.game.ClientboundRotateHeadPacket;
import net.minecraft.network.protocol.game.ClientboundTeleportEntityPacket;
import net.minecraft.network.protocol.game.ServerboundClientCommandPacket;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.TickTask;
import net.minecraft.server.level.ClientInformation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.network.CommonListenerCookie;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.player.Player;
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.phys.Vec3;
import java.util.UUID;
public class MinionFakePlayer extends ServerPlayer {
public Runnable fixStartingPosition = () -> {};
public boolean isAShadow;
public static void createMinion(String username, ServerLevel level, ServerPlayer owner, Vec3 pos, double yaw, double pitch) {
MinecraftServer server = level.getServer();
server.getProfileCache().getAsync(username).thenAcceptAsync((optional) -> {
GameProfile profile = null;
if(optional.isPresent()){
UUID uuid = optional.get().getId();
ProfileResult result = server.getSessionService().fetchProfile(uuid, true);
if(result != null) {
profile = result.profile();
}
}
if(profile == null) {
profile = new GameProfile(UUIDUtil.createOfflinePlayerUUID(username), username);
}
MinionFakePlayer instance = new MinionFakePlayer(server, level, profile, ClientInformation.createDefault(), false);
instance.fixStartingPosition = () -> instance.moveTo(pos.x, pos.y, pos.z, (float) yaw, (float) pitch);
server.getPlayerList().placeNewPlayer(new FakeClientConnection(PacketFlow.SERVERBOUND), instance, new CommonListenerCookie(profile, 0, instance.clientInformation()));
instance.teleportTo(level, pos.x, pos.y, pos.z, (float) yaw, (float) pitch);
instance.setHealth(20.0F);
instance.unsetRemoved();
instance.gameMode.changeGameModeForPlayer(GameType.SURVIVAL);
server.getPlayerList().broadcastAll(new ClientboundRotateHeadPacket(instance, (byte) (instance.yHeadRot * 256 / 360)), level.dimension());//instance.dimension);
server.getPlayerList().broadcastAll(new ClientboundTeleportEntityPacket(instance), level.dimension());//instance.dimension);
//instance.world.getChunkManager(). updatePosition(instance);
instance.entityData.set(DATA_PLAYER_MODE_CUSTOMISATION, (byte) 0x7f); // show all model layers (incl. capes)
instance.getAbilities().flying = false;
instance.setMaxUpStep(0.6F);
});
}
@SuppressWarnings("unused") //Don't know if I'll need this
public static MinionFakePlayer createShadow(MinecraftServer server, ServerPlayer player)
{
player.getServer().getPlayerList().remove(player);
player.connection.disconnect(Component.translatable("multiplayer.disconnect.duplicate_login"));
ServerLevel worldIn = player.serverLevel();//.getWorld(player.dimension);
GameProfile gameprofile = player.getGameProfile();
MinionFakePlayer playerShadow = new MinionFakePlayer(server, worldIn, gameprofile, player.clientInformation(), true);
playerShadow.setChatSession(player.getChatSession());
server.getPlayerList().placeNewPlayer(new FakeClientConnection(PacketFlow.SERVERBOUND), playerShadow, new CommonListenerCookie(gameprofile, 0, player.clientInformation()));
playerShadow.setHealth(player.getHealth());
playerShadow.connection.teleport(player.getX(), player.getY(), player.getZ(), player.getYRot(), player.getXRot());
playerShadow.gameMode.changeGameModeForPlayer(player.gameMode.getGameModeForPlayer());
((ServerPlayerInterface) playerShadow).getActionPack().copyFrom(((ServerPlayerInterface) player).getActionPack());
playerShadow.entityData.set(DATA_PLAYER_MODE_CUSTOMISATION, player.getEntityData().get(DATA_PLAYER_MODE_CUSTOMISATION));
server.getPlayerList().broadcastAll(new ClientboundRotateHeadPacket(playerShadow, (byte) (player.yHeadRot * 256 / 360)), playerShadow.level().dimension());
server.getPlayerList().broadcastAll(new ClientboundPlayerInfoUpdatePacket(ClientboundPlayerInfoUpdatePacket.Action.ADD_PLAYER, playerShadow));
//player.world.getChunkManager().updatePosition(playerShadow);
playerShadow.getAbilities().flying = player.getAbilities().flying;
return playerShadow;
}
public static MinionFakePlayer respawnFake(MinecraftServer server, ServerLevel level, GameProfile profile, ClientInformation cli)
{
return new MinionFakePlayer(server, level, profile, cli, false);
}
private MinionFakePlayer(MinecraftServer server, ServerLevel worldIn, GameProfile profile, ClientInformation cli, boolean shadow)
{
super(server, worldIn, profile, cli);
isAShadow = shadow;
}
@Override
public void onEquipItem(final EquipmentSlot slot, final ItemStack previous, final ItemStack stack)
{
if (!isUsingItem()) super.onEquipItem(slot, previous, stack);
}
@Override
public void kill()
{
kill(Component.literal("Killed"));
}
public void kill(Component reason)
{
shakeOff();
if (reason.getContents() instanceof TranslatableContents text && text.getKey().equals("multiplayer.disconnect.duplicate_login")) {
this.connection.onDisconnect(reason);
} else {
this.server.tell(new TickTask(this.server.getTickCount(), () -> {
this.connection.onDisconnect(reason);
}));
}
}
@Override
public void tick()
{
if (this.getServer().getTickCount() % 10 == 0)
{
this.connection.resetPosition();
this.serverLevel().getChunkSource().move(this);
}
try
{
super.tick();
this.doTick();
}
catch (NullPointerException ignored)
{
// happens with that paper port thingy - not sure what that would fix, but hey
// the game not gonna crash violently.
}
}
private void shakeOff()
{
if (getVehicle() instanceof Player) stopRiding();
for (Entity passenger : getIndirectPassengers())
{
if (passenger instanceof Player) passenger.stopRiding();
}
}
@Override
public void die(DamageSource cause)
{
shakeOff();
super.die(cause);
setHealth(20);
this.foodData = new FoodData();
kill(this.getCombatTracker().getDeathMessage());
}
@Override
public String getIpAddress()
{
return "127.0.0.1";
}
@Override
public boolean allowsListing() {
return false;
}
@Override
protected void checkFallDamage(double y, boolean onGround, BlockState state, BlockPos pos) {
doCheckFallDamage(0.0, y, 0.0, onGround);
}
@Override
public Entity changeDimension(ServerLevel serverLevel)
{
super.changeDimension(serverLevel);
if (wonGame) {
ServerboundClientCommandPacket p = new ServerboundClientCommandPacket(ServerboundClientCommandPacket.Action.PERFORM_RESPAWN);
connection.handleClientCommand(p);
}
// 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).
if (connection.player.isChangingDimension()) {
connection.player.hasChangedDimension();
}
return connection.player;
}
}
@@ -1,8 +0,0 @@
package io.github.skippyall.minions.fakeplayer;
import io.github.skippyall.minions.fakeplayer.EntityPlayerActionPack;
public interface ServerPlayerInterface
{
EntityPlayerActionPack getActionPack();
}
@@ -1,14 +0,0 @@
package io.github.skippyall.minions.mixins;
import io.github.skippyall.minions.fakeplayer.ClientConnectionInterface;
import io.netty.channel.Channel;
import net.minecraft.network.Connection;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
@Mixin(Connection.class)
public abstract class ConnectionMixin implements ClientConnectionInterface {
@Override
@Accessor //Compat with adventure-platform-fabric
public abstract void setChannel(Channel channel);
}
@@ -1,56 +0,0 @@
package io.github.skippyall.minions.mixins;
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.mojang.authlib.GameProfile;
import io.github.skippyall.minions.fakeplayer.MinionFakePlayer;
import io.github.skippyall.minions.fakeplayer.NetHandlerPlayServerFake;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.Connection;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ClientInformation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.network.CommonListenerCookie;
import net.minecraft.server.network.ServerGamePacketListenerImpl;
import net.minecraft.server.players.PlayerList;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
@Mixin(PlayerList.class)
public class PlayerListMixin {
@Shadow
@Final
private MinecraftServer server;
@Inject(method = "load", at = @At(value = "RETURN", shift = At.Shift.BEFORE))
private void fixStartingPos(ServerPlayer serverPlayerEntity_1, CallbackInfoReturnable<CompoundTag> cir)
{
if (serverPlayerEntity_1 instanceof MinionFakePlayer)
{
((MinionFakePlayer) serverPlayerEntity_1).fixStartingPosition.run();
}
}
@WrapOperation(method = "placeNewPlayer", at = @At(value = "NEW", target = "(Lnet/minecraft/server/MinecraftServer;Lnet/minecraft/network/Connection;Lnet/minecraft/server/level/ServerPlayer;Lnet/minecraft/server/network/CommonListenerCookie;)Lnet/minecraft/server/network/ServerGamePacketListenerImpl;"))
private ServerGamePacketListenerImpl replaceNetworkHandler(MinecraftServer minecraftServer, Connection connection, ServerPlayer serverPlayer, CommonListenerCookie commonListenerCookie, Operation<ServerGamePacketListenerImpl> original)
{
if (serverPlayer instanceof MinionFakePlayer fake) {
return new NetHandlerPlayServerFake(this.server, connection, fake, commonListenerCookie);
} else {
return original.call(minecraftServer, connection, serverPlayer, commonListenerCookie);
}
}
@WrapOperation(method = "respawn", at = @At(value = "NEW", target = "(Lnet/minecraft/server/MinecraftServer;Lnet/minecraft/server/level/ServerLevel;Lcom/mojang/authlib/GameProfile;Lnet/minecraft/server/level/ClientInformation;)Lnet/minecraft/server/level/ServerPlayer;"))
public ServerPlayer makePlayerForRespawn(MinecraftServer minecraftServer, ServerLevel serverLevel, GameProfile gameProfile, ClientInformation clientInformation, Operation<ServerPlayer> original, ServerPlayer serverPlayer, boolean bl) {
if (serverPlayer instanceof MinionFakePlayer) {
return MinionFakePlayer.respawnFake(minecraftServer, serverLevel, gameProfile, clientInformation);
}
return original.call(minecraftServer, serverLevel, gameProfile, clientInformation);
}
}
@@ -1,37 +0,0 @@
package io.github.skippyall.minions.mixins;
import com.mojang.authlib.GameProfile;
import io.github.skippyall.minions.fakeplayer.EntityPlayerActionPack;
import io.github.skippyall.minions.fakeplayer.ServerPlayerInterface;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ClientInformation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(ServerPlayer.class)
public abstract class ServerPlayerMixin implements ServerPlayerInterface {
@Unique
public EntityPlayerActionPack actionPack;
@Override
public EntityPlayerActionPack getActionPack()
{
return actionPack;
}
@Inject(method = "<init>", at = @At(value = "RETURN"))
private void onServerPlayerEntityContructor(MinecraftServer minecraftServer, ServerLevel serverLevel, GameProfile gameProfile, ClientInformation clientInformation, CallbackInfo ci)
{
this.actionPack = new EntityPlayerActionPack((ServerPlayer) (Object) this);
}
@Inject(method = "tick", at = @At(value = "HEAD"))
private void onTick(CallbackInfo ci)
{
actionPack.onUpdate();
}
}
@@ -1,41 +0,0 @@
package io.github.skippyall.minions.networking;
import io.github.skippyall.minions.Minions;
import io.netty.buffer.Unpooled;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationNetworking;
import net.fabricmc.fabric.api.networking.v1.PacketSender;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientConfigurationPacketListenerImpl;
import net.minecraft.client.multiplayer.ClientHandshakePacketListenerImpl;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerConfigurationPacketListenerImpl;
import net.minecraft.world.entity.player.Player;
public class ClientToServerNetworking {
public static final ResourceLocation RL = new ResourceLocation(Minions.MOD_ID, "network");
@Environment(EnvType.CLIENT)
public static void sendJoinPacket(Player player) {
FriendlyByteBuf pbf = new FriendlyByteBuf(Unpooled.buffer());
pbf.writeUtf("BN|Init|V0.1");
ClientConfigurationNetworking.send(RL, pbf);
}
@Environment(EnvType.CLIENT)
public static void onConfigurationInit(ClientConfigurationPacketListenerImpl handler, Minecraft client) {
sendJoinPacket(client.player);
}
@Environment(EnvType.SERVER)
public static void receive(MinecraftServer server, ServerConfigurationPacketListenerImpl handler, FriendlyByteBuf buf, PacketSender responseSender) {
String message = buf.readUtf();
if (!message.startsWith("BN|")) {
Minions.LOGGER.debug("Message with wrong format: " + message);
}
String[] parts = message.split("|");
}
}
@@ -1,22 +0,0 @@
package io.github.skippyall.minions.networking;
import net.minecraft.world.entity.player.Player;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class VersionChecker {
protected static List<UUID> hasSupportedMod = new ArrayList<>();
public static boolean supportVersion(String version) {
return version.equals("v0.1");
}
public static boolean useSupportedMod(Player p) {
return hasSupportedMod.contains(p.getUUID());
}
public static void resetPlayer(UUID uuid) {
hasSupportedMod.remove(uuid);
}
}
@@ -1,12 +0,0 @@
package io.github.skippyall.minions.program.block;
import io.github.skippyall.minions.program.variables.Type;
import java.util.List;
public abstract class CodeBlock {
public CodeBlock(String name, List<Type> arguments) {
}
public abstract Object execute(Object... args);
}
@@ -1,5 +0,0 @@
package io.github.skippyall.minions.program.block;
public class CodeBlocks {
public static final ForwardBlock FORWARD = new ForwardBlock();
}
@@ -1,19 +0,0 @@
package io.github.skippyall.minions.program.block;
import io.github.skippyall.minions.program.variables.IntegerType;
import io.github.skippyall.minions.program.variables.Type;
import io.github.skippyall.minions.program.variables.Types;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ForwardBlock extends CodeBlock{
public ForwardBlock() {
super("forward", Arrays.asList(Types.INTEGER));
}
public Object execute(Object... args) {
return null;
}
}
@@ -1,15 +0,0 @@
package io.github.skippyall.minions.program.module;
import eu.pb4.polymer.core.api.item.PolymerItem;
import io.github.skippyall.minions.program.block.CodeBlock;
import java.util.List;
public abstract class Modul implements PolymerItem {
private final String name;
Modul(String name) {
this.name = name;
}
public abstract List<CodeBlock> getCodeBlocks();
}
@@ -1,5 +0,0 @@
package io.github.skippyall.minions.program.module;
public class Modules {
MoveModul MOVE = new MoveModul();
}
@@ -1,30 +0,0 @@
package io.github.skippyall.minions.program.module;
import io.github.skippyall.minions.program.block.CodeBlock;
import io.github.skippyall.minions.program.block.CodeBlocks;
import io.github.skippyall.minions.program.block.ForwardBlock;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
public class MoveModul extends Modul{
MoveModul() {
super("Movement");
}
public List<CodeBlock> getCodeBlocks() {
List<CodeBlock> codeBlocks = new ArrayList<>();
codeBlocks.add(CodeBlocks.FORWARD);
return codeBlocks;
}
@Override
public Item getPolymerItem(ItemStack itemStack, @Nullable ServerPlayer player) {
return Items.PURPLE_GLAZED_TERRACOTTA;
}
}
@@ -1,4 +0,0 @@
package io.github.skippyall.minions.program.variables;
public class IntegerType extends Type{
}
@@ -1,4 +0,0 @@
package io.github.skippyall.minions.program.variables;
public abstract class Type {
}
@@ -1,5 +0,0 @@
package io.github.skippyall.minions.program.variables;
public class Types {
public static final IntegerType INTEGER = new IntegerType();
}
@@ -1,22 +0,0 @@
package io.github.skippyall.minions.server;
import io.github.skippyall.minions.networking.ClientToServerNetworking;
import io.github.skippyall.minions.networking.VersionChecker;
import net.fabricmc.api.DedicatedServerModInitializer;
import net.fabricmc.fabric.api.networking.v1.ServerConfigurationConnectionEvents;
import net.fabricmc.fabric.api.networking.v1.ServerConfigurationNetworking;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerConfigurationPacketListenerImpl;
public class MinionsServer implements DedicatedServerModInitializer {
@Override
public void onInitializeServer() {
ServerConfigurationNetworking.registerGlobalReceiver(ClientToServerNetworking.RL, ClientToServerNetworking::receive);
ServerConfigurationConnectionEvents.CONFIGURE.register(new ServerConfigurationConnectionEvents.Configure() {
@Override
public void onSendConfiguration(ServerConfigurationPacketListenerImpl handler, MinecraftServer server) {
VersionChecker.resetPlayer(handler.getOwner().getId());
}
});
}
}
+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,46 @@
package io.github.skippyall.minions;
import net.fabricmc.loader.api.FabricLoader;
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
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
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,21 +1,45 @@
package io.github.skippyall.minions; package io.github.skippyall.minions;
import eu.pb4.polymer.core.api.entity.PolymerEntityUtils; import eu.pb4.polymer.resourcepack.api.PolymerResourcePackUtils;
import io.github.skippyall.minions.minion.MinionItem; import io.github.skippyall.minions.command.MinionsCommand;
import io.github.skippyall.minions.docs.DocsManager;
import io.github.skippyall.minions.minion.MinionPersistentState;
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
import io.github.skippyall.minions.polymer.VersionSync;
import io.github.skippyall.minions.registration.MinionRegistration;
import net.fabricmc.api.ModInitializer; import net.fabricmc.api.ModInitializer;
import net.minecraft.registry.Registries; import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.registry.Registry; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.minecraft.util.Identifier; import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
import net.minecraft.server.packs.PackType;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
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 MinionItem MINION_ITEM = Registry.register(Registries.ITEM, new Identifier(MOD_ID, "minion"), new MinionItem());
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() {
PolymerEntityUtils.registerType(); MinionsConfig.get();
MinionRegistration.register();
VersionSync.register();
ServerLifecycleEvents.SERVER_STARTED.register(server -> {
MinionPersistentState.get(server).getMinionData().forEach((uuid, data) -> {
if(data.isSpawned()) {
MinionFakePlayer.spawnMinion(data, server.overworld(), null, null, true);
}
});
});
CommandRegistrationCallback.EVENT.register(MinionsCommand::register);
PolymerResourcePackUtils.addModAssets(Minions.MOD_ID);
ResourceManagerHelper.get(PackType.SERVER_DATA).registerReloadListener(new DocsManager());
} }
} }
@@ -0,0 +1,86 @@
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 java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class MinionsConfig {
private static 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();
}
}*/
}
@@ -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,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()).name();
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,78 @@
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 java.util.Optional;
import java.util.UUID;
public abstract class InstructionBoundBlockEntity<L extends BlockEntityMinionListener<?>> extends BlockEntity {
protected 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 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 L getListener() {
return BlockEntityMinionListener.getListener(level, worldPosition, minionUuid, getListenerClass());
}
}
@@ -0,0 +1,137 @@
package io.github.skippyall.minions.block.miniontrigger;
import com.mojang.serialization.MapCodec;
import eu.pb4.polymer.core.api.block.PolymerBlock;
import eu.pb4.polymer.core.api.utils.PolymerClientDecoded;
import eu.pb4.polymer.resourcepack.api.PolymerResourcePackUtils;
import eu.pb4.polymer.virtualentity.api.BlockWithElementHolder;
import eu.pb4.polymer.virtualentity.api.ElementHolder;
import eu.pb4.polymer.virtualentity.api.elements.ItemDisplayElement;
import io.github.skippyall.minions.Minions;
import io.github.skippyall.minions.block.instruction_bound.InstructionBoundBlock;
import io.github.skippyall.minions.polymer.VersionSync;
import io.github.skippyall.minions.registration.MinionBlocks;
import net.fabricmc.fabric.api.networking.v1.context.PacketContext;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.component.DataComponents;
import net.minecraft.resources.Identifier;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.network.ServerGamePacketListenerImpl;
import net.minecraft.world.item.ItemDisplayContext;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
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.DiodeBlock;
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.jetbrains.annotations.Nullable;
public class MinionTriggerBlock extends InstructionBoundBlock implements PolymerBlock, PolymerClientDecoded, BlockWithElementHolder {
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
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;
}
@Override
public BlockState getPolymerBlockState(BlockState state, PacketContext context) {
return VersionSync.isOnClient(context) ? state : net.minecraft.world.level.block.Blocks.COMPARATOR.defaultBlockState().setValue(DiodeBlock.POWERED, state.getValue(POWERED));
}
@Override
public boolean handleMiningOnServer(ItemStack tool, BlockState state, BlockPos pos, ServerPlayer player) {
return false;
}
@Override
public @Nullable ElementHolder createElementHolder(ServerLevel world, BlockPos pos, BlockState initialBlockState) {
ElementHolder holder = new ElementHolder() {
@Override
public boolean startWatching(ServerGamePacketListenerImpl player) {
if(PolymerResourcePackUtils.hasMainPack(player) && !VersionSync.isOnClient(player)) {
return super.startWatching(player);
} else {
return false;
}
}
};
ItemStack stack = new ItemStack(Items.BARRIER);
stack.set(DataComponents.ITEM_MODEL, Identifier.fromNamespaceAndPath(Minions.MOD_ID, "minion_trigger_no_plate_" + (initialBlockState.getValue(MinionTriggerBlock.POWERED) ? "active" : "inactive")));
ItemDisplayElement element = new ItemDisplayElement(stack);
element.setItemDisplayContext(ItemDisplayContext.NONE);
holder.addElement(element);
return holder;
}
}
@@ -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,26 @@
package io.github.skippyall.minions.block.miniontrigger;
import eu.pb4.polymer.core.api.item.PolymerBlockItem;
import eu.pb4.polymer.resourcepack.api.PolymerResourcePackUtils;
import net.fabricmc.fabric.api.networking.v1.context.PacketContext;
import net.minecraft.core.HolderLookup;
import net.minecraft.resources.Identifier;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Block;
import org.jetbrains.annotations.Nullable;
public class MinionTriggerBlockItem extends PolymerBlockItem {
public MinionTriggerBlockItem(Block block, Properties settings, Item polymerItem) {
super(block, settings, polymerItem, true);
}
@Override
public @Nullable Identifier getPolymerItemModel(ItemStack stack, PacketContext context, HolderLookup.Provider lookup) {
if(PolymerResourcePackUtils.hasMainPack(context)) {
return super.getPolymerItemModel(stack, context, lookup);
} else {
return null;
}
}
}
@@ -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());
}
}
}
@@ -1,16 +0,0 @@
package io.github.skippyall.minions.client;
import io.github.skippyall.minions.networking.ClientToServerNetworking;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationConnectionEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientLoginConnectionEvents;
public class MinionsClient implements ClientModInitializer {
/**
* Runs the mod initializer on the client environment.
*/
@Override
public void onInitializeClient() {
ClientConfigurationConnectionEvents.INIT.register(ClientToServerNetworking::onConfigurationInit);
}
}
@@ -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,52 @@
package io.github.skippyall.minions.clipboard;
import eu.pb4.polymer.core.api.item.PolymerItem;
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
import io.github.skippyall.minions.registration.MinionComponentTypes;
import io.github.skippyall.minions.registration.MinionItems;
import net.fabricmc.fabric.api.networking.v1.context.PacketContext;
import net.minecraft.core.BlockPos;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.component.DataComponents;
import net.minecraft.resources.Identifier;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.Nullable;
public class ClipboardItem extends Item implements PolymerItem {
public ClipboardItem(Properties settings) {
super(settings);
}
@Override
public Item getPolymerItem(ItemStack itemStack, PacketContext context) {
return /*VersionSync.isOnClient(context) ? this : */Items.PAPER;
}
@Override
public @Nullable Identifier getPolymerItemModel(ItemStack stack, PacketContext context, HolderLookup.Provider lookup) {
return null;
}
@Override
public ItemStack getPolymerItemStack(ItemStack itemStack, TooltipFlag tooltipType, PacketContext context, HolderLookup.Provider lookup) {
ItemStack stack = PolymerItem.super.getPolymerItemStack(itemStack, tooltipType, context, lookup);
stack.set(DataComponents.ENCHANTMENT_GLINT_OVERRIDE, true);
return stack;
}
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,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.name() + "(" + minion.uuid() + "):" + 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.name());
}
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,56 @@
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 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, 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,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,143 @@
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.SimpleResourceReloadListener;
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.server.packs.resources.ResourceManager;
import net.minecraft.util.StrictJsonParser;
import net.minecraft.util.Tuple;
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;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
public class DocsManager implements SimpleResourceReloadListener<Tuple<Map<Identifier, DocsEntry>, DocsTree>> {
private static Map<Identifier, DocsEntry> docs;
private static DocsTree tree;
public static 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 DocsEntry getDocsEntry(Identifier id) {
return docs.get(id);
}
public static Collection<Identifier> getDocsEntryIds() {
return docs.keySet();
}
@Override
public CompletableFuture<Tuple<Map<Identifier, DocsEntry>, DocsTree>> load(ResourceManager resourceManager, Executor executor) {
return CompletableFuture.supplyAsync(() -> {
Map<Identifier, Resource> resources = resourceManager.listResources("docs", id -> id.getNamespace().equals(Minions.MOD_ID) && id.getPath().endsWith(".json"));
final DocsTree.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);
}
}, executor);
}
@Override
public CompletableFuture<Void> apply(Tuple<Map<Identifier, DocsEntry>, DocsTree> o, ResourceManager resourceManager, Executor executor) {
return CompletableFuture.supplyAsync(() -> {
docs = o.getA();
tree = o.getB();
return null;
});
}
@Override
public Identifier getFabricId() {
return Identifier.fromNamespaceAndPath(Minions.MOD_ID, "docs");
}
}
@@ -0,0 +1,146 @@
package io.github.skippyall.minions.docs;
import com.mojang.datafixers.util.Either;
import com.mojang.serialization.Codec;
import net.minecraft.resources.Identifier;
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 BranchElement parent;
public BranchElement getParent() {
return parent;
}
void setParent(BranchElement parent) {
this.parent = parent;
}
public DocElement next() {
return parent.next(this);
}
public DocElement previous() {
return parent.previous(this);
}
}
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 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 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,81 @@
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 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 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;
}
}
@@ -1,7 +0,0 @@
package io.github.skippyall.minions.fakeplayer;
import io.netty.channel.Channel;
public interface ClientConnectionInterface {
void setChannel(Channel channel);
}
@@ -1,625 +0,0 @@
package io.github.skippyall.minions.fakeplayer;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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
{
private final ServerPlayerEntity player;
private final Map<ActionType, Action> actions = new EnumMap<>(ActionType.class);
private BlockPos currentBlock;
private int blockHitDelay;
private boolean isHittingBlock;
private float curBlockDamageMP;
private boolean sneaking;
private boolean sprinting;
private float forward;
private float strafing;
private int itemUseCooldown;
public EntityPlayerActionPack(ServerPlayerEntity playerIn)
{
player = playerIn;
stopAll();
}
public void copyFrom(EntityPlayerActionPack other)
{
actions.putAll(other.actions);
currentBlock = other.currentBlock;
blockHitDelay = other.blockHitDelay;
isHittingBlock = other.isHittingBlock;
curBlockDamageMP = other.curBlockDamageMP;
sneaking = other.sneaking;
sprinting = other.sprinting;
forward = other.forward;
strafing = other.strafing;
itemUseCooldown = other.itemUseCooldown;
}
public EntityPlayerActionPack start(ActionType type, Action action)
{
Action previous = actions.remove(type);
if (previous != null) type.stop(player, previous);
if (action != null)
{
actions.put(type, action);
type.start(player, action); // noop
}
return this;
}
public EntityPlayerActionPack setSneaking(boolean doSneak)
{
sneaking = doSneak;
player.setSneaking(doSneak);
if (sprinting && sneaking)
setSprinting(false);
return this;
}
public EntityPlayerActionPack setSprinting(boolean doSprint)
{
sprinting = doSprint;
player.setSprinting(doSprint);
if (sneaking && sprinting)
setSneaking(false);
return this;
}
public EntityPlayerActionPack setForward(float value)
{
forward = value;
return this;
}
public EntityPlayerActionPack setStrafing(float value)
{
strafing = value;
return this;
}
public EntityPlayerActionPack look(Direction direction)
{
return switch (direction)
{
case NORTH -> look(180, 0);
case SOUTH -> look(0, 0);
case EAST -> look(-90, 0);
case WEST -> look(90, 0);
case UP -> look(player.getYaw(), -90);
case DOWN -> look(player.getYaw(), 90);
};
}
public EntityPlayerActionPack look(Vec2f rotation)
{
return look(rotation.x, rotation.y);
}
public EntityPlayerActionPack look(float yaw, float pitch)
{
player.setYaw(yaw % 360); //setYaw
player.setPitch(MathHelper.clamp(pitch, -90, 90)); // setPitch
// maybe player.moveTo(player.getX(), player.getY(), player.getZ(), yaw, Mth.clamp(pitch,-90.0F, 90.0F));
return this;
}
public EntityPlayerActionPack lookAt(Vec3d position)
{
player.lookAt(EntityAnchorArgumentType.EntityAnchor.EYES, position);
return this;
}
public EntityPlayerActionPack turn(float yaw, float pitch)
{
return look(player.getYaw() + yaw, player.getPitch() + pitch);
}
public EntityPlayerActionPack turn(Vec2f rotation)
{
return turn(rotation.x, rotation.y);
}
public EntityPlayerActionPack stopMovement()
{
setSneaking(false);
setSprinting(false);
forward = 0.0F;
strafing = 0.0F;
return this;
}
public EntityPlayerActionPack stopAll()
{
for (ActionType type : actions.keySet()) type.stop(player, actions.get(type));
actions.clear();
return stopMovement();
}
public EntityPlayerActionPack mount(boolean onlyRideables)
{
//test what happens
List<Entity> entities;
if (onlyRideables)
{
entities = player.getWorld().getOtherEntities(player, player.getBoundingBox().expand(3.0D, 1.0D, 3.0D),
e -> e instanceof MinecartEntity || e instanceof BoatEntity || e instanceof AbstractHorseEntity);
}
else
{
entities = player.getWorld().getOtherEntities(player, player.getBoundingBox().expand(3.0D, 1.0D, 3.0D));
}
if (entities.size()==0)
return this;
Entity closest = null;
double distance = Double.POSITIVE_INFINITY;
Entity currentVehicle = player.getVehicle();
for (Entity e: entities)
{
if (e == player || (currentVehicle == e))
continue;
double dd = player.squaredDistanceTo(e);
if (dd<distance)
{
distance = dd;
closest = e;
}
}
if (closest == null) return this;
if (closest instanceof AbstractHorseEntity && onlyRideables)
((AbstractHorseEntity) closest).interactMob(player, Hand.MAIN_HAND);
else
player.startRiding(closest,true);
return this;
}
public EntityPlayerActionPack dismount()
{
player.stopRiding();
return this;
}
public void onUpdate()
{
Map<ActionType, Boolean> actionAttempts = new HashMap<>();
actions.values().removeIf(e -> e.done);
for (Map.Entry<ActionType, Action> e : actions.entrySet())
{
ActionType type = e.getKey();
Action action = e.getValue();
// skipping attack if use was successful
if (!(actionAttempts.getOrDefault(ActionType.USE, false) && type == ActionType.ATTACK))
{
Boolean actionStatus = action.tick(this, type);
if (actionStatus != null)
actionAttempts.put(type, actionStatus);
}
// optionally retrying use after successful attack and unsuccessful use
if (type == ActionType.ATTACK
&& actionAttempts.getOrDefault(ActionType.ATTACK, false)
&& !actionAttempts.getOrDefault(ActionType.USE, true) )
{
// according to MinecraftClient.handleInputEvents
Action using = actions.get(ActionType.USE);
if (using != null) // this is always true - we know use worked, but just in case
{
using.retry(this, ActionType.USE);
}
}
}
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
if (forward != 0.0F || player instanceof MinionFakePlayer) {
player.forwardSpeed = forward * vel;
}
if (strafing != 0.0F || player instanceof MinionFakePlayer) {
player.sidewaysSpeed = strafing * vel;
}
}
static HitResult getTarget(ServerPlayerEntity player)
{
double reach = player.interactionManager.isCreative() ? 5 : 4.5f;
return Tracer.rayTrace(player, 1, reach, false);
}
private void dropItemFromSlot(int slot, boolean dropAll)
{
PlayerInventory inv = player.getInventory(); // getInventory;
if (!inv.getStack(slot).isEmpty())
player.dropItem(inv.removeStack(slot,
dropAll ? inv.getStack(slot).getCount() : 1
), false, true); // scatter, keep owner
}
public void drop(int selectedSlot, boolean dropAll)
{
PlayerInventory inv = player.getInventory(); // getInventory;
if (selectedSlot == -2) // all
{
for (int i = inv.size(); i >= 0; i--)
dropItemFromSlot(i, dropAll);
}
else // one slot
{
if (selectedSlot == -1)
selectedSlot = inv.selectedSlot;
dropItemFromSlot(selectedSlot, dropAll);
}
}
public void setSlot(int slot)
{
player.getInventory().selectedSlot = slot-1;
player.networkHandler.sendPacket(new UpdateSelectedSlotS2CPacket(slot-1));
}
public enum ActionType
{
USE(true)
{
@Override
boolean execute(ServerPlayerEntity player, Action action)
{
EntityPlayerActionPack ap = ((ServerPlayerInterface) player).getActionPack();
if (ap.itemUseCooldown > 0)
{
ap.itemUseCooldown--;
return true;
}
if (player.isUsingItem())
{
return true;
}
HitResult hit = getTarget(player);
for (Hand hand : Hand.values())
{
switch (hit.getType())
{
case BLOCK:
{
player.updateLastActionTime();
ServerWorld world = player.getServerWorld();
BlockHitResult blockHit = (BlockHitResult) hit;
BlockPos pos = blockHit.getBlockPos();
Direction side = blockHit.getSide();
if (pos.getY() < player.getWorld().getTopY() - (side == Direction.UP ? 1 : 0) && world.canPlayerModifyAt(player, pos))
{
ActionResult result = player.interactionManager.interactBlock(player, world, player.getStackInHand(hand), hand, blockHit);
if (result.isAccepted())
{
if (result.shouldSwingHand()) player.swingHand(hand);
ap.itemUseCooldown = 3;
return true;
}
}
break;
}
case ENTITY:
{
player.updateLastActionTime();
EntityHitResult entityHit = (EntityHitResult) hit;
Entity entity = entityHit.getEntity();
boolean handWasEmpty = player.getStackInHand(hand).isEmpty();
boolean itemFrameEmpty = (entity instanceof ItemFrameEntity) && ((ItemFrameEntity) entity).getHeldItemStack().isEmpty();
Vec3d relativeHitPos = entityHit.getPos().subtract(entity.getX(), entity.getY(), entity.getZ());
if (entity.interactAt(player, relativeHitPos, hand).isAccepted())
{
ap.itemUseCooldown = 3;
return true;
}
// fix for SS itemframe always returns CONSUME even if no action is performed
if (player.interact(entity, hand).isAccepted() && !(handWasEmpty && itemFrameEmpty))
{
ap.itemUseCooldown = 3;
return true;
}
break;
}
}
ItemStack handItem = player.getStackInHand(hand);
if (player.interactionManager.interactItem(player, player.getWorld(), handItem, hand).isAccepted())
{
ap.itemUseCooldown = 3;
return true;
}
}
return false;
}
@Override
void inactiveTick(ServerPlayerEntity player, Action action)
{
EntityPlayerActionPack ap = ((ServerPlayerInterface) player).getActionPack();
ap.itemUseCooldown = 0;
player.stopUsingItem();
}
},
ATTACK(true) {
@Override
boolean execute(ServerPlayerEntity player, Action action) {
HitResult hit = getTarget(player);
switch (hit.getType()) {
case ENTITY: {
EntityHitResult entityHit = (EntityHitResult) hit;
if (!action.isContinuous)
{
player.attack(entityHit.getEntity());
player.swingHand(Hand.MAIN_HAND);
}
player.resetLastAttackedTicks();
player.updateLastActionTime();
return true;
}
case BLOCK: {
EntityPlayerActionPack ap = ((ServerPlayerInterface) player).getActionPack();
if (ap.blockHitDelay > 0)
{
ap.blockHitDelay--;
return false;
}
BlockHitResult blockHit = (BlockHitResult) hit;
BlockPos pos = blockHit.getBlockPos();
Direction side = blockHit.getSide();
if (player.isBlockBreakingRestricted(player.getWorld(), pos, player.interactionManager.getGameMode())) return false;
if (ap.currentBlock != null && player.getWorld().getBlockState(ap.currentBlock).isAir())
{
ap.currentBlock = null;
return false;
}
BlockState state = player.getWorld().getBlockState(pos);
boolean blockBroken = false;
if (player.interactionManager.getGameMode().isCreative())
{
player.interactionManager.processBlockBreakingAction(pos, PlayerActionC2SPacket.Action.START_DESTROY_BLOCK, side, player.getWorld().getTopY(), -1);
ap.blockHitDelay = 5;
blockBroken = true;
}
else if (ap.currentBlock == null || !ap.currentBlock.equals(pos))
{
if (ap.currentBlock != null)
{
player.interactionManager.processBlockBreakingAction(ap.currentBlock, PlayerActionC2SPacket.Action.ABORT_DESTROY_BLOCK, side, player.getWorld().getTopY(), -1);
}
player.interactionManager.processBlockBreakingAction(pos, PlayerActionC2SPacket.Action.START_DESTROY_BLOCK, side, player.getWorld().getTopY(), -1);
boolean notAir = !state.isAir();
if (notAir && ap.curBlockDamageMP == 0)
{
state.onBlockBreakStart(player.getWorld(), pos, player);
}
if (notAir && state.calcBlockBreakingDelta(player, player.getWorld(), pos) >= 1)
{
ap.currentBlock = null;
//instamine??
blockBroken = true;
}
else
{
ap.currentBlock = pos;
ap.curBlockDamageMP = 0;
}
}
else
{
ap.curBlockDamageMP += state.calcBlockBreakingDelta(player, player.getWorld(), pos);
if (ap.curBlockDamageMP >= 1)
{
player.interactionManager.processBlockBreakingAction(pos, PlayerActionC2SPacket.Action.STOP_DESTROY_BLOCK, side, player.getWorld().getTopY(), -1);
ap.currentBlock = null;
ap.blockHitDelay = 5;
blockBroken = true;
}
player.getWorld().setBlockBreakingInfo(-1, pos, (int) (ap.curBlockDamageMP * 10));
}
player.updateLastActionTime();
player.swingHand(Hand.MAIN_HAND);
return blockBroken;
}
}
return false;
}
@Override
void inactiveTick(ServerPlayerEntity player, Action action)
{
EntityPlayerActionPack ap = ((ServerPlayerInterface) player).getActionPack();
if (ap.currentBlock == null) return;
player.getWorld().setBlockBreakingInfo(-1, ap.currentBlock, -1);
player.interactionManager.processBlockBreakingAction(ap.currentBlock, PlayerActionC2SPacket.Action.ABORT_DESTROY_BLOCK, Direction.DOWN, player.getWorld().getTopY(), -1);
ap.currentBlock = null;
}
},
JUMP(true)
{
@Override
boolean execute(ServerPlayerEntity player, Action action)
{
if (action.limit == 1)
{
if (player.isOnGround()) player.jump(); // onGround
}
else
{
player.setJumping(true);
}
return false;
}
@Override
void inactiveTick(ServerPlayerEntity player, Action action)
{
player.setJumping(false);
}
},
DROP_ITEM(true)
{
@Override
boolean execute(ServerPlayerEntity player, Action action)
{
player.updateLastActionTime();
player.dropSelectedItem(false); // dropSelectedItem
return false;
}
},
DROP_STACK(true)
{
@Override
boolean execute(ServerPlayerEntity player, Action action)
{
player.updateLastActionTime();
player.dropSelectedItem(true); // dropSelectedItem
return false;
}
},
SWAP_HANDS(true)
{
@Override
boolean execute(ServerPlayerEntity player, Action action)
{
player.updateLastActionTime();
ItemStack itemStack_1 = player.getStackInHand(Hand.OFF_HAND);
player.setStackInHand(Hand.OFF_HAND, player.getStackInHand(Hand.MAIN_HAND));
player.setStackInHand(Hand.MAIN_HAND, itemStack_1);
return false;
}
};
public final boolean preventSpectator;
ActionType(boolean preventSpectator)
{
this.preventSpectator = preventSpectator;
}
void start(ServerPlayerEntity player, Action action) {}
abstract boolean execute(ServerPlayerEntity player, Action action);
void inactiveTick(ServerPlayerEntity player, Action action) {}
void stop(ServerPlayerEntity player, Action action)
{
inactiveTick(player, action);
}
}
public static class Action
{
public boolean done = false;
public final int limit;
public final int interval;
public final int offset;
private int count;
private int next;
private final boolean isContinuous;
private Action(int limit, int interval, int offset, boolean continuous)
{
this.limit = limit;
this.interval = interval;
this.offset = offset;
next = interval + offset;
isContinuous = continuous;
}
public static Action once()
{
return new Action(1, 1, 0, false);
}
public static Action continuous()
{
return new Action(-1, 1, 0, true);
}
public static Action interval(int interval)
{
return new Action(-1, interval, 0, false);
}
public static Action interval(int interval, int offset)
{
return new Action(-1, interval, offset, false);
}
Boolean tick(EntityPlayerActionPack actionPack, ActionType type)
{
next--;
Boolean cancel = null;
if (next <= 0)
{
if (interval == 1 && !isContinuous)
{
// need to allow entity to tick, otherwise won't have effect (bow)
// actions are 20 tps, so need to clear status mid tick, allowing entities process it till next time
if (!type.preventSpectator || !actionPack.player.isSpectator())
{
type.inactiveTick(actionPack.player, this);
}
}
if (!type.preventSpectator || !actionPack.player.isSpectator())
{
cancel = type.execute(actionPack.player, this);
}
count++;
if (count == limit)
{
type.stop(actionPack.player, null);
done = true;
return cancel;
}
next = interval;
}
else
{
if (!type.preventSpectator || !actionPack.player.isSpectator())
{
type.inactiveTick(actionPack.player, this);
}
}
return cancel;
}
void retry(EntityPlayerActionPack actionPack, ActionType type)
{
//assuming action run but was unsuccesful that tick, but opportunity emerged to retry it, lets retry it.
if (!type.preventSpectator || !actionPack.player.isSpectator())
{
type.execute(actionPack.player, this);
}
count++;
if (count == limit)
{
type.stop(actionPack.player, null);
done = true;
}
}
}
}
@@ -1,37 +0,0 @@
package io.github.skippyall.minions.fakeplayer;
import io.netty.channel.embedded.EmbeddedChannel;
import net.minecraft.network.ClientConnection;
import net.minecraft.network.NetworkSide;
import net.minecraft.network.listener.PacketListener;
import net.minecraft.text.Text;
public class FakeClientConnection extends ClientConnection {
public FakeClientConnection(NetworkSide p)
{
super(p);
// compat with adventure-platform-fabric. This does NOT trigger other vanilla handlers for establishing a channel
// also makes #isOpen return true, allowing enderpearls to teleport fake players
((ClientConnectionInterface)this).setChannel(new EmbeddedChannel());
}
@Override
public void tryDisableAutoRead()
{
}
@Override
public void handleDisconnection() {
getPacketListener().onDisconnected(getDisconnectReason()==null ? Text.literal("Disconnected"): getDisconnectReason());
}
@Override
public void setInitialPacketListener(PacketListener packetListener)
{
}
@Override
public void setPacketListener(PacketListener packetListener) {
}
}
@@ -1,277 +0,0 @@
package io.github.skippyall.minions.fakeplayer;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.yggdrasil.ProfileResult;
import io.github.skippyall.minions.minion.MinionInventory;
import io.github.skippyall.minions.minion.ModuleInventory;
import net.fabricmc.fabric.api.entity.FakePlayer;
import net.minecraft.block.BlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.MovementType;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.player.HungerManager;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
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.EntityPositionS2CPacket;
import net.minecraft.network.packet.s2c.play.EntitySetHeadYawS2CPacket;
import net.minecraft.network.packet.s2c.play.PlayerListS2CPacket;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.ServerTask;
import net.minecraft.server.network.ConnectedClientData;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableTextContent;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.Uuids;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.GameMode;
import java.util.UUID;
public class MinionFakePlayer extends ServerPlayerEntity {
public Runnable fixStartingPosition = () -> {};
public boolean isAShadow;
private float moveForward;
private float moveSideways;
private boolean programmable;
private ModuleInventory moduleInventory = new ModuleInventory();
public static void createMinion(String username, ServerWorld level, ServerPlayerEntity owner, boolean canProgram, Vec3d pos, double yaw, double pitch) {
MinecraftServer server = level.getServer();
server.getUserCache().findByNameAsync(username).thenAcceptAsync((optional) -> {
GameProfile profile = null;
if(optional.isPresent()){
UUID uuid = optional.get().getId();
ProfileResult result = server.getSessionService().fetchProfile(uuid, true);
if(result != null) {
profile = result.profile();
}
}
if(profile == null) {
profile = new GameProfile(Uuids.getOfflinePlayerUuid(username), username);
}
MinionFakePlayer instance = new MinionFakePlayer(server, level, profile, SyncedClientOptions.createDefault(), false, canProgram);
instance.fixStartingPosition = () -> instance.refreshPositionAndAngles(pos.x, pos.y, pos.z, (float) yaw, (float) pitch);
server.getPlayerManager().onPlayerConnect(new FakeClientConnection(NetworkSide.SERVERBOUND), instance, new ConnectedClientData(profile, 0, instance.getClientOptions()));
instance.teleport(level, pos.x, pos.y, pos.z, (float) yaw, (float) pitch);
instance.setHealth(20.0F);
instance.unsetRemoved();
instance.interactionManager.changeGameMode(GameMode.SURVIVAL);
server.getPlayerManager().sendToDimension(new EntitySetHeadYawS2CPacket(instance, (byte) (instance.headYaw * 256 / 360)), level.getRegistryKey());//instance.dimension);
server.getPlayerManager().sendToDimension(new EntityPositionS2CPacket(instance), level.getRegistryKey());//instance.dimension);
//instance.world.getChunkManager(). updatePosition(instance);
instance.dataTracker.set(PLAYER_MODEL_PARTS, (byte) 0x7f); // show all model layers (incl. capes)
instance.getAbilities().flying = false;
instance.setStepHeight(0.6F);
});
}
@SuppressWarnings("unused") //Don't know if I'll need this
public static MinionFakePlayer createShadow(MinecraftServer server, ServerPlayerEntity player)
{
player.getServer().getPlayerManager().remove(player);
player.networkHandler.disconnect(Text.translatable("multiplayer.disconnect.duplicate_login"));
ServerWorld worldIn = player.getServerWorld();//.getWorld(player.dimension);
GameProfile gameprofile = player.getGameProfile();
MinionFakePlayer playerShadow = new MinionFakePlayer(server, worldIn, gameprofile, player.getClientOptions(), true, false);
playerShadow.setSession(player.getSession());
server.getPlayerManager().onPlayerConnect(new FakeClientConnection(NetworkSide.SERVERBOUND), playerShadow, new ConnectedClientData(gameprofile, 0, player.getClientOptions()));
playerShadow.setHealth(player.getHealth());
playerShadow.networkHandler.requestTeleport(player.getX(), player.getY(), player.getZ(), player.getYaw(), player.getPitch());
playerShadow.interactionManager.changeGameMode(player.interactionManager.getGameMode());
((ServerPlayerInterface) playerShadow).getActionPack().copyFrom(((ServerPlayerInterface) player).getActionPack());
playerShadow.dataTracker.set(PLAYER_MODEL_PARTS, player.getDataTracker().get(PLAYER_MODEL_PARTS));
server.getPlayerManager().sendToDimension(new EntitySetHeadYawS2CPacket(playerShadow, (byte) (player.headYaw * 256 / 360)), playerShadow.getWorld().getRegistryKey());
server.getPlayerManager().sendToAll(new PlayerListS2CPacket(PlayerListS2CPacket.Action.ADD_PLAYER, playerShadow));
//player.world.getChunkManager().updatePosition(playerShadow);
playerShadow.getAbilities().flying = player.getAbilities().flying;
return playerShadow;
}
public static MinionFakePlayer respawnFake(MinecraftServer server, ServerWorld level, GameProfile profile, SyncedClientOptions cli, boolean programmable)
{
return new MinionFakePlayer(server, level, profile, cli, false, programmable);
}
private MinionFakePlayer(MinecraftServer server, ServerWorld worldIn, GameProfile profile, SyncedClientOptions cli, boolean shadow, boolean programmable)
{
super(server, worldIn, profile, cli);
isAShadow = shadow;
}
public boolean isProgrammable() {
return programmable;
}
public ModuleInventory getModuleInventory() {
return moduleInventory;
}
public EntityPlayerActionPack getActionPack() {
return ((ServerPlayerInterface)this).getActionPack();
}
@Override
public ActionResult interact(PlayerEntity player, Hand hand) {
if(player instanceof ServerPlayerEntity spe) {
MinionInventory.openInventory(spe, this);
}
return ActionResult.CONSUME;
}
@Override
public ActionResult interactAt(PlayerEntity player, Vec3d hitPos, Hand hand) {
return interact(player, hand);
}
@Override
public void onEquipStack(final EquipmentSlot slot, final ItemStack previous, final ItemStack stack)
{
if (!isUsingItem()) super.onEquipStack(slot, previous, stack);
}
@Override
public void kill()
{
kill(Text.literal("Killed"));
}
public void kill(Text reason)
{
shakeOff();
if (reason.getContent() instanceof TranslatableTextContent text && text.getKey().equals("multiplayer.disconnect.duplicate_login")) {
this.networkHandler.onDisconnected(reason);
} else {
this.server.send(new ServerTask(this.server.getTicks(), () -> {
this.networkHandler.onDisconnected(reason);
}));
}
}
@Override
public void tick()
{
if (this.getServer().getTicks() % 10 == 0)
{
this.networkHandler.syncWithPlayerPosition();
this.getServerWorld().getChunkManager().updatePosition(this);
}
try
{
super.tick();
this.playerTick();
}
catch (NullPointerException ignored)
{
// happens with that paper port thingy - not sure what that would fix, but hey
// the game not gonna crash violently.
}
}
private void shakeOff()
{
if (getVehicle() instanceof PlayerEntity) stopRiding();
for (Entity passenger : getPassengersDeep())
{
if (passenger instanceof PlayerEntity) passenger.stopRiding();
}
}
@Override
public void onDeath(DamageSource cause)
{
shakeOff();
super.onDeath(cause);
setHealth(20);
this.hungerManager = new HungerManager();
kill(this.getDamageTracker().getDeathMessage());
}
@Override
public String getIp()
{
return "127.0.0.1";
}
@Override
public boolean allowsServerListing() {
return false;
}
@Override
protected void fall(double y, boolean onGround, BlockState state, BlockPos pos) {
handleFall(0.0, y, 0.0, onGround);
}
@Override
public Entity moveToWorld(ServerWorld serverLevel)
{
super.moveToWorld(serverLevel);
if (notInAnyWorld) {
ClientStatusC2SPacket p = new ClientStatusC2SPacket(ClientStatusC2SPacket.Mode.PERFORM_RESPAWN);
networkHandler.onClientStatus(p);
}
// 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).
if (networkHandler.player.isInTeleportationState()) {
networkHandler.player.onTeleportationDone();
}
return networkHandler.player;
}
public void moveForward(float forward) {
this.moveForward += forward;
EntityPlayerActionPack actionPack = getActionPack();
if (moveForward != 0) {
actionPack.setForward(moveForward > 0 ? 1 : -1);
}
}
public void moveSideways(float sideways) {
this.moveSideways += sideways;
EntityPlayerActionPack actionPack = getActionPack();
if (moveSideways != 0) {
actionPack.setStrafing(moveSideways > 0 ? 1 : -1);
}
}
@Override
public void move(MovementType movementType, Vec3d movement) {
float newForward = (float) (moveForward - movement.z);
float newSideways = (float) (moveSideways - movement.x);
Vec3d newMovement = movement;
if ((newForward < 0 && moveForward >= 0) || (newForward > 0 && moveForward <= 0)) {
newMovement = new Vec3d(newMovement.x, newMovement.y, moveForward);
moveForward = 0;
getActionPack().setForward(0);
}else {
moveForward = newForward;
}
if ((newSideways < 0 && moveSideways >= 0) || (newSideways > 0 && moveSideways <= 0)) {
newMovement = new Vec3d(newMovement.x, newMovement.y, moveSideways);
moveSideways = 0;
getActionPack().setStrafing(0);
}else {
moveSideways = newSideways;
}
super.move(movementType, newMovement);
}
}
@@ -1,45 +0,0 @@
package io.github.skippyall.minions.fakeplayer;
import net.minecraft.network.ClientConnection;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.s2c.play.PositionFlag;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ConnectedClientData;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableTextContent;
import java.util.Set;
public class NetHandlerPlayServerFake extends ServerPlayNetworkHandler
{
public NetHandlerPlayServerFake(final MinecraftServer minecraftServer, final ClientConnection connection, final ServerPlayerEntity serverPlayer, final ConnectedClientData i)
{
super(minecraftServer, connection, serverPlayer, i);
}
@Override
public void sendPacket(final Packet<?> packetIn)
{
}
@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);
}
}
@Override
public void requestTeleport(double d, double e, double f, float g, float h, Set<PositionFlag> set)
{
super.requestTeleport(d, e, f, g, h, set);
if (player.getServerWorld().getPlayerByUuid(player.getUuid()) != null) {
syncWithPlayerPosition();
player.getServerWorld().getChunkManager().updatePosition(player);
}
}
}
@@ -1,8 +0,0 @@
package io.github.skippyall.minions.fakeplayer;
import io.github.skippyall.minions.fakeplayer.EntityPlayerActionPack;
public interface ServerPlayerInterface
{
EntityPlayerActionPack getActionPack();
}
@@ -1,89 +0,0 @@
package io.github.skippyall.minions.fakeplayer;
import java.util.Optional;
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 static HitResult rayTrace(Entity source, float partialTicks, double reach, boolean fluids)
{
BlockHitResult blockHit = rayTraceBlocks(source, partialTicks, reach, fluids);
double maxSqDist = reach * reach;
if (blockHit != null)
{
maxSqDist = blockHit.getPos().squaredDistanceTo(source.getCameraPosVec(partialTicks));
}
EntityHitResult entityHit = rayTraceEntities(source, partialTicks, reach, maxSqDist);
return entityHit == null ? blockHit : entityHit;
}
public static BlockHitResult rayTraceBlocks(Entity source, float partialTicks, double reach, boolean fluids)
{
Vec3d pos = source.getCameraPosVec(partialTicks);
Vec3d rotation = source.getRotationVec(partialTicks);
Vec3d reachEnd = pos.add(rotation.x * reach, rotation.y * reach, rotation.z * reach);
return source.getWorld().raycast(new RaycastContext(pos, reachEnd, RaycastContext.ShapeType.OUTLINE, fluids ?
RaycastContext.FluidHandling.ANY : RaycastContext.FluidHandling.NONE, source));
}
public static EntityHitResult rayTraceEntities(Entity source, float partialTicks, double reach, double maxSqDist)
{
Vec3d pos = source.getCameraPosVec(partialTicks);
Vec3d reachVec = source.getRotationVec(partialTicks).multiply(reach);
Box box = source.getBoundingBox().stretch(reachVec).expand(1);
return rayTraceEntities(source, pos, pos.add(reachVec), box, e -> !e.isSpectator() && e.canHit(), maxSqDist);
}
public static EntityHitResult rayTraceEntities(Entity source, Vec3d start, Vec3d end, Box box, Predicate<Entity> predicate, double maxSqDistance)
{
World world = source.getWorld();
double targetDistance = maxSqDistance;
Entity target = null;
Vec3d targetHitPos = null;
for (Entity current : world.getOtherEntities(source, box, predicate))
{
Box currentBox = current.getBoundingBox().expand(current.getTargetingMargin());
Optional<Vec3d> currentHit = currentBox.raycast(start, end);
if (currentBox.contains(start))
{
if (targetDistance >= 0)
{
target = current;
targetHitPos = currentHit.orElse(start);
targetDistance = 0;
}
}
else if (currentHit.isPresent())
{
Vec3d currentHitPos = currentHit.get();
double currentDistance = start.squaredDistanceTo(currentHitPos);
if (currentDistance < targetDistance || targetDistance == 0)
{
if (current.getRootVehicle() == source.getRootVehicle())
{
if (targetDistance == 0)
{
target = current;
targetHitPos = currentHitPos;
}
}
else
{
target = current;
targetHitPos = currentHitPos;
targetDistance = currentDistance;
}
}
}
}
return target == null ? null : new EntityHitResult(target, targetHitPos);
}
}
@@ -0,0 +1,5 @@
package io.github.skippyall.minions.gui;
public interface Displayable {
GuiDisplay getDisplay();
}
@@ -0,0 +1,162 @@
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_DISPLAY = createTooltipDisplay();
private static TooltipDisplay createTooltipDisplay() {
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_DISPLAY)
.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))
.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;
}
}
}
@@ -0,0 +1,98 @@
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 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.Optional;
public class MinionLookGui extends SimpleGui {
private ItemStack minionItem;
private SkinProvider currentSkinProvider;
public MinionLookGui(ServerPlayer player, ItemStack minionItem) {
super(MenuType.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(Component.literal(getData().name()))
.setCallback(() -> {
openRenameGui(player, minionItem);
})
);
}
private void updateSkin() {
GuiElementBuilder builder = new GuiElementBuilder()
.setItem(Items.PLAYER_HEAD)
.setCallback(() -> currentSkinProvider.openSkinMenu(player)
.thenCompose(profile -> profile.resolveProfile(player.level().getServer().services().profileResolver()))
.thenAccept(skin -> MinionItem.setData(player.level().getServer(), getData().withSkin(Optional.of(skin.properties())), minionItem)));
if(MinionItem.getData(player.level().getServer(), minionItem) != null && MinionItem.getData(player.level().getServer(), minionItem).skin().isPresent()) {
builder.setComponent(DataComponents.PROFILE, ResolvableProfile.createResolved(new GameProfile(FakePlayer.DEFAULT_UUID, "", getData().skin().get())));
}
setSlot(16, builder);
}
private void cycleSkinProvider() {
int currentId = MinionRegistries.SKIN_PROVIDERS.getId(currentSkinProvider);
currentId++;
if(MinionRegistries.SKIN_PROVIDERS.size() == currentId) {
currentId = 0;
}
currentSkinProvider = MinionRegistries.SKIN_PROVIDERS.byId(currentId);
updateSkinProvider();
}
private void updateSkinProvider() {
setSlot(25, new GuiElementBuilder()
.setItem(Items.GREEN_STAINED_GLASS_PANE)
.setComponent(DataComponents.CUSTOM_NAME, currentSkinProvider.getDisplayName())
.setCallback(this::cycleSkinProvider)
);
}
private MinionData getData() {
return MinionItem.getDataOrDefault(player.level().getServer(), minionItem);
}
public static void open(ServerPlayer player, ItemStack minionItem) {
MinionLookGui gui = new MinionLookGui(player, minionItem);
gui.update();
gui.open();
}
public void openRenameGui(ServerPlayer player, ItemStack minionItem) {
TextInput.inputSync(player, Component.translatable("minions.gui.look.rename.title"), "Minion", name -> MinionProfileUtils.checkMinionNameWithoutPrefix(player.level().getServer(), name))
.thenAccept(name -> {
MinionItem.setData(player.level().getServer(), getData().withName(MinionProfileUtils.getPrefix() + name), minionItem);
open();
});
}
}
@@ -0,0 +1,76 @@
package io.github.skippyall.minions.gui;
import eu.pb4.sgui.api.elements.GuiElementBuilder;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.Items;
import org.jetbrains.annotations.Nullable;
public abstract class MinionsGui {
protected final @Nullable MinionsGui parent;
protected final ServerPlayer viewer;
protected @Nullable MinionsGui child = null;
private boolean open = true;
public MinionsGui(MinionsGui parent) {
this.viewer = parent.viewer;
this.parent = parent;
parent.child = this;
}
public MinionsGui(ServerPlayer viewer) {
this.viewer = viewer;
this.parent = null;
}
public ServerPlayer getViewer() {
return viewer;
}
protected abstract void open();
protected void reopen() {
open();
}
public void onBackingClosed() {
if(child != null && child.open) {
return;
}
close(true);
}
public void close() {
close(false);
}
public void close(boolean alreadyClosed) {
if(open) {
open = false;
if(child != null) {
child.close(alreadyClosed);
} else if(!alreadyClosed) {
closeBacking();
}
}
}
public void goBack() {
if(parent != null) {
open = false;
parent.child = null;
parent.reopen();
} else {
close(false);
}
}
public GuiElementBuilder backButton() {
return new GuiElementBuilder(Items.MANGROVE_DOOR)
.setName(Component.translatable("gui.back"))
.setCallback(this::goBack);
}
protected abstract void closeBacking();
}
@@ -0,0 +1,87 @@
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 java.util.List;
import java.util.function.BiFunction;
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;
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();
}
@Override
protected void open() {
gui = new SimpleGui(MenuType.GENERIC_9x6, viewer, false) {
@Override
public void onPlayerClose(boolean success) {
onBackingClosed();
}
};
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));
}
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,122 @@
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.Displayable;
import io.github.skippyall.minions.gui.GuiDisplay;
import io.github.skippyall.minions.gui.MinionsGui;
import io.github.skippyall.minions.gui.minion.SimpleMinionsGui;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.item.Items;
import org.jetbrains.annotations.Nullable;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiFunction;
import java.util.function.Function;
public class ChoiceInput {
public static <T> BiFunction<ServerPlayer, T, CompletableFuture<T>> createDialogOpener(MenuType<?> screen, Component title, Function<T, GuiDisplay> displayFunction, T[] values, @Nullable T fallback) {
return (player, object) -> {
CompletableFuture<T> future = new CompletableFuture<>();
SimpleGui gui = new SimpleGui(screen, player, false) {
@Override
public void onPlayerClose(boolean success) {
if(fallback == null) {
future.cancel(false);
} else {
future.complete(fallback);
}
}
};
gui.setTitle(title);
for(T value : values) {
gui.addSlot(new GuiElementBuilder(displayFunction.apply(value).createItemStack())
.setCallback(() -> future.complete(value))
);
}
gui.open();
return future;
};
}
public static <T extends Displayable> BiFunction<ServerPlayer, T, CompletableFuture<T>> createDialogOpener(T[] values) {
return createDialogOpener(MenuType.GENERIC_9x3, Component.empty(), t -> t != null ? t.getDisplay() : null, values, null);
}
public static CompletableFuture<Void> confirm(ServerPlayer player, Component title) {
CompletableFuture<Void> future = new CompletableFuture<>();
SimpleGui gui = new SimpleGui(MenuType.GENERIC_3x3, player, false) {
@Override
public void onPlayerClose(boolean success) {
future.cancel(false);
}
};
gui.setTitle(title);
gui.setSlot(3, new GuiElementBuilder(Items.REDSTONE_BLOCK)
.setName(Component.translatable("minions.gui.abort"))
.setCallback(() -> future.cancel(false))
);
gui.setSlot(5, new GuiElementBuilder(Items.EMERALD_BLOCK)
.setName(Component.translatable("minions.gui.confirm"))
.setCallback(() -> future.complete(null))
);
gui.open();
return future;
}
public static CompletableFuture<Boolean> confirm(MinionsGui parent, Component title) {
CompletableFuture<Boolean> future = new CompletableFuture<>();
new SimpleMinionsGui(parent, (onClose, me) -> {
SimpleGui gui = new SimpleGui(MenuType.GENERIC_3x3, parent.getViewer(), false) {
@Override
public void onPlayerClose(boolean success) {
future.complete(false);
onClose.run();
}
};
gui.setTitle(title);
gui.setSlot(3, new GuiElementBuilder(Items.REDSTONE_BLOCK)
.setName(Component.translatable("minions.gui.abort"))
.setCallback(() -> {
future.complete(false);
me.goBack();
})
);
gui.setSlot(5, new GuiElementBuilder(Items.EMERALD_BLOCK)
.setName(Component.translatable("minions.gui.confirm"))
.setCallback(() -> {
future.complete(true);
me.goBack();
})
);
gui.open();
return gui;
});
return future;
}
public static BiFunction<ServerPlayer, Boolean, CompletableFuture<Boolean>> inputBoolean(Component title) {
return createDialogOpener(MenuType.GENERIC_3x3, title, value -> {
if(value) {
return new GuiDisplay.ItemBased(Items.EMERALD_BLOCK);
} else {
return new GuiDisplay.ItemBased(Items.REDSTONE_BLOCK);
}
}, new Boolean[]{false, true}, false);
}
}
@@ -0,0 +1,184 @@
package io.github.skippyall.minions.gui.input;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
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));
}
}
static <T, E> Result<T, E> ofNullable(@Nullable T value, E error) {
if(value != null) {
return new Success<>(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<>(value);
} else {
return new Error<>(error.get());
}
}
boolean isSuccess();
T getOrDefault(T defaultValue);
T getOrThrow();
E getErrorOrThrow();
@NotNull Optional<T> getOptional();
@NotNull Optional<E> getOptionalError();
void ifSuccess(@NotNull Consumer<T> handler);
void ifError(@NotNull 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 @NotNull 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 @NotNull Optional<T> getOptional() {
return Optional.ofNullable(result);
}
@Override
public void ifSuccess(@NotNull Consumer<T> handler) {
handler.accept(result);
}
@Override
public void ifError(@NotNull 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 @NotNull Optional<T> getOptional() {
return Optional.empty();
}
@Override
public @NotNull Optional<E> getOptionalError() {
return Optional.ofNullable(message);
}
@Override
public void ifSuccess(@NotNull Consumer<T> handler) {
}
@Override
public void ifError(@NotNull 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,125 @@
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 io.github.skippyall.minions.gui.minion.SimpleMinionsGui;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.inventory.AnvilMenu;
import net.minecraft.world.item.Items;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
public class TextInput<T> extends AnvilInputGui {
private final GuiElementBuilder valid = new GuiElementBuilder()
.setItem(Items.EMERALD_BLOCK)
.setName(Component.literal("OK"))
.setCallback(this::onConfirm);
private final GuiElementBuilder invalid = new GuiElementBuilder()
.setItem(Items.REDSTONE_BLOCK);
private final Function<String, CompletableFuture<Result<T, Component>>> parser;
private final CompletableFuture<T> future;
private Result<T, Component> result;
private boolean isConfirm;
public TextInput(ServerPlayer player, Component title, String defaultValue, Function<String, CompletableFuture<Result<T, Component>>> 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(ServerPlayer player, Component title, String defaultValue, Function<String, Result<T, Component>> parser) {
return input(player, title, defaultValue, (String string) -> CompletableFuture.completedFuture(parser.apply(string)));
}
public static <T> CompletableFuture<T> inputSync(MinionsGui gui, Component title, String defaultValue, Function<String, Result<T, Component>> parser) {
return input(gui, title, defaultValue, (String string) -> CompletableFuture.completedFuture(parser.apply(string)));
}
public static <T> CompletableFuture<T> input(ServerPlayer player, Component title, String defaultValue, Function<String, CompletableFuture<Result<T, Component>>> parser) {
CompletableFuture<T> future = new CompletableFuture<>();
new TextInput<>(player, title, defaultValue, parser, future).open();
return future;
}
public static <T> CompletableFuture<T> input(MinionsGui gui, Component title, String defaultValue, Function<String, CompletableFuture<Result<T, Component>>> parser) {
CompletableFuture<T> future = new CompletableFuture<>();
new SimpleMinionsGui(gui, (onClose, me) -> {
TextInput<T> input = new TextInput<>(gui.getViewer(), title, defaultValue, parser, future);
future.whenComplete((v, e) -> {
if(e != null) {
onClose.run();
} else {
me.goBack();
}
});
input.open();
return input;
});
return future;
}
public static CompletableFuture<String> inputString(ServerPlayer player, Component title, String defaultValue) {
return inputSync(player, title, defaultValue, Result.Success::new);
}
public static CompletableFuture<String> inputString(MinionsGui gui, Component title, String defaultValue) {
return inputSync(gui, title, defaultValue, Result.Success::new);
}
public static CompletableFuture<Long> inputLong(ServerPlayer player, Component title, String defaultValue) {
return inputSync(player, title, defaultValue, string -> Result.wrapCustomError(() -> Long.valueOf(string), Component.translatable("minions.command.input.int.fail")));
}
public static CompletableFuture<Long> inputLong(MinionsGui gui, Component title, String defaultValue) {
return inputSync(gui, title, defaultValue, string -> Result.wrapCustomError(() -> Long.valueOf(string), Component.translatable("minions.command.input.int.fail")));
}
public static CompletableFuture<Double> inputDouble(ServerPlayer player, Component title, String defaultValue) {
return inputSync(player, title, defaultValue, string -> Result.wrapCustomError(() -> Double.valueOf(string), Component.translatable("minions.command.input.float.fail")));
}
public static CompletableFuture<Double> inputDouble(MinionsGui gui, Component title, String defaultValue) {
return inputSync(gui, title, defaultValue, string -> Result.wrapCustomError(() -> Double.valueOf(string), Component.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(AnvilMenu.RESULT_SLOT, valid);
} else {
Component text = result.getErrorOrThrow();
setSlot(AnvilMenu.RESULT_SLOT, invalid.setName(text));
}
});
}
@Override
public void onPlayerClose(boolean success) {
if(!future.isDone() && !isConfirm) {
future.cancel(false);
}
}
public void onConfirm() {
if(result != null) {
result.ifSuccess(success -> {
isConfirm = true;
future.complete(success);
});
}
}
}
@@ -0,0 +1,157 @@
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.minion.fakeplayer.MinionFakePlayer;
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.jetbrains.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 @Nullable ValueSupplierList.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);
if(entry != null) {
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,144 @@
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.ChoiceInput;
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;
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 -> {
minion.getInstructionManager().setInstructionName(name, newName);
reopen();
}))
);
gui.setSlot(7, new GuiElementBuilder(Items.LAVA_BUCKET)
.setName(Component.translatable("minions.gui.instruction.configure.delete"))
.setCallback(() -> ChoiceInput.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));
})
);
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();
}
@Override
public void onStop(ConfiguredInstruction<?> instruction) {
updateRunSlot();
}
@Override
public void onSupplierChange(ConfiguredInstruction<?> instruction, Parameter<?> parameter) {
updateSuppliers();
}
private void updateRunSlot() {
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()))
);
}
}
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,122 @@
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.jetbrains.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);
open();
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;
}
@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,111 @@
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;
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()) {
page++;
updateConverters();
}
})
);
gui.setSlot(8, backButton());
gui.open();
}
public void updateConverters() {
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
int converterIndex = page * 4 + i;
//without input element
int actualConverterIndex = converterIndex - 1;
int slot = 9 + 2 * i;
if(converterIndex == 0) {
gui.setSlot(slot, new GuiElementBuilder(Items.DROPPER));
} else if(converterIndex == converters.getConverters().size() + 1) {
gui.setSlot(slot, new GuiElementBuilder(Items.HOPPER));
} else {
ValueConverter<?, ?> converter = converters.getConverters().get(actualConverterIndex);
ValueType<?> fromType = actualConverterIndex >= 1 ? converters.getConverters().get(actualConverterIndex - 1).getTo() : inputType;
ValueType<?> toType = actualConverterIndex < converters.getConverters().size() - 1 ? converters.getConverters().get(actualConverterIndex + 1).getFrom() : outputType;
gui.setSlot(slot, new GuiElementBuilder(GuiDisplay.getDisplayStackWithName(MinionRegistries.VALUE_CONVERTER_TYPES, converter.getType(), viewer.registryAccess()))
.addLoreLine(converter.getDisplayText())
.setCallback(() -> new ConverterGui(this, converter, fromType, toType, converters, false, actualConverterIndex))
);
}
if(i != 4 && actualConverterIndex != converters.getConverters().size()) {
ValueType<?> fromType = actualConverterIndex >= 0 ? converters.getConverters().get(actualConverterIndex).getTo() : inputType;
ValueType<?> toType = actualConverterIndex < converters.getConverters().size() - 1 ? converters.getConverters().get(actualConverterIndex + 1).getFrom() : outputType;
gui.setSlot(slot + 1, 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, actualConverterIndex + 1))
);
}
}
}
@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.jetbrains.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.getViewer();
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.getViewer();
selectInstructionModuleMenu(parent, context).thenAccept(instructionType ->
inputInstructionName(parent, context, "Instruction").thenAccept(name -> {
if (!minion.isRemoved() && !minion.hasDisconnected()) {
ConfiguredInstruction<MinionRuntime> configuredInstruction = minion.getInstructionManager().createInstruction(name, instructionType);
new ConfigureInstructionGui(parent, GuiContext.Instruction.create(context, configuredInstruction, name));
}
})
);
}
public static CompletableFuture<String> inputInstructionName(MinionsGui parent, GuiContext.Minion context, String defaultValue) {
return TextInput.inputSync(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.getViewer();
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.getViewer(), 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.getViewer().registryAccess())
.setCallback(() -> future.complete(instructionType))
);
slot++;
}
gui.open();
return gui;
});
return future;
}
public static GuiElementBuilder createInstructionElement(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,43 @@
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;
public interface GuiContext {
ServerPlayer getViewer();
static GuiContext create(ServerPlayer viewer) {
return new GuiContextImpl(viewer);
}
interface Minion extends GuiContext {
MinionFakePlayer getMinion();
static GuiContext.Minion create(GuiContext context, MinionFakePlayer minion) {
return new GuiContextImpl.MinionImpl(context, minion);
}
}
interface Instruction extends Minion {
ConfiguredInstruction<MinionRuntime> getInstruction();
String getName();
void setName(String name);
static GuiContext.Instruction create(GuiContext.Minion context, ConfiguredInstruction<MinionRuntime> instruction, String name) {
return new GuiContextImpl.InstructionImpl(context, instruction, name);
}
}
interface ValueSupplier extends Instruction {
Parameter<?> getParameter();
static GuiContext.ValueSupplier create(GuiContext.Instruction context, Parameter<?> parameter) {
return new GuiContextImpl.ValueSupplierImpl(context, parameter);
}
}
}
@@ -0,0 +1,120 @@
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;
//If only this mod was kotlin
public class GuiContextImpl implements GuiContext {
private final ServerPlayer viewer;
public GuiContextImpl(ServerPlayer viewer) {
this.viewer = viewer;
}
@Override
public ServerPlayer getViewer() {
return viewer;
}
public static class MinionImpl extends DelegatingGuiContextImpl<GuiContext> implements GuiContext.Minion {
private final MinionFakePlayer minion;
public MinionImpl(GuiContext context, MinionFakePlayer minion) {
super(context instanceof DelegatingGuiContextImpl<?> impl ? impl.context : context);
this.minion = minion;
}
@Override
public MinionFakePlayer getMinion() {
return minion;
}
}
public static class InstructionImpl extends DelegatingMinionImpl<GuiContext.Minion> implements GuiContext.Instruction {
private final ConfiguredInstruction<MinionRuntime> instruction;
private String name;
public InstructionImpl(GuiContext.Minion context, ConfiguredInstruction<MinionRuntime> instruction, String name) {
super(context instanceof DelegatingMinionImpl<?> impl ? impl.context : context);
this.instruction = instruction;
this.name = name;
}
@Override
public ConfiguredInstruction<MinionRuntime> getInstruction() {
return instruction;
}
@Override
public String getName() {
return name;
}
@Override
public void setName(String name) {
this.name = name;
}
}
public static class ValueSupplierImpl extends DelegatingInstructionImpl<GuiContext.Instruction> implements GuiContext.ValueSupplier {
private final Parameter<?> parameter;
public ValueSupplierImpl(GuiContext.Instruction context, Parameter<?> parameter) {
super(context instanceof DelegatingInstructionImpl<?> impl ? impl.context : context);
this.parameter = parameter;
}
@Override
public Parameter<?> getParameter() {
return parameter;
}
}
public static class DelegatingGuiContextImpl<C extends GuiContext> implements GuiContext {
protected final C context;
public DelegatingGuiContextImpl(C context) {
this.context = context;
}
@Override
public ServerPlayer getViewer() {
return context.getViewer();
}
}
public static class DelegatingMinionImpl<C extends GuiContext.Minion> extends DelegatingGuiContextImpl<C> implements GuiContext.Minion {
public DelegatingMinionImpl(C context) {
super(context);
}
@Override
public MinionFakePlayer getMinion() {
return context.getMinion();
}
}
public static class DelegatingInstructionImpl<C extends GuiContext.Instruction> extends DelegatingMinionImpl<C> implements GuiContext.Instruction {
public DelegatingInstructionImpl(C context) {
super(context);
}
@Override
public ConfiguredInstruction<MinionRuntime> getInstruction() {
return context.getInstruction();
}
@Override
public String getName() {
return context.getName();
}
@Override
public void setName(String name) {
context.setName(name);
}
}
}
@@ -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,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.jetbrains.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<?>> T getListener(Level world, BlockPos pos, UUID minionUuid, Class<T> clazz) {
if(minionUuid != null) {
for (MinionListener listener : MinionPersistentState.get(world.getServer()).getMinionData(minionUuid).listeners()) {
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).listeners().addListener(this);
MinionPersistentState.get(server).setDirty();
this.minion = (MinionFakePlayer) server.getPlayerList().getPlayer(minionUuid);
}
public void remove(MinecraftServer server) {
MinionPersistentState.get(server).getMinionData(minionUuid).listeners().removeListener(this);
MinionPersistentState.get(server).setDirty();
}
public enum BlockEntityState {
LOADED,
REMOVED,
UNLOADED
}
}
@@ -0,0 +1,37 @@
package io.github.skippyall.minions.listener;
import org.jetbrains.annotations.NotNull;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
public class ListenerManager<T> implements Iterable<T> {
protected final Set<T> listeners;
public ListenerManager() {
this(new CopyOnWriteArraySet<>());
}
protected ListenerManager(Set<T> listeners) {
this.listeners = listeners;
}
public void addListener(T listener) {
listeners.add(listener);
}
public void removeListener(T listener) {
listeners.remove(listener);
}
@Override
public @NotNull Iterator<T> iterator() {
return listeners.iterator();
}
@Override
public boolean equals(Object obj) {
return super.equals(obj);
}
}
@@ -0,0 +1,46 @@
package io.github.skippyall.minions.listener;
import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import net.minecraft.core.Registry;
import net.minecraft.resources.Identifier;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
public class SerializableListenerManager<T extends SerializableListenerManager.SerializableListener> extends ListenerManager<T> {
public SerializableListenerManager() {
super();
}
protected SerializableListenerManager(Set<T> listeners) {
super(listeners);
}
public static <T extends SerializableListener> Codec<SerializableListenerManager<T>> getCodec(Registry<Codec<? extends T>> registry) {
return registry.byNameCodec().<T>dispatch(
listener -> listener.getCodecId().map(registry::getValue).orElse(MapCodec.unitCodec(null)),
codec -> codec.fieldOf("data")
).listOf().xmap(
list -> new SerializableListenerManager<>(new CopyOnWriteArraySet<>(list)),
manager -> {
List<T> serializableListeners = new ArrayList<>();
for(T listener : manager.listeners) {
if(listener.getCodecId().isPresent()) {
serializableListeners.add(listener);
}
}
return serializableListeners;
}
);
}
public interface SerializableListener {
default Optional<Identifier> getCodecId() {
return Optional.empty();
}
}
}
@@ -1,132 +0,0 @@
package io.github.skippyall.minions.minion;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.Inventories;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.util.collection.DefaultedList;
/**
* A simple {@code Inventory} implementation with only default methods + an item list getter.
*
* Originally by Juuz
*/
public interface ImplementedInventory extends Inventory {
/**
* Retrieves the item list of this inventory.
* Must return the same instance every time it's called.
*/
DefaultedList<ItemStack> getItems();
/**
* Creates an inventory from the item list.
*/
static ImplementedInventory of(DefaultedList<ItemStack> items) {
return () -> items;
}
/**
* Creates a new inventory with the specified size.
*/
static ImplementedInventory ofSize(int size) {
return of(DefaultedList.ofSize(size, ItemStack.EMPTY));
}
/**
* Returns the inventory size.
*/
@Override
default int size() {
return getItems().size();
}
/**
* Checks if the inventory is empty.
* @return true if this inventory has only empty stacks, false otherwise.
*/
@Override
default boolean isEmpty() {
for (int i = 0; i < size(); i++) {
ItemStack stack = getStack(i);
if (!stack.isEmpty()) {
return false;
}
}
return true;
}
/**
* Retrieves the item in the slot.
*/
@Override
default ItemStack getStack(int slot) {
return getItems().get(slot);
}
/**
* Removes items from an inventory slot.
* @param slot The slot to remove from.
* @param count How many items to remove. If there are less items in the slot than what are requested,
* takes all items in that slot.
*/
@Override
default ItemStack removeStack(int slot, int count) {
ItemStack result = Inventories.splitStack(getItems(), slot, count);
if (!result.isEmpty()) {
markDirty();
}
return result;
}
/**
* Removes all items from an inventory slot.
* @param slot The slot to remove from.
*/
@Override
default ItemStack removeStack(int slot) {
return Inventories.removeStack(getItems(), slot);
}
/**
* Replaces the current stack in an inventory slot with the provided stack.
* @param slot The inventory slot of which to replace the itemstack.
* @param stack The replacing itemstack. If the stack is too big for
* this inventory ({@link Inventory#getMaxCountPerStack()}),
* it gets resized to this inventory's maximum amount.
*/
@Override
default void setStack(int slot, ItemStack stack) {
getItems().set(slot, stack);
if (stack.getCount() > stack.getMaxCount()) {
stack.setCount(stack.getMaxCount());
}
}
/**
* Clears the inventory.
*/
@Override
default void clear() {
getItems().clear();
}
/**
* Marks the state as dirty.
* Must be called after changes in the inventory, so that the game can properly save
* the inventory contents and notify neighboring blocks of inventory changes.
*/
@Override
default void markDirty() {
// Override if you want behavior.
}
/**
* @return true if the player can use the inventory, false otherwise.
*/
@Override
default boolean canPlayerUse(PlayerEntity player) {
return true;
}
}
@@ -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);
}
}
}
@@ -0,0 +1,58 @@
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.Optional;
import java.util.UUID;
public record MinionData(
UUID uuid,
String name,
Optional<PropertyMap> skin,
boolean isSpawned,
SerializableListenerManager<MinionListener> listeners,
MinionConfig config
) {
public static final Codec<MinionData> CODEC = 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(MinionRegistries.MINION_LISTENER_CODECS).optionalFieldOf("listeners").xmap(
optional -> optional.orElseGet(SerializableListenerManager::new),
Optional::of
).forGetter(MinionData::listeners),
MinionConfig.CODEC.optionalFieldOf("config", new MinionConfig()).forGetter(MinionData::config)
).apply(instance, MinionData::new)
);
public static MinionData createDefault(MinecraftServer server) {
return new MinionData(
UUID.randomUUID(),
MinionProfileUtils.newDefaultMinionName(server),
Optional.empty(),
false,
new SerializableListenerManager<>(),
new MinionConfig()
);
}
public MinionData withName(String name) {
return new MinionData(uuid, name, skin, isSpawned, listeners, config);
}
public MinionData withSkin(Optional<PropertyMap> skin) {
return new MinionData(uuid, name, skin, isSpawned, listeners, config);
}
public MinionData withSpawned(boolean isSpawned) {
return new MinionData(uuid, name, skin, isSpawned, listeners, config);
}
}
@@ -1,62 +0,0 @@
package io.github.skippyall.minions.minion;
import eu.pb4.sgui.api.elements.GuiElementBuilder;
import eu.pb4.sgui.api.gui.SimpleGui;
import io.github.skippyall.minions.fakeplayer.MinionFakePlayer;
import net.minecraft.item.Items;
import net.minecraft.screen.GenericContainerScreenHandler;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.screen.SimpleNamedScreenHandlerFactory;
import net.minecraft.screen.slot.Slot;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
public class MinionInventory {
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(Text.literal("Minion"));
gui.setSlot(4, new GuiElementBuilder()
.setItem(Items.REDSTONE)
.setName(Text.literal("Programming"))
.setCallback((i, clickType, clickType1) -> {
openProgrammingInventory(player, minion);
})
);
gui.setSlot(3, new GuiElementBuilder()
.setItem(Items.NETHERITE_UPGRADE_SMITHING_TEMPLATE)
.setName(Text.literal("Modules and Detectors"))
.setCallback((i, clickType, clickType1) -> {
openModuleInventory(player, minion);
})
);
gui.setSlot(5, new GuiElementBuilder()
.setItem(Items.CHEST)
.setName(Text.literal("Inventory"))
.setCallback((i, clickType, clickType1) -> {
openMinionInventory(player, minion);
})
);
gui.open();
}
public static void openProgrammingInventory(ServerPlayerEntity player, MinionFakePlayer minion) {
}
public static void openModuleInventory(ServerPlayerEntity player, MinionFakePlayer minion) {
player.openHandledScreen(new SimpleNamedScreenHandlerFactory((syncId, playerInventory, player2) -> GenericContainerScreenHandler.createGeneric9x3(syncId, playerInventory, minion.getModuleInventory()), Text.literal("")));
}
public static void openMinionInventory(ServerPlayerEntity player, MinionFakePlayer minion) {
SimpleGui gui = new SimpleGui(ScreenHandlerType.GENERIC_9X5, player, false);
gui.setTitle(Text.literal("Minion"));
for (int i = 0; i < minion.getInventory().size(); i++) {
gui.setSlotRedirect(i, new Slot(minion.getInventory(), i, 0, 0));
}
gui.open();
}
}
@@ -1,50 +1,106 @@
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.PolymerItem;
import eu.pb4.polymer.core.api.item.PolymerItemUtils; import io.github.skippyall.minions.gui.MinionLookGui;
import io.github.skippyall.minions.fakeplayer.MinionFakePlayer; import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
import net.fabricmc.fabric.api.item.v1.FabricItemSettings; import io.github.skippyall.minions.registration.MinionComponentTypes;
import net.minecraft.client.item.TooltipContext; import net.fabricmc.fabric.api.networking.v1.context.PacketContext;
import net.minecraft.enchantment.Enchantments; import net.minecraft.core.HolderLookup;
import net.minecraft.item.*; import net.minecraft.core.component.DataComponents;
import net.minecraft.nbt.NbtCompound; import net.minecraft.network.chat.Component;
import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.resources.Identifier;
import net.minecraft.server.world.ServerWorld; import net.minecraft.server.MinecraftServer;
import net.minecraft.text.PlainTextContent; import net.minecraft.server.level.ServerLevel;
import net.minecraft.text.TextContent; import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.ActionResult; import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.component.TooltipDisplay;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec2;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.function.Consumer;
public class MinionItem extends Item implements PolymerItem { public class MinionItem extends Item implements PolymerItem {
private boolean canProgram; public MinionItem(Properties settings) {
public MinionItem(boolean canProgram) { super(settings);
super(new FabricItemSettings());
} }
@Override @Override
public Item getPolymerItem(ItemStack itemStack, @Nullable ServerPlayerEntity player) { public @Nullable Identifier getPolymerItemModel(ItemStack stack, PacketContext context, HolderLookup.Provider lookup) {
return null;
}
@Override
public Item getPolymerItem(ItemStack itemStack, PacketContext player) {
return Items.ARMOR_STAND; return Items.ARMOR_STAND;
} }
@Override @Override
public ItemStack getPolymerItemStack(ItemStack stack, TooltipContext flag, ServerPlayerEntity player) { public ItemStack getPolymerItemStack(ItemStack stack, TooltipFlag tooltipType, PacketContext player, HolderLookup.Provider lookup) {
ItemStack out = PolymerItemUtils.createItemStack(stack, flag, player); ItemStack out = PolymerItem.super.getPolymerItemStack(stack, tooltipType, player, lookup);
out.addEnchantment(Enchantments.MENDING, 1); out.set(DataComponents.ENCHANTMENT_GLINT_OVERRIDE, true);
return out; return out;
} }
@Override @Override
public ActionResult useOnBlock(ItemUsageContext context) { public void appendHoverText(ItemStack stack, TooltipContext context, TooltipDisplay component, Consumer<Component> tooltip, TooltipFlag type) {
String contents = context.getStack().getName().getLiteralString(); MinionData data = null /*getData(stack)*/;
String name; if(data != null) {
if(contents != null) { tooltip.accept(Component.translatable("minions.minion_item.tooltip", data.name()));
name = contents; }
} else { }
name = "Minion";
} @Override
if(!context.getWorld().isClient) { public InteractionResult use(Level world, Player user, InteractionHand hand) {
MinionFakePlayer.createMinion(name, (ServerWorld) context.getWorld(), (ServerPlayerEntity) context.getPlayer(), canProgram, context.getBlockPos().toCenterPos().add(0,0.5,0), 0, 0); if(user instanceof ServerPlayer serverPlayer) {
} ItemStack stack = user.getItemInHand(hand);
return ActionResult.SUCCESS; MinionLookGui.open(serverPlayer, stack);
return InteractionResult.SUCCESS;
}
return InteractionResult.SUCCESS_SERVER;
}
@Override
public InteractionResult useOn(UseOnContext context) {
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;
}
public static void setData(MinecraftServer server, MinionData data, ItemStack item) {
item.set(MinionComponentTypes.MINION_DATA, data.uuid());
MinionPersistentState.get(server).updateMinionData(data);
}
@Nullable
public static MinionData getData(MinecraftServer server, ItemStack item) {
if(item.has(MinionComponentTypes.MINION_DATA)) {
return MinionPersistentState.get(server).getMinionData(item.get(MinionComponentTypes.MINION_DATA));
}
return null;
}
public static MinionData getDataOrDefault(MinecraftServer server, ItemStack item) {
MinionData data = getData(server, item);
if(data == null) {
data = MinionData.createDefault(server);
setData(server, data, item);
}
return data;
}
public static boolean containsData(ItemStack item) {
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.jetbrains.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);
}
}
}
}
@@ -0,0 +1,72 @@
package io.github.skippyall.minions.minion;
import com.mojang.serialization.Codec;
import io.github.skippyall.minions.Minions;
import net.minecraft.resources.Identifier;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.saveddata.SavedData;
import net.minecraft.world.level.saveddata.SavedDataType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
public class MinionPersistentState extends SavedData {
public static final Codec<MinionPersistentState> CODEC = MinionData.CODEC.listOf().xmap(MinionPersistentState::new, MinionPersistentState::getMinionDataList);
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<>();
public MinionPersistentState() {
}
public MinionPersistentState(List<MinionData> dataList) {
for (MinionData data : dataList) {
minionData.put(data.uuid(), data);
}
}
public MinionData getMinionData(UUID uuid) {
return minionData.get(uuid);
}
public Map<UUID, MinionData> getMinionData() {
return minionData;
}
public List<MinionData> getMinionDataList() {
return List.copyOf(minionData.values());
}
public void updateMinionData(MinionData data) {
minionData.put(data.uuid(), data);
setDirty();
}
public boolean isMinion(UUID uuid) {
return minionData.containsKey(uuid);
}
public boolean isMinionNameTaken(String name) {
return getMinionWithName(name).isPresent();
}
public Optional<MinionData> getMinionWithName(String name) {
return minionData.values().stream()
.filter(data -> data.name().equals(name))
.findFirst();
}
public static MinionPersistentState get(MinecraftServer server) {
return server.getLevel(Level.OVERWORLD).getDataStorage().computeIfAbsent(TYPE);
}
}
@@ -0,0 +1,63 @@
package io.github.skippyall.minions.minion;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.PropertyMap;
import com.mojang.brigadier.StringReader;
import io.github.skippyall.minions.MinionsConfig;
import io.github.skippyall.minions.gui.input.Result;
import net.minecraft.network.chat.Component;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.StringUtil;
import java.util.UUID;
import static io.github.skippyall.minions.Minions.LOGGER;
public class MinionProfileUtils {
public static String getPrefix() {
return MinionsConfig.get().minion.minionPrefix;
}
public static GameProfile makeNewMinionProfile(UUID uuidMinion, String username, PropertyMap skin) {
if(uuidMinion == null) {
uuidMinion = UUID.randomUUID();
}
GameProfile newProfile = new GameProfile(uuidMinion, username, skin != null ? skin : PropertyMap.EMPTY);
LOGGER.info("Minion Profile: {}", newProfile);
return newProfile;
}
public static Result<String, Component> checkMinionNameWithoutPrefix(MinecraftServer server, String name) {
for(char c : name.toCharArray()) {
if(!StringReader.isAllowedInUnquotedString(c)) {
return new Result.Error<>(Component.translatable("minions.generic.name.invalid_char"));
}
}
if((getPrefix() + name).length() > 16) {
return new Result.Error<>(Component.translatable("minions.generic.name.too_long"));
}
if(!StringUtil.isValidPlayerName(getPrefix() + name)) {
return new Result.Error<>(Component.translatable("minions.generic.name.invalid"));
}
if(MinionPersistentState.get(server).isMinionNameTaken(getPrefix() + name)) {
return new Result.Error<>(Component.translatable("minions.generic.name.taken"));
}
return new Result.Success<>(name);
}
public static String newDefaultMinionName(MinecraftServer server) {
int i = 0;
while (MinionPersistentState.get(server).isMinionNameTaken("+Minion" + i)) {
i++;
}
return "+Minion" + i;
}
public static boolean isMinion(MinecraftServer server, UUID uuid) {
return MinionPersistentState.get(server).isMinion(uuid);
}
}

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