Minion Item(drop) contains Minion Data
Movement fixes
This commit is contained in:
@@ -4,11 +4,14 @@ import com.mojang.authlib.GameProfile;
|
|||||||
import com.mojang.authlib.yggdrasil.ProfileResult;
|
import com.mojang.authlib.yggdrasil.ProfileResult;
|
||||||
import io.github.skippyall.minions.Minions;
|
import io.github.skippyall.minions.Minions;
|
||||||
import io.github.skippyall.minions.minion.MinionInventory;
|
import io.github.skippyall.minions.minion.MinionInventory;
|
||||||
|
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.minion.ModuleInventory;
|
import io.github.skippyall.minions.minion.ModuleInventory;
|
||||||
import io.github.skippyall.minions.mixins.GameProfileMixin;
|
import io.github.skippyall.minions.mixins.GameProfileMixin;
|
||||||
import io.github.skippyall.minions.program.runtime.MinionRuntime;
|
import io.github.skippyall.minions.program.runtime.MinionRuntime;
|
||||||
import net.minecraft.block.BlockState;
|
import net.minecraft.block.BlockState;
|
||||||
|
import net.minecraft.component.ComponentType;
|
||||||
|
import net.minecraft.component.DataComponentTypes;
|
||||||
import net.minecraft.entity.Entity;
|
import net.minecraft.entity.Entity;
|
||||||
import net.minecraft.entity.EquipmentSlot;
|
import net.minecraft.entity.EquipmentSlot;
|
||||||
import net.minecraft.entity.MovementType;
|
import net.minecraft.entity.MovementType;
|
||||||
@@ -128,6 +131,43 @@ public class MinionFakePlayer extends ServerPlayerEntity {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void spawnMinionAt(MinionPersistentState.MinionData data, ServerWorld level, Vec3d pos, double yaw, double pitch) {
|
||||||
|
MinecraftServer server = level.getServer();
|
||||||
|
server.getUserCache().findByNameAsync(data.name).thenAcceptAsync((optional) -> {
|
||||||
|
GameProfile profile = null;
|
||||||
|
if (optional.isPresent()) {
|
||||||
|
ProfileResult result = server.getSessionService().fetchProfile(optional.get().getId(), true);
|
||||||
|
if (result != null) {
|
||||||
|
profile = result.profile();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (profile == null) {
|
||||||
|
profile = new GameProfile(new UUID(0, 0), data.name);
|
||||||
|
}
|
||||||
|
GameProfile newProfile = new GameProfile(data.uuid, data.name);
|
||||||
|
newProfile.getProperties().putAll(profile.getProperties());
|
||||||
|
GameProfile finalProfile = newProfile;
|
||||||
|
((GameProfileMixin)finalProfile).setId(data.uuid);
|
||||||
|
Minions.addExecuteOnNextTick(() -> {
|
||||||
|
MinionFakePlayer instance = new MinionFakePlayer(server, level, finalProfile, SyncedClientOptions.createDefault(), false, data.programmable);
|
||||||
|
instance.fixStartingPosition = () -> instance.refreshPositionAndAngles(pos.x, pos.y, pos.z, (float) yaw, (float) pitch);
|
||||||
|
System.out.println(instance.getPos());
|
||||||
|
server.getPlayerManager().onPlayerConnect(new FakeClientConnection(NetworkSide.SERVERBOUND), instance, new ConnectedClientData(finalProfile, 0, instance.getClientOptions(), false));
|
||||||
|
System.out.println(instance.getPos());
|
||||||
|
instance.teleport(level, pos.x, pos.y, pos.z, (float) yaw, (float) pitch);
|
||||||
|
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;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unused") //Don't know if I'll need this
|
@SuppressWarnings("unused") //Don't know if I'll need this
|
||||||
public static MinionFakePlayer createShadow(MinecraftServer server, ServerPlayerEntity player)
|
public static MinionFakePlayer createShadow(MinecraftServer server, ServerPlayerEntity player)
|
||||||
{
|
{
|
||||||
@@ -316,14 +356,14 @@ public class MinionFakePlayer extends ServerPlayerEntity {
|
|||||||
float newForward = (float) (moveForward - movement.z);
|
float newForward = (float) (moveForward - movement.z);
|
||||||
float newSideways = (float) (moveSideways - movement.x);
|
float newSideways = (float) (moveSideways - movement.x);
|
||||||
Vec3d newMovement = movement;
|
Vec3d newMovement = movement;
|
||||||
if ((newForward < 0 && moveForward >= 0) || (newForward > 0 && moveForward <= 0)) {
|
if ((newForward < 0 && moveForward > 0) || (newForward > 0 && moveForward < 0)) {
|
||||||
newMovement = new Vec3d(newMovement.x, newMovement.y, moveForward);
|
newMovement = new Vec3d(newMovement.x, newMovement.y, moveForward);
|
||||||
moveForward = 0;
|
moveForward = 0;
|
||||||
getMinionActionPack().setForward(0);
|
getMinionActionPack().setForward(0);
|
||||||
}else {
|
}else {
|
||||||
moveForward = newForward;
|
moveForward = newForward;
|
||||||
}
|
}
|
||||||
if ((newSideways < 0 && moveSideways >= 0) || (newSideways > 0 && moveSideways <= 0)) {
|
if ((newSideways < 0 && moveSideways > 0) || (newSideways > 0 && moveSideways < 0)) {
|
||||||
newMovement = new Vec3d(newMovement.x, newMovement.y, moveSideways);
|
newMovement = new Vec3d(newMovement.x, newMovement.y, moveSideways);
|
||||||
moveSideways = 0;
|
moveSideways = 0;
|
||||||
getMinionActionPack().setStrafing(0);
|
getMinionActionPack().setStrafing(0);
|
||||||
@@ -336,13 +376,22 @@ public class MinionFakePlayer extends ServerPlayerEntity {
|
|||||||
@Override
|
@Override
|
||||||
protected void drop(ServerWorld world, DamageSource damageSource) {
|
protected void drop(ServerWorld world, DamageSource damageSource) {
|
||||||
super.drop(world, damageSource);
|
super.drop(world, damageSource);
|
||||||
dropItem(new ItemStack(Minions.MINION_ITEM), true, false);
|
dropItem(toItemStack(), true, false);
|
||||||
for(ItemStack item : moduleInventory.getItems()) {
|
for(ItemStack item : moduleInventory.getItems()) {
|
||||||
dropItem(item, true, false);
|
dropItem(item, true, false);
|
||||||
}
|
}
|
||||||
moduleInventory.clear();
|
moduleInventory.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private ItemStack toItemStack() {
|
||||||
|
ItemStack stack = new ItemStack(Minions.MINION_ITEM);
|
||||||
|
MinionItem.setData(MinionPersistentState.MinionData.fromMinion(this), stack);
|
||||||
|
if (!getMinionName().equals("Minion")) {
|
||||||
|
stack.set(DataComponentTypes.CUSTOM_NAME, Text.of(getMinionName()));
|
||||||
|
}
|
||||||
|
return stack;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void writeCustomDataToNbt(NbtCompound nbt) {
|
public void writeCustomDataToNbt(NbtCompound nbt) {
|
||||||
super.writeCustomDataToNbt(nbt);
|
super.writeCustomDataToNbt(nbt);
|
||||||
|
|||||||
@@ -3,9 +3,14 @@ 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.fakeplayer.MinionFakePlayer;
|
||||||
|
import net.minecraft.client.render.VertexFormatElement;
|
||||||
|
import net.minecraft.component.ComponentType;
|
||||||
import net.minecraft.component.DataComponentTypes;
|
import net.minecraft.component.DataComponentTypes;
|
||||||
|
import net.minecraft.component.type.NbtComponent;
|
||||||
import net.minecraft.item.*;
|
import net.minecraft.item.*;
|
||||||
import net.minecraft.item.tooltip.TooltipType;
|
import net.minecraft.item.tooltip.TooltipType;
|
||||||
|
import net.minecraft.nbt.NbtCompound;
|
||||||
|
import net.minecraft.nbt.NbtElement;
|
||||||
import net.minecraft.registry.RegistryWrapper;
|
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;
|
||||||
@@ -41,8 +46,37 @@ public class MinionItem extends Item implements PolymerItem {
|
|||||||
name = "Minion";
|
name = "Minion";
|
||||||
}
|
}
|
||||||
if(!context.getWorld().isClient) {
|
if(!context.getWorld().isClient) {
|
||||||
|
MinionPersistentState.MinionData data = getData(context.getStack());
|
||||||
|
if (data == null) {
|
||||||
MinionFakePlayer.createMinion(name, (ServerWorld) context.getWorld(), (ServerPlayerEntity) context.getPlayer(), canProgram, context.getBlockPos().toCenterPos().add(0,0.5,0), 0, 0);
|
MinionFakePlayer.createMinion(name, (ServerWorld) context.getWorld(), (ServerPlayerEntity) context.getPlayer(), canProgram, context.getBlockPos().toCenterPos().add(0,0.5,0), 0, 0);
|
||||||
|
}else {
|
||||||
|
MinionFakePlayer.spawnMinionAt(data, (ServerWorld) context.getWorld(), context.getBlockPos().toCenterPos().add(0,0.5,0), 0, 0);
|
||||||
|
MinionPersistentState.INSTANCE.addMinion(data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return ActionResult.SUCCESS;
|
return ActionResult.SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void setData(MinionPersistentState.MinionData data, ItemStack item) {
|
||||||
|
NbtCompound nbt = item.getOrDefault(DataComponentTypes.CUSTOM_DATA, NbtComponent.DEFAULT).copyNbt();
|
||||||
|
nbt.put("data", data.writeNbt());
|
||||||
|
item.set(DataComponentTypes.CUSTOM_DATA, NbtComponent.of(nbt));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public static MinionPersistentState.MinionData getData(ItemStack item) {
|
||||||
|
NbtCompound nbt = item.getOrDefault(DataComponentTypes.CUSTOM_DATA, NbtComponent.DEFAULT).copyNbt();
|
||||||
|
if (nbt.getType("data") == NbtElement.COMPOUND_TYPE) {
|
||||||
|
return MinionPersistentState.MinionData.readNbt(nbt.getCompound("data"));
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean containsData(ItemStack item) {
|
||||||
|
NbtComponent nbt = item.get(DataComponentTypes.CUSTOM_DATA);
|
||||||
|
if (nbt == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return nbt.copyNbt().contains("data");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package io.github.skippyall.minions.mixins;
|
||||||
|
|
||||||
|
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
|
||||||
|
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
|
||||||
|
import it.unimi.dsi.fastutil.objects.ObjectSet;
|
||||||
|
import net.minecraft.server.world.ChunkTicketManager;
|
||||||
|
import org.spongepowered.asm.mixin.Mixin;
|
||||||
|
import org.spongepowered.asm.mixin.injection.At;
|
||||||
|
|
||||||
|
@Mixin(value = ChunkTicketManager.class, remap = false)
|
||||||
|
public class ChunkTicketManagerFixMixin {
|
||||||
|
@WrapOperation(method = "handleChunkLeave", at = @At(value = "INVOKE", target = "Lit/unimi/dsi/fastutil/objects/ObjectSet;remove(Ljava/lang/Object;)Z"))
|
||||||
|
public boolean filterIfNull(ObjectSet instance, Object o, Operation<Boolean> original) {
|
||||||
|
if (instance != null) {
|
||||||
|
return original.call(instance, o);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;//Unused
|
||||||
|
}
|
||||||
|
|
||||||
|
@WrapOperation(method = "handleChunkLeave", at = @At(value = "INVOKE", target = "Lit/unimi/dsi/fastutil/objects/ObjectSet;isEmpty()Z"))
|
||||||
|
public boolean filterIfNull(ObjectSet instance, Operation<Boolean> original) {
|
||||||
|
if (instance != null) {
|
||||||
|
return original.call(instance);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;//Unused
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@
|
|||||||
"package": "io.github.skippyall.minions.mixins",
|
"package": "io.github.skippyall.minions.mixins",
|
||||||
"compatibilityLevel": "JAVA_17",
|
"compatibilityLevel": "JAVA_17",
|
||||||
"mixins": [
|
"mixins": [
|
||||||
|
"ChunkTicketManagerFixMixin",
|
||||||
"ConnectionMixin",
|
"ConnectionMixin",
|
||||||
"Debug2Mixin",
|
"Debug2Mixin",
|
||||||
"DebugMixin",
|
"DebugMixin",
|
||||||
|
|||||||
Reference in New Issue
Block a user