implement entity teleportation and biome locate

This commit is contained in:
dfsek
2021-03-10 02:46:00 -07:00
parent 67aae87754
commit 41933b84a0
16 changed files with 284 additions and 102 deletions

View File

@@ -170,4 +170,9 @@ public class Location implements Cloneable {
public String toString() {
return "[" + world + ": (" + getX() + ", " + getY() + ", " + getZ() + ")]";
}
public Location multiply(double v) {
vector.multiply(v);
return this;
}
}

View File

@@ -8,5 +8,7 @@ import com.dfsek.terra.api.platform.world.World;
public interface Entity extends Handle, CommandSender {
Location getLocation();
void setLocation(Location location);
World getWorld();
}

View File

@@ -0,0 +1,46 @@
package com.dfsek.terra.commands;
import com.dfsek.terra.api.TerraPlugin;
import com.dfsek.terra.api.command.CommandTemplate;
import com.dfsek.terra.api.command.annotation.Command;
import com.dfsek.terra.api.command.annotation.Subcommand;
import com.dfsek.terra.api.command.annotation.type.PlayerCommand;
import com.dfsek.terra.api.command.annotation.type.WorldCommand;
import com.dfsek.terra.api.injection.annotations.Inject;
import com.dfsek.terra.api.platform.CommandSender;
import com.dfsek.terra.api.platform.entity.Player;
import com.dfsek.terra.api.world.biome.UserDefinedBiome;
import com.dfsek.terra.api.world.biome.provider.BiomeProvider;
import com.dfsek.terra.commands.biome.BiomeInfoCommand;
import com.dfsek.terra.commands.biome.BiomeLocateCommand;
import com.dfsek.terra.config.lang.LangUtil;
@Command(
subcommands = {
@Subcommand(
value = "info",
aliases = {"i"},
clazz = BiomeInfoCommand.class
),
@Subcommand(
value = "locate",
aliases = {"l"},
clazz = BiomeLocateCommand.class
)
}
)
@WorldCommand
@PlayerCommand
public class BiomeCommand implements CommandTemplate {
@Inject
private TerraPlugin main;
@Override
public void execute(CommandSender sender) {
Player player = (Player) sender;
BiomeProvider provider = main.getWorld(player.getWorld()).getBiomeProvider();
UserDefinedBiome biome = (UserDefinedBiome) provider.getBiome(player.getLocation());
LangUtil.send("command.biome.in", sender, biome.getID());
}
}

View File

@@ -11,5 +11,8 @@ public final class CommandUtil {
manager.register("reload", ReloadCommand.class);
manager.register("addons", AddonsCommand.class);
manager.register("version", VersionCommand.class);
manager.register("getblock", GetBlockCommand.class);
manager.register("packs", PacksCommand.class);
manager.register("biome", BiomeCommand.class);
}
}

View File

@@ -0,0 +1,26 @@
package com.dfsek.terra.commands;
import com.dfsek.terra.api.TerraPlugin;
import com.dfsek.terra.api.command.CommandTemplate;
import com.dfsek.terra.api.command.annotation.Command;
import com.dfsek.terra.api.command.annotation.type.DebugCommand;
import com.dfsek.terra.api.command.annotation.type.PlayerCommand;
import com.dfsek.terra.api.command.annotation.type.WorldCommand;
import com.dfsek.terra.api.injection.annotations.Inject;
import com.dfsek.terra.api.platform.CommandSender;
import com.dfsek.terra.api.platform.entity.Player;
@WorldCommand
@DebugCommand
@PlayerCommand
@Command
public class GetBlockCommand implements CommandTemplate {
@Inject
private TerraPlugin main;
@Override
public void execute(CommandSender sender) {
Player player = (Player) sender;
sender.sendMessage("Block: " + main.getWorld(player.getWorld()).getUngeneratedBlock(player.getLocation()).getAsString());
}
}

View File

@@ -0,0 +1,33 @@
package com.dfsek.terra.commands;
import com.dfsek.terra.api.TerraPlugin;
import com.dfsek.terra.api.command.CommandTemplate;
import com.dfsek.terra.api.command.annotation.Command;
import com.dfsek.terra.api.injection.annotations.Inject;
import com.dfsek.terra.api.platform.CommandSender;
import com.dfsek.terra.api.registry.CheckedRegistry;
import com.dfsek.terra.config.lang.LangUtil;
import com.dfsek.terra.config.pack.ConfigPack;
import com.dfsek.terra.config.pack.ConfigPackTemplate;
@Command
public class PacksCommand implements CommandTemplate {
@Inject
private TerraPlugin main;
@Override
public void execute(CommandSender sender) {
CheckedRegistry<ConfigPack> registry = main.getConfigRegistry();
if(registry.entries().size() == 0) {
LangUtil.send("command.packs.none", sender);
return;
}
LangUtil.send("command.packs.main", sender);
registry.entries().forEach(entry -> {
ConfigPackTemplate template = entry.getTemplate();
LangUtil.send("command.packs.pack", sender, template.getID(), template.getAuthor(), template.getVersion());
});
}
}

View File

@@ -0,0 +1,24 @@
package com.dfsek.terra.commands.biome;
import com.dfsek.terra.api.command.CommandTemplate;
import com.dfsek.terra.api.command.annotation.Argument;
import com.dfsek.terra.api.command.annotation.Command;
import com.dfsek.terra.api.platform.CommandSender;
import com.dfsek.terra.commands.biome.arg.BiomeArgumentParser;
import com.dfsek.terra.commands.biome.tab.BiomeTabCompleter;
@Command(
arguments = {
@Argument(
value = "biome",
tabCompleter = BiomeTabCompleter.class,
argumentParser = BiomeArgumentParser.class
)
}
)
public class BiomeInfoCommand implements CommandTemplate {
@Override
public void execute(CommandSender sender) {
}
}

View File

@@ -0,0 +1,78 @@
package com.dfsek.terra.commands.biome;
import com.dfsek.terra.api.TerraPlugin;
import com.dfsek.terra.api.command.CommandTemplate;
import com.dfsek.terra.api.command.annotation.Argument;
import com.dfsek.terra.api.command.annotation.Command;
import com.dfsek.terra.api.command.annotation.Switch;
import com.dfsek.terra.api.command.annotation.inject.ArgumentTarget;
import com.dfsek.terra.api.command.annotation.inject.SwitchTarget;
import com.dfsek.terra.api.command.annotation.type.PlayerCommand;
import com.dfsek.terra.api.command.annotation.type.WorldCommand;
import com.dfsek.terra.api.command.arg.IntegerArgumentParser;
import com.dfsek.terra.api.injection.annotations.Inject;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.math.vector.Vector3;
import com.dfsek.terra.api.platform.CommandSender;
import com.dfsek.terra.api.platform.entity.Player;
import com.dfsek.terra.api.world.biome.TerraBiome;
import com.dfsek.terra.api.world.locate.AsyncBiomeFinder;
import com.dfsek.terra.commands.biome.arg.BiomeArgumentParser;
import com.dfsek.terra.commands.biome.tab.BiomeTabCompleter;
import com.dfsek.terra.config.lang.LangUtil;
import java.util.Locale;
@PlayerCommand
@WorldCommand
@Command(
arguments = {
@Argument(
value = "biome",
tabCompleter = BiomeTabCompleter.class,
argumentParser = BiomeArgumentParser.class
),
@Argument(
value = "radius",
required = false,
defaultValue = "1000",
argumentParser = IntegerArgumentParser.class
)
},
switches = {
@Switch(
value = "teleport",
aliases = {"t", "tp"}
)
}
)
public class BiomeLocateCommand implements CommandTemplate {
@ArgumentTarget("radius")
private Integer radius;
@ArgumentTarget("biome")
private TerraBiome biome;
@SwitchTarget("teleport")
private boolean teleport;
@Inject
private TerraPlugin main;
@Override
public void execute(CommandSender sender) {
Player player = (Player) sender;
new Thread(new AsyncBiomeFinder(main.getWorld(player.getWorld()).getBiomeProvider(), biome, player.getLocation().clone().multiply((1D / main.getTerraConfig().getBiomeSearchResolution())), 0, radius, location -> {
if(location != null) {
sender.sendMessage(String.format("The nearest %s is at [%d, ~, %d] (%.1f blocks away)", biome.getID().toLowerCase(Locale.ROOT), location.getBlockX(), location.getBlockZ(), location.add(new Vector3(0, player.getLocation().getY(), 0)).distance(player.getLocation().toVector())));
if(teleport) {
player.setLocation(new Location(player.getWorld(), location.getX(), player.getLocation().getY(), location.getZ()));
}
} else LangUtil.send("command.biome.unable-to-locate", sender);
}, main), "Biome Location Thread").start();
}
}

View File

@@ -0,0 +1,19 @@
package com.dfsek.terra.commands.biome.arg;
import com.dfsek.terra.api.TerraPlugin;
import com.dfsek.terra.api.command.arg.ArgumentParser;
import com.dfsek.terra.api.injection.annotations.Inject;
import com.dfsek.terra.api.platform.CommandSender;
import com.dfsek.terra.api.platform.entity.Player;
import com.dfsek.terra.api.world.biome.TerraBiome;
public class BiomeArgumentParser implements ArgumentParser<TerraBiome> {
@Inject
private TerraPlugin main;
@Override
public TerraBiome parse(CommandSender sender, String arg) {
Player player = (Player) sender;
return main.getWorld(player.getWorld()).getConfig().getBiomeRegistry().get(arg);
}
}

View File

@@ -0,0 +1,22 @@
package com.dfsek.terra.commands.biome.tab;
import com.dfsek.terra.api.TerraPlugin;
import com.dfsek.terra.api.command.tab.TabCompleter;
import com.dfsek.terra.api.injection.annotations.Inject;
import com.dfsek.terra.api.platform.CommandSender;
import com.dfsek.terra.api.platform.entity.Player;
import com.dfsek.terra.api.world.biome.TerraBiome;
import java.util.List;
import java.util.stream.Collectors;
public class BiomeTabCompleter implements TabCompleter {
@Inject
private TerraPlugin main;
@Override
public List<String> complete(CommandSender sender) {
Player player = (Player) sender;
return main.getWorld(player.getWorld()).getConfig().getBiomeRegistry().entries().stream().map(TerraBiome::getID).collect(Collectors.toList());
}
}

View File

@@ -22,6 +22,11 @@ public class BukkitEntity implements Entity {
return BukkitAdapter.adapt(entity.getLocation());
}
@Override
public void setLocation(Location location) {
entity.teleport(BukkitAdapter.adapt(location));
}
@Override
public World getWorld() {
return BukkitAdapter.adapt(entity.getWorld());

View File

@@ -23,6 +23,11 @@ public class BukkitPlayer implements Player {
return new Location(BukkitAdapter.adapt(bukkit.getWorld()), bukkit.getX(), bukkit.getY(), bukkit.getZ());
}
@Override
public void setLocation(Location location) {
delegate.teleport(BukkitAdapter.adapt(location));
}
@Override
public World getWorld() {
return BukkitAdapter.adapt(delegate.getWorld());

View File

@@ -1,44 +0,0 @@
package com.dfsek.terra.bukkit.command.command;
import com.dfsek.terra.bukkit.command.WorldCommand;
import com.dfsek.terra.bukkit.world.BukkitAdapter;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.Collections;
import java.util.List;
public class GetBlockCommand extends WorldCommand {
public GetBlockCommand(com.dfsek.terra.bukkit.command.Command parent) {
super(parent);
}
@Override
public boolean execute(@NotNull Player player, @NotNull Command command, @NotNull String s, @NotNull String[] strings, World world) {
player.sendMessage("Block: " + getMain().getWorld(BukkitAdapter.adapt(world)).getUngeneratedBlock(BukkitAdapter.adapt(player.getLocation())).getAsString());
return true;
}
@Override
public String getName() {
return "block";
}
@Override
public List<com.dfsek.terra.bukkit.command.Command> getSubCommands() {
return Collections.emptyList();
}
@Override
public int arguments() {
return 0;
}
@Override
public List<String> getTabCompletions(@NotNull CommandSender commandSender, @NotNull String s, @NotNull String[] strings) {
return Collections.emptyList();
}
}

View File

@@ -1,58 +0,0 @@
package com.dfsek.terra.bukkit.command.command;
import com.dfsek.terra.api.registry.CheckedRegistry;
import com.dfsek.terra.bukkit.BukkitCommandSender;
import com.dfsek.terra.bukkit.command.Command;
import com.dfsek.terra.config.lang.LangUtil;
import com.dfsek.terra.config.pack.ConfigPack;
import com.dfsek.terra.config.pack.ConfigPackTemplate;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import java.util.Collections;
import java.util.List;
public class PacksCommand extends Command {
public PacksCommand(Command parent) {
super(parent);
}
@Override
public String getName() {
return "packs";
}
@Override
public List<Command> getSubCommands() {
return Collections.emptyList();
}
@Override
public boolean execute(@NotNull CommandSender commandSender, org.bukkit.command.@NotNull Command command, @NotNull String s, @NotNull String[] strings) {
CheckedRegistry<ConfigPack> registry = getMain().getConfigRegistry();
if(registry.entries().size() == 0) {
LangUtil.send("command.packs.none", new BukkitCommandSender(commandSender));
return true;
}
LangUtil.send("command.packs.main", new BukkitCommandSender(commandSender));
registry.entries().forEach(entry -> {
ConfigPackTemplate template = entry.getTemplate();
LangUtil.send("command.packs.pack", new BukkitCommandSender(commandSender), template.getID(), template.getAuthor(), template.getVersion());
});
return true;
}
@Override
public int arguments() {
return 0;
}
@Override
public List<String> getTabCompletions(@NotNull CommandSender commandSender, @NotNull String s, @NotNull String[] strings) {
return Collections.emptyList();
}
}

View File

@@ -5,6 +5,8 @@ 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;
@@ -28,6 +30,12 @@ public class FabricEntity implements Entity {
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

@@ -5,7 +5,9 @@ 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 com.dfsek.terra.fabric.world.handles.world.FabricWorldHandle;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.LiteralText;
public class FabricPlayer implements Player {
@@ -34,4 +36,10 @@ public class FabricPlayer implements Player {
public World getWorld() {
return new FabricWorldAccess(delegate.world);
}
@Override
public void setLocation(Location location) {
delegate.teleport(location.getX(), location.getY(), location.getZ());
delegate.moveToWorld((ServerWorld) ((FabricWorldHandle) location).getWorld());
}
}