Skip to content

Commit

Permalink
Possibly improve folia support or break it even more who knows
Browse files Browse the repository at this point in the history
  • Loading branch information
Lauriichan committed Apr 14, 2024
1 parent 7186495 commit a9cfddc
Show file tree
Hide file tree
Showing 33 changed files with 220 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public List<Location> resolve(final Location start, final int radius, final List
return resolved;
}
final Location location = new Location(world, cx, y, cz);
if (change.isWoodBlock(Locator.getBlock(location))) {
if (change.isWoodBlock(Locator.getBlockState(location))) {
if (current.contains(location)) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public List<Location> resolve(final Location start, final int radius, final List
return resolved;
}
final Location location = new Location(world, cx, y, cz);
if (change.isWoodBlock(Locator.getBlock(location))) {
if (change.isWoodBlock(Locator.getBlockState(location))) {
if (current.contains(location) || storage.getData(cx, y, cz).getPlayerId() != null) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.syntaxphoenix.spigot.smoothtimber.compatibility.coreprotect;

import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;

public interface CoreCompat {

void logRemoval(String user, Location location, Block block);
void logRemoval(String user, Location location, BlockState block);

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.syntaxphoenix.spigot.smoothtimber.compatibility.coreprotect;

import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.plugin.Plugin;

import net.coreprotect.CoreProtectAPI;
Expand All @@ -15,7 +15,7 @@ protected CoreCompat_v1_13_x(final Plugin plugin) {
}

@Override
public void logRemoval(final String user, final Location location, final Block block) {
public void logRemoval(final String user, final Location location, final BlockState block) {
api.logRemoval(user, location, block.getType(), block.getBlockData());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.syntaxphoenix.spigot.smoothtimber.compatibility.coreprotect;

import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.plugin.Plugin;

import com.syntaxphoenix.spigot.smoothtimber.version.manager.VersionChanger;
Expand All @@ -20,7 +20,7 @@ protected CoreCompat_v1_8_x(final Plugin plugin, final VersionChanger changer) {

@SuppressWarnings("deprecation")
@Override
public void logRemoval(final String user, final Location location, final Block block) {
public void logRemoval(final String user, final Location location, final BlockState block) {
api.logRemoval(user, location, block.getType(), changer.getData(block));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ protected CoreProtectChopListener(final CoreCompat compat) {
@EventHandler
public void onChopEvent(final AsyncPlayerChoppedTreeEvent event) {
for (final Location location : event.getBlockLocations()) {
compat.logRemoval(event.getPlayer().getName(), location, Locator.getBlock(location));
compat.logRemoval(event.getPlayer().getName(), location, Locator.getBlockState(location));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public void onGrow(final StructureGrowEvent event) {
}
final Player player = event.getPlayer();
final String user = player != null ? "#st_" + player.getName() : "#tree";
Platform.getPlatform().regionalSyncTaskLater(event.getLocation(), () -> {
Platform.getPlatform().asyncTaskLater(() -> {
for (final BlockState state : event.getBlocks()) {
compat.logRemoval(user, state.getLocation(), state.getWorld().getBlockAt(state.getLocation()));
compat.logRemoval(user, state.getLocation(), state);
}
}, 5);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.plugin.Plugin;

import com.syntaxphoenix.spigot.smoothtimber.config.config.CutterConfig;
Expand Down Expand Up @@ -51,7 +51,7 @@ public List<Location> resolve(final Location start, final int radius, final List
for (int cx = x - radius; cx <= x + radius; cx++) {
for (int cz = z - radius; cz <= z + radius; cz++) {
final Location location = new Location(world, cx, y, cz);
final Block block = Locator.getBlock(location);
final BlockState block = Locator.getBlockState(location);
if (change.isWoodBlock(block)) {
if (current.contains(location)) {
continue;
Expand Down Expand Up @@ -89,8 +89,8 @@ public List<Location> resolve(final Location start, final int radius, final List
return new ArrayList<>(found);
}

public boolean isPlayerPlaced(final Block block) {
final List<String[]> list = api.blockLookup(block, 0);
public boolean isPlayerPlaced(final BlockState block) {
final List<String[]> list = api.blockLookup(block.getBlock(), 0);
if (list == null || list.isEmpty()) {
return false;
}
Expand All @@ -105,7 +105,7 @@ public boolean isPlayerPlaced(final Block block) {

@Override
public boolean isPlayerPlaced(final Location location) {
final Block block = Locator.getBlock(location);
final BlockState block = Locator.getBlockState(location);
if (block == null) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public LogBlockChopListener(final Consumer logblockConsumer) {
public void onChopEvent(final AsyncPlayerChoppedTreeEvent event) {
final Actor actor = new Actor("#sm_" + event.getPlayer().getName());
for (final Location location : event.getBlockLocations()) {
logblockConsumer.queueBlockBreak(actor, location, Locator.getBlock(location).getBlockData());
logblockConsumer.queueBlockBreak(actor, location, Locator.getBlockState(location).getBlockData());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;

import com.syntaxphoenix.spigot.smoothtimber.SmoothTimber;

Expand All @@ -36,7 +36,7 @@ public LogBlockDatabaseAccessor(final LogBlock logBlock) {
this.logBlock = logBlock;
}

public boolean isPlayerPlaced(final Block block) {
public boolean isPlayerPlaced(final BlockState block) {
final String tableName = getTableName(block.getWorld());
if (tableName == null) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;

import com.syntaxphoenix.spigot.smoothtimber.config.config.CutterConfig;
import com.syntaxphoenix.spigot.smoothtimber.utilities.PluginUtils;
Expand Down Expand Up @@ -50,7 +50,7 @@ public List<Location> resolve(final Location start, final int radius, final List
for (int cx = x - radius; cx <= x + radius; cx++) {
for (int cz = z - radius; cz <= z + radius; cz++) {
final Location location = new Location(world, cx, y, cz);
final Block block = Locator.getBlock(location);
final BlockState block = Locator.getBlockState(location);
if (change.isWoodBlock(block)) {
if (current.contains(location)) {
continue;
Expand Down Expand Up @@ -85,16 +85,16 @@ public List<Location> resolve(final Location start, final int radius, final List

}

public boolean isPlayerPlaced(final Block block) {
public boolean isPlayerPlaced(final BlockState block) {
if (block == null) {
return false;
}
return isPlayerPlaced(block.getLocation());
return databaseAccessor.isPlayerPlaced(block);
}

@Override
public boolean isPlayerPlaced(final Location location) {
return databaseAccessor.isPlayerPlaced(location.getBlock());
return databaseAccessor.isPlayerPlaced(Locator.getBlockState(location));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,27 @@
import com.bekvon.bukkit.residence.protection.ResidenceManager;
import com.syntaxphoenix.spigot.smoothtimber.event.AsyncPlayerChopTreeEvent;
import com.syntaxphoenix.spigot.smoothtimber.event.reason.DefaultReason;
import com.syntaxphoenix.spigot.smoothtimber.utilities.PluginUtils;
import com.syntaxphoenix.spigot.smoothtimber.platform.Platform;
import com.syntaxphoenix.spigot.smoothtimber.utilities.task.Task;

public final class ResidenceChopListener implements Listener {

protected ResidenceChopListener() {

}
protected ResidenceChopListener() {}

@EventHandler(ignoreCancelled = true)
public void onChopEvent(final AsyncPlayerChopTreeEvent event) {
PluginUtils.getObjectFromMainThread(() -> {
Task task = new Task(() -> {
final ResidenceManager manager = Residence.getInstance().getResidenceManager();
for (final Location location : event.getBlockLocations()) {
final ClaimedResidence residence = manager.getByLoc(location);
if (residence != null && !residence.getPermissions().playerHas(event.getPlayer(), Flags.build, true)) {
event.setCancelled(true);
event.setReason(DefaultReason.RESIDENCE);
return null;
return;
}
}
return null;
}, 1000);
});
Platform.getPlatform().syncTask(task);
task.await(2000L);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.syntaxphoenix.spigot.smoothtimber.compatibility.towny;

import org.bukkit.Bukkit;
import org.bukkit.block.Block;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;

import com.palmergames.bukkit.towny.object.TownyPermission;
import com.palmergames.bukkit.towny.utils.PlayerCacheUtil;
import com.syntaxphoenix.spigot.smoothtimber.SmoothTimber;
import com.syntaxphoenix.spigot.smoothtimber.compatibility.CompatibilityAddon;
import com.syntaxphoenix.spigot.smoothtimber.utilities.locate.Locator;
import com.syntaxphoenix.spigot.smoothtimber.utilities.plugin.PluginPackage;

public class Towny extends CompatibilityAddon {
Expand All @@ -27,8 +28,8 @@ public void onDisable(final SmoothTimber smoothTimber) throws Exception {
}
}

public boolean canDestroy(final Player player, final Block block) {
return PlayerCacheUtil.getCachePermission(player, block.getLocation(), block.getType(), TownyPermission.ActionType.DESTROY);
public boolean canDestroy(final Player player, final Location location) {
return PlayerCacheUtil.getCachePermission(player, location, Locator.getBlockState(location).getType(), TownyPermission.ActionType.DESTROY);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public TownyChopListener(final Towny towny) {
@EventHandler
public void onAsyncPlayerChopTree(final AsyncPlayerChopTreeEvent event) {
for (final Location location : event.getBlockLocations()) {
if (!towny.canDestroy(event.getPlayer(), location.getBlock())) {
if (!towny.canDestroy(event.getPlayer(), location)) {
event.setCancelled(true);
event.setReason(DefaultReason.TOWNY);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void onBlockBreak(final BlockBreakEvent event) {
return;
}
final VersionChanger change = PluginUtils.CHANGER;
if (!change.isWoodBlock(event.getBlock())) {
if (!change.isWoodBlock(Locator.getBlockState(event.getBlock()))) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ protected void internalShutdown() {
global.cancelTasks(plugin);
async.cancelTasks(plugin);
}

@Override
public boolean isRegional() {
return true;
}

@Override
public void regionalTask(final Location location, final Runnable runnable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ private static Platform createPlatform(final SmoothTimber plugin) {
}

protected abstract void internalShutdown();

public boolean isRegional() {
return false;
}

public void regionalTask(final Location location, final Runnable runnable) {
runnable.run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

import static com.syntaxphoenix.spigot.smoothtimber.SmoothTimber.COMMANDS;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.PluginCommand;
Expand Down Expand Up @@ -105,29 +101,6 @@ private void registerTasks() {
Platform.getPlatform().asyncTaskTimer(ConfigTimer.TIMER, 20, 60);
}

/*
* Task Util
*/

public static <E> E getObjectFromMainThread(final Supplier<E> supply) {
return getObjectFromMainThread(supply, CutterConfig.GLOBAL_SYNC_TIME);
}

public static <E> E getObjectFromMainThread(final Supplier<E> supply, final long wait) {
final CountDownLatch latch = new CountDownLatch(1);
final StoredObject<E> value = new StoredObject<>();
Platform.getPlatform().syncTask(() -> {
value.setObject(supply.get());
latch.countDown();
});
try {
latch.await(wait, TimeUnit.MILLISECONDS);
} catch (final InterruptedException e) {
throw new IllegalStateException("Thread interrupted", e);
}
return value.getObject();
}

/*
* Console Util
*/
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public List<Location> resolve(final Location start, final int radius, final List
return resolved;
}
final Location location = new Location(world, cx, y, cz);
if (change.isWoodBlock(Locator.getBlock(location))) {
if (change.isWoodBlock(Locator.getBlockState(location))) {
if (current.contains(location)) {
continue;
}
Expand Down
Loading

0 comments on commit a9cfddc

Please sign in to comment.