Skip to content
Permalink
Browse files

Use Paper's PreCreatureSpawnEvent for performance.

  • Loading branch information...
wizjany committed Jun 15, 2019
1 parent fe4f0cf commit 235c00cb305be1666cc6b4f3ca686bc99a7b6507
@@ -22,6 +22,8 @@
<subpackage name="bukkit">
<allow pkg="org.bukkit"/>
<allow pkg="org.bstats.bukkit"/>
<allow pkg="io.papermc.lib"/>
<allow pkg="com.destroystokyo.paper"/>
</subpackage>

<subpackage name="sponge">
@@ -2,10 +2,8 @@ apply plugin: 'eclipse'
apply plugin: 'idea'

repositories {
maven {
name = 'bstats repo'
url = 'https://repo.codemc.org/repository/maven-public'
}
maven { url = 'https://repo.codemc.org/repository/maven-public' }
maven { url = 'https://papermc.io/repo/repository/maven-public/' }
}

dependencies {
@@ -14,6 +12,8 @@ dependencies {
api ('com.sk89q.worldedit:worldedit-bukkit:7.0.1-SNAPSHOT') {
exclude group: 'io.papermc', module: 'paperlib'
}
implementation 'io.papermc:paperlib:1.0.2'
compileOnly 'com.destroystokyo.paper:paper-api:1.13.2-R0.1-SNAPSHOT'
implementation ('com.sk89q:commandbook:2.3') {
exclude group: 'com.sk89q', module: 'worldedit'
exclude group: 'com.zachsthings.libcomponents'
@@ -46,6 +46,9 @@ shadowJar {
relocate ("org.bstats", "com.sk89q.worldguard.bukkit.bstats") {
include(dependency("org.bstats:bstats-bukkit:1.5"))
}
relocate ("io.papermc.lib", "com.sk89q.worldguard.bukkit.paperlib") {
include(dependency("io.papermc:paperlib:1.0.2"))
}
}

relocate('org.flywaydb', 'com.sk89q.worldguard.internal.flywaydb')
@@ -35,6 +35,7 @@
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.bukkit.listener.PaperListener;
import com.sk89q.worldguard.commands.GeneralCommands;
import com.sk89q.worldguard.commands.ProtectionCommands;
import com.sk89q.worldguard.commands.ToggleCommands;
@@ -68,6 +69,7 @@
import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import com.sk89q.worldguard.util.logging.RecordMessagePrefixer;
import io.papermc.lib.PaperLib;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
@@ -181,6 +183,10 @@ public void onEnable() {
if ("true".equalsIgnoreCase(System.getProperty("worldguard.debug.listener"))) {
(new DebuggingListener(this, WorldGuard.logger)).registerEvents();
}
if (PaperLib.isPaper()) {
(new PaperListener(this)).registerEvents();
}
PaperLib.suggestPaper(this);

platform.getGlobalStateManager().updateCommandBookGodMode();

@@ -0,0 +1,58 @@
/*
* WorldGuard, a suite of tools for Minecraft
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldGuard team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.sk89q.worldguard.bukkit.listener;

import com.destroystokyo.paper.event.entity.PreCreatureSpawnEvent;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.config.ConfigurationManager;
import com.sk89q.worldguard.config.WorldConfiguration;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;

public class PaperListener extends AbstractListener {

/**
* Construct the listener.
*
* @param plugin an instance of WorldGuardPlugin
*/
public PaperListener(WorldGuardPlugin plugin) {
super(plugin);
}

@EventHandler(ignoreCancelled = true, priority = EventPriority.LOW)
public void onPreCreatureSpawn(PreCreatureSpawnEvent event) {
ConfigurationManager cfg = WorldGuard.getInstance().getPlatform().getGlobalStateManager();

if (cfg.activityHaltToggle) {
event.setCancelled(true);
return;
}

WorldConfiguration wcfg = cfg.get(BukkitAdapter.adapt(event.getSpawnLocation().getWorld()));
if (WorldGuardEntityListener.checkCreatureSpawn(event, cfg.useRegionsCreatureSpawnEvent && wcfg.useRegions,
wcfg, event.getType(), event.getReason(), event.getSpawnLocation())) {
event.setShouldAbortSpawn(true);
}
}

}
@@ -54,6 +54,7 @@
import org.bukkit.entity.WitherSkull;
import org.bukkit.entity.Wolf;
import org.bukkit.entity.minecart.ExplosiveMinecart;
import org.bukkit.event.Cancellable;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
@@ -616,57 +617,58 @@ public void onCreatureSpawn(CreatureSpawnEvent event) {
return;
}

WorldConfiguration wcfg = cfg.get(BukkitAdapter.adapt(event.getEntity().getWorld()));

// allow spawning of creatures from plugins
if (!wcfg.blockPluginSpawning && event.getSpawnReason() == CreatureSpawnEvent.SpawnReason.CUSTOM) {
return;
}
WorldConfiguration wcfg = cfg.get(BukkitAdapter.adapt(event.getLocation().getWorld()));

// armor stands are living entities, but we check them as blocks/non-living entities, so ignore them here
if (Entities.isConsideredBuildingIfUsed(event.getEntity())) {
return;
}

if (wcfg.allowTamedSpawns
&& event.getEntity() instanceof Tameable // nullsafe check
&& ((Tameable) event.getEntity()).isTamed()) {
if (wcfg.allowTamedSpawns && Entities.isTamed(event.getEntity())) {
return;
}

EntityType entityType = event.getEntityType();
checkCreatureSpawn(event, wcfg.useRegions && cfg.useRegionsCreatureSpawnEvent, wcfg, entityType, event.getSpawnReason(), event.getLocation());
}

static boolean checkCreatureSpawn(Cancellable event, boolean checkRegions, WorldConfiguration wcfg,
EntityType entityType, SpawnReason spawnReason, Location eventLoc) {
// allow spawning of creatures from plugins
if (!wcfg.blockPluginSpawning && spawnReason == SpawnReason.CUSTOM) {
return true;
}

com.sk89q.worldedit.world.entity.EntityType weEntityType = BukkitAdapter.adapt(entityType);

if (weEntityType != null && wcfg.blockCreatureSpawn.contains(weEntityType)) {
event.setCancelled(true);
return;
return true;
}

Location eventLoc = event.getLocation();

if (wcfg.useRegions && cfg.useRegionsCreatureSpawnEvent) {
if (checkRegions) {
ApplicableRegionSet set =
WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery().getApplicableRegions(BukkitAdapter.adapt(eventLoc));

if (!set.testState(null, Flags.MOB_SPAWNING)) {
event.setCancelled(true);
return;
return true;
}

Set<com.sk89q.worldedit.world.entity.EntityType> entityTypes = set.queryValue(null, Flags.DENY_SPAWN);
if (entityTypes != null && weEntityType != null && entityTypes.contains(weEntityType)) {
event.setCancelled(true);
return;
return true;
}
}

if (wcfg.blockGroundSlimes && entityType == EntityType.SLIME
&& eventLoc.getY() >= 60
&& event.getSpawnReason() == SpawnReason.NATURAL) {
&& spawnReason == SpawnReason.NATURAL) {
event.setCancelled(true);
return;
return true;
}
return false;
}

private static class UselessIn114Listener implements Listener {

0 comments on commit 235c00c

Please sign in to comment.
You can’t perform that action at this time.