Merge pull request #54 from Badbird5907/master

Target specific player + github actions fix
This commit is contained in:
Badbird5907
2022-08-11 21:32:58 -04:00
committed by GitHub
9 changed files with 168 additions and 31 deletions

View File

@@ -36,16 +36,33 @@ jobs:
with:
languages: ${{ matrix.language }}
- name: Autobuild
uses: github/codeql-action/autobuild@v1
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
- name: Upload a Build Artifact
uses: actions/upload-artifact@v2.2.4
if: success()
with:
name: TerminatorPlus
path: target/
gradle:
strategy:
matrix:
os: [ ubuntu-latest ]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-java@v3
with:
distribution: temurin
java-version: 17
- name: Make gradlew executable
run: chmod +x ./gradlew
- name: Setup Gradle
uses: gradle/gradle-build-action@v2
- name: Execute Gradle build
run: ./gradlew build
- name: Upload a Build Artifact
uses: actions/upload-artifact@v2.2.4
if: success()
with:
name: TerminatorPlus
path: build/libs/

View File

@@ -46,5 +46,8 @@ public interface BotManager {
Terminator getBot(int entityId);
boolean isMobTarget();
void setMobTarget(boolean mobTarget);
}

View File

@@ -11,6 +11,8 @@ import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Vector;
import java.util.UUID;
public interface Terminator {
String getBotName();
@@ -110,4 +112,8 @@ public interface Terminator {
void renderBot(Object packetListener, boolean login);
void setOnFirePackets(boolean onFire);
UUID getTargetPlayer();
void setTargetPlayer(UUID target);
}

View File

@@ -11,9 +11,10 @@ public enum EnumTargetGoal {
NEAREST_BOT("Locate the nearest bot."),
NEAREST_BOT_DIFFER("Locate the nearest bot with a different username."),
NEAREST_BOT_DIFFER_ALPHA("Locate the nearest bot with a different username after filtering out non-alpha characters."),
PLAYER("Target a single player. Defaults to NEAREST_VULNERABLE_PLAYER if no player found."),
NONE("No target goal.");
private static final Map<String, EnumTargetGoal> VALUES = new HashMap<String, EnumTargetGoal>() {
private static final Map<String, EnumTargetGoal> VALUES = new HashMap<>() {
{
this.put("none", NONE);
this.put("nearestvulnerableplayer", NEAREST_VULNERABLE_PLAYER);

View File

@@ -10,6 +10,7 @@ import net.nuggetmc.tplus.api.agent.legacyagent.ai.NeuralNetwork;
import net.nuggetmc.tplus.api.event.BotDamageByPlayerEvent;
import net.nuggetmc.tplus.api.event.BotDeathEvent;
import net.nuggetmc.tplus.api.event.BotFallDamageEvent;
import net.nuggetmc.tplus.api.event.TerminatorLocateTargetEvent;
import net.nuggetmc.tplus.api.utils.MathUtils;
import net.nuggetmc.tplus.api.utils.PlayerUtils;
import org.bukkit.*;
@@ -1115,10 +1116,12 @@ public class LegacyAgent extends Agent {
this.goal = goal;
}
public LivingEntity locateTarget(Terminator bot, Location loc) {
public LivingEntity locateTarget(Terminator bot, Location loc, EnumTargetGoal... targetGoal) {
LivingEntity result = null;
switch (goal) {
EnumTargetGoal g = goal;
if (targetGoal.length > 0) g = targetGoal[0];
switch (g) {
default:
return null;
@@ -1205,9 +1208,20 @@ public class LegacyAgent extends Agent {
}
}
}
case PLAYER: { //Target a single player. Defaults to NEAREST_VULNERABLE_PLAYER if no player found.
if (bot.getTargetPlayer() != null) {
Player player = Bukkit.getPlayer(bot.getTargetPlayer());
if (player != null) {
return player;
}
}
return locateTarget(bot, loc, EnumTargetGoal.NEAREST_VULNERABLE_PLAYER);
}
}
return result;
TerminatorLocateTargetEvent event = new TerminatorLocateTargetEvent(bot, result);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) return null;
return event.getTarget();
}
private boolean validateCloserEntity(LivingEntity entity, Location loc, LivingEntity result) {

View File

@@ -0,0 +1,52 @@
package net.nuggetmc.tplus.api.event;
import net.nuggetmc.tplus.api.Terminator;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
public class TerminatorLocateTargetEvent extends Event implements Cancellable {
private static final HandlerList handlerList = new HandlerList();
private Terminator terminator;
private LivingEntity target;
private boolean cancelled;
public TerminatorLocateTargetEvent(Terminator terminator, LivingEntity target) {
this.terminator = terminator;
this.target = target;
}
public static HandlerList getHandlerList() {
return handlerList;
}
@Override
public HandlerList getHandlers() {
return handlerList;
}
public Terminator getTerminator() {
return terminator;
}
public LivingEntity getTarget() {
return target;
}
public void setTarget(LivingEntity target) {
this.target = target;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancel) {
cancelled = cancel;
}
}

View File

@@ -73,7 +73,7 @@ public class Bot extends ServerPlayer implements Terminator {
private byte jumpTicks;
private byte noFallTicks;
private boolean ignoredByMobs = true;
private UUID targetPlayer = null;
private Bot(MinecraftServer minecraftServer, ServerLevel worldServer, GameProfile profile) {
super(minecraftServer, worldServer, profile, null);
@@ -373,6 +373,16 @@ public class Bot extends ServerPlayer implements Terminator {
//sendPacket(new ClientboundSetEntityDataPacket(getId(), entityData, false));
}
@Override
public UUID getTargetPlayer() {
return targetPlayer;
}
@Override
public void setTargetPlayer(UUID target) {
this.targetPlayer = target;
}
@Override
public boolean isBotOnFire() {
return fireTicks != 0;

View File

@@ -34,7 +34,7 @@ public class BotManagerImpl implements BotManager, Listener {
private final NumberFormat numberFormat;
public boolean joinMessages = false;
private boolean mobTarget = false;
public BotManagerImpl() {
this.agent = new LegacyAgent(this, TerminatorPlus.getInstance());
this.bots = ConcurrentHashMap.newKeySet(); //should fix concurrentmodificationexception
@@ -139,6 +139,7 @@ public class BotManagerImpl implements BotManager, Listener {
} else if (i > 1) {
bot.setVelocity(randomVelocity().multiply(f));
}
bot.setIgnoredByMobs(!mobTarget);
bots.add(bot);
i++;
@@ -194,6 +195,16 @@ public class BotManagerImpl implements BotManager, Listener {
return null;
}
@Override
public boolean isMobTarget() {
return mobTarget;
}
@Override
public void setMobTarget(boolean mobTarget) {
this.mobTarget = mobTarget;
}
@EventHandler
public void onJoin(PlayerJoinEvent event) {
ServerGamePacketListenerImpl connection = ((CraftPlayer) event.getPlayer()).getHandle().connection;

View File

@@ -267,29 +267,47 @@ public class BotCommand extends CommandInstance {
String extra = ChatColor.GRAY + " [" + ChatColor.YELLOW + "/bot settings" + ChatColor.GRAY + "]";
if (arg1 == null || (!arg1.equals("setgoal"))) {
if (arg1 == null || ((!arg1.equalsIgnoreCase("setgoal")) && !arg1.equalsIgnoreCase("mobtarget") && !arg1.equalsIgnoreCase("playertarget"))) {
sender.sendMessage(ChatUtils.LINE);
sender.sendMessage(ChatColor.GOLD + "Bot Settings" + extra);
sender.sendMessage(ChatUtils.BULLET_FORMATTED + ChatColor.YELLOW + "setgoal" + ChatUtils.BULLET_FORMATTED + "Set the global bot target selection method.");
sender.sendMessage(ChatUtils.BULLET_FORMATTED + ChatColor.YELLOW + "mobtarget" + ChatUtils.BULLET_FORMATTED + "Allow all future bots spawned to be targetted by hostile mobs.");
sender.sendMessage(ChatUtils.BULLET_FORMATTED + ChatColor.YELLOW + "mobtarget" + ChatUtils.BULLET_FORMATTED + "Allow all future bots spawned to be targeted by hostile mobs.");
sender.sendMessage(ChatUtils.LINE);
return;
}
EnumTargetGoal goal = EnumTargetGoal.from(arg2 == null ? "" : arg2);
if (arg1.equalsIgnoreCase("setgoal")) {
EnumTargetGoal goal = EnumTargetGoal.from(arg2 == null ? "" : arg2);
if (goal == null) {
sender.sendMessage(ChatUtils.LINE);
sender.sendMessage(ChatColor.GOLD + "Goal Selection Types" + extra);
Arrays.stream(EnumTargetGoal.values()).forEach(g -> sender.sendMessage(ChatUtils.BULLET_FORMATTED + ChatColor.YELLOW + g.name().replace("_", "").toLowerCase()
+ ChatUtils.BULLET_FORMATTED + g.description()));
sender.sendMessage(ChatUtils.LINE);
return;
if (goal == null) {
sender.sendMessage(ChatUtils.LINE);
sender.sendMessage(ChatColor.GOLD + "Goal Selection Types" + extra);
Arrays.stream(EnumTargetGoal.values()).forEach(g -> sender.sendMessage(ChatUtils.BULLET_FORMATTED + ChatColor.YELLOW + g.name().replace("_", "").toLowerCase()
+ ChatUtils.BULLET_FORMATTED + g.description()));
sender.sendMessage(ChatUtils.LINE);
return;
}
agent.setTargetType(goal);
sender.sendMessage("The global bot goal has been set to " + ChatColor.BLUE + goal.name() + ChatColor.RESET + ".");
} else if (arg1.equalsIgnoreCase("mobtarget")) {
manager.setMobTarget(!manager.isMobTarget());
sender.sendMessage("Mob targeting is now " + (manager.isMobTarget() ? ChatColor.GREEN + "enabled" : ChatColor.RED + "disabled") + ChatColor.RESET + ". (for all future bots)");
} else if (arg1.equalsIgnoreCase("playertarget")) {
if (args.size() < 2) {
sender.sendMessage(ChatColor.RED + "You must specify a player name!");
return;
}
String playerName = args.get(1);
Player player = Bukkit.getPlayer(playerName);
if (player == null) {
sender.sendMessage(ChatColor.RED + "Could not find player " + ChatColor.YELLOW + playerName + ChatColor.RED + "!");
return;
}
for (Terminator fetch : manager.fetch()) {
fetch.setTargetPlayer(player.getUniqueId());
}
sender.sendMessage("All spawned bots are now set to target " + ChatColor.BLUE + player.getName() + ChatColor.RESET + ". They will target the closest player if they can't be found.\nYou may need to set the goal to PLAYER.");
}
agent.setTargetType(goal);
sender.sendMessage("The global bot goal has been set to " + ChatColor.BLUE + goal.name() + ChatColor.RESET + ".");
}
@Autofill
@@ -306,10 +324,15 @@ public class BotCommand extends CommandInstance {
if (args.length == 2) {
output.add("setgoal");
output.add("mobtarget");
} else if (args.length == 3) {
if (args[1].equalsIgnoreCase("setgoal")) {
Arrays.stream(EnumTargetGoal.values()).forEach(goal -> output.add(goal.name().replace("_", "").toLowerCase()));
}
if (args[1].equalsIgnoreCase("mobtarget")) {
output.add("true");
output.add("false");
}
}
return output;