9
0
mirror of https://github.com/Xiao-MoMi/Custom-Crops.git synced 2025-12-24 01:19:28 +00:00

implement antigrief

This commit is contained in:
XiaoMoMi
2024-09-04 17:11:12 +08:00
parent a1885e1dda
commit 87ca9cbe1b
8 changed files with 92 additions and 69 deletions

View File

@@ -17,6 +17,7 @@
package net.momirealms.customcrops.bukkit;
import net.momirealms.antigrieflib.AntiGriefLib;
import net.momirealms.customcrops.api.BukkitCustomCropsPlugin;
import net.momirealms.customcrops.api.core.ConfigManager;
import net.momirealms.customcrops.api.core.SimpleRegistryAccess;
@@ -86,7 +87,7 @@ public class BukkitCustomCropsPluginImpl extends BukkitCustomCropsPlugin {
this.logger = new JavaPluginLogger(getBoostrap().getLogger());
this.classPathAppender = new ReflectionClassPathAppender(this);
this.dependencyManager = new DependencyManagerImpl(this);
this.registryAccess = new SimpleRegistryAccess(this);
this.registryAccess = SimpleRegistryAccess.getInstance();
}
@Override
@@ -176,29 +177,40 @@ public class BukkitCustomCropsPluginImpl extends BukkitCustomCropsPlugin {
boolean downloadFromPolymart = polymart.equals("1");
boolean downloadFromBBB = buildByBit.equals("true");
this.getScheduler().sync().runLater(() -> {
getPluginLogger().info("CustomCrops Registry has been frozen");
((SimpleRegistryAccess) registryAccess).freeze();
this.reload();
if (ConfigManager.metrics()) new Metrics((JavaPlugin) getBoostrap(), 16593);
if (ConfigManager.checkUpdate()) {
VersionHelper.UPDATE_CHECKER.apply(this).thenAccept(result -> {
String link;
if (downloadFromPolymart) {
link = "https://polymart.org/resource/2625/";
} else if (downloadFromBBB) {
link = "https://builtbybit.com/resources/36363/";
} else {
link = "https://github.com/Xiao-MoMi/Custom-Crops/";
}
if (!result) {
this.getPluginLogger().info("You are using the latest version.");
} else {
this.getPluginLogger().warn("Update is available: " + link);
}
});
}
}, 1, null);
((SimpleRegistryAccess) registryAccess).freeze();
this.reload();
if (ConfigManager.metrics()) new Metrics((JavaPlugin) getBoostrap(), 16593);
if (ConfigManager.checkUpdate()) {
VersionHelper.UPDATE_CHECKER.apply(this).thenAccept(result -> {
String link;
if (downloadFromPolymart) {
link = "https://polymart.org/resource/2625/";
} else if (downloadFromBBB) {
link = "https://builtbybit.com/resources/36363/";
} else {
link = "https://github.com/Xiao-MoMi/Custom-Crops/";
}
if (!result) {
this.getPluginLogger().info("You are using the latest version.");
} else {
this.getPluginLogger().warn("Update is available: " + link);
}
});
}
// delayed init task
if (VersionHelper.isFolia()) {
Bukkit.getGlobalRegionScheduler().run(getBoostrap(), (scheduledTask) -> {
((SimpleRegistryAccess) registryAccess).freeze();
logger.info("Registry access has been frozen");
((BukkitItemManager) itemManager).setAntiGriefLib(AntiGriefLib.builder((JavaPlugin) getBoostrap()).silentLogs(true).ignoreOP(true).build());
});
} else {
Bukkit.getScheduler().runTask(getBoostrap(), () -> {
((SimpleRegistryAccess) registryAccess).freeze();
logger.info("Registry access has been frozen");
((BukkitItemManager) itemManager).setAntiGriefLib(AntiGriefLib.builder((JavaPlugin) getBoostrap()).silentLogs(true).ignoreOP(true).build());
});
}
}
@Override

View File

@@ -19,6 +19,7 @@ package net.momirealms.customcrops.bukkit.item;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.sound.Sound;
import net.momirealms.antigrieflib.AntiGriefLib;
import net.momirealms.customcrops.api.BukkitCustomCropsPlugin;
import net.momirealms.customcrops.api.core.*;
import net.momirealms.customcrops.api.core.block.BreakReason;
@@ -52,6 +53,7 @@ import org.bukkit.event.player.PlayerItemDamageEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -68,6 +70,7 @@ public class BukkitItemManager extends AbstractItemManager {
private final HashMap<String, ItemProvider> itemProviders = new HashMap<>();
private ItemProvider[] itemDetectArray = new ItemProvider[0];
private final BukkitItemFactory factory;
private AntiGriefLib antiGriefLib;
public BukkitItemManager(BukkitCustomCropsPlugin plugin) {
this.plugin = plugin;
@@ -82,6 +85,10 @@ public class BukkitItemManager extends AbstractItemManager {
this.factory = BukkitItemFactory.create(plugin);
}
public void setAntiGriefLib(AntiGriefLib antiGriefLib) {
this.antiGriefLib = antiGriefLib;
}
@Override
public void load() {
this.resetItemDetectionOrder();
@@ -398,6 +405,10 @@ public class BukkitItemManager extends AbstractItemManager {
return;
}
if (antiGriefLib != null && !antiGriefLib.canInteract(player, block.getLocation())) {
return;
}
String itemID = id(itemInHand);
CustomCropsWorld<?> world = optionalWorld.get();
WrappedInteractEvent wrapped = new WrappedInteractEvent(ExistenceForm.BLOCK, player, world, block.getLocation(), blockID, itemInHand, itemID, hand, blockFace, event);
@@ -412,6 +423,10 @@ public class BukkitItemManager extends AbstractItemManager {
return;
}
if (antiGriefLib != null && !antiGriefLib.canInteract(player, location)) {
return;
}
String itemID = id(itemInHand);
CustomCropsWorld<?> world = optionalWorld.get();
WrappedInteractEvent wrapped = new WrappedInteractEvent(ExistenceForm.FURNITURE, player, world, location, furnitureID, itemInHand, itemID, hand, null, event);
@@ -442,6 +457,10 @@ public class BukkitItemManager extends AbstractItemManager {
return;
}
if (antiGriefLib != null && !antiGriefLib.canBreak(player, location)) {
return;
}
String itemID = id(itemInHand);
CustomCropsWorld<?> world = optionalWorld.get();
WrappedBreakEvent wrapped = new WrappedBreakEvent(player, null, world, location, brokenID, itemInHand, itemID, BreakReason.BREAK, event);
@@ -503,6 +522,10 @@ public class BukkitItemManager extends AbstractItemManager {
return;
}
if (antiGriefLib != null && !antiGriefLib.canPlace(player, location)) {
return;
}
CustomCropsWorld<?> world = optionalWorld.get();
Pos3 pos3 = Pos3.from(location);
Optional<CustomCropsBlockState> optionalState = world.getBlockState(pos3);

View File

@@ -16,28 +16,4 @@ softdepend:
- RealisticSeasons
- AdvancedSeasons
- SlimeWorldManager
- MythicMobs
- HuskClaims
- HuskTowns
- Residence
- BentoBox
- FabledSkyBlock
- CrashClaim
- GriefDefender
- GriefPrevention
- BentoBox
- IridiumSkyblock
- KingdomsX
- Landlord
- Lands
- PlotSquared
- ProtectionStones
- RedProtect
- Factions
- SuperiorSkyblock2
- Towny
- UltimateClaims
- UltimateClans
- uSkyBlock
- WorldGuard
- XClaim
- MythicMobs