Skip to content

Commit

Permalink
Bring Folia support (#3)
Browse files Browse the repository at this point in the history
Also deprecates OldPvP flags
  • Loading branch information
imDaniX authored Oct 25, 2024
2 parents 42fc18d + 3c062f7 commit db72b69
Show file tree
Hide file tree
Showing 14 changed files with 153 additions and 109 deletions.
1 change: 1 addition & 0 deletions resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ restrictcommands:
extendedwewand: false
misc:
pvpmode: default
old-pvp-flags: true
3 changes: 2 additions & 1 deletion resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
name: '${project.name}'
main: wgextender.WGExtender
api-version: '1.13'
api-version: '1.20'
version: '${project.version}'
author: _Shevchik_
depend: [WorldGuard, WorldEdit, Vault]
folia-supported: true
commands:
wgextender:
aliases: [wgex]
7 changes: 5 additions & 2 deletions src/wgextender/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@ public Config(WGExtender plugin) {
public Map<Flag<?>, String> claimAutoFlags = new HashMap<>();

public boolean restrictCommandsInRegionEnabled = false;
public Set<String> restrictedCommandsInRegion = new HashSet<>();
public List<String> restrictedCommandsInRegion = new ArrayList<>();

public boolean extendedWorldEditWandEnabled = false;

public Boolean miscDefaultPvPFlagOperationMode = null;

public boolean miscOldPvpFlags = true;

protected static final String miscPvPFlagOperationModeAllow = "allow";
protected static final String miscPvPFlagOperationModeDeny = "deny";
protected static final String miscPvPFlagOperationModeDefault = "default";
Expand Down Expand Up @@ -129,7 +131,7 @@ private void loadAll() {
}

restrictCommandsInRegionEnabled = config.getBoolean("restrictcommands.enabled", restrictCommandsInRegionEnabled);
restrictedCommandsInRegion = new HashSet<>(config.getStringList("restrictcommands.commands"));
restrictedCommandsInRegion = new ArrayList<>(config.getStringList("restrictcommands.commands"));

extendedWorldEditWandEnabled = config.getBoolean("extendedwewand", extendedWorldEditWandEnabled);

Expand All @@ -141,6 +143,7 @@ private void loadAll() {
} else {
miscDefaultPvPFlagOperationMode = null;
}
miscOldPvpFlags = config.getBoolean("misc.old-pvp-flags");
}

private static BigInteger asBig(ConfigurationSection section, String key) {
Expand Down
11 changes: 9 additions & 2 deletions src/wgextender/WGExtender.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,14 @@ public void onEnable() {
pvplistener = new PvPHandlingListener(config);
pvplistener.inject(this);
oldpvphandler = new OldPVPFlagsHandler();
oldpvphandler.start(this);
if (config.miscOldPvpFlags) {
getLogger().warning(
"Enabling the old-PvP flags. Do note that they're not supported, " +
"as they're very out of scope of extending WG capabilities. " +
"Consider turning them off by setting 'misc.old-pvp-flags' to 'false'"
);
oldpvphandler.start(this);
}
} catch (Throwable t) {
getLogger().log(Level.SEVERE, "Unable to inject, shutting down", t);
t.printStackTrace();
Expand All @@ -90,7 +97,7 @@ public void onDisable() {
WEWandCommandWrapper.uninject();
WGRegionCommandWrapper.uninject();
pvplistener.uninject();
oldpvphandler.stop();
oldpvphandler.stop(this);
} catch (Throwable t) {
getLogger().log(Level.SEVERE, "Unable to uninject, shutting down", t);
Bukkit.shutdown();
Expand Down
3 changes: 2 additions & 1 deletion src/wgextender/features/claimcommand/WGClaimCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ protected static void claim(String id, CommandSender sender) throws CommandExcep
BukkitWorldConfiguration wcfg = WGRegionUtils.getWorldConfig(player);

if (wcfg.maxClaimVolume == Integer.MAX_VALUE) {
throw new CommandException("The maximum claim volume get in the configuration is higher than is supported. " + "Currently, it must be " + Integer.MAX_VALUE + " or smaller. Please contact a server administrator.");
throw new CommandException("The maximum claim volume get in the configuration is higher than is supported. " +
"Currently, it must be " + Integer.MAX_VALUE + " or smaller. Please contact a server administrator.");
}

LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer(player);
Expand Down
24 changes: 12 additions & 12 deletions src/wgextender/features/claimcommand/WGRegionCommandWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,28 +82,28 @@ public boolean execute(CommandSender sender, String label, String[] args) {

private boolean process(Player player) {
BlockLimits.ProcessedClaimInfo info = blockLimits.processClaimInfo(config, player);
switch (info.result()) {
default: return true;
case DENY_MAX_VOLUME: {
return switch (info.result()) {
case ALLOW -> true;
case DENY_MAX_VOLUME -> {
player.sendMessage(RED + "Вы не можете заприватить такой большой регион");
player.sendMessage(RED + "Ваш лимит: "+info.assignedLimit()+", вы попытались заприватить: "+info.assignedSize());
return false;
yield false;
}
case DENY_MIN_VOLUME: {
case DENY_MIN_VOLUME -> {
player.sendMessage(RED + "Вы не можете заприватить такой маленький регион");
player.sendMessage(RED + "Минимальный объем: "+info.assignedLimit()+", вы попытались заприватить: "+info.assignedSize());
return false;
yield false;
}
case DENY_HORIZONTAL: {
player.sendMessage(RED + "Вы не можете заприватить такой маленький регион");
case DENY_HORIZONTAL -> {
player.sendMessage(RED + "Вы не можете заприватить такой узкий регион");
player.sendMessage(RED + "Минимальная ширина: "+info.assignedLimit()+", вы попытались заприватить: "+info.assignedSize());
return false;
yield false;
}
case DENY_VERTICAL: {
case DENY_VERTICAL -> {
player.sendMessage(RED + "Вы не можете заприватить такой низкий регион");
player.sendMessage(RED + "Минимальная высота: "+info.assignedLimit()+", вы попытались заприватить: "+info.assignedSize());
return false;
yield false;
}
}
};
}
}
15 changes: 6 additions & 9 deletions src/wgextender/features/extendedwand/WEWand.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@
import org.bukkit.inventory.meta.ItemMeta;
import wgextender.utils.WEUtils;

public class WEWand {

public class WEWand { // TODO Use PersistentData API
protected static final String WAND_NAME = ChatColor.LIGHT_PURPLE + "Selection wand";

protected static Material cachedWandMaterial;

protected static Material getWandMaterial() {
protected static Material getWandMaterial() { // TODO Rework to better handle registry
String weWandMaterialName = WEUtils.getWorldEditPlugin().getLocalConfiguration().wandItem.toUpperCase();
if ((cachedWandMaterial == null) || !cachedWandMaterial.toString().equals(weWandMaterialName)) {
cachedWandMaterial = Material.getMaterial(weWandMaterialName.split(":")[1]);
Expand All @@ -46,12 +45,10 @@ public static ItemStack getWand() {
return itemstack;
}

public static boolean isWand(ItemStack itemstack) {
if (itemstack.getType().equals(getWandMaterial())) {
ItemMeta im = itemstack.getItemMeta();
if (im != null) {
return WAND_NAME.equals(im.getDisplayName());
}
public static boolean isWand(ItemStack item) {
if (item.getType().equals(getWandMaterial()) && item.hasItemMeta()) {
ItemMeta meta = item.getItemMeta();
return meta.hasDisplayName() && WAND_NAME.equals(meta.getDisplayName());
}
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/wgextender/features/flags/ChorusFruitFlagHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void onItemUse(PlayerItemConsumeEvent event) {
!WGRegionUtils.canBypassProtection(event.getPlayer()) &&
!WGRegionUtils.isFlagAllows(player, player.getLocation(), WGExtenderFlags.CHORUS_FRUIT_USE_FLAG)
) {
player.sendMessage(ChatColor.RED + "Вы не можете использовать фрукт телепортации в этом регионе");
player.sendMessage(ChatColor.RED + "Вы не можете использовать хорус в этом регионе");
event.setCancelled(true);
}
}
Expand Down
63 changes: 37 additions & 26 deletions src/wgextender/features/flags/OldPVPFlagsHandler.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package wgextender.features.flags;

import com.google.common.base.Function;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeInstance;
import org.bukkit.entity.Entity;
Expand All @@ -16,22 +14,27 @@
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDamageEvent.DamageModifier;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.plugin.Plugin;
import wgextender.WGExtender;
import wgextender.utils.WGRegionUtils;

import java.lang.reflect.Field;
import java.util.*;
import java.util.EnumSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;

@Deprecated
public class OldPVPFlagsHandler implements Listener {
@SuppressWarnings("deprecation")
private static final Set<EntityDamageEvent.DamageModifier> PVP_MODIFIERS = EnumSet.of(
DamageModifier.ARMOR, DamageModifier.RESISTANCE, DamageModifier.MAGIC, DamageModifier.ABSORPTION
);
private final Map<UUID, Double> oldValues = new HashMap<>();
private final Map<UUID, Double> oldValues = new ConcurrentHashMap<>();
private Field functionsField;

public void start(Plugin plugin) {
Expand All @@ -47,25 +50,22 @@ public void start(Plugin plugin) {
return;
}

Server server = plugin.getServer();
server.getPluginManager().registerEvents(this, plugin);
server.getScheduler().runTaskTimer(WGExtender.getInstance(), () -> {
for (Player player : Bukkit.getOnlinePlayers()) {
if (WGRegionUtils.isFlagTrue(player.getLocation(), WGExtenderFlags.OLDPVP_ATTACKSPEED)) {
if (!oldValues.containsKey(player.getUniqueId())) {
AttributeInstance attribute = player.getAttribute(Attribute.GENERIC_ATTACK_SPEED);
oldValues.put(player.getUniqueId(), attribute.getBaseValue());
attribute.setBaseValue(16.0);
}
} else {
reset(player);
}
}
}, 0, 1);
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}

public void stop() {
for (Player player : Bukkit.getOnlinePlayers()) {
public void stop(Plugin plugin) {
for (Player player : plugin.getServer().getOnlinePlayers()) {
reset(player);
}
}

private void handlePlayer(Player player) {
if (WGRegionUtils.isFlagTrue(player.getLocation(), WGExtenderFlags.OLDPVP_ATTACKSPEED)) {
if (oldValues.containsKey(player.getUniqueId())) return;
AttributeInstance attribute = player.getAttribute(Attribute.GENERIC_ATTACK_SPEED);
oldValues.put(player.getUniqueId(), attribute.getBaseValue());
attribute.setBaseValue(16.0);
} else {
reset(player);
}
}
Expand All @@ -77,12 +77,22 @@ private void reset(Player player) {
}
}

@EventHandler
public void onJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
handlePlayer(player);
player.getScheduler().runAtFixedRate(WGExtender.getInstance(),
(task) -> handlePlayer(player),
() -> reset(player),
1, 1
);
}

@EventHandler(priority = EventPriority.LOWEST)
public void onQuit(PlayerQuitEvent event) {
reset(event.getPlayer());
}

@SuppressWarnings({"unchecked", "deprecation"})
@EventHandler(priority = EventPriority.LOWEST)
public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
Entity entity = event.getEntity();
Expand All @@ -93,15 +103,16 @@ public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
}
Map<DamageModifier, Function<Double, Double>> func;
try {
func = (Map<DamageModifier, Function<Double, Double>>) functionsField.get(event);
//noinspection unchecked
func = (Map<DamageModifier, Function<Double, Double>>) functionsField.get(event);
} catch (IllegalArgumentException | IllegalAccessException e) {
WGExtender.getInstance().getLogger().log(Level.SEVERE, "Unable to recalculate blocking damage", e);
return;
}
double totalDamage = event.getDamage() + event.getDamage(DamageModifier.HARD_HAT);
//reset blocking modifier
// Reset blocking modifier
event.setDamage(DamageModifier.BLOCKING, 0);
//recalculate other modifiers
// Recalculate other modifiers
for (var modifier : PVP_MODIFIERS) {
double damage = func.get(modifier).apply(totalDamage);
event.setDamage(modifier, damage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ public void onDamageEntity(DamageEntityEvent event) {
Entities.isAmbient(event.getEntity()) ||
Entities.isVehicle(event.getEntity().getType())
) {
canDamage = event.getRelevantFlags().isEmpty() || (query.queryState(weTarget, associable, combine(event)) != State.DENY);
canDamage = event.getRelevantFlags().isEmpty() || (query.queryState(weTarget, associable, getFlags(event)) != State.DENY);
what = "hit that";

/* Paintings, item frames, etc. */
} else if (Entities.isConsideredBuildingIfUsed(event.getEntity())) {
canDamage = query.testBuild(weTarget, associable, combine(event));
canDamage = query.testBuild(weTarget, associable, getFlags(event));
what = "change that";

/* PVP */
Expand All @@ -133,18 +133,18 @@ public void onDamageEntity(DamageEntityEvent event) {
// false - disallow pvp when flag not set
if (config.miscDefaultPvPFlagOperationMode == null) {
canDamage =
query.testBuild(weTarget, associable, combine(event, Flags.PVP)) &&
(query.queryState(weAttacker, localPlayerAttacker, combine(event, Flags.PVP)) != State.DENY) &&
(query.queryState(weTarget, localPlayerAttacker, combine(event, Flags.PVP)) != State.DENY);
query.testBuild(weTarget, associable, getFlags(event, Flags.PVP)) &&
(query.queryState(weAttacker, localPlayerAttacker, getFlags(event, Flags.PVP)) != State.DENY) &&
(query.queryState(weTarget, localPlayerAttacker, getFlags(event, Flags.PVP)) != State.DENY);
} else if (config.miscDefaultPvPFlagOperationMode) {
canDamage =
(query.queryState(weAttacker, localPlayerAttacker, combine(event, Flags.PVP)) != State.DENY) &&
(query.queryState(weTarget, localPlayerAttacker, combine(event, Flags.PVP)) != State.DENY);
(query.queryState(weAttacker, localPlayerAttacker, getFlags(event, Flags.PVP)) != State.DENY) &&
(query.queryState(weTarget, localPlayerAttacker, getFlags(event, Flags.PVP)) != State.DENY);
} else {
if (!WGRegionUtils.isInWGRegion(playerAttacker.getLocation()) && !WGRegionUtils.isInWGRegion(target)) {
canDamage = true;
} else {
canDamage = (query.queryState(weAttacker, localPlayerAttacker, combine(event, Flags.PVP)) == State.ALLOW) && (query.queryState(weTarget, localPlayerAttacker, combine(event, Flags.PVP)) == State.ALLOW);
canDamage = (query.queryState(weAttacker, localPlayerAttacker, getFlags(event, Flags.PVP)) == State.ALLOW) && (query.queryState(weTarget, localPlayerAttacker, getFlags(event, Flags.PVP)) == State.ALLOW);
}

}
Expand All @@ -158,17 +158,17 @@ public void onDamageEntity(DamageEntityEvent event) {

/* Player damage not caused by another player */
} else if (event.getEntity() instanceof Player) {
canDamage = event.getRelevantFlags().isEmpty() || (query.queryState(weTarget, associable, combine(event)) != State.DENY);
canDamage = event.getRelevantFlags().isEmpty() || (query.queryState(weTarget, associable, getFlags(event)) != State.DENY);
what = "damage that";

/* damage to non-hostile mobs (e.g. animals) */
} else if (Entities.isNonHostile(event.getEntity())) {
canDamage = query.testBuild(weTarget, associable, combine(event, Flags.DAMAGE_ANIMALS));
canDamage = query.testBuild(weTarget, associable, getFlags(event, Flags.DAMAGE_ANIMALS));
what = "harm that";

/* Everything else */
} else {
canDamage = query.testBuild(weTarget, associable, combine(event, Flags.INTERACT));
canDamage = query.testBuild(weTarget, associable, getFlags(event, Flags.INTERACT));
what = "hit that";
}

Expand Down Expand Up @@ -234,19 +234,21 @@ private void tellErrorMessage(DelegateEvent event, Cause cause, Location locatio
}
}

private static void formatAndSendDenyMessage(String what, LocalPlayer localPlayer, String message) {
if (message == null || message.isEmpty()) return;
message = WGRegionUtils.getPlatform().getMatcher().replaceMacros(localPlayer, message);
message = CommandUtils.replaceColorMacros(message);
localPlayer.printRaw(message.replace("%what%", what));
private static void formatAndSendDenyMessage(String what, LocalPlayer localPlayer, String message) {
if (message == null || message.isEmpty()) return;
message = WGRegionUtils.getPlatform().getMatcher().replaceMacros(localPlayer, message);
message = CommandUtils.replaceColorMacros(message);
localPlayer.printRaw(message.replace("%what%", what));
}

private static StateFlag[] getFlags(DelegateEvent event) {
return event.getRelevantFlags().toArray(new StateFlag[0]);
}

private static StateFlag[] combine(DelegateEvent event, StateFlag... flag) {
private static StateFlag[] getFlags(DelegateEvent event, StateFlag flag) {
List<StateFlag> extra = event.getRelevantFlags();
StateFlag[] flags = Arrays.copyOf(flag, flag.length + extra.size());
for (int i = 0; i < extra.size(); i++) {
flags[flag.length + i] = extra.get(i);
}
return flags;
StateFlag[] result = Arrays.copyOf(getFlags(event), extra.size() + 1);
result[extra.size()] = flag;
return result;
}
}
Loading

0 comments on commit db72b69

Please sign in to comment.