Compare commits
37 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| bc6bbc448a | |||
| 9b61dba4c7 | |||
| 0382798a5d | |||
| 1e430ef506 | |||
| 5bd7c08f83 | |||
| 017b2ec590 | |||
| 382b394523 | |||
| 3f2a52fd0a | |||
| c5b3c883ca | |||
| b413592abf | |||
| cc1320d5c6 | |||
| da4351cff2 | |||
| 640e6f801a | |||
| bcfb406547 | |||
| b6f7bfc57b | |||
| e27b1698e4 | |||
| 216855d9c9 | |||
| e42e3adf84 | |||
| 66cbad38cb | |||
| 6640f95291 | |||
| 362bf15a19 | |||
| 2d64426496 | |||
| f5e0c1d426 | |||
| ec31e090b9 | |||
| 935b2225c9 | |||
| cdd0a594f3 | |||
| a9d0b7fd94 | |||
| 1931848068 | |||
| a2a189a0c1 | |||
| d2d96b3f67 | |||
| 08e7952e34 | |||
| cc69b22aba | |||
| c7ff6de42b | |||
| d3b25b595a | |||
| 8007778c39 | |||
| fc70b2fe5a | |||
| 2f35493580 |
@@ -1,5 +1,6 @@
|
|||||||
# User-specific stuff
|
# User-specific stuff
|
||||||
.idea/
|
.idea/
|
||||||
|
urlaub/
|
||||||
|
|
||||||
*.iml
|
*.iml
|
||||||
*.ipr
|
*.ipr
|
||||||
|
|||||||
+45
-8
@@ -1,5 +1,5 @@
|
|||||||
plugins {
|
plugins {
|
||||||
id 'fabric-loom' version '1.6-SNAPSHOT'
|
id 'net.fabricmc.fabric-loom-remap' version '1.14-SNAPSHOT'
|
||||||
id 'maven-publish'
|
id 'maven-publish'
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -10,6 +10,22 @@ base {
|
|||||||
archivesName = project.archives_base_name
|
archivesName = project.archives_base_name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
loom {
|
||||||
|
accessWidenerPath = file("src/main/resources/minions.accesswidener")
|
||||||
|
splitEnvironmentSourceSets()
|
||||||
|
|
||||||
|
mods {
|
||||||
|
minions {
|
||||||
|
sourceSet sourceSets.main
|
||||||
|
sourceSet sourceSets.client
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
runs.forEach {
|
||||||
|
it.vmArg("-XX:+AllowEnhancedClassRedefinition")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
// Add repositories to retrieve artifacts from in here.
|
// Add repositories to retrieve artifacts from in here.
|
||||||
// You should only use this when depending on other mods because
|
// You should only use this when depending on other mods because
|
||||||
@@ -17,6 +33,18 @@ repositories {
|
|||||||
// See https://docs.gradle.org/current/userguide/declaring_repositories.html
|
// See https://docs.gradle.org/current/userguide/declaring_repositories.html
|
||||||
// for more information about repositories.
|
// for more information about repositories.
|
||||||
maven { url 'https://maven.nucleoid.xyz' }
|
maven { url 'https://maven.nucleoid.xyz' }
|
||||||
|
|
||||||
|
exclusiveContent {
|
||||||
|
forRepository {
|
||||||
|
maven {
|
||||||
|
name = "Modrinth"
|
||||||
|
url = "https://api.modrinth.com/maven"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
filter {
|
||||||
|
includeGroup "maven.modrinth"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
@@ -27,9 +55,18 @@ dependencies {
|
|||||||
|
|
||||||
// Fabric API. This is technically optional, but you probably want it anyway.
|
// Fabric API. This is technically optional, but you probably want it anyway.
|
||||||
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
|
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
|
||||||
|
|
||||||
modImplementation "eu.pb4:polymer-core:${project.polymer_version}"
|
modImplementation "eu.pb4:polymer-core:${project.polymer_version}"
|
||||||
|
modImplementation "eu.pb4:polymer-virtual-entity:${project.polymer_version}"
|
||||||
|
modImplementation "eu.pb4:polymer-resource-pack:${project.polymer_version}"
|
||||||
|
modLocalRuntime "eu.pb4:polymer-autohost:${project.polymer_version}"
|
||||||
modImplementation include("eu.pb4:sgui:${project.sgui_version}")
|
modImplementation include("eu.pb4:sgui:${project.sgui_version}")
|
||||||
modImplementation include("xyz.nucleoid:server-translations-api:${project.server_translations_version}")
|
modImplementation include("xyz.nucleoid:server-translations-api:${project.server_translations_version}")
|
||||||
|
|
||||||
|
implementation include("com.electronwill.night-config:toml:${project.night_config_version}")
|
||||||
|
compileOnly("io.netty:netty-codec-http:${project.netty_version}.Final")
|
||||||
|
|
||||||
|
modCompileOnly "maven.modrinth:universal-graves:${project.universal_graves_version}"
|
||||||
}
|
}
|
||||||
|
|
||||||
processResources {
|
processResources {
|
||||||
@@ -70,14 +107,14 @@ java {
|
|||||||
|
|
||||||
jar {
|
jar {
|
||||||
from("LICENSE") {
|
from("LICENSE") {
|
||||||
rename { "${it}_${project.archivesBaseName}"}
|
rename { "${it}_${project.archives_base_name}"}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// configure the maven publication
|
// configure the maven publication
|
||||||
publishing {
|
publishing {
|
||||||
publications {
|
publications {
|
||||||
minions (MavenPublication) {
|
maven (MavenPublication) {
|
||||||
from components.java
|
from components.java
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -88,14 +125,14 @@ publishing {
|
|||||||
// Notice: This block does NOT have the same function as the block in the top level.
|
// 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
|
// The repositories here will be used for publishing your artifact, not for
|
||||||
// retrieving dependencies.
|
// retrieving dependencies.
|
||||||
|
if(project.hasProperty("foxgalaxy_user_name") && project.hasProperty("foxgalaxy_password")) {
|
||||||
maven {
|
maven {
|
||||||
def release = "https://maven.foxgalaxy.de/private-releases"
|
url = "https://maven.foxgalaxy.de/private"
|
||||||
def snapshot = "https://maven.foxgalaxy.de/private-snapshot"
|
|
||||||
url = version.endsWith('SNAPSHOT') ? snapshot : release
|
|
||||||
|
|
||||||
credentials {
|
credentials {
|
||||||
username = "${maven_user}"
|
username = project.property("foxgalaxy_user_name")
|
||||||
password = "${maven_password}"
|
password = project.property("foxgalaxy_password")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-8
@@ -3,19 +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.1
|
minecraft_version=1.21.7
|
||||||
loader_version=0.16.3
|
loader_version=0.16.14
|
||||||
yarn_mappings=1.21.1+build.3
|
yarn_mappings=1.21.7+build.2
|
||||||
|
|
||||||
# Mod Properties
|
# Mod Properties
|
||||||
mod_version = 0.0.1-SNAPSHOT
|
mod_version = 1.0.0-SNAPSHOT-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.103.0+1.21.1
|
fabric_version=0.128.1+1.21.7
|
||||||
|
|
||||||
polymer_version=0.9.12+1.21.1
|
polymer_version=0.13.3+1.21.6
|
||||||
sgui_version=1.6.0+1.21
|
sgui_version=1.10.0+1.21.6
|
||||||
server_translations_version=2.3.1+1.21-pre2
|
server_translations_version=2.5.1+1.21.5
|
||||||
|
|
||||||
|
night_config_version=3.8.3
|
||||||
|
netty_version=4.1.130
|
||||||
|
|
||||||
|
universal_graves_version=3.8.0+1.21.6
|
||||||
|
|||||||
+1
-1
@@ -1 +1 @@
|
|||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
|
distributionUrl=https\://services.gradle.org/distributions/gradle-9.2.1-bin.zip
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package io.github.skippyall.minions.client;
|
||||||
|
|
||||||
|
import eu.pb4.polymer.networking.api.client.PolymerClientNetworking;
|
||||||
|
import io.github.skippyall.minions.registration.MinionBlocks;
|
||||||
|
import io.github.skippyall.minions.util.PolymerUtil;
|
||||||
|
import net.fabricmc.api.ClientModInitializer;
|
||||||
|
import net.fabricmc.fabric.api.client.rendering.v1.BlockRenderLayerMap;
|
||||||
|
import net.minecraft.client.render.BlockRenderLayer;
|
||||||
|
|
||||||
|
public class MinionsClient implements ClientModInitializer {
|
||||||
|
@Override
|
||||||
|
public void onInitializeClient() {
|
||||||
|
BlockRenderLayerMap.putBlock(MinionBlocks.MINION_TRIGGER_BLOCK, BlockRenderLayer.TRANSLUCENT);
|
||||||
|
|
||||||
|
PolymerClientNetworking.registerCommonHandler(PolymerUtil.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.mixins.compat.universal_graves.")) {
|
||||||
|
return MinionsConfig.get().compat.enableGravesCompat && FabricLoader.getInstance().isModLoaded("universal-graves");
|
||||||
|
}
|
||||||
|
if(mixinClassName.startsWith("io.github.skippyall.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,65 +1,46 @@
|
|||||||
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.fakeplayer.MinionFakePlayer;
|
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
|
||||||
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.registration.MinionRegistration;
|
||||||
|
import io.github.skippyall.minions.util.PolymerUtil;
|
||||||
import net.fabricmc.api.ModInitializer;
|
import net.fabricmc.api.ModInitializer;
|
||||||
|
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.minecraft.item.Item;
|
|
||||||
import net.minecraft.item.Items;
|
|
||||||
import net.minecraft.registry.Registries;
|
|
||||||
import net.minecraft.registry.Registry;
|
|
||||||
import net.minecraft.server.MinecraftServer;
|
|
||||||
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;
|
|
||||||
|
|
||||||
public class Minions implements ModInitializer {
|
public class Minions implements ModInitializer {
|
||||||
public static final String MOD_ID = "minions";
|
public static final String MOD_ID = "minions";
|
||||||
public static final MinionItem MINION_ITEM = Registry.register(Registries.ITEM, Identifier.of(MOD_ID, "minion"), new MinionItem(false));
|
|
||||||
public static final SimplePolymerItem BASIC_UPGRADE_BASE = Registry.register(Registries.ITEM, Identifier.of(MOD_ID, "basic_upgrade_base"), new SimplePolymerItem(new Item.Settings(), Items.NETHERITE_UPGRADE_SMITHING_TEMPLATE));
|
|
||||||
|
|
||||||
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
|
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
|
||||||
|
|
||||||
private static final List<Runnable> executeOnNextTick = new ArrayList<>();
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onInitialize() {
|
public void onInitialize() {
|
||||||
LOGGER.debug("Add Customthing");
|
MinionsConfig.get();
|
||||||
PolymerEntityUtils.registerType();
|
|
||||||
|
MinionRegistration.register();
|
||||||
|
|
||||||
|
PolymerUtil.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(data -> {
|
if(data.isSpawned()) {
|
||||||
System.out.println("spawn Minion " + data.name);
|
MinionFakePlayer.spawnMinion(data, server.getOverworld(), null, null, true);
|
||||||
MinionFakePlayer.spawnMinionAt(data, server.getOverworld(), null, null);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
ServerTickEvents.START_SERVER_TICK.register(server -> {
|
|
||||||
exec(() -> {
|
|
||||||
for (Runnable run:executeOnNextTick) {
|
|
||||||
run.run();
|
|
||||||
}
|
}
|
||||||
executeOnNextTick.clear();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
Modules.register();
|
CommandRegistrationCallback.EVENT.register(MinionsCommand::register);
|
||||||
}
|
|
||||||
|
|
||||||
private static synchronized void exec(Runnable run) {
|
/*ServerBlockEntityEvents.BLOCK_ENTITY_LOAD.register((blockEntity, world) -> {
|
||||||
run.run();
|
if(blockEntity instanceof MinionTriggerBlockEntity) {
|
||||||
|
world.updateComparators(blockEntity.getPos(), MinionBlocks.MINION_TRIGGER_BLOCK);
|
||||||
}
|
}
|
||||||
|
});*/
|
||||||
|
|
||||||
public static void addExecuteOnNextTick(Runnable run) {
|
PolymerResourcePackUtils.addModAssets(Minions.MOD_ID);
|
||||||
exec(() -> {
|
|
||||||
executeOnNextTick.add(run);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,91 @@
|
|||||||
|
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.core.serde.annotations.SerdeSkipDeserializingIf;
|
||||||
|
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;
|
||||||
|
|
||||||
|
import static com.electronwill.nightconfig.core.serde.annotations.SerdeSkipDeserializingIf.SkipDeIf.IS_MISSING;
|
||||||
|
|
||||||
|
public class MinionsConfig {
|
||||||
|
private static MinionsConfig INSTANCE;
|
||||||
|
|
||||||
|
@SerdeSkipDeserializingIf(IS_MISSING)
|
||||||
|
public Minion minion = new Minion();
|
||||||
|
|
||||||
|
public static class Minion {
|
||||||
|
@SerdeComment("The prefix for all minion names")
|
||||||
|
@SerdeSkipDeserializingIf(IS_MISSING)
|
||||||
|
public String minionPrefix = "+";
|
||||||
|
|
||||||
|
@SerdeComment("Makes minions not raise the mob cap if they can't spawn mobs.")
|
||||||
|
@SerdeComment("Might cause incompatibilities.")
|
||||||
|
@SerdeSkipDeserializingIf(IS_MISSING)
|
||||||
|
public boolean enableMobCapHacks = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SerdeSkipDeserializingIf(IS_MISSING)
|
||||||
|
public Compat compat = new Compat();
|
||||||
|
|
||||||
|
public static class Compat {
|
||||||
|
@SerdeComment("Enables compat with Universal Graves, which allows everyone to pick up graves from minions")
|
||||||
|
@SerdeSkipDeserializingIf(IS_MISSING)
|
||||||
|
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 config = new TomlParser().parse(getPath(), (file, configFormat) -> {
|
||||||
|
CommentedConfig defaultConfig = ObjectSerializer.standard().serializeFields(new MinionsConfig(), TomlFormat::newConfig);
|
||||||
|
configFormat.createWriter().write(defaultConfig, file, WritingMode.REPLACE);
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
}
|
||||||
+177
@@ -0,0 +1,177 @@
|
|||||||
|
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.core.api.utils.PolymerKeepModel;
|
||||||
|
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.registration.MinionBlocks;
|
||||||
|
import io.github.skippyall.minions.Minions;
|
||||||
|
import io.github.skippyall.minions.minion.MinionPersistentState;
|
||||||
|
import io.github.skippyall.minions.clipboard.InstructionClipboard;
|
||||||
|
import io.github.skippyall.minions.registration.MinionComponentTypes;
|
||||||
|
import io.github.skippyall.minions.util.PolymerUtil;
|
||||||
|
import net.minecraft.block.AbstractRedstoneGateBlock;
|
||||||
|
import net.minecraft.block.Block;
|
||||||
|
import net.minecraft.block.BlockState;
|
||||||
|
import net.minecraft.block.BlockWithEntity;
|
||||||
|
import net.minecraft.block.ShapeContext;
|
||||||
|
import net.minecraft.block.SideShapeType;
|
||||||
|
import net.minecraft.block.entity.BlockEntity;
|
||||||
|
import net.minecraft.component.DataComponentTypes;
|
||||||
|
import net.minecraft.entity.player.PlayerEntity;
|
||||||
|
import net.minecraft.item.ItemStack;
|
||||||
|
import net.minecraft.item.Items;
|
||||||
|
import net.minecraft.server.network.ServerPlayNetworkHandler;
|
||||||
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
|
import net.minecraft.server.world.ServerWorld;
|
||||||
|
import net.minecraft.sound.SoundCategory;
|
||||||
|
import net.minecraft.sound.SoundEvents;
|
||||||
|
import net.minecraft.state.StateManager;
|
||||||
|
import net.minecraft.state.property.BooleanProperty;
|
||||||
|
import net.minecraft.text.Text;
|
||||||
|
import net.minecraft.util.ActionResult;
|
||||||
|
import net.minecraft.util.Hand;
|
||||||
|
import net.minecraft.util.Identifier;
|
||||||
|
import net.minecraft.util.hit.BlockHitResult;
|
||||||
|
import net.minecraft.util.math.BlockPos;
|
||||||
|
import net.minecraft.util.math.Direction;
|
||||||
|
import net.minecraft.util.shape.VoxelShape;
|
||||||
|
import net.minecraft.world.BlockView;
|
||||||
|
import net.minecraft.world.World;
|
||||||
|
import net.minecraft.world.WorldView;
|
||||||
|
import net.minecraft.world.block.WireOrientation;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import xyz.nucleoid.packettweaker.PacketContext;
|
||||||
|
|
||||||
|
public class MinionTriggerBlock extends BlockWithEntity implements PolymerBlock, PolymerKeepModel, PolymerClientDecoded, BlockWithElementHolder {
|
||||||
|
public static final MapCodec<MinionTriggerBlock> CODEC = createCodec(MinionTriggerBlock::new);
|
||||||
|
|
||||||
|
public static final BooleanProperty POWERED = BooleanProperty.of("powered");
|
||||||
|
public static final VoxelShape SHAPE = Block.createColumnShape(16.0, 0.0, 2.0);
|
||||||
|
|
||||||
|
public MinionTriggerBlock(Settings settings) {
|
||||||
|
super(settings);
|
||||||
|
setDefaultState(getDefaultState().with(POWERED, false));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
|
||||||
|
return SHAPE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean canPlaceAt(BlockState state, WorldView world, BlockPos pos) {
|
||||||
|
BlockPos blockPos = pos.down();
|
||||||
|
return this.canPlaceAbove(world, blockPos, world.getBlockState(blockPos));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean canPlaceAbove(WorldView world, BlockPos pos, BlockState state) {
|
||||||
|
return state.isSideSolid(world, pos, Direction.UP, SideShapeType.RIGID);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onStateReplaced(BlockState state, ServerWorld world, BlockPos pos, boolean moved) {
|
||||||
|
super.onStateReplaced(state, world, pos, moved);
|
||||||
|
world.getBlockEntity(pos, MinionBlocks.MINION_TRIGGER_BE_TYPE).ifPresent(MinionTriggerBlockEntity::removeListener);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
|
||||||
|
builder.add(POWERED);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected ActionResult onUseWithItem(ItemStack stack, BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
|
||||||
|
if(stack.get(MinionComponentTypes.REFERENCE) instanceof InstructionClipboard instruction) {
|
||||||
|
world.getBlockEntity(pos, MinionBlocks.MINION_TRIGGER_BE_TYPE).ifPresent(be -> {
|
||||||
|
be.setInstruction(instruction.selectedMinion(), instruction.selectedInstruction());
|
||||||
|
player.playSoundToPlayer(SoundEvents.BLOCK_NOTE_BLOCK_CHIME.value(), SoundCategory.BLOCKS, 1, 1);
|
||||||
|
stack.decrement(1);
|
||||||
|
});
|
||||||
|
return ActionResult.SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
return super.onUseWithItem(stack, state, world, pos, player, hand, hit);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hit) {
|
||||||
|
if(world.isClient()) {
|
||||||
|
return ActionResult.CONSUME;
|
||||||
|
}
|
||||||
|
|
||||||
|
world.getBlockEntity(pos, MinionBlocks.MINION_TRIGGER_BE_TYPE).ifPresent(be -> {
|
||||||
|
String name = MinionPersistentState.get(world.getServer()).getMinionData(be.getMinionUuid()).name();
|
||||||
|
player.sendMessage(Text.translatable("minions.reference.instruction.tooltip", name, be.getInstructionName()), true);
|
||||||
|
});
|
||||||
|
return ActionResult.SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void neighborUpdate(BlockState state, World world, BlockPos pos, Block sourceBlock, @Nullable WireOrientation wireOrientation, boolean notify) {
|
||||||
|
if(!canPlaceAt(state, world, pos)) {
|
||||||
|
dropStacks(state, world, pos);
|
||||||
|
world.removeBlock(pos, false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean newPower = world.isReceivingRedstonePower(pos);
|
||||||
|
if(state.get(POWERED) != newPower) {
|
||||||
|
world.setBlockState(pos, state.with(POWERED, newPower));
|
||||||
|
world.getBlockEntity(pos, MinionBlocks.MINION_TRIGGER_BE_TYPE).ifPresent(MinionTriggerBlockEntity::updatePower);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean hasComparatorOutput(BlockState state) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected int getComparatorOutput(BlockState state, World world, BlockPos pos) {
|
||||||
|
return world.getBlockEntity(pos, MinionBlocks.MINION_TRIGGER_BE_TYPE).map(MinionTriggerBlockEntity::getComparatorOutput).orElse(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected MapCodec<? extends BlockWithEntity> getCodec() {
|
||||||
|
return CODEC;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @Nullable BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
|
||||||
|
return new MinionTriggerBlockEntity(pos, state);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BlockState getPolymerBlockState(BlockState state, PacketContext context) {
|
||||||
|
return PolymerUtil.isOnClient(context) ? state : net.minecraft.block.Blocks.COMPARATOR.getDefaultState().with(AbstractRedstoneGateBlock.POWERED, state.get(POWERED));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean handleMiningOnServer(ItemStack tool, BlockState state, BlockPos pos, ServerPlayerEntity player) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @Nullable ElementHolder createElementHolder(ServerWorld world, BlockPos pos, BlockState initialBlockState) {
|
||||||
|
ElementHolder holder = new ElementHolder() {
|
||||||
|
@Override
|
||||||
|
public boolean startWatching(ServerPlayNetworkHandler player) {
|
||||||
|
if(PolymerResourcePackUtils.hasMainPack(player)) {
|
||||||
|
return super.startWatching(player);
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
ItemStack stack = new ItemStack(Items.BARRIER);
|
||||||
|
stack.set(DataComponentTypes.ITEM_MODEL, Identifier.of(Minions.MOD_ID, "minion_trigger_no_plate_" + (initialBlockState.get(MinionTriggerBlock.POWERED) ? "active" : "inactive")));
|
||||||
|
|
||||||
|
holder.addElement(new ItemDisplayElement(stack));
|
||||||
|
return holder;
|
||||||
|
}
|
||||||
|
}
|
||||||
+109
@@ -0,0 +1,109 @@
|
|||||||
|
package io.github.skippyall.minions.block.miniontrigger;
|
||||||
|
|
||||||
|
import io.github.skippyall.minions.listener.BlockEntityMinionListener;
|
||||||
|
import io.github.skippyall.minions.registration.MinionBlocks;
|
||||||
|
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.block.BlockState;
|
||||||
|
import net.minecraft.block.entity.BlockEntity;
|
||||||
|
import net.minecraft.storage.ReadView;
|
||||||
|
import net.minecraft.storage.WriteView;
|
||||||
|
import net.minecraft.util.Uuids;
|
||||||
|
import net.minecraft.util.math.BlockPos;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class MinionTriggerBlockEntity extends BlockEntity {
|
||||||
|
private UUID minionUuid;
|
||||||
|
private String instructionName = "";
|
||||||
|
|
||||||
|
public MinionTriggerBlockEntity(BlockPos pos, BlockState state) {
|
||||||
|
super(MinionBlocks.MINION_TRIGGER_BE_TYPE, pos, state);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeListener() {
|
||||||
|
MinionTriggerMinionListener.removeListener(world, pos, minionUuid, instructionName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addListener() {
|
||||||
|
MinionTriggerMinionListener.addListener(world, pos, minionUuid, instructionName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setInstruction(UUID minionUuid, String instructionName) {
|
||||||
|
removeListener();
|
||||||
|
this.minionUuid = minionUuid;
|
||||||
|
this.instructionName = instructionName;
|
||||||
|
addListener();
|
||||||
|
markDirty();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updatePower() {
|
||||||
|
boolean powered = getCachedState().get(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;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<MinionFakePlayer> getMinion() {
|
||||||
|
if(minionUuid != null && world != null && world.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 MinionTriggerMinionListener getListener() {
|
||||||
|
return BlockEntityMinionListener.getListener(world, pos, minionUuid, MinionTriggerMinionListener.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void readData(ReadView view) {
|
||||||
|
minionUuid = view.read("minionUuid", Uuids.CODEC).orElse(null);
|
||||||
|
instructionName = view.getString("instructionName", "");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void writeData(WriteView view) {
|
||||||
|
if(minionUuid != null) {
|
||||||
|
view.put("minionUuid", Uuids.CODEC, minionUuid);
|
||||||
|
}
|
||||||
|
view.putString("instructionName", instructionName);
|
||||||
|
}
|
||||||
|
}
|
||||||
+25
@@ -0,0 +1,25 @@
|
|||||||
|
package io.github.skippyall.minions.block.miniontrigger;
|
||||||
|
|
||||||
|
import eu.pb4.polymer.core.api.item.PolymerBlockItem;
|
||||||
|
import eu.pb4.polymer.resourcepack.api.PolymerResourcePackUtils;
|
||||||
|
import net.minecraft.block.Block;
|
||||||
|
import net.minecraft.item.Item;
|
||||||
|
import net.minecraft.item.ItemStack;
|
||||||
|
import net.minecraft.util.Identifier;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import xyz.nucleoid.packettweaker.PacketContext;
|
||||||
|
|
||||||
|
public class MinionTriggerBlockItem extends PolymerBlockItem {
|
||||||
|
public MinionTriggerBlockItem(Block block, Settings settings, Item polymerItem) {
|
||||||
|
super(block, settings, polymerItem, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @Nullable Identifier getPolymerItemModel(ItemStack stack, PacketContext context) {
|
||||||
|
if(PolymerResourcePackUtils.hasMainPack(context)) {
|
||||||
|
return super.getPolymerItemModel(stack, context);
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+126
@@ -0,0 +1,126 @@
|
|||||||
|
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.registration.MinionBlocks;
|
||||||
|
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 net.minecraft.registry.RegistryKey;
|
||||||
|
import net.minecraft.server.MinecraftServer;
|
||||||
|
import net.minecraft.util.Identifier;
|
||||||
|
import net.minecraft.util.Uuids;
|
||||||
|
import net.minecraft.util.math.BlockPos;
|
||||||
|
import net.minecraft.world.World;
|
||||||
|
|
||||||
|
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(
|
||||||
|
World.CODEC.fieldOf("world").forGetter(listener -> listener.worldKey),
|
||||||
|
BlockPos.CODEC.fieldOf("pos").forGetter(listener -> listener.pos),
|
||||||
|
Uuids.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;
|
||||||
|
|
||||||
|
private MinionTriggerMinionListener(RegistryKey<World> worldKey, BlockPos pos, UUID minionUuid, String instructionName) {
|
||||||
|
super(worldKey, pos, minionUuid, MinionBlocks.MINION_TRIGGER_BE_TYPE);
|
||||||
|
this.instructionName = Objects.requireNonNull(instructionName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void addListener(World world, BlockPos pos, UUID minion, String instructionName) {
|
||||||
|
MinionTriggerMinionListener listener = new MinionTriggerMinionListener(world.getRegistryKey(), pos, minion, instructionName);
|
||||||
|
listener.add(world.getServer());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void removeListener(World world, BlockPos pos, UUID minion, String instructionName) {
|
||||||
|
MinionTriggerMinionListener old = getListener(world, pos, minion, MinionTriggerMinionListener.class);
|
||||||
|
if(old != null) {
|
||||||
|
old.remove(world.getServer());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@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
|
||||||
|
protected void add(MinecraftServer server) {
|
||||||
|
super.add(server);
|
||||||
|
runningCache = minion.getInstructionManager().getInstruction(instructionName).isRunning();
|
||||||
|
updateComparatorsIfLoaded(server);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<Identifier> getCodecId() {
|
||||||
|
return Optional.of(Identifier.of(Minions.MOD_ID, "minion_trigger"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateComparatorsIfLoaded(MinecraftServer server) {
|
||||||
|
World world = server.getWorld(worldKey);
|
||||||
|
if(world.isPosLoaded(pos)) {
|
||||||
|
world.updateComparators(pos, MinionBlocks.MINION_TRIGGER_BLOCK);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isRunning() {
|
||||||
|
return runningCache;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class TriggerInstructionListener implements ConfiguredInstructionListener {
|
||||||
|
@Override
|
||||||
|
public void onRun(ConfiguredInstruction<?> instruction) {
|
||||||
|
runningCache = true;
|
||||||
|
updateComparatorsIfLoaded(minion.getServer());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onStop(ConfiguredInstruction<?> instruction) {
|
||||||
|
runningCache = false;
|
||||||
|
updateComparatorsIfLoaded(minion.getServer());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
package io.github.skippyall.minions.client;
|
|
||||||
|
|
||||||
import io.github.skippyall.minions.networking.ClientToServerNetworking;
|
|
||||||
import net.fabricmc.api.ClientModInitializer;
|
|
||||||
import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationConnectionEvents;
|
|
||||||
import net.fabricmc.fabric.api.client.networking.v1.ClientLoginConnectionEvents;
|
|
||||||
|
|
||||||
public class MinionsClient implements ClientModInitializer {
|
|
||||||
/**
|
|
||||||
* Runs the mod initializer on the client environment.
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void onInitializeClient() {
|
|
||||||
//ClientConfigurationConnectionEvents.INIT.register(ClientToServerNetworking::onConfigurationInit);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,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.item.tooltip.TooltipAppender;
|
||||||
|
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
public interface Clipboard extends TooltipAppender {
|
||||||
|
Codec<Clipboard> CODEC = MinionRegistries.CLIPBOARD_TYPES.getCodec().dispatch(Clipboard::getCodec, Function.identity());
|
||||||
|
|
||||||
|
MapCodec<? extends Clipboard> getCodec();
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package io.github.skippyall.minions.clipboard;
|
||||||
|
|
||||||
|
import eu.pb4.polymer.core.api.item.PolymerItem;
|
||||||
|
import io.github.skippyall.minions.registration.MinionComponentTypes;
|
||||||
|
import io.github.skippyall.minions.registration.MinionItems;
|
||||||
|
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
|
||||||
|
import net.minecraft.component.DataComponentTypes;
|
||||||
|
import net.minecraft.item.Item;
|
||||||
|
import net.minecraft.item.ItemStack;
|
||||||
|
import net.minecraft.item.Items;
|
||||||
|
import net.minecraft.item.tooltip.TooltipType;
|
||||||
|
import net.minecraft.util.Identifier;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import xyz.nucleoid.packettweaker.PacketContext;
|
||||||
|
|
||||||
|
public class ClipboardItem extends Item implements PolymerItem {
|
||||||
|
public ClipboardItem(Settings settings) {
|
||||||
|
super(settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Item getPolymerItem(ItemStack itemStack, PacketContext context) {
|
||||||
|
return Items.PAPER;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @Nullable Identifier getPolymerItemModel(ItemStack stack, PacketContext context) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ItemStack getPolymerItemStack(ItemStack itemStack, TooltipType tooltipType, PacketContext context) {
|
||||||
|
ItemStack stack = PolymerItem.super.getPolymerItemStack(itemStack, tooltipType, context);
|
||||||
|
stack.set(DataComponentTypes.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().getName()));
|
||||||
|
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.component.ComponentsAccess;
|
||||||
|
import net.minecraft.item.Item;
|
||||||
|
import net.minecraft.item.tooltip.TooltipType;
|
||||||
|
import net.minecraft.text.Text;
|
||||||
|
import net.minecraft.util.Uuids;
|
||||||
|
|
||||||
|
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(
|
||||||
|
Uuids.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 appendTooltip(Item.TooltipContext context, Consumer<Text> textConsumer, TooltipType type, ComponentsAccess components) {
|
||||||
|
textConsumer.accept(Text.translatable("minions.reference.instruction.tooltip", selectedInstruction, visualMinionName));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
package io.github.skippyall.minions.command;
|
|
||||||
|
|
||||||
import net.minecraft.item.Item;
|
|
||||||
import net.minecraft.text.Text;
|
|
||||||
|
|
||||||
public interface Command extends CommandExecutor {
|
|
||||||
Text getName();
|
|
||||||
Text getDescription();
|
|
||||||
Item getItemRepresentation();
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
package io.github.skippyall.minions.command;
|
|
||||||
|
|
||||||
import io.github.skippyall.minions.fakeplayer.MinionFakePlayer;
|
|
||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
|
||||||
|
|
||||||
public interface CommandExecutor {
|
|
||||||
void execute(ServerPlayerEntity player, MinionFakePlayer minion);
|
|
||||||
}
|
|
||||||
@@ -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.server.command.ServerCommandSource;
|
||||||
|
import net.minecraft.text.Text;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import static net.minecraft.server.command.CommandManager.literal;
|
||||||
|
|
||||||
|
public class ListSubcommand {
|
||||||
|
public static final LiteralArgumentBuilder<ServerCommandSource> LIST = literal("list")
|
||||||
|
.executes(ListSubcommand::list);
|
||||||
|
|
||||||
|
public static int list(CommandContext<ServerCommandSource> context) {
|
||||||
|
Collection<MinionData> minions = MinionPersistentState.get(context.getSource().getServer()).getMinionData().values();
|
||||||
|
for (MinionData minion : minions) {
|
||||||
|
context.getSource().sendFeedback(() -> Text.literal(minion.name() + "(" + minion.uuid() + "):" + minion.isSpawned()), false);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
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.server.MinecraftServer;
|
||||||
|
import net.minecraft.server.command.ServerCommandSource;
|
||||||
|
import net.minecraft.text.Text;
|
||||||
|
|
||||||
|
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(Text.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<ServerCommandSource> {
|
||||||
|
@Override
|
||||||
|
public CompletableFuture<Suggestions> getSuggestions(CommandContext<ServerCommandSource> context, SuggestionsBuilder builder) throws CommandSyntaxException {
|
||||||
|
for (MinionData data : MinionPersistentState.get(context.getSource().getServer()).getMinionDataList()) {
|
||||||
|
builder.suggest(data.name());
|
||||||
|
}
|
||||||
|
|
||||||
|
return builder.buildFuture();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
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.command.CommandRegistryAccess;
|
||||||
|
import net.minecraft.server.command.CommandManager;
|
||||||
|
import net.minecraft.server.command.ServerCommandSource;
|
||||||
|
|
||||||
|
import static net.minecraft.server.command.CommandManager.literal;
|
||||||
|
|
||||||
|
public class MinionsCommand {
|
||||||
|
public static void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess access, CommandManager.RegistrationEnvironment environment) {
|
||||||
|
LiteralArgumentBuilder<ServerCommandSource> builder = literal("minions")
|
||||||
|
.then(SpawnSubcommand.SPAWN)
|
||||||
|
.then(ListSubcommand.LIST);
|
||||||
|
|
||||||
|
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.ServerChunkManagerAccessor;
|
||||||
|
import net.minecraft.server.command.ServerCommandSource;
|
||||||
|
import net.minecraft.server.world.ChunkLevelManager;
|
||||||
|
import net.minecraft.text.Text;
|
||||||
|
|
||||||
|
import static net.minecraft.server.command.CommandManager.literal;
|
||||||
|
|
||||||
|
public class MobCapDebugSubcommand {
|
||||||
|
public static final LiteralArgumentBuilder<ServerCommandSource> MOB_CAP_DEBUG = literal("mobcapdebug")
|
||||||
|
.executes(MobCapDebugSubcommand::mobcapdebugCommand);
|
||||||
|
|
||||||
|
public static int mobcapdebugCommand(CommandContext<ServerCommandSource> context) {
|
||||||
|
ChunkLevelManager levelManager = ((ServerChunkManagerAccessor)context.getSource().getWorld().getChunkManager()).getLevelManager();
|
||||||
|
int tickedChunkCount = ((ChunkLevelManager$DistanceFromNearestPlayerTrackerAccessor)((ChunkLevelManagerAccessor)levelManager).minions$getMinionless()).minions$getTickedChunkCount();
|
||||||
|
context.getSource().sendFeedback(() -> Text.of(String.valueOf(tickedChunkCount)), false);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
package io.github.skippyall.minions.command;
|
|
||||||
|
|
||||||
import io.github.skippyall.minions.fakeplayer.MinionFakePlayer;
|
|
||||||
import net.minecraft.item.Item;
|
|
||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
|
||||||
import net.minecraft.text.Text;
|
|
||||||
|
|
||||||
public class SimpleCommand implements Command {
|
|
||||||
private final Text name;
|
|
||||||
private final Text description;
|
|
||||||
private final Item itemRepresentation;
|
|
||||||
private final CommandExecutor executor;
|
|
||||||
|
|
||||||
public SimpleCommand(Text name, Text description, Item itemRepresentation, CommandExecutor executor) {
|
|
||||||
this.name = name;
|
|
||||||
this.description = description;
|
|
||||||
this.itemRepresentation = itemRepresentation;
|
|
||||||
this.executor = executor;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Text getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Text getDescription() {
|
|
||||||
return description;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Item getItemRepresentation() {
|
|
||||||
return itemRepresentation;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void execute(ServerPlayerEntity player, MinionFakePlayer minion) {
|
|
||||||
executor.execute(player, minion);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
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.command.argument.PosArgument;
|
||||||
|
import net.minecraft.command.argument.Vec3ArgumentType;
|
||||||
|
import net.minecraft.server.command.ServerCommandSource;
|
||||||
|
|
||||||
|
import static net.minecraft.server.command.CommandManager.argument;
|
||||||
|
import static net.minecraft.server.command.CommandManager.literal;
|
||||||
|
|
||||||
|
public class SpawnSubcommand {
|
||||||
|
public static final LiteralArgumentBuilder<ServerCommandSource> SPAWN = literal("spawn")
|
||||||
|
.requires(source -> source.hasPermissionLevel(2))
|
||||||
|
.then(argument("minion", StringArgumentType.word())
|
||||||
|
.suggests(MinionArgument.SUGGESTION_PROVIDER)
|
||||||
|
.then(argument("pos", Vec3ArgumentType.vec3())
|
||||||
|
.executes(context ->
|
||||||
|
spawnCommand(
|
||||||
|
context.getSource(),
|
||||||
|
StringArgumentType.getString(context, "minion"),
|
||||||
|
Vec3ArgumentType.getPosArgument(context, "pos"),
|
||||||
|
false
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.then(argument("force", BoolArgumentType.bool())
|
||||||
|
.executes(context ->
|
||||||
|
spawnCommand(
|
||||||
|
context.getSource(),
|
||||||
|
StringArgumentType.getString(context, "minion"),
|
||||||
|
Vec3ArgumentType.getPosArgument(context, "pos"),
|
||||||
|
BoolArgumentType.getBool(context, "force")
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.executes(context ->
|
||||||
|
spawnCommand(
|
||||||
|
context.getSource(),
|
||||||
|
StringArgumentType.getString(context, "minion"),
|
||||||
|
null,
|
||||||
|
false
|
||||||
|
))
|
||||||
|
);
|
||||||
|
|
||||||
|
public static int spawnCommand(ServerCommandSource source, String minion, PosArgument pos, boolean force) throws CommandSyntaxException {
|
||||||
|
MinionData data = MinionArgument.parse(source.getServer(), minion);
|
||||||
|
MinionFakePlayer.spawnMinion(data, source.getWorld(), pos != null ? pos.getPos(source) : null, pos != null ? pos.getRotation(source) : null, force);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package io.github.skippyall.minions.docs;
|
||||||
|
|
||||||
|
import com.mojang.serialization.Codec;
|
||||||
|
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
||||||
|
import net.minecraft.text.Text;
|
||||||
|
import net.minecraft.text.TextCodecs;
|
||||||
|
|
||||||
|
public record ReferenceEntry(Text shortDescription, Text longDescription) {
|
||||||
|
public static final Codec<ReferenceEntry> CODEC = RecordCodecBuilder.create(instance ->
|
||||||
|
instance.group(
|
||||||
|
TextCodecs.CODEC.fieldOf("shortDescription").forGetter(ReferenceEntry::shortDescription),
|
||||||
|
TextCodecs.CODEC.fieldOf("longDescription").forGetter(ReferenceEntry::longDescription)
|
||||||
|
).apply(instance, ReferenceEntry::new));
|
||||||
|
}
|
||||||
@@ -1,352 +0,0 @@
|
|||||||
package io.github.skippyall.minions.fakeplayer;
|
|
||||||
|
|
||||||
import com.mojang.authlib.GameProfile;
|
|
||||||
import io.github.skippyall.minions.Minions;
|
|
||||||
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.MinionPersistentState;
|
|
||||||
import io.github.skippyall.minions.minion.MinionProfileUtils;
|
|
||||||
import io.github.skippyall.minions.gui.ModuleInventory;
|
|
||||||
import io.github.skippyall.minions.program.runtime.MinionRuntime;
|
|
||||||
import net.minecraft.block.BlockState;
|
|
||||||
import net.minecraft.component.DataComponentTypes;
|
|
||||||
import net.minecraft.entity.Entity;
|
|
||||||
import net.minecraft.entity.EquipmentSlot;
|
|
||||||
import net.minecraft.entity.MovementType;
|
|
||||||
import net.minecraft.entity.attribute.EntityAttributes;
|
|
||||||
import net.minecraft.entity.damage.DamageSource;
|
|
||||||
import net.minecraft.entity.player.HungerManager;
|
|
||||||
import net.minecraft.entity.player.PlayerEntity;
|
|
||||||
import net.minecraft.item.ItemStack;
|
|
||||||
import net.minecraft.nbt.NbtCompound;
|
|
||||||
import net.minecraft.network.DisconnectionInfo;
|
|
||||||
import net.minecraft.network.NetworkSide;
|
|
||||||
import net.minecraft.network.packet.c2s.common.SyncedClientOptions;
|
|
||||||
import net.minecraft.network.packet.c2s.play.ClientStatusC2SPacket;
|
|
||||||
import net.minecraft.network.packet.s2c.play.EntityPositionS2CPacket;
|
|
||||||
import net.minecraft.network.packet.s2c.play.EntitySetHeadYawS2CPacket;
|
|
||||||
import net.minecraft.server.MinecraftServer;
|
|
||||||
import net.minecraft.server.ServerTask;
|
|
||||||
import net.minecraft.server.network.ConnectedClientData;
|
|
||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
|
||||||
import net.minecraft.server.world.ServerWorld;
|
|
||||||
import net.minecraft.text.Text;
|
|
||||||
import net.minecraft.text.TranslatableTextContent;
|
|
||||||
import net.minecraft.util.ActionResult;
|
|
||||||
import net.minecraft.util.Hand;
|
|
||||||
import net.minecraft.util.math.BlockPos;
|
|
||||||
import net.minecraft.util.math.Vec2f;
|
|
||||||
import net.minecraft.util.math.Vec3d;
|
|
||||||
import net.minecraft.world.GameMode;
|
|
||||||
import net.minecraft.world.TeleportTarget;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.util.UUID;
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
|
|
||||||
public class MinionFakePlayer extends ServerPlayerEntity {
|
|
||||||
public Runnable fixStartingPosition = () -> {};
|
|
||||||
|
|
||||||
private float moveForward;
|
|
||||||
private float moveSideways;
|
|
||||||
|
|
||||||
private boolean programmable;
|
|
||||||
private final ModuleInventory moduleInventory = new ModuleInventory();
|
|
||||||
private final MinionRuntime runtime = new MinionRuntime(this);
|
|
||||||
|
|
||||||
private UUID skinUuid = null;
|
|
||||||
|
|
||||||
public static void createMinion(MinionData data, ServerWorld level, ServerPlayerEntity owner, boolean canProgram, Vec3d pos, double yaw, double pitch) {
|
|
||||||
MinecraftServer server = level.getServer();
|
|
||||||
|
|
||||||
CompletableFuture<GameProfile> future;
|
|
||||||
if(data.skinUuid != null) {
|
|
||||||
future = MinionProfileUtils.getSkinOwnerProfile(server, data.skinUuid);
|
|
||||||
} else {
|
|
||||||
future = MinionProfileUtils.lookupSkinOwnerProfile(server, data.name);
|
|
||||||
}
|
|
||||||
|
|
||||||
future.thenAccept(skinProfile -> {
|
|
||||||
GameProfile profile = MinionProfileUtils.makeNewMinionProfile(null, data.name, skinProfile);
|
|
||||||
Minions.addExecuteOnNextTick(() -> {
|
|
||||||
MinionFakePlayer instance = new MinionFakePlayer(server, level, profile, SyncedClientOptions.createDefault());
|
|
||||||
if(skinProfile != null) {
|
|
||||||
instance.skinUuid = skinProfile.getId();
|
|
||||||
}
|
|
||||||
|
|
||||||
instance.programmable = canProgram;
|
|
||||||
instance.fixStartingPosition = () -> instance.refreshPositionAndAngles(pos.x, pos.y, pos.z, (float) yaw, (float) pitch);
|
|
||||||
server.getPlayerManager().onPlayerConnect(new FakeClientConnection(NetworkSide.SERVERBOUND), instance, new ConnectedClientData(profile, 0, instance.getClientOptions(), false));
|
|
||||||
instance.teleport(level, pos.x, pos.y, pos.z, (float) yaw, (float) pitch);
|
|
||||||
instance.setHealth(20.0F);
|
|
||||||
instance.unsetRemoved();
|
|
||||||
instance.getAttributeInstance(EntityAttributes.GENERIC_STEP_HEIGHT).setBaseValue(0.6F);
|
|
||||||
instance.interactionManager.changeGameMode(GameMode.SURVIVAL);
|
|
||||||
server.getPlayerManager().sendToDimension(new EntitySetHeadYawS2CPacket(instance, (byte) (instance.headYaw * 256 / 360)), level.getRegistryKey());//instance.dimension);
|
|
||||||
server.getPlayerManager().sendToDimension(new EntityPositionS2CPacket(instance), level.getRegistryKey());//instance.dimension);
|
|
||||||
//instance.world.getChunkManager(). updatePosition(instance);
|
|
||||||
instance.dataTracker.set(PLAYER_MODEL_PARTS, (byte) 0x7f); // show all model layers (incl. capes)
|
|
||||||
instance.getAbilities().flying = false;
|
|
||||||
MinionPersistentState.INSTANCE.addMinion(instance);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void spawnMinionAt(MinionData data, ServerWorld level, @Nullable Vec3d pos, @Nullable Vec2f rot) {
|
|
||||||
MinecraftServer server = level.getServer();
|
|
||||||
|
|
||||||
CompletableFuture<GameProfile> future;
|
|
||||||
if(data.skinUuid != null) {
|
|
||||||
future = MinionProfileUtils.getSkinOwnerProfile(server, data.skinUuid);
|
|
||||||
} else {
|
|
||||||
future = MinionProfileUtils.lookupSkinOwnerProfile(server, data.name);
|
|
||||||
}
|
|
||||||
|
|
||||||
future.thenAccept((skinProfile) -> {
|
|
||||||
GameProfile profile = MinionProfileUtils.makeNewMinionProfile(data.uuid, data.name, skinProfile);
|
|
||||||
Minions.addExecuteOnNextTick(() -> {
|
|
||||||
MinionFakePlayer instance = new MinionFakePlayer(server, level, profile, SyncedClientOptions.createDefault());
|
|
||||||
if (skinProfile != null) {
|
|
||||||
instance.skinUuid = skinProfile.getId();
|
|
||||||
}
|
|
||||||
if(pos != null && rot != null) {
|
|
||||||
instance.fixStartingPosition = () -> instance.refreshPositionAndAngles(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));
|
|
||||||
System.out.println(instance.getPos());
|
|
||||||
if(pos != null && rot != null) {
|
|
||||||
instance.teleport(level, pos.x, pos.y, pos.z, rot.x, rot.y);
|
|
||||||
}
|
|
||||||
instance.setVelocity(0,0,0);
|
|
||||||
instance.setHealth(20.0F);
|
|
||||||
instance.unsetRemoved();
|
|
||||||
instance.interactionManager.changeGameMode(GameMode.SURVIVAL);
|
|
||||||
server.getPlayerManager().sendToDimension(new EntitySetHeadYawS2CPacket(instance, (byte) (instance.headYaw * 256 / 360)), level.getRegistryKey());//instance.dimension);
|
|
||||||
server.getPlayerManager().sendToDimension(new EntityPositionS2CPacket(instance), level.getRegistryKey());//instance.dimension);
|
|
||||||
//instance.world.getChunkManager(). updatePosition(instance);
|
|
||||||
instance.dataTracker.set(PLAYER_MODEL_PARTS, (byte) 0x7f); // show all model layers (incl. capes)
|
|
||||||
instance.getAbilities().flying = false;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public static MinionFakePlayer respawnFake(MinecraftServer server, ServerWorld level, GameProfile profile, SyncedClientOptions cli)
|
|
||||||
{
|
|
||||||
return new MinionFakePlayer(server, level, profile, cli);
|
|
||||||
}
|
|
||||||
|
|
||||||
private MinionFakePlayer(MinecraftServer server, ServerWorld worldIn, GameProfile profile, SyncedClientOptions cli)
|
|
||||||
{
|
|
||||||
super(server, worldIn, profile, cli);
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isProgrammable() {
|
|
||||||
return programmable;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setProgrammable(boolean programmable) {
|
|
||||||
this.programmable = programmable;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ModuleInventory getModuleInventory() {
|
|
||||||
return moduleInventory;
|
|
||||||
}
|
|
||||||
|
|
||||||
public MinionRuntime getRuntime() {
|
|
||||||
return runtime;
|
|
||||||
}
|
|
||||||
|
|
||||||
public EntityPlayerActionPack getMinionActionPack() {
|
|
||||||
return ((ServerPlayerInterface)this).getActionPack();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ActionResult interact(PlayerEntity player, Hand hand) {
|
|
||||||
if(player instanceof ServerPlayerEntity spe) {
|
|
||||||
MinionGui.openInventory(spe, this);
|
|
||||||
}
|
|
||||||
return ActionResult.CONSUME;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ActionResult interactAt(PlayerEntity player, Vec3d hitPos, Hand hand) {
|
|
||||||
return interact(player, hand);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onEquipStack(final EquipmentSlot slot, final ItemStack previous, final ItemStack stack)
|
|
||||||
{
|
|
||||||
if (!isUsingItem()) super.onEquipStack(slot, previous, stack);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*@Override
|
|
||||||
public void kill()
|
|
||||||
{
|
|
||||||
kill(Text.literal("Killed"));
|
|
||||||
}*/
|
|
||||||
|
|
||||||
public void kill(Text reason)
|
|
||||||
{
|
|
||||||
shakeOff();
|
|
||||||
|
|
||||||
if (reason.getContent() instanceof TranslatableTextContent text && text.getKey().equals("multiplayer.disconnect.duplicate_login")) {
|
|
||||||
this.networkHandler.onDisconnected(new DisconnectionInfo(reason));
|
|
||||||
} else {
|
|
||||||
this.server.send(new ServerTask(this.server.getTicks(), () -> {
|
|
||||||
this.networkHandler.onDisconnected(new DisconnectionInfo(reason));
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
MinionPersistentState.INSTANCE.removeMinion(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void tick()
|
|
||||||
{
|
|
||||||
if (this.getServer().getTicks() % 10 == 0)
|
|
||||||
{
|
|
||||||
this.networkHandler.syncWithPlayerPosition();
|
|
||||||
this.getServerWorld().getChunkManager().updatePosition(this);
|
|
||||||
}
|
|
||||||
try
|
|
||||||
{
|
|
||||||
super.tick();
|
|
||||||
this.playerTick();
|
|
||||||
}
|
|
||||||
catch (NullPointerException ignored)
|
|
||||||
{
|
|
||||||
// happens with that paper port thingy - not sure what that would fix, but hey
|
|
||||||
// the game not gonna crash violently.
|
|
||||||
}
|
|
||||||
runtime.tick();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private void shakeOff()
|
|
||||||
{
|
|
||||||
if (getVehicle() instanceof PlayerEntity) stopRiding();
|
|
||||||
for (Entity passenger : getPassengersDeep())
|
|
||||||
{
|
|
||||||
if (passenger instanceof PlayerEntity) passenger.stopRiding();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDeath(DamageSource cause)
|
|
||||||
{
|
|
||||||
shakeOff();
|
|
||||||
super.onDeath(cause);
|
|
||||||
setHealth(20);
|
|
||||||
this.hungerManager = new HungerManager();
|
|
||||||
kill(this.getDamageTracker().getDeathMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getIp()
|
|
||||||
{
|
|
||||||
return "127.0.0.1";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean allowsServerListing() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void fall(double y, boolean onGround, BlockState state, BlockPos pos) {
|
|
||||||
handleFall(0.0, y, 0.0, onGround);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Entity teleportTo(TeleportTarget target)
|
|
||||||
{
|
|
||||||
super.teleportTo(target);
|
|
||||||
if (notInAnyWorld) {
|
|
||||||
ClientStatusC2SPacket p = new ClientStatusC2SPacket(ClientStatusC2SPacket.Mode.PERFORM_RESPAWN);
|
|
||||||
networkHandler.onClientStatus(p);
|
|
||||||
}
|
|
||||||
|
|
||||||
// If above branch was taken, *this* has been removed and replaced, the new instance has been set
|
|
||||||
// on 'our' connection (which is now theirs, but we still have a ref).
|
|
||||||
if (networkHandler.player.isInTeleportationState()) {
|
|
||||||
networkHandler.player.onTeleportationDone();
|
|
||||||
}
|
|
||||||
return networkHandler.player;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void moveForward(float forward) {
|
|
||||||
this.moveForward += forward;
|
|
||||||
EntityPlayerActionPack actionPack = 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void move(MovementType movementType, Vec3d movement) {
|
|
||||||
float newForward = (float) (moveForward - movement.z);
|
|
||||||
float newSideways = (float) (moveSideways - movement.x);
|
|
||||||
Vec3d newMovement = movement;
|
|
||||||
if ((newForward < 0 && moveForward > 0) || (newForward > 0 && moveForward < 0)) {
|
|
||||||
newMovement = new Vec3d(newMovement.x, newMovement.y, moveForward);
|
|
||||||
moveForward = 0;
|
|
||||||
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
|
|
||||||
protected void drop(ServerWorld world, DamageSource damageSource) {
|
|
||||||
super.drop(world, damageSource);
|
|
||||||
dropStack(toItemStack());
|
|
||||||
}
|
|
||||||
|
|
||||||
private ItemStack toItemStack() {
|
|
||||||
ItemStack stack = new ItemStack(Minions.MINION_ITEM);
|
|
||||||
MinionItem.setData(MinionData.fromMinion(this), stack);
|
|
||||||
if (!getMinionName().equals("Minion")) {
|
|
||||||
stack.set(DataComponentTypes.CUSTOM_NAME, Text.of(getMinionName()));
|
|
||||||
}
|
|
||||||
return stack;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void writeCustomDataToNbt(NbtCompound nbt) {
|
|
||||||
super.writeCustomDataToNbt(nbt);
|
|
||||||
nbt.put("modules", moduleInventory.writeNbt(new NbtCompound(), getRegistryManager()));
|
|
||||||
nbt.putBoolean("programmable", programmable);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void readCustomDataFromNbt(NbtCompound nbt) {
|
|
||||||
super.readCustomDataFromNbt(nbt);
|
|
||||||
moduleInventory.readNbt(nbt.getCompound("modules"), getRegistryManager());
|
|
||||||
programmable = nbt.getBoolean("programmable");
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getMinionName() {
|
|
||||||
return getGameProfile().getName();
|
|
||||||
}
|
|
||||||
|
|
||||||
public UUID getSkinUuid() {
|
|
||||||
return skinUuid;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
package io.github.skippyall.minions.fakeplayer;
|
|
||||||
|
|
||||||
import io.github.skippyall.minions.fakeplayer.EntityPlayerActionPack;
|
|
||||||
|
|
||||||
public interface ServerPlayerInterface
|
|
||||||
{
|
|
||||||
EntityPlayerActionPack getActionPack();
|
|
||||||
}
|
|
||||||
@@ -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.command.Command;
|
|
||||||
import io.github.skippyall.minions.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,117 @@
|
|||||||
|
package io.github.skippyall.minions.gui;
|
||||||
|
|
||||||
|
import eu.pb4.sgui.api.elements.GuiElementBuilder;
|
||||||
|
import io.github.skippyall.minions.gui.input.ChoiceInput;
|
||||||
|
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.clipboard.ClipboardItem;
|
||||||
|
import net.minecraft.item.Items;
|
||||||
|
import net.minecraft.screen.ScreenHandlerType;
|
||||||
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
|
import net.minecraft.sound.SoundCategory;
|
||||||
|
import net.minecraft.sound.SoundEvents;
|
||||||
|
import net.minecraft.text.Text;
|
||||||
|
|
||||||
|
public class ConfigureInstructionGui extends InstructionBoundSimpleGui {
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
private ConfigureInstructionGui(ScreenHandlerType<?> type, ServerPlayerEntity player, MinionFakePlayer minion, ConfiguredInstruction<MinionRuntime> configuredInstruction, String name) {
|
||||||
|
super(type, player, minion, configuredInstruction);
|
||||||
|
this.name = name;
|
||||||
|
init();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void configureInstructionMenu(String name, ConfiguredInstruction<MinionRuntime> instruction, MinionFakePlayer minion, ServerPlayerEntity player) {
|
||||||
|
if(!InstructionGui.checkInstructionExists(name, instruction, minion, player)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ConfigureInstructionGui gui = new ConfigureInstructionGui(ScreenHandlerType.GENERIC_9X3, player, minion, instruction, name);
|
||||||
|
|
||||||
|
gui.open();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void init() {
|
||||||
|
setTitle(Text.literal(name));
|
||||||
|
|
||||||
|
setSlot(7, new GuiElementBuilder(Items.ANVIL)
|
||||||
|
.setName(Text.translatable("minions.gui.instruction.configure.rename"))
|
||||||
|
.setCallback(() -> InstructionGui.inputInstructionName(minion, player, name).thenAccept(newName -> {
|
||||||
|
minion.getInstructionManager().setInstructionName(name, newName);
|
||||||
|
configureInstructionMenu(newName, instruction, minion, player);
|
||||||
|
}))
|
||||||
|
);
|
||||||
|
|
||||||
|
setSlot(8, new GuiElementBuilder(Items.LAVA_BUCKET)
|
||||||
|
.setName(Text.translatable("minions.gui.instruction.configure.delete"))
|
||||||
|
.setCallback(() -> ChoiceInput.confirm(player, Text.translatable("minions.gui.instruction.configure.delete.confirm", name))
|
||||||
|
.thenAccept(v -> {
|
||||||
|
minion.getInstructionManager().removeInstruction(name);
|
||||||
|
InstructionGui.instructionList(minion, player);
|
||||||
|
}))
|
||||||
|
);
|
||||||
|
|
||||||
|
updateSuppliers();
|
||||||
|
|
||||||
|
setSlot(13, InstructionGui.createInstructionElement(instruction.getInstruction(), player.getRegistryManager()));
|
||||||
|
|
||||||
|
setSlot(25, new GuiElementBuilder(Items.FEATHER)
|
||||||
|
.setName(Text.translatable("minions.gui.instruction.configure.copy"))
|
||||||
|
.addLoreLine(Text.translatable("minions.gui.instruction.configure.copy.description"))
|
||||||
|
.setCallback(() -> {
|
||||||
|
player.getInventory().offer(ClipboardItem.createInstructionReference(minion, name), true);
|
||||||
|
player.playSoundToPlayer(SoundEvents.BLOCK_NOTE_BLOCK_CHIME.value(), SoundCategory.BLOCKS, 1, 1);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
updateRunSlot();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onInstructionRename(MinionFakePlayer minion, ConfiguredInstruction<?> instruction, String oldName, String newName) {
|
||||||
|
this.setTitle(Text.literal(newName));
|
||||||
|
name = 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()) {
|
||||||
|
setSlot(26, new GuiElementBuilder(Items.ARROW)
|
||||||
|
.setName(Text.translatable("minions.gui.instruction.run"))
|
||||||
|
.setCallback(() -> instruction.run(minion.getInstructionManager()))
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
setSlot(26, new GuiElementBuilder(Items.BARRIER)
|
||||||
|
.setName(Text.translatable("minions.gui.instruction.stop"))
|
||||||
|
.setCallback(() -> instruction.stop(minion.getInstructionManager()))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateSuppliers() {
|
||||||
|
int slot = 12;
|
||||||
|
for(Parameter<?> parameter : instruction.getInstruction().getParameters().reversed()) {
|
||||||
|
setSlot(slot, InstructionGui.createParameterElement(parameter, instruction.getArguments().getArgument(parameter), player.getRegistryManager())
|
||||||
|
.setCallback(() -> InstructionGui.configureArgumentMenu(name, instruction, parameter, minion, player))
|
||||||
|
);
|
||||||
|
slot--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package io.github.skippyall.minions.gui;
|
||||||
|
|
||||||
|
public interface Displayable {
|
||||||
|
GuiDisplay getDisplay();
|
||||||
|
}
|
||||||
@@ -0,0 +1,143 @@
|
|||||||
|
package io.github.skippyall.minions.gui;
|
||||||
|
|
||||||
|
import com.mojang.authlib.properties.PropertyMap;
|
||||||
|
import com.mojang.serialization.Codec;
|
||||||
|
import io.github.skippyall.minions.registration.MinionRegistries;
|
||||||
|
import io.github.skippyall.minions.util.TranslationUtil;
|
||||||
|
import it.unimi.dsi.fastutil.objects.ReferenceSortedSets;
|
||||||
|
import net.minecraft.component.DataComponentTypes;
|
||||||
|
import net.minecraft.component.type.ProfileComponent;
|
||||||
|
import net.minecraft.component.type.TooltipDisplayComponent;
|
||||||
|
import net.minecraft.item.Item;
|
||||||
|
import net.minecraft.item.ItemStack;
|
||||||
|
import net.minecraft.item.Items;
|
||||||
|
import net.minecraft.registry.DynamicRegistryManager;
|
||||||
|
import net.minecraft.registry.Registries;
|
||||||
|
import net.minecraft.registry.Registry;
|
||||||
|
import net.minecraft.text.Text;
|
||||||
|
import net.minecraft.util.Identifier;
|
||||||
|
import net.minecraft.util.Rarity;
|
||||||
|
import net.minecraft.util.Uuids;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public interface GuiDisplay {
|
||||||
|
Codec<GuiDisplay> CODEC = MinionRegistries.GUI_DISPLAY_TYPE.getCodec().dispatch(GuiDisplay::getCodec, codec -> codec.fieldOf("data"));
|
||||||
|
GuiDisplay DEFAULT_DISPLAY = new ItemBased(Items.BARRIER);
|
||||||
|
|
||||||
|
static GuiDisplay getGuiDisplay(Identifier id, DynamicRegistryManager manager) {
|
||||||
|
return manager.getOptional(MinionRegistries.GUI_DISPLAY).map(registry -> registry.get(id)).orElse(DEFAULT_DISPLAY);
|
||||||
|
}
|
||||||
|
|
||||||
|
static <T> GuiDisplay getGuiDisplayFor(Registry<T> registry, T element, DynamicRegistryManager manager) {
|
||||||
|
Identifier elementId = registry.getId(element);
|
||||||
|
if(elementId == null) {
|
||||||
|
return DEFAULT_DISPLAY;
|
||||||
|
}
|
||||||
|
Identifier displayId = elementId.withPrefixedPath(registry.getKey().getValue().getPath() + "/");
|
||||||
|
|
||||||
|
return getGuiDisplay(displayId, manager);
|
||||||
|
}
|
||||||
|
|
||||||
|
static <T> ItemStack getDisplayStack(Registry<T> registry, T element, DynamicRegistryManager manager) {
|
||||||
|
return getGuiDisplayFor(registry, element, manager).createItemStack();
|
||||||
|
}
|
||||||
|
|
||||||
|
static <T> ItemStack getDisplayStackWithName(Registry<T> registry, T element, DynamicRegistryManager manager) {
|
||||||
|
ItemStack stack = getDisplayStack(registry, element, manager);
|
||||||
|
stack.set(DataComponentTypes.ITEM_NAME, Text.translatable(TranslationUtil.getTranslationKey(element, registry)));
|
||||||
|
return stack;
|
||||||
|
}
|
||||||
|
|
||||||
|
ItemStack createItemStack();
|
||||||
|
|
||||||
|
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 ItemStack createItemStack() {
|
||||||
|
ItemStack stack = new ItemStack(Items.BARRIER);
|
||||||
|
stack.set(DataComponentTypes.ITEM_MODEL, model);
|
||||||
|
return stack;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Codec<? extends GuiDisplay> getCodec() {
|
||||||
|
return CODEC;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ItemBased implements GuiDisplay {
|
||||||
|
public static final Codec<ItemBased> CODEC = Registries.ITEM.getCodec().xmap(ItemBased::new, display -> display.item);
|
||||||
|
|
||||||
|
private final Item item;
|
||||||
|
|
||||||
|
public ItemBased(Item item) {
|
||||||
|
this.item = item;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ItemStack createItemStack() {
|
||||||
|
ItemStack stack = new ItemStack(item);
|
||||||
|
stack.set(DataComponentTypes.TOOLTIP_DISPLAY, new TooltipDisplayComponent(true, ReferenceSortedSets.emptySet()));
|
||||||
|
stack.set(DataComponentTypes.RARITY, Rarity.COMMON);
|
||||||
|
return stack;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Codec<? extends GuiDisplay> getCodec() {
|
||||||
|
return CODEC;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class HeadBased implements GuiDisplay {
|
||||||
|
public static final Codec<HeadBased> CODEC = Uuids.CODEC.xmap(HeadBased::new, display -> display.uuid);
|
||||||
|
|
||||||
|
private final UUID uuid;
|
||||||
|
|
||||||
|
public HeadBased(UUID uuid) {
|
||||||
|
this.uuid = uuid;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ItemStack createItemStack() {
|
||||||
|
ItemStack stack = new ItemStack(Items.PLAYER_HEAD);
|
||||||
|
stack.set(DataComponentTypes.PROFILE, new ProfileComponent(Optional.empty(), Optional.of(uuid), new PropertyMap()));
|
||||||
|
return stack;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Codec<? extends GuiDisplay> getCodec() {
|
||||||
|
return CODEC;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class StackBased implements GuiDisplay {
|
||||||
|
public static final Codec<StackBased> CODEC = ItemStack.CODEC.xmap(StackBased::new, StackBased::createItemStack);
|
||||||
|
|
||||||
|
private final ItemStack stack;
|
||||||
|
|
||||||
|
public StackBased(ItemStack stack) {
|
||||||
|
this.stack = stack;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ItemStack createItemStack() {
|
||||||
|
return stack;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Codec<? extends GuiDisplay> getCodec() {
|
||||||
|
return CODEC;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package io.github.skippyall.minions.gui;
|
||||||
|
|
||||||
|
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 net.minecraft.screen.ScreenHandlerType;
|
||||||
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
|
|
||||||
|
public class InstructionBoundSimpleGui extends MinionBoundSimpleGui implements ConfiguredInstructionListener {
|
||||||
|
protected final ConfiguredInstruction<MinionRuntime> instruction;
|
||||||
|
|
||||||
|
public InstructionBoundSimpleGui(ScreenHandlerType<?> type, ServerPlayerEntity player, MinionFakePlayer minion, ConfiguredInstruction<MinionRuntime> instruction) {
|
||||||
|
super(type, player, minion);
|
||||||
|
this.instruction = instruction;
|
||||||
|
instruction.addListener(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onInstructionRemove(ConfiguredInstruction<?> instruction) {
|
||||||
|
close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onClose() {
|
||||||
|
super.onClose();
|
||||||
|
instruction.removeListener(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,235 @@
|
|||||||
|
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.program.value.ValueType;
|
||||||
|
import io.github.skippyall.minions.registration.MinionComponentTypes;
|
||||||
|
import io.github.skippyall.minions.registration.MinionRegistries;
|
||||||
|
import io.github.skippyall.minions.gui.input.Result;
|
||||||
|
import io.github.skippyall.minions.gui.input.TextInput;
|
||||||
|
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.supplier.ValueSupplier;
|
||||||
|
import io.github.skippyall.minions.program.supplier.ValueSupplierType;
|
||||||
|
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.util.TranslationUtil;
|
||||||
|
import net.minecraft.item.ItemStack;
|
||||||
|
import net.minecraft.item.Items;
|
||||||
|
import net.minecraft.registry.DynamicRegistryManager;
|
||||||
|
import net.minecraft.screen.ScreenHandlerType;
|
||||||
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
|
import net.minecraft.text.Text;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
|
public class InstructionGui {
|
||||||
|
public static void openInstructionMainMenu(MinionFakePlayer minion, ServerPlayerEntity player) {
|
||||||
|
SimpleGui gui = new MinionBoundSimpleGui(ScreenHandlerType.GENERIC_3X3, player, minion);
|
||||||
|
gui.setTitle(Text.translatable("minions.gui.instruction.title"));
|
||||||
|
|
||||||
|
gui.setSlot(3, new GuiElementBuilder()
|
||||||
|
.setItem(Items.BOOK)
|
||||||
|
.setName(Text.translatable("minions.gui.instruction.list"))
|
||||||
|
.setCallback(() -> instructionList(minion, player))
|
||||||
|
);
|
||||||
|
gui.setSlot(5, new GuiElementBuilder()
|
||||||
|
.setItem(Items.WRITABLE_BOOK)
|
||||||
|
.setName(Text.translatable("minions.gui.instruction.create"))
|
||||||
|
.setCallback(() -> createNewInstruction(minion, player))
|
||||||
|
);
|
||||||
|
|
||||||
|
gui.open();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void instructionList(MinionFakePlayer minion, ServerPlayerEntity player) {
|
||||||
|
SimpleGui gui = new MinionBoundSimpleGui(ScreenHandlerType.GENERIC_9X3, player, minion) {
|
||||||
|
@Override
|
||||||
|
public void onInstructionsUpdate(MinionFakePlayer minion) {
|
||||||
|
resetInstructionList(this, minion, player);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
gui.setTitle(Text.translatable("minions.gui.instruction.title"));
|
||||||
|
resetInstructionList(gui, minion, player);
|
||||||
|
|
||||||
|
gui.open();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void resetInstructionList(SimpleGui gui, MinionFakePlayer minion, ServerPlayerEntity player) {
|
||||||
|
int i = 0;
|
||||||
|
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(), player.getRegistryManager()).createItemStack())
|
||||||
|
.setName(Text.literal(instructionName))
|
||||||
|
.setCallback(() -> ConfigureInstructionGui.configureInstructionMenu(instructionName, instruction, minion, player))
|
||||||
|
);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void createNewInstruction(MinionFakePlayer minion, ServerPlayerEntity player) {
|
||||||
|
selectInstructionModuleMenu(minion, player).thenAccept(instructionType ->
|
||||||
|
inputInstructionName(minion, player, "Instruction").thenAccept(name -> {
|
||||||
|
if (!minion.isRemoved() && !minion.isDisconnected()) {
|
||||||
|
ConfiguredInstruction<MinionRuntime> configuredInstruction = minion.getInstructionManager().createInstruction(name, instructionType);
|
||||||
|
ConfigureInstructionGui.configureInstructionMenu(name, configuredInstruction, minion, player);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static CompletableFuture<String> inputInstructionName(MinionFakePlayer minion, ServerPlayerEntity player, String defaultValue) {
|
||||||
|
return TextInput.inputSync(player, Text.translatable("minions.gui.instruction.enter_name"), defaultValue, name -> {
|
||||||
|
if (minion.getInstructionManager().hasInstruction(name)) {
|
||||||
|
return new Result.Error<>(Text.translatable("minions.gui.instruction.name_already_used"));
|
||||||
|
}
|
||||||
|
return new Result.Success<>(name);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean checkInstructionExists(String name, ConfiguredInstruction<?> instruction, MinionFakePlayer minion, ServerPlayerEntity player) {
|
||||||
|
boolean stillExists = !minion.isRemoved() && !minion.isDisconnected() && minion.getInstructionManager().getInstruction(name) == instruction;
|
||||||
|
if (!stillExists) {
|
||||||
|
player.closeHandledScreen();
|
||||||
|
player.sendMessage(Text.translatable("minions.gui.instruction.removed"));
|
||||||
|
}
|
||||||
|
return stillExists;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static <T, A extends ValueSupplier<T, MinionRuntime>> void configureArgumentMenu(String instructionName, ConfiguredInstruction<MinionRuntime> instruction, Parameter<T> parameter, MinionFakePlayer minion, ServerPlayerEntity player) {
|
||||||
|
if (!checkInstructionExists(instructionName, instruction, minion, player)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable A argument = instruction.getArguments().getArgument(parameter);
|
||||||
|
|
||||||
|
if(argument == null) {
|
||||||
|
configureTypeAndValue(instructionName, instruction, parameter, minion, player);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
SimpleGui gui = new InstructionBoundSimpleGui(ScreenHandlerType.GENERIC_3X3, player, minion, instruction);
|
||||||
|
|
||||||
|
ItemStack displayStack = GuiDisplay.getDisplayStack(MinionRegistries.VALUE_SUPPLIER_TYPES, argument.getType(), player.getRegistryManager());
|
||||||
|
|
||||||
|
gui.setSlot(3, new GuiElementBuilder(displayStack)
|
||||||
|
.setName(Text.translatable("minions.gui.instruction.argument.configure.type", Text.translatable(TranslationUtil.getTranslationKey(argument.getType(), MinionRegistries.VALUE_SUPPLIER_TYPES, "minions.gui.instruction.argument.configure.type.unset"))))
|
||||||
|
.setCallback(() -> configureTypeAndValue(instructionName, instruction, parameter, minion, player))
|
||||||
|
);
|
||||||
|
gui.setSlot(5, new GuiElementBuilder(Items.STRUCTURE_VOID)
|
||||||
|
.setName(Text.literal("Configure"))
|
||||||
|
.setCallback(() -> argument.getType().openConfiguration(player, argument.getValueType(), argument)
|
||||||
|
.thenAccept(newArgument -> instruction.getArguments().setArgument(parameter, newArgument))
|
||||||
|
)
|
||||||
|
);
|
||||||
|
gui.open();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static CompletableFuture<ValueSupplierType<MinionRuntime>> selectArgumentType(ServerPlayerEntity player, MinionFakePlayer minion, ValueType<?> valueType, ConfiguredInstruction<MinionRuntime> instruction) {
|
||||||
|
CompletableFuture<ValueSupplierType<MinionRuntime>> future = new CompletableFuture<>();
|
||||||
|
SimpleGui gui = new InstructionBoundSimpleGui(ScreenHandlerType.GENERIC_9X3, player, minion, instruction);
|
||||||
|
for (ValueSupplierType<MinionRuntime> type : MinionRegistries.VALUE_SUPPLIER_TYPES) {
|
||||||
|
if(type.isConfigurable(player, valueType, minion)) {
|
||||||
|
gui.addSlot(new GuiElementBuilder(GuiDisplay.getDisplayStackWithName(MinionRegistries.VALUE_SUPPLIER_TYPES, type, player.getRegistryManager()))
|
||||||
|
.setCallback(() -> future.complete(type))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
gui.open();
|
||||||
|
return future;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> void configureTypeAndValue(String name, ConfiguredInstruction<MinionRuntime> instruction, Parameter<T> parameter, MinionFakePlayer minion, ServerPlayerEntity player) {
|
||||||
|
selectArgumentType(player, minion, parameter.type(), instruction)
|
||||||
|
.thenApply(type -> type.openConfiguration(player, parameter.type(), null)
|
||||||
|
.thenAccept(newArgument -> {
|
||||||
|
instruction.getArguments().setArgument(parameter, newArgument);
|
||||||
|
configureArgumentMenu(name, instruction, parameter, minion, player);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static CompletableFuture<InstructionType<MinionRuntime>> selectInstructionModuleMenu(MinionFakePlayer minion, ServerPlayerEntity player) {
|
||||||
|
if (minion.getModuleInventory().getModules().isEmpty()) {
|
||||||
|
player.sendMessage(Text.translatable("minions.gui.instruction.no_modules"));
|
||||||
|
return CompletableFuture.failedFuture(new NoSuchElementException("No modules"));
|
||||||
|
}
|
||||||
|
|
||||||
|
CompletableFuture<InstructionType<MinionRuntime>> future = new CompletableFuture<>();
|
||||||
|
|
||||||
|
SimpleGui gui = new MinionBoundSimpleGui(ScreenHandlerType.GENERIC_9X3, player, minion) {
|
||||||
|
@Override
|
||||||
|
public void onClose() {
|
||||||
|
if (!future.isDone()) {
|
||||||
|
future.cancel(false);
|
||||||
|
}
|
||||||
|
super.onClose();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
gui.setTitle(Text.translatable("minions.gui.instruction.select_instruction"));
|
||||||
|
|
||||||
|
for (int i = 0; i < minion.getModuleInventory().size(); i++) {
|
||||||
|
ItemStack moduleItem = minion.getModuleInventory().getStack(i);
|
||||||
|
MinionModule module = moduleItem.get(MinionComponentTypes.MODULE);
|
||||||
|
if (module != null && !module.instructions().isEmpty()) {
|
||||||
|
gui.addSlot(new GuiElementBuilder(moduleItem)
|
||||||
|
.setCallback(() -> selectInstructionMenu(module, minion, player)
|
||||||
|
.thenApply(future::complete)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
gui.open();
|
||||||
|
return future;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static CompletableFuture<InstructionType<MinionRuntime>> selectInstructionMenu(MinionModule module, MinionFakePlayer minion, ServerPlayerEntity player) {
|
||||||
|
CompletableFuture<InstructionType<MinionRuntime>> future = new CompletableFuture<>();
|
||||||
|
|
||||||
|
SimpleGui gui = new MinionBoundSimpleGui(ScreenHandlerType.GENERIC_9X3, player, minion) {
|
||||||
|
@Override
|
||||||
|
public void onClose() {
|
||||||
|
if (!future.isDone()) {
|
||||||
|
future.cancel(false);
|
||||||
|
}
|
||||||
|
super.onClose();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
gui.setTitle(Text.translatable("minions.gui.instruction.select_instruction"));
|
||||||
|
|
||||||
|
for (InstructionType<MinionRuntime> instructionType : module.instructions()) {
|
||||||
|
gui.addSlot(createInstructionElement(instructionType, player.getRegistryManager())
|
||||||
|
.setCallback(() -> future.complete(instructionType))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
gui.open();
|
||||||
|
return future;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static GuiElementBuilder createInstructionElement(InstructionType<MinionRuntime> instructionType, DynamicRegistryManager manager) {
|
||||||
|
GuiElementBuilder instructionBuilder;
|
||||||
|
if (instructionType != null) {
|
||||||
|
instructionBuilder = new GuiElementBuilder(GuiDisplay.getDisplayStackWithName(MinionRegistries.INSTRUCTION_TYPES, instructionType, manager));
|
||||||
|
} else {
|
||||||
|
instructionBuilder = new GuiElementBuilder(Items.RED_WOOL)
|
||||||
|
.setName(Text.translatable("minions.gui.instruction.no_instruction_set"));
|
||||||
|
}
|
||||||
|
return instructionBuilder;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static GuiElementBuilder createParameterElement(Parameter<?> parameter, @Nullable ValueSupplier<?,?> valueSupplier, DynamicRegistryManager manager) {
|
||||||
|
GuiElementBuilder builder = new GuiElementBuilder(GuiDisplay.getDisplayStack(MinionRegistries.VALUE_TYPES, parameter.type(), manager))
|
||||||
|
.setName(Text.translatable("minions.gui.instruction.parameter", parameter.name(), Text.translatable(TranslationUtil.getTranslationKey(parameter.type(), MinionRegistries.VALUE_TYPES))));
|
||||||
|
if(valueSupplier != null) {
|
||||||
|
builder.addLoreLine(Text.translatable("minions.gui.instruction.argument", valueSupplier.getDisplayText()));
|
||||||
|
}
|
||||||
|
return builder;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package io.github.skippyall.minions.gui;
|
||||||
|
|
||||||
|
import eu.pb4.sgui.api.gui.SimpleGui;
|
||||||
|
import io.github.skippyall.minions.minion.MinionListener;
|
||||||
|
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
|
||||||
|
import net.minecraft.screen.ScreenHandlerType;
|
||||||
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
|
|
||||||
|
public class MinionBoundSimpleGui extends SimpleGui implements MinionListener {
|
||||||
|
protected final MinionFakePlayer minion;
|
||||||
|
|
||||||
|
public MinionBoundSimpleGui(ScreenHandlerType<?> type, ServerPlayerEntity player, MinionFakePlayer minion) {
|
||||||
|
super(type, player, false);
|
||||||
|
this.minion = minion;
|
||||||
|
minion.addMinionListener(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public MinionFakePlayer getMinion() {
|
||||||
|
return minion;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onMinionRemove(MinionFakePlayer minion) {
|
||||||
|
close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onClose() {
|
||||||
|
minion.removeMinionListener(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (!(o instanceof MinionBoundSimpleGui that)) return false;
|
||||||
|
return minion == that.minion && player == that.player;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,11 +2,14 @@ package io.github.skippyall.minions.gui;
|
|||||||
|
|
||||||
import eu.pb4.sgui.api.elements.GuiElementBuilder;
|
import eu.pb4.sgui.api.elements.GuiElementBuilder;
|
||||||
import eu.pb4.sgui.api.gui.SimpleGui;
|
import eu.pb4.sgui.api.gui.SimpleGui;
|
||||||
import io.github.skippyall.minions.fakeplayer.MinionFakePlayer;
|
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
|
||||||
|
import io.github.skippyall.minions.module.ModuleInventory;
|
||||||
|
import net.minecraft.entity.EquipmentSlot;
|
||||||
|
import net.minecraft.entity.player.PlayerInventory;
|
||||||
|
import net.minecraft.item.ItemStack;
|
||||||
import net.minecraft.item.Items;
|
import net.minecraft.item.Items;
|
||||||
import net.minecraft.screen.GenericContainerScreenHandler;
|
|
||||||
import net.minecraft.screen.ScreenHandlerType;
|
import net.minecraft.screen.ScreenHandlerType;
|
||||||
import net.minecraft.screen.SimpleNamedScreenHandlerFactory;
|
import net.minecraft.screen.slot.ArmorSlot;
|
||||||
import net.minecraft.screen.slot.Slot;
|
import net.minecraft.screen.slot.Slot;
|
||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
import net.minecraft.text.Text;
|
import net.minecraft.text.Text;
|
||||||
@@ -22,50 +25,61 @@ public class MinionGui {
|
|||||||
|
|
||||||
gui.setSlot(1, new GuiElementBuilder()
|
gui.setSlot(1, new GuiElementBuilder()
|
||||||
.setItem(Items.COMMAND_BLOCK)
|
.setItem(Items.COMMAND_BLOCK)
|
||||||
.setName(Text.translatable("minions.gui.main.commands"))
|
.setName(Text.translatable("minions.gui.main.instructions"))
|
||||||
.setCallback((i, clickType, slotActionType) -> {
|
.setCallback((i, clickType, slotActionType) -> {
|
||||||
openCommandsGui(player, minion);
|
InstructionGui.openInstructionMainMenu(minion, player);
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
if(minion.isProgrammable()) {
|
|
||||||
gui.setSlot(4, new GuiElementBuilder()
|
|
||||||
.setItem(Items.REDSTONE)
|
|
||||||
.setName(Text.translatable("minions.gui.main.programming"))
|
|
||||||
.setCallback((i, clickType, slotActionType) -> {
|
|
||||||
openProgrammingInventory(player, minion);
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
gui.setSlot(3, new GuiElementBuilder()
|
gui.setSlot(3, new GuiElementBuilder()
|
||||||
.setItem(Items.NETHERITE_UPGRADE_SMITHING_TEMPLATE)
|
.setItem(Items.NETHERITE_UPGRADE_SMITHING_TEMPLATE)
|
||||||
.setName(Text.translatable("minions.gui.main.modules"))
|
.setName(Text.translatable("minions.gui.main.modules"))
|
||||||
.setCallback((i, clickType, slotActionType) -> {
|
.setCallback(() -> {
|
||||||
ModuleInventory.openModuleInventory(player, minion);
|
ModuleInventory.openModuleInventory(player, minion);
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
gui.setSlot(5, new GuiElementBuilder()
|
gui.setSlot(5, new GuiElementBuilder()
|
||||||
.setItem(Items.CHEST)
|
.setItem(Items.CHEST)
|
||||||
.setName(Text.translatable("minions.gui.main.inventory"))
|
.setName(Text.translatable("minions.gui.main.inventory"))
|
||||||
.setCallback((i, clickType, slotActionType) -> {
|
.setCallback(() -> {
|
||||||
openMinionInventory(player, minion);
|
openMinionInventory(player, minion);
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
gui.setSlot(7, new GuiElementBuilder()
|
||||||
|
.setItem(Items.BARRIER)
|
||||||
|
.setName(Text.translatable("minions.gui.main.pickup"))
|
||||||
|
.setCallback(() -> {
|
||||||
|
minion.kill(minion.getWorld());
|
||||||
|
})
|
||||||
|
);
|
||||||
gui.open();
|
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) {
|
public static void openMinionInventory(ServerPlayerEntity player, MinionFakePlayer minion) {
|
||||||
SimpleGui gui = new SimpleGui(ScreenHandlerType.GENERIC_9X5, player, false);
|
SimpleGui gui = new SimpleGui(ScreenHandlerType.GENERIC_9X6, player, false);
|
||||||
gui.setTitle(Text.translatable("minions.gui.inventory.title", minion.getName()));
|
gui.setTitle(Text.translatable("minions.gui.inventory.title"));
|
||||||
for (int i = 0; i < minion.getInventory().size(); i++) {
|
|
||||||
gui.setSlotRedirect(i, new Slot(minion.getInventory(), i, 0, 0));
|
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.setSlotRedirect(2 + 9, new ArmorSlot(minion.getInventory(), minion, EquipmentSlot.HEAD, EquipmentSlot.HEAD.getOffsetEntitySlotId(PlayerInventory.MAIN_SIZE), 0, 0, null));
|
||||||
|
gui.setSlotRedirect(3 + 9, new ArmorSlot(minion.getInventory(), minion, EquipmentSlot.CHEST, EquipmentSlot.CHEST.getOffsetEntitySlotId(PlayerInventory.MAIN_SIZE), 0, 0, null));
|
||||||
|
gui.setSlotRedirect(4 + 9, new ArmorSlot(minion.getInventory(), minion, EquipmentSlot.LEGS, EquipmentSlot.LEGS.getOffsetEntitySlotId(PlayerInventory.MAIN_SIZE), 0, 0, null));
|
||||||
|
gui.setSlotRedirect(5 + 9, new ArmorSlot(minion.getInventory(), minion, EquipmentSlot.FEET, EquipmentSlot.FEET.getOffsetEntitySlotId(PlayerInventory.MAIN_SIZE), 0, 0, null));
|
||||||
|
gui.setSlotRedirect(6 + 9, new Slot(minion.getInventory(), PlayerInventory.OFF_HAND_SLOT, 0, 0));
|
||||||
|
|
||||||
|
for (int i = PlayerInventory.HOTBAR_SIZE; i < PlayerInventory.MAIN_SIZE; i++) {
|
||||||
|
gui.setSlotRedirect(i + 9, new Slot(minion.getInventory(), i, 0, 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < PlayerInventory.HOTBAR_SIZE; i++) {
|
||||||
|
gui.setSlotRedirect(i + 45, new Slot(minion.getInventory(), i, 0, 0));
|
||||||
}
|
}
|
||||||
gui.open();
|
gui.open();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,96 @@
|
|||||||
|
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.registration.MinionRegistries;
|
||||||
|
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.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(player.getServer(), getData().withSkin(skin), minionItem);
|
||||||
|
}));
|
||||||
|
if(MinionItem.getData(player.getServer(), minionItem) != null && MinionItem.getData(player.getServer(), minionItem).skin().isPresent()) {
|
||||||
|
builder.setComponent(DataComponentTypes.PROFILE, new ProfileComponent(Optional.empty(), Optional.empty(), getData().skin().get()));
|
||||||
|
}
|
||||||
|
setSlot(16, builder);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void cycleSkinProvider() {
|
||||||
|
int currentId = MinionRegistries.SKIN_PROVIDERS.getRawId(currentSkinProvider);
|
||||||
|
currentId++;
|
||||||
|
if(MinionRegistries.SKIN_PROVIDERS.size() == currentId) {
|
||||||
|
currentId = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
currentSkinProvider = MinionRegistries.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(player.getServer(), 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", name -> MinionProfileUtils.checkMinionNameWithoutPrefix(player.getServer(), name))
|
||||||
|
.thenAccept(name -> {
|
||||||
|
MinionItem.setData(player.getServer(), getData().withName(MinionProfileUtils.getPrefix() + name), minionItem);
|
||||||
|
open();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,84 +0,0 @@
|
|||||||
package io.github.skippyall.minions.gui;
|
|
||||||
|
|
||||||
import io.github.skippyall.minions.command.Command;
|
|
||||||
import io.github.skippyall.minions.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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
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 net.minecraft.item.Items;
|
||||||
|
import net.minecraft.screen.ScreenHandlerType;
|
||||||
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
|
import net.minecraft.text.Text;
|
||||||
|
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<ServerPlayerEntity, T, CompletableFuture<T>> createDialogOpener(ScreenHandlerType<?> screen, Text 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 onClose() {
|
||||||
|
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<ServerPlayerEntity, T, CompletableFuture<T>> createDialogOpener(T[] values) {
|
||||||
|
return createDialogOpener(ScreenHandlerType.GENERIC_9X3, Text.empty(), t -> t != null ? t.getDisplay() : null, values, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static CompletableFuture<Void> confirm(ServerPlayerEntity player, Text title) {
|
||||||
|
CompletableFuture<Void> future = new CompletableFuture<>();
|
||||||
|
|
||||||
|
SimpleGui gui = new SimpleGui(ScreenHandlerType.GENERIC_3X3, player, false) {
|
||||||
|
@Override
|
||||||
|
public void onClose() {
|
||||||
|
future.cancel(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
gui.setTitle(title);
|
||||||
|
|
||||||
|
gui.setSlot(3, new GuiElementBuilder(Items.REDSTONE_BLOCK)
|
||||||
|
.setName(Text.translatable("minions.gui.abort"))
|
||||||
|
.setCallback(() -> future.cancel(false))
|
||||||
|
);
|
||||||
|
|
||||||
|
gui.setSlot(5, new GuiElementBuilder(Items.EMERALD_BLOCK)
|
||||||
|
.setName(Text.translatable("minions.gui.confirm"))
|
||||||
|
.setCallback(() -> future.complete(null))
|
||||||
|
);
|
||||||
|
|
||||||
|
gui.open();
|
||||||
|
return future;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static BiFunction<ServerPlayerEntity, Boolean, CompletableFuture<Boolean>> inputBoolean(Text title) {
|
||||||
|
return createDialogOpener(ScreenHandlerType.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,117 @@
|
|||||||
|
package io.github.skippyall.minions.gui.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);
|
||||||
|
}
|
||||||
|
|
||||||
|
@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(@NotNull Consumer<Error<T, E>> handler) {
|
||||||
|
handler.accept(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
interface UnsafeOperation<T> {
|
||||||
|
T run() throws Exception;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
package io.github.skippyall.minions.gui.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;
|
||||||
|
|
||||||
|
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;
|
||||||
|
private boolean isConfirm;
|
||||||
|
|
||||||
|
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<Long> inputLong(ServerPlayerEntity player, Text title, String defaultValue) {
|
||||||
|
return inputSync(player, title, defaultValue, string -> Result.wrapCustomError(() -> Long.valueOf(string), Text.translatable("minions.command.input.int.fail")));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static CompletableFuture<Double> inputDouble(ServerPlayerEntity player, Text title, String defaultValue) {
|
||||||
|
return inputSync(player, title, defaultValue, string -> Result.wrapCustomError(() -> Double.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() && !isConfirm) {
|
||||||
|
future.cancel(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onConfirm() {
|
||||||
|
if(result != null) {
|
||||||
|
result.ifSuccess(success -> {
|
||||||
|
isConfirm = true;
|
||||||
|
close();
|
||||||
|
future.complete(success);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,27 +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;
|
|
||||||
|
|
||||||
public class TextInput {
|
|
||||||
public static CompletableFuture<String> inputText(ServerPlayerEntity player, Text title, String defaultText) {
|
|
||||||
CompletableFuture<String> future = new CompletableFuture<>();
|
|
||||||
|
|
||||||
AnvilInputGui gui = new AnvilInputGui(player, false);
|
|
||||||
gui.setSlot(AnvilScreenHandler.OUTPUT_ID, new GuiElementBuilder()
|
|
||||||
.setItem(Items.EMERALD_BLOCK)
|
|
||||||
.setName(Text.literal("OK"))
|
|
||||||
.setCallback(() -> future.complete(gui.getInput()))
|
|
||||||
);
|
|
||||||
gui.setTitle(title);
|
|
||||||
gui.setDefaultInputValue(defaultText);
|
|
||||||
gui.open();
|
|
||||||
return future;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package io.github.skippyall.minions.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.ValueSupplierList;
|
||||||
|
import net.minecraft.storage.ReadView;
|
||||||
|
import net.minecraft.storage.WriteView;
|
||||||
|
|
||||||
|
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(ValueSupplierList<MinionRuntime> parameters, MinionRuntime minion) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void save(WriteView view, MinionRuntime minion) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void load(ReadView view, MinionRuntime runtime) {
|
||||||
|
runtime.getMinion().getMinionActionPack().start(action, EntityPlayerActionPack.Action.continuous());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,142 @@
|
|||||||
|
//partially code from https://github.com/gnembon/fabric-carpet (EntityPlayerActionPack)
|
||||||
|
package io.github.skippyall.minions.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.ValueSupplierList;
|
||||||
|
import net.minecraft.block.BlockState;
|
||||||
|
import net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket;
|
||||||
|
import net.minecraft.storage.ReadView;
|
||||||
|
import net.minecraft.storage.WriteView;
|
||||||
|
import net.minecraft.util.Hand;
|
||||||
|
import net.minecraft.util.hit.BlockHitResult;
|
||||||
|
import net.minecraft.util.hit.HitResult;
|
||||||
|
import net.minecraft.util.math.BlockPos;
|
||||||
|
import net.minecraft.util.math.Direction;
|
||||||
|
|
||||||
|
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.isBlockBreakingRestricted(player.getWorld(), hit.getBlockPos(), player.interactionManager.getGameMode())) {
|
||||||
|
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.getWorld().getBlockState(currentBlock).isAir()) {
|
||||||
|
done = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
BlockState state = player.getWorld().getBlockState(currentBlock);
|
||||||
|
boolean blockBroken = false;
|
||||||
|
if (first) {
|
||||||
|
first = false;
|
||||||
|
player.interactionManager.processBlockBreakingAction(currentBlock, PlayerActionC2SPacket.Action.START_DESTROY_BLOCK, newBlockHit.getSide(), player.getWorld().getTopYInclusive(), -1);
|
||||||
|
boolean notAir = !state.isAir();
|
||||||
|
if (notAir)
|
||||||
|
{
|
||||||
|
state.onBlockBreakStart(player.getWorld(), currentBlock, player);
|
||||||
|
}
|
||||||
|
if (notAir && state.calcBlockBreakingDelta(player, player.getWorld(), currentBlock) >= 1)
|
||||||
|
{
|
||||||
|
//instamine??
|
||||||
|
blockBroken = true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
currentBlockDamage += state.calcBlockBreakingDelta(player, player.getWorld(), currentBlock);
|
||||||
|
if (currentBlockDamage >= 1) {
|
||||||
|
player.interactionManager.processBlockBreakingAction(currentBlock, PlayerActionC2SPacket.Action.STOP_DESTROY_BLOCK, newBlockHit.getSide(), player.getWorld().getTopYInclusive(), -1);
|
||||||
|
ap.blockHitDelay = 5;
|
||||||
|
blockBroken = true;
|
||||||
|
}
|
||||||
|
player.getWorld().setBlockBreakingInfo(-1, currentBlock, (int) (currentBlockDamage * 10));
|
||||||
|
|
||||||
|
}
|
||||||
|
player.updateLastActionTime();
|
||||||
|
player.swingHand(Hand.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.getWorld().setBlockBreakingInfo(-1, currentBlock, -1);
|
||||||
|
player.interactionManager.processBlockBreakingAction(currentBlock, PlayerActionC2SPacket.Action.ABORT_DESTROY_BLOCK, Direction.DOWN, player.getWorld().getTopYInclusive(), -1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void readArguments(ValueSupplierList<MinionRuntime> arguments, MinionRuntime runtime) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void save(WriteView view, MinionRuntime runtime) {
|
||||||
|
view.put("currentBlock", BlockPos.CODEC, currentBlock);
|
||||||
|
view.putFloat("currentBlockDamage", currentBlockDamage);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void load(ReadView view, MinionRuntime runtime) {
|
||||||
|
currentBlock = view.read("currentBlock", BlockPos.CODEC).orElse(null);
|
||||||
|
currentBlockDamage = view.getFloat("currentBlockDamage", 0);
|
||||||
|
if(currentBlock == null) {
|
||||||
|
done = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+133
@@ -0,0 +1,133 @@
|
|||||||
|
package io.github.skippyall.minions.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.ValueSupplierList;
|
||||||
|
import io.github.skippyall.minions.registration.ValueTypes;
|
||||||
|
import net.minecraft.item.ItemStack;
|
||||||
|
import net.minecraft.screen.ScreenHandler;
|
||||||
|
import net.minecraft.screen.slot.Slot;
|
||||||
|
import net.minecraft.screen.slot.SlotActionType;
|
||||||
|
import net.minecraft.storage.ReadView;
|
||||||
|
import net.minecraft.storage.WriteView;
|
||||||
|
|
||||||
|
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.currentScreenHandler == 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().offerOrDrop(cursor);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ScreenHandler getScreen(MinionFakePlayer minion, boolean screen) {
|
||||||
|
if(screen) {
|
||||||
|
return minion.currentScreenHandler;
|
||||||
|
} else {
|
||||||
|
return minion.playerScreenHandler;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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).getStack();
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean canExchange(MinionFakePlayer minion, int slotIndex, boolean screen, ItemStack newStack) {
|
||||||
|
ScreenHandler screenHandler = getScreen(minion, screen);
|
||||||
|
Slot slot = screenHandler.getSlot(slotIndex);
|
||||||
|
if(!slot.getStack().isEmpty() && !slot.canTakeItems(minion)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if(!newStack.isEmpty() && !slot.canInsert(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) {
|
||||||
|
ScreenHandler screenHandler = getScreen(minion, screen);
|
||||||
|
ItemStack previousCursor = screenHandler.getCursorStack();
|
||||||
|
screenHandler.setCursorStack(cursor);
|
||||||
|
screenHandler.onSlotClick(slotIndex, 0, SlotActionType.SWAP, minion);
|
||||||
|
cursor = screenHandler.getCursorStack();
|
||||||
|
screenHandler.setCursorStack(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(ValueSupplierList<MinionRuntime> arguments, MinionRuntime runtime) {
|
||||||
|
fromSlot = Math.clamp(arguments.getValue(FROM_SLOT, runtime), 0, Integer.MAX_VALUE);
|
||||||
|
fromScreen = arguments.getValue(FROM_SCREEN, runtime);
|
||||||
|
toSlot = Math.clamp(arguments.getValue(TO_SLOT, runtime), 0, Integer.MAX_VALUE);
|
||||||
|
toScreen = arguments.getValue(TO_SCREEN, runtime);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void save(WriteView view, MinionRuntime runtime) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void load(ReadView view, MinionRuntime runtime) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package io.github.skippyall.minions.instruction.move;
|
||||||
|
|
||||||
|
import io.github.skippyall.minions.minion.MinionRuntime;
|
||||||
|
import io.github.skippyall.minions.program.instruction.InstructionExecution;
|
||||||
|
import net.minecraft.storage.ReadView;
|
||||||
|
import net.minecraft.storage.WriteView;
|
||||||
|
|
||||||
|
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().getYaw();
|
||||||
|
float rotatePitch = targetPitch - minion.getMinion().getPitch();
|
||||||
|
|
||||||
|
minion.getMinion().getMinionActionPack().turn(Math.min(rotateYaw, anglePerTick), Math.min(rotatePitch, anglePerTick));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isDone(MinionRuntime minion) {
|
||||||
|
return Math.abs(targetYaw - minion.getMinion().getYaw()) < 0.001F && Math.abs(targetPitch - minion.getMinion().getPitch()) < 0.001F;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void save(WriteView view, MinionRuntime runtime) {
|
||||||
|
view.putFloat("targetYaw", targetYaw);
|
||||||
|
view.putFloat("targetPitch", targetPitch);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void load(ReadView view, MinionRuntime runtime) {
|
||||||
|
targetYaw = view.getFloat("targetYaw", 0);
|
||||||
|
targetPitch = view.getFloat("targetPitch", 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
package io.github.skippyall.minions.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.entity.MovementType;
|
||||||
|
|
||||||
|
public class ContinuousWalkExecution implements ContinuousInstructionExecution<MinionRuntime>, InstructionExecution.Stateless<MinionRuntime> {
|
||||||
|
@Override
|
||||||
|
public void tick(MinionRuntime minion) {
|
||||||
|
minion.getMinion().move(MovementType.SELF, minion.getMinion().getHorizontalFacing().getDoubleVector().normalize().multiply(minion.getMinion().getMovementSpeed()));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package io.github.skippyall.minions.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.StringIdentifiable;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public enum TurnDirection implements StringIdentifiable, Displayable {
|
||||||
|
LEFT("left", -1, 0),
|
||||||
|
UP("up", 0, -1),
|
||||||
|
RIGHT("right", 1, 0),
|
||||||
|
DOWN("down", 0, 1);
|
||||||
|
|
||||||
|
public static final Codec<TurnDirection> CODEC = StringIdentifiable.createCodec(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 asString() {
|
||||||
|
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);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package io.github.skippyall.minions.instruction.move;
|
||||||
|
|
||||||
|
import io.github.skippyall.minions.minion.MinionRuntime;
|
||||||
|
import io.github.skippyall.minions.program.supplier.ValueSupplierList;
|
||||||
|
import io.github.skippyall.minions.program.supplier.Parameter;
|
||||||
|
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(ValueSupplierList<MinionRuntime> arguments, MinionRuntime minion) {
|
||||||
|
float maxAngle = arguments.getValue(ANGLE, minion).floatValue();
|
||||||
|
TurnDirection direction = arguments.getValue(DIRECTION, minion);
|
||||||
|
|
||||||
|
float turnYaw = maxAngle * direction.xFactor;
|
||||||
|
float turnPitch = maxAngle * direction.yFactor;
|
||||||
|
|
||||||
|
targetYaw = minion.getMinion().getYaw() + turnYaw;
|
||||||
|
targetPitch = minion.getMinion().getPitch() + turnPitch;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package io.github.skippyall.minions.instruction.move;
|
||||||
|
|
||||||
|
import io.github.skippyall.minions.minion.MinionRuntime;
|
||||||
|
import io.github.skippyall.minions.program.supplier.Parameter;
|
||||||
|
import io.github.skippyall.minions.program.supplier.ValueSupplierList;
|
||||||
|
import io.github.skippyall.minions.registration.ValueTypes;
|
||||||
|
import net.minecraft.util.math.MathHelper;
|
||||||
|
import net.minecraft.util.math.Vec2f;
|
||||||
|
import net.minecraft.util.math.Vec3d;
|
||||||
|
|
||||||
|
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(ValueSupplierList<MinionRuntime> arguments, MinionRuntime runtime) {
|
||||||
|
double x = arguments.getValue(X, runtime);
|
||||||
|
double y = arguments.getValue(Y, runtime);
|
||||||
|
double z = arguments.getValue(Z, runtime);
|
||||||
|
|
||||||
|
Vec3d vector = new Vec3d(x, y, z);
|
||||||
|
Vec2f rotation = vectorToRotation(vector);
|
||||||
|
targetYaw = rotation.x;
|
||||||
|
targetPitch = rotation.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
//copied from Entity#lookAt (why no helper, Mojang?)
|
||||||
|
public static Vec2f vectorToRotation(Vec3d vector) {
|
||||||
|
double g = Math.sqrt(vector.x * vector.x + vector.z * vector.z);
|
||||||
|
float pitch = MathHelper.wrapDegrees((float)(-(MathHelper.atan2(vector.y, g) * 180.0F / (float)Math.PI)));
|
||||||
|
float yaw = MathHelper.wrapDegrees((float)(MathHelper.atan2(vector.z, vector.x) * 180.0F / (float)Math.PI) - 90.0F);
|
||||||
|
|
||||||
|
return new Vec2f(yaw, pitch);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package io.github.skippyall.minions.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.ValueSupplierList;
|
||||||
|
import io.github.skippyall.minions.registration.ValueTypes;
|
||||||
|
import net.minecraft.entity.MovementType;
|
||||||
|
import net.minecraft.storage.ReadView;
|
||||||
|
import net.minecraft.storage.WriteView;
|
||||||
|
|
||||||
|
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().getMovementSpeed(), totalBlocksToMove - blocksMoved);
|
||||||
|
minion.getMinion().move(MovementType.SELF, minion.getMinion().getHorizontalFacing().getDoubleVector().normalize().multiply(speed));
|
||||||
|
blocksMoved += speed;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isDone(MinionRuntime minion) {
|
||||||
|
return totalBlocksToMove - blocksMoved < ACCURACY;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void readArguments(ValueSupplierList<MinionRuntime> parameters, MinionRuntime minion) {
|
||||||
|
totalBlocksToMove = parameters.getValue(blocksToMoveParam, minion).floatValue();
|
||||||
|
blocksMoved = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void save(WriteView view, MinionRuntime minion) {
|
||||||
|
view.putDouble("totalBlocksToMove", totalBlocksToMove);
|
||||||
|
view.putDouble("blocksMoved", blocksMoved);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void load(ReadView view, MinionRuntime minion) {
|
||||||
|
totalBlocksToMove = view.getDouble("totalBlocksToMove", 0F);
|
||||||
|
blocksMoved = view.getDouble("blocksMoved", 0F);
|
||||||
|
}
|
||||||
|
}
|
||||||
+55
@@ -0,0 +1,55 @@
|
|||||||
|
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.block.entity.BlockEntity;
|
||||||
|
import net.minecraft.block.entity.BlockEntityType;
|
||||||
|
import net.minecraft.registry.RegistryKey;
|
||||||
|
import net.minecraft.server.MinecraftServer;
|
||||||
|
import net.minecraft.util.math.BlockPos;
|
||||||
|
import net.minecraft.world.World;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public abstract class BlockEntityMinionInstructionListener<E extends BlockEntity> extends BlockEntityMinionListener<E> {
|
||||||
|
protected BlockEntityMinionInstructionListener(RegistryKey<World> 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
|
||||||
|
protected 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.block.entity.BlockEntity;
|
||||||
|
import net.minecraft.block.entity.BlockEntityType;
|
||||||
|
import net.minecraft.registry.RegistryKey;
|
||||||
|
import net.minecraft.server.MinecraftServer;
|
||||||
|
import net.minecraft.util.Uuids;
|
||||||
|
import net.minecraft.util.math.BlockPos;
|
||||||
|
import net.minecraft.world.World;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public abstract class BlockEntityMinionListener<E extends BlockEntity> implements MinionListener {
|
||||||
|
protected RegistryKey<World> worldKey;
|
||||||
|
protected BlockPos pos;
|
||||||
|
protected UUID minionUuid;
|
||||||
|
protected BlockEntityType<E> type;
|
||||||
|
protected @Nullable MinionFakePlayer minion;
|
||||||
|
|
||||||
|
protected BlockEntityMinionListener(RegistryKey<World> 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(World 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.getRegistryKey()) && clazz.isInstance(tl)) {
|
||||||
|
return clazz.cast(tl);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <L extends BlockEntityMinionListener<?>> Codec<L> getCodec(Function3<RegistryKey<World>, BlockPos, UUID, L> constructor) {
|
||||||
|
return RecordCodecBuilder.create(instance ->
|
||||||
|
instance.group(
|
||||||
|
World.CODEC.fieldOf("world").forGetter(listener -> listener.worldKey),
|
||||||
|
BlockPos.CODEC.fieldOf("pos").forGetter(listener -> listener.pos),
|
||||||
|
Uuids.CODEC.fieldOf("minionUuid").forGetter(listener -> listener.minionUuid)
|
||||||
|
).apply(instance, constructor));
|
||||||
|
}
|
||||||
|
|
||||||
|
private BlockEntityState getBlockEntityState(MinecraftServer server) {
|
||||||
|
World world = server.getWorld(worldKey);
|
||||||
|
if(world == null || !world.isPosLoaded(pos)) {
|
||||||
|
return BlockEntityState.UNLOADED;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(world.getBlockEntity(pos, type).isPresent()) {
|
||||||
|
return BlockEntityState.LOADED;
|
||||||
|
} else {
|
||||||
|
return BlockEntityState.REMOVED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<E> getBlockEntity(MinecraftServer server) {
|
||||||
|
World world = server.getWorld(worldKey);
|
||||||
|
if(world != null && world.isPosLoaded(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;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void add(MinecraftServer server) {
|
||||||
|
MinionPersistentState.get(server).getMinionData(minionUuid).listeners().addListener(this);
|
||||||
|
MinionPersistentState.get(server).markDirty();
|
||||||
|
this.minion = (MinionFakePlayer) server.getPlayerManager().getPlayer(minionUuid);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void remove(MinecraftServer server) {
|
||||||
|
MinionPersistentState.get(server).getMinionData(minionUuid).listeners().removeListener(this);
|
||||||
|
MinionPersistentState.get(server).markDirty();
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum BlockEntityState {
|
||||||
|
LOADED,
|
||||||
|
REMOVED,
|
||||||
|
UNLOADED
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasListener(T listener) {
|
||||||
|
return listeners.contains(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull Iterator<T> iterator() {
|
||||||
|
return listeners.iterator();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
return super.equals(obj);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,81 @@
|
|||||||
|
package io.github.skippyall.minions.listener;
|
||||||
|
|
||||||
|
import com.mojang.serialization.Codec;
|
||||||
|
import net.minecraft.registry.Registry;
|
||||||
|
import net.minecraft.storage.ReadView;
|
||||||
|
import net.minecraft.storage.WriteView;
|
||||||
|
import net.minecraft.util.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> {
|
||||||
|
private final Registry<Codec<? extends T>> registry;
|
||||||
|
|
||||||
|
public SerializableListenerManager(Registry<Codec<? extends T>> registry) {
|
||||||
|
this.registry = registry;
|
||||||
|
}
|
||||||
|
|
||||||
|
private SerializableListenerManager(Registry<Codec<? extends T>> registry, Set<T> listeners) {
|
||||||
|
super(listeners);
|
||||||
|
this.registry = registry;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T extends SerializableListener> Codec<SerializableListenerManager<T>> getCodec(Registry<Codec<? extends T>> registry) {
|
||||||
|
return registry.getCodec().<T>dispatch(
|
||||||
|
listener -> listener.getCodecId().map(registry::get).orElse(Codec.unit(null)),
|
||||||
|
codec -> codec.fieldOf("data")
|
||||||
|
).listOf().xmap(
|
||||||
|
list -> new SerializableListenerManager<>(registry, new CopyOnWriteArraySet<>(list)),
|
||||||
|
manager -> {
|
||||||
|
List<T> serializableListeners = new ArrayList<>();
|
||||||
|
for(T listener : manager.listeners) {
|
||||||
|
if(listener.getCodecId().isPresent()) {
|
||||||
|
serializableListeners.add(listener);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return serializableListeners;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void save(WriteView view) {
|
||||||
|
WriteView.ListView listView = view.getList("listeners");
|
||||||
|
for (T listener : listeners) {
|
||||||
|
if(listener.getCodecId().isPresent()) {
|
||||||
|
WriteView listenerView = listView.add();
|
||||||
|
Codec<? extends T> codec = registry.get(listener.getCodecId().get());
|
||||||
|
listenerView.put("id", Identifier.CODEC, listener.getCodecId().get());
|
||||||
|
//noinspection unchecked
|
||||||
|
listenerView.put("data", (Codec<T>) codec, listener);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void load(ReadView view) {
|
||||||
|
ReadView.ListReadView listView = view.getListReadView("listeners");
|
||||||
|
for (ReadView listenerView : listView) {
|
||||||
|
Optional<Identifier> id = listenerView.read("id", Identifier.CODEC);
|
||||||
|
if(id.isEmpty()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
Codec<? extends T> codec = registry.get(id.get());
|
||||||
|
|
||||||
|
Optional<? extends T> listener = listenerView.read("data", codec);
|
||||||
|
|
||||||
|
if(listener.isPresent()) {
|
||||||
|
listeners.add(listener.get());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface SerializableListener {
|
||||||
|
default Optional<Identifier> getCodecId() {
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
package io.github.skippyall.minions.minion;
|
|
||||||
|
|
||||||
import com.mojang.serialization.Codec;
|
|
||||||
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
|
||||||
import net.minecraft.component.ComponentType;
|
|
||||||
|
|
||||||
public class MinionComponent {
|
|
||||||
/*public static final Codec<MinionComponent> CODEC = RecordCodecBuilder.create(instance ->
|
|
||||||
instance.group()
|
|
||||||
);
|
|
||||||
public static final ComponentType<MinionComponent> TYPE = ComponentType.<MinionComponent>builder().codec(CODEC).build();
|
|
||||||
*/
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -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.util.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.getCodec(),
|
||||||
|
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,45 +1,59 @@
|
|||||||
package io.github.skippyall.minions.minion;
|
package io.github.skippyall.minions.minion;
|
||||||
|
|
||||||
import io.github.skippyall.minions.fakeplayer.MinionFakePlayer;
|
import com.mojang.authlib.properties.PropertyMap;
|
||||||
import net.minecraft.nbt.NbtCompound;
|
import com.mojang.serialization.Codec;
|
||||||
|
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
||||||
|
import io.github.skippyall.minions.registration.MinionRegistries;
|
||||||
|
import io.github.skippyall.minions.listener.SerializableListenerManager;
|
||||||
|
import net.minecraft.server.MinecraftServer;
|
||||||
|
import net.minecraft.util.Uuids;
|
||||||
|
import net.minecraft.util.dynamic.Codecs;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
public class MinionData {
|
public record MinionData(
|
||||||
public UUID uuid;
|
UUID uuid,
|
||||||
public String name;
|
String name,
|
||||||
public UUID skinUuid;
|
Optional<PropertyMap> skin,
|
||||||
|
boolean isSpawned,
|
||||||
|
SerializableListenerManager<MinionListener> listeners,
|
||||||
|
MinionConfig config
|
||||||
|
) {
|
||||||
|
public static final Codec<MinionData> CODEC = RecordCodecBuilder.create(instance ->
|
||||||
|
instance.group(
|
||||||
|
Uuids.CODEC.fieldOf("uuid").forGetter(MinionData::uuid),
|
||||||
|
Codec.STRING.fieldOf("name").forGetter(MinionData::name),
|
||||||
|
Codecs.GAME_PROFILE_PROPERTY_MAP.optionalFieldOf("skin").forGetter(MinionData::skin),
|
||||||
|
Codec.BOOL.optionalFieldOf("isSpawned", false).forGetter(MinionData::isSpawned),
|
||||||
|
SerializableListenerManager.getCodec(MinionRegistries.MINION_LISTENER_CODECS).optionalFieldOf("listeners").xmap(
|
||||||
|
optional -> optional.orElseGet(() -> new SerializableListenerManager<>(MinionRegistries.MINION_LISTENER_CODECS)),
|
||||||
|
Optional::of
|
||||||
|
).forGetter(MinionData::listeners),
|
||||||
|
MinionConfig.CODEC.optionalFieldOf("config", new MinionConfig()).forGetter(MinionData::config)
|
||||||
|
).apply(instance, MinionData::new)
|
||||||
|
);
|
||||||
|
|
||||||
public MinionData(UUID uuid, String name, UUID skinUuid) {
|
public static MinionData createDefault(MinecraftServer server) {
|
||||||
this.uuid = uuid;
|
return new MinionData(
|
||||||
this.name = name;
|
UUID.randomUUID(),
|
||||||
this.skinUuid = skinUuid;
|
MinionProfileUtils.newDefaultMinionName(server),
|
||||||
|
Optional.empty(),
|
||||||
|
false,
|
||||||
|
new SerializableListenerManager<>(MinionRegistries.MINION_LISTENER_CODECS),
|
||||||
|
new MinionConfig()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public NbtCompound writeNbt() {
|
public MinionData withName(String name) {
|
||||||
NbtCompound nbt = new NbtCompound();
|
return new MinionData(uuid, name, skin, isSpawned, listeners, config);
|
||||||
|
|
||||||
nbt.putUuid("uuid", uuid);
|
|
||||||
nbt.putString("name", name);
|
|
||||||
if(skinUuid != null) {
|
|
||||||
nbt.putUuid("skinUuid", skinUuid);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nbt;
|
public MinionData withSkin(Optional<PropertyMap> skin) {
|
||||||
|
return new MinionData(uuid, name, skin, isSpawned, listeners, config);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static MinionData readNbt(NbtCompound nbt) {
|
public MinionData withSpawned(boolean isSpawned) {
|
||||||
UUID uuid = nbt.getUuid("uuid");
|
return new MinionData(uuid, name, skin, isSpawned, listeners, config);
|
||||||
String name = nbt.getString("name");
|
|
||||||
UUID skinUuid = null;
|
|
||||||
if(nbt.contains("skinUuid")) {
|
|
||||||
skinUuid = nbt.getUuid("skinUuid");
|
|
||||||
}
|
|
||||||
|
|
||||||
return new MinionData(uuid, name, skinUuid);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static MinionData fromMinion(MinionFakePlayer minion) {
|
|
||||||
return new MinionData(minion.getUuid(), minion.getMinionName(), minion.getSkinUuid());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,91 +2,105 @@ 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 eu.pb4.polymer.core.api.item.PolymerItemUtils;
|
||||||
import io.github.skippyall.minions.fakeplayer.MinionFakePlayer;
|
import io.github.skippyall.minions.gui.MinionLookGui;
|
||||||
|
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
|
||||||
|
import io.github.skippyall.minions.registration.MinionComponentTypes;
|
||||||
import net.minecraft.component.DataComponentTypes;
|
import net.minecraft.component.DataComponentTypes;
|
||||||
import net.minecraft.component.type.NbtComponent;
|
import net.minecraft.component.type.TooltipDisplayComponent;
|
||||||
|
import net.minecraft.entity.player.PlayerEntity;
|
||||||
import net.minecraft.item.Item;
|
import net.minecraft.item.Item;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
import net.minecraft.item.ItemUsageContext;
|
import net.minecraft.item.ItemUsageContext;
|
||||||
import net.minecraft.item.Items;
|
import net.minecraft.item.Items;
|
||||||
import net.minecraft.item.tooltip.TooltipType;
|
import net.minecraft.item.tooltip.TooltipType;
|
||||||
import net.minecraft.nbt.NbtCompound;
|
import net.minecraft.server.MinecraftServer;
|
||||||
import net.minecraft.nbt.NbtElement;
|
|
||||||
import net.minecraft.registry.RegistryWrapper;
|
|
||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
import net.minecraft.server.world.ServerWorld;
|
import net.minecraft.server.world.ServerWorld;
|
||||||
|
import net.minecraft.text.Text;
|
||||||
import net.minecraft.util.ActionResult;
|
import net.minecraft.util.ActionResult;
|
||||||
|
import net.minecraft.util.Hand;
|
||||||
|
import net.minecraft.util.Identifier;
|
||||||
import net.minecraft.util.math.Vec2f;
|
import net.minecraft.util.math.Vec2f;
|
||||||
|
import net.minecraft.world.World;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import xyz.nucleoid.packettweaker.PacketContext;
|
||||||
|
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public class MinionItem extends Item implements PolymerItem {
|
public class MinionItem extends Item implements PolymerItem {
|
||||||
private final boolean canProgram;
|
public MinionItem(Settings settings) {
|
||||||
|
super(settings);
|
||||||
public MinionItem(boolean canProgram) {
|
|
||||||
super(new Item.Settings());
|
|
||||||
this.canProgram = canProgram;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Item getPolymerItem(ItemStack itemStack, @Nullable ServerPlayerEntity player) {
|
public @Nullable Identifier getPolymerItemModel(ItemStack stack, PacketContext context) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Item getPolymerItem(ItemStack itemStack, PacketContext player) {
|
||||||
return Items.ARMOR_STAND;
|
return Items.ARMOR_STAND;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ItemStack getPolymerItemStack(ItemStack stack, TooltipType tooltipType, RegistryWrapper.WrapperLookup lookup, ServerPlayerEntity player) {
|
public ItemStack getPolymerItemStack(ItemStack stack, TooltipType tooltipType, PacketContext player) {
|
||||||
ItemStack out = PolymerItemUtils.createItemStack(stack, lookup, player);
|
ItemStack out = PolymerItemUtils.createItemStack(stack, tooltipType, player);
|
||||||
out.set(DataComponentTypes.ENCHANTMENT_GLINT_OVERRIDE, true);
|
out.set(DataComponentTypes.ENCHANTMENT_GLINT_OVERRIDE, true);
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void appendTooltip(ItemStack stack, TooltipContext context, TooltipDisplayComponent component, Consumer<Text> tooltip, TooltipType type) {
|
||||||
|
MinionData data = null /*getData(stack)*/;
|
||||||
|
if(data != null) {
|
||||||
|
tooltip.accept(Text.translatable("minions.minion_item.tooltip", data.name()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ActionResult use(World world, PlayerEntity user, Hand hand) {
|
||||||
|
if(user instanceof ServerPlayerEntity serverPlayer) {
|
||||||
|
ItemStack stack = user.getStackInHand(hand);
|
||||||
|
MinionLookGui.open(serverPlayer, stack);
|
||||||
|
return ActionResult.SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ActionResult.SUCCESS_SERVER;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ActionResult useOnBlock(ItemUsageContext context) {
|
public ActionResult useOnBlock(ItemUsageContext context) {
|
||||||
String contents = context.getStack().getName().getLiteralString();
|
|
||||||
String name;
|
|
||||||
if(contents != null && contents.length() <= 16) {
|
|
||||||
name = contents;
|
|
||||||
} else {
|
|
||||||
name = "Minion";
|
|
||||||
}
|
|
||||||
if(!context.getWorld().isClient) {
|
if(!context.getWorld().isClient) {
|
||||||
MinionData data = getData(context.getStack());
|
MinionData data = getDataOrDefault(context.getWorld().getServer(), context.getStack());
|
||||||
|
MinionFakePlayer.spawnMinion(data, (ServerWorld) context.getWorld(), context.getBlockPos().toCenterPos().add(0,0.5,0), new Vec2f(0, 0));
|
||||||
if(data == null) {
|
|
||||||
data = new MinionData(null, name, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data.uuid == null) {
|
|
||||||
MinionFakePlayer.createMinion(data, (ServerWorld) context.getWorld(), (ServerPlayerEntity) context.getPlayer(), canProgram, context.getBlockPos().toCenterPos().add(0,0.5,0), 0, 0);
|
|
||||||
}else {
|
|
||||||
data.name = name;
|
|
||||||
MinionFakePlayer.spawnMinionAt(data, (ServerWorld) context.getWorld(), context.getBlockPos().toCenterPos().add(0,0.5,0), new Vec2f(0, 0));
|
|
||||||
MinionPersistentState.INSTANCE.addMinion(data);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
context.getStack().decrement(1);
|
context.getStack().decrement(1);
|
||||||
return ActionResult.SUCCESS;
|
return ActionResult.SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setData(MinionData data, ItemStack item) {
|
public static void setData(MinecraftServer server, MinionData data, ItemStack item) {
|
||||||
NbtCompound nbt = item.getOrDefault(DataComponentTypes.CUSTOM_DATA, NbtComponent.DEFAULT).copyNbt();
|
item.set(MinionComponentTypes.MINION_DATA, data.uuid());
|
||||||
nbt.put("data", data.writeNbt());
|
MinionPersistentState.get(server).updateMinionData(data);
|
||||||
item.set(DataComponentTypes.CUSTOM_DATA, NbtComponent.of(nbt));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public static MinionData getData(ItemStack item) {
|
public static MinionData getData(MinecraftServer server, ItemStack item) {
|
||||||
NbtCompound nbt = item.getOrDefault(DataComponentTypes.CUSTOM_DATA, NbtComponent.DEFAULT).copyNbt();
|
if(item.contains(MinionComponentTypes.MINION_DATA)) {
|
||||||
if (nbt.getType("data") == NbtElement.COMPOUND_TYPE) {
|
return MinionPersistentState.get(server).getMinionData(item.get(MinionComponentTypes.MINION_DATA));
|
||||||
return MinionData.readNbt(nbt.getCompound("data"));
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static MinionData getDataOrDefault(MinecraftServer server, ItemStack item) {
|
||||||
|
MinionData data = getData(server, item);
|
||||||
|
if(data == null) {
|
||||||
|
data = MinionData.createDefault(server);
|
||||||
|
setData(server, data, item);
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
public static boolean containsData(ItemStack item) {
|
public static boolean containsData(ItemStack item) {
|
||||||
NbtComponent nbt = item.get(DataComponentTypes.CUSTOM_DATA);
|
return item.contains(MinionComponentTypes.MINION_DATA);
|
||||||
if (nbt == null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return nbt.copyNbt().contains("data");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package io.github.skippyall.minions.minion;
|
||||||
|
|
||||||
|
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
|
||||||
|
import io.github.skippyall.minions.program.instruction.ConfiguredInstruction;
|
||||||
|
import io.github.skippyall.minions.listener.SerializableListenerManager;
|
||||||
|
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,76 +1,66 @@
|
|||||||
package io.github.skippyall.minions.minion;
|
package io.github.skippyall.minions.minion;
|
||||||
|
|
||||||
import io.github.skippyall.minions.fakeplayer.MinionFakePlayer;
|
import com.mojang.serialization.Codec;
|
||||||
import net.minecraft.nbt.NbtCompound;
|
|
||||||
import net.minecraft.nbt.NbtElement;
|
|
||||||
import net.minecraft.nbt.NbtList;
|
|
||||||
import net.minecraft.registry.RegistryWrapper;
|
|
||||||
import net.minecraft.server.MinecraftServer;
|
import net.minecraft.server.MinecraftServer;
|
||||||
import net.minecraft.world.PersistentState;
|
import net.minecraft.world.PersistentState;
|
||||||
|
import net.minecraft.world.PersistentStateType;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
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 PersistentState {
|
||||||
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 PersistentStateType<MinionPersistentState> TYPE = new PersistentStateType<>("minion", MinionPersistentState::new, MinionPersistentState.CODEC, null);
|
||||||
|
|
||||||
private List<MinionData> minionData = new ArrayList<>();
|
private final Map<UUID, MinionData> minionData = new HashMap<>();
|
||||||
|
|
||||||
|
public MinionPersistentState() {
|
||||||
|
|
||||||
@Override
|
|
||||||
public NbtCompound writeNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLookup) {
|
|
||||||
NbtList list = new NbtList();
|
|
||||||
for(MinionData data : minionData) {
|
|
||||||
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) {
|
|
||||||
instance.addMinion(MinionData.readNbt((NbtCompound) element));
|
|
||||||
}
|
}
|
||||||
return instance;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addMinion(MinionFakePlayer minion) {
|
public MinionData getMinionData(UUID uuid) {
|
||||||
addMinion(MinionData.fromMinion(minion));
|
return minionData.get(uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addMinion(MinionData data) {
|
public Map<UUID, MinionData> getMinionData() {
|
||||||
System.out.println("add Minion " + data.name);
|
|
||||||
minionData.add(data);
|
|
||||||
markDirty();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void removeMinion(MinionFakePlayer minionData) {
|
|
||||||
removeMinion(minionData.getUuid());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void removeMinion(UUID minionUUID) {
|
|
||||||
MinionData removal = null;
|
|
||||||
for (MinionData data : minionData) {
|
|
||||||
if (data.uuid.equals(minionUUID)) {
|
|
||||||
removal = data;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (removal != null) {
|
|
||||||
minionData.remove(removal);
|
|
||||||
}
|
|
||||||
markDirty();
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<MinionData> getMinionData() {
|
|
||||||
return minionData;
|
return minionData;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void create(MinecraftServer server) {
|
public List<MinionData> getMinionDataList() {
|
||||||
INSTANCE = server.getWorld(World.OVERWORLD).getPersistentStateManager().getOrCreate(TYPE, "minion");
|
return List.copyOf(minionData.values());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateMinionData(MinionData data) {
|
||||||
|
minionData.put(data.uuid(), data);
|
||||||
|
markDirty();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isMinion(UUID uuid) {
|
||||||
|
return minionData.containsKey(uuid);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isMinionNameTaken(String name) {
|
||||||
|
return getMinionWithName(name).isPresent();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<MinionData> getMinionWithName(String name) {
|
||||||
|
return minionData.values().stream()
|
||||||
|
.filter(data -> data.name().equals(name))
|
||||||
|
.findFirst();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static MinionPersistentState get(MinecraftServer server) {
|
||||||
|
return server.getWorld(World.OVERWORLD).getPersistentStateManager().getOrCreate(TYPE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,71 +1,67 @@
|
|||||||
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.yggdrasil.ProfileResult;
|
import com.mojang.brigadier.StringReader;
|
||||||
|
import io.github.skippyall.minions.MinionsConfig;
|
||||||
|
import io.github.skippyall.minions.gui.input.Result;
|
||||||
import net.minecraft.server.MinecraftServer;
|
import net.minecraft.server.MinecraftServer;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import net.minecraft.text.Text;
|
||||||
|
import net.minecraft.util.StringHelper;
|
||||||
|
|
||||||
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 CompletableFuture<@Nullable GameProfile> lookupSkinOwnerProfile(MinecraftServer server, String username) {
|
public static String getPrefix() {
|
||||||
CompletableFuture<GameProfile> future = new CompletableFuture<>();
|
return MinionsConfig.get().minion.minionPrefix;
|
||||||
|
|
||||||
ForkJoinPool.commonPool().execute(() -> {
|
|
||||||
try {
|
|
||||||
server.getGameProfileRepo().findProfilesByNames(new String[]{username}, new ProfileLookupCallback() {
|
|
||||||
@Override
|
|
||||||
public void onProfileLookupSucceeded(GameProfile found) {
|
|
||||||
LOGGER.info("SkinProfile: {}", found);
|
|
||||||
try {
|
|
||||||
getSkinOwnerProfile(server, found.getId()).thenAccept(future::complete);
|
|
||||||
} catch (Throwable ex) {
|
|
||||||
LOGGER.warn("Exception during Game Profile creation", ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public static GameProfile makeNewMinionProfile(UUID uuidMinion, String username, PropertyMap skin) {
|
||||||
public void onProfileLookupFailed(String profileName, Exception exception) {
|
if(uuidMinion == null) {
|
||||||
LOGGER.warn("Lookup Error: ", exception);
|
uuidMinion = UUID.randomUUID();
|
||||||
future.complete(null);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch (Throwable e) {
|
|
||||||
LOGGER.warn("Failed to get UUID for username " + username, e);
|
|
||||||
future.complete(null);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return future;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static CompletableFuture<@Nullable GameProfile> getSkinOwnerProfile(MinecraftServer server, @Nullable UUID uuid) {
|
GameProfile newProfile = new GameProfile(uuidMinion, username);
|
||||||
CompletableFuture<GameProfile> future = new CompletableFuture<>();
|
if (skin != null) {
|
||||||
future.completeAsync(() -> {
|
newProfile.getProperties().putAll(skin);
|
||||||
GameProfile profile = null;
|
|
||||||
if(uuid != null) {
|
|
||||||
ProfileResult result = server.getSessionService().fetchProfile(uuid, true);
|
|
||||||
if (result != null) {
|
|
||||||
profile = result.profile();
|
|
||||||
LOGGER.info("Full SkinProfile: {}", profile);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return profile;
|
|
||||||
});
|
|
||||||
return future;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static GameProfile makeNewMinionProfile(@Nullable UUID uuidMinion, String username, @Nullable GameProfile skinProfile) {
|
|
||||||
GameProfile newProfile = new GameProfile(uuidMinion != null ? uuidMinion : UUID.randomUUID(), username);
|
|
||||||
if (skinProfile != null) {
|
|
||||||
newProfile.getProperties().putAll(skinProfile.getProperties());
|
|
||||||
}
|
}
|
||||||
LOGGER.info("Minion Profile: {}", newProfile);
|
LOGGER.info("Minion Profile: {}", newProfile);
|
||||||
return newProfile;
|
return newProfile;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Result<String, Text> checkMinionNameWithoutPrefix(MinecraftServer server, String name) {
|
||||||
|
for(char c : name.toCharArray()) {
|
||||||
|
if(!StringReader.isAllowedInUnquotedString(c)) {
|
||||||
|
return new Result.Error<>(Text.translatable("minions.generic.name.invalid_char"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if((getPrefix() + name).length() > 16) {
|
||||||
|
return new Result.Error<>(Text.translatable("minions.generic.name.too_long"));
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!StringHelper.isValidPlayerName(getPrefix() + name)) {
|
||||||
|
return new Result.Error<>(Text.translatable("minions.generic.name.invalid"));
|
||||||
|
}
|
||||||
|
|
||||||
|
if(MinionPersistentState.get(server).isMinionNameTaken(getPrefix() + name)) {
|
||||||
|
return new Result.Error<>(Text.translatable("minions.generic.name.taken"));
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Result.Success<>(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String newDefaultMinionName(MinecraftServer server) {
|
||||||
|
int i = 0;
|
||||||
|
while (MinionPersistentState.get(server).isMinionNameTaken("+Minion" + i)) {
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return "+Minion" + i;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isMinion(MinecraftServer server, UUID uuid) {
|
||||||
|
return MinionPersistentState.get(server).isMinion(uuid);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,168 @@
|
|||||||
|
package io.github.skippyall.minions.minion;
|
||||||
|
|
||||||
|
import io.github.skippyall.minions.program.instruction.ConfiguredInstructionListener;
|
||||||
|
import io.github.skippyall.minions.registration.MinionRegistries;
|
||||||
|
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.supplier.ValueSupplierType;
|
||||||
|
import io.github.skippyall.minions.program.instruction.ConfiguredInstruction;
|
||||||
|
import io.github.skippyall.minions.program.instruction.InstructionType;
|
||||||
|
import io.github.skippyall.minions.program.consumer.ValueConsumerType;
|
||||||
|
import net.minecraft.registry.Registry;
|
||||||
|
import net.minecraft.storage.ReadView;
|
||||||
|
import net.minecraft.storage.WriteView;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
public class MinionRuntime implements InstructionRuntime<MinionRuntime> {
|
||||||
|
private final MinionFakePlayer minion;
|
||||||
|
private final Map<String, ConfiguredInstruction<MinionRuntime>> configuredInstructions = new HashMap<>();
|
||||||
|
private final Map<Integer, ConfiguredInstruction<MinionRuntime>> webSocketInstructions = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
public MinionRuntime(MinionFakePlayer minion) {
|
||||||
|
this.minion = minion;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MinionFakePlayer getMinion() {
|
||||||
|
return minion;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void tick() {
|
||||||
|
for (ConfiguredInstruction<MinionRuntime> instruction : configuredInstructions.values()) {
|
||||||
|
instruction.tick(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
for(ConfiguredInstruction<MinionRuntime> instruction : webSocketInstructions.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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for(ConfiguredInstruction<MinionRuntime> instruction : webSocketInstructions.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 addWebSocketInstruction(int id, ConfiguredInstruction<MinionRuntime> instruction) {
|
||||||
|
webSocketInstructions.put(id, instruction);
|
||||||
|
instruction.addListener(new ConfiguredInstructionListener() {
|
||||||
|
@Override
|
||||||
|
public void onStop(ConfiguredInstruction<?> instruction) {
|
||||||
|
webSocketInstructions.remove(id);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public void save(WriteView view) {
|
||||||
|
WriteView.ListView list = view.getList("configuredInstructions");
|
||||||
|
for (Map.Entry<String, ConfiguredInstruction<MinionRuntime>> instruction : configuredInstructions.entrySet()) {
|
||||||
|
WriteView inner = list.add();
|
||||||
|
inner.putString("name", instruction.getKey());
|
||||||
|
instruction.getValue().save(inner, this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void load(ReadView view) {
|
||||||
|
ReadView.ListReadView list = view.getListReadView("configuredInstructions");
|
||||||
|
for (ReadView inner : list) {
|
||||||
|
Optional<String> name = inner.getOptionalString("name");
|
||||||
|
if(name.isEmpty()) {
|
||||||
|
Minions.LOGGER.error("Tried deserializing configured instruction without a name of minion \"{}\":", minion.getGameProfile().getName());
|
||||||
|
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().getName(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Registry<ValueSupplierType<MinionRuntime>> getArgumentTypeRegistry() {
|
||||||
|
return MinionRegistries.VALUE_SUPPLIER_TYPES;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Registry<InstructionType<MinionRuntime>> getInstructionTypeRegistry() {
|
||||||
|
return MinionRegistries.INSTRUCTION_TYPES;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Registry<ValueConsumerType<MinionRuntime>> getValueConsumerTypeRegistry() {
|
||||||
|
return MinionRegistries.VALUE_CONSUMER_TYPES;
|
||||||
|
}
|
||||||
|
}
|
||||||
+2
-1
@@ -1,4 +1,5 @@
|
|||||||
package io.github.skippyall.minions.fakeplayer;
|
//code from https://github.com/gnembon/fabric-carpet
|
||||||
|
package io.github.skippyall.minions.minion.fakeplayer;
|
||||||
|
|
||||||
import io.netty.channel.Channel;
|
import io.netty.channel.Channel;
|
||||||
|
|
||||||
+65
-40
@@ -1,4 +1,5 @@
|
|||||||
package io.github.skippyall.minions.fakeplayer;
|
//partially code from https://github.com/gnembon/fabric-carpet
|
||||||
|
package io.github.skippyall.minions.minion.fakeplayer;
|
||||||
|
|
||||||
|
|
||||||
import java.util.EnumMap;
|
import java.util.EnumMap;
|
||||||
@@ -33,12 +34,12 @@ 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;
|
||||||
|
|
||||||
@@ -49,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();
|
||||||
@@ -70,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);
|
||||||
@@ -82,6 +91,12 @@ public class EntityPlayerActionPack
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public EntityPlayerActionPack stop(ActionType type) {
|
||||||
|
Action previous = actions.remove(type);
|
||||||
|
if (previous != null) type.stop(player, previous);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public EntityPlayerActionPack setSneaking(boolean doSneak)
|
public EntityPlayerActionPack setSneaking(boolean doSneak)
|
||||||
{
|
{
|
||||||
sneaking = doSneak;
|
sneaking = doSneak;
|
||||||
@@ -246,9 +261,13 @@ public class EntityPlayerActionPack
|
|||||||
if (strafing != 0.0F || player instanceof MinionFakePlayer) {
|
if (strafing != 0.0F || player instanceof MinionFakePlayer) {
|
||||||
player.sidewaysSpeed = strafing * vel;
|
player.sidewaysSpeed = strafing * vel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(blockHitDelay > 0) {
|
||||||
|
blockHitDelay--;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static HitResult getTarget(ServerPlayerEntity player)
|
public static HitResult getTarget(ServerPlayerEntity player)
|
||||||
{
|
{
|
||||||
double reach = player.interactionManager.isCreative() ? 5 : 4.5f;
|
double reach = player.interactionManager.isCreative() ? 5 : 4.5f;
|
||||||
return Tracer.rayTrace(player, 1, reach, false);
|
return Tracer.rayTrace(player, 1, reach, false);
|
||||||
@@ -274,14 +293,14 @@ public class EntityPlayerActionPack
|
|||||||
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.networkHandler.sendPacket(new UpdateSelectedSlotS2CPacket(slot-1));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -290,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).getActionPack();
|
EntityPlayerActionPack ap = player.getMinionActionPack();
|
||||||
if (ap.itemUseCooldown > 0)
|
if (ap.itemUseCooldown > 0)
|
||||||
{
|
{
|
||||||
ap.itemUseCooldown--;
|
ap.itemUseCooldown--;
|
||||||
@@ -310,16 +329,16 @@ public class EntityPlayerActionPack
|
|||||||
case BLOCK:
|
case BLOCK:
|
||||||
{
|
{
|
||||||
player.updateLastActionTime();
|
player.updateLastActionTime();
|
||||||
ServerWorld world = player.getServerWorld();
|
ServerWorld world = player.getWorld();
|
||||||
BlockHitResult blockHit = (BlockHitResult) hit;
|
BlockHitResult blockHit = (BlockHitResult) hit;
|
||||||
BlockPos pos = blockHit.getBlockPos();
|
BlockPos pos = blockHit.getBlockPos();
|
||||||
Direction side = blockHit.getSide();
|
Direction side = blockHit.getSide();
|
||||||
if (pos.getY() < player.getWorld().getTopY() - (side == Direction.UP ? 1 : 0) && world.canPlayerModifyAt(player, pos))
|
if (pos.getY() < player.getWorld().getTopYInclusive() - (side == Direction.UP ? 1 : 0) && world.canEntityModifyAt(player, pos))
|
||||||
{
|
{
|
||||||
ActionResult result = player.interactionManager.interactBlock(player, world, player.getStackInHand(hand), hand, blockHit);
|
ActionResult result = player.interactionManager.interactBlock(player, world, player.getStackInHand(hand), hand, blockHit);
|
||||||
if (result.isAccepted())
|
if (result instanceof ActionResult.Success success)
|
||||||
{
|
{
|
||||||
if (result.shouldSwingHand()) player.swingHand(hand);
|
if (success.swingSource() == ActionResult.SwingSource.SERVER) player.swingHand(hand);
|
||||||
ap.itemUseCooldown = 3;
|
ap.itemUseCooldown = 3;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -359,21 +378,21 @@ public class EntityPlayerActionPack
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
void inactiveTick(ServerPlayerEntity player, Action action)
|
void inactiveTick(MinionFakePlayer player, Action action)
|
||||||
{
|
{
|
||||||
EntityPlayerActionPack ap = ((ServerPlayerInterface) player).getActionPack();
|
EntityPlayerActionPack ap = player.getMinionActionPack();
|
||||||
ap.itemUseCooldown = 0;
|
ap.itemUseCooldown = 0;
|
||||||
player.stopUsingItem();
|
player.stopUsingItem();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
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.swingHand(Hand.MAIN_HAND);
|
||||||
@@ -383,10 +402,9 @@ public class EntityPlayerActionPack
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
case BLOCK: {
|
case BLOCK: {
|
||||||
EntityPlayerActionPack ap = ((ServerPlayerInterface) player).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;
|
||||||
@@ -402,7 +420,7 @@ public class EntityPlayerActionPack
|
|||||||
boolean blockBroken = false;
|
boolean blockBroken = false;
|
||||||
if (player.interactionManager.getGameMode().isCreative())
|
if (player.interactionManager.getGameMode().isCreative())
|
||||||
{
|
{
|
||||||
player.interactionManager.processBlockBreakingAction(pos, PlayerActionC2SPacket.Action.START_DESTROY_BLOCK, side, player.getWorld().getTopY(), -1);
|
player.interactionManager.processBlockBreakingAction(pos, PlayerActionC2SPacket.Action.START_DESTROY_BLOCK, side, player.getWorld().getTopYInclusive(), -1);
|
||||||
ap.blockHitDelay = 5;
|
ap.blockHitDelay = 5;
|
||||||
blockBroken = true;
|
blockBroken = true;
|
||||||
}
|
}
|
||||||
@@ -410,9 +428,9 @@ public class EntityPlayerActionPack
|
|||||||
{
|
{
|
||||||
if (ap.currentBlock != null)
|
if (ap.currentBlock != null)
|
||||||
{
|
{
|
||||||
player.interactionManager.processBlockBreakingAction(ap.currentBlock, PlayerActionC2SPacket.Action.ABORT_DESTROY_BLOCK, side, player.getWorld().getTopY(), -1);
|
player.interactionManager.processBlockBreakingAction(ap.currentBlock, PlayerActionC2SPacket.Action.ABORT_DESTROY_BLOCK, side, player.getWorld().getTopYInclusive(), -1);
|
||||||
}
|
}
|
||||||
player.interactionManager.processBlockBreakingAction(pos, PlayerActionC2SPacket.Action.START_DESTROY_BLOCK, side, player.getWorld().getTopY(), -1);
|
player.interactionManager.processBlockBreakingAction(pos, PlayerActionC2SPacket.Action.START_DESTROY_BLOCK, side, player.getWorld().getTopYInclusive(), -1);
|
||||||
boolean notAir = !state.isAir();
|
boolean notAir = !state.isAir();
|
||||||
if (notAir && ap.curBlockDamageMP == 0)
|
if (notAir && ap.curBlockDamageMP == 0)
|
||||||
{
|
{
|
||||||
@@ -435,7 +453,7 @@ public class EntityPlayerActionPack
|
|||||||
ap.curBlockDamageMP += state.calcBlockBreakingDelta(player, player.getWorld(), pos);
|
ap.curBlockDamageMP += state.calcBlockBreakingDelta(player, player.getWorld(), pos);
|
||||||
if (ap.curBlockDamageMP >= 1)
|
if (ap.curBlockDamageMP >= 1)
|
||||||
{
|
{
|
||||||
player.interactionManager.processBlockBreakingAction(pos, PlayerActionC2SPacket.Action.STOP_DESTROY_BLOCK, side, player.getWorld().getTopY(), -1);
|
player.interactionManager.processBlockBreakingAction(pos, PlayerActionC2SPacket.Action.STOP_DESTROY_BLOCK, side, player.getWorld().getTopYInclusive(), -1);
|
||||||
ap.currentBlock = null;
|
ap.currentBlock = null;
|
||||||
ap.blockHitDelay = 5;
|
ap.blockHitDelay = 5;
|
||||||
blockBroken = true;
|
blockBroken = true;
|
||||||
@@ -452,19 +470,19 @@ public class EntityPlayerActionPack
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
void inactiveTick(ServerPlayerEntity player, Action action)
|
void inactiveTick(MinionFakePlayer player, Action action)
|
||||||
{
|
{
|
||||||
EntityPlayerActionPack ap = ((ServerPlayerInterface) player).getActionPack();
|
EntityPlayerActionPack ap = player.getMinionActionPack();
|
||||||
if (ap.currentBlock == null) return;
|
if (ap.currentBlock == null) return;
|
||||||
player.getWorld().setBlockBreakingInfo(-1, ap.currentBlock, -1);
|
player.getWorld().setBlockBreakingInfo(-1, ap.currentBlock, -1);
|
||||||
player.interactionManager.processBlockBreakingAction(ap.currentBlock, PlayerActionC2SPacket.Action.ABORT_DESTROY_BLOCK, Direction.DOWN, player.getWorld().getTopY(), -1);
|
player.interactionManager.processBlockBreakingAction(ap.currentBlock, PlayerActionC2SPacket.Action.ABORT_DESTROY_BLOCK, Direction.DOWN, player.getWorld().getTopYInclusive(), -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)
|
||||||
{
|
{
|
||||||
@@ -478,7 +496,7 @@ public class EntityPlayerActionPack
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
void inactiveTick(ServerPlayerEntity player, Action action)
|
void inactiveTick(MinionFakePlayer player, Action action)
|
||||||
{
|
{
|
||||||
player.setJumping(false);
|
player.setJumping(false);
|
||||||
}
|
}
|
||||||
@@ -486,7 +504,7 @@ 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.updateLastActionTime();
|
||||||
player.dropSelectedItem(false); // dropSelectedItem
|
player.dropSelectedItem(false); // dropSelectedItem
|
||||||
@@ -496,7 +514,7 @@ public class EntityPlayerActionPack
|
|||||||
DROP_STACK(true)
|
DROP_STACK(true)
|
||||||
{
|
{
|
||||||
@Override
|
@Override
|
||||||
boolean execute(ServerPlayerEntity player, Action action)
|
boolean execute(MinionFakePlayer player, Action action)
|
||||||
{
|
{
|
||||||
player.updateLastActionTime();
|
player.updateLastActionTime();
|
||||||
player.dropSelectedItem(true); // dropSelectedItem
|
player.dropSelectedItem(true); // dropSelectedItem
|
||||||
@@ -506,7 +524,7 @@ public class EntityPlayerActionPack
|
|||||||
SWAP_HANDS(true)
|
SWAP_HANDS(true)
|
||||||
{
|
{
|
||||||
@Override
|
@Override
|
||||||
boolean execute(ServerPlayerEntity player, Action action)
|
boolean execute(MinionFakePlayer player, Action action)
|
||||||
{
|
{
|
||||||
player.updateLastActionTime();
|
player.updateLastActionTime();
|
||||||
ItemStack itemStack_1 = player.getStackInHand(Hand.OFF_HAND);
|
ItemStack itemStack_1 = player.getStackInHand(Hand.OFF_HAND);
|
||||||
@@ -523,10 +541,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);
|
||||||
}
|
}
|
||||||
@@ -541,8 +559,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;
|
||||||
@@ -553,22 +572,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)
|
||||||
@@ -599,6 +623,7 @@ public class EntityPlayerActionPack
|
|||||||
return cancel;
|
return cancel;
|
||||||
}
|
}
|
||||||
next = interval;
|
next = interval;
|
||||||
|
first = false;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
+3
-3
@@ -1,11 +1,11 @@
|
|||||||
package io.github.skippyall.minions.fakeplayer;
|
//code from https://github.com/gnembon/fabric-carpet
|
||||||
|
package io.github.skippyall.minions.minion.fakeplayer;
|
||||||
|
|
||||||
import io.netty.channel.embedded.EmbeddedChannel;
|
import io.netty.channel.embedded.EmbeddedChannel;
|
||||||
import net.minecraft.network.ClientConnection;
|
import net.minecraft.network.ClientConnection;
|
||||||
import net.minecraft.network.NetworkSide;
|
import net.minecraft.network.NetworkSide;
|
||||||
import net.minecraft.network.NetworkState;
|
import net.minecraft.network.state.NetworkState;
|
||||||
import net.minecraft.network.listener.PacketListener;
|
import net.minecraft.network.listener.PacketListener;
|
||||||
import net.minecraft.text.Text;
|
|
||||||
|
|
||||||
public class FakeClientConnection extends ClientConnection {
|
public class FakeClientConnection extends ClientConnection {
|
||||||
public FakeClientConnection(NetworkSide p)
|
public FakeClientConnection(NetworkSide p)
|
||||||
@@ -0,0 +1,293 @@
|
|||||||
|
//partially code from https://github.com/gnembon/fabric-carpet
|
||||||
|
package io.github.skippyall.minions.minion.fakeplayer;
|
||||||
|
|
||||||
|
import com.mojang.authlib.GameProfile;
|
||||||
|
import com.mojang.authlib.properties.PropertyMap;
|
||||||
|
import io.github.skippyall.minions.registration.MinionConfigOptions;
|
||||||
|
import io.github.skippyall.minions.registration.MinionItems;
|
||||||
|
import io.github.skippyall.minions.minion.MinionListener;
|
||||||
|
import io.github.skippyall.minions.minion.MinionData;
|
||||||
|
import io.github.skippyall.minions.gui.MinionGui;
|
||||||
|
import io.github.skippyall.minions.minion.MinionRuntime;
|
||||||
|
import io.github.skippyall.minions.minion.MinionItem;
|
||||||
|
import io.github.skippyall.minions.minion.MinionPersistentState;
|
||||||
|
import io.github.skippyall.minions.minion.MinionProfileUtils;
|
||||||
|
import io.github.skippyall.minions.module.ModuleInventory;
|
||||||
|
import io.github.skippyall.minions.registration.SpecialAbilities;
|
||||||
|
import io.github.skippyall.minions.listener.SerializableListenerManager;
|
||||||
|
import net.minecraft.block.BlockState;
|
||||||
|
import net.minecraft.entity.Entity;
|
||||||
|
import net.minecraft.entity.EquipmentSlot;
|
||||||
|
import net.minecraft.entity.ItemEntity;
|
||||||
|
import net.minecraft.entity.attribute.EntityAttributes;
|
||||||
|
import net.minecraft.entity.damage.DamageSource;
|
||||||
|
import net.minecraft.entity.player.HungerManager;
|
||||||
|
import net.minecraft.entity.player.PlayerEntity;
|
||||||
|
import net.minecraft.item.ItemStack;
|
||||||
|
import net.minecraft.network.DisconnectionInfo;
|
||||||
|
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.ServerTask;
|
||||||
|
import net.minecraft.server.network.ConnectedClientData;
|
||||||
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
|
import net.minecraft.server.world.ServerWorld;
|
||||||
|
import net.minecraft.storage.ReadView;
|
||||||
|
import net.minecraft.storage.WriteView;
|
||||||
|
import net.minecraft.text.Text;
|
||||||
|
import net.minecraft.text.TranslatableTextContent;
|
||||||
|
import net.minecraft.util.ActionResult;
|
||||||
|
import net.minecraft.util.Hand;
|
||||||
|
import net.minecraft.util.math.BlockPos;
|
||||||
|
import net.minecraft.util.math.Vec2f;
|
||||||
|
import net.minecraft.util.math.Vec3d;
|
||||||
|
import net.minecraft.world.GameMode;
|
||||||
|
import net.minecraft.world.TeleportTarget;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
public class MinionFakePlayer extends ServerPlayerEntity {
|
||||||
|
public Runnable fixStartingPosition = () -> {};
|
||||||
|
|
||||||
|
private EntityPlayerActionPack actionPack;
|
||||||
|
|
||||||
|
private final ModuleInventory moduleInventory = new ModuleInventory(this);
|
||||||
|
private final MinionRuntime instructionManager = new MinionRuntime(this);
|
||||||
|
|
||||||
|
public static void spawnMinion(MinionData data, ServerWorld level, @Nullable Vec3d pos, @Nullable Vec2f rot) {
|
||||||
|
spawnMinion(data, level, pos, rot, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void spawnMinion(MinionData data, ServerWorld level, @Nullable Vec3d pos, @Nullable Vec2f rot, boolean force) {
|
||||||
|
if(!data.isSpawned() || force) {
|
||||||
|
MinecraftServer server = level.getServer();
|
||||||
|
|
||||||
|
PropertyMap skin = data.skin().orElse(null);
|
||||||
|
|
||||||
|
GameProfile profile = MinionProfileUtils.makeNewMinionProfile(data.uuid(), data.name(), skin);
|
||||||
|
server.send(server.createTask(() -> doSpawn(data, profile, server, level, pos, rot)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void doSpawn(MinionData data, GameProfile profile, MinecraftServer server, ServerWorld level, @Nullable Vec3d pos, @Nullable Vec2f rot) {
|
||||||
|
MinionFakePlayer instance = new MinionFakePlayer(server, level, profile, SyncedClientOptions.createDefault());
|
||||||
|
MinionPersistentState.get(server).updateMinionData(data.withSpawned(true));
|
||||||
|
|
||||||
|
if(pos != null && rot != null) {
|
||||||
|
instance.fixStartingPosition = () -> instance.refreshPositionAndAngles(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));
|
||||||
|
System.out.println(instance.getPos());
|
||||||
|
if(pos != null && rot != null) {
|
||||||
|
instance.teleport(level, pos.x, pos.y, pos.z, Set.of(), rot.x, rot.y, true);
|
||||||
|
}
|
||||||
|
instance.setVelocity(0,0,0);
|
||||||
|
instance.setHealth(20.0F);
|
||||||
|
instance.unsetRemoved();
|
||||||
|
instance.getAttributeInstance(EntityAttributes.STEP_HEIGHT).setBaseValue(0.6F);
|
||||||
|
instance.interactionManager.changeGameMode(GameMode.SURVIVAL);
|
||||||
|
server.getPlayerManager().sendToDimension(new EntitySetHeadYawS2CPacket(instance, (byte) (instance.headYaw * 256 / 360)), level.getRegistryKey());
|
||||||
|
server.getPlayerManager().sendToDimension(EntityPositionSyncS2CPacket.create(instance), level.getRegistryKey());
|
||||||
|
instance.getWorld().getChunkManager().updatePosition(instance);
|
||||||
|
instance.dataTracker.set(PLAYER_MODEL_PARTS, (byte) 0x7f); // show all model layers (incl. capes)
|
||||||
|
instance.getAbilities().flying = false;
|
||||||
|
|
||||||
|
instance.listeners().forEach(listener -> listener.onMinionSpawn(instance));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static MinionFakePlayer respawnFake(MinecraftServer server, ServerWorld level, GameProfile profile, SyncedClientOptions cli)
|
||||||
|
{
|
||||||
|
return new MinionFakePlayer(server, level, profile, cli);
|
||||||
|
}
|
||||||
|
|
||||||
|
private MinionFakePlayer(MinecraftServer server, ServerWorld worldIn, GameProfile profile, SyncedClientOptions cli)
|
||||||
|
{
|
||||||
|
super(server, worldIn, profile, cli);
|
||||||
|
actionPack = new EntityPlayerActionPack(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ModuleInventory getModuleInventory() {
|
||||||
|
return moduleInventory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public EntityPlayerActionPack getMinionActionPack() {
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ActionResult interact(PlayerEntity player, Hand hand) {
|
||||||
|
if(player instanceof ServerPlayerEntity spe) {
|
||||||
|
MinionGui.openInventory(spe, this);
|
||||||
|
}
|
||||||
|
return ActionResult.CONSUME;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ActionResult interactAt(PlayerEntity player, Vec3d hitPos, Hand hand) {
|
||||||
|
return interact(player, hand);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onEquipStack(final EquipmentSlot slot, final ItemStack previous, final ItemStack stack)
|
||||||
|
{
|
||||||
|
if (!isUsingItem()) super.onEquipStack(slot, previous, stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void kill(Text reason)
|
||||||
|
{
|
||||||
|
listeners().forEach(listener -> listener.onMinionRemove(this));
|
||||||
|
|
||||||
|
shakeOff();
|
||||||
|
|
||||||
|
if (reason.getContent() instanceof TranslatableTextContent text && text.getKey().equals("multiplayer.disconnect.duplicate_login")) {
|
||||||
|
this.networkHandler.onDisconnected(new DisconnectionInfo(reason));
|
||||||
|
} else {
|
||||||
|
this.getServer().send(new ServerTask(this.getServer().getTicks(), () -> {
|
||||||
|
this.networkHandler.onDisconnected(new DisconnectionInfo(reason));
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
MinionPersistentState.get(getServer()).updateMinionData(getData().withSpawned(false));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void tick()
|
||||||
|
{
|
||||||
|
actionPack.onUpdate();
|
||||||
|
if (this.getServer().getTicks() % 10 == 0)
|
||||||
|
{
|
||||||
|
this.networkHandler.syncWithPlayerPosition();
|
||||||
|
this.getWorld().getChunkManager().updatePosition(this);
|
||||||
|
}
|
||||||
|
try
|
||||||
|
{
|
||||||
|
super.tick();
|
||||||
|
this.playerTick();
|
||||||
|
instructionManager.tick();
|
||||||
|
}
|
||||||
|
catch (NullPointerException ignored)
|
||||||
|
{
|
||||||
|
// happens with that paper port thingy - not sure what that would fix, but hey
|
||||||
|
// the game not gonna crash violently.
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void shakeOff()
|
||||||
|
{
|
||||||
|
if (getVehicle() instanceof PlayerEntity) stopRiding();
|
||||||
|
for (Entity passenger : getPassengersDeep())
|
||||||
|
{
|
||||||
|
if (passenger instanceof PlayerEntity) passenger.stopRiding();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDeath(DamageSource cause)
|
||||||
|
{
|
||||||
|
shakeOff();
|
||||||
|
super.onDeath(cause);
|
||||||
|
setHealth(20);
|
||||||
|
this.hungerManager = new HungerManager();
|
||||||
|
kill(this.getDamageTracker().getDeathMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getIp()
|
||||||
|
{
|
||||||
|
return "127.0.0.1";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean allowsServerListing() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void fall(double y, boolean onGround, BlockState state, BlockPos pos) {
|
||||||
|
handleFall(0.0, y, 0.0, onGround);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ServerPlayerEntity teleportTo(TeleportTarget target)
|
||||||
|
{
|
||||||
|
super.teleportTo(target);
|
||||||
|
if (notInAnyWorld) {
|
||||||
|
ClientStatusC2SPacket p = new ClientStatusC2SPacket(ClientStatusC2SPacket.Mode.PERFORM_RESPAWN);
|
||||||
|
networkHandler.onClientStatus(p);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If above branch was taken, *this* has been removed and replaced, the new instance has been set
|
||||||
|
// on 'our' connection (which is now theirs, but we still have a ref).
|
||||||
|
if (networkHandler.player.isInTeleportationState()) {
|
||||||
|
networkHandler.player.onTeleportationDone();
|
||||||
|
}
|
||||||
|
return networkHandler.player;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void drop(ServerWorld world, DamageSource damageSource) {
|
||||||
|
super.drop(world, damageSource);
|
||||||
|
ItemEntity entity = dropStack(world, toItemStack(getServer()));
|
||||||
|
if (entity != null) {
|
||||||
|
entity.setNeverDespawn();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private ItemStack toItemStack(MinecraftServer server) {
|
||||||
|
ItemStack stack = new ItemStack(MinionItems.MINION_ITEM);
|
||||||
|
MinionItem.setData(server, getData(), stack);
|
||||||
|
return stack;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void writeCustomData(WriteView view) {
|
||||||
|
super.writeCustomData(view);
|
||||||
|
moduleInventory.writeData(view.get("modules"));
|
||||||
|
instructionManager.save(view.get("instructionManager"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void readCustomData(ReadView view) {
|
||||||
|
super.readCustomData(view);
|
||||||
|
moduleInventory.readData(view.getReadView("modules"));
|
||||||
|
instructionManager.load(view.getReadView("instructionManager"));
|
||||||
|
}
|
||||||
|
}
|
||||||
+7
-5
@@ -1,5 +1,7 @@
|
|||||||
package io.github.skippyall.minions.fakeplayer;
|
//code from https://github.com/gnembon/fabric-carpet
|
||||||
|
package io.github.skippyall.minions.minion.fakeplayer;
|
||||||
|
|
||||||
|
import net.minecraft.entity.player.PlayerPosition;
|
||||||
import net.minecraft.network.ClientConnection;
|
import net.minecraft.network.ClientConnection;
|
||||||
import net.minecraft.network.packet.Packet;
|
import net.minecraft.network.packet.Packet;
|
||||||
import net.minecraft.network.packet.s2c.play.PositionFlag;
|
import net.minecraft.network.packet.s2c.play.PositionFlag;
|
||||||
@@ -33,12 +35,12 @@ public class NetHandlerPlayServerFake extends ServerPlayNetworkHandler
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void requestTeleport(double d, double e, double f, float g, float h, Set<PositionFlag> set)
|
public void requestTeleport(PlayerPosition pos, Set<PositionFlag> set)
|
||||||
{
|
{
|
||||||
super.requestTeleport(d, e, f, g, h, set);
|
super.requestTeleport(pos, set);
|
||||||
if (player.getServerWorld().getPlayerByUuid(player.getUuid()) != null) {
|
if (player.getWorld().getPlayerByUuid(player.getUuid()) != null) {
|
||||||
syncWithPlayerPosition();
|
syncWithPlayerPosition();
|
||||||
player.getServerWorld().getChunkManager().updatePosition(player);
|
player.getWorld().getChunkManager().updatePosition(player);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
+2
-1
@@ -1,4 +1,5 @@
|
|||||||
package io.github.skippyall.minions.fakeplayer;
|
//code from https://github.com/gnembon/fabric-carpet
|
||||||
|
package io.github.skippyall.minions.minion.fakeplayer;
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
@@ -0,0 +1,107 @@
|
|||||||
|
package io.github.skippyall.minions.minion.skin;
|
||||||
|
|
||||||
|
import com.mojang.authlib.properties.Property;
|
||||||
|
import com.mojang.authlib.properties.PropertyMap;
|
||||||
|
import io.github.skippyall.minions.Minions;
|
||||||
|
import net.minecraft.dialog.AfterAction;
|
||||||
|
import net.minecraft.dialog.DialogActionButtonData;
|
||||||
|
import net.minecraft.dialog.DialogButtonData;
|
||||||
|
import net.minecraft.dialog.DialogCommonData;
|
||||||
|
import net.minecraft.dialog.action.DynamicCustomDialogAction;
|
||||||
|
import net.minecraft.dialog.input.TextInputControl;
|
||||||
|
import net.minecraft.dialog.type.Dialog;
|
||||||
|
import net.minecraft.dialog.type.DialogInput;
|
||||||
|
import net.minecraft.dialog.type.NoticeDialog;
|
||||||
|
import net.minecraft.nbt.NbtCompound;
|
||||||
|
import net.minecraft.nbt.NbtElement;
|
||||||
|
import net.minecraft.registry.RegistryKey;
|
||||||
|
import net.minecraft.registry.RegistryKeys;
|
||||||
|
import net.minecraft.registry.entry.RegistryEntry;
|
||||||
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
|
import net.minecraft.text.Text;
|
||||||
|
import net.minecraft.util.Identifier;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
|
public class Base64SkinProvider implements SkinProvider {
|
||||||
|
public static final RegistryKey<Dialog> DIALOG = RegistryKey.of(RegistryKeys.DIALOG, Identifier.of(Minions.MOD_ID, "base_64_input"));
|
||||||
|
public static final Identifier CUSTOM_DIALOG_ACTION = Identifier.of(Minions.MOD_ID, "base_64_submit");
|
||||||
|
|
||||||
|
private static long dialogIdCounter = 0;
|
||||||
|
private static Map<Long, CompletableFuture<Optional<PropertyMap>>> futures = new HashMap<>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CompletableFuture<Optional<PropertyMap>> openSkinMenu(ServerPlayerEntity player) {
|
||||||
|
dialogIdCounter++;
|
||||||
|
player.openDialog(getDialog());
|
||||||
|
CompletableFuture<Optional<PropertyMap>> future = new CompletableFuture<>();
|
||||||
|
futures.put(dialogIdCounter, future);
|
||||||
|
return future;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void onCustomDialogAction(Optional<NbtElement> element) {
|
||||||
|
if(element.isPresent() && element.get() instanceof NbtCompound 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();
|
||||||
|
map.put("textures", new Property("textures", base64.get().strip()));
|
||||||
|
|
||||||
|
futures.get(id.get()).complete(Optional.of(map));
|
||||||
|
futures.remove(id.get());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static RegistryEntry<Dialog> getDialog() {
|
||||||
|
NbtCompound additionalData = new NbtCompound();
|
||||||
|
additionalData.putLong("dialog_id", dialogIdCounter);
|
||||||
|
return RegistryEntry.of(
|
||||||
|
new NoticeDialog(
|
||||||
|
new DialogCommonData(
|
||||||
|
Text.translatable("minions.gui.look.skin.base64.title"),
|
||||||
|
Optional.empty(),
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
AfterAction.CLOSE,
|
||||||
|
List.of(),
|
||||||
|
List.of(
|
||||||
|
new DialogInput("base_64", new TextInputControl(
|
||||||
|
200,
|
||||||
|
Text.empty(),
|
||||||
|
false,
|
||||||
|
"",
|
||||||
|
2000,
|
||||||
|
Optional.empty()
|
||||||
|
))
|
||||||
|
)
|
||||||
|
),
|
||||||
|
new DialogActionButtonData(
|
||||||
|
new DialogButtonData(
|
||||||
|
Text.translatable("gui.ok"),
|
||||||
|
150
|
||||||
|
),
|
||||||
|
Optional.of(
|
||||||
|
new DynamicCustomDialogAction(
|
||||||
|
CUSTOM_DIALOG_ACTION,
|
||||||
|
Optional.of(
|
||||||
|
additionalData
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Text getDisplayName() {
|
||||||
|
return Text.translatable("minions.gui.look.skin.base64");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package io.github.skippyall.minions.minion.skin;
|
||||||
|
|
||||||
|
import com.mojang.authlib.GameProfile;
|
||||||
|
import com.mojang.authlib.properties.PropertyMap;
|
||||||
|
import io.github.skippyall.minions.gui.input.TextInput;
|
||||||
|
import net.minecraft.block.entity.SkullBlockEntity;
|
||||||
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
|
import net.minecraft.text.Text;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
|
public class NameSkinProvider implements SkinProvider {
|
||||||
|
@Override
|
||||||
|
public CompletableFuture<Optional<PropertyMap>> openSkinMenu(ServerPlayerEntity player) {
|
||||||
|
return TextInput.inputString(player, Text.translatable("minions.gui.look.skin.name.title"), "")
|
||||||
|
.thenCompose(SkullBlockEntity::fetchProfileByName)
|
||||||
|
.thenApply(gameProfile -> gameProfile.map(GameProfile::getProperties));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Text getDisplayName() {
|
||||||
|
return Text.translatable("minions.gui.look.skin.name");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package io.github.skippyall.minions.minion.skin;
|
||||||
|
|
||||||
|
import com.mojang.authlib.properties.PropertyMap;
|
||||||
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
|
import net.minecraft.text.Text;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
|
public interface SkinProvider {
|
||||||
|
CompletableFuture<Optional<PropertyMap>> openSkinMenu(ServerPlayerEntity player);
|
||||||
|
|
||||||
|
Text getDisplayName();
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package io.github.skippyall.minions.minion.skin;
|
||||||
|
|
||||||
|
import com.mojang.authlib.GameProfile;
|
||||||
|
import com.mojang.authlib.properties.PropertyMap;
|
||||||
|
import io.github.skippyall.minions.gui.input.TextInput;
|
||||||
|
import net.minecraft.block.entity.SkullBlockEntity;
|
||||||
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
|
import net.minecraft.text.Text;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
|
public class UUIDSkinProvider implements SkinProvider {
|
||||||
|
@Override
|
||||||
|
public CompletableFuture<Optional<PropertyMap>> openSkinMenu(ServerPlayerEntity player) {
|
||||||
|
return TextInput.inputString(player, Text.translatable("minions.gui.look.skin.uuid.title"), "")
|
||||||
|
.thenCompose(uuidString -> SkullBlockEntity.fetchProfileByUuid(UUID.fromString(uuidString)))
|
||||||
|
.thenApply(gameProfile -> gameProfile.map(GameProfile::getProperties));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Text getDisplayName() {
|
||||||
|
return Text.translatable("minions.gui.look.skin.uuid");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package io.github.skippyall.minions.mixinhelper;
|
||||||
|
|
||||||
|
import net.minecraft.entity.Entity;
|
||||||
|
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
|
public class EntityViewMixinHelper {
|
||||||
|
public static final ThreadLocal<Predicate<Entity>> ADDITIONAL_PREDICATE = ThreadLocal.withInitial(() -> entity -> true);
|
||||||
|
}
|
||||||
+9
@@ -0,0 +1,9 @@
|
|||||||
|
package io.github.skippyall.minions.mixinhelper.antimobcap;
|
||||||
|
|
||||||
|
public interface ChunkLevelManager$DistanceFromNearestPlayerTrackerAccessor {
|
||||||
|
void minions$markAsMinionless();
|
||||||
|
void minions$markAsTarget();
|
||||||
|
int minions$getTickedChunkCount();
|
||||||
|
|
||||||
|
boolean minions$isRealPlayerInChunk(long chunkPos);
|
||||||
|
}
|
||||||
+11
@@ -0,0 +1,11 @@
|
|||||||
|
package io.github.skippyall.minions.mixinhelper.antimobcap;
|
||||||
|
|
||||||
|
import it.unimi.dsi.fastutil.objects.ObjectSet;
|
||||||
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
|
import net.minecraft.server.world.ChunkLevelManager;
|
||||||
|
|
||||||
|
public interface ChunkLevelManagerAccessor {
|
||||||
|
ObjectSet<ServerPlayerEntity> minions$getPlayers(long chunkpos);
|
||||||
|
|
||||||
|
ChunkLevelManager.DistanceFromNearestPlayerTracker minions$getMinionless();
|
||||||
|
}
|
||||||
@@ -3,18 +3,27 @@ package io.github.skippyall.minions.mixins;
|
|||||||
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
|
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
|
||||||
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
|
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
|
||||||
import it.unimi.dsi.fastutil.objects.ObjectSet;
|
import it.unimi.dsi.fastutil.objects.ObjectSet;
|
||||||
import net.minecraft.server.world.ChunkTicketManager;
|
import net.minecraft.server.world.ChunkLevelManager;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.spongepowered.asm.mixin.Final;
|
||||||
import org.spongepowered.asm.mixin.Mixin;
|
import org.spongepowered.asm.mixin.Mixin;
|
||||||
|
import org.spongepowered.asm.mixin.Shadow;
|
||||||
import org.spongepowered.asm.mixin.injection.At;
|
import org.spongepowered.asm.mixin.injection.At;
|
||||||
|
|
||||||
@Mixin(value = ChunkTicketManager.class)
|
@Mixin(value = ChunkLevelManager.class)
|
||||||
public class ChunkTicketManagerFixMixin {
|
public class ChunkTicketManagerFixMixin {
|
||||||
|
@Shadow
|
||||||
|
@Final
|
||||||
|
private static Logger LOGGER;
|
||||||
|
|
||||||
@WrapOperation(method = "handleChunkLeave", at = @At(value = "INVOKE", target = "Lit/unimi/dsi/fastutil/objects/ObjectSet;remove(Ljava/lang/Object;)Z", remap = false))
|
@WrapOperation(method = "handleChunkLeave", at = @At(value = "INVOKE", target = "Lit/unimi/dsi/fastutil/objects/ObjectSet;remove(Ljava/lang/Object;)Z", remap = false))
|
||||||
public boolean filterIfNull(ObjectSet instance, Object o, Operation<Boolean> original) {
|
public boolean filterIfNull(ObjectSet instance, Object o, Operation<Boolean> original) {
|
||||||
if (instance != null) {
|
if (instance != null) {
|
||||||
return original.call(instance, o);
|
return original.call(instance, o);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LOGGER.error("Prevented NPE in handleChunkLeave");
|
||||||
|
|
||||||
return false;//Unused
|
return false;//Unused
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -24,6 +33,8 @@ public class ChunkTicketManagerFixMixin {
|
|||||||
return original.call(instance);
|
return original.call(instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LOGGER.error("Prevented NPE in handleChunkLeave");
|
||||||
|
|
||||||
return true;//Unused
|
return true;//Unused
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
|
//code from https://github.com/gnembon/fabric-carpet
|
||||||
package io.github.skippyall.minions.mixins;
|
package io.github.skippyall.minions.mixins;
|
||||||
|
|
||||||
import io.github.skippyall.minions.fakeplayer.ClientConnectionInterface;
|
import io.github.skippyall.minions.minion.fakeplayer.ClientConnectionInterface;
|
||||||
import io.netty.channel.Channel;
|
import io.netty.channel.Channel;
|
||||||
import net.minecraft.network.ClientConnection;
|
import net.minecraft.network.ClientConnection;
|
||||||
import org.spongepowered.asm.mixin.Mixin;
|
import org.spongepowered.asm.mixin.Mixin;
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package io.github.skippyall.minions.mixins;
|
||||||
|
|
||||||
|
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
|
||||||
|
import net.minecraft.entity.Entity;
|
||||||
|
import net.minecraft.entity.LivingEntity;
|
||||||
|
import net.minecraft.world.World;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import org.spongepowered.asm.mixin.Mixin;
|
||||||
|
import org.spongepowered.asm.mixin.Shadow;
|
||||||
|
import org.spongepowered.asm.mixin.injection.At;
|
||||||
|
import org.spongepowered.asm.mixin.injection.Inject;
|
||||||
|
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
||||||
|
|
||||||
|
@Mixin(Entity.class)
|
||||||
|
public abstract class EntityMixin {
|
||||||
|
@Shadow
|
||||||
|
public abstract @Nullable LivingEntity getControllingPassenger();
|
||||||
|
|
||||||
|
@Shadow
|
||||||
|
private World world;
|
||||||
|
|
||||||
|
@Inject(method = "isLogicalSideForUpdatingMovement", at = @At("HEAD"), cancellable = true)
|
||||||
|
private void isFakePlayer(CallbackInfoReturnable<Boolean> cir)
|
||||||
|
{
|
||||||
|
if (getControllingPassenger() instanceof MinionFakePlayer) cir.setReturnValue(!world.isClient);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package io.github.skippyall.minions.mixins;
|
||||||
|
|
||||||
|
import io.github.skippyall.minions.mixinhelper.EntityViewMixinHelper;
|
||||||
|
import net.minecraft.entity.Entity;
|
||||||
|
import net.minecraft.world.EntityView;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import org.spongepowered.asm.mixin.Mixin;
|
||||||
|
import org.spongepowered.asm.mixin.injection.At;
|
||||||
|
import org.spongepowered.asm.mixin.injection.ModifyArg;
|
||||||
|
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
|
@Mixin(EntityView.class)
|
||||||
|
public interface EntityViewMixin {
|
||||||
|
@ModifyArg(method = "getClosestPlayer(DDDDZ)Lnet/minecraft/entity/player/PlayerEntity;", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/EntityView;getClosestPlayer(DDDDLjava/util/function/Predicate;)Lnet/minecraft/entity/player/PlayerEntity;"))
|
||||||
|
private @Nullable Predicate<Entity> addMinionPredicate(@Nullable Predicate<Entity> targetPredicate) {
|
||||||
|
Predicate<Entity> predicate = EntityViewMixinHelper.ADDITIONAL_PREDICATE.get();
|
||||||
|
if(targetPredicate != null) {
|
||||||
|
return predicate.and(targetPredicate);
|
||||||
|
} else {
|
||||||
|
return predicate;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,22 +1,39 @@
|
|||||||
package io.github.skippyall.minions.mixins;
|
package io.github.skippyall.minions.mixins;
|
||||||
|
|
||||||
import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
|
import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
|
||||||
import io.github.skippyall.minions.fakeplayer.MinionFakePlayer;
|
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
|
||||||
|
import io.github.skippyall.minions.minion.skin.Base64SkinProvider;
|
||||||
|
import io.github.skippyall.minions.registration.MinionConfigOptions;
|
||||||
|
import net.minecraft.nbt.NbtElement;
|
||||||
import net.minecraft.server.MinecraftServer;
|
import net.minecraft.server.MinecraftServer;
|
||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
|
import net.minecraft.util.Identifier;
|
||||||
import org.spongepowered.asm.mixin.Mixin;
|
import org.spongepowered.asm.mixin.Mixin;
|
||||||
import org.spongepowered.asm.mixin.injection.At;
|
import org.spongepowered.asm.mixin.injection.At;
|
||||||
|
import org.spongepowered.asm.mixin.injection.Inject;
|
||||||
|
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Mixin(MinecraftServer.class)
|
@Mixin(MinecraftServer.class)
|
||||||
public class MinecraftServerMixin {
|
public class MinecraftServerMixin {
|
||||||
|
|
||||||
@ModifyExpressionValue(method = "createMetadataPlayers", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/PlayerManager;getPlayerList()Ljava/util/List;"))
|
@ModifyExpressionValue(method = "createMetadataPlayers", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/PlayerManager;getPlayerList()Ljava/util/List;"))
|
||||||
public List<ServerPlayerEntity> ignoreFakePlayers(List<ServerPlayerEntity> original) {
|
public List<ServerPlayerEntity> ignoreFakePlayers(List<ServerPlayerEntity> original) {
|
||||||
return original.stream()
|
return original.stream()
|
||||||
.filter(player -> !(player instanceof MinionFakePlayer))
|
.filter(player -> !(player instanceof MinionFakePlayer minion
|
||||||
|
&& !minion.getData().config().getOption(MinionConfigOptions.showInServerList)))
|
||||||
.collect(Collectors.toCollection(ArrayList::new));
|
.collect(Collectors.toCollection(ArrayList::new));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Inject(method = "handleCustomClickAction", at = @At("HEAD"), cancellable = true)
|
||||||
|
private void onCustomClickAction(Identifier id, Optional<NbtElement> payload, CallbackInfo ci) {
|
||||||
|
if(id.equals(Base64SkinProvider.CUSTOM_DIALOG_ACTION)) {
|
||||||
|
Base64SkinProvider.onCustomDialogAction(payload);
|
||||||
|
ci.cancel();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,29 +1,29 @@
|
|||||||
package io.github.skippyall.minions.mixins;
|
package io.github.skippyall.minions.mixins;
|
||||||
|
|
||||||
import io.github.skippyall.minions.fakeplayer.MinionFakePlayer;
|
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
|
||||||
import io.github.skippyall.minions.module.MobSpawningModule;
|
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
|
||||||
|
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
|
||||||
|
import io.github.skippyall.minions.mixinhelper.EntityViewMixinHelper;
|
||||||
import net.minecraft.entity.Entity;
|
import net.minecraft.entity.Entity;
|
||||||
import net.minecraft.entity.mob.MobEntity;
|
import net.minecraft.entity.mob.MobEntity;
|
||||||
import net.minecraft.entity.player.PlayerEntity;
|
import net.minecraft.entity.player.PlayerEntity;
|
||||||
import net.minecraft.predicate.entity.EntityPredicates;
|
|
||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
import org.spongepowered.asm.mixin.Mixin;
|
import org.spongepowered.asm.mixin.Mixin;
|
||||||
import org.spongepowered.asm.mixin.injection.At;
|
import org.spongepowered.asm.mixin.injection.At;
|
||||||
import org.spongepowered.asm.mixin.injection.Redirect;
|
|
||||||
|
|
||||||
@Mixin(MobEntity.class)
|
@Mixin(MobEntity.class)
|
||||||
public abstract class MobEntityMixin {
|
public abstract class MobEntityMixin {
|
||||||
@Redirect(method = "checkDespawn", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;getClosestPlayer(Lnet/minecraft/entity/Entity;D)Lnet/minecraft/entity/player/PlayerEntity;"))
|
@WrapOperation(method = "checkDespawn", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;getClosestPlayer(Lnet/minecraft/entity/Entity;D)Lnet/minecraft/entity/player/PlayerEntity;"))
|
||||||
public PlayerEntity checkMobDespawningMinion(World instance, Entity entity, double maxDistance) {
|
public PlayerEntity checkMobDespawningMinion(World instance, Entity entity, double maxDistance, Operation<PlayerEntity> original) {
|
||||||
return instance.getClosestPlayer(entity.getX(), entity.getY(), entity.getZ(), maxDistance, EntityPredicates.EXCEPT_SPECTATOR.and(entity1 -> {
|
EntityViewMixinHelper.ADDITIONAL_PREDICATE.set(entity2 -> {
|
||||||
if(entity1 instanceof ServerPlayerEntity player) {
|
if(entity2 instanceof MinionFakePlayer minion) {
|
||||||
if(player instanceof MinionFakePlayer minion) {
|
return minion.canDespawnMobs();
|
||||||
return MobSpawningModule.canMinionDespawnMobs(minion);
|
} else {
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
});
|
||||||
}));
|
PlayerEntity player = original.call(instance, entity, maxDistance);
|
||||||
|
EntityViewMixinHelper.ADDITIONAL_PREDICATE.remove();
|
||||||
|
return player;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-3
@@ -1,7 +1,8 @@
|
|||||||
package io.github.skippyall.minions.mixins;
|
package io.github.skippyall.minions.mixins;
|
||||||
|
|
||||||
import com.llamalad7.mixinextras.sugar.Local;
|
import com.llamalad7.mixinextras.sugar.Local;
|
||||||
import io.github.skippyall.minions.fakeplayer.MinionFakePlayer;
|
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
|
||||||
|
import io.github.skippyall.minions.registration.MinionConfigOptions;
|
||||||
import net.minecraft.network.packet.s2c.play.PlayerListS2CPacket;
|
import net.minecraft.network.packet.s2c.play.PlayerListS2CPacket;
|
||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
import org.spongepowered.asm.mixin.Mixin;
|
import org.spongepowered.asm.mixin.Mixin;
|
||||||
@@ -10,9 +11,9 @@ import org.spongepowered.asm.mixin.injection.ModifyArg;
|
|||||||
|
|
||||||
@Mixin(PlayerListS2CPacket.Entry.class)
|
@Mixin(PlayerListS2CPacket.Entry.class)
|
||||||
public class PlayerListEntryS2CPacket$EntryMixin {
|
public class PlayerListEntryS2CPacket$EntryMixin {
|
||||||
@ModifyArg(method = "<init>(Lnet/minecraft/server/network/ServerPlayerEntity;)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/packet/s2c/play/PlayerListS2CPacket$Entry;<init>(Ljava/util/UUID;Lcom/mojang/authlib/GameProfile;ZILnet/minecraft/world/GameMode;Lnet/minecraft/text/Text;Lnet/minecraft/network/encryption/PublicPlayerSession$Serialized;)V"))
|
@ModifyArg(method = "<init>(Lnet/minecraft/server/network/ServerPlayerEntity;)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/packet/s2c/play/PlayerListS2CPacket$Entry;<init>(Ljava/util/UUID;Lcom/mojang/authlib/GameProfile;ZILnet/minecraft/world/GameMode;Lnet/minecraft/text/Text;ZILnet/minecraft/network/encryption/PublicPlayerSession$Serialized;)V"), index = 2)
|
||||||
private static boolean removeMinionFromTabList(boolean original, @Local(argsOnly = true) ServerPlayerEntity player) {
|
private static boolean removeMinionFromTabList(boolean original, @Local(argsOnly = true) ServerPlayerEntity player) {
|
||||||
if(player instanceof MinionFakePlayer) {
|
if(player instanceof MinionFakePlayer minion && !minion.getData().config().getOption(MinionConfigOptions.showInTabList)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
//partially code from https://github.com/gnembon/fabric-carpet
|
||||||
package io.github.skippyall.minions.mixins;
|
package io.github.skippyall.minions.mixins;
|
||||||
|
|
||||||
import com.llamalad7.mixinextras.injector.ModifyReceiver;
|
import com.llamalad7.mixinextras.injector.ModifyReceiver;
|
||||||
@@ -5,9 +6,9 @@ import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
|
|||||||
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
|
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
|
||||||
import com.llamalad7.mixinextras.sugar.Local;
|
import com.llamalad7.mixinextras.sugar.Local;
|
||||||
import com.mojang.authlib.GameProfile;
|
import com.mojang.authlib.GameProfile;
|
||||||
import io.github.skippyall.minions.fakeplayer.MinionFakePlayer;
|
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
|
||||||
import io.github.skippyall.minions.fakeplayer.NetHandlerPlayServerFake;
|
import io.github.skippyall.minions.minion.fakeplayer.NetHandlerPlayServerFake;
|
||||||
import net.minecraft.nbt.NbtCompound;
|
import io.github.skippyall.minions.registration.MinionConfigOptions;
|
||||||
import net.minecraft.network.ClientConnection;
|
import net.minecraft.network.ClientConnection;
|
||||||
import net.minecraft.network.packet.c2s.common.SyncedClientOptions;
|
import net.minecraft.network.packet.c2s.common.SyncedClientOptions;
|
||||||
import net.minecraft.server.MinecraftServer;
|
import net.minecraft.server.MinecraftServer;
|
||||||
@@ -16,7 +17,9 @@ import net.minecraft.server.network.ConnectedClientData;
|
|||||||
import net.minecraft.server.network.ServerPlayNetworkHandler;
|
import net.minecraft.server.network.ServerPlayNetworkHandler;
|
||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
import net.minecraft.server.world.ServerWorld;
|
import net.minecraft.server.world.ServerWorld;
|
||||||
|
import net.minecraft.storage.ReadView;
|
||||||
import net.minecraft.text.Text;
|
import net.minecraft.text.Text;
|
||||||
|
import net.minecraft.util.ErrorReporter;
|
||||||
import org.spongepowered.asm.mixin.Mixin;
|
import org.spongepowered.asm.mixin.Mixin;
|
||||||
import org.spongepowered.asm.mixin.injection.At;
|
import org.spongepowered.asm.mixin.injection.At;
|
||||||
import org.spongepowered.asm.mixin.injection.Inject;
|
import org.spongepowered.asm.mixin.injection.Inject;
|
||||||
@@ -24,17 +27,18 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Mixin(PlayerManager.class)
|
@Mixin(PlayerManager.class)
|
||||||
public class PlayerListMixin {
|
public class PlayerListMixin {
|
||||||
|
|
||||||
@Inject(method = "loadPlayerData", at = @At(value = "RETURN", shift = At.Shift.BEFORE))
|
@Inject(method = "loadPlayerData", at = @At(value = "RETURN", shift = At.Shift.BEFORE))
|
||||||
private void fixStartingPos(ServerPlayerEntity serverPlayerEntity_1, CallbackInfoReturnable<NbtCompound> cir)
|
private void fixStartingPos(ServerPlayerEntity player, ErrorReporter errorReporter, CallbackInfoReturnable<Optional<ReadView>> cir)
|
||||||
{
|
{
|
||||||
if (serverPlayerEntity_1 instanceof MinionFakePlayer)
|
if (player instanceof MinionFakePlayer)
|
||||||
{
|
{
|
||||||
((MinionFakePlayer) serverPlayerEntity_1).fixStartingPosition.run();
|
((MinionFakePlayer) player).fixStartingPosition.run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,7 +62,7 @@ public class PlayerListMixin {
|
|||||||
|
|
||||||
@WrapOperation(method = "onPlayerConnect", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/PlayerManager;broadcast(Lnet/minecraft/text/Text;Z)V"))
|
@WrapOperation(method = "onPlayerConnect", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/PlayerManager;broadcast(Lnet/minecraft/text/Text;Z)V"))
|
||||||
public void noLoginMessage(PlayerManager instance, Text message, boolean overlay, Operation<Void> original, @Local(argsOnly = true) ServerPlayerEntity player) {
|
public void noLoginMessage(PlayerManager instance, Text message, boolean overlay, Operation<Void> original, @Local(argsOnly = true) ServerPlayerEntity player) {
|
||||||
if(!(player instanceof MinionFakePlayer)) {
|
if(!(player instanceof MinionFakePlayer minion && !minion.getData().config().getOption(MinionConfigOptions.sendLoginMessage))) {
|
||||||
original.call(instance, message, overlay);
|
original.call(instance, message, overlay);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -66,7 +70,7 @@ public class PlayerListMixin {
|
|||||||
@ModifyReceiver(method = "checkCanJoin", at = @At(value = "INVOKE", target = "Ljava/util/List;size()I"))
|
@ModifyReceiver(method = "checkCanJoin", at = @At(value = "INVOKE", target = "Ljava/util/List;size()I"))
|
||||||
public List<ServerPlayerEntity> noMinionCounting(List<ServerPlayerEntity> instance) {
|
public List<ServerPlayerEntity> noMinionCounting(List<ServerPlayerEntity> instance) {
|
||||||
return instance.stream()
|
return instance.stream()
|
||||||
.filter(player -> !(player instanceof MinionFakePlayer))
|
.filter(player -> !(player instanceof MinionFakePlayer minion && !minion.getData().config().getOption(MinionConfigOptions.countForPlayerLimit)))
|
||||||
.collect(Collectors.toCollection(ArrayList::new));
|
.collect(Collectors.toCollection(ArrayList::new));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
//code from https://github.com/gnembon/fabric-carpet
|
||||||
|
package io.github.skippyall.minions.mixins;
|
||||||
|
|
||||||
|
import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
|
||||||
|
import com.llamalad7.mixinextras.sugar.Local;
|
||||||
|
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
|
||||||
|
import net.minecraft.entity.Entity;
|
||||||
|
import net.minecraft.entity.player.PlayerEntity;
|
||||||
|
import org.spongepowered.asm.mixin.Mixin;
|
||||||
|
import org.spongepowered.asm.mixin.injection.At;
|
||||||
|
|
||||||
|
@Mixin(PlayerEntity.class)
|
||||||
|
public abstract class PlayerMixin {
|
||||||
|
/**
|
||||||
|
* To make sure player attacks are able to knockback fake players
|
||||||
|
*/
|
||||||
|
@ModifyExpressionValue(
|
||||||
|
method = "attack",
|
||||||
|
at = @At(
|
||||||
|
value = "FIELD",
|
||||||
|
target = "Lnet/minecraft/entity/Entity;velocityModified:Z",
|
||||||
|
ordinal = 0
|
||||||
|
)
|
||||||
|
)
|
||||||
|
private boolean velocityModifiedAndNotCarpetFakePlayer(boolean value, @Local(argsOnly = true) Entity entity) {
|
||||||
|
return value && !(entity instanceof MinionFakePlayer);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,7 +2,8 @@ package io.github.skippyall.minions.mixins;
|
|||||||
|
|
||||||
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
|
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
|
||||||
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
|
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
|
||||||
import io.github.skippyall.minions.fakeplayer.MinionFakePlayer;
|
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
|
||||||
|
import io.github.skippyall.minions.registration.MinionConfigOptions;
|
||||||
import net.minecraft.server.PlayerManager;
|
import net.minecraft.server.PlayerManager;
|
||||||
import net.minecraft.server.network.ServerPlayNetworkHandler;
|
import net.minecraft.server.network.ServerPlayNetworkHandler;
|
||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
@@ -18,7 +19,7 @@ public class ServerPlayNetworkHandlerMixin {
|
|||||||
|
|
||||||
@WrapOperation(method = "cleanUp", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/PlayerManager;broadcast(Lnet/minecraft/text/Text;Z)V"))
|
@WrapOperation(method = "cleanUp", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/PlayerManager;broadcast(Lnet/minecraft/text/Text;Z)V"))
|
||||||
public void noLogoutMessage(PlayerManager instance, Text message, boolean overlay, Operation<Void> original) {
|
public void noLogoutMessage(PlayerManager instance, Text message, boolean overlay, Operation<Void> original) {
|
||||||
if(!(player instanceof MinionFakePlayer)) {
|
if(!(player instanceof MinionFakePlayer minion && !minion.getData().config().getOption(MinionConfigOptions.sendLogoutMessage))) {
|
||||||
original.call(instance, message, overlay);
|
original.call(instance, message, overlay);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,37 +0,0 @@
|
|||||||
package io.github.skippyall.minions.mixins;
|
|
||||||
|
|
||||||
import com.mojang.authlib.GameProfile;
|
|
||||||
import io.github.skippyall.minions.fakeplayer.EntityPlayerActionPack;
|
|
||||||
import io.github.skippyall.minions.fakeplayer.ServerPlayerInterface;
|
|
||||||
import net.minecraft.network.packet.c2s.common.SyncedClientOptions;
|
|
||||||
import net.minecraft.server.MinecraftServer;
|
|
||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
|
||||||
import net.minecraft.server.world.ServerWorld;
|
|
||||||
import org.spongepowered.asm.mixin.Mixin;
|
|
||||||
import org.spongepowered.asm.mixin.Unique;
|
|
||||||
import org.spongepowered.asm.mixin.injection.At;
|
|
||||||
import org.spongepowered.asm.mixin.injection.Inject;
|
|
||||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
|
||||||
|
|
||||||
@Mixin(ServerPlayerEntity.class)
|
|
||||||
public abstract class ServerPlayerMixin implements ServerPlayerInterface {
|
|
||||||
@Unique
|
|
||||||
public EntityPlayerActionPack actionPack;
|
|
||||||
@Override
|
|
||||||
public EntityPlayerActionPack getActionPack()
|
|
||||||
{
|
|
||||||
return actionPack;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Inject(method = "<init>", at = @At(value = "RETURN"))
|
|
||||||
private void onServerPlayerEntityConstructor(MinecraftServer minecraftServer, ServerWorld serverLevel, GameProfile gameProfile, SyncedClientOptions clientInformation, CallbackInfo ci)
|
|
||||||
{
|
|
||||||
this.actionPack = new EntityPlayerActionPack((ServerPlayerEntity) (Object) this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Inject(method = "tick", at = @At(value = "HEAD"))
|
|
||||||
private void onTick(CallbackInfo ci)
|
|
||||||
{
|
|
||||||
actionPack.onUpdate();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -2,7 +2,8 @@ package io.github.skippyall.minions.mixins;
|
|||||||
|
|
||||||
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
|
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
|
||||||
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
|
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
|
||||||
import io.github.skippyall.minions.fakeplayer.MinionFakePlayer;
|
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
|
||||||
|
import io.github.skippyall.minions.registration.MinionConfigOptions;
|
||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
import net.minecraft.server.world.SleepManager;
|
import net.minecraft.server.world.SleepManager;
|
||||||
import org.spongepowered.asm.mixin.Mixin;
|
import org.spongepowered.asm.mixin.Mixin;
|
||||||
@@ -12,7 +13,7 @@ import org.spongepowered.asm.mixin.injection.At;
|
|||||||
public class SleepManagerMixin {
|
public class SleepManagerMixin {
|
||||||
@WrapOperation(method = "update", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerPlayerEntity;isSpectator()Z"))
|
@WrapOperation(method = "update", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerPlayerEntity;isSpectator()Z"))
|
||||||
public boolean excludeMinions(ServerPlayerEntity instance, Operation<Boolean> original) {
|
public boolean excludeMinions(ServerPlayerEntity instance, Operation<Boolean> original) {
|
||||||
if (instance instanceof MinionFakePlayer) {
|
if (instance instanceof MinionFakePlayer minion && !minion.getData().config().getOption(MinionConfigOptions.countForSleeping)) {
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
return original.call(instance);
|
return original.call(instance);
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package io.github.skippyall.minions.mixins;
|
package io.github.skippyall.minions.mixins;
|
||||||
|
|
||||||
import io.github.skippyall.minions.fakeplayer.MinionFakePlayer;
|
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
|
||||||
import io.github.skippyall.minions.module.MobSpawningModule;
|
|
||||||
import net.minecraft.entity.player.PlayerEntity;
|
import net.minecraft.entity.player.PlayerEntity;
|
||||||
import net.minecraft.predicate.entity.EntityPredicates;
|
import net.minecraft.predicate.entity.EntityPredicates;
|
||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
@@ -18,7 +17,7 @@ public class SpawnHelperMixin {
|
|||||||
return instance.getClosestPlayer(x, y, z, maxDistance, EntityPredicates.EXCEPT_SPECTATOR.and(entity -> {
|
return instance.getClosestPlayer(x, y, z, maxDistance, EntityPredicates.EXCEPT_SPECTATOR.and(entity -> {
|
||||||
if(entity instanceof ServerPlayerEntity player) {
|
if(entity instanceof ServerPlayerEntity player) {
|
||||||
if(player instanceof MinionFakePlayer minion) {
|
if(player instanceof MinionFakePlayer minion) {
|
||||||
return MobSpawningModule.canMinionSpawnMobs(minion);
|
return minion.canSpawnMobs();
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
+84
@@ -0,0 +1,84 @@
|
|||||||
|
package io.github.skippyall.minions.mixins.antimobcap;
|
||||||
|
|
||||||
|
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
|
||||||
|
import io.github.skippyall.minions.mixinhelper.antimobcap.ChunkLevelManager$DistanceFromNearestPlayerTrackerAccessor;
|
||||||
|
import io.github.skippyall.minions.mixinhelper.antimobcap.ChunkLevelManagerAccessor;
|
||||||
|
import it.unimi.dsi.fastutil.longs.Long2ByteMap;
|
||||||
|
import it.unimi.dsi.fastutil.objects.ObjectSet;
|
||||||
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
|
import net.minecraft.server.world.ChunkLevelManager;
|
||||||
|
import org.spongepowered.asm.mixin.Final;
|
||||||
|
import org.spongepowered.asm.mixin.Mixin;
|
||||||
|
import org.spongepowered.asm.mixin.Shadow;
|
||||||
|
import org.spongepowered.asm.mixin.Unique;
|
||||||
|
import org.spongepowered.asm.mixin.injection.At;
|
||||||
|
import org.spongepowered.asm.mixin.injection.Inject;
|
||||||
|
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||||
|
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
||||||
|
|
||||||
|
@Mixin(value = ChunkLevelManager.DistanceFromNearestPlayerTracker.class)
|
||||||
|
public abstract class ChunkLevelManager$DistanceFromNearestPlayerTrackerMixin extends ChunkPosDistanceLevelPropagatorMixin implements ChunkLevelManager$DistanceFromNearestPlayerTrackerAccessor {
|
||||||
|
@Final
|
||||||
|
@Shadow
|
||||||
|
ChunkLevelManager field_17462;
|
||||||
|
|
||||||
|
@Shadow
|
||||||
|
@Final
|
||||||
|
protected Long2ByteMap distanceFromNearestPlayer;
|
||||||
|
|
||||||
|
@Shadow protected abstract boolean isPlayerInChunk(long chunkPos);
|
||||||
|
|
||||||
|
@Unique
|
||||||
|
boolean minions$minionless, minions$target;
|
||||||
|
|
||||||
|
@Inject(method = "isPlayerInChunk", at = @At("RETURN"), cancellable = true)
|
||||||
|
public void minions$filterMinions(long chunkPos, CallbackInfoReturnable<Boolean> cir) {
|
||||||
|
if (minions$minionless) {
|
||||||
|
cir.setReturnValue(minions$isRealPlayerInChunk(chunkPos));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean minions$isRealPlayerInChunk(long chunkPos) {
|
||||||
|
ObjectSet<ServerPlayerEntity> players = ((ChunkLevelManagerAccessor)field_17462).minions$getPlayers(chunkPos);
|
||||||
|
boolean contains = false;
|
||||||
|
if(players != null) {
|
||||||
|
contains = players.stream().anyMatch(player -> {
|
||||||
|
if (player instanceof MinionFakePlayer minion) {
|
||||||
|
return minion.canSpawnMobs();
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return contains;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Inject(method = "updateLevels", at = @At("HEAD"))
|
||||||
|
public void minions$sync(CallbackInfo info) {
|
||||||
|
if (minions$target) {
|
||||||
|
((ChunkLevelManagerAccessor)field_17462).minions$getMinionless().updateLevels();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void minions$updateLevel(long chunkPos, int distance, boolean decrease, CallbackInfo info) {
|
||||||
|
if (minions$target && (distance == Integer.MAX_VALUE || minions$isRealPlayerInChunk(chunkPos))) {
|
||||||
|
((ChunkLevelManagerAccessor)field_17462).minions$getMinionless().updateLevel(chunkPos, distance, decrease);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void minions$markAsMinionless() {
|
||||||
|
minions$minionless = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void minions$markAsTarget() {
|
||||||
|
minions$target = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int minions$getTickedChunkCount() {
|
||||||
|
return distanceFromNearestPlayer.size();
|
||||||
|
}
|
||||||
|
}
|
||||||
+52
@@ -0,0 +1,52 @@
|
|||||||
|
package io.github.skippyall.minions.mixins.antimobcap;
|
||||||
|
|
||||||
|
import com.llamalad7.mixinextras.sugar.Local;
|
||||||
|
import io.github.skippyall.minions.mixinhelper.antimobcap.ChunkLevelManager$DistanceFromNearestPlayerTrackerAccessor;
|
||||||
|
import io.github.skippyall.minions.mixinhelper.antimobcap.ChunkLevelManagerAccessor;
|
||||||
|
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
|
||||||
|
import it.unimi.dsi.fastutil.objects.ObjectSet;
|
||||||
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
|
import net.minecraft.server.world.ChunkLevelManager;
|
||||||
|
import net.minecraft.server.world.ChunkTicketManager;
|
||||||
|
import net.minecraft.util.math.ChunkSectionPos;
|
||||||
|
import org.spongepowered.asm.mixin.Final;
|
||||||
|
import org.spongepowered.asm.mixin.Mixin;
|
||||||
|
import org.spongepowered.asm.mixin.Shadow;
|
||||||
|
import org.spongepowered.asm.mixin.Unique;
|
||||||
|
import org.spongepowered.asm.mixin.injection.At;
|
||||||
|
import org.spongepowered.asm.mixin.injection.Inject;
|
||||||
|
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||||
|
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
|
@Mixin(ChunkLevelManager.class)
|
||||||
|
public class ChunkLevelManagerMixin implements ChunkLevelManagerAccessor {
|
||||||
|
@Shadow @Final Long2ObjectMap<ObjectSet<ServerPlayerEntity>> playersByChunkPos;
|
||||||
|
@Shadow @Final private ChunkLevelManager.DistanceFromNearestPlayerTracker distanceFromNearestPlayerTracker;
|
||||||
|
@Unique
|
||||||
|
ChunkLevelManager.DistanceFromNearestPlayerTracker minionless;
|
||||||
|
|
||||||
|
@Inject(method = "<init>", at = @At("RETURN"))
|
||||||
|
public void createMinionlessClone(ChunkTicketManager ticketManager, Executor executor, Executor mainThreadExecutor, CallbackInfo ci) {
|
||||||
|
ChunkLevelManager manager = ((ChunkLevelManager)(Object)this);
|
||||||
|
minionless = manager.new DistanceFromNearestPlayerTracker(8);
|
||||||
|
((ChunkLevelManager$DistanceFromNearestPlayerTrackerAccessor)minionless).minions$markAsMinionless();
|
||||||
|
((ChunkLevelManager$DistanceFromNearestPlayerTrackerAccessor)distanceFromNearestPlayerTracker).minions$markAsTarget();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ObjectSet<ServerPlayerEntity> minions$getPlayers(long chunkpos) {
|
||||||
|
return playersByChunkPos.get(chunkpos);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ChunkLevelManager.DistanceFromNearestPlayerTracker minions$getMinionless() {
|
||||||
|
return minionless;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Inject(method = "handleChunkLeave", at = @At(value = "INVOKE", target = "Lit/unimi/dsi/fastutil/objects/ObjectSet;remove(Ljava/lang/Object;)Z", shift = At.Shift.AFTER, remap = false))
|
||||||
|
public void minion$updateMinionlessIfNoMinionInChunk(ChunkSectionPos pos, ServerPlayerEntity player, CallbackInfo ci, @Local long chunk) {
|
||||||
|
if (!((ChunkLevelManager$DistanceFromNearestPlayerTrackerAccessor)minionless).minions$isRealPlayerInChunk(chunk)) {
|
||||||
|
minionless.updateLevel(chunk, Integer.MAX_VALUE, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+15
@@ -0,0 +1,15 @@
|
|||||||
|
package io.github.skippyall.minions.mixins.antimobcap;
|
||||||
|
|
||||||
|
import net.minecraft.server.world.ChunkPosDistanceLevelPropagator;
|
||||||
|
import org.spongepowered.asm.mixin.Mixin;
|
||||||
|
import org.spongepowered.asm.mixin.injection.At;
|
||||||
|
import org.spongepowered.asm.mixin.injection.Inject;
|
||||||
|
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||||
|
|
||||||
|
@Mixin(ChunkPosDistanceLevelPropagator.class)
|
||||||
|
public class ChunkPosDistanceLevelPropagatorMixin {
|
||||||
|
@Inject(method = "updateLevel", at = @At("HEAD"))
|
||||||
|
public void minions$updateLevel(long chunkPos, int distance, boolean decrease, CallbackInfo info) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
+12
@@ -0,0 +1,12 @@
|
|||||||
|
package io.github.skippyall.minions.mixins.antimobcap;
|
||||||
|
|
||||||
|
import net.minecraft.server.world.ChunkLevelManager;
|
||||||
|
import net.minecraft.server.world.ServerChunkManager;
|
||||||
|
import org.spongepowered.asm.mixin.Mixin;
|
||||||
|
import org.spongepowered.asm.mixin.gen.Accessor;
|
||||||
|
|
||||||
|
@Mixin(ServerChunkManager.class)
|
||||||
|
public interface ServerChunkManagerAccessor {
|
||||||
|
@Accessor
|
||||||
|
ChunkLevelManager getLevelManager();
|
||||||
|
}
|
||||||
+23
@@ -0,0 +1,23 @@
|
|||||||
|
package io.github.skippyall.minions.mixins.antimobcap;
|
||||||
|
|
||||||
|
import io.github.skippyall.minions.mixinhelper.antimobcap.ChunkLevelManager$DistanceFromNearestPlayerTrackerAccessor;
|
||||||
|
import io.github.skippyall.minions.mixinhelper.antimobcap.ChunkLevelManagerAccessor;
|
||||||
|
import net.minecraft.server.world.ChunkLevelManager;
|
||||||
|
import net.minecraft.server.world.ServerChunkManager;
|
||||||
|
import org.spongepowered.asm.mixin.Final;
|
||||||
|
import org.spongepowered.asm.mixin.Mixin;
|
||||||
|
import org.spongepowered.asm.mixin.Shadow;
|
||||||
|
import org.spongepowered.asm.mixin.injection.At;
|
||||||
|
import org.spongepowered.asm.mixin.injection.ModifyArg;
|
||||||
|
|
||||||
|
@Mixin(ServerChunkManager.class)
|
||||||
|
public class ServerChunkManagerMixin {
|
||||||
|
@Shadow
|
||||||
|
@Final
|
||||||
|
private ChunkLevelManager levelManager;
|
||||||
|
|
||||||
|
@ModifyArg(method = "tickChunks(Lnet/minecraft/util/profiler/Profiler;J)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/SpawnHelper;setupSpawn(ILjava/lang/Iterable;Lnet/minecraft/world/SpawnHelper$ChunkSource;Lnet/minecraft/world/SpawnDensityCapper;)Lnet/minecraft/world/SpawnHelper$Info;"))
|
||||||
|
public int useMinionless(int spawningChunkCount) {
|
||||||
|
return ((ChunkLevelManager$DistanceFromNearestPlayerTrackerAccessor)((ChunkLevelManagerAccessor)levelManager).minions$getMinionless()).minions$getTickedChunkCount();
|
||||||
|
}
|
||||||
|
}
|
||||||
+20
@@ -0,0 +1,20 @@
|
|||||||
|
package io.github.skippyall.minions.mixins.compat.universal_graves;
|
||||||
|
|
||||||
|
import com.llamalad7.mixinextras.sugar.Local;
|
||||||
|
import eu.pb4.graves.grave.Grave;
|
||||||
|
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
|
||||||
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
|
import org.spongepowered.asm.mixin.Mixin;
|
||||||
|
import org.spongepowered.asm.mixin.injection.At;
|
||||||
|
import org.spongepowered.asm.mixin.injection.ModifyArg;
|
||||||
|
|
||||||
|
@Mixin(Grave.class)
|
||||||
|
public class GraveMixin {
|
||||||
|
@ModifyArg(method = "createBlock", at = @At(value = "INVOKE", target = "Leu/pb4/graves/grave/Grave;<init>(JLcom/mojang/authlib/GameProfile;BLnet/minecraft/util/Arm;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/util/Identifier;Leu/pb4/graves/grave/GraveType;JJILnet/minecraft/text/Text;Ljava/util/Collection;Ljava/util/Collection;ZI)V"))
|
||||||
|
private static boolean createGrave(boolean profile, @Local(argsOnly = true) ServerPlayerEntity player) {
|
||||||
|
if(player instanceof MinionFakePlayer) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return profile;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
package io.github.skippyall.minions.module;
|
|
||||||
|
|
||||||
import eu.pb4.sgui.api.elements.GuiElementBuilder;
|
|
||||||
import eu.pb4.sgui.api.gui.SimpleGui;
|
|
||||||
import io.github.skippyall.minions.command.CommandExecutor;
|
|
||||||
import io.github.skippyall.minions.fakeplayer.EntityPlayerActionPack;
|
|
||||||
import io.github.skippyall.minions.fakeplayer.MinionFakePlayer;
|
|
||||||
import net.minecraft.item.Items;
|
|
||||||
import net.minecraft.screen.ScreenHandlerType;
|
|
||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
|
||||||
import net.minecraft.text.Text;
|
|
||||||
|
|
||||||
public class ActionModules {
|
|
||||||
public static void executeOnce(EntityPlayerActionPack.ActionType actionType, ServerPlayerEntity player, MinionFakePlayer minion) {
|
|
||||||
minion.getMinionActionPack().start(actionType, EntityPlayerActionPack.Action.once());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void intervalExecutor(EntityPlayerActionPack.ActionType actionType, ServerPlayerEntity player, MinionFakePlayer minion) {
|
|
||||||
minion.getMinionActionPack().start(actionType, EntityPlayerActionPack.Action.interval());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static CommandExecutor detailSelectionExecutor(EntityPlayerActionPack.ActionType actionType, Text actionName) {
|
|
||||||
return (player, minion) -> {
|
|
||||||
SimpleGui gui = new SimpleGui(ScreenHandlerType.GENERIC_3X3, player, false);
|
|
||||||
gui.setTitle(Text.translatable("minions.command.action.details", actionName));
|
|
||||||
|
|
||||||
gui.setSlot(1, new GuiElementBuilder()
|
|
||||||
.setItem(Items.COMMAND_BLOCK)
|
|
||||||
.setName(Text.translatable("minions.command.action.once", actionName))
|
|
||||||
.setCallback(() -> executeOnce(actionType, player, minion))
|
|
||||||
);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
package io.github.skippyall.minions.module;
|
|
||||||
|
|
||||||
import io.github.skippyall.minions.Minions;
|
|
||||||
import io.github.skippyall.minions.command.SimpleCommand;
|
|
||||||
import net.minecraft.item.Items;
|
|
||||||
import net.minecraft.text.Text;
|
|
||||||
import net.minecraft.util.Identifier;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
import static io.github.skippyall.minions.module.Modules.register;
|
|
||||||
|
|
||||||
public class ChatModule {
|
|
||||||
public static final SimpleModuleItem CHAT_MODULE = register(Identifier.of(Minions.MOD_ID, "chat_module"),
|
|
||||||
new SimpleModuleItem(new ArrayList<>(), Arrays.asList(
|
|
||||||
new SimpleCommand(Text.of("Message"), Text.of("Send Message in Public Chat"), Items.PAPER, (player, minion) -> minion.getServer().getPlayerManager().broadcast(Text.of("message"), true)),
|
|
||||||
new SimpleCommand(Text.of("Prvt-Message"), Text.of("Send Message to one Person"), Items.TRIAL_KEY, (player, minion) -> {})
|
|
||||||
), Items.PAPER));
|
|
||||||
|
|
||||||
public static void registerMe() {}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package io.github.skippyall.minions.module;
|
||||||
|
|
||||||
|
import com.mojang.serialization.Codec;
|
||||||
|
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
||||||
|
import io.github.skippyall.minions.registration.MinionRegistries;
|
||||||
|
import io.github.skippyall.minions.minion.MinionRuntime;
|
||||||
|
import io.github.skippyall.minions.program.instruction.InstructionType;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public record MinionModule(List<InstructionType<MinionRuntime>> instructions, List<SpecialAbility> specialAbilities) {
|
||||||
|
public static final Codec<MinionModule> CODEC = RecordCodecBuilder.create(instance ->
|
||||||
|
instance.group(
|
||||||
|
MinionRegistries.INSTRUCTION_TYPES.getCodec().listOf().fieldOf("instructions").forGetter(MinionModule::instructions),
|
||||||
|
MinionRegistries.SPECIAL_ABILITIES.getCodec().listOf().fieldOf("specialAbilities").forGetter(MinionModule::specialAbilities)
|
||||||
|
).apply(instance, MinionModule::new)
|
||||||
|
);
|
||||||
|
|
||||||
|
public static final MinionModule EMPTY = new MinionModule(List.of());
|
||||||
|
|
||||||
|
public MinionModule(List<InstructionType<MinionRuntime>> instructions) {
|
||||||
|
this(instructions, List.of());
|
||||||
|
}
|
||||||
|
|
||||||
|
public MinionModule(List<InstructionType<MinionRuntime>> instructions, List<SpecialAbility> specialAbilities) {
|
||||||
|
this.instructions = List.copyOf(instructions);
|
||||||
|
this.specialAbilities = List.copyOf(specialAbilities);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package io.github.skippyall.minions.module;
|
||||||
|
|
||||||
|
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
|
||||||
|
|
||||||
|
public class MobSpawningAbility implements SpecialAbility {
|
||||||
|
@Override
|
||||||
|
public void onAdd(MinionFakePlayer minion) {
|
||||||
|
SpecialAbility.super.onAdd(minion);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRemove(MinionFakePlayer minion) {
|
||||||
|
SpecialAbility.super.onRemove(minion);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
package io.github.skippyall.minions.module;
|
|
||||||
|
|
||||||
import io.github.skippyall.minions.Minions;
|
|
||||||
import io.github.skippyall.minions.fakeplayer.MinionFakePlayer;
|
|
||||||
import net.minecraft.item.Items;
|
|
||||||
import net.minecraft.util.Identifier;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static io.github.skippyall.minions.module.Modules.register;
|
|
||||||
|
|
||||||
public class MobSpawningModule {
|
|
||||||
public static final SimpleModuleItem MOB_SPAWNING_MODULE = register(Identifier.of(Minions.MOD_ID, "mob_spawning_module"), new SimpleModuleItem(List.of(), List.of(), Items.SPAWNER));
|
|
||||||
|
|
||||||
public static boolean canMinionSpawnMobs(MinionFakePlayer minion) {
|
|
||||||
return minion.getModuleInventory().hasModule(MOB_SPAWNING_MODULE);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean canMinionDespawnMobs(MinionFakePlayer minion) {
|
|
||||||
return minion.getModuleInventory().hasModule(MOB_SPAWNING_MODULE);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void registerMe() {}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,118 @@
|
|||||||
|
package io.github.skippyall.minions.module;
|
||||||
|
|
||||||
|
import io.github.skippyall.minions.minion.MinionRuntime;
|
||||||
|
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
|
||||||
|
import io.github.skippyall.minions.program.instruction.InstructionType;
|
||||||
|
import io.github.skippyall.minions.registration.MinionComponentTypes;
|
||||||
|
import net.minecraft.inventory.Inventories;
|
||||||
|
import net.minecraft.inventory.SimpleInventory;
|
||||||
|
import net.minecraft.item.ItemStack;
|
||||||
|
import net.minecraft.screen.SimpleNamedScreenHandlerFactory;
|
||||||
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
|
import net.minecraft.storage.ReadView;
|
||||||
|
import net.minecraft.storage.WriteView;
|
||||||
|
import net.minecraft.text.Text;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class ModuleInventory extends SimpleInventory {
|
||||||
|
private final Set<MinionModule> modules = new HashSet<>();
|
||||||
|
private final Set<InstructionType<MinionRuntime>> instructions = new HashSet<>();
|
||||||
|
private final Set<SpecialAbility> specialAbilities = new HashSet<>();
|
||||||
|
|
||||||
|
private final MinionFakePlayer minion;
|
||||||
|
|
||||||
|
public ModuleInventory(MinionFakePlayer minion) {
|
||||||
|
super(27);
|
||||||
|
this.minion = minion;
|
||||||
|
}
|
||||||
|
|
||||||
|
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.contains(MinionComponentTypes.MODULE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void markDirty() {
|
||||||
|
super.markDirty();
|
||||||
|
updateModules();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateModules() {
|
||||||
|
Set<MinionModule> oldModules = Set.copyOf(modules);
|
||||||
|
Set<InstructionType<MinionRuntime>> oldInstructions = Set.copyOf(instructions);
|
||||||
|
Set<SpecialAbility> oldAbilities = Set.copyOf(specialAbilities);
|
||||||
|
|
||||||
|
modules.clear();
|
||||||
|
instructions.clear();
|
||||||
|
specialAbilities.clear();
|
||||||
|
for (ItemStack heldStack : heldStacks) {
|
||||||
|
MinionModule module = heldStack.get(MinionComponentTypes.MODULE);
|
||||||
|
if(module != null) {
|
||||||
|
modules.add(module);
|
||||||
|
instructions.addAll(module.instructions());
|
||||||
|
specialAbilities.addAll(module.specialAbilities());
|
||||||
|
|
||||||
|
for(InstructionType<MinionRuntime> instructionType : module.instructions()) {
|
||||||
|
if(!oldInstructions.contains(instructionType)) {
|
||||||
|
minion.getInstructionManager().enableInstructionType(instructionType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(SpecialAbility ability : module.specialAbilities()) {
|
||||||
|
if(!oldAbilities.contains(ability)) {
|
||||||
|
ability.onAdd(minion);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(InstructionType<MinionRuntime> instructionType : oldInstructions) {
|
||||||
|
if(!instructions.contains(instructionType)) {
|
||||||
|
minion.getInstructionManager().disableInstructionType(instructionType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(SpecialAbility ability : oldAbilities) {
|
||||||
|
if(!specialAbilities.contains(ability)) {
|
||||||
|
ability.onRemove(minion);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void readData(ReadView view) {
|
||||||
|
Inventories.readData(view, heldStacks);
|
||||||
|
updateModules();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void writeData(WriteView view) {
|
||||||
|
Inventories.writeData(view, heldStacks);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Collection<MinionModule> getModules() {
|
||||||
|
return modules;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasAbility(SpecialAbility ability) {
|
||||||
|
return specialAbilities.contains(ability);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasInstruction(InstructionType<MinionRuntime> instructionType) {
|
||||||
|
return instructions.contains(instructionType);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Collection<InstructionType<MinionRuntime>> getAllInstructions() {
|
||||||
|
return instructions;
|
||||||
|
}
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user