Skip to content

Commit

Permalink
Prevent AI from breaking bed through bed defense
Browse files Browse the repository at this point in the history
  • Loading branch information
boiscljo committed May 23, 2022
1 parent 3f673d5 commit 9c20999
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void doGoal() {
Player aiPlayer = fakeDeathTrait.getNpcEntity();
var distance = targetBlock.getLocation().distance(aiPlayer.getLocation());
if (distance < 3) {
fakeDeathTrait.blockPlace().breakBlock(targetBlock);
fakeDeathTrait.blockPlace().breakBlock(fakeDeathTrait.blockPlace().viewedBlock(targetBlock));
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -66,7 +67,6 @@ public boolean placeBlockIfPossible(Location currentLocation) {
Player aiPlayer = (Player) npc.getEntity();
ItemStack blockToPlace = getBlock(aiPlayer.getInventory());


if (blockToPlace != null) {

var against = getAgainst(block);
Expand All @@ -91,26 +91,54 @@ public boolean placeBlockIfPossible(Location currentLocation) {
return false;
}

public Block viewedBlock(Block b) {
Player aiPlayer = (Player) getNPC().getEntity();
if (aiPlayer == null)
return null;
World w = aiPlayer.getWorld();
var rayTraceCheck = w.rayTraceBlocks(aiPlayer.getEyeLocation(),
aiPlayer.getEyeLocation().subtract(b.getLocation()).getDirection(),
10);

return rayTraceCheck.getHitBlock();
}

public boolean isBreakableBlock(Block b) {
if (isBreaking())
return false;
Player aiPlayer = (Player) npc.getEntity();
Game g = Main.getInstance().getGameOfPlayer(aiPlayer);
if (g == null)
return false;
return g.isBlockAddedDuringGame(b.getLocation());
if (!g.isBlockAddedDuringGame(b.getLocation()))
return false;
return isBlockVisible(b);
}

public boolean isBlockVisible(Block b) {
Player aiPlayer = (Player) getNPC().getEntity();
if (aiPlayer == null)
return false;
World w = aiPlayer.getWorld();
var rayTraceCheck = w.rayTraceBlocks(aiPlayer.getEyeLocation(),
aiPlayer.getEyeLocation().subtract(b.getLocation()).getDirection(),
10);

return rayTraceCheck.getHitBlock().equals(b);
}

public void breakBlock(Block b) {
Player aiPlayer = (Player) npc.getEntity();
var destroySpeed = b.getDestroySpeed(aiPlayer.getItemInHand());
if (b != null) {
Player aiPlayer = (Player) npc.getEntity();
var destroySpeed = b.getDestroySpeed(aiPlayer.getItemInHand());

blockBreakerCooldown = (int) (100 / destroySpeed);
blockBreakerTotal = (int) (100 / destroySpeed);
blockToBreak = b;
startBreak = aiPlayer.getLocation();
blockBreakerCooldown = (int) (100 / destroySpeed);
blockBreakerTotal = (int) (100 / destroySpeed);
blockToBreak = b;
startBreak = aiPlayer.getLocation();

npc.getNavigator().cancelNavigation();
npc.getNavigator().cancelNavigation();
}
}

public boolean isBreaking() {
Expand Down Expand Up @@ -158,8 +186,7 @@ public boolean isEmpty(Block testBlock) {
public ItemStack getBlock(Inventory inv) {

ItemStack is = null;
if (useStores)
{
if (useStores) {
for (var item : inv.getContents()) {
if (item != null) {
if (item.getType().isBlock())
Expand All @@ -171,9 +198,7 @@ public ItemStack getBlock(Inventory inv) {
if (is == null) {
Logger.trace("NPC {} needs blocks", getNPC().getName());
}
}
else
{
} else {
return new ItemStack(Material.OAK_PLANKS);
}
return is;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public Block findSecondBedBlock(Block b) {
public boolean isAvailable() {
if (this.fakeDeathTrait.blockPlace() == null)
return false;
this.fakeDeathTrait.blockPlace().getBlock(this.fakeDeathTrait.getNpcEntity().getInventory());
if (this.fakeDeathTrait.blockPlace().isInNeedOfBlock())
return false;
if (bedBlock == null) {
Expand Down Expand Up @@ -109,9 +110,7 @@ public void doGoal() {
this.fakeDeathTrait.getNPC().getEntity()
.teleport(toPlace.getLocation().toBlockLocation().add(0.5, 1, 0.5));
}
}
else
{
} else {
this.fakeDeathTrait.getNPC().getNavigator().setTarget(toPlace.getLocation());
}
}
Expand Down

0 comments on commit 9c20999

Please sign in to comment.