PlayerEntityMixin and EntityMixin

This commit is contained in:
dfsek 2021-05-02 20:38:25 -07:00
parent c0368f1c6d
commit 35d85f2aa3
9 changed files with 92 additions and 92 deletions

View File

@ -18,6 +18,8 @@ import com.dfsek.terra.api.event.annotations.Priority;
import com.dfsek.terra.api.event.events.config.ConfigPackPreLoadEvent;
import com.dfsek.terra.api.platform.CommandSender;
import com.dfsek.terra.api.platform.block.BlockData;
import com.dfsek.terra.api.platform.entity.Entity;
import com.dfsek.terra.api.platform.entity.Player;
import com.dfsek.terra.api.platform.handle.ItemHandle;
import com.dfsek.terra.api.platform.handle.WorldHandle;
import com.dfsek.terra.api.platform.world.Tree;
@ -54,6 +56,7 @@ import com.dfsek.terra.registry.master.ConfigRegistry;
import com.dfsek.terra.world.TerraWorld;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
@ -363,6 +366,10 @@ public class TerraFabricPlugin implements TerraPlugin, ModInitializer {
return in.suggests((context, builder) -> {
List<String> args = parseCommand(context.getInput());
CommandSender sender = (CommandSender) context.getSource();
try {
sender = (Entity) context.getSource().getEntityOrThrow();
} catch(CommandSyntaxException ignore) {
}
try {
manager.tabComplete(args.remove(0), sender, args).forEach(builder::suggest);
} catch(CommandException e) {
@ -371,8 +378,13 @@ public class TerraFabricPlugin implements TerraPlugin, ModInitializer {
return builder.buildFuture();
}).executes(context -> {
List<String> args = parseCommand(context.getInput());
CommandSender sender = (CommandSender) context.getSource();
try {
manager.execute(args.remove(0), (CommandSender) context.getSource(), args);
sender = (Entity) context.getSource().getEntityOrThrow();
} catch(CommandSyntaxException ignore) {
}
try {
manager.execute(args.remove(0), sender, args);
} catch(CommandException e) {
context.getSource().sendError(new LiteralText(e.getMessage()));
}

View File

@ -0,0 +1,61 @@
package com.dfsek.terra.fabric.mixin.entity;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.platform.world.World;
import com.dfsek.terra.fabric.world.FabricAdapter;
import com.dfsek.terra.fabric.world.handles.world.FabricWorldAccess;
import com.dfsek.terra.fabric.world.handles.world.FabricWorldHandle;
import net.minecraft.entity.Entity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Implements;
import org.spongepowered.asm.mixin.Interface;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import java.util.UUID;
@Mixin(Entity.class)
@Implements(@Interface(iface = com.dfsek.terra.api.platform.entity.Entity.class, prefix = "vw$"))
public abstract class EntityMixin {
@Shadow
public net.minecraft.world.World world;
@Shadow
private BlockPos blockPos;
@Shadow
public abstract void teleport(double destX, double destY, double destZ);
@Shadow
@Nullable
public abstract Entity moveToWorld(ServerWorld destination);
@Shadow
public abstract void sendSystemMessage(Text message, UUID senderUuid);
public Object vw$getHandle() {
return this;
}
public Location vw$getLocation() {
return new Location(new FabricWorldAccess(world), FabricAdapter.adapt(blockPos));
}
public void vw$setLocation(Location location) {
teleport(location.getX(), location.getY(), location.getZ());
moveToWorld((ServerWorld) ((FabricWorldHandle) location).getWorld());
}
public World getWorld() {
return new FabricWorldAccess(world);
}
public void vw$sendMessage(String message) {
sendSystemMessage(new LiteralText(message), UUID.randomUUID()); // TODO: look into how this actually works and make it less jank
}
}

View File

@ -0,0 +1,13 @@
package com.dfsek.terra.fabric.mixin.entity;
import com.dfsek.terra.api.platform.entity.Player;
import net.minecraft.entity.player.PlayerEntity;
import org.spongepowered.asm.mixin.Implements;
import org.spongepowered.asm.mixin.Interface;
import org.spongepowered.asm.mixin.Mixin;
@Mixin(PlayerEntity.class)
@Implements(@Interface(iface = Player.class, prefix = "vw$"))
public abstract class PlayerEntityMixin extends EntityMixin {
}

View File

@ -1,43 +0,0 @@
package com.dfsek.terra.fabric.world.entity;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.platform.entity.Entity;
import com.dfsek.terra.api.platform.world.World;
import com.dfsek.terra.fabric.world.FabricAdapter;
import com.dfsek.terra.fabric.world.handles.world.FabricWorldAccess;
import com.dfsek.terra.fabric.world.handles.world.FabricWorldHandle;
import net.minecraft.server.world.ServerWorld;
public class FabricEntity implements Entity {
private final net.minecraft.entity.Entity delegate;
public FabricEntity(net.minecraft.entity.Entity delegate) {
this.delegate = delegate;
}
@Override
public void sendMessage(String message) {
}
@Override
public Object getHandle() {
return null;
}
@Override
public Location getLocation() {
return new Location(new FabricWorldAccess(delegate.world), FabricAdapter.adapt(delegate.getBlockPos()));
}
@Override
public void setLocation(Location location) {
delegate.teleport(location.getX(), location.getY(), location.getZ());
delegate.moveToWorld((ServerWorld) ((FabricWorldHandle) location).getWorld());
}
@Override
public World getWorld() {
return new FabricWorldAccess(delegate.world);
}
}

View File

@ -1,42 +0,0 @@
package com.dfsek.terra.fabric.world.entity;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.platform.entity.Player;
import com.dfsek.terra.api.platform.world.World;
import com.dfsek.terra.fabric.world.FabricAdapter;
import com.dfsek.terra.fabric.world.handles.world.FabricWorldAccess;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.text.LiteralText;
public class FabricPlayer implements Player {
private final PlayerEntity delegate;
public FabricPlayer(PlayerEntity delegate) {
this.delegate = delegate;
}
@Override
public void sendMessage(String message) {
delegate.sendMessage(new LiteralText(message), false);
}
@Override
public Object getHandle() {
return delegate;
}
@Override
public Location getLocation() {
return FabricAdapter.adapt(delegate.getBlockPos()).toLocation(new FabricWorldAccess(delegate.world));
}
@Override
public World getWorld() {
return new FabricWorldAccess(delegate.world);
}
@Override
public void setLocation(Location location) {
delegate.teleport(location.getX(), location.getY(), location.getZ());
}
}

View File

@ -9,7 +9,6 @@ import com.dfsek.terra.api.platform.world.World;
import com.dfsek.terra.api.platform.world.generator.ChunkGenerator;
import com.dfsek.terra.fabric.world.FabricAdapter;
import com.dfsek.terra.fabric.world.block.FabricBlock;
import com.dfsek.terra.fabric.world.entity.FabricEntity;
import com.dfsek.terra.fabric.world.handles.chunk.FabricChunk;
import com.dfsek.terra.fabric.world.handles.world.FabricWorldHandle;
import net.minecraft.server.world.ServerWorld;
@ -72,7 +71,7 @@ public class FabricWorld implements World, FabricWorldHandle {
net.minecraft.entity.Entity entity = FabricAdapter.adapt(entityType).create(delegate.world);
entity.setPos(location.getX(), location.getY(), location.getZ());
delegate.world.spawnEntity(entity);
return new FabricEntity(entity);
return (Entity) entity;
}
@Override

View File

@ -9,7 +9,6 @@ import com.dfsek.terra.api.platform.world.World;
import com.dfsek.terra.api.platform.world.generator.ChunkGenerator;
import com.dfsek.terra.fabric.world.FabricAdapter;
import com.dfsek.terra.fabric.world.block.FabricBlock;
import com.dfsek.terra.fabric.world.entity.FabricEntity;
import com.dfsek.terra.fabric.world.generator.FabricChunkGenerator;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
@ -60,7 +59,7 @@ public class FabricSeededWorldAccess implements World, FabricWorldHandle {
net.minecraft.entity.Entity entity = FabricAdapter.adapt(entityType).create((ServerWorld) handle.worldAccess);
entity.setPos(location.getX(), location.getY(), location.getZ());
handle.worldAccess.spawnEntity(entity);
return new FabricEntity(entity);
return (Entity) entity;
}
@Override

View File

@ -9,7 +9,6 @@ import com.dfsek.terra.api.platform.world.World;
import com.dfsek.terra.api.platform.world.generator.ChunkGenerator;
import com.dfsek.terra.fabric.world.FabricAdapter;
import com.dfsek.terra.fabric.world.block.FabricBlock;
import com.dfsek.terra.fabric.world.entity.FabricEntity;
import com.dfsek.terra.fabric.world.generator.FabricChunkGenerator;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.ServerWorldAccess;
@ -59,7 +58,7 @@ public class FabricWorldAccess implements World, FabricWorldHandle {
net.minecraft.entity.Entity entity = FabricAdapter.adapt(entityType).create(((ServerWorldAccess) delegate).toServerWorld());
entity.setPos(location.getX(), location.getY(), location.getZ());
delegate.spawnEntity(entity);
return new FabricEntity(entity);
return (Entity) entity;
}
@Override

View File

@ -5,6 +5,8 @@
"compatibilityLevel": "JAVA_8",
"mixins": [
"MixinGeneratorOptions",
"entity.EntityMixin",
"entity.PlayerEntityMixin",
"entity.ServerCommandSourceMixin",
"world.ChunkRegionMixin",
"world.ProtoChunkMixin",