Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
byxiaobai committed Aug 18, 2020
1 parent 7e15587 commit fb55b6b
Show file tree
Hide file tree
Showing 42 changed files with 494 additions and 49 deletions.
14 changes: 10 additions & 4 deletions src/main/java/com/focess/betterai/BetterAI.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.focess.betterai;

import com.focess.betterai.api.command.Command;
import com.focess.betterai.command.BetterAICommand;
import com.focess.betterai.listener.EntityListener;
import com.focess.betterai.util.BetterAIConfiguration;
import com.focess.betterai.util.command.Command;
import com.focess.betterai.utils.BetterAIConfiguration;

import org.bukkit.entity.Animals;
import org.bukkit.plugin.java.JavaPlugin;

Expand All @@ -20,10 +21,15 @@ public static BetterAI getInstance() {

public void onEnable(){
instance = this;
loadConfig();

loadConfig();//加载配置文件
BetterAIConfiguration.loadDefault(this);

this.getServer().getPluginManager().registerEvents(new EntityListener(),this);
//注册事件

Command.register(new BetterAICommand());
//注册指令
}

private void loadConfig() {
Expand All @@ -36,6 +42,6 @@ private void loadConfig() {

@Override
public void onDisable() {

Command.unregisterAllCommand();
}
}
65 changes: 60 additions & 5 deletions src/main/java/com/focess/betterai/command/BetterAICommand.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
package com.focess.betterai.command;


import com.focess.betterai.util.command.Command;
import com.focess.betterai.util.command.PlayerCommandExecutor;
import com.focess.betterai.api.command.Command;
import com.focess.betterai.api.command.PlayerCommandExecutor;
import com.focess.betterai.utils.NMSUtil;
import com.focess.betterai.utils.PacketUtil;
import com.focess.betterai.utils.ZombieUtil;
import com.focess.betterai.zombie.AIZombie;
import com.focess.betterai.zombie.ZombieManager;
import com.focess.pathfinder.core.entity.NMSFocessEntity;
import com.focess.pathfinder.core.navigation.focess.FocessPathPoint;
import com.focess.pathfinder.entity.EntityManager;
import com.focess.pathfinder.entity.FocessEntity;
import com.focess.pathfinder.goal.WrappedGoal;
import com.google.common.collect.Lists;
import com.toolapi.utils.EntityUtil;

import org.bukkit.Bukkit;
import org.bukkit.block.Block;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.entity.Zombie;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.util.Vector;

import java.util.List;
import java.util.UUID;
Expand All @@ -26,16 +41,56 @@ protected List<String> getCompleteLists(CommandSender sender, String cmd, String
@Override
public void init() {
this.addExecutor(1,(PlayerCommandExecutor)(sender, args)->{
FocessEntity focessEntity = EntityManager.getFocessEntity(Bukkit.getEntity(UUID.fromString(args[0])));
NMSFocessEntity focessEntity = EntityManager.getFocessEntity(Bukkit.getEntity(UUID.fromString(args[0])));
for (WrappedGoal goal:focessEntity.getGoalSelector().getGoals())
sender.sendMessage(goal.getNmsGoal().getClass().getName() + ":" + goal.getPriority() + ":" + goal.getControls().toString());
},"show");
this.addExecutor(2,(PlayerCommandExecutor)(sender,args)->{
Integer pos = Integer.parseInt(args[1]);
FocessEntity focessEntity = EntityManager.getFocessEntity(Bukkit.getEntity(UUID.fromString(args[0])));
NMSFocessEntity focessEntity = EntityManager.getFocessEntity(Bukkit.getEntity(UUID.fromString(args[0])));
focessEntity.getGoalSelector().removeExactGoal(focessEntity.getGoalSelector().getGoals().get(pos));
sender.sendMessage("Successfully");
},"remove");

this.addExecutor(0,(PlayerCommandExecutor)(sender,args)->{
AIZombie aiZombie=ZombieUtil.spawnAIZombie(sender.getLocation());
sender.sendMessage("id:"+aiZombie.getEntityID());
},"create");//生成僵尸

this.addExecutor(1,(PlayerCommandExecutor)(sender,args)->{
int id=Integer.parseInt(args[0]);
AIZombie aiZombie=ZombieManager.INSTANCE.getZombieByEntityID(id);
aiZombie.getZombieNavigation().gotoPathPoint(new FocessPathPoint(0,sender.getLocation()));
sender.sendMessage("id:"+aiZombie.getEntityID());
},"move");//移动僵尸 bai move ID

this.addExecutor(1,(PlayerCommandExecutor)(sender,args)->{
int id=Integer.parseInt(args[0]);
AIZombie aiZombie=ZombieManager.INSTANCE.getZombieByEntityID(id);
aiZombie.getBukkitEntity().setVelocity(new Vector(0,0.5,0));
sender.sendMessage("id:"+aiZombie.getEntityID());
},"jump");//跳跃僵尸 bai jump ID

this.addExecutor(2,(PlayerCommandExecutor)(sender,args)->{
int id=Integer.parseInt(args[0]);
int range=Integer.parseInt(args[1]);
AIZombie aiZombie=ZombieManager.INSTANCE.getZombieByEntityID(id);
Player player=EntityUtil.getNearestPlayerInRange(aiZombie.getBukkitEntity(), range);
if(player==null) {
sender.sendMessage("null");
}else
sender.sendMessage("name:"+player.getName());
},"find");//寻找最近的玩家 bai find ID 范围

this.addExecutor(2,(PlayerCommandExecutor)(sender,args)->{
int id=Integer.parseInt(args[0]);
int breaksit=Integer.parseInt(args[1]);
Block block=sender.getTargetBlock(10);
System.out.println("block:"+block.getY());
if(block!=null) {
PacketUtil.sendBreakAnimationPacket(id, block, breaksit);
}
},"break");
}

@Override
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/com/focess/betterai/goal/ZombieAttackPlayerGoal.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package com.focess.betterai.goal;

import com.focess.pathfinder.core.entity.NMSFocessEntity;
import com.focess.pathfinder.entity.EntityManager;
import com.focess.pathfinder.entity.FocessEntity;
import com.focess.pathfinder.goal.Goal;
import org.bukkit.GameMode;
import org.bukkit.entity.*;

public class ZombieAttackPlayerGoal extends Goal {

private final FocessEntity zombie;
private final NMSFocessEntity zombie;

private static final int TIMEOUT = 30;
private static final int TIMEOUT = 30;

private Entity target = null;
private int time = 0;
Expand Down Expand Up @@ -46,6 +46,9 @@ public void tick() {
((Creature)this.zombie.getBukkitEntity()).setTarget((LivingEntity) this.target);
}

/**
* 寻找进攻目标
*/
private void searchTarget() {
this.zombie.getBukkitEntity().getNearbyEntities(20,20,20).stream().filter(ZombieAttackPlayerGoal::isPlayer).sorted((i, j)->{
double distance1 = this.zombie.getBukkitEntity().getLocation().distance(i.getLocation());
Expand All @@ -54,6 +57,11 @@ private void searchTarget() {
}).findFirst().ifPresent((entity)->target = entity);
}

/**
* 判断是否为玩家
* @param entity
* @return
*/
private static boolean isPlayer(Entity entity) {
if (!entity.getType().equals(EntityType.PLAYER))
return false;
Expand Down
22 changes: 3 additions & 19 deletions src/main/java/com/focess/betterai/goal/ZombieBlockGoal.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.wrappers.BlockPosition;
import com.focess.betterai.utils.PacketUtil;
import com.focess.pathfinder.core.util.NMSManager;
import org.bukkit.Bukkit;
import org.bukkit.Location;
Expand Down Expand Up @@ -45,32 +46,15 @@ public void start() {
@Override
public void stop() {
super.stop();
sendBreakAnimationPacket(-1);
PacketUtil.sendBreakAnimationPacket(this.zombie.getID(),this.block,-1);
}

private void sendBreakAnimationPacket(int breakSit) {
for (World world:Bukkit.getWorlds())
for (Player player:world.getEntitiesByClass(Player.class)) {
PacketContainer packet = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.BLOCK_BREAK_ANIMATION);
packet.getIntegers().write(0,this.zombie.getID()).write(1,breakSit);
packet.getBlockPositionModifier().write(0,new BlockPosition(this.block.getLocation().toVector()));
if (this.block.getLocation().distanceSquared(player.getLocation()) < 1024D) {
try {
ProtocolLibrary.getProtocolManager().sendServerPacket(player,packet);
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}

@Override
public void tick() {
super.tick();
this.breakTime++;
int breakSit = (int)(this.breakTime / 3.0F);
if (breakSit != this.breakSit) {
this.sendBreakAnimationPacket(breakSit);
PacketUtil.sendBreakAnimationPacket(this.zombie.getID(),this.block,breakSit);
this.breakSit = breakSit;
}
if (this.breakTime == 30) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.focess.betterai.goal;

import com.focess.betterai.util.BetterAIConfiguration;
import com.focess.betterai.utils.BetterAIConfiguration;
import com.focess.pathfinder.core.entity.NMSFocessEntity;
import com.focess.pathfinder.entity.EntityManager;
import com.focess.pathfinder.entity.FocessEntity;
import com.focess.pathfinder.goal.Goal;
import org.bukkit.Location;
import org.bukkit.Material;
Expand All @@ -15,7 +15,7 @@
public class ZombieBlockPlaceGoal extends Goal {


private final FocessEntity zombie;
private final NMSFocessEntity zombie;

public ZombieBlockPlaceGoal(Zombie zombie) {
this.zombie = EntityManager.getFocessEntity(zombie);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.focess.betterai.goal;

import com.focess.pathfinder.core.entity.NMSFocessEntity;
import com.focess.pathfinder.entity.EntityManager;
import com.focess.pathfinder.entity.FocessEntity;
import com.focess.pathfinder.goal.Goal;
import org.bukkit.Material;
import org.bukkit.block.Block;
Expand All @@ -12,7 +12,7 @@
import org.bukkit.util.Vector;

public abstract class ZombieInteractBlockGoal extends Goal {
protected final FocessEntity zombie;
protected final NMSFocessEntity zombie;
protected Block block;
private boolean isStop;
private float x;
Expand Down
20 changes: 12 additions & 8 deletions src/main/java/com/focess/betterai/listener/EntityListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import com.focess.betterai.goal.ZombieAttackPlayerGoal;
import com.focess.betterai.goal.ZombieBlockGoal;
import com.focess.betterai.goal.ZombieBlockPlaceGoal;
import com.focess.betterai.util.BetterAIConfiguration;
import com.focess.betterai.utils.BetterAIConfiguration;
import com.focess.pathfinder.core.entity.NMSFocessEntity;
import com.focess.pathfinder.entity.EntityManager;
import com.focess.pathfinder.entity.FocessEntity;
import com.focess.pathfinder.goal.FocessGoalItem;
import com.focess.pathfinder.goal.GoalSelector;
import com.focess.pathfinder.goal.WrappedGoal;
Expand All @@ -27,7 +27,7 @@ public void onEntitySpawn(CreatureSpawnEvent event) {
if (event.getEntity() instanceof Zombie && BetterAIConfiguration.isEnableZombie()) {
event.getEntity().getEquipment();
event.getEntity().setCanPickupItems(true);
FocessEntity entity = EntityManager.getFocessEntity(event.getEntity());
NMSFocessEntity entity = EntityManager.getFocessEntity(event.getEntity());
GoalSelector goalSelector = entity.getGoalSelector();
// for (WrappedGoal wrappedGoal:goalSelector.getGoals())
// if (wrappedGoal.getPriority() == 2 && wrappedGoal.getGoalItems().contains(Goals.TARGET.NEAREST_ATTACKABLE_TARGET))
Expand All @@ -37,13 +37,17 @@ public void onEntitySpawn(CreatureSpawnEvent event) {
.clear()
.writeEntityInsentient(WrappedEntityInsentient.getWrappedEntityInsentient((Mob) event.getEntity()))
.build(0, false));

//躲避太阳
goalSelector.addGoal(Goals.MOVE.FLEE_SUN.clear().writeEntityCreature(WrappedEntityCreature.getWrappedEntityCreature((Creature) event.getEntity())).writeDouble(1.0).build(3, false));
goalSelector.addGoal(Goals.RESTRICT_SUN.clear().writeEntityCreature(WrappedEntityCreature.getWrappedEntityCreature((Creature) event.getEntity())).build(2, false));
goalSelector.addGoal(new FocessGoalItem(new ZombieBlockGoal((Zombie) event.getEntity())).build(1,false));
goalSelector.addGoal(new FocessGoalItem(new ZombieBlockPlaceGoal((Zombie) event.getEntity())).build(1,
false));
goalSelector.addGoal(new FocessGoalItem(new ZombieAttackPlayerGoal((Zombie)event.getEntity())).build(0,
false));

//破坏方块
//goalSelector.addGoal(new FocessGoalItem(new ZombieBlockGoal((Zombie) event.getEntity())).build(1,false));
//放置方块
//goalSelector.addGoal(new FocessGoalItem(new ZombieBlockPlaceGoal((Zombie) event.getEntity())).build(1,false));
//攻击玩家
//goalSelector.addGoal(new FocessGoalItem(new ZombieAttackPlayerGoal((Zombie)event.getEntity())).build(0,false));
}
}
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.focess.betterai.util.command;
package com.focess.betterai.api.command;

import com.focess.betterai.BetterAI;
import com.google.common.collect.Lists;
Expand Down Expand Up @@ -39,11 +39,18 @@ private static void getCommandMap() throws Exception {
public static void register(final Command command) {
command.registered = true;
Command.commands.add(command);
command.init();
}

public static void unregister(final Command command) {
command.unregister();
}

public static void unregisterAllCommand() {
for(Command com:commands) {
com.unregister();
}
}

public final void addExecutor(final int count, final CommandExecutor executor, final String... subCommands) {
this.executors.add(new Executor(count, subCommands).addExecutor(executor));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.focess.betterai.util.command;
package com.focess.betterai.api.command;

import org.bukkit.command.CommandSender;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.focess.betterai.util.command;
package com.focess.betterai.api.command;

import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.focess.betterai.util.command;
package com.focess.betterai.api.command;

import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
Expand Down
Loading

0 comments on commit fb55b6b

Please sign in to comment.