Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Intelli committed May 12, 2024
1 parent 42af5dc commit 5a49ef8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 77 deletions.
Original file line number Diff line number Diff line change
@@ -1,49 +1,42 @@
package net.coreprotect.worldedit;

import org.bukkit.Bukkit;

import com.sk89q.worldedit.EditSession.Stage;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.event.extent.EditSessionEvent;
import com.sk89q.worldedit.util.eventbus.Subscribe;

import net.coreprotect.CoreProtect;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.language.Phrase;
import net.coreprotect.language.Selector;
import net.coreprotect.thread.Scheduler;
import net.coreprotect.utility.Chat;
import org.bukkit.Bukkit;

public class CoreProtectEditSessionEvent
{
public class CoreProtectEditSessionEvent {
private static boolean initialized = false;
private static boolean isFAWE = false;
private static CoreProtectEditSessionEvent event = new CoreProtectEditSessionEvent();

public static boolean isInitialized()
{
public static boolean isInitialized() {
return initialized;
}

public static boolean isFAWE()
{
public static boolean isFAWE() {
return isFAWE;
}

public static void register()
{
if (isInitialized())
{
public static void register() {
if (isInitialized()) {
return;
}

try
{
WorldEdit.getInstance().getEventBus().register(new Object()
{
try {
WorldEdit.getInstance().getEventBus().register(new Object() {
@Subscribe
public void onEditSessionEvent(EditSessionEvent event)
{
if (event.getActor() != null && event.getStage() == Stage.BEFORE_CHANGE)
{
public void onEditSessionEvent(EditSessionEvent event) {
if (event.getActor() != null && event.getStage() == Stage.BEFORE_CHANGE) {
event.setExtent(new CoreProtectLogger(event.getActor(), event.getWorld(), event.getExtent()));
}
}
Expand All @@ -52,47 +45,37 @@ public void onEditSessionEvent(EditSessionEvent event)
ConfigHandler.worldeditEnabled = true;
isFAWE = (Bukkit.getServer().getPluginManager().getPlugin("FastAsyncWorldEdit") != null);
}
catch (Exception e)
{
catch (Exception e) {
// Failed to initialize WorldEdit logging
}

Scheduler.runTask(CoreProtect.getInstance(), () ->
{
try
{
if (isInitialized())
{
Scheduler.runTask(CoreProtect.getInstance(), () -> {
try {
if (isInitialized()) {
Chat.console(Phrase.build(Phrase.INTEGRATION_SUCCESS, isFAWE() ? "FastAsyncWorldEdit" : "WorldEdit", Selector.FIRST));
}
else
{
else {
Chat.console(Phrase.build(Phrase.INTEGRATION_ERROR, isFAWE() ? "FastAsyncWorldEdit" : "WorldEdit", Selector.FIRST));
}
}
catch (Exception e)
{
catch (Exception e) {
e.printStackTrace();
}
});
}

public static void unregister()
{
if (!isInitialized())
{
public static void unregister() {
if (!isInitialized()) {
return;
}

try
{
try {
WorldEdit.getInstance().getEventBus().unregister(event);
initialized = false;
ConfigHandler.worldeditEnabled = false;
Chat.console(Phrase.build(Phrase.INTEGRATION_SUCCESS, isFAWE() ? "FastAsyncWorldEdit" : "WorldEdit", Selector.SECOND));
}
catch (Exception e)
{
catch (Exception e) {
Chat.console(Phrase.build(Phrase.INTEGRATION_ERROR, isFAWE() ? "FastAsyncWorldEdit" : "WorldEdit", Selector.SECOND));
}
}
Expand Down
61 changes: 23 additions & 38 deletions src/main/java/net/coreprotect/worldedit/CoreProtectLogger.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package net.coreprotect.worldedit;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;

import com.sk89q.worldedit.MaxChangedBlocksException;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
Expand All @@ -14,38 +18,30 @@
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockStateHolder;

import net.coreprotect.config.Config;
import net.coreprotect.utility.Util;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;

public class CoreProtectLogger extends AbstractDelegateExtent
{
public class CoreProtectLogger extends AbstractDelegateExtent {
private final Actor eventActor;
private final World eventWorld;
private final Extent eventExtent;

protected CoreProtectLogger(Actor actor, World world, Extent extent)
{
protected CoreProtectLogger(Actor actor, World world, Extent extent) {
super(extent);
this.eventActor = actor;
this.eventWorld = world;
this.eventExtent = extent;
}

@Override
public <T extends BlockStateHolder<T>> boolean setBlock(BlockVector3 position, T block) throws WorldEditException
{
public <T extends BlockStateHolder<T>> boolean setBlock(BlockVector3 position, T block) throws WorldEditException {
org.bukkit.World world = BukkitAdapter.adapt(eventWorld);
if (!Config.getConfig(world).WORLDEDIT)
{
if (CoreProtectEditSessionEvent.isFAWE())
{
if (!Config.getConfig(world).WORLDEDIT) {
if (CoreProtectEditSessionEvent.isFAWE()) {
return eventExtent.setBlock(position.getX(), position.getY(), position.getZ(), block);
}
else
{
else {
return eventExtent.setBlock(position, block);
}
}
Expand All @@ -60,18 +56,14 @@ public <T extends BlockStateHolder<T>> boolean setBlock(BlockVector3 position, T
// e.g. BaseBlock block = eventWorld.getBlock(position);
ItemStack[] containerData = CoreProtectEditSessionEvent.isFAWE() ? null : Util.getContainerContents(oldType, null, location);

if (CoreProtectEditSessionEvent.isFAWE())
{
if (eventExtent.setBlock(position.getX(), position.getY(), position.getZ(), block))
{
if (CoreProtectEditSessionEvent.isFAWE()) {
if (eventExtent.setBlock(position.getX(), position.getY(), position.getZ(), block)) {
WorldEditLogger.postProcess(eventExtent, eventActor, position, location, block, baseBlock, oldType, oldBlock, containerData);
return true;
}
}
else
{
if (eventExtent.setBlock(position, block))
{
else {
if (eventExtent.setBlock(position, block)) {
WorldEditLogger.postProcess(eventExtent, eventActor, position, location, block, baseBlock, oldType, oldBlock, containerData);
return true;
}
Expand All @@ -81,39 +73,32 @@ public <T extends BlockStateHolder<T>> boolean setBlock(BlockVector3 position, T
}

@Override
public <T extends BlockStateHolder<T>> boolean setBlock(int x, int y, int z, T block) throws WorldEditException
{
public <T extends BlockStateHolder<T>> boolean setBlock(int x, int y, int z, T block) throws WorldEditException {
return this.setBlock(BlockVector3.at(x, y, z), block);
}

@Override
public int replaceBlocks(final Region region, final Mask mask, final Pattern pattern) throws MaxChangedBlocksException
{
public int replaceBlocks(final Region region, final Mask mask, final Pattern pattern) throws MaxChangedBlocksException {
org.bukkit.World world = BukkitAdapter.adapt(eventWorld);
if (!Config.getConfig(world).WORLDEDIT)
{
if (!Config.getConfig(world).WORLDEDIT) {
return eventExtent.replaceBlocks(region, mask, pattern);
}
processPatternToBlocks(world, region, pattern);
return eventExtent.replaceBlocks(region, mask, pattern);
}

@Override
public int setBlocks(Region region, Pattern pattern) throws MaxChangedBlocksException
{
public int setBlocks(Region region, Pattern pattern) throws MaxChangedBlocksException {
org.bukkit.World world = BukkitAdapter.adapt(eventWorld);
if (!Config.getConfig(world).WORLDEDIT)
{
if (!Config.getConfig(world).WORLDEDIT) {
return eventExtent.setBlocks(region, pattern);
}
processPatternToBlocks(world, region, pattern);
return eventExtent.setBlocks(region, pattern);
}

private void processPatternToBlocks(org.bukkit.World world, Region region, Pattern pattern)
{
for (BlockVector3 position : region.clone())
{
private void processPatternToBlocks(org.bukkit.World world, Region region, Pattern pattern) {
for (BlockVector3 position : region.clone()) {
BlockState oldBlock = eventExtent.getBlock(position);
Material oldType = BukkitAdapter.adapt(oldBlock.getBlockType());
Location location = new Location(world, position.getBlockX(), position.getBlockY(), position.getBlockZ());
Expand All @@ -126,4 +111,4 @@ private void processPatternToBlocks(org.bukkit.World world, Region region, Patte
WorldEditLogger.postProcess(eventExtent, eventActor, position, location, pattern.applyBlock(position), baseBlock, oldType, oldBlock, containerData);
}
}
}
}

0 comments on commit 5a49ef8

Please sign in to comment.