Skip to content

Commit

Permalink
Merge pull request #21 from APDevTeam/experimental
Browse files Browse the repository at this point in the history
Experimental 1.13+ support and solved some NPEs on logout of a siege leader
  • Loading branch information
TylerS1066 authored Mar 28, 2021
2 parents 336e5f5 + d915978 commit 2b4d330
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 31 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>net.countercraft.movecraft.warfare</groupId>
<artifactId>Movecraft-Warfare</artifactId>
<version>0.5.2</version>
<version>0.5.3</version>
<packaging>jar</packaging>

<name>Movecraft-Warfare</name>
Expand Down Expand Up @@ -82,7 +82,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.10.2-R0.1-SNAPSHOT</version>
<version>1.13.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<!--Movecraft-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,77 @@
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

public class BlockListener implements Listener {
final int[] fragileBlocks = new int[]{26, 34, 50, 55, 63, 64, 65, 68, 69, 70, 71, 72, 75, 76, 77, 93, 94, 96, 131, 132, 143, 147, 148, 149, 150, 151, 171, 323, 324, 330, 331, 356, 404};
private final HashSet<Material> fragileBlocks;
private long lastDamagesUpdate = 0;

public BlockListener() {
fragileBlocks = new HashSet<>();
for(Material m : Material.values()) {
String name = m.name();
if(name.contains("SIGN") ||
name.contains("DOOR") ||
name.contains("PRESSURE_PLATE") ||
name.contains("CARPET") ||
name.contains("BED") ||
name.contains("BUTTON") ||
name.contains("TORCH") ||
name.contains("TRIPWIRE") ||
name.contains("PISTON_HEAD") ||
name.contains("LEVER") ||
name.contains("LADDER") ||
name.contains("REDSTONE") ||
name.contains("REPEATER") ||
name.contains("COMPARATOR") ||
name.contains("DAYLIGHT_DETECTOR")) {
fragileBlocks.add(m);
}
}

fragileBlocks.remove(Material.REDSTONE_BLOCK);
}

@EventHandler(priority = EventPriority.NORMAL)
public void explodeEvent(EntityExplodeEvent e) {
List<Assault> assaults = MovecraftWarfare.getInstance().getAssaultManager() != null ? MovecraftWarfare.getInstance().getAssaultManager().getAssaults() : null;
if (assaults == null || assaults.size() == 0) {
if (assaults == null || assaults.size() == 0)
return;
}

for (final Assault assault : assaults) {
if(e.getLocation().getWorld() != assault.getWorld())
continue;

Iterator<Block> i = e.blockList().iterator();
while (i.hasNext()) {
Block b = i.next();
if (b.getWorld() != assault.getWorld())
continue;

// first see if it is outside the region area
Location l = b.getLocation();
if (!MovecraftWorldGuard.getInstance().getWGUtils().regionContains(assault.getRegionName(), l))
continue;

// first see if it is outside the destroyable area
MovecraftLocation min = assault.getMinPos();
MovecraftLocation max = assault.getMaxPos();

// remove it outside assault area
if (l.getBlockX() < min.getX() ||
l.getBlockX() > max.getX() ||
l.getBlockZ() < min.getZ() ||
l.getBlockZ() > max.getZ() ||
!Config.AssaultDestroyableBlocks.contains(b.getType()) ||
Arrays.binarySearch(fragileBlocks, b.getRelative(BlockFace.SOUTH).getTypeId()) >= 0 ||
Arrays.binarySearch(fragileBlocks, b.getRelative(BlockFace.DOWN).getTypeId()) >= 0 ||
Arrays.binarySearch(fragileBlocks, b.getRelative(BlockFace.UP).getTypeId()) >= 0 ||
Arrays.binarySearch(fragileBlocks, b.getRelative(BlockFace.EAST).getTypeId()) >= 0 ||
Arrays.binarySearch(fragileBlocks, b.getRelative(BlockFace.WEST).getTypeId()) >= 0 ||
Arrays.binarySearch(fragileBlocks, b.getRelative(BlockFace.NORTH).getTypeId()) >= 0) {
l.getBlockZ() > max.getZ())
i.remove();
}

// remove if not destroyable
if(!Config.AssaultDestroyableBlocks.contains(b.getType()))
i.remove();

// remove if fragile
if(isFragile(b))
i.remove();

// whether or not you actually destroyed the block, add to damages
long damages = assault.getDamages() + Config.AssaultDamagesPerBlock;
Expand Down Expand Up @@ -88,4 +114,24 @@ public void run() {
}
}
}

private boolean isFragile(@NotNull Block base) {
for(Block b : getNearbyBlocks(base)) {
if(fragileBlocks.contains(b.getType()))
return true;
}
return false;
}

@NotNull
private HashSet<Block> getNearbyBlocks(@NotNull Block b) {
HashSet<Block> blocks = new HashSet<>();
blocks.add(b.getRelative(BlockFace.SOUTH));
blocks.add(b.getRelative(BlockFace.DOWN));
blocks.add(b.getRelative(BlockFace.UP));
blocks.add(b.getRelative(BlockFace.EAST));
blocks.add(b.getRelative(BlockFace.WEST));
blocks.add(b.getRelative(BlockFace.NORTH));
return blocks;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void run() {
return;
}
siege.setJustCommenced(false);
@NotNull Player siegeLeader = Movecraft.getInstance().getServer().getPlayer(siege.getPlayerUUID());
@Nullable Player siegeLeader = Movecraft.getInstance().getServer().getPlayer(siege.getPlayerUUID());
@Nullable Craft siegeCraft = CraftManager.getInstance().getCraftByPlayer(siegeLeader);

if(timeLeft > 10) {
Expand All @@ -47,7 +47,7 @@ public void run() {
else {
Bukkit.getServer().broadcastMessage(String.format(
I18nSupport.getInternationalisedString("Siege - Flagship Not In Box"),
siege.getName(), siegeLeader.getDisplayName())
siege.getName(), getSiegeLeaderName(siegeLeader))
+ formatMinutes(timeLeft));
}
}
Expand All @@ -61,9 +61,9 @@ public void run() {
}
}

private void endSiege(@Nullable Craft siegeCraft, @NotNull Player siegeLeader) {
if (leaderPilotingShip(siegeCraft)) {
if(MovecraftWorldGuard.getInstance().getWGUtils().craftFullyInRegion(siege.getAttackRegion(), siegeLeader.getWorld(), siegeCraft)) {
private void endSiege(@Nullable Craft siegeCraft, @Nullable Player siegeLeader) {
if (leaderPilotingShip(siegeCraft) && siegeLeader != null) {
if(MovecraftWorldGuard.getInstance().getWGUtils().craftFullyInRegion(siege.getAttackRegion(), siegeCraft.getW(), siegeCraft)) {
Bukkit.getServer().broadcastMessage(String.format(I18nSupport.getInternationalisedString("Siege - Siege Success"),
siege.getName(), siegeLeader.getDisplayName()));
winSiege(siegeLeader);
Expand All @@ -81,24 +81,25 @@ private void endSiege(@Nullable Craft siegeCraft, @NotNull Player siegeLeader) {
private void winSiege(@NotNull Player siegeLeader) {
Bukkit.getPluginManager().callEvent(new SiegeWinEvent(siege));
MovecraftWorldGuard.getInstance().getWGUtils().clearAndSetOwnership(siege.getCaptureRegion(), siegeLeader.getWorld(), siege.getPlayerUUID());
processCommands(siegeLeader, true);
processCommands(siegeLeader.getName(), true);
}

private void failSiege(@NotNull Player siegeLeader) {
private void failSiege(@Nullable Player siegeLeader) {
Bukkit.getPluginManager().callEvent(new SiegeLoseEvent(siege));
String name = getSiegeLeaderName(siegeLeader);
Bukkit.getServer().broadcastMessage(String.format(I18nSupport.getInternationalisedString("Siege - Siege Failure"),
siege.getName(), siegeLeader.getDisplayName()));
siege.getName(), name));

processCommands(siegeLeader, false);
processCommands(name, false);
}

private void processCommands(@NotNull Player siegeLeader, boolean win) {
private void processCommands(@NotNull String siegeLeader, boolean win) {
List<String> commands = win ? siege.getCommandsOnWin() : siege.getCommandsOnLose();
for (String command : commands) {
Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), command
.replaceAll("%r", siege.getCaptureRegion())
.replaceAll("%c", "" + siege.getCost())
.replaceAll("%l", siegeLeader.getName()));
.replaceAll("%l", siegeLeader));
}
}

Expand All @@ -108,4 +109,18 @@ private boolean leaderPilotingShip(@Nullable Craft siegeCraft) {
else
return siege.getCraftsToWin().contains(siegeCraft.getType().getCraftName());
}

private String getSiegeLeaderName(@Nullable Player siegeLeader) {
String name;
if(siegeLeader == null) {
name = Bukkit.getOfflinePlayer(siege.getPlayerUUID()).getName();

if(name == null)
name = "null"; // Note, there's not a better way to deal with this sadly
}
else
name = siegeLeader.getDisplayName();

return name;
}
}
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Movecraft-Warfare
depend: [Movecraft, Movecraft-Repair, WorldGuard, WorldEdit]
version: ${project.version}
api-version: 1.10
api-version: 1.13
description: Movecraft Warfare Addon
authors: [TylerS1066, cccm5, eirik1996, BaccaYarro]
website: https://github.com/APDevTeam/Movecraft-Warfare
Expand Down

0 comments on commit 2b4d330

Please sign in to comment.