Skip to content

TerminatorPlus Bot GUI #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

target/
.idea/*
3 changes: 2 additions & 1 deletion src/main/java/net/nuggetmc/tplus/TerminatorPlus.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.nuggetmc.tplus.bot.BotManager;
import net.nuggetmc.tplus.command.CommandHandler;
import net.nuggetmc.tplus.ui.menu.MenuListener;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;

Expand Down Expand Up @@ -41,7 +42,7 @@ public void onEnable() {
this.handler = new CommandHandler(this);

// Register event listeners
this.registerEvents(manager);
this.registerEvents(manager,new MenuListener());
}

@Override
Expand Down
65 changes: 60 additions & 5 deletions src/main/java/net/nuggetmc/tplus/bot/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@
import org.bukkit.util.Vector;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Objects;
import java.util.UUID;
import java.text.DecimalFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;

public class Bot extends EntityPlayer {

private final DecimalFormat formatter;
private final TerminatorPlus plugin;
private final BukkitScheduler scheduler;
private final Agent agent;
Expand All @@ -57,6 +56,8 @@ public boolean hasNeuralNetwork() {

public ItemStack defaultItem;

private String skinName;

private boolean shield;
private boolean blocking;
private boolean blockUse;
Expand All @@ -79,6 +80,7 @@ public boolean hasNeuralNetwork() {
private Bot(MinecraftServer minecraftServer, WorldServer worldServer, GameProfile profile, PlayerInteractManager manager) {
super(minecraftServer, worldServer, profile, manager);

this.formatter = new DecimalFormat("0.##");
this.plugin = TerminatorPlus.getInstance();
this.scheduler = Bukkit.getScheduler();
this.agent = plugin.getManager().getAgent();
Expand Down Expand Up @@ -108,6 +110,7 @@ public static Bot createBot(Location loc, String name, String[] skin) {

Bot bot = new Bot(nmsServer, nmsWorld, profile, interactManager);


bot.playerConnection = new PlayerConnection(nmsServer, new NetworkManager(EnumProtocolDirection.CLIENTBOUND) {

@Override
Expand Down Expand Up @@ -627,6 +630,15 @@ private void kb(Location loc1, Location loc2) {
velocity = vel;
}

public String getSkinName(){
return skinName;
}

public void setSkinName(String skinName){
this.skinName = skinName;
}


public int getKills() {
return kills;
}
Expand Down Expand Up @@ -742,4 +754,47 @@ public void playerTick() {
this.lastYaw = this.yaw;
this.lastPitch = this.pitch;
}


public List<String> botLore(){

long aliveTimeHours = TimeUnit.SECONDS.toHours(this.getAliveTicks() / 20);
long aliveTimeMinutes = TimeUnit.SECONDS.toMinutes((this.getAliveTicks() / 20) % 3600);
long aliveTimeSeconds = TimeUnit.SECONDS.toSeconds((this.getAliveTicks() / 20) % 60);
String aliveTime;
if (aliveTimeHours == 0){
aliveTime = String.format("%02d", aliveTimeMinutes) + ":" + String.format("%02d", aliveTimeSeconds);
}
else{
aliveTime = aliveTimeHours + ":" + String.format("%02d", aliveTimeMinutes) + ":" + String.format("%02d", aliveTimeSeconds);
}



String world = this.getBukkitEntity().getWorld().getName();
Location loc = this.getLocation();

String location = org.bukkit.ChatColor.AQUA + formatter.format(loc.getBlockX()) + ", " + formatter.format(loc.getBlockY()) + ", " + formatter.format(loc.getBlockZ());

Vector vel = this.getVelocity();
String velocity = org.bukkit.ChatColor.AQUA + formatter.format(vel.getX()) + ", " + formatter.format(vel.getY()) + ", " + formatter.format(vel.getZ());

String neuralNetwork;
try{
neuralNetwork = this.getNeuralNetwork().toString();
}
catch (Exception e){
neuralNetwork = "None";
}

List<String> lore = new ArrayList<>();
lore.add(net.md_5.bungee.api.ChatColor.WHITE + "Time Alive - " + net.md_5.bungee.api.ChatColor.YELLOW + aliveTime);
lore.add(net.md_5.bungee.api.ChatColor.WHITE + "World - " + net.md_5.bungee.api.ChatColor.YELLOW + world);
lore.add(net.md_5.bungee.api.ChatColor.WHITE + "Location - " + net.md_5.bungee.api.ChatColor.AQUA + location);
lore.add(net.md_5.bungee.api.ChatColor.WHITE + "Velocity - " + net.md_5.bungee.api.ChatColor.AQUA + velocity);
lore.add(net.md_5.bungee.api.ChatColor.WHITE + "Health - " + net.md_5.bungee.api.ChatColor.RED + this.getHealth());
lore.add(net.md_5.bungee.api.ChatColor.WHITE + "Kills - " + net.md_5.bungee.api.ChatColor.RED + this.getKills());
lore.add(net.md_5.bungee.api.ChatColor.WHITE + "Neural Network - " + net.md_5.bungee.api.ChatColor.GREEN + neuralNetwork);
return lore;
}
}
10 changes: 6 additions & 4 deletions src/main/java/net/nuggetmc/tplus/bot/BotManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,25 @@ public void createBots(Player sender, String name, String skinName, int n, Neura

skinName = skinName == null ? name : skinName;

createBots(sender.getLocation(), name, MojangAPI.getSkin(skinName), n, network);
createBots(sender.getLocation(), name, skinName, n, network);

sender.sendMessage("Process completed (" + ChatColor.RED + ((System.currentTimeMillis() - timestamp) / 1000D) + "s" + ChatColor.RESET + ").");
}

public Set<Bot> createBots(Location loc, String name, String[] skin, int n, NeuralNetwork network) {
public Set<Bot> createBots(Location loc, String name, String skinName, int n, NeuralNetwork network) {
List<NeuralNetwork> networks = new ArrayList<>();

for (int i = 0; i < n; i++) {
networks.add(network);
}

return createBots(loc, name, skin, networks);
return createBots(loc, name, skinName, networks);
}

public Set<Bot> createBots(Location loc, String name, String[] skin, List<NeuralNetwork> networks) {
public Set<Bot> createBots(Location loc, String name, String skinName, List<NeuralNetwork> networks) {
Set<Bot> bots = new HashSet<>();
World world = loc.getWorld();
String[] skin = MojangAPI.getSkin(skinName);

int n = networks.size();
int i = 1;
Expand All @@ -107,6 +108,7 @@ public Set<Bot> createBots(Location loc, String name, String[] skin, List<Neural

for (NeuralNetwork network : networks) {
Bot bot = Bot.createBot(loc, name.replace("%", String.valueOf(i)), skin);
bot.setSkinName(skinName);

if (network != null) {
bot.setNeuralNetwork(network == NeuralNetwork.RANDOM ? NeuralNetwork.generateRandomNetwork() : network);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@ private void runGeneration() throws InterruptedException {

print("Fetching skin data for " + ChatColor.GREEN + skinName + ChatColor.RESET + "...");

String[] skinData = MojangAPI.getSkin(skinName);

String botName = this.botName.endsWith("%") ? this.botName : this.botName + "%";

print("Creating " + (populationSize == 1 ? "new bot" : ChatColor.RED + NumberFormat.getInstance(Locale.US).format(populationSize) + ChatColor.RESET + " new bots")
Expand All @@ -125,7 +123,7 @@ private void runGeneration() throws InterruptedException {
Set<Bot> bots;

if (loadedProfiles == null) {
bots = manager.createBots(loc, botName, skinData, populationSize, NeuralNetwork.RANDOM);
bots = manager.createBots(loc, botName, skinName, populationSize, NeuralNetwork.RANDOM);
} else {
List<NeuralNetwork> networks = new ArrayList<>();
loadedProfiles.forEach(profile -> networks.add(NeuralNetwork.createNetworkFromProfile(profile)));
Expand All @@ -137,7 +135,7 @@ private void runGeneration() throws InterruptedException {
return;
}

bots = manager.createBots(loc, botName, skinData, networks);
bots = manager.createBots(loc, botName, skinName, networks);
}

bots.forEach(bot -> {
Expand Down
37 changes: 10 additions & 27 deletions src/main/java/net/nuggetmc/tplus/command/commands/BotCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.nuggetmc.tplus.command.annotation.Autofill;
import net.nuggetmc.tplus.command.annotation.Command;
import net.nuggetmc.tplus.command.annotation.OptArg;
import net.nuggetmc.tplus.ui.UIManager;
import net.nuggetmc.tplus.utils.ChatUtils;
import net.nuggetmc.tplus.utils.Debugger;
import org.bukkit.Bukkit;
Expand Down Expand Up @@ -179,9 +180,9 @@ public List<String> armorAutofill(CommandSender sender, String[] args) {
desc = "Information about loaded bots.",
autofill = "infoAutofill"
)
public void info(CommandSender sender, @Arg("bot-name") String name) {
if (name == null) {
sender.sendMessage(ChatColor.YELLOW + "Bot GUI coming soon!");
public void info(CommandSender sender, @OptArg("bot-name") String name) {
if (name == null && sender instanceof Player) {
UIManager.openBotListGUI((Player) sender);
return;
}

Expand All @@ -196,30 +197,11 @@ public void info(CommandSender sender, @Arg("bot-name") String name) {
return;
}

/*
* time created
* current life (how long it has lived for)
* health
* inventory
* current target
* current kills
* skin
* neural network values (network name if loaded, otherwise RANDOM)
*/

String botName = bot.getName();
String world = ChatColor.YELLOW + bot.getBukkitEntity().getWorld().getName();
Location loc = bot.getLocation();
String strLoc = ChatColor.YELLOW + formatter.format(loc.getBlockX()) + ", " + formatter.format(loc.getBlockY()) + ", " + formatter.format(loc.getBlockZ());
Vector vel = bot.getVelocity();
String strVel = ChatColor.AQUA + formatter.format(vel.getX()) + ", " + formatter.format(vel.getY()) + ", " + formatter.format(vel.getZ());

sender.sendMessage(ChatUtils.LINE);
sender.sendMessage(ChatColor.GREEN + botName);
sender.sendMessage(ChatUtils.BULLET_FORMATTED + "World: " + world);
sender.sendMessage(ChatUtils.BULLET_FORMATTED + "Position: " + strLoc);
sender.sendMessage(ChatUtils.BULLET_FORMATTED + "Velocity: " + strVel);
sender.sendMessage(ChatUtils.LINE);
List<String> botLore = bot.botLore();
sender.sendMessage("Statistics about bot " + ChatColor.GREEN + name + ChatColor.RESET + ":");
for(String str : botLore){
sender.sendMessage(str);
}
}

catch (Exception e) {
Expand All @@ -233,6 +215,7 @@ public List<String> infoAutofill(CommandSender sender, String[] args) {
return args.length == 2 ? manager.fetchNames() : null;
}


@Command(
name = "reset",
desc = "Remove all loaded bots."
Expand Down
66 changes: 66 additions & 0 deletions src/main/java/net/nuggetmc/tplus/ui/BotListGUI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package net.nuggetmc.tplus.ui;

import net.md_5.bungee.api.ChatColor;
import net.nuggetmc.tplus.TerminatorPlus;
import net.nuggetmc.tplus.bot.Bot;
import net.nuggetmc.tplus.ui.menu.buttons.Button;
import net.nuggetmc.tplus.ui.menu.menu.PaginatedMenu;
import net.nuggetmc.tplus.utils.ItemBuilder;
import net.nuggetmc.tplus.utils.PlayerUtils;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class BotListGUI extends PaginatedMenu {
@Override
public String getPagesTitle(Player player) {
return ChatColor.DARK_GREEN + "TerminatorPlus" + ChatColor.DARK_GRAY + " | Bots"; //for some reason getPage() breaks the gui if its over 1 ?!
}

@Override
public List<Button> getPaginatedButtons(Player player) {
Set<Bot> bots = TerminatorPlus.getInstance().getManager().fetch();
List<Button> buttons = new ArrayList<>();
int index = 0;
for (Bot bot : bots) {
buttons.add(new BotButton(++index,bots.size(), bot));
}
return buttons;
}

@Override
public List<Button> getPersistantSlots(Player player) {
return null;
}
private class BotButton extends Button {
private int index,total;
private Bot bot;

private BotButton(int index,int total,Bot bot1) {
this.index = index;
this.total = total;
this.bot = bot1;
}

@Override
public ItemStack getItem(Player player) {
return new ItemBuilder(PlayerUtils.getPlayerHead(bot.getSkinName())).name(ChatColor.GOLD + bot.getName() + ChatColor.RESET + " - Bot " + index + " out of " + total)
.lore(bot.botLore()).build();
}

@Override
public void onClick(Player player, int slot, ClickType clickType, InventoryClickEvent event) {
new BotManagerGUI(bot).open(player);
}

@Override
public int getSlot() {
return 0;
}
}
}
Loading