Compare commits
32 Commits
1.21.4
...
f3c934c619
| Author | SHA1 | Date | |
|---|---|---|---|
| f3c934c619 | |||
| 71016f9e70 | |||
| 08f9763b83 | |||
| e117139a63 | |||
| f5202a4264 | |||
| cc9fedd63b | |||
| 324fea04a9 | |||
| 7acd083e79 | |||
| 9b61dba4c7 | |||
| 0382798a5d | |||
| 1e430ef506 | |||
| 5bd7c08f83 | |||
| 017b2ec590 | |||
| 382b394523 | |||
| 3f2a52fd0a | |||
| c5b3c883ca | |||
| b413592abf | |||
| cc1320d5c6 | |||
| da4351cff2 | |||
| 640e6f801a | |||
| bcfb406547 | |||
| b6f7bfc57b | |||
| e27b1698e4 | |||
| 216855d9c9 | |||
| e42e3adf84 | |||
| 66cbad38cb | |||
| 6640f95291 | |||
| 362bf15a19 | |||
| 2d64426496 | |||
| f5e0c1d426 | |||
| ec31e090b9 | |||
| 935b2225c9 |
@@ -1,5 +1,6 @@
|
|||||||
# User-specific stuff
|
# User-specific stuff
|
||||||
.idea/
|
.idea/
|
||||||
|
urlaub/
|
||||||
|
|
||||||
*.iml
|
*.iml
|
||||||
*.ipr
|
*.ipr
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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 ✓
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
- Module:
|
||||||
|
- Bewegungsmodul ✓
|
||||||
|
- Angriffsmodul ✓
|
||||||
|
- Abbaumodul ✓
|
||||||
|
- Platziermodul / Verwendungsmodul ✓
|
||||||
|
- Werfmodul
|
||||||
|
- Inventarmanagementmodul (Noch nicht wirklich)
|
||||||
|
- Inventaropenmodul (Nä)
|
||||||
|
|
||||||
|
- Detektoren:
|
||||||
|
- Blockdetektor
|
||||||
|
- Entitydetektor
|
||||||
|
- Dimensiondetektor
|
||||||
|
- Positiondetektor
|
||||||
|
- Inventardetektor
|
||||||
|
|
||||||
|
- Programmieren: Nö
|
||||||
|
- Variablen Nö
|
||||||
|
- (Listen) Nö
|
||||||
|
- Warten Nö
|
||||||
|
- Wiederholen Nö
|
||||||
|
- Wiederhole bis/während Nö
|
||||||
|
- Wiederhole x mal/(für jedes Element) Nö
|
||||||
|
- Bedingung Nö
|
||||||
|
|
||||||
|
- VariableTypen:
|
||||||
|
- Integer ✓
|
||||||
|
- String ✓
|
||||||
|
- Position (Nö)
|
||||||
|
- Block (weisned)
|
||||||
|
- BlockType
|
||||||
|
- (NBT) Nö
|
||||||
|
- (World) Nö
|
||||||
|
- (Chunk) Nö
|
||||||
|
- Inventory (weisned)
|
||||||
-124
@@ -1,124 +0,0 @@
|
|||||||
plugins {
|
|
||||||
id 'fabric-loom' version '1.8-SNAPSHOT'
|
|
||||||
id 'maven-publish'
|
|
||||||
}
|
|
||||||
|
|
||||||
version = project.mod_version
|
|
||||||
group = project.maven_group
|
|
||||||
|
|
||||||
loom {
|
|
||||||
accessWidenerPath = file("src/main/resources/minions.accesswidener")
|
|
||||||
}
|
|
||||||
|
|
||||||
base {
|
|
||||||
archivesName = project.archives_base_name
|
|
||||||
}
|
|
||||||
|
|
||||||
loom {
|
|
||||||
accessWidenerPath = file("src/main/resources/minions.accesswidener")
|
|
||||||
}
|
|
||||||
|
|
||||||
repositories {
|
|
||||||
// Add repositories to retrieve artifacts from in here.
|
|
||||||
// You should only use this when depending on other mods because
|
|
||||||
// Loom adds the essential maven repositories to download Minecraft and libraries from automatically.
|
|
||||||
// See https://docs.gradle.org/current/userguide/declaring_repositories.html
|
|
||||||
// for more information about repositories.
|
|
||||||
maven { url 'https://maven.nucleoid.xyz' }
|
|
||||||
|
|
||||||
exclusiveContent {
|
|
||||||
forRepository {
|
|
||||||
maven {
|
|
||||||
name = "Modrinth"
|
|
||||||
url = "https://api.modrinth.com/maven"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
filter {
|
|
||||||
includeGroup "maven.modrinth"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
dependencies {
|
|
||||||
// To change the versions see the gradle.properties file
|
|
||||||
minecraft "com.mojang:minecraft:${project.minecraft_version}"
|
|
||||||
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
|
|
||||||
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"
|
|
||||||
|
|
||||||
// Fabric API. This is technically optional, but you probably want it anyway.
|
|
||||||
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
|
|
||||||
modImplementation "eu.pb4:polymer-core:${project.polymer_version}"
|
|
||||||
modImplementation include("eu.pb4:sgui:${project.sgui_version}")
|
|
||||||
modImplementation include("xyz.nucleoid:server-translations-api:${project.server_translations_version}")
|
|
||||||
|
|
||||||
modCompileOnly "maven.modrinth:universal-graves:${project.universal_graves_version}"
|
|
||||||
}
|
|
||||||
|
|
||||||
processResources {
|
|
||||||
inputs.property "version", project.version
|
|
||||||
inputs.property "minecraft_version", project.minecraft_version
|
|
||||||
inputs.property "loader_version", project.loader_version
|
|
||||||
filteringCharset "UTF-8"
|
|
||||||
|
|
||||||
filesMatching("fabric.mod.json") {
|
|
||||||
expand "version": project.version,
|
|
||||||
"minecraft_version": project.minecraft_version,
|
|
||||||
"loader_version": project.loader_version
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
def targetJavaVersion = 21
|
|
||||||
tasks.withType(JavaCompile).configureEach {
|
|
||||||
// ensure that the encoding is set to UTF-8, no matter what the system default is
|
|
||||||
// this fixes some edge cases with special characters not displaying correctly
|
|
||||||
// see http://yodaconditions.net/blog/fix-for-java-file-encoding-problems-with-gradle.html
|
|
||||||
// If Javadoc is generated, this must be specified in that task too.
|
|
||||||
it.options.encoding = "UTF-8"
|
|
||||||
if (targetJavaVersion >= 10 || JavaVersion.current().isJava10Compatible()) {
|
|
||||||
it.options.release.set(targetJavaVersion)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
java {
|
|
||||||
def javaVersion = JavaVersion.toVersion(targetJavaVersion)
|
|
||||||
if (JavaVersion.current() < javaVersion) {
|
|
||||||
toolchain.languageVersion = JavaLanguageVersion.of(targetJavaVersion)
|
|
||||||
}
|
|
||||||
// Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task
|
|
||||||
// if it is present.
|
|
||||||
// If you remove this line, sources will not be generated.
|
|
||||||
withSourcesJar()
|
|
||||||
}
|
|
||||||
|
|
||||||
jar {
|
|
||||||
from("LICENSE") {
|
|
||||||
rename { "${it}_${project.archivesBaseName}"}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// configure the maven publication
|
|
||||||
publishing {
|
|
||||||
publications {
|
|
||||||
minions (MavenPublication) {
|
|
||||||
from components.java
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// See https://docs.gradle.org/current/userguide/publishing_maven.html for information on how to set up publishing.
|
|
||||||
repositories {
|
|
||||||
// Add repositories to publish to here.
|
|
||||||
// Notice: This block does NOT have the same function as the block in the top level.
|
|
||||||
// The repositories here will be used for publishing your artifact, not for
|
|
||||||
// retrieving dependencies.
|
|
||||||
maven {
|
|
||||||
def release = "https://maven.foxgalaxy.de/private-releases"
|
|
||||||
def snapshot = "https://maven.foxgalaxy.de/private-snapshot"
|
|
||||||
url = version.endsWith('SNAPSHOT') ? snapshot : release
|
|
||||||
|
|
||||||
credentials {
|
|
||||||
username = "${maven_user}"
|
|
||||||
password = "${maven_password}"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,128 @@
|
|||||||
|
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
|
||||||
|
|
||||||
|
plugins {
|
||||||
|
id("net.fabricmc.fabric-loom")
|
||||||
|
`maven-publish`
|
||||||
|
id("org.jetbrains.kotlin.jvm") version "2.3.21"
|
||||||
|
}
|
||||||
|
|
||||||
|
version = providers.gradleProperty("mod_version").get()
|
||||||
|
group = providers.gradleProperty("maven_group").get()
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
// Add repositories to retrieve artifacts from in here.
|
||||||
|
// You should only use this when depending on other mods because
|
||||||
|
// Loom adds the essential maven repositories to download Minecraft and libraries from automatically.
|
||||||
|
// See https://docs.gradle.org/current/userguide/declaring_repositories.html
|
||||||
|
// for more information about repositories.
|
||||||
|
|
||||||
|
maven("https://maven.nucleoid.xyz")
|
||||||
|
|
||||||
|
exclusiveContent {
|
||||||
|
forRepository {
|
||||||
|
maven ("https://api.modrinth.com/maven")
|
||||||
|
}
|
||||||
|
filter {
|
||||||
|
includeGroup("maven.modrinth")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
loom {
|
||||||
|
splitEnvironmentSourceSets()
|
||||||
|
|
||||||
|
mods {
|
||||||
|
register("minions") {
|
||||||
|
sourceSet(sourceSets.main.get())
|
||||||
|
sourceSet(sourceSets.getByName("client"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (settings in runs) {
|
||||||
|
settings.vmArg("-XX:+AllowEnhancedClassRedefinition")
|
||||||
|
}
|
||||||
|
|
||||||
|
accessWidenerPath = file("src/main/resources/minions.classtweaker")
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
// To change the versions see the gradle.properties file
|
||||||
|
minecraft("com.mojang:minecraft:${providers.gradleProperty("minecraft_version").get()}")
|
||||||
|
|
||||||
|
implementation("net.fabricmc:fabric-loader:${providers.gradleProperty("loader_version").get()}")
|
||||||
|
|
||||||
|
// Fabric API. This is technically optional, but you probably want it anyway.
|
||||||
|
implementation("net.fabricmc.fabric-api:fabric-api:${providers.gradleProperty("fabric_api_version").get()}")
|
||||||
|
implementation("net.fabricmc:fabric-language-kotlin:${providers.gradleProperty("fabric_kotlin_version").get()}")
|
||||||
|
|
||||||
|
val polymer_version = providers.gradleProperty("polymer_version").get()
|
||||||
|
implementation("eu.pb4:polymer-core:${polymer_version}")
|
||||||
|
implementation("eu.pb4:polymer-virtual-entity:${polymer_version}")
|
||||||
|
implementation("eu.pb4:polymer-resource-pack:${polymer_version}")
|
||||||
|
localRuntime("eu.pb4:polymer-autohost:${polymer_version}")
|
||||||
|
implementation("eu.pb4:sgui:${providers.gradleProperty("sgui_version").get()}")
|
||||||
|
implementation("xyz.nucleoid:server-translations-api:${providers.gradleProperty("server_translations_version").get()}")
|
||||||
|
|
||||||
|
implementation("com.electronwill.night-config:toml:${providers.gradleProperty("night_config_version").get()}")
|
||||||
|
|
||||||
|
compileOnly("maven.modrinth:universal-graves:${providers.gradleProperty("universal_graves_version").get()}")
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.processResources {
|
||||||
|
val version = version
|
||||||
|
inputs.property("version", version)
|
||||||
|
|
||||||
|
filesMatching("fabric.mod.json") {
|
||||||
|
expand("version" to version)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.withType<JavaCompile>().configureEach {
|
||||||
|
options.release = 25
|
||||||
|
}
|
||||||
|
|
||||||
|
kotlin {
|
||||||
|
compilerOptions {
|
||||||
|
jvmTarget = JvmTarget.JVM_25
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
java {
|
||||||
|
// Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task
|
||||||
|
// if it is present.
|
||||||
|
// If you remove this line, sources will not be generated.
|
||||||
|
withSourcesJar()
|
||||||
|
|
||||||
|
sourceCompatibility = JavaVersion.VERSION_25
|
||||||
|
targetCompatibility = JavaVersion.VERSION_25
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.jar {
|
||||||
|
val projectName = project.name
|
||||||
|
inputs.property("projectName", projectName)
|
||||||
|
|
||||||
|
from("LICENSE") {
|
||||||
|
rename { "${it}_$projectName" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// configure the maven publication
|
||||||
|
publishing {
|
||||||
|
publications {
|
||||||
|
register<MavenPublication>("mavenJava") {
|
||||||
|
from(components["java"])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// See https://docs.gradle.org/current/userguide/publishing_maven.html for information on how to set up publishing.
|
||||||
|
repositories {
|
||||||
|
// Add repositories to publish to here.
|
||||||
|
// Notice: This block does NOT have the same function as the block in the top level.
|
||||||
|
// The repositories here will be used for publishing your artifact, not for
|
||||||
|
// retrieving dependencies.
|
||||||
|
maven("https://maven.foxgalaxy.de/private") {
|
||||||
|
name = "foxgalaxy"
|
||||||
|
credentials(PasswordCredentials::class.java)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+12
-9
@@ -3,21 +3,24 @@ org.gradle.jvmargs=-Xmx1G
|
|||||||
|
|
||||||
# Fabric Properties
|
# Fabric Properties
|
||||||
# check these on https://modmuss50.me/fabric.html
|
# check these on https://modmuss50.me/fabric.html
|
||||||
minecraft_version=1.21.4
|
loom_version=1.16-SNAPSHOT
|
||||||
loader_version=0.16.13
|
minecraft_version=26.1.2
|
||||||
yarn_mappings=1.21.4+build.8
|
loader_version=0.19.2
|
||||||
|
|
||||||
# Mod Properties
|
# Mod Properties
|
||||||
mod_version = 0.1.2-SNAPSHOT
|
mod_version = 1.0.0-TEST-1
|
||||||
maven_group = io.github.skippyall
|
maven_group = io.github.skippyall
|
||||||
archives_base_name = Minions
|
archives_base_name = Minions
|
||||||
|
|
||||||
# Dependencies
|
# Dependencies
|
||||||
# check this on https://modmuss50.me/fabric.html
|
# check this on https://modmuss50.me/fabric.html
|
||||||
fabric_version=0.115.1+1.21.4
|
fabric_api_version=0.147.0+26.1.2
|
||||||
|
fabric_kotlin_version=1.13.11+kotlin.2.3.21
|
||||||
|
|
||||||
polymer_version=0.11.5+1.21.4
|
polymer_version=0.16.3+26.1.2
|
||||||
sgui_version=1.8.2+1.21.4
|
sgui_version=2.0.0+26.1
|
||||||
server_translations_version=2.4.0+1.21.2-rc1
|
server_translations_version=3.0.3+26.1
|
||||||
|
|
||||||
universal_graves_version=3.5.0+1.21.2
|
night_config_version=3.8.3
|
||||||
|
|
||||||
|
universal_graves_version=3.11.0+26.1.2
|
||||||
|
|||||||
Vendored
BIN
Binary file not shown.
+7
-1
@@ -1 +1,7 @@
|
|||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip
|
distributionBase=GRADLE_USER_HOME
|
||||||
|
distributionPath=wrapper/dists
|
||||||
|
distributionUrl=https\://services.gradle.org/distributions/gradle-9.4.1-bin.zip
|
||||||
|
networkTimeout=10000
|
||||||
|
validateDistributionUrl=true
|
||||||
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
|
zipStorePath=wrapper/dists
|
||||||
|
|||||||
@@ -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
@@ -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
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
bash gradlew publish -PfoxgalaxyPassword=$(read -s -p "Enter Password: ")
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
pluginManagement {
|
|
||||||
repositories {
|
|
||||||
maven {
|
|
||||||
name = 'Fabric'
|
|
||||||
url = 'https://maven.fabricmc.net/'
|
|
||||||
}
|
|
||||||
gradlePluginPortal()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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,70 +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 eu.pb4.polymer.core.api.item.SimplePolymerItem;
|
import io.github.skippyall.minions.command.MinionsCommand;
|
||||||
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
|
import io.github.skippyall.minions.docs.DocsManager;
|
||||||
import io.github.skippyall.minions.minion.MinionData;
|
|
||||||
import io.github.skippyall.minions.minion.MinionItem;
|
|
||||||
import io.github.skippyall.minions.minion.MinionPersistentState;
|
import io.github.skippyall.minions.minion.MinionPersistentState;
|
||||||
import io.github.skippyall.minions.module.Modules;
|
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
|
||||||
|
import io.github.skippyall.minions.polymer.VersionSync;
|
||||||
|
import io.github.skippyall.minions.registration.MinionRegistration;
|
||||||
import net.fabricmc.api.ModInitializer;
|
import net.fabricmc.api.ModInitializer;
|
||||||
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
|
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
|
||||||
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
|
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
|
||||||
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
|
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
|
||||||
import net.minecraft.component.DataComponentTypes;
|
import net.minecraft.server.packs.PackType;
|
||||||
import net.minecraft.component.type.DamageResistantComponent;
|
|
||||||
import net.minecraft.entity.boss.WitherEntity;
|
|
||||||
import net.minecraft.entity.damage.DamageType;
|
|
||||||
import net.minecraft.item.Item;
|
|
||||||
import net.minecraft.item.Items;
|
|
||||||
import net.minecraft.registry.Registries;
|
|
||||||
import net.minecraft.registry.Registry;
|
|
||||||
import net.minecraft.registry.RegistryKey;
|
|
||||||
import net.minecraft.registry.RegistryKeys;
|
|
||||||
import net.minecraft.registry.tag.TagKey;
|
|
||||||
import net.minecraft.server.world.ChunkTicketManager;
|
|
||||||
import net.minecraft.util.Identifier;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.function.Function;
|
|
||||||
|
|
||||||
public class Minions implements ModInitializer {
|
public class Minions implements ModInitializer {
|
||||||
public static final String MOD_ID = "minions";
|
public static final String MOD_ID = "minions";
|
||||||
public static final TagKey<DamageType> MINION_ITEM_RESISTS = TagKey.of(RegistryKeys.DAMAGE_TYPE, Identifier.of(MOD_ID, "minion_item_resists"));
|
|
||||||
public static final MinionItem MINION_ITEM = registerItem(Identifier.of(MOD_ID, "minion"), settings -> new MinionItem(settings.component(DataComponentTypes.DAMAGE_RESISTANT, new DamageResistantComponent(MINION_ITEM_RESISTS))));
|
|
||||||
public static final SimplePolymerItem BASIC_UPGRADE_BASE = registerItem(Identifier.of(MOD_ID, "basic_upgrade_base"), settings -> new SimplePolymerItem(settings, Items.NETHERITE_UPGRADE_SMITHING_TEMPLATE));
|
|
||||||
public static final SimplePolymerItem ADVANCED_UPGRADE_BASE = registerItem(Identifier.of(MOD_ID, "advanced_upgrade_base"), settings -> new SimplePolymerItem(settings, Items.NETHERITE_UPGRADE_SMITHING_TEMPLATE));
|
|
||||||
|
|
||||||
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
|
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onInitialize() {
|
public void onInitialize() {
|
||||||
MinionData.register();
|
MinionsConfig.get();
|
||||||
PolymerEntityUtils.registerType();
|
|
||||||
|
MinionRegistration.register();
|
||||||
|
|
||||||
|
VersionSync.register();
|
||||||
|
|
||||||
ServerLifecycleEvents.SERVER_STARTED.register(server -> {
|
ServerLifecycleEvents.SERVER_STARTED.register(server -> {
|
||||||
MinionPersistentState.create(server);
|
MinionPersistentState.get(server).getMinionData().forEach((uuid, data) -> {
|
||||||
MinionPersistentState.INSTANCE.getMinionData().forEach((uuid, data) -> {
|
|
||||||
if(data.isSpawned()) {
|
if(data.isSpawned()) {
|
||||||
MinionFakePlayer.spawnMinion(data, server.getOverworld(), null, null);
|
MinionFakePlayer.spawnMinion(data, server.overworld(), null, null, true);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
CommandRegistrationCallback.EVENT.register((commandDispatcher, commandRegistryAccess, registrationEnvironment) -> {
|
CommandRegistrationCallback.EVENT.register(MinionsCommand::register);
|
||||||
MobCapCommand.registerCommand(commandDispatcher);
|
|
||||||
});
|
|
||||||
|
|
||||||
Modules.register();
|
PolymerResourcePackUtils.addModAssets(Minions.MOD_ID);
|
||||||
}
|
|
||||||
|
|
||||||
private static <T extends Item> T registerItem(Identifier identifier, Function<Item.Settings, T> constructor, Item.Settings settings) {
|
ResourceManagerHelper.get(PackType.SERVER_DATA).registerReloadListener(new DocsManager());
|
||||||
return Registry.register(Registries.ITEM, identifier, constructor.apply(settings.registryKey(RegistryKey.of(RegistryKeys.ITEM, identifier))));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static <T extends Item> T registerItem(Identifier identifier, Function<Item.Settings, T> constructor) {
|
|
||||||
return registerItem(identifier, constructor, new Item.Settings());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,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();
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
}
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
package io.github.skippyall.minions;
|
|
||||||
|
|
||||||
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class MinionsTickExecutor {
|
|
||||||
private static final List<Runnable> executeOnNextTick = Collections.synchronizedList(new ArrayList<>());
|
|
||||||
|
|
||||||
public static void register() {
|
|
||||||
ServerTickEvents.START_SERVER_TICK.register(server -> {
|
|
||||||
synchronized (executeOnNextTick) {
|
|
||||||
for (Runnable run:executeOnNextTick) {
|
|
||||||
run.run();
|
|
||||||
}
|
|
||||||
executeOnNextTick.clear();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void addExecuteOnNextTick(Runnable run) {
|
|
||||||
executeOnNextTick.add(run);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
package io.github.skippyall.minions;
|
|
||||||
|
|
||||||
import com.mojang.brigadier.CommandDispatcher;
|
|
||||||
import io.github.skippyall.minions.mixinhelper.ChunkTicketManager$DistanceFromNearestPlayerTrackerAccessor;
|
|
||||||
import io.github.skippyall.minions.mixinhelper.ChunkTicketManagerAccessor;
|
|
||||||
import io.github.skippyall.minions.mixins.antimobcap.ServerChunkManagerAccessor;
|
|
||||||
import net.minecraft.server.command.ServerCommandSource;
|
|
||||||
import net.minecraft.server.world.ChunkTicketManager;
|
|
||||||
import net.minecraft.text.Text;
|
|
||||||
|
|
||||||
import static net.minecraft.server.command.CommandManager.literal;
|
|
||||||
|
|
||||||
public class MobCapCommand {
|
|
||||||
public static void registerCommand(CommandDispatcher<ServerCommandSource> dispatcher) {
|
|
||||||
dispatcher.register(literal("mobcapdebug")
|
|
||||||
.executes(context -> {
|
|
||||||
ChunkTicketManager ticketManager = ((ServerChunkManagerAccessor)context.getSource().getWorld().getChunkManager()).getTicketManager();
|
|
||||||
int tickedChunkCount = ((ChunkTicketManager$DistanceFromNearestPlayerTrackerAccessor)((ChunkTicketManagerAccessor)ticketManager).getMinionless()).minions$getTickedChunkCount();
|
|
||||||
context.getSource().sendFeedback(() -> Text.of(String.valueOf(tickedChunkCount)), false);
|
|
||||||
return 0;
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package io.github.skippyall.minions.block.input;
|
||||||
|
|
||||||
|
import io.github.skippyall.minions.clipboard.ClipboardItem;
|
||||||
|
import net.minecraft.core.BlockPos;
|
||||||
|
import net.minecraft.world.InteractionResult;
|
||||||
|
import net.minecraft.world.entity.player.Player;
|
||||||
|
import net.minecraft.world.level.Level;
|
||||||
|
import net.minecraft.world.level.block.Block;
|
||||||
|
import net.minecraft.world.level.block.state.BlockState;
|
||||||
|
import net.minecraft.world.phys.BlockHitResult;
|
||||||
|
|
||||||
|
public class AnalogInputBlock extends Block {
|
||||||
|
public AnalogInputBlock(Properties settings) {
|
||||||
|
super(settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected InteractionResult useWithoutItem(BlockState state, Level world, BlockPos pos, Player player, BlockHitResult hit) {
|
||||||
|
if(!world.isClientSide()) {
|
||||||
|
player.getInventory().placeItemBackInInventory(ClipboardItem.createBlockPosReference(world, pos), true);
|
||||||
|
}
|
||||||
|
return InteractionResult.SUCCESS;
|
||||||
|
}
|
||||||
|
}
|
||||||
+65
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
+78
@@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
+137
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
+66
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
+26
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+114
@@ -0,0 +1,114 @@
|
|||||||
|
package io.github.skippyall.minions.block.miniontrigger;
|
||||||
|
|
||||||
|
import com.mojang.serialization.Codec;
|
||||||
|
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
||||||
|
import io.github.skippyall.minions.Minions;
|
||||||
|
import io.github.skippyall.minions.listener.BlockEntityMinionInstructionListener;
|
||||||
|
import io.github.skippyall.minions.minion.MinionRuntime;
|
||||||
|
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
|
||||||
|
import io.github.skippyall.minions.program.instruction.ConfiguredInstruction;
|
||||||
|
import io.github.skippyall.minions.program.instruction.ConfiguredInstructionListener;
|
||||||
|
import io.github.skippyall.minions.registration.MinionBlocks;
|
||||||
|
import net.minecraft.core.BlockPos;
|
||||||
|
import net.minecraft.core.UUIDUtil;
|
||||||
|
import net.minecraft.resources.Identifier;
|
||||||
|
import net.minecraft.resources.ResourceKey;
|
||||||
|
import net.minecraft.server.MinecraftServer;
|
||||||
|
import net.minecraft.world.level.Level;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class MinionTriggerMinionListener extends BlockEntityMinionInstructionListener<MinionTriggerBlockEntity> {
|
||||||
|
public static final Codec<MinionTriggerMinionListener> CODEC = RecordCodecBuilder.create(instance ->
|
||||||
|
instance.group(
|
||||||
|
Level.RESOURCE_KEY_CODEC.fieldOf("world").forGetter(listener -> listener.worldKey),
|
||||||
|
BlockPos.CODEC.fieldOf("pos").forGetter(listener -> listener.pos),
|
||||||
|
UUIDUtil.AUTHLIB_CODEC.fieldOf("minionUuid").forGetter(listener -> listener.minionUuid),
|
||||||
|
Codec.STRING.fieldOf("instructionName").forGetter(listener -> listener.instructionName)
|
||||||
|
).apply(instance, MinionTriggerMinionListener::new));
|
||||||
|
|
||||||
|
String instructionName;
|
||||||
|
final TriggerInstructionListener listener = new TriggerInstructionListener();
|
||||||
|
|
||||||
|
boolean runningCache;
|
||||||
|
boolean incomingPowerCache;
|
||||||
|
|
||||||
|
MinionTriggerMinionListener(ResourceKey<Level> worldKey, BlockPos pos, UUID minionUuid, String instructionName) {
|
||||||
|
super(worldKey, pos, minionUuid, MinionBlocks.MINION_TRIGGER_BE_TYPE);
|
||||||
|
this.instructionName = Objects.requireNonNull(instructionName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Map<String, ConfiguredInstructionListener> getInstructionListeners() {
|
||||||
|
return Map.of(instructionName, listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onMinionSpawn(MinionFakePlayer minion) {
|
||||||
|
super.onMinionSpawn(minion);
|
||||||
|
runningCache = minion.getInstructionManager().getInstruction(instructionName).isRunning();
|
||||||
|
updateComparatorsIfLoaded(minion.getServer());
|
||||||
|
|
||||||
|
ConfiguredInstruction<MinionRuntime> instruction = minion.getInstructionManager().getInstruction(instructionName);
|
||||||
|
if(instruction.isRunning() && !incomingPowerCache) {
|
||||||
|
instruction.stop(minion.getInstructionManager());
|
||||||
|
} else if (!instruction.isRunning() && incomingPowerCache) {
|
||||||
|
instruction.run(minion.getInstructionManager());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onMinionRemove(MinionFakePlayer minion) {
|
||||||
|
super.onMinionRemove(minion);
|
||||||
|
runningCache = false;
|
||||||
|
updateComparatorsIfLoaded(minion.getServer());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onInstructionRename(MinionFakePlayer minion, ConfiguredInstruction<?> instruction, String oldName, String newName) {
|
||||||
|
super.onInstructionRename(minion, instruction, oldName, newName);
|
||||||
|
if(instructionName.equals(oldName)) {
|
||||||
|
instructionName = newName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void add(MinecraftServer server) {
|
||||||
|
super.add(server);
|
||||||
|
runningCache = minion.getInstructionManager().getInstruction(instructionName).isRunning();
|
||||||
|
updateComparatorsIfLoaded(server);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<Identifier> getCodecId() {
|
||||||
|
return Optional.of(Identifier.fromNamespaceAndPath(Minions.MOD_ID, "minion_trigger"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateComparatorsIfLoaded(MinecraftServer server) {
|
||||||
|
Level world = server.getLevel(worldKey);
|
||||||
|
if(world.isLoaded(pos)) {
|
||||||
|
world.updateNeighbourForOutputSignal(pos, MinionBlocks.MINION_TRIGGER_BLOCK);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isRunning() {
|
||||||
|
return runningCache;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class TriggerInstructionListener implements ConfiguredInstructionListener {
|
||||||
|
@Override
|
||||||
|
public void onRun(ConfiguredInstruction<?> instruction) {
|
||||||
|
runningCache = true;
|
||||||
|
updateComparatorsIfLoaded(minion.getServer());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onStop(ConfiguredInstruction<?> instruction) {
|
||||||
|
runningCache = false;
|
||||||
|
updateComparatorsIfLoaded(minion.getServer());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,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,52 +0,0 @@
|
|||||||
package io.github.skippyall.minions.gui;
|
|
||||||
|
|
||||||
import eu.pb4.sgui.api.elements.GuiElementBuilder;
|
|
||||||
import eu.pb4.sgui.api.gui.SimpleGui;
|
|
||||||
import io.github.skippyall.minions.module.command.Command;
|
|
||||||
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
|
|
||||||
import io.github.skippyall.minions.module.ModuleItem;
|
|
||||||
import net.minecraft.screen.ScreenHandlerType;
|
|
||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
|
||||||
import net.minecraft.text.Text;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class CommandsGui {
|
|
||||||
public static void openServerModuleCommandGui(ServerPlayerEntity player, MinionFakePlayer minion) {
|
|
||||||
List<ModuleItem> modules = minion.getModuleInventory().getModuleItems();
|
|
||||||
|
|
||||||
SimpleGui gui = new SimpleGui(ScreenHandlerType.GENERIC_9X3, player, false);
|
|
||||||
|
|
||||||
gui.setTitle(Text.translatable("minions.gui.module_commands.title"));
|
|
||||||
|
|
||||||
for (int i = 0; i < modules.size(); i++) {
|
|
||||||
ModuleItem module = modules.get(i);
|
|
||||||
gui.setSlot(i, new GuiElementBuilder()
|
|
||||||
.setItem(module.asItem())
|
|
||||||
.setCallback(() -> openServerCommandGui(player, minion, module))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
gui.open();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void openServerCommandGui(ServerPlayerEntity player, MinionFakePlayer minion, ModuleItem module) {
|
|
||||||
List<Command> commands = module.getCommands();
|
|
||||||
|
|
||||||
SimpleGui commandGui = new SimpleGui(ScreenHandlerType.GENERIC_9X3, player, false);
|
|
||||||
|
|
||||||
commandGui.setTitle(Text.translatable("minions.gui.commands.title", module.asItem().getName()));
|
|
||||||
|
|
||||||
for(int j = 0; j < commands.size(); j++) {
|
|
||||||
Command command = commands.get(j);
|
|
||||||
commandGui.setSlot(j, new GuiElementBuilder()
|
|
||||||
.setItem(command.getItemRepresentation())
|
|
||||||
.setName(command.getName())
|
|
||||||
.addLoreLine(command.getDescription())
|
|
||||||
.setCallback(() -> command.execute(player, minion))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
commandGui.open();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package io.github.skippyall.minions.gui;
|
||||||
|
|
||||||
|
public interface Displayable {
|
||||||
|
GuiDisplay getDisplay();
|
||||||
|
}
|
||||||
@@ -0,0 +1,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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,77 +0,0 @@
|
|||||||
package io.github.skippyall.minions.gui;
|
|
||||||
|
|
||||||
import eu.pb4.sgui.api.elements.GuiElementBuilder;
|
|
||||||
import eu.pb4.sgui.api.gui.SimpleGui;
|
|
||||||
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
|
|
||||||
import net.minecraft.item.Items;
|
|
||||||
import net.minecraft.screen.ScreenHandlerType;
|
|
||||||
import net.minecraft.screen.slot.Slot;
|
|
||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
|
||||||
import net.minecraft.text.Text;
|
|
||||||
|
|
||||||
public class MinionGui {
|
|
||||||
public static void openInventory(ServerPlayerEntity player, MinionFakePlayer minion) {
|
|
||||||
openServerSideInventory(player, minion);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void openServerSideInventory(ServerPlayerEntity player, MinionFakePlayer minion) {
|
|
||||||
SimpleGui gui = new SimpleGui(ScreenHandlerType.GENERIC_3X3, player, false);
|
|
||||||
gui.setTitle(minion.getName());
|
|
||||||
|
|
||||||
gui.setSlot(1, new GuiElementBuilder()
|
|
||||||
.setItem(Items.COMMAND_BLOCK)
|
|
||||||
.setName(Text.translatable("minions.gui.main.commands"))
|
|
||||||
.setCallback((i, clickType, slotActionType) -> {
|
|
||||||
openCommandsGui(player, minion);
|
|
||||||
})
|
|
||||||
);
|
|
||||||
if(minion.isProgrammable()) {
|
|
||||||
gui.setSlot(4, new GuiElementBuilder()
|
|
||||||
.setItem(Items.REDSTONE)
|
|
||||||
.setName(Text.translatable("minions.gui.main.programming"))
|
|
||||||
.setCallback(() -> {
|
|
||||||
openProgrammingInventory(player, minion);
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
gui.setSlot(3, new GuiElementBuilder()
|
|
||||||
.setItem(Items.NETHERITE_UPGRADE_SMITHING_TEMPLATE)
|
|
||||||
.setName(Text.translatable("minions.gui.main.modules"))
|
|
||||||
.setCallback(() -> {
|
|
||||||
ModuleInventory.openModuleInventory(player, minion);
|
|
||||||
})
|
|
||||||
);
|
|
||||||
gui.setSlot(5, new GuiElementBuilder()
|
|
||||||
.setItem(Items.CHEST)
|
|
||||||
.setName(Text.translatable("minions.gui.main.inventory"))
|
|
||||||
.setCallback(() -> {
|
|
||||||
openMinionInventory(player, minion);
|
|
||||||
})
|
|
||||||
);
|
|
||||||
gui.setSlot(7, new GuiElementBuilder()
|
|
||||||
.setItem(Items.BARRIER)
|
|
||||||
.setName(Text.translatable("minions.gui.main.pickup"))
|
|
||||||
.setCallback(() -> {
|
|
||||||
minion.kill(minion.getServerWorld());
|
|
||||||
})
|
|
||||||
);
|
|
||||||
gui.open();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void openCommandsGui(ServerPlayerEntity player, MinionFakePlayer minion) {
|
|
||||||
CommandsGui.openServerModuleCommandGui(player, minion);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void openProgrammingInventory(ServerPlayerEntity player, MinionFakePlayer minion) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void openMinionInventory(ServerPlayerEntity player, MinionFakePlayer minion) {
|
|
||||||
SimpleGui gui = new SimpleGui(ScreenHandlerType.GENERIC_9X5, player, false);
|
|
||||||
gui.setTitle(Text.translatable("minions.gui.inventory.title", minion.getName()));
|
|
||||||
for (int i = 0; i < minion.getInventory().size(); i++) {
|
|
||||||
gui.setSlotRedirect(i, new Slot(minion.getInventory(), i, 0, 0));
|
|
||||||
}
|
|
||||||
gui.open();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,95 +0,0 @@
|
|||||||
package io.github.skippyall.minions.gui;
|
|
||||||
|
|
||||||
import eu.pb4.sgui.api.elements.GuiElementBuilder;
|
|
||||||
import eu.pb4.sgui.api.gui.SimpleGui;
|
|
||||||
import io.github.skippyall.minions.input.TextInput;
|
|
||||||
import io.github.skippyall.minions.minion.MinionData;
|
|
||||||
import io.github.skippyall.minions.minion.MinionItem;
|
|
||||||
import io.github.skippyall.minions.minion.MinionProfileUtils;
|
|
||||||
import io.github.skippyall.minions.minion.skin.SkinProvider;
|
|
||||||
import io.github.skippyall.minions.minion.skin.SkinProviders;
|
|
||||||
import net.minecraft.component.DataComponentTypes;
|
|
||||||
import net.minecraft.component.type.ProfileComponent;
|
|
||||||
import net.minecraft.item.ItemStack;
|
|
||||||
import net.minecraft.item.Items;
|
|
||||||
import net.minecraft.screen.ScreenHandlerType;
|
|
||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
|
||||||
import net.minecraft.text.Text;
|
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
public class MinionLookGui extends SimpleGui {
|
|
||||||
private ItemStack minionItem;
|
|
||||||
private SkinProvider currentSkinProvider;
|
|
||||||
|
|
||||||
public MinionLookGui(ServerPlayerEntity player, ItemStack minionItem) {
|
|
||||||
super(ScreenHandlerType.GENERIC_9X3, player, false);
|
|
||||||
this.minionItem = minionItem;
|
|
||||||
this.currentSkinProvider = SkinProviders.NAME;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void update() {
|
|
||||||
updateName();
|
|
||||||
updateSkin();
|
|
||||||
updateSkinProvider();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void updateName() {
|
|
||||||
setSlot(10, new GuiElementBuilder()
|
|
||||||
.setItem(Items.OAK_SIGN)
|
|
||||||
.setName(Text.literal(getData().name()))
|
|
||||||
.setCallback(() -> {
|
|
||||||
openRenameGui(player, minionItem);
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void updateSkin() {
|
|
||||||
GuiElementBuilder builder = new GuiElementBuilder()
|
|
||||||
.setItem(Items.PLAYER_HEAD)
|
|
||||||
.setCallback(() -> currentSkinProvider.openSkinMenu(player).thenAccept(skin -> {
|
|
||||||
MinionItem.setData(getData().withSkin(skin), minionItem);
|
|
||||||
}));
|
|
||||||
if(MinionItem.getData(minionItem) != null && MinionItem.getData(minionItem).skin().isPresent()) {
|
|
||||||
builder.setComponent(DataComponentTypes.PROFILE, new ProfileComponent(Optional.empty(), Optional.empty(), getData().skin().get()));
|
|
||||||
}
|
|
||||||
setSlot(16, builder);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void cycleSkinProvider() {
|
|
||||||
int currentId = SkinProviders.SKIN_PROVIDERS.getRawId(currentSkinProvider);
|
|
||||||
currentId++;
|
|
||||||
if(SkinProviders.SKIN_PROVIDERS.size() == currentId) {
|
|
||||||
currentId = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
currentSkinProvider = SkinProviders.SKIN_PROVIDERS.get(currentId);
|
|
||||||
updateSkinProvider();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void updateSkinProvider() {
|
|
||||||
setSlot(25, new GuiElementBuilder()
|
|
||||||
.setItem(Items.GREEN_STAINED_GLASS_PANE)
|
|
||||||
.setComponent(DataComponentTypes.CUSTOM_NAME, currentSkinProvider.getDisplayName())
|
|
||||||
.setCallback(this::cycleSkinProvider)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private MinionData getData() {
|
|
||||||
return MinionItem.getDataOrDefault(minionItem);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void open(ServerPlayerEntity player, ItemStack minionItem) {
|
|
||||||
MinionLookGui gui = new MinionLookGui(player, minionItem);
|
|
||||||
gui.update();
|
|
||||||
gui.open();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void openRenameGui(ServerPlayerEntity player, ItemStack minionItem) {
|
|
||||||
TextInput.inputSync(player, Text.translatable("minions.gui.look.rename.title"), "Minion", MinionProfileUtils::checkMinionNameWithoutPrefix)
|
|
||||||
.thenAccept(name -> {
|
|
||||||
MinionItem.setData(getData().withName(MinionProfileUtils.PREFIX + name), minionItem);
|
|
||||||
open();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,136 @@
|
|||||||
|
package io.github.skippyall.minions.gui
|
||||||
|
|
||||||
|
import com.mojang.authlib.GameProfile
|
||||||
|
import eu.pb4.sgui.api.elements.GuiElementBuilder
|
||||||
|
import eu.pb4.sgui.api.gui.SimpleGui
|
||||||
|
import io.github.skippyall.minions.gui.input.TextInput
|
||||||
|
import io.github.skippyall.minions.minion.MinionData
|
||||||
|
import io.github.skippyall.minions.minion.MinionItem
|
||||||
|
import io.github.skippyall.minions.minion.MinionProfileUtils
|
||||||
|
import io.github.skippyall.minions.minion.skin.SkinProvider
|
||||||
|
import io.github.skippyall.minions.registration.MinionRegistries
|
||||||
|
import io.github.skippyall.minions.registration.SkinProviders
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import net.fabricmc.fabric.api.entity.FakePlayer
|
||||||
|
import net.minecraft.core.component.DataComponents
|
||||||
|
import net.minecraft.network.chat.Component
|
||||||
|
import net.minecraft.server.level.ServerPlayer
|
||||||
|
import net.minecraft.world.inventory.MenuType
|
||||||
|
import net.minecraft.world.item.ItemStack
|
||||||
|
import net.minecraft.world.item.Items
|
||||||
|
import net.minecraft.world.item.component.ResolvableProfile
|
||||||
|
import java.util.Optional
|
||||||
|
import java.util.function.Consumer
|
||||||
|
import java.util.function.Function
|
||||||
|
|
||||||
|
class MinionLookGui(
|
||||||
|
viewer: ServerPlayer,
|
||||||
|
private val minionItem: ItemStack
|
||||||
|
) : MinionsGui(viewer) {
|
||||||
|
private lateinit var gui: SimpleGui
|
||||||
|
|
||||||
|
private var currentSkinProvider: SkinProvider = SkinProviders.NAME
|
||||||
|
|
||||||
|
private val data: MinionData
|
||||||
|
get() = MinionItem.getDataOrDefault(viewer.level().server, minionItem)
|
||||||
|
|
||||||
|
init {
|
||||||
|
open()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun open() {
|
||||||
|
gui = object : SimpleGui(MenuType.GENERIC_9x3, viewer, false) {
|
||||||
|
override fun onPlayerClose(success: Boolean) {
|
||||||
|
onBackingClosed()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
update()
|
||||||
|
gui.open()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun closeBacking() {
|
||||||
|
gui.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun update() {
|
||||||
|
updateName()
|
||||||
|
updateSkin()
|
||||||
|
updateSkinProvider()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun updateName() {
|
||||||
|
gui.setSlot(
|
||||||
|
10, GuiElementBuilder()
|
||||||
|
.setItem(Items.OAK_SIGN)
|
||||||
|
.setName(Component.literal(this.data.name))
|
||||||
|
.setCallback(this::openRenameGui)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun updateSkin() {
|
||||||
|
val builder = GuiElementBuilder()
|
||||||
|
.setItem(Items.PLAYER_HEAD)
|
||||||
|
.setCallback(this::openSkinGui)
|
||||||
|
|
||||||
|
if (MinionItem.getData(viewer.level().server, minionItem)?.skin?.isPresent ?: false) {
|
||||||
|
builder.setComponent(
|
||||||
|
DataComponents.PROFILE,
|
||||||
|
ResolvableProfile.createResolved(GameProfile(FakePlayer.DEFAULT_UUID, "", this.data.skin.get()))
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
gui.setSlot(16, builder)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun openSkinGui() {
|
||||||
|
currentSkinProvider.openSkinMenu(this)
|
||||||
|
.thenCompose(Function { profile: ResolvableProfile? ->
|
||||||
|
profile!!.resolveProfile(
|
||||||
|
viewer.level().server.services().profileResolver()
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.thenAccept(Consumer { skin: GameProfile? ->
|
||||||
|
MinionItem.setData(
|
||||||
|
viewer.level().server, this.data.withSkin(
|
||||||
|
Optional.of(skin!!.properties())
|
||||||
|
), minionItem
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun cycleSkinProvider() {
|
||||||
|
var currentId = MinionRegistries.SKIN_PROVIDERS.getId(currentSkinProvider)
|
||||||
|
currentId++
|
||||||
|
if (MinionRegistries.SKIN_PROVIDERS.size() == currentId) {
|
||||||
|
currentId = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
currentSkinProvider = MinionRegistries.SKIN_PROVIDERS.byId(currentId)!!
|
||||||
|
updateSkinProvider()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun updateSkinProvider() {
|
||||||
|
gui.setSlot(
|
||||||
|
25, GuiElementBuilder()
|
||||||
|
.setItem(Items.GREEN_STAINED_GLASS_PANE)
|
||||||
|
.setComponent(DataComponents.CUSTOM_NAME, currentSkinProvider.getDisplayName())
|
||||||
|
.setCallback(Runnable { this.cycleSkinProvider() })
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun openRenameGui() {
|
||||||
|
scope.launch {
|
||||||
|
val newName = TextInput.input(
|
||||||
|
this@MinionLookGui,
|
||||||
|
Component.translatable("minions.gui.look.rename.title"),
|
||||||
|
"Minion",
|
||||||
|
) { name ->
|
||||||
|
MinionProfileUtils.checkMinionNameWithoutPrefix(viewer.level().server, name)
|
||||||
|
}
|
||||||
|
|
||||||
|
if(newName != null) {
|
||||||
|
this@MinionLookGui.data.withName(newName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
package io.github.skippyall.minions.gui
|
||||||
|
|
||||||
|
import eu.pb4.sgui.api.elements.GuiElementBuilder
|
||||||
|
import kotlinx.coroutines.CompletableJob
|
||||||
|
import kotlinx.coroutines.CoroutineName
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.Job
|
||||||
|
import kotlinx.coroutines.cancel
|
||||||
|
import net.minecraft.network.chat.Component
|
||||||
|
import net.minecraft.server.level.ServerPlayer
|
||||||
|
import net.minecraft.world.item.Items
|
||||||
|
|
||||||
|
abstract class MinionsGui {
|
||||||
|
protected val parent: MinionsGui?
|
||||||
|
@JvmField
|
||||||
|
val viewer: ServerPlayer
|
||||||
|
protected var child: MinionsGui? = null
|
||||||
|
private var open = true
|
||||||
|
|
||||||
|
private val job: CompletableJob
|
||||||
|
val scope: CoroutineScope
|
||||||
|
|
||||||
|
constructor(parent: MinionsGui) {
|
||||||
|
this.viewer = parent.viewer
|
||||||
|
this.parent = parent
|
||||||
|
parent.child = this
|
||||||
|
job = Job(parent.job)
|
||||||
|
scope = CoroutineScope(Dispatchers.Unconfined.plus(CoroutineName("MinionsGui")).plus(job))
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor(viewer: ServerPlayer) {
|
||||||
|
this.viewer = viewer
|
||||||
|
this.parent = null
|
||||||
|
job = Job(null)
|
||||||
|
scope = CoroutineScope(Dispatchers.Unconfined.plus(CoroutineName("MinionsGui")).plus(job))
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract fun open()
|
||||||
|
|
||||||
|
protected fun reopen() {
|
||||||
|
open()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onBackingClosed() {
|
||||||
|
if (child != null && child!!.open) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
close(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmOverloads
|
||||||
|
fun close(alreadyClosed: Boolean = false) {
|
||||||
|
if (open) {
|
||||||
|
scope.cancel(null)
|
||||||
|
open = false
|
||||||
|
if (child != null) {
|
||||||
|
child!!.close(alreadyClosed)
|
||||||
|
} else if (!alreadyClosed) {
|
||||||
|
closeBacking()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun goBack() {
|
||||||
|
if (parent != null) {
|
||||||
|
open = false
|
||||||
|
parent.child = null
|
||||||
|
parent.reopen()
|
||||||
|
close(true)
|
||||||
|
} else {
|
||||||
|
close(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun backButton(): GuiElementBuilder? {
|
||||||
|
return GuiElementBuilder(Items.MANGROVE_DOOR)
|
||||||
|
.setName(Component.translatable("gui.back"))
|
||||||
|
.setCallback(Runnable { this.goBack() })
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract fun closeBacking()
|
||||||
|
}
|
||||||
@@ -1,84 +0,0 @@
|
|||||||
package io.github.skippyall.minions.gui;
|
|
||||||
|
|
||||||
import io.github.skippyall.minions.module.command.Command;
|
|
||||||
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
|
|
||||||
import io.github.skippyall.minions.module.ModuleItem;
|
|
||||||
import io.github.skippyall.minions.program.block.CodeBlock;
|
|
||||||
import net.minecraft.inventory.Inventories;
|
|
||||||
import net.minecraft.inventory.SimpleInventory;
|
|
||||||
import net.minecraft.item.ItemStack;
|
|
||||||
import net.minecraft.nbt.NbtCompound;
|
|
||||||
import net.minecraft.registry.RegistryWrapper;
|
|
||||||
import net.minecraft.screen.SimpleNamedScreenHandlerFactory;
|
|
||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
|
||||||
import net.minecraft.text.Text;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class ModuleInventory extends SimpleInventory {
|
|
||||||
public ModuleInventory() {
|
|
||||||
super(27);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void openModuleInventory(ServerPlayerEntity player, MinionFakePlayer minion) {
|
|
||||||
player.openHandledScreen(new SimpleNamedScreenHandlerFactory((syncId, playerInventory, player2) -> new ModuleInventoryScreenHandler(syncId, playerInventory, minion.getModuleInventory()), Text.translatable("minions.gui.modules.title", minion.getName())));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getMaxCountPerStack() {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isValid(int slot, ItemStack stack) {
|
|
||||||
return (stack.getCount() <= getMaxCountPerStack()) && stack.getItem() instanceof ModuleItem;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void readNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup lookup) {
|
|
||||||
Inventories.readNbt(nbt, heldStacks, lookup);
|
|
||||||
}
|
|
||||||
|
|
||||||
public NbtCompound writeNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup lookup) {
|
|
||||||
return Inventories.writeNbt(nbt, heldStacks, lookup);
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean hasModule(ModuleItem module) {
|
|
||||||
for(ItemStack stack : heldStacks) {
|
|
||||||
if(stack.getItem() instanceof ModuleItem module2 && module2 == module) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<ModuleItem> getModuleItems() {
|
|
||||||
ArrayList<ModuleItem> modules = new ArrayList<>();
|
|
||||||
for(ItemStack stack : heldStacks) {
|
|
||||||
if(stack.getItem() instanceof ModuleItem module) {
|
|
||||||
modules.add(module);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return modules;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Command> getAllCommands() {
|
|
||||||
ArrayList<Command> commands = new ArrayList<>();
|
|
||||||
for(ItemStack stack : heldStacks) {
|
|
||||||
if(stack.getItem() instanceof ModuleItem module) {
|
|
||||||
commands.addAll(module.getCommands());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return commands;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<CodeBlock<?,?>> getAllCodeBlocks() {
|
|
||||||
ArrayList<CodeBlock<?,?>> commands = new ArrayList<>();
|
|
||||||
for(ItemStack stack : heldStacks) {
|
|
||||||
if(stack.getItem() instanceof ModuleItem module) {
|
|
||||||
commands.addAll(module.getCodeBlocks());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return commands;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,80 +0,0 @@
|
|||||||
package io.github.skippyall.minions.gui;
|
|
||||||
|
|
||||||
import net.minecraft.entity.player.PlayerEntity;
|
|
||||||
import net.minecraft.entity.player.PlayerInventory;
|
|
||||||
import net.minecraft.item.ItemStack;
|
|
||||||
import net.minecraft.screen.GenericContainerScreenHandler;
|
|
||||||
import net.minecraft.screen.ScreenHandler;
|
|
||||||
import net.minecraft.screen.ScreenHandlerType;
|
|
||||||
import net.minecraft.screen.slot.Slot;
|
|
||||||
|
|
||||||
public class ModuleInventoryScreenHandler extends ScreenHandler {
|
|
||||||
private final int rows = 3;
|
|
||||||
private final ModuleInventory inventory;
|
|
||||||
public ModuleInventoryScreenHandler(int syncId, ModuleInventory inventory) {
|
|
||||||
super(ScreenHandlerType.GENERIC_9X3, syncId);
|
|
||||||
this.inventory = inventory;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ModuleInventoryScreenHandler(int syncId, PlayerInventory playerInventory, ModuleInventory inventory) {
|
|
||||||
super(ScreenHandlerType.GENERIC_9X3, syncId);
|
|
||||||
int k;
|
|
||||||
int j;
|
|
||||||
GenericContainerScreenHandler.checkSize(inventory, 3 * 9);
|
|
||||||
this.inventory = inventory;
|
|
||||||
inventory.onOpen(playerInventory.player);
|
|
||||||
int i = (rows - 4) * 18;
|
|
||||||
for (j = 0; j < rows; ++j) {
|
|
||||||
for (k = 0; k < 9; ++k) {
|
|
||||||
this.addSlot(new Slot(inventory, k + j * 9, 8 + k * 18, 18 + j * 18) {
|
|
||||||
@Override
|
|
||||||
public boolean canInsert(ItemStack stack) {
|
|
||||||
return super.canInsert(stack) && inventory.isValid(getIndex(), stack);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (j = 0; j < 3; ++j) {
|
|
||||||
for (k = 0; k < 9; ++k) {
|
|
||||||
this.addSlot(new Slot(playerInventory, k + j * 9 + 9, 8 + k * 18, 103 + j * 18 + i));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (j = 0; j < 9; ++j) {
|
|
||||||
this.addSlot(new Slot(playerInventory, j, 8 + j * 18, 161 + i));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean canUse(PlayerEntity player) {
|
|
||||||
return this.inventory.canPlayerUse(player);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ItemStack quickMove(PlayerEntity player, int slot) {
|
|
||||||
ItemStack itemStack = ItemStack.EMPTY;
|
|
||||||
Slot slot2 = this.slots.get(slot);
|
|
||||||
if (slot2 != null && slot2.hasStack()) {
|
|
||||||
ItemStack itemStack2 = slot2.getStack();
|
|
||||||
itemStack = itemStack2.copy();
|
|
||||||
if (slot < this.rows * 9 ? !this.insertItem(itemStack2, this.rows * 9, this.slots.size(), true) : !this.insertItem(itemStack2, 0, this.rows * 9, false)) {
|
|
||||||
return ItemStack.EMPTY;
|
|
||||||
}
|
|
||||||
if (itemStack2.isEmpty()) {
|
|
||||||
slot2.setStack(ItemStack.EMPTY);
|
|
||||||
} else {
|
|
||||||
slot2.markDirty();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return itemStack;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onClosed(PlayerEntity player) {
|
|
||||||
super.onClosed(player);
|
|
||||||
this.inventory.onClose(player);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ModuleInventory getInventory() {
|
|
||||||
return inventory;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,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.viewer, 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,222 @@
|
|||||||
|
package io.github.skippyall.minions.gui.input
|
||||||
|
|
||||||
|
import eu.pb4.sgui.api.elements.GuiElementBuilder
|
||||||
|
import eu.pb4.sgui.api.gui.AnvilInputGui
|
||||||
|
import io.github.skippyall.minions.gui.MinionsGui
|
||||||
|
import kotlinx.coroutines.Job
|
||||||
|
import kotlinx.coroutines.async
|
||||||
|
import kotlinx.coroutines.future.asCompletableFuture
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import net.minecraft.network.chat.Component
|
||||||
|
import net.minecraft.world.inventory.AnvilMenu
|
||||||
|
import net.minecraft.world.item.Items
|
||||||
|
import java.util.concurrent.CompletableFuture
|
||||||
|
|
||||||
|
class TextInput<T>(
|
||||||
|
parent: MinionsGui,
|
||||||
|
val title: Component,
|
||||||
|
val defaultValue: String,
|
||||||
|
val parser: suspend (String) -> Result<T, Component>
|
||||||
|
) : MinionsGui(parent) {
|
||||||
|
private val valid: GuiElementBuilder = GuiElementBuilder()
|
||||||
|
.setItem(Items.EMERALD_BLOCK)
|
||||||
|
.setName(Component.literal("OK"))
|
||||||
|
.setCallback(Runnable { this.onConfirm() })
|
||||||
|
|
||||||
|
private val invalid: GuiElementBuilder = GuiElementBuilder()
|
||||||
|
.setItem(Items.REDSTONE_BLOCK)
|
||||||
|
|
||||||
|
private lateinit var gui: AnvilInputGui
|
||||||
|
|
||||||
|
private var result: Result<T, Component>? = null
|
||||||
|
private var isConfirm = false
|
||||||
|
|
||||||
|
val job = Job()
|
||||||
|
|
||||||
|
init {
|
||||||
|
updateConfirmButton(defaultValue)
|
||||||
|
open()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun open() {
|
||||||
|
gui = object : AnvilInputGui(viewer, false) {
|
||||||
|
override fun onInput(input: String) {
|
||||||
|
updateConfirmButton(input)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onPlayerClose(success: Boolean) {
|
||||||
|
onBackingClosed()
|
||||||
|
if (job.isActive && !isConfirm) {
|
||||||
|
job.cancel()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
gui.setTitle(title)
|
||||||
|
gui.setDefaultInputValue(defaultValue)
|
||||||
|
gui.open()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun closeBacking() {
|
||||||
|
gui.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun updateConfirmButton(input: String) {
|
||||||
|
scope.launch {
|
||||||
|
val result = parser(input)
|
||||||
|
this@TextInput.result = result
|
||||||
|
if (result.isSuccess()) {
|
||||||
|
gui.setSlot(AnvilMenu.RESULT_SLOT, valid)
|
||||||
|
} else {
|
||||||
|
val text = result.getErrorOrThrow()
|
||||||
|
gui.setSlot(AnvilMenu.RESULT_SLOT, invalid.setName(text))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onConfirm() {
|
||||||
|
result?.ifSuccess { _: T? ->
|
||||||
|
isConfirm = true
|
||||||
|
}
|
||||||
|
job.complete()
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
@JvmStatic
|
||||||
|
suspend fun <T>input(
|
||||||
|
gui: MinionsGui,
|
||||||
|
title: Component,
|
||||||
|
defaultValue: String,
|
||||||
|
parser: suspend (String) -> Result<T, Component>,
|
||||||
|
): T? {
|
||||||
|
val input = TextInput(
|
||||||
|
parent = gui,
|
||||||
|
title = title,
|
||||||
|
defaultValue = defaultValue,
|
||||||
|
parser = parser,
|
||||||
|
)
|
||||||
|
|
||||||
|
input.job.join()
|
||||||
|
|
||||||
|
return input.result?.getOrDefault(null)
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun <T>inputFuture(
|
||||||
|
gui: MinionsGui,
|
||||||
|
title: Component,
|
||||||
|
defaultValue: String,
|
||||||
|
parser: (String) -> Result<T, Component>,
|
||||||
|
): CompletableFuture<T?> {
|
||||||
|
return gui.scope.async {
|
||||||
|
val input = TextInput(
|
||||||
|
parent = gui,
|
||||||
|
title = title,
|
||||||
|
defaultValue = defaultValue,
|
||||||
|
parser = parser,
|
||||||
|
)
|
||||||
|
|
||||||
|
input.job.join()
|
||||||
|
|
||||||
|
return@async input.result?.getOrDefault(null)
|
||||||
|
}.asCompletableFuture()
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
suspend fun inputString(
|
||||||
|
gui: MinionsGui,
|
||||||
|
title: Component,
|
||||||
|
defaultValue: String,
|
||||||
|
): String? {
|
||||||
|
return input<String>(
|
||||||
|
gui = gui,
|
||||||
|
title = title,
|
||||||
|
defaultValue = defaultValue,
|
||||||
|
parser = { result: String? -> Result.Success<String, Component>(result) },
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun inputStringFuture(
|
||||||
|
gui: MinionsGui,
|
||||||
|
title: Component,
|
||||||
|
defaultValue: String,
|
||||||
|
): CompletableFuture<String?> {
|
||||||
|
return gui.scope.async {
|
||||||
|
inputString(
|
||||||
|
gui,
|
||||||
|
title,
|
||||||
|
defaultValue
|
||||||
|
)
|
||||||
|
}.asCompletableFuture()
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
suspend fun inputLong(
|
||||||
|
gui: MinionsGui,
|
||||||
|
title: Component,
|
||||||
|
defaultValue: Long,
|
||||||
|
): Long? {
|
||||||
|
return input<Long>(
|
||||||
|
gui = gui,
|
||||||
|
title = title,
|
||||||
|
defaultValue = defaultValue.toString(),
|
||||||
|
parser = { string ->
|
||||||
|
Result.wrapCustomError<Long, Component>(
|
||||||
|
{ string.toLong() },
|
||||||
|
Component.translatable("minions.command.input.int.fail")
|
||||||
|
)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun inputLongFuture(
|
||||||
|
gui: MinionsGui,
|
||||||
|
title: Component,
|
||||||
|
defaultValue: Long,
|
||||||
|
): CompletableFuture<Long?> {
|
||||||
|
return gui.scope.async {
|
||||||
|
inputLong(
|
||||||
|
gui,
|
||||||
|
title,
|
||||||
|
defaultValue
|
||||||
|
)
|
||||||
|
}.asCompletableFuture()
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
suspend fun inputDouble(
|
||||||
|
gui: MinionsGui,
|
||||||
|
title: Component,
|
||||||
|
defaultValue: Double,
|
||||||
|
): Double? {
|
||||||
|
return input<Double>(
|
||||||
|
gui = gui,
|
||||||
|
title = title,
|
||||||
|
defaultValue = defaultValue.toString(),
|
||||||
|
parser = { string ->
|
||||||
|
Result.wrapCustomError<Double, Component>(
|
||||||
|
{ string.toDouble() },
|
||||||
|
Component.translatable("minions.command.input.int.fail")
|
||||||
|
)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun inputDoubleFuture(
|
||||||
|
gui: MinionsGui,
|
||||||
|
title: Component,
|
||||||
|
defaultValue: Double,
|
||||||
|
): CompletableFuture<Double?> {
|
||||||
|
return gui.scope.async {
|
||||||
|
inputDouble(
|
||||||
|
gui,
|
||||||
|
title,
|
||||||
|
defaultValue
|
||||||
|
)
|
||||||
|
}.asCompletableFuture()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+144
@@ -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.viewer;
|
||||||
|
|
||||||
|
SimpleGui gui = new SimpleGui(MenuType.GENERIC_3x3, player, false) {
|
||||||
|
@Override
|
||||||
|
public void onPlayerClose(boolean success) {
|
||||||
|
onClose.run();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
gui.setTitle(Component.translatable("minions.gui.instruction.title"));
|
||||||
|
|
||||||
|
gui.setSlot(2, me.backButton());
|
||||||
|
gui.setSlot(3, new GuiElementBuilder()
|
||||||
|
.setItem(Items.BOOK)
|
||||||
|
.setName(Component.translatable("minions.gui.instruction.list"))
|
||||||
|
.setCallback(() -> new InstructionListGui(me, context))
|
||||||
|
);
|
||||||
|
gui.setSlot(5, new GuiElementBuilder()
|
||||||
|
.setItem(Items.WRITABLE_BOOK)
|
||||||
|
.setName(Component.translatable("minions.gui.instruction.create"))
|
||||||
|
.setCallback(() -> createNewInstruction(me, context))
|
||||||
|
);
|
||||||
|
|
||||||
|
gui.open();
|
||||||
|
return gui;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void createNewInstruction(MinionsGui parent, GuiContext.Minion context) {
|
||||||
|
MinionFakePlayer minion = context.getMinion();
|
||||||
|
ServerPlayer viewer = parent.viewer;
|
||||||
|
selectInstructionModuleMenu(parent, context).thenAccept(instructionType ->
|
||||||
|
inputInstructionName(parent, context, "Instruction").thenAccept(name -> {
|
||||||
|
if (!minion.isRemoved() && !minion.hasDisconnected()) {
|
||||||
|
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.inputFuture(parent, Component.translatable("minions.gui.instruction.enter_name"), defaultValue, name -> {
|
||||||
|
if (context.getMinion().getInstructionManager().hasInstruction(name)) {
|
||||||
|
return new Result.Error<>(Component.translatable("minions.gui.instruction.name_already_used"));
|
||||||
|
}
|
||||||
|
return new Result.Success<>(name);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean checkInstructionExists(String name, ConfiguredInstruction<?> instruction, MinionFakePlayer minion, ServerPlayer player) {
|
||||||
|
boolean stillExists = !minion.isRemoved() && !minion.hasDisconnected() && minion.getInstructionManager().getInstruction(name) == instruction;
|
||||||
|
if (!stillExists) {
|
||||||
|
player.closeContainer();
|
||||||
|
player.sendSystemMessage(Component.translatable("minions.gui.instruction.removed"));
|
||||||
|
}
|
||||||
|
return stillExists;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static CompletableFuture<InstructionType<MinionRuntime>> selectInstructionModuleMenu(MinionsGui parent, GuiContext.Minion context) {
|
||||||
|
MinionFakePlayer minion = context.getMinion();
|
||||||
|
ServerPlayer viewer = parent.viewer;
|
||||||
|
|
||||||
|
if (minion.getModuleInventory().getModules().isEmpty()) {
|
||||||
|
viewer.sendSystemMessage(Component.translatable("minions.gui.instruction.no_modules"));
|
||||||
|
return CompletableFuture.failedFuture(new NoSuchElementException("No modules"));
|
||||||
|
}
|
||||||
|
|
||||||
|
CompletableFuture<InstructionType<MinionRuntime>> future = new CompletableFuture<>();
|
||||||
|
|
||||||
|
new SimpleMinionsGui(parent, (closeHandler, me) -> {
|
||||||
|
SimpleGui gui = new SimpleGui(MenuType.GENERIC_9x4, viewer, false) {
|
||||||
|
@Override
|
||||||
|
public void onPlayerClose(boolean success) {
|
||||||
|
if (!future.isDone()) {
|
||||||
|
future.cancel(false);
|
||||||
|
}
|
||||||
|
closeHandler.run();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
gui.setTitle(Component.translatable("minions.gui.instruction.select_instruction"));
|
||||||
|
|
||||||
|
gui.setSlot(8, me.backButton());
|
||||||
|
|
||||||
|
for (int i = 0; i < minion.getModuleInventory().getContainerSize(); i++) {
|
||||||
|
ItemStack moduleItem = minion.getModuleInventory().getItem(i);
|
||||||
|
MinionModule module = moduleItem.get(MinionComponentTypes.MODULE);
|
||||||
|
if (module != null && !module.instructions().isEmpty()) {
|
||||||
|
gui.setSlot(i + 9, new GuiElementBuilder(moduleItem)
|
||||||
|
.setCallback(() -> selectInstructionMenu(parent, context, module)
|
||||||
|
.thenApply(future::complete)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
gui.open();
|
||||||
|
return gui;
|
||||||
|
});
|
||||||
|
return future;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static CompletableFuture<InstructionType<MinionRuntime>> selectInstructionMenu(MinionsGui parent, GuiContext.Minion context, MinionModule module) {
|
||||||
|
CompletableFuture<InstructionType<MinionRuntime>> future = new CompletableFuture<>();
|
||||||
|
|
||||||
|
new SimpleMinionsGui(parent, (closeHandler, me) -> {
|
||||||
|
SimpleGui gui = new SimpleGui(MenuType.GENERIC_9x4, parent.viewer, false) {
|
||||||
|
@Override
|
||||||
|
public void onPlayerClose(boolean success) {
|
||||||
|
if (!future.isDone()) {
|
||||||
|
future.cancel(false);
|
||||||
|
}
|
||||||
|
closeHandler.run();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
gui.setTitle(Component.translatable("minions.gui.instruction.select_instruction"));
|
||||||
|
|
||||||
|
gui.setSlot(8, me.backButton());
|
||||||
|
int slot = 9;
|
||||||
|
for (InstructionType<MinionRuntime> instructionType : module.instructions()) {
|
||||||
|
gui.setSlot(slot, createInstructionElement(instructionType, parent.viewer.registryAccess())
|
||||||
|
.setCallback(() -> future.complete(instructionType))
|
||||||
|
);
|
||||||
|
slot++;
|
||||||
|
}
|
||||||
|
|
||||||
|
gui.open();
|
||||||
|
return gui;
|
||||||
|
});
|
||||||
|
return future;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static GuiElementBuilder createInstructionElement(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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,117 +0,0 @@
|
|||||||
package io.github.skippyall.minions.input;
|
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.function.Consumer;
|
|
||||||
import java.util.function.Function;
|
|
||||||
|
|
||||||
public interface Result<T, E> {
|
|
||||||
static <T> Result<T, String> wrap(UnsafeOperation<T> toWrap) {
|
|
||||||
return wrapCustomError(toWrap, Exception::getMessage);
|
|
||||||
}
|
|
||||||
|
|
||||||
static <T, E> Result<T, E> wrapCustomError(UnsafeOperation<T> toWrap, E error) {
|
|
||||||
return wrapCustomError(toWrap, e -> error);
|
|
||||||
}
|
|
||||||
|
|
||||||
static <T, E> Result<T, E> wrapCustomError(UnsafeOperation<T> toWrap, Function<Exception, E> errorTransformer) {
|
|
||||||
try {
|
|
||||||
return new Result.Success<>(toWrap.run());
|
|
||||||
} catch (Exception e) {
|
|
||||||
return new Result.Error<>(errorTransformer.apply(e));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean isSuccess();
|
|
||||||
|
|
||||||
@NotNull T getOrDefault(@NotNull T defaultValue);
|
|
||||||
|
|
||||||
@NotNull T getOrThrow();
|
|
||||||
|
|
||||||
@NotNull E getErrorOrThrow();
|
|
||||||
|
|
||||||
@NotNull Optional<T> getOptional();
|
|
||||||
|
|
||||||
void ifSuccess(@NotNull Consumer<T> handler);
|
|
||||||
|
|
||||||
void ifError(@NotNull Consumer<Error<T, E>> handler);
|
|
||||||
|
|
||||||
record Success<T, E>(@NotNull T result) implements Result<T, E> {
|
|
||||||
@Override
|
|
||||||
public boolean isSuccess() {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull T getOrDefault(@NotNull T defaultValue) {
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull T getOrThrow() {
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull E getErrorOrThrow() {
|
|
||||||
throw new RuntimeException("Result was not an Error");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull Optional<T> getOptional() {
|
|
||||||
return Optional.of(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void ifSuccess(@NotNull Consumer<T> handler) {
|
|
||||||
handler.accept(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void ifError(@NotNull Consumer<Error<T, E>> handler) {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
record Error<T, E>(@NotNull E message) implements Result<T, E> {
|
|
||||||
@Override
|
|
||||||
public boolean isSuccess() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull T getOrDefault(@NotNull T defaultValue) {
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull T getOrThrow() {
|
|
||||||
throw new RuntimeException("Result was an error: " + message.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull E getErrorOrThrow() {
|
|
||||||
return message;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull Optional<T> getOptional() {
|
|
||||||
return Optional.empty();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void ifSuccess(@NotNull Consumer<T> handler) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void ifError(Consumer<Error<T, E>> handler) {
|
|
||||||
handler.accept(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
interface UnsafeOperation<T> {
|
|
||||||
T run() throws Exception;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,90 +0,0 @@
|
|||||||
package io.github.skippyall.minions.input;
|
|
||||||
|
|
||||||
import eu.pb4.sgui.api.elements.GuiElementBuilder;
|
|
||||||
import eu.pb4.sgui.api.gui.AnvilInputGui;
|
|
||||||
import net.minecraft.item.Items;
|
|
||||||
import net.minecraft.screen.AnvilScreenHandler;
|
|
||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
|
||||||
import net.minecraft.text.Text;
|
|
||||||
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
import java.util.function.Function;
|
|
||||||
import java.util.function.Predicate;
|
|
||||||
|
|
||||||
public class TextInput<T> extends AnvilInputGui {
|
|
||||||
private final GuiElementBuilder valid = new GuiElementBuilder()
|
|
||||||
.setItem(Items.EMERALD_BLOCK)
|
|
||||||
.setName(Text.literal("OK"))
|
|
||||||
.setCallback(this::onConfirm);
|
|
||||||
|
|
||||||
private final GuiElementBuilder invalid = new GuiElementBuilder()
|
|
||||||
.setItem(Items.REDSTONE_BLOCK);
|
|
||||||
private final Function<String, CompletableFuture<Result<T, Text>>> parser;
|
|
||||||
private final CompletableFuture<T> future;
|
|
||||||
private Result<T, Text> result;
|
|
||||||
|
|
||||||
public TextInput(ServerPlayerEntity player, Text title, String defaultValue, Function<String, CompletableFuture<Result<T, Text>>> parser, CompletableFuture<T> future) {
|
|
||||||
super(player, false);
|
|
||||||
setTitle(title);
|
|
||||||
setDefaultInputValue(defaultValue);
|
|
||||||
this.parser = parser;
|
|
||||||
this.future = future;
|
|
||||||
|
|
||||||
updateConfirmButton(defaultValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> CompletableFuture<T> inputSync(ServerPlayerEntity player, Text title, String defaultValue, Function<String, Result<T, Text>> parser) {
|
|
||||||
return input(player, title, defaultValue, (String string) -> CompletableFuture.completedFuture(parser.apply(string)));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> CompletableFuture<T> input(ServerPlayerEntity player, Text title, String defaultValue, Function<String, CompletableFuture<Result<T, Text>>> parser) {
|
|
||||||
CompletableFuture<T> future = new CompletableFuture<>();
|
|
||||||
new TextInput<>(player, title, defaultValue, parser, future).open();
|
|
||||||
return future;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static CompletableFuture<String> inputString(ServerPlayerEntity player, Text title, String defaultValue) {
|
|
||||||
return inputSync(player, title, defaultValue, Result.Success::new);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static CompletableFuture<Integer> inputInt(ServerPlayerEntity player, Text title, String defaultValue) {
|
|
||||||
return inputSync(player, title, defaultValue, string -> Result.wrapCustomError(() -> Integer.valueOf(string), Text.translatable("minions.command.input.int.fail")));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static CompletableFuture<Float> inputFloat(ServerPlayerEntity player, Text title, String defaultValue) {
|
|
||||||
return inputSync(player, title, defaultValue, string -> Result.wrapCustomError(() -> Float.valueOf(string), Text.translatable("minions.command.input.float.fail")));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onInput(String input) {
|
|
||||||
updateConfirmButton(input);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void updateConfirmButton(String input) {
|
|
||||||
parser.apply(input).thenAccept(result -> {
|
|
||||||
this.result = result;
|
|
||||||
if(result.isSuccess()) {
|
|
||||||
setSlot(AnvilScreenHandler.OUTPUT_ID, valid);
|
|
||||||
} else {
|
|
||||||
Text text = result.getErrorOrThrow();
|
|
||||||
setSlot(AnvilScreenHandler.OUTPUT_ID, invalid.setName(text));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onClose() {
|
|
||||||
if(!future.isDone()) {
|
|
||||||
future.cancel(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void onConfirm() {
|
|
||||||
if(result != null) {
|
|
||||||
result.ifSuccess(success -> {
|
|
||||||
future.complete(success);
|
|
||||||
close();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+54
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
package io.github.skippyall.minions.minion;
|
||||||
|
|
||||||
|
import com.mojang.serialization.Codec;
|
||||||
|
import io.github.skippyall.minions.registration.MinionRegistries;
|
||||||
|
import net.minecraft.resources.Identifier;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public class MinionConfig {
|
||||||
|
public static final Codec<MinionConfig> CODEC = Codec.<Option<?>, Object>dispatchedMap(
|
||||||
|
MinionRegistries.MINION_CONFIG_OPTIONS.byNameCodec(),
|
||||||
|
Option::codec
|
||||||
|
).xmap(MinionConfig::new, config -> config.values);
|
||||||
|
|
||||||
|
private final Map<Option<?>, Object> values;
|
||||||
|
|
||||||
|
public MinionConfig() {
|
||||||
|
values = new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
private MinionConfig(Map<Option<?>, Object> values) {
|
||||||
|
this.values = new HashMap<>(values);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> T getOption(Option<T> option) {
|
||||||
|
if(values.containsKey(option)) {
|
||||||
|
//noinspection unchecked
|
||||||
|
return (T) values.get(option);
|
||||||
|
} else {
|
||||||
|
return option.defaultValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (!(o instanceof MinionConfig that)) return false;
|
||||||
|
return Objects.equals(values, that.values);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hashCode(values);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Option<Boolean> booleanOption(Identifier key, boolean defaultValue) {
|
||||||
|
return new Option<>(key, defaultValue, Codec.BOOL);
|
||||||
|
}
|
||||||
|
|
||||||
|
public record Option<T>(Identifier key, T defaultValue, Codec<T> codec) {
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (!(o instanceof Option<?> option)) return false;
|
||||||
|
return Objects.equals(key, option.key);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hashCode(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,65 +1,58 @@
|
|||||||
package io.github.skippyall.minions.minion;
|
package io.github.skippyall.minions.minion;
|
||||||
|
|
||||||
import com.mojang.authlib.properties.PropertyMap;
|
import com.mojang.authlib.properties.PropertyMap;
|
||||||
import com.mojang.datafixers.util.Pair;
|
|
||||||
import com.mojang.serialization.Codec;
|
import com.mojang.serialization.Codec;
|
||||||
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
||||||
import eu.pb4.polymer.core.api.other.PolymerComponent;
|
import io.github.skippyall.minions.listener.SerializableListenerManager;
|
||||||
import io.github.skippyall.minions.Minions;
|
import io.github.skippyall.minions.registration.MinionRegistries;
|
||||||
import net.minecraft.component.ComponentType;
|
import net.minecraft.core.UUIDUtil;
|
||||||
import net.minecraft.nbt.NbtCompound;
|
import net.minecraft.server.MinecraftServer;
|
||||||
import net.minecraft.nbt.NbtOps;
|
import net.minecraft.util.ExtraCodecs;
|
||||||
import net.minecraft.registry.Registries;
|
|
||||||
import net.minecraft.registry.Registry;
|
|
||||||
import net.minecraft.util.Identifier;
|
|
||||||
import net.minecraft.util.Uuids;
|
|
||||||
import net.minecraft.util.dynamic.Codecs;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
public record MinionData(UUID uuid, String name, Optional<PropertyMap> skin, boolean isSpawned) {
|
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 ->
|
public static final Codec<MinionData> CODEC = RecordCodecBuilder.create(instance ->
|
||||||
instance.group(
|
instance.group(
|
||||||
Uuids.CODEC.fieldOf("uuid").forGetter(MinionData::uuid),
|
UUIDUtil.AUTHLIB_CODEC.fieldOf("uuid").forGetter(MinionData::uuid),
|
||||||
Codec.STRING.fieldOf("name").forGetter(MinionData::name),
|
Codec.STRING.fieldOf("name").forGetter(MinionData::name),
|
||||||
Codecs.GAME_PROFILE_PROPERTY_MAP.optionalFieldOf("skin").forGetter(MinionData::skin),
|
ExtraCodecs.PROPERTY_MAP.optionalFieldOf("skin").forGetter(MinionData::skin),
|
||||||
Codec.BOOL.optionalFieldOf("isSpawned", false).forGetter(MinionData::isSpawned)
|
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)
|
).apply(instance, MinionData::new)
|
||||||
);
|
);
|
||||||
|
|
||||||
public static final ComponentType<UUID> COMPONENT = Registry.register(Registries.DATA_COMPONENT_TYPE, Identifier.of(Minions.MOD_ID, "minion_data"), ComponentType.<UUID>builder().codec(Uuids.CODEC).build());
|
public static MinionData createDefault(MinecraftServer server) {
|
||||||
|
return new MinionData(
|
||||||
public static MinionData createDefault() {
|
UUID.randomUUID(),
|
||||||
return new MinionData(UUID.randomUUID(), MinionProfileUtils.newDefaultMinionName(), Optional.empty(), false);
|
MinionProfileUtils.newDefaultMinionName(server),
|
||||||
}
|
Optional.empty(),
|
||||||
|
false,
|
||||||
public MinionData withUuid(UUID uuid) {
|
new SerializableListenerManager<>(),
|
||||||
return new MinionData(uuid, name, skin, isSpawned);
|
new MinionConfig()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public MinionData withName(String name) {
|
public MinionData withName(String name) {
|
||||||
return new MinionData(uuid, name, skin, isSpawned);
|
return new MinionData(uuid, name, skin, isSpawned, listeners, config);
|
||||||
}
|
}
|
||||||
|
|
||||||
public MinionData withSkin(Optional<PropertyMap> skin) {
|
public MinionData withSkin(Optional<PropertyMap> skin) {
|
||||||
return new MinionData(uuid, name, skin, isSpawned);
|
return new MinionData(uuid, name, skin, isSpawned, listeners, config);
|
||||||
}
|
}
|
||||||
|
|
||||||
public MinionData withSpawned(boolean isSpawned) {
|
public MinionData withSpawned(boolean isSpawned) {
|
||||||
return new MinionData(uuid, name, skin, isSpawned);
|
return new MinionData(uuid, name, skin, isSpawned, listeners, config);
|
||||||
}
|
|
||||||
|
|
||||||
public NbtCompound writeNbt() {
|
|
||||||
return (NbtCompound) MinionData.CODEC.encode(this, NbtOps.INSTANCE, null).result().orElse(new NbtCompound());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static MinionData readNbt(NbtCompound nbt) {
|
|
||||||
return MinionData.CODEC.decode(NbtOps.INSTANCE, nbt).resultOrPartial().map(Pair::getFirst).orElseGet(MinionData::createDefault);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void register() {
|
|
||||||
PolymerComponent.registerDataComponent(COMPONENT);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,36 +1,39 @@
|
|||||||
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.gui.MinionLookGui;
|
||||||
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
|
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
|
||||||
import net.minecraft.component.DataComponentTypes;
|
import io.github.skippyall.minions.registration.MinionComponentTypes;
|
||||||
import net.minecraft.entity.player.PlayerEntity;
|
import net.fabricmc.fabric.api.networking.v1.context.PacketContext;
|
||||||
import net.minecraft.item.Item;
|
import net.minecraft.core.HolderLookup;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.core.component.DataComponents;
|
||||||
import net.minecraft.item.ItemUsageContext;
|
import net.minecraft.network.chat.Component;
|
||||||
import net.minecraft.item.Items;
|
import net.minecraft.resources.Identifier;
|
||||||
import net.minecraft.item.tooltip.TooltipType;
|
import net.minecraft.server.MinecraftServer;
|
||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
import net.minecraft.server.level.ServerLevel;
|
||||||
import net.minecraft.server.world.ServerWorld;
|
import net.minecraft.server.level.ServerPlayer;
|
||||||
import net.minecraft.text.Text;
|
import net.minecraft.world.InteractionHand;
|
||||||
import net.minecraft.util.ActionResult;
|
import net.minecraft.world.InteractionResult;
|
||||||
import net.minecraft.util.Hand;
|
import net.minecraft.world.entity.player.Player;
|
||||||
import net.minecraft.util.Identifier;
|
import net.minecraft.world.item.Item;
|
||||||
import net.minecraft.util.math.Vec2f;
|
import net.minecraft.world.item.ItemStack;
|
||||||
import net.minecraft.world.World;
|
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 xyz.nucleoid.packettweaker.PacketContext;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public class MinionItem extends Item implements PolymerItem {
|
public class MinionItem extends Item implements PolymerItem {
|
||||||
public MinionItem(Settings settings) {
|
public MinionItem(Properties settings) {
|
||||||
super(settings);
|
super(settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @Nullable Identifier getPolymerItemModel(ItemStack stack, PacketContext context) {
|
public @Nullable Identifier getPolymerItemModel(ItemStack stack, PacketContext context, HolderLookup.Provider lookup) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,64 +43,64 @@ public class MinionItem extends Item implements PolymerItem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ItemStack getPolymerItemStack(ItemStack stack, TooltipType tooltipType, PacketContext player) {
|
public ItemStack getPolymerItemStack(ItemStack stack, TooltipFlag tooltipType, PacketContext player, HolderLookup.Provider lookup) {
|
||||||
ItemStack out = PolymerItemUtils.createItemStack(stack, tooltipType, player);
|
ItemStack out = PolymerItem.super.getPolymerItemStack(stack, tooltipType, player, lookup);
|
||||||
out.set(DataComponentTypes.ENCHANTMENT_GLINT_OVERRIDE, true);
|
out.set(DataComponents.ENCHANTMENT_GLINT_OVERRIDE, true);
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void appendTooltip(ItemStack stack, TooltipContext context, List<Text> tooltip, TooltipType type) {
|
public void appendHoverText(ItemStack stack, TooltipContext context, TooltipDisplay component, Consumer<Component> tooltip, TooltipFlag type) {
|
||||||
MinionData data = getData(stack);
|
//MinionData data = getData(stack);
|
||||||
if(data != null) {
|
//if(data != null) {
|
||||||
tooltip.add(Text.translatable("minions.minion_item.tooltip", data.name()));
|
// tooltip.accept(Component.translatable("minions.minion_item.tooltip", data.name()));
|
||||||
}
|
//}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ActionResult use(World world, PlayerEntity user, Hand hand) {
|
public InteractionResult use(Level world, Player user, InteractionHand hand) {
|
||||||
if(user instanceof ServerPlayerEntity serverPlayer) {
|
if(user instanceof ServerPlayer serverPlayer) {
|
||||||
ItemStack stack = user.getStackInHand(hand);
|
ItemStack stack = user.getItemInHand(hand);
|
||||||
MinionLookGui.open(serverPlayer, stack);
|
new MinionLookGui(serverPlayer, stack);
|
||||||
return ActionResult.SUCCESS;
|
return InteractionResult.SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ActionResult.SUCCESS_SERVER;
|
return InteractionResult.SUCCESS_SERVER;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ActionResult useOnBlock(ItemUsageContext context) {
|
public InteractionResult useOn(UseOnContext context) {
|
||||||
if(!context.getWorld().isClient) {
|
if(!context.getLevel().isClientSide()) {
|
||||||
MinionData data = getDataOrDefault(context.getStack());
|
MinionData data = getDataOrDefault(context.getLevel().getServer(), context.getItemInHand());
|
||||||
MinionFakePlayer.spawnMinion(data, (ServerWorld) context.getWorld(), context.getBlockPos().toCenterPos().add(0,0.5,0), new Vec2f(0, 0));
|
MinionFakePlayer.spawnMinion(data, (ServerLevel) context.getLevel(), context.getClickedPos().getCenter().add(0,0.5,0), new Vec2(0, 0));
|
||||||
}
|
}
|
||||||
context.getStack().decrement(1);
|
context.getItemInHand().shrink(1);
|
||||||
return ActionResult.SUCCESS;
|
return InteractionResult.SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setData(MinionData data, ItemStack item) {
|
public static void setData(MinecraftServer server, MinionData data, ItemStack item) {
|
||||||
item.set(MinionData.COMPONENT, data.uuid());
|
item.set(MinionComponentTypes.MINION_DATA, data.uuid());
|
||||||
MinionPersistentState.INSTANCE.updateMinionData(data);
|
MinionPersistentState.get(server).updateMinionData(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public static MinionData getData(ItemStack item) {
|
public static MinionData getData(MinecraftServer server, ItemStack item) {
|
||||||
if(item.contains(MinionData.COMPONENT)) {
|
if(item.has(MinionComponentTypes.MINION_DATA)) {
|
||||||
return MinionPersistentState.INSTANCE.getMinionData(item.get(MinionData.COMPONENT));
|
return MinionPersistentState.get(server).getMinionData(item.get(MinionComponentTypes.MINION_DATA));
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static MinionData getDataOrDefault(ItemStack item) {
|
public static MinionData getDataOrDefault(MinecraftServer server, ItemStack item) {
|
||||||
MinionData data = getData(item);
|
MinionData data = getData(server, item);
|
||||||
if(data == null) {
|
if(data == null) {
|
||||||
data = MinionData.createDefault();
|
data = MinionData.createDefault(server);
|
||||||
setData(data, item);
|
setData(server, data, item);
|
||||||
}
|
}
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean containsData(ItemStack item) {
|
public static boolean containsData(ItemStack item) {
|
||||||
return item.contains(MinionData.COMPONENT);
|
return item.has(MinionComponentTypes.MINION_DATA);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package io.github.skippyall.minions.minion;
|
||||||
|
|
||||||
|
import io.github.skippyall.minions.listener.SerializableListenerManager;
|
||||||
|
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
|
||||||
|
import io.github.skippyall.minions.program.instruction.ConfiguredInstruction;
|
||||||
|
import org.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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,45 +1,38 @@
|
|||||||
package io.github.skippyall.minions.minion;
|
package io.github.skippyall.minions.minion;
|
||||||
|
|
||||||
import net.minecraft.nbt.NbtCompound;
|
import com.mojang.serialization.Codec;
|
||||||
import net.minecraft.nbt.NbtElement;
|
import io.github.skippyall.minions.Minions;
|
||||||
import net.minecraft.nbt.NbtList;
|
import net.minecraft.resources.Identifier;
|
||||||
import net.minecraft.registry.RegistryWrapper;
|
|
||||||
import net.minecraft.server.MinecraftServer;
|
import net.minecraft.server.MinecraftServer;
|
||||||
import net.minecraft.world.PersistentState;
|
import net.minecraft.world.level.Level;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.level.saveddata.SavedData;
|
||||||
|
import net.minecraft.world.level.saveddata.SavedDataType;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
public class MinionPersistentState extends PersistentState {
|
public class MinionPersistentState extends SavedData {
|
||||||
public static Type<MinionPersistentState> TYPE = new Type<>(MinionPersistentState::new, MinionPersistentState::read, null);
|
public static final Codec<MinionPersistentState> CODEC = MinionData.CODEC.listOf().xmap(MinionPersistentState::new, MinionPersistentState::getMinionDataList);
|
||||||
|
|
||||||
public static MinionPersistentState INSTANCE;
|
public static SavedDataType<MinionPersistentState> TYPE = new SavedDataType<>(
|
||||||
|
Identifier.fromNamespaceAndPath(Minions.MOD_ID, "minion"),
|
||||||
|
MinionPersistentState::new,
|
||||||
|
MinionPersistentState.CODEC,
|
||||||
|
null
|
||||||
|
);
|
||||||
|
|
||||||
private final Map<UUID, MinionData> minionData = new HashMap<>();
|
private final Map<UUID, MinionData> minionData = new HashMap<>();
|
||||||
|
|
||||||
@Override
|
public MinionPersistentState() {
|
||||||
public NbtCompound writeNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLookup) {
|
|
||||||
NbtList list = new NbtList();
|
|
||||||
for(MinionData data : minionData.values()) {
|
|
||||||
list.add(data.writeNbt());
|
|
||||||
}
|
|
||||||
nbt.put("minions", list);
|
|
||||||
|
|
||||||
return nbt;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static MinionPersistentState read(NbtCompound compound, RegistryWrapper.WrapperLookup lookup) {
|
public MinionPersistentState(List<MinionData> dataList) {
|
||||||
NbtList list = compound.getList("minions", NbtElement.COMPOUND_TYPE);
|
for (MinionData data : dataList) {
|
||||||
MinionPersistentState instance = new MinionPersistentState();
|
minionData.put(data.uuid(), data);
|
||||||
for(NbtElement element : list) {
|
|
||||||
if(element instanceof NbtCompound compound1) {
|
|
||||||
MinionData data = MinionData.readNbt(compound1);
|
|
||||||
instance.minionData.put(data.uuid(), data);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return instance;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public MinionData getMinionData(UUID uuid) {
|
public MinionData getMinionData(UUID uuid) {
|
||||||
@@ -50,9 +43,13 @@ public class MinionPersistentState extends PersistentState {
|
|||||||
return minionData;
|
return minionData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<MinionData> getMinionDataList() {
|
||||||
|
return List.copyOf(minionData.values());
|
||||||
|
}
|
||||||
|
|
||||||
public void updateMinionData(MinionData data) {
|
public void updateMinionData(MinionData data) {
|
||||||
minionData.put(data.uuid(), data);
|
minionData.put(data.uuid(), data);
|
||||||
markDirty();
|
setDirty();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isMinion(UUID uuid) {
|
public boolean isMinion(UUID uuid) {
|
||||||
@@ -60,10 +57,16 @@ public class MinionPersistentState extends PersistentState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean isMinionNameTaken(String name) {
|
public boolean isMinionNameTaken(String name) {
|
||||||
return minionData.values().stream().anyMatch(data -> data.name().equals(name));
|
return getMinionWithName(name).isPresent();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void create(MinecraftServer server) {
|
public Optional<MinionData> getMinionWithName(String name) {
|
||||||
INSTANCE = server.getWorld(World.OVERWORLD).getPersistentStateManager().getOrCreate(TYPE, "minion");
|
return minionData.values().stream()
|
||||||
|
.filter(data -> data.name().equals(name))
|
||||||
|
.findFirst();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static MinionPersistentState get(MinecraftServer server) {
|
||||||
|
return server.getLevel(Level.OVERWORLD).getDataStorage().computeIfAbsent(TYPE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,70 +1,63 @@
|
|||||||
package io.github.skippyall.minions.minion;
|
package io.github.skippyall.minions.minion;
|
||||||
|
|
||||||
import com.mojang.authlib.GameProfile;
|
import com.mojang.authlib.GameProfile;
|
||||||
import com.mojang.authlib.ProfileLookupCallback;
|
|
||||||
import com.mojang.authlib.properties.PropertyMap;
|
import com.mojang.authlib.properties.PropertyMap;
|
||||||
import com.mojang.authlib.yggdrasil.ProfileResult;
|
|
||||||
import com.mojang.brigadier.StringReader;
|
import com.mojang.brigadier.StringReader;
|
||||||
import io.github.skippyall.minions.input.Result;
|
import io.github.skippyall.minions.MinionsConfig;
|
||||||
import net.minecraft.block.entity.SkullBlockEntity;
|
import io.github.skippyall.minions.gui.input.Result;
|
||||||
|
import net.minecraft.network.chat.Component;
|
||||||
import net.minecraft.server.MinecraftServer;
|
import net.minecraft.server.MinecraftServer;
|
||||||
import net.minecraft.text.Text;
|
import net.minecraft.util.StringUtil;
|
||||||
import net.minecraft.util.StringHelper;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
import java.util.concurrent.ForkJoinPool;
|
|
||||||
|
|
||||||
import static io.github.skippyall.minions.Minions.LOGGER;
|
import static io.github.skippyall.minions.Minions.LOGGER;
|
||||||
|
|
||||||
public class MinionProfileUtils {
|
public class MinionProfileUtils {
|
||||||
public static final String PREFIX = "+";
|
public static String getPrefix() {
|
||||||
|
return MinionsConfig.get().minion.minionPrefix;
|
||||||
|
}
|
||||||
|
|
||||||
public static GameProfile makeNewMinionProfile(UUID uuidMinion, String username, PropertyMap skin) {
|
public static GameProfile makeNewMinionProfile(UUID uuidMinion, String username, PropertyMap skin) {
|
||||||
if(uuidMinion == null) {
|
if(uuidMinion == null) {
|
||||||
uuidMinion = UUID.randomUUID();
|
uuidMinion = UUID.randomUUID();
|
||||||
}
|
}
|
||||||
|
|
||||||
GameProfile newProfile = new GameProfile(uuidMinion, username);
|
GameProfile newProfile = new GameProfile(uuidMinion, username, skin != null ? skin : PropertyMap.EMPTY);
|
||||||
if (skin != null) {
|
|
||||||
newProfile.getProperties().putAll(skin);
|
|
||||||
}
|
|
||||||
LOGGER.info("Minion Profile: {}", newProfile);
|
LOGGER.info("Minion Profile: {}", newProfile);
|
||||||
return newProfile;
|
return newProfile;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Result<String, Text> checkMinionNameWithoutPrefix(String name) {
|
public static Result<String, Component> checkMinionNameWithoutPrefix(MinecraftServer server, String name) {
|
||||||
for(char c : name.toCharArray()) {
|
for(char c : name.toCharArray()) {
|
||||||
if(!StringReader.isAllowedInUnquotedString(c)) {
|
if(!StringReader.isAllowedInUnquotedString(c)) {
|
||||||
return new Result.Error<>(Text.translatable("minions.generic.name.invalid_char"));
|
return new Result.Error<>(Component.translatable("minions.generic.name.invalid_char"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if((PREFIX + name).length() > 16) {
|
if((getPrefix() + name).length() > 16) {
|
||||||
return new Result.Error<>(Text.translatable("minions.generic.name.too_long"));
|
return new Result.Error<>(Component.translatable("minions.generic.name.too_long"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!StringHelper.isValidPlayerName(PREFIX + name)) {
|
if(!StringUtil.isValidPlayerName(getPrefix() + name)) {
|
||||||
return new Result.Error<>(Text.translatable("minions.generic.name.invalid"));
|
return new Result.Error<>(Component.translatable("minions.generic.name.invalid"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if(MinionPersistentState.INSTANCE.isMinionNameTaken(PREFIX + name)) {
|
if(MinionPersistentState.get(server).isMinionNameTaken(getPrefix() + name)) {
|
||||||
return new Result.Error<>(Text.translatable("minions.generic.name.taken"));
|
return new Result.Error<>(Component.translatable("minions.generic.name.taken"));
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Result.Success<>(name);
|
return new Result.Success<>(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String newDefaultMinionName() {
|
public static String newDefaultMinionName(MinecraftServer server) {
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while (MinionPersistentState.INSTANCE.isMinionNameTaken("+Minion" + i)) {
|
while (MinionPersistentState.get(server).isMinionNameTaken("+Minion" + i)) {
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
return "+Minion" + i;
|
return "+Minion" + i;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isMinion(UUID uuid) {
|
public static boolean isMinion(MinecraftServer server, UUID uuid) {
|
||||||
return MinionPersistentState.INSTANCE.isMinion(uuid);
|
return MinionPersistentState.get(server).isMinion(uuid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,146 @@
|
|||||||
|
package io.github.skippyall.minions.minion;
|
||||||
|
|
||||||
|
import io.github.skippyall.minions.Minions;
|
||||||
|
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
|
||||||
|
import io.github.skippyall.minions.program.InstructionRuntime;
|
||||||
|
import io.github.skippyall.minions.program.consumer.ValueConsumerType;
|
||||||
|
import io.github.skippyall.minions.program.instruction.ConfiguredInstruction;
|
||||||
|
import io.github.skippyall.minions.program.instruction.InstructionType;
|
||||||
|
import io.github.skippyall.minions.program.supplier.ValueSupplierType;
|
||||||
|
import io.github.skippyall.minions.registration.MinionRegistries;
|
||||||
|
import net.minecraft.core.Registry;
|
||||||
|
import net.minecraft.world.level.storage.ValueInput;
|
||||||
|
import net.minecraft.world.level.storage.ValueOutput;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class MinionRuntime implements InstructionRuntime<MinionRuntime> {
|
||||||
|
private final MinionFakePlayer minion;
|
||||||
|
private final Map<String, ConfiguredInstruction<MinionRuntime>> configuredInstructions = new HashMap<>();
|
||||||
|
|
||||||
|
public MinionRuntime(MinionFakePlayer minion) {
|
||||||
|
this.minion = minion;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MinionFakePlayer getMinion() {
|
||||||
|
return minion;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void tick() {
|
||||||
|
for (ConfiguredInstruction<MinionRuntime> instruction : configuredInstructions.values()) {
|
||||||
|
instruction.tick(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void disableInstructionType(InstructionType<MinionRuntime> instructionType) {
|
||||||
|
updatePausedStatus(instructionType);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void enableInstructionType(InstructionType<MinionRuntime> instructionType) {
|
||||||
|
updatePausedStatus(instructionType);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updatePausedStatus(InstructionType<MinionRuntime> instructionType) {
|
||||||
|
for(ConfiguredInstruction<MinionRuntime> instruction : configuredInstructions.values()) {
|
||||||
|
if(instruction.getInstruction() == instructionType) {
|
||||||
|
instruction.updatePauseStatus(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isInstructionEnabled(InstructionType<MinionRuntime> type) {
|
||||||
|
return minion.getModuleInventory().hasInstruction(type);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<String> getInstructionNames() {
|
||||||
|
return configuredInstructions.keySet();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ConfiguredInstruction<MinionRuntime> createInstruction(String name, InstructionType<MinionRuntime> instructionType) {
|
||||||
|
if(configuredInstructions.containsKey(name)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
ConfiguredInstruction<MinionRuntime> instruction = new ConfiguredInstruction<>(instructionType);
|
||||||
|
configuredInstructions.put(name, instruction);
|
||||||
|
minion.forEachMinionListener(listener -> listener.onInstructionsUpdate(minion));
|
||||||
|
return instruction;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeInstruction(String name) {
|
||||||
|
ConfiguredInstruction<MinionRuntime> instruction = getInstruction(name);
|
||||||
|
instruction.stop(this);
|
||||||
|
configuredInstructions.remove(name);
|
||||||
|
|
||||||
|
instruction.onInstructionRemove();
|
||||||
|
minion.forEachMinionListener(listener -> listener.onInstructionsUpdate(minion));
|
||||||
|
}
|
||||||
|
|
||||||
|
public ConfiguredInstruction<MinionRuntime> getInstruction(String name) {
|
||||||
|
return configuredInstructions.get(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasInstruction(String name) {
|
||||||
|
return configuredInstructions.containsKey(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setInstructionName(String oldName, String newName) {
|
||||||
|
if(!configuredInstructions.containsKey(newName) && configuredInstructions.containsKey(oldName)) {
|
||||||
|
ConfiguredInstruction<MinionRuntime> instruction = configuredInstructions.get(oldName);
|
||||||
|
configuredInstructions.remove(oldName);
|
||||||
|
configuredInstructions.put(newName, instruction);
|
||||||
|
|
||||||
|
|
||||||
|
minion.forEachMinionListener(minionListener -> {
|
||||||
|
minionListener.onInstructionRename(minion, instruction, oldName, newName);
|
||||||
|
minionListener.onInstructionsUpdate(minion);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void save(ValueOutput view) {
|
||||||
|
ValueOutput.ValueOutputList list = view.childrenList("configuredInstructions");
|
||||||
|
for (Map.Entry<String, ConfiguredInstruction<MinionRuntime>> instruction : configuredInstructions.entrySet()) {
|
||||||
|
ValueOutput inner = list.addChild();
|
||||||
|
inner.putString("name", instruction.getKey());
|
||||||
|
instruction.getValue().save(inner, this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void load(ValueInput view) {
|
||||||
|
ValueInput.ValueInputList list = view.childrenListOrEmpty("configuredInstructions");
|
||||||
|
for (ValueInput inner : list) {
|
||||||
|
Optional<String> name = inner.getString("name");
|
||||||
|
if(name.isEmpty()) {
|
||||||
|
Minions.LOGGER.error("Tried deserializing configured instruction without a name of minion \"{}\":", minion.getGameProfile().name());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
ConfiguredInstruction<MinionRuntime> instruction = ConfiguredInstruction.load(inner, this);
|
||||||
|
configuredInstructions.put(name.get(), instruction);
|
||||||
|
} catch (Exception e) {
|
||||||
|
Minions.LOGGER.error("Could not deserialize configured instruction \"{}\" of minion \"{}\":", name.get(), minion.getGameProfile().name(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Registry<ValueSupplierType<MinionRuntime>> getArgumentTypeRegistry() {
|
||||||
|
return MinionRegistries.VALUE_SUPPLIER_TYPES;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Registry<InstructionType<MinionRuntime>> getInstructionTypeRegistry() {
|
||||||
|
return MinionRegistries.INSTRUCTION_TYPES;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Registry<ValueConsumerType<MinionRuntime>> getValueConsumerTypeRegistry() {
|
||||||
|
return MinionRegistries.VALUE_CONSUMER_TYPES;
|
||||||
|
}
|
||||||
|
}
|
||||||
+147
-128
@@ -2,44 +2,44 @@
|
|||||||
package io.github.skippyall.minions.minion.fakeplayer;
|
package io.github.skippyall.minions.minion.fakeplayer;
|
||||||
|
|
||||||
|
|
||||||
|
import io.github.skippyall.minions.mixins.EntityAccessor;
|
||||||
|
import net.minecraft.commands.arguments.EntityAnchorArgument;
|
||||||
|
import net.minecraft.core.BlockPos;
|
||||||
|
import net.minecraft.core.Direction;
|
||||||
|
import net.minecraft.network.protocol.game.ClientboundSetHeldSlotPacket;
|
||||||
|
import net.minecraft.network.protocol.game.ServerboundPlayerActionPacket;
|
||||||
|
import net.minecraft.server.level.ServerLevel;
|
||||||
|
import net.minecraft.server.level.ServerPlayer;
|
||||||
|
import net.minecraft.util.Mth;
|
||||||
|
import net.minecraft.world.InteractionHand;
|
||||||
|
import net.minecraft.world.InteractionResult;
|
||||||
|
import net.minecraft.world.entity.Entity;
|
||||||
|
import net.minecraft.world.entity.animal.equine.AbstractHorse;
|
||||||
|
import net.minecraft.world.entity.decoration.ItemFrame;
|
||||||
|
import net.minecraft.world.entity.player.Inventory;
|
||||||
|
import net.minecraft.world.entity.vehicle.boat.Boat;
|
||||||
|
import net.minecraft.world.entity.vehicle.minecart.Minecart;
|
||||||
|
import net.minecraft.world.item.ItemStack;
|
||||||
|
import net.minecraft.world.level.block.state.BlockState;
|
||||||
|
import net.minecraft.world.phys.BlockHitResult;
|
||||||
|
import net.minecraft.world.phys.EntityHitResult;
|
||||||
|
import net.minecraft.world.phys.HitResult;
|
||||||
|
import net.minecraft.world.phys.Vec2;
|
||||||
|
import net.minecraft.world.phys.Vec3;
|
||||||
|
|
||||||
import java.util.EnumMap;
|
import java.util.EnumMap;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import io.github.skippyall.minions.mixins.EntityAccessor;
|
|
||||||
import net.minecraft.block.BlockState;
|
|
||||||
import net.minecraft.command.argument.EntityAnchorArgumentType;
|
|
||||||
import net.minecraft.entity.Entity;
|
|
||||||
import net.minecraft.entity.decoration.ItemFrameEntity;
|
|
||||||
import net.minecraft.entity.passive.AbstractHorseEntity;
|
|
||||||
import net.minecraft.entity.player.PlayerInventory;
|
|
||||||
import net.minecraft.entity.vehicle.BoatEntity;
|
|
||||||
import net.minecraft.entity.vehicle.MinecartEntity;
|
|
||||||
import net.minecraft.item.ItemStack;
|
|
||||||
import net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket;
|
|
||||||
import net.minecraft.network.packet.s2c.play.UpdateSelectedSlotS2CPacket;
|
|
||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
|
||||||
import net.minecraft.server.world.ServerWorld;
|
|
||||||
import net.minecraft.util.ActionResult;
|
|
||||||
import net.minecraft.util.Hand;
|
|
||||||
import net.minecraft.util.hit.BlockHitResult;
|
|
||||||
import net.minecraft.util.hit.EntityHitResult;
|
|
||||||
import net.minecraft.util.hit.HitResult;
|
|
||||||
import net.minecraft.util.math.BlockPos;
|
|
||||||
import net.minecraft.util.math.Direction;
|
|
||||||
import net.minecraft.util.math.MathHelper;
|
|
||||||
import net.minecraft.util.math.Vec2f;
|
|
||||||
import net.minecraft.util.math.Vec3d;
|
|
||||||
|
|
||||||
public class EntityPlayerActionPack
|
public class EntityPlayerActionPack
|
||||||
{
|
{
|
||||||
private final ServerPlayerEntity player;
|
private final MinionFakePlayer player;
|
||||||
|
|
||||||
private final Map<ActionType, Action> actions = new EnumMap<>(ActionType.class);
|
private final Map<ActionType, Action> actions = new EnumMap<>(ActionType.class);
|
||||||
|
|
||||||
private BlockPos currentBlock;
|
private BlockPos currentBlock;
|
||||||
private int blockHitDelay;
|
public int blockHitDelay;
|
||||||
private boolean isHittingBlock;
|
private boolean isHittingBlock;
|
||||||
private float curBlockDamageMP;
|
private float curBlockDamageMP;
|
||||||
|
|
||||||
@@ -50,7 +50,7 @@ public class EntityPlayerActionPack
|
|||||||
|
|
||||||
private int itemUseCooldown;
|
private int itemUseCooldown;
|
||||||
|
|
||||||
public EntityPlayerActionPack(ServerPlayerEntity playerIn)
|
public EntityPlayerActionPack(MinionFakePlayer playerIn)
|
||||||
{
|
{
|
||||||
player = playerIn;
|
player = playerIn;
|
||||||
stopAll();
|
stopAll();
|
||||||
@@ -71,6 +71,14 @@ public class EntityPlayerActionPack
|
|||||||
itemUseCooldown = other.itemUseCooldown;
|
itemUseCooldown = other.itemUseCooldown;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Action getAction(ActionType type) {
|
||||||
|
return actions.get(type);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasAction(ActionType type) {
|
||||||
|
return actions.containsKey(type);
|
||||||
|
}
|
||||||
|
|
||||||
public EntityPlayerActionPack start(ActionType type, Action action)
|
public EntityPlayerActionPack start(ActionType type, Action action)
|
||||||
{
|
{
|
||||||
Action previous = actions.remove(type);
|
Action previous = actions.remove(type);
|
||||||
@@ -92,7 +100,7 @@ public class EntityPlayerActionPack
|
|||||||
public EntityPlayerActionPack setSneaking(boolean doSneak)
|
public EntityPlayerActionPack setSneaking(boolean doSneak)
|
||||||
{
|
{
|
||||||
sneaking = doSneak;
|
sneaking = doSneak;
|
||||||
player.setSneaking(doSneak);
|
player.setShiftKeyDown(doSneak);
|
||||||
if (sprinting && sneaking)
|
if (sprinting && sneaking)
|
||||||
setSprinting(false);
|
setSprinting(false);
|
||||||
return this;
|
return this;
|
||||||
@@ -124,35 +132,35 @@ public class EntityPlayerActionPack
|
|||||||
case SOUTH -> look(0, 0);
|
case SOUTH -> look(0, 0);
|
||||||
case EAST -> look(-90, 0);
|
case EAST -> look(-90, 0);
|
||||||
case WEST -> look(90, 0);
|
case WEST -> look(90, 0);
|
||||||
case UP -> look(player.getYaw(), -90);
|
case UP -> look(player.getYRot(), -90);
|
||||||
case DOWN -> look(player.getYaw(), 90);
|
case DOWN -> look(player.getYRot(), 90);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
public EntityPlayerActionPack look(Vec2f rotation)
|
public EntityPlayerActionPack look(Vec2 rotation)
|
||||||
{
|
{
|
||||||
return look(rotation.x, rotation.y);
|
return look(rotation.x, rotation.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
public EntityPlayerActionPack look(float yaw, float pitch)
|
public EntityPlayerActionPack look(float yaw, float pitch)
|
||||||
{
|
{
|
||||||
player.setYaw(yaw % 360); //setYaw
|
player.setYRot(yaw % 360); //setYaw
|
||||||
player.setPitch(MathHelper.clamp(pitch, -90, 90)); // setPitch
|
player.setXRot(Mth.clamp(pitch, -90, 90)); // setPitch
|
||||||
// maybe player.moveTo(player.getX(), player.getY(), player.getZ(), yaw, Mth.clamp(pitch,-90.0F, 90.0F));
|
// maybe player.moveTo(player.getX(), player.getY(), player.getZ(), yaw, Mth.clamp(pitch,-90.0F, 90.0F));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public EntityPlayerActionPack lookAt(Vec3d position)
|
public EntityPlayerActionPack lookAt(Vec3 position)
|
||||||
{
|
{
|
||||||
player.lookAt(EntityAnchorArgumentType.EntityAnchor.EYES, position);
|
player.lookAt(EntityAnchorArgument.Anchor.EYES, position);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public EntityPlayerActionPack turn(float yaw, float pitch)
|
public EntityPlayerActionPack turn(float yaw, float pitch)
|
||||||
{
|
{
|
||||||
return look(player.getYaw() + yaw, player.getPitch() + pitch);
|
return look(player.getYRot() + yaw, player.getXRot() + pitch);
|
||||||
}
|
}
|
||||||
|
|
||||||
public EntityPlayerActionPack turn(Vec2f rotation)
|
public EntityPlayerActionPack turn(Vec2 rotation)
|
||||||
{
|
{
|
||||||
return turn(rotation.x, rotation.y);
|
return turn(rotation.x, rotation.y);
|
||||||
}
|
}
|
||||||
@@ -181,12 +189,12 @@ public class EntityPlayerActionPack
|
|||||||
List<Entity> entities;
|
List<Entity> entities;
|
||||||
if (onlyRideables)
|
if (onlyRideables)
|
||||||
{
|
{
|
||||||
entities = player.getWorld().getOtherEntities(player, player.getBoundingBox().expand(3.0D, 1.0D, 3.0D),
|
entities = player.level().getEntities(player, player.getBoundingBox().inflate(3.0D, 1.0D, 3.0D),
|
||||||
e -> (e instanceof MinecartEntity || e instanceof BoatEntity || e instanceof AbstractHorseEntity) && ((EntityAccessor)e).invokeCanAddPassenger(player));
|
e -> (e instanceof Minecart || e instanceof Boat || e instanceof AbstractHorse) && ((EntityAccessor)e).minions$canAddPassenger(player));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
entities = player.getWorld().getOtherEntities(player, player.getBoundingBox().expand(3.0D, 1.0D, 3.0D));
|
entities = player.level().getEntities(player, player.getBoundingBox().inflate(3.0D, 1.0D, 3.0D));
|
||||||
}
|
}
|
||||||
if (entities.size()==0)
|
if (entities.size()==0)
|
||||||
return this;
|
return this;
|
||||||
@@ -197,7 +205,7 @@ public class EntityPlayerActionPack
|
|||||||
{
|
{
|
||||||
if (e == player || (currentVehicle == e))
|
if (e == player || (currentVehicle == e))
|
||||||
continue;
|
continue;
|
||||||
double dd = player.squaredDistanceTo(e);
|
double dd = player.distanceToSqr(e);
|
||||||
if (dd<distance)
|
if (dd<distance)
|
||||||
{
|
{
|
||||||
distance = dd;
|
distance = dd;
|
||||||
@@ -205,10 +213,10 @@ public class EntityPlayerActionPack
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (closest == null) return this;
|
if (closest == null) return this;
|
||||||
if (closest instanceof AbstractHorseEntity && onlyRideables)
|
if (closest instanceof AbstractHorse && onlyRideables)
|
||||||
((AbstractHorseEntity) closest).interactMob(player, Hand.MAIN_HAND);
|
((AbstractHorse) closest).mobInteract(player, InteractionHand.MAIN_HAND);
|
||||||
else
|
else
|
||||||
player.startRiding(closest, !onlyRideables);
|
player.startRiding(closest, !onlyRideables, true);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
public EntityPlayerActionPack dismount()
|
public EntityPlayerActionPack dismount()
|
||||||
@@ -248,48 +256,52 @@ public class EntityPlayerActionPack
|
|||||||
float vel = sneaking?0.3F:1.0F;
|
float vel = sneaking?0.3F:1.0F;
|
||||||
// The != 0.0F checks are needed given else real players can't control minecarts, however it works with fakes and else they don't stop immediately
|
// The != 0.0F checks are needed given else real players can't control minecarts, however it works with fakes and else they don't stop immediately
|
||||||
if (forward != 0.0F || player instanceof MinionFakePlayer) {
|
if (forward != 0.0F || player instanceof MinionFakePlayer) {
|
||||||
player.forwardSpeed = forward * vel;
|
player.zza = forward * vel;
|
||||||
}
|
}
|
||||||
if (strafing != 0.0F || player instanceof MinionFakePlayer) {
|
if (strafing != 0.0F || player instanceof MinionFakePlayer) {
|
||||||
player.sidewaysSpeed = strafing * vel;
|
player.xxa = strafing * vel;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(blockHitDelay > 0) {
|
||||||
|
blockHitDelay--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static HitResult getTarget(ServerPlayerEntity player)
|
public static HitResult getTarget(ServerPlayer player)
|
||||||
{
|
{
|
||||||
double reach = player.interactionManager.isCreative() ? 5 : 4.5f;
|
double reach = player.gameMode.isCreative() ? 5 : 4.5f;
|
||||||
return Tracer.rayTrace(player, 1, reach, false);
|
return Tracer.rayTrace(player, 1, reach, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void dropItemFromSlot(int slot, boolean dropAll)
|
private void dropItemFromSlot(int slot, boolean dropAll)
|
||||||
{
|
{
|
||||||
PlayerInventory inv = player.getInventory(); // getInventory;
|
Inventory inv = player.getInventory(); // getInventory;
|
||||||
if (!inv.getStack(slot).isEmpty())
|
if (!inv.getItem(slot).isEmpty())
|
||||||
player.dropItem(inv.removeStack(slot,
|
player.drop(inv.removeItem(slot,
|
||||||
dropAll ? inv.getStack(slot).getCount() : 1
|
dropAll ? inv.getItem(slot).getCount() : 1
|
||||||
), false, true); // scatter, keep owner
|
), false, true); // scatter, keep owner
|
||||||
}
|
}
|
||||||
|
|
||||||
public void drop(int selectedSlot, boolean dropAll)
|
public void drop(int selectedSlot, boolean dropAll)
|
||||||
{
|
{
|
||||||
PlayerInventory inv = player.getInventory(); // getInventory;
|
Inventory inv = player.getInventory(); // getInventory;
|
||||||
if (selectedSlot == -2) // all
|
if (selectedSlot == -2) // all
|
||||||
{
|
{
|
||||||
for (int i = inv.size(); i >= 0; i--)
|
for (int i = inv.getContainerSize(); i >= 0; i--)
|
||||||
dropItemFromSlot(i, dropAll);
|
dropItemFromSlot(i, dropAll);
|
||||||
}
|
}
|
||||||
else // one slot
|
else // one slot
|
||||||
{
|
{
|
||||||
if (selectedSlot == -1)
|
if (selectedSlot == -1)
|
||||||
selectedSlot = inv.selectedSlot;
|
selectedSlot = inv.getSelectedSlot();
|
||||||
dropItemFromSlot(selectedSlot, dropAll);
|
dropItemFromSlot(selectedSlot, dropAll);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSlot(int slot)
|
public void setSlot(int slot)
|
||||||
{
|
{
|
||||||
player.getInventory().selectedSlot = slot-1;
|
player.getInventory().setSelectedSlot(slot-1);
|
||||||
player.networkHandler.sendPacket(new UpdateSelectedSlotS2CPacket(slot-1));
|
player.connection.send(new ClientboundSetHeldSlotPacket(slot-1));
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum ActionType
|
public enum ActionType
|
||||||
@@ -297,9 +309,9 @@ public class EntityPlayerActionPack
|
|||||||
USE(true)
|
USE(true)
|
||||||
{
|
{
|
||||||
@Override
|
@Override
|
||||||
boolean execute(ServerPlayerEntity player, Action action)
|
boolean execute(MinionFakePlayer player, Action action)
|
||||||
{
|
{
|
||||||
EntityPlayerActionPack ap = ((ServerPlayerInterface) player).minions$getActionPack();
|
EntityPlayerActionPack ap = player.getMinionActionPack();
|
||||||
if (ap.itemUseCooldown > 0)
|
if (ap.itemUseCooldown > 0)
|
||||||
{
|
{
|
||||||
ap.itemUseCooldown--;
|
ap.itemUseCooldown--;
|
||||||
@@ -310,23 +322,23 @@ public class EntityPlayerActionPack
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
HitResult hit = getTarget(player);
|
HitResult hit = getTarget(player);
|
||||||
for (Hand hand : Hand.values())
|
for (InteractionHand hand : InteractionHand.values())
|
||||||
{
|
{
|
||||||
switch (hit.getType())
|
switch (hit.getType())
|
||||||
{
|
{
|
||||||
case BLOCK:
|
case BLOCK:
|
||||||
{
|
{
|
||||||
player.updateLastActionTime();
|
player.resetLastActionTime();
|
||||||
ServerWorld world = player.getServerWorld();
|
ServerLevel world = player.level();
|
||||||
BlockHitResult blockHit = (BlockHitResult) hit;
|
BlockHitResult blockHit = (BlockHitResult) hit;
|
||||||
BlockPos pos = blockHit.getBlockPos();
|
BlockPos pos = blockHit.getBlockPos();
|
||||||
Direction side = blockHit.getSide();
|
Direction side = blockHit.getDirection();
|
||||||
if (pos.getY() < player.getServerWorld().getTopYInclusive() - (side == Direction.UP ? 1 : 0) && world.canPlayerModifyAt(player, pos))
|
if (pos.getY() < player.level().getMaxY() - (side == Direction.UP ? 1 : 0) && world.mayInteract(player, pos))
|
||||||
{
|
{
|
||||||
ActionResult result = player.interactionManager.interactBlock(player, world, player.getStackInHand(hand), hand, blockHit);
|
InteractionResult result = player.gameMode.useItemOn(player, world, player.getItemInHand(hand), hand, blockHit);
|
||||||
if (result instanceof ActionResult.Success success)
|
if (result instanceof InteractionResult.Success success)
|
||||||
{
|
{
|
||||||
if (success.swingSource() == ActionResult.SwingSource.SERVER) player.swingHand(hand);
|
if (success.swingSource() == InteractionResult.SwingSource.SERVER) player.swing(hand);
|
||||||
ap.itemUseCooldown = 3;
|
ap.itemUseCooldown = 3;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -335,19 +347,19 @@ public class EntityPlayerActionPack
|
|||||||
}
|
}
|
||||||
case ENTITY:
|
case ENTITY:
|
||||||
{
|
{
|
||||||
player.updateLastActionTime();
|
player.resetLastActionTime();
|
||||||
EntityHitResult entityHit = (EntityHitResult) hit;
|
EntityHitResult entityHit = (EntityHitResult) hit;
|
||||||
Entity entity = entityHit.getEntity();
|
Entity entity = entityHit.getEntity();
|
||||||
boolean handWasEmpty = player.getStackInHand(hand).isEmpty();
|
boolean handWasEmpty = player.getItemInHand(hand).isEmpty();
|
||||||
boolean itemFrameEmpty = (entity instanceof ItemFrameEntity) && ((ItemFrameEntity) entity).getHeldItemStack().isEmpty();
|
boolean itemFrameEmpty = (entity instanceof ItemFrame) && ((ItemFrame) entity).getItem().isEmpty();
|
||||||
Vec3d relativeHitPos = entityHit.getPos().subtract(entity.getX(), entity.getY(), entity.getZ());
|
Vec3 relativeHitPos = entityHit.getLocation().subtract(entity.getX(), entity.getY(), entity.getZ());
|
||||||
if (entity.interactAt(player, relativeHitPos, hand).isAccepted())
|
if (entity.interact(player, hand, relativeHitPos).consumesAction())
|
||||||
{
|
{
|
||||||
ap.itemUseCooldown = 3;
|
ap.itemUseCooldown = 3;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
// fix for SS itemframe always returns CONSUME even if no action is performed
|
// fix for SS itemframe always returns CONSUME even if no action is performed
|
||||||
if (player.interact(entity, hand).isAccepted() && !(handWasEmpty && itemFrameEmpty))
|
if (player.interactOn(entity, hand, relativeHitPos).consumesAction() && !(handWasEmpty && itemFrameEmpty))
|
||||||
{
|
{
|
||||||
ap.itemUseCooldown = 3;
|
ap.itemUseCooldown = 3;
|
||||||
return true;
|
return true;
|
||||||
@@ -355,8 +367,8 @@ public class EntityPlayerActionPack
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ItemStack handItem = player.getStackInHand(hand);
|
ItemStack handItem = player.getItemInHand(hand);
|
||||||
if (player.interactionManager.interactItem(player, player.getWorld(), handItem, hand).isAccepted())
|
if (player.gameMode.useItem(player, player.level(), handItem, hand).consumesAction())
|
||||||
{
|
{
|
||||||
ap.itemUseCooldown = 3;
|
ap.itemUseCooldown = 3;
|
||||||
return true;
|
return true;
|
||||||
@@ -366,50 +378,49 @@ public class EntityPlayerActionPack
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
void inactiveTick(ServerPlayerEntity player, Action action)
|
void inactiveTick(MinionFakePlayer player, Action action)
|
||||||
{
|
{
|
||||||
EntityPlayerActionPack ap = ((ServerPlayerInterface) player).minions$getActionPack();
|
EntityPlayerActionPack ap = player.getMinionActionPack();
|
||||||
ap.itemUseCooldown = 0;
|
ap.itemUseCooldown = 0;
|
||||||
player.stopUsingItem();
|
player.releaseUsingItem();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
ATTACK(true) {
|
ATTACK(true) {
|
||||||
@Override
|
@Override
|
||||||
boolean execute(ServerPlayerEntity player, Action action) {
|
boolean execute(MinionFakePlayer player, Action action) {
|
||||||
HitResult hit = getTarget(player);
|
HitResult hit = getTarget(player);
|
||||||
switch (hit.getType()) {
|
switch (hit.getType()) {
|
||||||
case ENTITY: {
|
case ENTITY: {
|
||||||
EntityHitResult entityHit = (EntityHitResult) hit;
|
EntityHitResult entityHit = (EntityHitResult) hit;
|
||||||
if (!action.isContinuous)
|
if (!action.isContinuous || action.first)
|
||||||
{
|
{
|
||||||
player.attack(entityHit.getEntity());
|
player.attack(entityHit.getEntity());
|
||||||
player.swingHand(Hand.MAIN_HAND);
|
player.swing(InteractionHand.MAIN_HAND);
|
||||||
}
|
}
|
||||||
player.resetLastAttackedTicks();
|
player.resetAttackStrengthTicker();
|
||||||
player.updateLastActionTime();
|
player.resetLastActionTime();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
case BLOCK: {
|
case BLOCK: {
|
||||||
EntityPlayerActionPack ap = ((ServerPlayerInterface) player).minions$getActionPack();
|
EntityPlayerActionPack ap = player.getMinionActionPack();
|
||||||
if (ap.blockHitDelay > 0)
|
if (ap.blockHitDelay > 0)
|
||||||
{
|
{
|
||||||
ap.blockHitDelay--;
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
BlockHitResult blockHit = (BlockHitResult) hit;
|
BlockHitResult blockHit = (BlockHitResult) hit;
|
||||||
BlockPos pos = blockHit.getBlockPos();
|
BlockPos pos = blockHit.getBlockPos();
|
||||||
Direction side = blockHit.getSide();
|
Direction side = blockHit.getDirection();
|
||||||
if (player.isBlockBreakingRestricted(player.getWorld(), pos, player.interactionManager.getGameMode())) return false;
|
if (player.blockActionRestricted(player.level(), pos, player.gameMode.getGameModeForPlayer())) return false;
|
||||||
if (ap.currentBlock != null && player.getWorld().getBlockState(ap.currentBlock).isAir())
|
if (ap.currentBlock != null && player.level().getBlockState(ap.currentBlock).isAir())
|
||||||
{
|
{
|
||||||
ap.currentBlock = null;
|
ap.currentBlock = null;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
BlockState state = player.getWorld().getBlockState(pos);
|
BlockState state = player.level().getBlockState(pos);
|
||||||
boolean blockBroken = false;
|
boolean blockBroken = false;
|
||||||
if (player.interactionManager.getGameMode().isCreative())
|
if (player.gameMode.getGameModeForPlayer().isCreative())
|
||||||
{
|
{
|
||||||
player.interactionManager.processBlockBreakingAction(pos, PlayerActionC2SPacket.Action.START_DESTROY_BLOCK, side, player.getWorld().getTopYInclusive(), -1);
|
player.gameMode.handleBlockBreakAction(pos, ServerboundPlayerActionPacket.Action.START_DESTROY_BLOCK, side, player.level().getMaxY(), -1);
|
||||||
ap.blockHitDelay = 5;
|
ap.blockHitDelay = 5;
|
||||||
blockBroken = true;
|
blockBroken = true;
|
||||||
}
|
}
|
||||||
@@ -417,15 +428,15 @@ public class EntityPlayerActionPack
|
|||||||
{
|
{
|
||||||
if (ap.currentBlock != null)
|
if (ap.currentBlock != null)
|
||||||
{
|
{
|
||||||
player.interactionManager.processBlockBreakingAction(ap.currentBlock, PlayerActionC2SPacket.Action.ABORT_DESTROY_BLOCK, side, player.getWorld().getTopYInclusive(), -1);
|
player.gameMode.handleBlockBreakAction(ap.currentBlock, ServerboundPlayerActionPacket.Action.ABORT_DESTROY_BLOCK, side, player.level().getMaxY(), -1);
|
||||||
}
|
}
|
||||||
player.interactionManager.processBlockBreakingAction(pos, PlayerActionC2SPacket.Action.START_DESTROY_BLOCK, side, player.getWorld().getTopYInclusive(), -1);
|
player.gameMode.handleBlockBreakAction(pos, ServerboundPlayerActionPacket.Action.START_DESTROY_BLOCK, side, player.level().getMaxY(), -1);
|
||||||
boolean notAir = !state.isAir();
|
boolean notAir = !state.isAir();
|
||||||
if (notAir && ap.curBlockDamageMP == 0)
|
if (notAir && ap.curBlockDamageMP == 0)
|
||||||
{
|
{
|
||||||
state.onBlockBreakStart(player.getWorld(), pos, player);
|
state.attack(player.level(), pos, player);
|
||||||
}
|
}
|
||||||
if (notAir && state.calcBlockBreakingDelta(player, player.getWorld(), pos) >= 1)
|
if (notAir && state.getDestroyProgress(player, player.level(), pos) >= 1)
|
||||||
{
|
{
|
||||||
ap.currentBlock = null;
|
ap.currentBlock = null;
|
||||||
//instamine??
|
//instamine??
|
||||||
@@ -439,19 +450,19 @@ public class EntityPlayerActionPack
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ap.curBlockDamageMP += state.calcBlockBreakingDelta(player, player.getWorld(), pos);
|
ap.curBlockDamageMP += state.getDestroyProgress(player, player.level(), pos);
|
||||||
if (ap.curBlockDamageMP >= 1)
|
if (ap.curBlockDamageMP >= 1)
|
||||||
{
|
{
|
||||||
player.interactionManager.processBlockBreakingAction(pos, PlayerActionC2SPacket.Action.STOP_DESTROY_BLOCK, side, player.getWorld().getTopYInclusive(), -1);
|
player.gameMode.handleBlockBreakAction(pos, ServerboundPlayerActionPacket.Action.STOP_DESTROY_BLOCK, side, player.level().getMaxY(), -1);
|
||||||
ap.currentBlock = null;
|
ap.currentBlock = null;
|
||||||
ap.blockHitDelay = 5;
|
ap.blockHitDelay = 5;
|
||||||
blockBroken = true;
|
blockBroken = true;
|
||||||
}
|
}
|
||||||
player.getWorld().setBlockBreakingInfo(-1, pos, (int) (ap.curBlockDamageMP * 10));
|
player.level().destroyBlockProgress(-1, pos, (int) (ap.curBlockDamageMP * 10));
|
||||||
|
|
||||||
}
|
}
|
||||||
player.updateLastActionTime();
|
player.resetLastActionTime();
|
||||||
player.swingHand(Hand.MAIN_HAND);
|
player.swing(InteractionHand.MAIN_HAND);
|
||||||
return blockBroken;
|
return blockBroken;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -459,23 +470,24 @@ public class EntityPlayerActionPack
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
void inactiveTick(ServerPlayerEntity player, Action action)
|
void inactiveTick(MinionFakePlayer player, Action action)
|
||||||
{
|
{
|
||||||
EntityPlayerActionPack ap = ((ServerPlayerInterface) player).minions$getActionPack();
|
EntityPlayerActionPack ap = player.getMinionActionPack();
|
||||||
if (ap.currentBlock == null) return;
|
if (ap.currentBlock == null) return;
|
||||||
player.getWorld().setBlockBreakingInfo(-1, ap.currentBlock, -1);
|
player.level().destroyBlockProgress(-1, ap.currentBlock, -1);
|
||||||
player.interactionManager.processBlockBreakingAction(ap.currentBlock, PlayerActionC2SPacket.Action.ABORT_DESTROY_BLOCK, Direction.DOWN, player.getWorld().getTopYInclusive(), -1);
|
player.gameMode.handleBlockBreakAction(ap.currentBlock, ServerboundPlayerActionPacket.Action.ABORT_DESTROY_BLOCK, Direction.DOWN, player.level().getMaxY(), -1);
|
||||||
ap.currentBlock = null;
|
ap.currentBlock = null;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
JUMP(true)
|
JUMP(true)
|
||||||
{
|
{
|
||||||
@Override
|
@Override
|
||||||
boolean execute(ServerPlayerEntity player, Action action)
|
boolean execute(MinionFakePlayer player, Action action)
|
||||||
{
|
{
|
||||||
if (action.limit == 1)
|
if (action.limit == 1)
|
||||||
{
|
{
|
||||||
if (player.isOnGround()) player.jump(); // onGround
|
if (player.onGround()) player.jumpFromGround();
|
||||||
|
else if (!player.onClimbable()) player.tryToStartFallFlying();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -485,7 +497,7 @@ public class EntityPlayerActionPack
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
void inactiveTick(ServerPlayerEntity player, Action action)
|
void inactiveTick(MinionFakePlayer player, Action action)
|
||||||
{
|
{
|
||||||
player.setJumping(false);
|
player.setJumping(false);
|
||||||
}
|
}
|
||||||
@@ -493,32 +505,32 @@ public class EntityPlayerActionPack
|
|||||||
DROP_ITEM(true)
|
DROP_ITEM(true)
|
||||||
{
|
{
|
||||||
@Override
|
@Override
|
||||||
boolean execute(ServerPlayerEntity player, Action action)
|
boolean execute(MinionFakePlayer player, Action action)
|
||||||
{
|
{
|
||||||
player.updateLastActionTime();
|
player.resetLastActionTime();
|
||||||
player.dropSelectedItem(false); // dropSelectedItem
|
player.drop(false); // dropSelectedItem
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
DROP_STACK(true)
|
DROP_STACK(true)
|
||||||
{
|
{
|
||||||
@Override
|
@Override
|
||||||
boolean execute(ServerPlayerEntity player, Action action)
|
boolean execute(MinionFakePlayer player, Action action)
|
||||||
{
|
{
|
||||||
player.updateLastActionTime();
|
player.resetLastActionTime();
|
||||||
player.dropSelectedItem(true); // dropSelectedItem
|
player.drop(true); // dropSelectedItem
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
SWAP_HANDS(true)
|
SWAP_HANDS(true)
|
||||||
{
|
{
|
||||||
@Override
|
@Override
|
||||||
boolean execute(ServerPlayerEntity player, Action action)
|
boolean execute(MinionFakePlayer player, Action action)
|
||||||
{
|
{
|
||||||
player.updateLastActionTime();
|
player.resetLastActionTime();
|
||||||
ItemStack itemStack_1 = player.getStackInHand(Hand.OFF_HAND);
|
ItemStack itemStack_1 = player.getItemInHand(InteractionHand.OFF_HAND);
|
||||||
player.setStackInHand(Hand.OFF_HAND, player.getStackInHand(Hand.MAIN_HAND));
|
player.setItemInHand(InteractionHand.OFF_HAND, player.getItemInHand(InteractionHand.MAIN_HAND));
|
||||||
player.setStackInHand(Hand.MAIN_HAND, itemStack_1);
|
player.setItemInHand(InteractionHand.MAIN_HAND, itemStack_1);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -530,10 +542,10 @@ public class EntityPlayerActionPack
|
|||||||
this.preventSpectator = preventSpectator;
|
this.preventSpectator = preventSpectator;
|
||||||
}
|
}
|
||||||
|
|
||||||
void start(ServerPlayerEntity player, Action action) {}
|
void start(MinionFakePlayer player, Action action) {}
|
||||||
abstract boolean execute(ServerPlayerEntity player, Action action);
|
abstract boolean execute(MinionFakePlayer player, Action action);
|
||||||
void inactiveTick(ServerPlayerEntity player, Action action) {}
|
void inactiveTick(MinionFakePlayer player, Action action) {}
|
||||||
void stop(ServerPlayerEntity player, Action action)
|
void stop(MinionFakePlayer player, Action action)
|
||||||
{
|
{
|
||||||
inactiveTick(player, action);
|
inactiveTick(player, action);
|
||||||
}
|
}
|
||||||
@@ -548,8 +560,9 @@ public class EntityPlayerActionPack
|
|||||||
private int count;
|
private int count;
|
||||||
private int next;
|
private int next;
|
||||||
private final boolean isContinuous;
|
private final boolean isContinuous;
|
||||||
|
boolean first = true;
|
||||||
|
|
||||||
private Action(int limit, int interval, int offset, boolean continuous)
|
private Action(int limit, int interval, int offset, boolean continuous, boolean first)
|
||||||
{
|
{
|
||||||
this.limit = limit;
|
this.limit = limit;
|
||||||
this.interval = interval;
|
this.interval = interval;
|
||||||
@@ -560,22 +573,27 @@ public class EntityPlayerActionPack
|
|||||||
|
|
||||||
public static Action once()
|
public static Action once()
|
||||||
{
|
{
|
||||||
return new Action(1, 1, 0, false);
|
return new Action(1, 1, 0, false, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Action startContinuous()
|
||||||
|
{
|
||||||
|
return new Action(-1, 1, 0, true, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Action continuous()
|
public static Action continuous()
|
||||||
{
|
{
|
||||||
return new Action(-1, 1, 0, true);
|
return new Action(-1, 1, 0, true, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Action interval(int interval)
|
public static Action interval(int interval)
|
||||||
{
|
{
|
||||||
return new Action(-1, interval, 0, false);
|
return new Action(-1, interval, 0, false, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Action interval(int interval, int offset)
|
public static Action interval(int interval, int offset)
|
||||||
{
|
{
|
||||||
return new Action(-1, interval, offset, false);
|
return new Action(-1, interval, offset, false, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
Boolean tick(EntityPlayerActionPack actionPack, ActionType type)
|
Boolean tick(EntityPlayerActionPack actionPack, ActionType type)
|
||||||
@@ -606,6 +624,7 @@ public class EntityPlayerActionPack
|
|||||||
return cancel;
|
return cancel;
|
||||||
}
|
}
|
||||||
next = interval;
|
next = interval;
|
||||||
|
first = false;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
+17
-9
@@ -1,14 +1,17 @@
|
|||||||
//code from https://github.com/gnembon/fabric-carpet
|
//code from https://github.com/gnembon/fabric-carpet
|
||||||
package io.github.skippyall.minions.minion.fakeplayer;
|
package io.github.skippyall.minions.minion.fakeplayer;
|
||||||
|
|
||||||
|
import io.netty.channel.ChannelFutureListener;
|
||||||
import io.netty.channel.embedded.EmbeddedChannel;
|
import io.netty.channel.embedded.EmbeddedChannel;
|
||||||
import net.minecraft.network.ClientConnection;
|
import net.minecraft.network.Connection;
|
||||||
import net.minecraft.network.NetworkSide;
|
import net.minecraft.network.PacketListener;
|
||||||
import net.minecraft.network.NetworkState;
|
import net.minecraft.network.ProtocolInfo;
|
||||||
import net.minecraft.network.listener.PacketListener;
|
import net.minecraft.network.protocol.Packet;
|
||||||
|
import net.minecraft.network.protocol.PacketFlow;
|
||||||
|
import org.jspecify.annotations.Nullable;
|
||||||
|
|
||||||
public class FakeClientConnection extends ClientConnection {
|
public class FakeClientConnection extends Connection {
|
||||||
public FakeClientConnection(NetworkSide p)
|
public FakeClientConnection(PacketFlow p)
|
||||||
{
|
{
|
||||||
super(p);
|
super(p);
|
||||||
// compat with adventure-platform-fabric. This does NOT trigger other vanilla handlers for establishing a channel
|
// compat with adventure-platform-fabric. This does NOT trigger other vanilla handlers for establishing a channel
|
||||||
@@ -17,7 +20,12 @@ public class FakeClientConnection extends ClientConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void tryDisableAutoRead()
|
public void send(Packet<?> packet, @Nullable ChannelFutureListener listener, boolean flush) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setReadOnly()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,11 +34,11 @@ public class FakeClientConnection extends ClientConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setInitialPacketListener(PacketListener packetListener)
|
public void setListenerForServerboundHandshake(PacketListener packetListener)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T extends PacketListener> void transitionInbound(NetworkState<T> state, T packetListener) {
|
public <T extends PacketListener> void setupInboundProtocol(ProtocolInfo<T> state, T packetListener) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+212
-185
@@ -4,307 +4,334 @@ package io.github.skippyall.minions.minion.fakeplayer;
|
|||||||
import com.mojang.authlib.GameProfile;
|
import com.mojang.authlib.GameProfile;
|
||||||
import com.mojang.authlib.properties.PropertyMap;
|
import com.mojang.authlib.properties.PropertyMap;
|
||||||
import io.github.skippyall.minions.Minions;
|
import io.github.skippyall.minions.Minions;
|
||||||
import io.github.skippyall.minions.MinionsTickExecutor;
|
import io.github.skippyall.minions.gui.minion.MinionGui;
|
||||||
|
import io.github.skippyall.minions.listener.SerializableListenerManager;
|
||||||
import io.github.skippyall.minions.minion.MinionData;
|
import io.github.skippyall.minions.minion.MinionData;
|
||||||
import io.github.skippyall.minions.gui.MinionGui;
|
|
||||||
import io.github.skippyall.minions.minion.MinionItem;
|
import io.github.skippyall.minions.minion.MinionItem;
|
||||||
|
import io.github.skippyall.minions.minion.MinionListener;
|
||||||
import io.github.skippyall.minions.minion.MinionPersistentState;
|
import io.github.skippyall.minions.minion.MinionPersistentState;
|
||||||
import io.github.skippyall.minions.minion.MinionProfileUtils;
|
import io.github.skippyall.minions.minion.MinionProfileUtils;
|
||||||
import io.github.skippyall.minions.gui.ModuleInventory;
|
import io.github.skippyall.minions.minion.MinionRuntime;
|
||||||
import io.github.skippyall.minions.program.runtime.MinionRuntime;
|
import io.github.skippyall.minions.module.ModuleInventory;
|
||||||
import net.minecraft.block.BlockState;
|
import io.github.skippyall.minions.registration.MinionConfigOptions;
|
||||||
import net.minecraft.block.EndPortalBlock;
|
import io.github.skippyall.minions.registration.MinionItems;
|
||||||
import net.minecraft.entity.Entity;
|
import io.github.skippyall.minions.registration.SpecialAbilities;
|
||||||
import net.minecraft.entity.EquipmentSlot;
|
import net.fabricmc.fabric.impl.networking.context.PacketContextImpl;
|
||||||
import net.minecraft.entity.MovementType;
|
import net.minecraft.core.BlockPos;
|
||||||
import net.minecraft.entity.attribute.EntityAttributes;
|
import net.minecraft.network.DisconnectionDetails;
|
||||||
import net.minecraft.entity.damage.DamageSource;
|
import net.minecraft.network.chat.Component;
|
||||||
import net.minecraft.entity.player.HungerManager;
|
import net.minecraft.network.chat.contents.TranslatableContents;
|
||||||
import net.minecraft.entity.player.PlayerEntity;
|
import net.minecraft.network.protocol.PacketFlow;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.network.protocol.game.ClientboundEntityPositionSyncPacket;
|
||||||
import net.minecraft.nbt.NbtCompound;
|
import net.minecraft.network.protocol.game.ClientboundRotateHeadPacket;
|
||||||
import net.minecraft.network.DisconnectionInfo;
|
import net.minecraft.network.protocol.game.ServerboundClientCommandPacket;
|
||||||
import net.minecraft.network.NetworkSide;
|
|
||||||
import net.minecraft.network.packet.c2s.common.SyncedClientOptions;
|
|
||||||
import net.minecraft.network.packet.c2s.play.ClientStatusC2SPacket;
|
|
||||||
import net.minecraft.network.packet.s2c.play.EntityPositionSyncS2CPacket;
|
|
||||||
import net.minecraft.network.packet.s2c.play.EntitySetHeadYawS2CPacket;
|
|
||||||
import net.minecraft.server.MinecraftServer;
|
import net.minecraft.server.MinecraftServer;
|
||||||
import net.minecraft.server.ServerTask;
|
import net.minecraft.server.TickTask;
|
||||||
import net.minecraft.server.network.ConnectedClientData;
|
import net.minecraft.server.level.ClientInformation;
|
||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
import net.minecraft.server.level.ServerLevel;
|
||||||
import net.minecraft.server.world.ServerWorld;
|
import net.minecraft.server.level.ServerPlayer;
|
||||||
import net.minecraft.text.Text;
|
import net.minecraft.server.network.CommonListenerCookie;
|
||||||
import net.minecraft.text.TranslatableTextContent;
|
import net.minecraft.util.ProblemReporter;
|
||||||
import net.minecraft.util.ActionResult;
|
import net.minecraft.world.InteractionHand;
|
||||||
import net.minecraft.util.Hand;
|
import net.minecraft.world.InteractionResult;
|
||||||
import net.minecraft.util.math.BlockPos;
|
import net.minecraft.world.damagesource.DamageSource;
|
||||||
import net.minecraft.util.math.Vec2f;
|
import net.minecraft.world.entity.Entity;
|
||||||
import net.minecraft.util.math.Vec3d;
|
import net.minecraft.world.entity.EquipmentSlot;
|
||||||
import net.minecraft.world.GameMode;
|
import net.minecraft.world.entity.ai.attributes.Attributes;
|
||||||
import net.minecraft.world.TeleportTarget;
|
import net.minecraft.world.entity.item.ItemEntity;
|
||||||
|
import net.minecraft.world.entity.player.Player;
|
||||||
|
import net.minecraft.world.entity.vehicle.boat.AbstractBoat;
|
||||||
|
import net.minecraft.world.food.FoodData;
|
||||||
|
import net.minecraft.world.item.ItemStack;
|
||||||
|
import net.minecraft.world.level.GameType;
|
||||||
|
import net.minecraft.world.level.block.state.BlockState;
|
||||||
|
import net.minecraft.world.level.portal.TeleportTransition;
|
||||||
|
import net.minecraft.world.level.storage.TagValueInput;
|
||||||
|
import net.minecraft.world.level.storage.ValueInput;
|
||||||
|
import net.minecraft.world.level.storage.ValueOutput;
|
||||||
|
import net.minecraft.world.phys.Vec2;
|
||||||
|
import net.minecraft.world.phys.Vec3;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public class MinionFakePlayer extends ServerPlayerEntity {
|
public class MinionFakePlayer extends ServerPlayer {
|
||||||
public Runnable fixStartingPosition = () -> {};
|
public Runnable fixStartingPosition = () -> {};
|
||||||
|
|
||||||
private float moveForward;
|
private EntityPlayerActionPack actionPack;
|
||||||
private float moveSideways;
|
|
||||||
|
|
||||||
private boolean programmable;
|
private final ModuleInventory moduleInventory = new ModuleInventory(this);
|
||||||
private final ModuleInventory moduleInventory = new ModuleInventory();
|
private final MinionRuntime instructionManager = new MinionRuntime(this);
|
||||||
private final MinionRuntime runtime = new MinionRuntime(this);
|
|
||||||
|
|
||||||
private final MinionData data;
|
|
||||||
|
|
||||||
public static void spawnMinion(MinionData data, ServerWorld level, @Nullable Vec3d pos, @Nullable Vec2f rot) {
|
|
||||||
MinecraftServer server = level.getServer();
|
|
||||||
|
|
||||||
PropertyMap skin = data.skin().orElse(null);
|
|
||||||
|
|
||||||
GameProfile profile = MinionProfileUtils.makeNewMinionProfile(data.uuid(), data.name(), skin);
|
|
||||||
doSpawn(data, profile, server, level, pos, rot);
|
|
||||||
|
|
||||||
|
public static void spawnMinion(MinionData data, ServerLevel level, @Nullable Vec3 pos, @Nullable Vec2 rot) {
|
||||||
|
spawnMinion(data, level, pos, rot, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void doSpawn(MinionData data, GameProfile profile, MinecraftServer server, ServerWorld level, @Nullable Vec3d pos, @Nullable Vec2f rot) {
|
public static void spawnMinion(MinionData data, ServerLevel level, @Nullable Vec3 pos, @Nullable Vec2 rot, boolean force) {
|
||||||
|
if(!data.isSpawned() || force) {
|
||||||
|
MinecraftServer server = level.getServer();
|
||||||
|
|
||||||
MinionFakePlayer instance = new MinionFakePlayer(server, level, profile, SyncedClientOptions.createDefault(), data);
|
PropertyMap skin = data.skin().orElse(null);
|
||||||
MinionPersistentState.INSTANCE.updateMinionData(data.withSpawned(true));
|
|
||||||
|
GameProfile profile = MinionProfileUtils.makeNewMinionProfile(data.uuid(), data.name(), skin);
|
||||||
|
server.schedule(server.wrapRunnable(() -> doSpawn(data, profile, server, level, pos, rot)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void doSpawn(MinionData data, GameProfile profile, MinecraftServer server, ServerLevel level, @Nullable Vec3 pos, @Nullable Vec2 rot) {
|
||||||
|
MinionFakePlayer instance = new MinionFakePlayer(server, level, profile, ClientInformation.createDefault());
|
||||||
|
MinionPersistentState.get(server).updateMinionData(data.withSpawned(true));
|
||||||
|
|
||||||
if(pos != null && rot != null) {
|
if(pos != null && rot != null) {
|
||||||
instance.fixStartingPosition = () -> instance.refreshPositionAndAngles(pos.x, pos.y, pos.z, rot.x, rot.y);
|
instance.fixStartingPosition = () -> instance.snapTo(pos.x, pos.y, pos.z, rot.x, rot.y);
|
||||||
}
|
}
|
||||||
server.getPlayerManager().onPlayerConnect(new FakeClientConnection(NetworkSide.SERVERBOUND), instance, new ConnectedClientData(profile, 0, instance.getClientOptions(), false));
|
FakeClientConnection connection = new FakeClientConnection(PacketFlow.SERVERBOUND);
|
||||||
System.out.println(instance.getPos());
|
//noinspection UnstableApiUsage
|
||||||
|
connection.getPacketContext().set(PacketContextImpl.REGISTRY_ACCESS, server.registryAccess());
|
||||||
|
//noinspection UnstableApiUsage
|
||||||
|
connection.getPacketContext().set(PacketContextImpl.SERVER_INSTANCE, server);
|
||||||
|
//noinspection UnstableApiUsage
|
||||||
|
connection.getPacketContext().set(PacketContextImpl.GAME_PROFILE, profile);
|
||||||
|
|
||||||
|
server.getPlayerList().placeNewPlayer(connection, instance, new CommonListenerCookie(profile, 0, instance.clientInformation(), false));
|
||||||
|
loadPlayerData(instance);
|
||||||
|
instance.stopRiding(); // otherwise the created fake player will be on the vehicle
|
||||||
|
System.out.println(instance.position());
|
||||||
if(pos != null && rot != null) {
|
if(pos != null && rot != null) {
|
||||||
instance.teleport(level, pos.x, pos.y, pos.z, Set.of(), rot.x, rot.y, true);
|
instance.teleportTo(level, pos.x, pos.y, pos.z, Set.of(), rot.x, rot.y, true);
|
||||||
}
|
}
|
||||||
instance.setVelocity(0,0,0);
|
instance.setDeltaMovement(0,0,0);
|
||||||
instance.setHealth(20.0F);
|
instance.setHealth(20.0F);
|
||||||
instance.unsetRemoved();
|
instance.unsetRemoved();
|
||||||
instance.getAttributeInstance(EntityAttributes.STEP_HEIGHT).setBaseValue(0.6F);
|
instance.getAttribute(Attributes.STEP_HEIGHT).setBaseValue(0.6F);
|
||||||
instance.interactionManager.changeGameMode(GameMode.SURVIVAL);
|
instance.getAttribute(Attributes.WAYPOINT_TRANSMIT_RANGE).setBaseValue(0);
|
||||||
server.getPlayerManager().sendToDimension(new EntitySetHeadYawS2CPacket(instance, (byte) (instance.headYaw * 256 / 360)), level.getRegistryKey());//instance.dimension);
|
instance.gameMode.changeGameModeForPlayer(GameType.SURVIVAL);
|
||||||
server.getPlayerManager().sendToDimension(EntityPositionSyncS2CPacket.create(instance), level.getRegistryKey());//instance.dimension);
|
server.getPlayerList().broadcastAll(new ClientboundRotateHeadPacket(instance, (byte) (instance.yHeadRot * 256 / 360)), level.dimension());
|
||||||
//instance.world.getChunkManager(). updatePosition(instance);
|
server.getPlayerList().broadcastAll(ClientboundEntityPositionSyncPacket.of(instance), level.dimension());
|
||||||
instance.dataTracker.set(PLAYER_MODEL_PARTS, (byte) 0x7f); // show all model layers (incl. capes)
|
instance.level().getChunkSource().move(instance);
|
||||||
|
instance.entityData.set(DATA_PLAYER_MODE_CUSTOMISATION, (byte) 0x7f); // show all model layers (incl. capes)
|
||||||
instance.getAbilities().flying = false;
|
instance.getAbilities().flying = false;
|
||||||
|
|
||||||
|
instance.listeners().forEach(listener -> listener.onMinionSpawn(instance));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static MinionFakePlayer respawnFake(MinecraftServer server, ServerWorld level, GameProfile profile, SyncedClientOptions cli, MinionData data)
|
private static void loadPlayerData(MinionFakePlayer player)
|
||||||
{
|
{
|
||||||
return new MinionFakePlayer(server, level, profile, cli, data);
|
try (ProblemReporter.ScopedCollector scopedCollector = new ProblemReporter.ScopedCollector(player.problemPath(), Minions.LOGGER))
|
||||||
|
{
|
||||||
|
Optional<ValueInput> optional = player.level().getServer().getPlayerList().loadPlayerData(player.nameAndId()).map((compoundTag) -> TagValueInput.create(scopedCollector, player.registryAccess(), compoundTag));
|
||||||
|
optional.ifPresent( valueInput -> {
|
||||||
|
player.load(valueInput);
|
||||||
|
player.loadAndSpawnEnderPearls(valueInput);
|
||||||
|
player.loadAndSpawnParentVehicle(valueInput);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private MinionFakePlayer(MinecraftServer server, ServerWorld worldIn, GameProfile profile, SyncedClientOptions cli, MinionData data)
|
public static MinionFakePlayer respawnFake(MinecraftServer server, ServerLevel level, GameProfile profile, ClientInformation cli)
|
||||||
|
{
|
||||||
|
return new MinionFakePlayer(server, level, profile, cli);
|
||||||
|
}
|
||||||
|
|
||||||
|
private MinionFakePlayer(MinecraftServer server, ServerLevel worldIn, GameProfile profile, ClientInformation cli)
|
||||||
{
|
{
|
||||||
super(server, worldIn, profile, cli);
|
super(server, worldIn, profile, cli);
|
||||||
this.data = data;
|
actionPack = new EntityPlayerActionPack(this);
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isProgrammable() {
|
|
||||||
return programmable;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setProgrammable(boolean programmable) {
|
|
||||||
this.programmable = programmable;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ModuleInventory getModuleInventory() {
|
public ModuleInventory getModuleInventory() {
|
||||||
return moduleInventory;
|
return moduleInventory;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MinionRuntime getRuntime() {
|
|
||||||
return runtime;
|
|
||||||
}
|
|
||||||
|
|
||||||
public EntityPlayerActionPack getMinionActionPack() {
|
public EntityPlayerActionPack getMinionActionPack() {
|
||||||
return ((ServerPlayerInterface)this).minions$getActionPack();
|
return actionPack;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MinionRuntime getInstructionManager() {
|
||||||
|
return instructionManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MinionData getData() {
|
||||||
|
return MinionPersistentState.get(getServer()).getMinionData(getUUID());
|
||||||
|
}
|
||||||
|
|
||||||
|
public SerializableListenerManager<MinionListener> listeners() {
|
||||||
|
return getData().listeners();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addMinionListener(MinionListener listener) {
|
||||||
|
listeners().addListener(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeMinionListener(MinionListener listener) {
|
||||||
|
listeners().removeListener(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void forEachMinionListener(Consumer<MinionListener> listenerConsumer) {
|
||||||
|
listeners().forEach(listenerConsumer);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean canSpawnMobs() {
|
||||||
|
return moduleInventory.hasAbility(SpecialAbilities.MOB_SPAWNING) || getData().config().getOption(MinionConfigOptions.spawnAndDespawnMobs);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean canDespawnMobs() {
|
||||||
|
return canSpawnMobs();
|
||||||
|
}
|
||||||
|
|
||||||
|
public MinecraftServer getServer() {
|
||||||
|
return level().getServer();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ActionResult interact(PlayerEntity player, Hand hand) {
|
public InteractionResult interact(Player player, InteractionHand hand, Vec3 location) {
|
||||||
if(player instanceof ServerPlayerEntity spe) {
|
if(player instanceof ServerPlayer spe) {
|
||||||
MinionGui.openInventory(spe, this);
|
new MinionGui(spe, this);
|
||||||
}
|
}
|
||||||
return ActionResult.CONSUME;
|
return InteractionResult.CONSUME;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ActionResult interactAt(PlayerEntity player, Vec3d hitPos, Hand hand) {
|
public void onEquipItem(final EquipmentSlot slot, final ItemStack previous, final ItemStack stack)
|
||||||
return interact(player, hand);
|
{
|
||||||
|
if (!isUsingItem()) super.onEquipItem(slot, previous, stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public void kill(Component reason)
|
||||||
public void onEquipStack(final EquipmentSlot slot, final ItemStack previous, final ItemStack stack)
|
|
||||||
{
|
{
|
||||||
if (!isUsingItem()) super.onEquipStack(slot, previous, stack);
|
listeners().forEach(listener -> listener.onMinionRemove(this));
|
||||||
}
|
|
||||||
|
|
||||||
/*@Override
|
|
||||||
public void kill()
|
|
||||||
{
|
|
||||||
kill(Text.literal("Killed"));
|
|
||||||
}*/
|
|
||||||
|
|
||||||
public void kill(Text reason)
|
|
||||||
{
|
|
||||||
shakeOff();
|
shakeOff();
|
||||||
|
|
||||||
if (reason.getContent() instanceof TranslatableTextContent text && text.getKey().equals("multiplayer.disconnect.duplicate_login")) {
|
if (reason.getContents() instanceof TranslatableContents text && text.getKey().equals("multiplayer.disconnect.duplicate_login")) {
|
||||||
this.networkHandler.onDisconnected(new DisconnectionInfo(reason));
|
this.connection.onDisconnect(new DisconnectionDetails(reason));
|
||||||
} else {
|
} else {
|
||||||
this.server.send(new ServerTask(this.server.getTicks(), () -> {
|
this.getServer().schedule(new TickTask(this.getServer().getTickCount(), () -> {
|
||||||
this.networkHandler.onDisconnected(new DisconnectionInfo(reason));
|
this.connection.onDisconnect(new DisconnectionDetails(reason));
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
MinionPersistentState.INSTANCE.updateMinionData(data.withSpawned(false));
|
MinionPersistentState.get(getServer()).updateMinionData(getData().withSpawned(false));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void tick()
|
public void tick()
|
||||||
{
|
{
|
||||||
if (this.getServer().getTicks() % 10 == 0)
|
actionPack.onUpdate();
|
||||||
|
if (this.getServer().getTickCount() % 10 == 0)
|
||||||
{
|
{
|
||||||
this.networkHandler.syncWithPlayerPosition();
|
this.connection.resetPosition();
|
||||||
this.getServerWorld().getChunkManager().updatePosition(this);
|
this.level().getChunkSource().move(this);
|
||||||
}
|
}
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
super.tick();
|
super.tick();
|
||||||
this.playerTick();
|
this.doTick();
|
||||||
|
instructionManager.tick();
|
||||||
}
|
}
|
||||||
catch (NullPointerException ignored)
|
catch (NullPointerException ignored)
|
||||||
{
|
{
|
||||||
// happens with that paper port thingy - not sure what that would fix, but hey
|
// happens with that paper port thingy - not sure what that would fix, but hey
|
||||||
// the game not gonna crash violently.
|
// the game not gonna crash violently.
|
||||||
}
|
}
|
||||||
runtime.tick();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean startRiding(Entity entityToRide, boolean force, boolean sendEventAndTriggers) {
|
||||||
|
if (super.startRiding(entityToRide, force, sendEventAndTriggers)) {
|
||||||
|
// from ClientPacketListener.handleSetEntityPassengersPacket
|
||||||
|
if (entityToRide instanceof AbstractBoat) {
|
||||||
|
this.yRotO = entityToRide.getYRot();
|
||||||
|
this.setYRot(entityToRide.getYRot());
|
||||||
|
this.setYHeadRot(entityToRide.getYHeadRot());
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void shakeOff()
|
private void shakeOff()
|
||||||
{
|
{
|
||||||
if (getVehicle() instanceof PlayerEntity) stopRiding();
|
if (getVehicle() instanceof Player) stopRiding();
|
||||||
for (Entity passenger : getPassengersDeep())
|
for (Entity passenger : getIndirectPassengers())
|
||||||
{
|
{
|
||||||
if (passenger instanceof PlayerEntity) passenger.stopRiding();
|
if (passenger instanceof Player) passenger.stopRiding();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDeath(DamageSource cause)
|
public void die(DamageSource cause)
|
||||||
{
|
{
|
||||||
shakeOff();
|
shakeOff();
|
||||||
super.onDeath(cause);
|
super.die(cause);
|
||||||
setHealth(20);
|
setHealth(20);
|
||||||
this.hungerManager = new HungerManager();
|
this.foodData = new FoodData();
|
||||||
kill(this.getDamageTracker().getDeathMessage());
|
kill(this.getCombatTracker().getDeathMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getIp()
|
public String getIpAddress()
|
||||||
{
|
{
|
||||||
return "127.0.0.1";
|
return "127.0.0.1";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean allowsServerListing() {
|
public boolean allowsListing() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void fall(double y, boolean onGround, BlockState state, BlockPos pos) {
|
protected void checkFallDamage(double y, boolean onGround, BlockState state, BlockPos pos) {
|
||||||
handleFall(0.0, y, 0.0, onGround);
|
doCheckFallDamage(0.0, y, 0.0, onGround);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ServerPlayerEntity teleportTo(TeleportTarget target)
|
public ServerPlayer teleport(TeleportTransition target)
|
||||||
{
|
{
|
||||||
super.teleportTo(target);
|
super.teleport(target);
|
||||||
if (notInAnyWorld) {
|
if (wonGame) {
|
||||||
ClientStatusC2SPacket p = new ClientStatusC2SPacket(ClientStatusC2SPacket.Mode.PERFORM_RESPAWN);
|
ServerboundClientCommandPacket p = new ServerboundClientCommandPacket(ServerboundClientCommandPacket.Action.PERFORM_RESPAWN);
|
||||||
networkHandler.onClientStatus(p);
|
connection.handleClientCommand(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If above branch was taken, *this* has been removed and replaced, the new instance has been set
|
// If above branch was taken, *this* has been removed and replaced, the new instance has been set
|
||||||
// on 'our' connection (which is now theirs, but we still have a ref).
|
// on 'our' connection (which is now theirs, but we still have a ref).
|
||||||
if (networkHandler.player.isInTeleportationState()) {
|
if (connection.player.isChangingDimension()) {
|
||||||
networkHandler.player.onTeleportationDone();
|
connection.player.hasChangedDimension();
|
||||||
}
|
|
||||||
return networkHandler.player;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void moveForward(float forward) {
|
|
||||||
this.moveForward += forward;
|
|
||||||
EntityPlayerActionPack actionPack = getMinionActionPack();
|
|
||||||
if (moveForward != 0) {
|
|
||||||
actionPack.setForward(moveForward > 0 ? 1 : -1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void moveSideways(float sideways) {
|
|
||||||
this.moveSideways += sideways;
|
|
||||||
EntityPlayerActionPack actionPack = getMinionActionPack();
|
|
||||||
if (moveSideways != 0) {
|
|
||||||
actionPack.setStrafing(moveSideways > 0 ? 1 : -1);
|
|
||||||
}
|
}
|
||||||
|
return connection.player;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void move(MovementType movementType, Vec3d movement) {
|
public void dropAllDeathLoot(ServerLevel world, DamageSource damageSource) {
|
||||||
float newForward = (float) (moveForward - movement.z);
|
super.dropAllDeathLoot(world, damageSource);
|
||||||
float newSideways = (float) (moveSideways - movement.x);
|
ItemEntity entity = drop(toItemStack(world.getServer()), true, false);
|
||||||
Vec3d newMovement = movement;
|
if (entity != null) {
|
||||||
if ((newForward < 0 && moveForward > 0) || (newForward > 0 && moveForward < 0)) {
|
entity.setUnlimitedLifetime();
|
||||||
newMovement = new Vec3d(newMovement.x, newMovement.y, moveForward);
|
|
||||||
moveForward = 0;
|
|
||||||
getMinionActionPack().setForward(0);
|
|
||||||
}else {
|
|
||||||
moveForward = newForward;
|
|
||||||
}
|
}
|
||||||
if ((newSideways < 0 && moveSideways > 0) || (newSideways > 0 && moveSideways < 0)) {
|
|
||||||
newMovement = new Vec3d(newMovement.x, newMovement.y, moveSideways);
|
|
||||||
moveSideways = 0;
|
|
||||||
getMinionActionPack().setStrafing(0);
|
|
||||||
}else {
|
|
||||||
moveSideways = newSideways;
|
|
||||||
}
|
|
||||||
super.move(movementType, newMovement);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private ItemStack toItemStack(MinecraftServer server) {
|
||||||
public void drop(ServerWorld world, DamageSource damageSource) {
|
ItemStack stack = new ItemStack(MinionItems.MINION_ITEM);
|
||||||
super.drop(world, damageSource);
|
MinionItem.setData(server, getData(), stack);
|
||||||
dropStack(world, toItemStack()).setNeverDespawn();
|
|
||||||
}
|
|
||||||
|
|
||||||
private ItemStack toItemStack() {
|
|
||||||
ItemStack stack = new ItemStack(Minions.MINION_ITEM);
|
|
||||||
MinionItem.setData(data, stack);
|
|
||||||
return stack;
|
return stack;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MinionData getData() {
|
@Override
|
||||||
return data;
|
public void addAdditionalSaveData(ValueOutput view) {
|
||||||
|
super.addAdditionalSaveData(view);
|
||||||
|
moduleInventory.writeData(view.child("modules"));
|
||||||
|
instructionManager.save(view.child("instructionManager"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void writeCustomDataToNbt(NbtCompound nbt) {
|
public void readAdditionalSaveData(ValueInput view) {
|
||||||
super.writeCustomDataToNbt(nbt);
|
super.readAdditionalSaveData(view);
|
||||||
nbt.put("modules", moduleInventory.writeNbt(new NbtCompound(), getRegistryManager()));
|
moduleInventory.readData(view.childOrEmpty("modules"));
|
||||||
nbt.putBoolean("programmable", programmable);
|
instructionManager.load(view.childOrEmpty("instructionManager"));
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void readCustomDataFromNbt(NbtCompound nbt) {
|
|
||||||
super.readCustomDataFromNbt(nbt);
|
|
||||||
moduleInventory.readNbt(nbt.getCompound("modules"), getRegistryManager());
|
|
||||||
programmable = nbt.getBoolean("programmable");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+18
-23
@@ -1,46 +1,41 @@
|
|||||||
//code from https://github.com/gnembon/fabric-carpet
|
//code from https://github.com/gnembon/fabric-carpet
|
||||||
package io.github.skippyall.minions.minion.fakeplayer;
|
package io.github.skippyall.minions.minion.fakeplayer;
|
||||||
|
|
||||||
import net.minecraft.entity.player.PlayerPosition;
|
import net.minecraft.network.Connection;
|
||||||
import net.minecraft.network.ClientConnection;
|
import net.minecraft.network.chat.Component;
|
||||||
import net.minecraft.network.packet.Packet;
|
import net.minecraft.network.chat.contents.TranslatableContents;
|
||||||
import net.minecraft.network.packet.s2c.play.PositionFlag;
|
|
||||||
import net.minecraft.server.MinecraftServer;
|
import net.minecraft.server.MinecraftServer;
|
||||||
import net.minecraft.server.network.ConnectedClientData;
|
import net.minecraft.server.level.ServerPlayer;
|
||||||
import net.minecraft.server.network.ServerPlayNetworkHandler;
|
import net.minecraft.server.network.CommonListenerCookie;
|
||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
import net.minecraft.server.network.ServerGamePacketListenerImpl;
|
||||||
import net.minecraft.text.Text;
|
import net.minecraft.world.entity.PositionMoveRotation;
|
||||||
import net.minecraft.text.TranslatableTextContent;
|
import net.minecraft.world.entity.Relative;
|
||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
public class NetHandlerPlayServerFake extends ServerPlayNetworkHandler
|
public class NetHandlerPlayServerFake extends ServerGamePacketListenerImpl
|
||||||
{
|
{
|
||||||
public NetHandlerPlayServerFake(final MinecraftServer minecraftServer, final ClientConnection connection, final ServerPlayerEntity serverPlayer, final ConnectedClientData i)
|
public NetHandlerPlayServerFake(final MinecraftServer minecraftServer, final Connection connection, final ServerPlayer serverPlayer, final CommonListenerCookie i)
|
||||||
{
|
{
|
||||||
super(minecraftServer, connection, serverPlayer, i);
|
super(minecraftServer, connection, serverPlayer, i);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendPacket(final Packet<?> packetIn)
|
public void disconnect(Component message)
|
||||||
{
|
{
|
||||||
}
|
if (message.getContents() instanceof TranslatableContents text && (text.getKey().equals("multiplayer.disconnect.idling") || text.getKey().equals("multiplayer.disconnect.duplicate_login")))
|
||||||
|
|
||||||
@Override
|
|
||||||
public void disconnect(Text message)
|
|
||||||
{
|
|
||||||
if (message.getContent() instanceof TranslatableTextContent text && (text.getKey().equals("multiplayer.disconnect.idling") || text.getKey().equals("multiplayer.disconnect.duplicate_login")))
|
|
||||||
{
|
{
|
||||||
((MinionFakePlayer) player).kill(message);
|
((MinionFakePlayer) player).kill(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void requestTeleport(PlayerPosition pos, Set<PositionFlag> set)
|
public void teleport(PositionMoveRotation pos, Set<Relative> set)
|
||||||
{
|
{
|
||||||
super.requestTeleport(pos, set);
|
super.teleport(pos, set);
|
||||||
if (player.getServerWorld().getPlayerByUuid(player.getUuid()) != null) {
|
if (player.level().getPlayerByUUID(player.getUUID()) != null) {
|
||||||
syncWithPlayerPosition();
|
resetPosition();
|
||||||
player.getServerWorld().getChunkManager().updatePosition(player);
|
player.level().getChunkSource().move(player);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
//code from https://github.com/gnembon/fabric-carpet
|
|
||||||
package io.github.skippyall.minions.minion.fakeplayer;
|
|
||||||
|
|
||||||
public interface ServerPlayerInterface
|
|
||||||
{
|
|
||||||
EntityPlayerActionPack minions$getActionPack();
|
|
||||||
}
|
|
||||||
@@ -1,16 +1,17 @@
|
|||||||
//code from https://github.com/gnembon/fabric-carpet
|
//code from https://github.com/gnembon/fabric-carpet
|
||||||
package io.github.skippyall.minions.minion.fakeplayer;
|
package io.github.skippyall.minions.minion.fakeplayer;
|
||||||
|
|
||||||
|
import net.minecraft.world.entity.Entity;
|
||||||
|
import net.minecraft.world.level.ClipContext;
|
||||||
|
import net.minecraft.world.level.Level;
|
||||||
|
import net.minecraft.world.phys.AABB;
|
||||||
|
import net.minecraft.world.phys.BlockHitResult;
|
||||||
|
import net.minecraft.world.phys.EntityHitResult;
|
||||||
|
import net.minecraft.world.phys.HitResult;
|
||||||
|
import net.minecraft.world.phys.Vec3;
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
import net.minecraft.entity.Entity;
|
|
||||||
import net.minecraft.util.hit.BlockHitResult;
|
|
||||||
import net.minecraft.util.hit.EntityHitResult;
|
|
||||||
import net.minecraft.util.hit.HitResult;
|
|
||||||
import net.minecraft.util.math.Box;
|
|
||||||
import net.minecraft.util.math.Vec3d;
|
|
||||||
import net.minecraft.world.RaycastContext;
|
|
||||||
import net.minecraft.world.World;
|
|
||||||
|
|
||||||
public class Tracer
|
public class Tracer
|
||||||
{
|
{
|
||||||
@@ -20,7 +21,7 @@ public class Tracer
|
|||||||
double maxSqDist = reach * reach;
|
double maxSqDist = reach * reach;
|
||||||
if (blockHit != null)
|
if (blockHit != null)
|
||||||
{
|
{
|
||||||
maxSqDist = blockHit.getPos().squaredDistanceTo(source.getCameraPosVec(partialTicks));
|
maxSqDist = blockHit.getLocation().distanceToSqr(source.getEyePosition(partialTicks));
|
||||||
}
|
}
|
||||||
EntityHitResult entityHit = rayTraceEntities(source, partialTicks, reach, maxSqDist);
|
EntityHitResult entityHit = rayTraceEntities(source, partialTicks, reach, maxSqDist);
|
||||||
return entityHit == null ? blockHit : entityHit;
|
return entityHit == null ? blockHit : entityHit;
|
||||||
@@ -28,31 +29,31 @@ public class Tracer
|
|||||||
|
|
||||||
public static BlockHitResult rayTraceBlocks(Entity source, float partialTicks, double reach, boolean fluids)
|
public static BlockHitResult rayTraceBlocks(Entity source, float partialTicks, double reach, boolean fluids)
|
||||||
{
|
{
|
||||||
Vec3d pos = source.getCameraPosVec(partialTicks);
|
Vec3 pos = source.getEyePosition(partialTicks);
|
||||||
Vec3d rotation = source.getRotationVec(partialTicks);
|
Vec3 rotation = source.getViewVector(partialTicks);
|
||||||
Vec3d reachEnd = pos.add(rotation.x * reach, rotation.y * reach, rotation.z * reach);
|
Vec3 reachEnd = pos.add(rotation.x * reach, rotation.y * reach, rotation.z * reach);
|
||||||
return source.getWorld().raycast(new RaycastContext(pos, reachEnd, RaycastContext.ShapeType.OUTLINE, fluids ?
|
return source.level().clip(new ClipContext(pos, reachEnd, ClipContext.Block.OUTLINE, fluids ?
|
||||||
RaycastContext.FluidHandling.ANY : RaycastContext.FluidHandling.NONE, source));
|
ClipContext.Fluid.ANY : ClipContext.Fluid.NONE, source));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static EntityHitResult rayTraceEntities(Entity source, float partialTicks, double reach, double maxSqDist)
|
public static EntityHitResult rayTraceEntities(Entity source, float partialTicks, double reach, double maxSqDist)
|
||||||
{
|
{
|
||||||
Vec3d pos = source.getCameraPosVec(partialTicks);
|
Vec3 pos = source.getEyePosition(partialTicks);
|
||||||
Vec3d reachVec = source.getRotationVec(partialTicks).multiply(reach);
|
Vec3 reachVec = source.getViewVector(partialTicks).scale(reach);
|
||||||
Box box = source.getBoundingBox().stretch(reachVec).expand(1);
|
AABB box = source.getBoundingBox().expandTowards(reachVec).inflate(1);
|
||||||
return rayTraceEntities(source, pos, pos.add(reachVec), box, e -> !e.isSpectator() && e.canHit(), maxSqDist);
|
return rayTraceEntities(source, pos, pos.add(reachVec), box, e -> !e.isSpectator() && e.isPickable(), maxSqDist);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static EntityHitResult rayTraceEntities(Entity source, Vec3d start, Vec3d end, Box box, Predicate<Entity> predicate, double maxSqDistance)
|
public static EntityHitResult rayTraceEntities(Entity source, Vec3 start, Vec3 end, AABB box, Predicate<Entity> predicate, double maxSqDistance)
|
||||||
{
|
{
|
||||||
World world = source.getWorld();
|
Level world = source.level();
|
||||||
double targetDistance = maxSqDistance;
|
double targetDistance = maxSqDistance;
|
||||||
Entity target = null;
|
Entity target = null;
|
||||||
Vec3d targetHitPos = null;
|
Vec3 targetHitPos = null;
|
||||||
for (Entity current : world.getOtherEntities(source, box, predicate))
|
for (Entity current : world.getEntities(source, box, predicate))
|
||||||
{
|
{
|
||||||
Box currentBox = current.getBoundingBox().expand(current.getTargetingMargin());
|
AABB currentBox = current.getBoundingBox().inflate(current.getPickRadius());
|
||||||
Optional<Vec3d> currentHit = currentBox.raycast(start, end);
|
Optional<Vec3> currentHit = currentBox.clip(start, end);
|
||||||
if (currentBox.contains(start))
|
if (currentBox.contains(start))
|
||||||
{
|
{
|
||||||
if (targetDistance >= 0)
|
if (targetDistance >= 0)
|
||||||
@@ -64,8 +65,8 @@ public class Tracer
|
|||||||
}
|
}
|
||||||
else if (currentHit.isPresent())
|
else if (currentHit.isPresent())
|
||||||
{
|
{
|
||||||
Vec3d currentHitPos = currentHit.get();
|
Vec3 currentHitPos = currentHit.get();
|
||||||
double currentDistance = start.squaredDistanceTo(currentHitPos);
|
double currentDistance = start.distanceToSqr(currentHitPos);
|
||||||
if (currentDistance < targetDistance || targetDistance == 0)
|
if (currentDistance < targetDistance || targetDistance == 0)
|
||||||
{
|
{
|
||||||
if (current.getRootVehicle() == source.getRootVehicle())
|
if (current.getRootVehicle() == source.getRootVehicle())
|
||||||
|
|||||||
+51
@@ -0,0 +1,51 @@
|
|||||||
|
package io.github.skippyall.minions.minion.program.instruction;
|
||||||
|
|
||||||
|
import io.github.skippyall.minions.minion.MinionRuntime;
|
||||||
|
import io.github.skippyall.minions.minion.fakeplayer.EntityPlayerActionPack;
|
||||||
|
import io.github.skippyall.minions.program.consumer.ValueConsumerList;
|
||||||
|
import io.github.skippyall.minions.program.instruction.execution.ContinuousInstructionExecution;
|
||||||
|
import io.github.skippyall.minions.program.supplier.ParameterValueList;
|
||||||
|
import net.minecraft.world.level.storage.ValueInput;
|
||||||
|
import net.minecraft.world.level.storage.ValueOutput;
|
||||||
|
|
||||||
|
public class ActionExecution implements ContinuousInstructionExecution<MinionRuntime> {
|
||||||
|
private final EntityPlayerActionPack.ActionType action;
|
||||||
|
|
||||||
|
public ActionExecution(EntityPlayerActionPack.ActionType action) {
|
||||||
|
this.action = action;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void start(MinionRuntime minion) {
|
||||||
|
EntityPlayerActionPack ap = minion.getMinion().getMinionActionPack();
|
||||||
|
if(!ap.hasAction(action)) {
|
||||||
|
minion.getMinion().getMinionActionPack().start(action, EntityPlayerActionPack.Action.startContinuous());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void stop(MinionRuntime minion, ValueConsumerList<MinionRuntime> valueConsumers) {
|
||||||
|
minion.getMinion().getMinionActionPack().stop(action);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void pause(MinionRuntime runtime) {
|
||||||
|
runtime.getMinion().getMinionActionPack().stop(action);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void resume(MinionRuntime runtime) {
|
||||||
|
runtime.getMinion().getMinionActionPack().start(action, EntityPlayerActionPack.Action.continuous());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void readArguments(ParameterValueList parameters, MinionRuntime minion) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void save(ValueOutput view, MinionRuntime minion) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void load(ValueInput view, MinionRuntime runtime) {
|
||||||
|
runtime.getMinion().getMinionActionPack().start(action, EntityPlayerActionPack.Action.continuous());
|
||||||
|
}
|
||||||
|
}
|
||||||
+142
@@ -0,0 +1,142 @@
|
|||||||
|
//partially code from https://github.com/gnembon/fabric-carpet (EntityPlayerActionPack)
|
||||||
|
package io.github.skippyall.minions.minion.program.instruction;
|
||||||
|
|
||||||
|
import io.github.skippyall.minions.minion.MinionRuntime;
|
||||||
|
import io.github.skippyall.minions.minion.fakeplayer.EntityPlayerActionPack;
|
||||||
|
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
|
||||||
|
import io.github.skippyall.minions.program.consumer.ValueConsumerList;
|
||||||
|
import io.github.skippyall.minions.program.instruction.InstructionExecution;
|
||||||
|
import io.github.skippyall.minions.program.supplier.ParameterValueList;
|
||||||
|
import net.minecraft.core.BlockPos;
|
||||||
|
import net.minecraft.core.Direction;
|
||||||
|
import net.minecraft.network.protocol.game.ServerboundPlayerActionPacket;
|
||||||
|
import net.minecraft.world.InteractionHand;
|
||||||
|
import net.minecraft.world.level.block.state.BlockState;
|
||||||
|
import net.minecraft.world.level.storage.ValueInput;
|
||||||
|
import net.minecraft.world.level.storage.ValueOutput;
|
||||||
|
import net.minecraft.world.phys.BlockHitResult;
|
||||||
|
import net.minecraft.world.phys.HitResult;
|
||||||
|
|
||||||
|
public class MineBlockExecution implements InstructionExecution<MinionRuntime> {
|
||||||
|
private BlockPos currentBlock;
|
||||||
|
private float currentBlockDamage = 0;
|
||||||
|
private boolean first = true;
|
||||||
|
private boolean done = false;
|
||||||
|
private boolean success = false;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void start(MinionRuntime runtime) {
|
||||||
|
MinionFakePlayer player = runtime.getMinion();
|
||||||
|
if(EntityPlayerActionPack.getTarget(player) instanceof BlockHitResult hit) {
|
||||||
|
this.currentBlock = hit.getBlockPos();
|
||||||
|
|
||||||
|
EntityPlayerActionPack ap = player.getMinionActionPack();
|
||||||
|
if (ap.blockHitDelay > 0) {
|
||||||
|
ap.blockHitDelay--;
|
||||||
|
done = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (player.blockActionRestricted(player.level(), hit.getBlockPos(), player.gameMode.getGameModeForPlayer())) {
|
||||||
|
done = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
done = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void tick(MinionRuntime runtime) {
|
||||||
|
if(done) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
MinionFakePlayer player = runtime.getMinion();
|
||||||
|
EntityPlayerActionPack ap = player.getMinionActionPack();
|
||||||
|
|
||||||
|
HitResult newHit = EntityPlayerActionPack.getTarget(player);
|
||||||
|
if(!(newHit instanceof BlockHitResult newBlockHit)) {
|
||||||
|
done = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
BlockPos newPos = newBlockHit.getBlockPos();
|
||||||
|
if(!newPos.equals(currentBlock)) {
|
||||||
|
done = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (player.level().getBlockState(currentBlock).isAir()) {
|
||||||
|
done = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
BlockState state = player.level().getBlockState(currentBlock);
|
||||||
|
boolean blockBroken = false;
|
||||||
|
if (first) {
|
||||||
|
first = false;
|
||||||
|
player.gameMode.handleBlockBreakAction(currentBlock, ServerboundPlayerActionPacket.Action.START_DESTROY_BLOCK, newBlockHit.getDirection(), player.level().getMaxY(), -1);
|
||||||
|
boolean notAir = !state.isAir();
|
||||||
|
if (notAir)
|
||||||
|
{
|
||||||
|
state.attack(player.level(), currentBlock, player);
|
||||||
|
}
|
||||||
|
if (notAir && state.getDestroyProgress(player, player.level(), currentBlock) >= 1)
|
||||||
|
{
|
||||||
|
//instamine??
|
||||||
|
blockBroken = true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
currentBlockDamage += state.getDestroyProgress(player, player.level(), currentBlock);
|
||||||
|
if (currentBlockDamage >= 1) {
|
||||||
|
player.gameMode.handleBlockBreakAction(currentBlock, ServerboundPlayerActionPacket.Action.STOP_DESTROY_BLOCK, newBlockHit.getDirection(), player.level().getMaxY(), -1);
|
||||||
|
ap.blockHitDelay = 5;
|
||||||
|
blockBroken = true;
|
||||||
|
}
|
||||||
|
player.level().destroyBlockProgress(-1, currentBlock, (int) (currentBlockDamage * 10));
|
||||||
|
|
||||||
|
}
|
||||||
|
player.resetLastActionTime();
|
||||||
|
player.swing(InteractionHand.MAIN_HAND);
|
||||||
|
|
||||||
|
if(blockBroken) {
|
||||||
|
done = true;
|
||||||
|
success = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isDone(MinionRuntime runtime) {
|
||||||
|
return done;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void stop(MinionRuntime runtime, ValueConsumerList<MinionRuntime> valueConsumers) {
|
||||||
|
MinionFakePlayer player = runtime.getMinion();
|
||||||
|
EntityPlayerActionPack ap = player.getMinionActionPack();
|
||||||
|
|
||||||
|
if(currentBlock != null) {
|
||||||
|
player.level().destroyBlockProgress(-1, currentBlock, -1);
|
||||||
|
player.gameMode.handleBlockBreakAction(currentBlock, ServerboundPlayerActionPacket.Action.ABORT_DESTROY_BLOCK, Direction.DOWN, player.level().getMaxY(), -1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void readArguments(ParameterValueList arguments, MinionRuntime runtime) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void save(ValueOutput view, MinionRuntime runtime) {
|
||||||
|
view.store("currentBlock", BlockPos.CODEC, currentBlock);
|
||||||
|
view.putFloat("currentBlockDamage", currentBlockDamage);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void load(ValueInput view, MinionRuntime runtime) {
|
||||||
|
currentBlock = view.read("currentBlock", BlockPos.CODEC).orElse(null);
|
||||||
|
currentBlockDamage = view.getFloatOr("currentBlockDamage", 0);
|
||||||
|
if(currentBlock == null) {
|
||||||
|
done = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+133
@@ -0,0 +1,133 @@
|
|||||||
|
package io.github.skippyall.minions.minion.program.instruction.inventory;
|
||||||
|
|
||||||
|
import io.github.skippyall.minions.minion.MinionRuntime;
|
||||||
|
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
|
||||||
|
import io.github.skippyall.minions.program.consumer.ValueConsumerList;
|
||||||
|
import io.github.skippyall.minions.program.instruction.InstructionExecution;
|
||||||
|
import io.github.skippyall.minions.program.supplier.Parameter;
|
||||||
|
import io.github.skippyall.minions.program.supplier.ParameterValueList;
|
||||||
|
import io.github.skippyall.minions.registration.ValueTypes;
|
||||||
|
import net.minecraft.world.inventory.AbstractContainerMenu;
|
||||||
|
import net.minecraft.world.inventory.ContainerInput;
|
||||||
|
import net.minecraft.world.inventory.Slot;
|
||||||
|
import net.minecraft.world.item.ItemStack;
|
||||||
|
import net.minecraft.world.level.storage.ValueInput;
|
||||||
|
import net.minecraft.world.level.storage.ValueOutput;
|
||||||
|
|
||||||
|
public class SwapItemExecution implements InstructionExecution<MinionRuntime> {
|
||||||
|
public static final Parameter<Long> FROM_SLOT = new Parameter<>("from_slot", ValueTypes.LONG);
|
||||||
|
public static final Parameter<Boolean> FROM_SCREEN = new Parameter<>("from_screen", ValueTypes.BOOLEAN);
|
||||||
|
public static final Parameter<Long> TO_SLOT = new Parameter<>("to_slot", ValueTypes.LONG);
|
||||||
|
public static final Parameter<Boolean> TO_SCREEN = new Parameter<>("to_screen", ValueTypes.BOOLEAN);
|
||||||
|
|
||||||
|
private int fromSlot;
|
||||||
|
private boolean fromScreen;
|
||||||
|
private int toSlot;
|
||||||
|
private boolean toScreen;
|
||||||
|
|
||||||
|
private ItemStack cursor = ItemStack.EMPTY;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void start(MinionRuntime runtime) {
|
||||||
|
MinionFakePlayer minion = runtime.getMinion();
|
||||||
|
|
||||||
|
if((fromScreen || toScreen) && minion.containerMenu == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if(!(checkBounds(minion, fromSlot, fromScreen) && checkBounds(minion, toSlot, toScreen))) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ItemStack fromStack = getStack(minion, fromSlot, fromScreen);
|
||||||
|
ItemStack toStack = getStack(minion, toSlot, toScreen);
|
||||||
|
|
||||||
|
if(!(canExchange(minion, fromSlot, fromScreen, toStack) && canExchange(minion, toSlot, toScreen, fromStack))) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
simulateClick(minion, fromSlot, fromScreen);
|
||||||
|
simulateClick(minion, toSlot, toScreen);
|
||||||
|
simulateClick(minion, fromSlot, fromScreen);
|
||||||
|
minion.getInventory().placeItemBackInInventory(cursor);
|
||||||
|
}
|
||||||
|
|
||||||
|
private AbstractContainerMenu getScreen(MinionFakePlayer minion, boolean screen) {
|
||||||
|
if(screen) {
|
||||||
|
return minion.containerMenu;
|
||||||
|
} else {
|
||||||
|
return minion.inventoryMenu;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean checkBounds(MinionFakePlayer minion, int slot, boolean screen) {
|
||||||
|
return slot >= 0 && slot < getScreen(minion, screen).slots.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
private ItemStack getStack(MinionFakePlayer minion, int slot, boolean screen) {
|
||||||
|
return getScreen(minion, screen).getSlot(slot).getItem();
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean canExchange(MinionFakePlayer minion, int slotIndex, boolean screen, ItemStack newStack) {
|
||||||
|
AbstractContainerMenu screenHandler = getScreen(minion, screen);
|
||||||
|
Slot slot = screenHandler.getSlot(slotIndex);
|
||||||
|
if(!slot.getItem().isEmpty() && !slot.mayPickup(minion)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if(!newStack.isEmpty() && !slot.mayPlace(newStack)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
/*else {
|
||||||
|
if(slotIndex >= PlayerInventory.MAIN_SIZE && slotIndex < PlayerInventory.OFF_HAND_SLOT) {
|
||||||
|
if(!minion.canEquip(newStack, PlayerInventory.EQUIPMENT_SLOTS.get(slotIndex))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if(EnchantmentHelper.hasAnyEnchantmentsWith(minion.getInventory().getStack(slotIndex), EnchantmentEffectComponentTypes.PREVENT_ARMOR_CHANGE)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void simulateClick(MinionFakePlayer minion, int slotIndex, boolean screen) {
|
||||||
|
AbstractContainerMenu screenHandler = getScreen(minion, screen);
|
||||||
|
ItemStack previousCursor = screenHandler.getCarried();
|
||||||
|
screenHandler.setCarried(cursor);
|
||||||
|
screenHandler.clicked(slotIndex, 0, ContainerInput.SWAP, minion);
|
||||||
|
cursor = screenHandler.getCarried();
|
||||||
|
screenHandler.setCarried(previousCursor);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void tick(MinionRuntime runtime) {
|
||||||
|
InstructionExecution.super.tick(runtime);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isDone(MinionRuntime runtime) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void stop(MinionRuntime runtime, ValueConsumerList<MinionRuntime> valueConsumers) {
|
||||||
|
InstructionExecution.super.stop(runtime, valueConsumers);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void readArguments(ParameterValueList arguments, MinionRuntime runtime) {
|
||||||
|
fromSlot = Math.clamp(arguments.getValue(FROM_SLOT), 0, Integer.MAX_VALUE);
|
||||||
|
fromScreen = arguments.getValue(FROM_SCREEN);
|
||||||
|
toSlot = Math.clamp(arguments.getValue(TO_SLOT), 0, Integer.MAX_VALUE);
|
||||||
|
toScreen = arguments.getValue(TO_SCREEN);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void save(ValueOutput view, MinionRuntime runtime) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void load(ValueInput view, MinionRuntime runtime) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
+38
@@ -0,0 +1,38 @@
|
|||||||
|
package io.github.skippyall.minions.minion.program.instruction.move;
|
||||||
|
|
||||||
|
import io.github.skippyall.minions.minion.MinionRuntime;
|
||||||
|
import io.github.skippyall.minions.program.instruction.InstructionExecution;
|
||||||
|
import net.minecraft.world.level.storage.ValueInput;
|
||||||
|
import net.minecraft.world.level.storage.ValueOutput;
|
||||||
|
|
||||||
|
public abstract class AbstractTurnExecution implements InstructionExecution<MinionRuntime> {
|
||||||
|
protected float targetYaw;
|
||||||
|
protected float targetPitch;
|
||||||
|
|
||||||
|
private static final float anglePerTick = 10;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void tick(MinionRuntime minion) {
|
||||||
|
float rotateYaw = targetYaw - minion.getMinion().getYRot();
|
||||||
|
float rotatePitch = targetPitch - minion.getMinion().getXRot();
|
||||||
|
|
||||||
|
minion.getMinion().getMinionActionPack().turn(Math.min(rotateYaw, anglePerTick), Math.min(rotatePitch, anglePerTick));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isDone(MinionRuntime minion) {
|
||||||
|
return Math.abs(targetYaw - minion.getMinion().getYRot()) < 0.001F && Math.abs(targetPitch - minion.getMinion().getXRot()) < 0.001F;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void save(ValueOutput view, MinionRuntime runtime) {
|
||||||
|
view.putFloat("targetYaw", targetYaw);
|
||||||
|
view.putFloat("targetPitch", targetPitch);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void load(ValueInput view, MinionRuntime runtime) {
|
||||||
|
targetYaw = view.getFloatOr("targetYaw", 0);
|
||||||
|
targetPitch = view.getFloatOr("targetPitch", 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
package io.github.skippyall.minions.minion.program.instruction.move;
|
||||||
|
|
||||||
|
import io.github.skippyall.minions.minion.MinionRuntime;
|
||||||
|
import io.github.skippyall.minions.program.instruction.InstructionExecution;
|
||||||
|
import io.github.skippyall.minions.program.instruction.execution.ContinuousInstructionExecution;
|
||||||
|
import net.minecraft.world.entity.MoverType;
|
||||||
|
|
||||||
|
public class ContinuousWalkExecution implements ContinuousInstructionExecution<MinionRuntime>, InstructionExecution.Stateless<MinionRuntime> {
|
||||||
|
@Override
|
||||||
|
public void tick(MinionRuntime minion) {
|
||||||
|
minion.getMinion().move(MoverType.SELF, minion.getMinion().getDirection().getUnitVec3().normalize().scale(minion.getMinion().getSpeed()));
|
||||||
|
}
|
||||||
|
}
|
||||||
+47
@@ -0,0 +1,47 @@
|
|||||||
|
package io.github.skippyall.minions.minion.program.instruction.move;
|
||||||
|
|
||||||
|
import com.mojang.serialization.Codec;
|
||||||
|
import io.github.skippyall.minions.gui.Displayable;
|
||||||
|
import io.github.skippyall.minions.gui.GuiDisplay;
|
||||||
|
import net.minecraft.util.StringRepresentable;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public enum TurnDirection implements StringRepresentable, Displayable {
|
||||||
|
LEFT("left", -1, 0),
|
||||||
|
UP("up", 0, -1),
|
||||||
|
RIGHT("right", 1, 0),
|
||||||
|
DOWN("down", 0, 1);
|
||||||
|
|
||||||
|
public static final Codec<TurnDirection> CODEC = StringRepresentable.fromEnum(TurnDirection::values);
|
||||||
|
|
||||||
|
private static final UUID MHF_ArrowLeft = UUID.fromString("a68f0b64-8d14-4000-a95f-4b9ba14f8df9");
|
||||||
|
private static final UUID MHF_ArrowUp = UUID.fromString("fef039ef-e6cd-4987-9c84-26a3e6134277");
|
||||||
|
private static final UUID MHF_ArrowRight = UUID.fromString("50c8510b-5ea0-4d60-be9a-7d542d6cd156");
|
||||||
|
private static final UUID MHF_ArrowDown = UUID.fromString("68f59b9b-5b0b-4b05-a9f2-e1d1405aa348");
|
||||||
|
|
||||||
|
public final String name;
|
||||||
|
public final int xFactor;
|
||||||
|
public final int yFactor;
|
||||||
|
|
||||||
|
TurnDirection(String name, int xFactor, int yFactor) {
|
||||||
|
this.xFactor = xFactor;
|
||||||
|
this.yFactor = yFactor;
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getSerializedName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public GuiDisplay getDisplay() {
|
||||||
|
return switch (this) {
|
||||||
|
case LEFT -> new GuiDisplay.HeadBased(MHF_ArrowLeft);
|
||||||
|
case UP -> new GuiDisplay.HeadBased(MHF_ArrowUp);
|
||||||
|
case RIGHT -> new GuiDisplay.HeadBased(MHF_ArrowRight);
|
||||||
|
case DOWN -> new GuiDisplay.HeadBased(MHF_ArrowDown);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
+23
@@ -0,0 +1,23 @@
|
|||||||
|
package io.github.skippyall.minions.minion.program.instruction.move;
|
||||||
|
|
||||||
|
import io.github.skippyall.minions.minion.MinionRuntime;
|
||||||
|
import io.github.skippyall.minions.program.supplier.Parameter;
|
||||||
|
import io.github.skippyall.minions.program.supplier.ParameterValueList;
|
||||||
|
import io.github.skippyall.minions.registration.ValueTypes;
|
||||||
|
|
||||||
|
public class TurnExecution extends AbstractTurnExecution {
|
||||||
|
public static final Parameter<Double> ANGLE = new Parameter<>("maxAngle", ValueTypes.DOUBLE);
|
||||||
|
public static final Parameter<TurnDirection> DIRECTION = new Parameter<>("direction", ValueTypes.TURN_DIRECTION);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void readArguments(ParameterValueList arguments, MinionRuntime minion) {
|
||||||
|
float maxAngle = arguments.getValue(ANGLE).floatValue();
|
||||||
|
TurnDirection direction = arguments.getValue(DIRECTION);
|
||||||
|
|
||||||
|
float turnYaw = maxAngle * direction.xFactor;
|
||||||
|
float turnPitch = maxAngle * direction.yFactor;
|
||||||
|
|
||||||
|
targetYaw = minion.getMinion().getYRot() + turnYaw;
|
||||||
|
targetPitch = minion.getMinion().getXRot() + turnPitch;
|
||||||
|
}
|
||||||
|
}
|
||||||
+36
@@ -0,0 +1,36 @@
|
|||||||
|
package io.github.skippyall.minions.minion.program.instruction.move;
|
||||||
|
|
||||||
|
import io.github.skippyall.minions.minion.MinionRuntime;
|
||||||
|
import io.github.skippyall.minions.program.supplier.Parameter;
|
||||||
|
import io.github.skippyall.minions.program.supplier.ParameterValueList;
|
||||||
|
import io.github.skippyall.minions.registration.ValueTypes;
|
||||||
|
import net.minecraft.util.Mth;
|
||||||
|
import net.minecraft.world.phys.Vec2;
|
||||||
|
import net.minecraft.world.phys.Vec3;
|
||||||
|
|
||||||
|
public class TurnVectorExecution extends AbstractTurnExecution {
|
||||||
|
public static final Parameter<Double> X = new Parameter<>("x", ValueTypes.DOUBLE);
|
||||||
|
public static final Parameter<Double> Y = new Parameter<>("y", ValueTypes.DOUBLE);
|
||||||
|
public static final Parameter<Double> Z = new Parameter<>("z", ValueTypes.DOUBLE);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void readArguments(ParameterValueList arguments, MinionRuntime runtime) {
|
||||||
|
double x = arguments.getValue(X);
|
||||||
|
double y = arguments.getValue(Y);
|
||||||
|
double z = arguments.getValue(Z);
|
||||||
|
|
||||||
|
Vec3 vector = new Vec3(x, y, z);
|
||||||
|
Vec2 rotation = vectorToRotation(vector);
|
||||||
|
targetYaw = rotation.x;
|
||||||
|
targetPitch = rotation.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
//copied from Entity#lookAt (why no helper, Mojang?)
|
||||||
|
public static Vec2 vectorToRotation(Vec3 vector) {
|
||||||
|
double g = Math.sqrt(vector.x * vector.x + vector.z * vector.z);
|
||||||
|
float pitch = Mth.wrapDegrees((float)(-(Mth.atan2(vector.y, g) * 180.0F / (float)Math.PI)));
|
||||||
|
float yaw = Mth.wrapDegrees((float)(Mth.atan2(vector.z, vector.x) * 180.0F / (float)Math.PI) - 90.0F);
|
||||||
|
|
||||||
|
return new Vec2(yaw, pitch);
|
||||||
|
}
|
||||||
|
}
|
||||||
+48
@@ -0,0 +1,48 @@
|
|||||||
|
package io.github.skippyall.minions.minion.program.instruction.move;
|
||||||
|
|
||||||
|
import io.github.skippyall.minions.minion.MinionRuntime;
|
||||||
|
import io.github.skippyall.minions.program.instruction.InstructionExecution;
|
||||||
|
import io.github.skippyall.minions.program.supplier.Parameter;
|
||||||
|
import io.github.skippyall.minions.program.supplier.ParameterValueList;
|
||||||
|
import io.github.skippyall.minions.registration.ValueTypes;
|
||||||
|
import net.minecraft.world.entity.MoverType;
|
||||||
|
import net.minecraft.world.level.storage.ValueInput;
|
||||||
|
import net.minecraft.world.level.storage.ValueOutput;
|
||||||
|
|
||||||
|
public class WalkExecution implements InstructionExecution<MinionRuntime> {
|
||||||
|
public static final Parameter<Double> blocksToMoveParam = new Parameter<>("blocksToMove", ValueTypes.DOUBLE);
|
||||||
|
private static final float ACCURACY = 1F / 32F;
|
||||||
|
|
||||||
|
private double totalBlocksToMove;
|
||||||
|
private double blocksMoved;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void tick(MinionRuntime minion) {
|
||||||
|
double speed = Math.min(minion.getMinion().getSpeed(), totalBlocksToMove - blocksMoved);
|
||||||
|
minion.getMinion().move(MoverType.SELF, minion.getMinion().getDirection().getUnitVec3().normalize().scale(speed));
|
||||||
|
blocksMoved += speed;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isDone(MinionRuntime minion) {
|
||||||
|
return totalBlocksToMove - blocksMoved < ACCURACY;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void readArguments(ParameterValueList parameters, MinionRuntime minion) {
|
||||||
|
totalBlocksToMove = parameters.getValue(blocksToMoveParam).floatValue();
|
||||||
|
blocksMoved = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void save(ValueOutput view, MinionRuntime minion) {
|
||||||
|
view.putDouble("totalBlocksToMove", totalBlocksToMove);
|
||||||
|
view.putDouble("blocksMoved", blocksMoved);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void load(ValueInput view, MinionRuntime minion) {
|
||||||
|
totalBlocksToMove = view.getDoubleOr("totalBlocksToMove", 0F);
|
||||||
|
blocksMoved = view.getDoubleOr("blocksMoved", 0F);
|
||||||
|
}
|
||||||
|
}
|
||||||
+107
@@ -0,0 +1,107 @@
|
|||||||
|
package io.github.skippyall.minions.minion.program.supplier;
|
||||||
|
|
||||||
|
import com.mojang.serialization.Codec;
|
||||||
|
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
||||||
|
import eu.pb4.sgui.api.elements.GuiElementBuilder;
|
||||||
|
import eu.pb4.sgui.api.gui.SimpleGui;
|
||||||
|
import io.github.skippyall.minions.clipboard.BlockPosClipboard;
|
||||||
|
import io.github.skippyall.minions.gui.MinionsGui;
|
||||||
|
import io.github.skippyall.minions.gui.minion.SimpleMinionsGui;
|
||||||
|
import io.github.skippyall.minions.minion.MinionRuntime;
|
||||||
|
import io.github.skippyall.minions.program.supplier.ValueSupplier;
|
||||||
|
import io.github.skippyall.minions.program.supplier.ValueSupplierType;
|
||||||
|
import io.github.skippyall.minions.program.value.ValueType;
|
||||||
|
import io.github.skippyall.minions.registration.MinionBlocks;
|
||||||
|
import io.github.skippyall.minions.registration.MinionComponentTypes;
|
||||||
|
import io.github.skippyall.minions.registration.MinionItems;
|
||||||
|
import io.github.skippyall.minions.registration.ValueSuppliers;
|
||||||
|
import io.github.skippyall.minions.registration.ValueTypes;
|
||||||
|
import net.minecraft.core.BlockPos;
|
||||||
|
import net.minecraft.network.chat.Component;
|
||||||
|
import net.minecraft.resources.ResourceKey;
|
||||||
|
import net.minecraft.world.inventory.MenuType;
|
||||||
|
import net.minecraft.world.item.ItemStack;
|
||||||
|
import net.minecraft.world.level.Level;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
|
public class AnalogInputSupplier implements ValueSupplier<Long, MinionRuntime> {
|
||||||
|
public static final Codec<AnalogInputSupplier> CODEC = RecordCodecBuilder.create(instance ->
|
||||||
|
instance.group(
|
||||||
|
Level.RESOURCE_KEY_CODEC.fieldOf("analogInputWorld").forGetter(s -> s.analogInputWorld),
|
||||||
|
BlockPos.CODEC.fieldOf("analogInputPos").forGetter(s -> s.analogInputPos)
|
||||||
|
).apply(instance, AnalogInputSupplier::new));
|
||||||
|
|
||||||
|
private final ResourceKey<Level> analogInputWorld;
|
||||||
|
private final BlockPos analogInputPos;
|
||||||
|
|
||||||
|
public AnalogInputSupplier(ResourceKey<Level> analogInputWorld, BlockPos analogInputPos) {
|
||||||
|
this.analogInputWorld = analogInputWorld;
|
||||||
|
this.analogInputPos = analogInputPos;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Long resolve(MinionRuntime minion) {
|
||||||
|
Level world = minion.getMinion().getServer().getLevel(analogInputWorld);
|
||||||
|
if(world != null && world.isLoaded(analogInputPos) && world.getBlockState(analogInputPos).is(MinionBlocks.ANALOG_INPUT_BLOCK)) {
|
||||||
|
return (long) world.getBestNeighborSignal(analogInputPos);
|
||||||
|
} else {
|
||||||
|
return 0L;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ValueType<Long> getValueType() {
|
||||||
|
return ValueTypes.LONG;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ValueSupplierType<MinionRuntime> getType() {
|
||||||
|
return ValueSuppliers.ANALOG_INPUT;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Component getDisplayText() {
|
||||||
|
return Component.translatable("value_supplier.minions.analog_input.display", analogInputPos.toShortString(), analogInputWorld.identifier().toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class AnalogInputSupplierType extends ValueSupplierType<MinionRuntime> {
|
||||||
|
@Override
|
||||||
|
public <T> Codec<? extends ValueSupplier<T, MinionRuntime>> getCodec(ValueType<T> type) {
|
||||||
|
if(type == ValueTypes.LONG) {
|
||||||
|
return ValueSupplier.castCodec(CODEC, ValueTypes.LONG, type);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> CompletableFuture<ValueSupplier<?, MinionRuntime>> openConfiguration(MinionsGui parent, ValueType<T> valueType, @Nullable ValueSupplier<?, MinionRuntime> previous) {
|
||||||
|
CompletableFuture<ValueSupplier<?, MinionRuntime>> future = new CompletableFuture<>();
|
||||||
|
new SimpleMinionsGui(parent, (onClose, me) -> {
|
||||||
|
SimpleGui gui = new SimpleGui(MenuType.GENERIC_3x3, parent.viewer, false) {
|
||||||
|
@Override
|
||||||
|
public void onPlayerClose(boolean success) {
|
||||||
|
onClose.run();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
gui.setTitle(Component.translatable("value_supplier.minions.analog_input"));
|
||||||
|
|
||||||
|
gui.setSlot(2, me.backButton());
|
||||||
|
gui.setSlot(4, new GuiElementBuilder(MinionItems.REFERENCE_ITEM)
|
||||||
|
.setCallback(() -> {
|
||||||
|
ItemStack cursor = parent.viewer.containerMenu.getCarried();
|
||||||
|
if (cursor.is(MinionItems.REFERENCE_ITEM) && cursor.get(MinionComponentTypes.REFERENCE) instanceof BlockPosClipboard pos) {
|
||||||
|
future.complete(new AnalogInputSupplier(pos.world(), pos.pos()));
|
||||||
|
me.goBack();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.setItemName(Component.translatable("value_supplier.minions.analog_input.config.click_with_reference"))
|
||||||
|
);
|
||||||
|
gui.open();
|
||||||
|
return gui;
|
||||||
|
});
|
||||||
|
return future;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,27 +1,112 @@
|
|||||||
package io.github.skippyall.minions.minion.skin;
|
package io.github.skippyall.minions.minion.skin;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableMultimap;
|
||||||
|
import com.mojang.authlib.GameProfile;
|
||||||
import com.mojang.authlib.properties.Property;
|
import com.mojang.authlib.properties.Property;
|
||||||
import com.mojang.authlib.properties.PropertyMap;
|
import com.mojang.authlib.properties.PropertyMap;
|
||||||
import io.github.skippyall.minions.input.TextInput;
|
import io.github.skippyall.minions.Minions;
|
||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
import io.github.skippyall.minions.gui.MinionsGui;
|
||||||
import net.minecraft.text.Text;
|
import net.fabricmc.fabric.api.entity.FakePlayer;
|
||||||
|
import net.minecraft.core.Holder;
|
||||||
|
import net.minecraft.core.registries.Registries;
|
||||||
|
import net.minecraft.nbt.CompoundTag;
|
||||||
|
import net.minecraft.nbt.Tag;
|
||||||
|
import net.minecraft.network.chat.Component;
|
||||||
|
import net.minecraft.resources.Identifier;
|
||||||
|
import net.minecraft.resources.ResourceKey;
|
||||||
|
import net.minecraft.server.dialog.ActionButton;
|
||||||
|
import net.minecraft.server.dialog.CommonButtonData;
|
||||||
|
import net.minecraft.server.dialog.CommonDialogData;
|
||||||
|
import net.minecraft.server.dialog.Dialog;
|
||||||
|
import net.minecraft.server.dialog.DialogAction;
|
||||||
|
import net.minecraft.server.dialog.Input;
|
||||||
|
import net.minecraft.server.dialog.NoticeDialog;
|
||||||
|
import net.minecraft.server.dialog.action.CustomAll;
|
||||||
|
import net.minecraft.server.dialog.input.TextInput;
|
||||||
|
import net.minecraft.world.item.component.ResolvableProfile;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
public class Base64SkinProvider implements SkinProvider {
|
public class Base64SkinProvider implements SkinProvider {
|
||||||
|
public static final ResourceKey<Dialog> DIALOG = ResourceKey.create(Registries.DIALOG, Identifier.fromNamespaceAndPath(Minions.MOD_ID, "base_64_input"));
|
||||||
|
public static final Identifier CUSTOM_DIALOG_ACTION = Identifier.fromNamespaceAndPath(Minions.MOD_ID, "base_64_submit");
|
||||||
|
|
||||||
|
private static long dialogIdCounter = 0;
|
||||||
|
private static Map<Long, CompletableFuture<ResolvableProfile>> futures = new HashMap<>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CompletableFuture<Optional<PropertyMap>> openSkinMenu(ServerPlayerEntity player) {
|
public CompletableFuture<ResolvableProfile> openSkinMenu(MinionsGui parent) {
|
||||||
return TextInput.inputString(player, Text.translatable("minions.gui.look.skin.base64.title"), "")
|
dialogIdCounter++;
|
||||||
.thenApply(base64String -> {
|
parent.viewer.openDialog(getDialog());
|
||||||
PropertyMap map = new PropertyMap();
|
CompletableFuture<ResolvableProfile> future = new CompletableFuture<>();
|
||||||
map.put("textures", new Property("textures", base64String));
|
futures.put(dialogIdCounter, future);
|
||||||
return Optional.of(map);
|
return future;
|
||||||
});
|
}
|
||||||
|
|
||||||
|
public static void onCustomDialogAction(Optional<Tag> element) {
|
||||||
|
if(element.isPresent() && element.get() instanceof CompoundTag compound) {
|
||||||
|
Optional<Long> id = compound.getLong("dialog_id");
|
||||||
|
Optional<String> base64 = compound.getString("base_64");
|
||||||
|
if(id.isPresent() && base64.isPresent() && !base64.get().isBlank()) {
|
||||||
|
if(futures.containsKey(id.get())) {
|
||||||
|
PropertyMap map = new PropertyMap(ImmutableMultimap.of(
|
||||||
|
"textures", new Property("textures", base64.get().strip())
|
||||||
|
));
|
||||||
|
|
||||||
|
futures.get(id.get()).complete(ResolvableProfile.createResolved(new GameProfile(FakePlayer.DEFAULT_UUID, "", map)));
|
||||||
|
futures.remove(id.get());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Holder<Dialog> getDialog() {
|
||||||
|
CompoundTag additionalData = new CompoundTag();
|
||||||
|
additionalData.putLong("dialog_id", dialogIdCounter);
|
||||||
|
return Holder.direct(
|
||||||
|
new NoticeDialog(
|
||||||
|
new CommonDialogData(
|
||||||
|
Component.translatable("minions.gui.look.skin.base64.title"),
|
||||||
|
Optional.empty(),
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
DialogAction.CLOSE,
|
||||||
|
List.of(),
|
||||||
|
List.of(
|
||||||
|
new Input("base_64", new TextInput(
|
||||||
|
200,
|
||||||
|
Component.empty(),
|
||||||
|
false,
|
||||||
|
"",
|
||||||
|
2000,
|
||||||
|
Optional.empty()
|
||||||
|
))
|
||||||
|
)
|
||||||
|
),
|
||||||
|
new ActionButton(
|
||||||
|
new CommonButtonData(
|
||||||
|
Component.translatable("gui.ok"),
|
||||||
|
150
|
||||||
|
),
|
||||||
|
Optional.of(
|
||||||
|
new CustomAll(
|
||||||
|
CUSTOM_DIALOG_ACTION,
|
||||||
|
Optional.of(
|
||||||
|
additionalData
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Text getDisplayName() {
|
public Component getDisplayName() {
|
||||||
return Text.translatable("minions.gui.look.skin.base64");
|
return Component.translatable("minions.gui.look.skin.base64");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,25 +1,21 @@
|
|||||||
package io.github.skippyall.minions.minion.skin;
|
package io.github.skippyall.minions.minion.skin;
|
||||||
|
|
||||||
import com.mojang.authlib.GameProfile;
|
import io.github.skippyall.minions.gui.MinionsGui;
|
||||||
import com.mojang.authlib.properties.PropertyMap;
|
import io.github.skippyall.minions.gui.input.TextInput;
|
||||||
import io.github.skippyall.minions.input.TextInput;
|
import net.minecraft.network.chat.Component;
|
||||||
import net.minecraft.block.entity.SkullBlockEntity;
|
import net.minecraft.world.item.component.ResolvableProfile;
|
||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
|
||||||
import net.minecraft.text.Text;
|
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
public class NameSkinProvider implements SkinProvider {
|
public class NameSkinProvider implements SkinProvider {
|
||||||
@Override
|
@Override
|
||||||
public CompletableFuture<Optional<PropertyMap>> openSkinMenu(ServerPlayerEntity player) {
|
public CompletableFuture<ResolvableProfile> openSkinMenu(MinionsGui parent) {
|
||||||
return TextInput.inputString(player, Text.translatable("minions.gui.look.skin.name.title"), "")
|
return TextInput.inputStringFuture(parent, Component.translatable("minions.gui.look.skin.name.title"), "")
|
||||||
.thenCompose(SkullBlockEntity::fetchProfileByName)
|
.thenApply(name -> name != null ? ResolvableProfile.createUnresolved(name) : null);
|
||||||
.thenApply(gameProfile -> gameProfile.map(GameProfile::getProperties));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Text getDisplayName() {
|
public Component getDisplayName() {
|
||||||
return Text.translatable("minions.gui.look.skin.name");
|
return Component.translatable("minions.gui.look.skin.name");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,13 @@
|
|||||||
package io.github.skippyall.minions.minion.skin;
|
package io.github.skippyall.minions.minion.skin;
|
||||||
|
|
||||||
import com.mojang.authlib.properties.PropertyMap;
|
import io.github.skippyall.minions.gui.MinionsGui;
|
||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
import net.minecraft.network.chat.Component;
|
||||||
import net.minecraft.text.Text;
|
import net.minecraft.world.item.component.ResolvableProfile;
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
public interface SkinProvider {
|
public interface SkinProvider {
|
||||||
CompletableFuture<Optional<PropertyMap>> openSkinMenu(ServerPlayerEntity player);
|
CompletableFuture<ResolvableProfile> openSkinMenu(MinionsGui parent);
|
||||||
|
|
||||||
Text getDisplayName();
|
Component getDisplayName();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,25 +0,0 @@
|
|||||||
package io.github.skippyall.minions.minion.skin;
|
|
||||||
|
|
||||||
import com.mojang.serialization.Lifecycle;
|
|
||||||
import io.github.skippyall.minions.Minions;
|
|
||||||
import net.minecraft.registry.Registry;
|
|
||||||
import net.minecraft.registry.RegistryKey;
|
|
||||||
import net.minecraft.registry.SimpleRegistry;
|
|
||||||
import net.minecraft.util.Identifier;
|
|
||||||
|
|
||||||
public class SkinProviders {
|
|
||||||
public static final Registry<SkinProvider> SKIN_PROVIDERS = new SimpleRegistry<>(RegistryKey.ofRegistry(Identifier.of(Minions.MOD_ID, "skin_providers")), Lifecycle.stable());
|
|
||||||
|
|
||||||
public static NameSkinProvider NAME = register(new NameSkinProvider(), "name");
|
|
||||||
public static UUIDSkinProvider UUID = register(new UUIDSkinProvider(), "uuid");
|
|
||||||
public static Base64SkinProvider BASE64 = register(new Base64SkinProvider(), "base64");
|
|
||||||
|
|
||||||
|
|
||||||
public static <T extends SkinProvider> T register(T skinProvider, String path) {
|
|
||||||
return Registry.register(SKIN_PROVIDERS, Identifier.of(Minions.MOD_ID, path), skinProvider);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void register() {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,32 +1,21 @@
|
|||||||
package io.github.skippyall.minions.minion.skin;
|
package io.github.skippyall.minions.minion.skin;
|
||||||
|
|
||||||
import com.mojang.authlib.GameProfile;
|
import io.github.skippyall.minions.gui.MinionsGui;
|
||||||
import com.mojang.authlib.properties.PropertyMap;
|
import io.github.skippyall.minions.gui.input.TextInput;
|
||||||
import com.mojang.serialization.Codec;
|
import net.minecraft.network.chat.Component;
|
||||||
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
import net.minecraft.world.item.component.ResolvableProfile;
|
||||||
import io.github.skippyall.minions.input.TextInput;
|
|
||||||
import io.github.skippyall.minions.minion.MinionProfileUtils;
|
|
||||||
import net.minecraft.block.entity.SkullBlockEntity;
|
|
||||||
import net.minecraft.server.MinecraftServer;
|
|
||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
|
||||||
import net.minecraft.text.Text;
|
|
||||||
import net.minecraft.util.Uuids;
|
|
||||||
import net.minecraft.util.dynamic.Codecs;
|
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.UUID;
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
public class UUIDSkinProvider implements SkinProvider {
|
public class UUIDSkinProvider implements SkinProvider {
|
||||||
@Override
|
@Override
|
||||||
public CompletableFuture<Optional<PropertyMap>> openSkinMenu(ServerPlayerEntity player) {
|
public CompletableFuture<ResolvableProfile> openSkinMenu(MinionsGui parent) {
|
||||||
return TextInput.inputString(player, Text.translatable("minions.gui.look.skin.uuid.title"), "")
|
return TextInput.inputStringFuture(parent, Component.translatable("minions.gui.look.skin.uuid.title"), "")
|
||||||
.thenCompose(uuidString -> SkullBlockEntity.fetchProfileByUuid(UUID.fromString(uuidString)))
|
.thenApply(name -> name != null ? ResolvableProfile.createUnresolved(name) : null);
|
||||||
.thenApply(gameProfile -> gameProfile.map(GameProfile::getProperties));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Text getDisplayName() {
|
public Component getDisplayName() {
|
||||||
return Text.translatable("minions.gui.look.skin.uuid");
|
return Component.translatable("minions.gui.look.skin.uuid");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user