Skip to content

Commit

Permalink
Enforce minimum api-version and fix NPE in WorldEditHook.
Browse files Browse the repository at this point in the history
  • Loading branch information
totemo committed Jan 7, 2021
1 parent 5e9c283 commit ed69a27
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 53 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>net.sothatsit</groupId>
<artifactId>blockstore</artifactId>
<version>1.6.1</version>
<version>1.7.0</version>
<packaging>jar</packaging>

<properties>
Expand Down
84 changes: 47 additions & 37 deletions src/main/java/net/sothatsit/blockstore/BlockStore.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,37 @@
package net.sothatsit.blockstore;

import net.sothatsit.blockstore.chunkstore.ChunkLoc;
import net.sothatsit.blockstore.chunkstore.ChunkManager;
import org.bukkit.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.*;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockGrowEvent;
import org.bukkit.event.block.BlockPistonExtendEvent;
import org.bukkit.event.block.BlockPistonRetractEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.event.world.ChunkLoadEvent;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;
import net.sothatsit.blockstore.chunkstore.ChunkLoc;
import net.sothatsit.blockstore.chunkstore.ChunkManager;

public class BlockStore extends JavaPlugin implements Listener {

private static BlockStore instance;
private final Map<String, ChunkManager> managers = new ConcurrentHashMap<>();
private final BlockStoreConfig blockStoreConfig = new BlockStoreConfig();
Expand All @@ -33,12 +43,12 @@ public void onEnable() {
blockStoreConfig.reload();

Bukkit.getPluginManager().registerEvents(this, this);

getCommand("blockstore").setExecutor(new BlockStoreCommand());

if (Bukkit.getPluginManager().getPlugin("WorldEdit") != null) {
Logger logger = getLogger();

try {
WorldEditHook hook = new WorldEditHook();

Expand All @@ -57,7 +67,7 @@ public void onEnable() {
}
}
}

@Override
public void onDisable() {
for (ChunkManager manager : managers.values()) {
Expand All @@ -71,9 +81,9 @@ public void onDisable() {
public BlockStoreConfig getBlockStoreConfig() {
return blockStoreConfig;
}
public ChunkManager getManager(String world) {
return managers.get(world);

public ChunkManager getManager(String worldName) {
return managers.computeIfAbsent(worldName, key -> new ChunkManager(Bukkit.getWorld(key)));
}

public ChunkManager getManager(Location location) {
Expand All @@ -83,35 +93,35 @@ public ChunkManager getManager(Location location) {
public ChunkManager getManager(World world) {
return managers.computeIfAbsent(world.getName(), key -> new ChunkManager(world));
}

public Map<String, ChunkManager> getChunkManagers() {
return managers;
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockGrow(BlockGrowEvent event) {
BlockStoreApi.setPlaced(event.getBlock(), false);
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockBreak(BlockBreakEvent event) {
BlockStoreApi.setPlaced(event.getBlock(), false);
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockPlace(BlockPlaceEvent event) {
BlockStoreApi.setPlaced(event.getBlock(), true);
}

private static final BlockFace[] PISTON_BLOCK_FACES_BY_DATA = {
BlockFace.DOWN,
BlockFace.UP,
BlockFace.NORTH,
BlockFace.SOUTH,
BlockFace.WEST,
BlockFace.EAST,
BlockFace.SELF,
BlockFace.SELF,
BlockFace.DOWN,
BlockFace.UP,
BlockFace.NORTH,
BlockFace.SOUTH,
BlockFace.WEST,
BlockFace.EAST,
BlockFace.SELF,
BlockFace.SELF,
};

private BlockFace getPistonDirection(Block block) {
Expand Down Expand Up @@ -144,7 +154,7 @@ public void onBlockPistonExtend(BlockPistonExtendEvent event) {

BlockStoreApi.setPlaced(pistonArm, pistonPlaced);
}

@SuppressWarnings("deprecation")
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockPistonRetract(BlockPistonRetractEvent event) {
Expand All @@ -171,41 +181,41 @@ public void onBlockPistonRetract(BlockPistonRetractEvent event) {
manager.moveBlocksAsync(blocks, direction.getOppositeFace());
});
}

@EventHandler
public void onChunkLoad(final ChunkLoadEvent event) {
if(blockStoreConfig.getPreloadStrategy() == PreloadStrategy.ALL) {
if (blockStoreConfig.getPreloadStrategy() == PreloadStrategy.ALL) {
getManager(event.getWorld()).preloadChunk(event.getChunk());
}
}

@EventHandler
public void onPlayerMove(PlayerMoveEvent event) {
if(blockStoreConfig.getPreloadStrategy() == PreloadStrategy.CLOSE) {
if (blockStoreConfig.getPreloadStrategy() == PreloadStrategy.CLOSE) {
ChunkLoc before = ChunkLoc.fromLocation(event.getFrom());
ChunkLoc after = ChunkLoc.fromLocation(event.getTo());

if(!before.equals(after)) {
if (!before.equals(after)) {
getManager(event.getTo().getWorld()).preloadStoresAround(after);
}
}
}

@EventHandler
public void onPlayerTeleport(PlayerTeleportEvent event) {
if(blockStoreConfig.getPreloadStrategy() == PreloadStrategy.CLOSE) {
if (blockStoreConfig.getPreloadStrategy() == PreloadStrategy.CLOSE) {
ChunkLoc before = ChunkLoc.fromLocation(event.getFrom());
ChunkLoc after = ChunkLoc.fromLocation(event.getTo());

if(!before.equals(after)) {
if (!before.equals(after)) {
getManager(event.getTo().getWorld()).preloadStoresAround(after);
}
}
}

@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
if(blockStoreConfig.getPreloadStrategy() == PreloadStrategy.CLOSE) {
if (blockStoreConfig.getPreloadStrategy() == PreloadStrategy.CLOSE) {
Player player = event.getPlayer();

ChunkLoc chunkLoc = ChunkLoc.fromLocation(player.getLocation());
Expand All @@ -215,9 +225,9 @@ public void onPlayerJoin(PlayerJoinEvent event) {
manager.preloadStoresAround(chunkLoc);
}
}

public static BlockStore getInstance() {
return instance;
}

}
31 changes: 16 additions & 15 deletions src/main/resources/plugin.yml
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
main: net.sothatsit.blockstore.BlockStore
author: sothatsit
name: BlockStore
version: ${project.version}
soft-depend: [WorldEdit]
commands:
blockstore:
description: 'BlockStore main command'
permissions:
blockstore.check:
description: 'Check whether a block is placed'
blockstore.info:
description: 'Info on the loaded chunk stores'
blockstore.reload:
description: 'Reload the BlockStore config'
main: net.sothatsit.blockstore.BlockStore
author: sothatsit
name: BlockStore
version: ${project.version}
soft-depend: [WorldEdit]
api-version: 1.13
commands:
blockstore:
description: 'BlockStore main command'
permissions:
blockstore.check:
description: 'Check whether a block is placed'
blockstore.info:
description: 'Info on the loaded chunk stores'
blockstore.reload:
description: 'Reload the BlockStore config'

0 comments on commit ed69a27

Please sign in to comment.