Skip to content

Commit 2b0ed8a

Browse files
Add space helmet item, add custom flags for custom armor things
1 parent aefb335 commit 2b0ed8a

File tree

9 files changed

+569
-7
lines changed

9 files changed

+569
-7
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>parallelmc</groupId>
88
<artifactId>parallelutils</artifactId>
9-
<version>1.5.0</version>
9+
<version>1.6.0</version>
1010
<packaging>jar</packaging>
1111

1212
<name>Parallelutils</name>

src/main/java/parallelmc/parallelutils/Constants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
public class Constants {
44

5-
public static final Version VERSION = new Version(1, 5, 0);
5+
public static final Version VERSION = new Version(1, 6, 0);
66
public static final String PLUGIN_NAME = "ParallelUtils";
77
public static final String DEFAULT_WORLD = "world";
88
}

src/main/java/parallelmc/parallelutils/modules/parallelflags/CustomFlagRegistry.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package parallelmc.parallelutils.modules.parallelflags;
22

33
import com.sk89q.worldguard.WorldGuard;
4+
import com.sk89q.worldguard.protection.flags.DoubleFlag;
45
import com.sk89q.worldguard.protection.flags.IntegerFlag;
56
import com.sk89q.worldguard.protection.flags.StateFlag;
7+
import com.sk89q.worldguard.protection.flags.StringFlag;
68
import com.sk89q.worldguard.protection.flags.registry.FlagConflictException;
79
import com.sk89q.worldguard.protection.flags.registry.FlagRegistry;
810

@@ -15,10 +17,14 @@ public class CustomFlagRegistry {
1517

1618
private final HashMap<String, StateFlag> stateFlags;
1719
private final HashMap<String, IntegerFlag> integerFlags;
20+
private final HashMap<String, DoubleFlag> doubleFlags;
21+
private final HashMap<String, StringFlag> stringFlags;
1822

1923
private CustomFlagRegistry() {
2024
stateFlags = new HashMap<>();
2125
integerFlags = new HashMap<>();
26+
doubleFlags = new HashMap<>();
27+
stringFlags = new HashMap<>();
2228
}
2329

2430
public static CustomFlagRegistry getInstance() {
@@ -61,4 +67,36 @@ public boolean addIntegerFlag(String name) {
6167
public IntegerFlag getIntegerFlag(String name) {
6268
return integerFlags.get(name);
6369
}
70+
71+
public boolean addDoubleFlag(String name) {
72+
FlagRegistry registry = WorldGuard.getInstance().getFlagRegistry();
73+
try {
74+
DoubleFlag flag = new DoubleFlag(name);
75+
registry.register(flag);
76+
doubleFlags.put(name, flag);
77+
return true;
78+
} catch (FlagConflictException e) {
79+
return false;
80+
}
81+
}
82+
83+
@Nullable
84+
public DoubleFlag getDoubleFlag(String name) {
85+
return doubleFlags.get(name);
86+
}
87+
88+
public boolean addStringFlag(String name) {
89+
FlagRegistry registry = WorldGuard.getInstance().getFlagRegistry();
90+
try {
91+
StringFlag flag = new StringFlag(name);
92+
registry.register(flag);
93+
stringFlags.put(name, flag);
94+
return true;
95+
} catch (FlagConflictException e) {
96+
return false;
97+
}
98+
}
99+
100+
@Nullable
101+
public StringFlag getStringFlag(String name) {return stringFlags.get(name);}
64102
}

src/main/java/parallelmc/parallelutils/modules/parallelflags/ParallelFlags.java

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
package parallelmc.parallelutils.modules.parallelflags;
22

3+
import com.sk89q.worldguard.WorldGuard;
4+
import com.sk89q.worldguard.session.SessionManager;
35
import org.bukkit.Bukkit;
46
import org.bukkit.plugin.Plugin;
57
import org.bukkit.plugin.PluginManager;
68
import parallelmc.parallelutils.Constants;
79
import parallelmc.parallelutils.ParallelModule;
810
import parallelmc.parallelutils.Parallelutils;
11+
import parallelmc.parallelutils.modules.parallelflags.events.ParallelFlagsDeathListener;
912
import parallelmc.parallelutils.modules.parallelflags.events.ParallelFlagsInteractListener;
1013
import parallelmc.parallelutils.modules.parallelflags.events.ParallelFlagsPlaceListener;
14+
import parallelmc.parallelutils.modules.parallelflags.session.CustomArmorDeny;
15+
import parallelmc.parallelutils.modules.parallelflags.session.CustomArmorHealth;
1116

1217
import java.util.logging.Level;
1318

@@ -48,13 +53,71 @@ public void onLoad() {
4853
Parallelutils.log(Level.WARNING, "Unable to create trapdoors flag. Will not use");
4954
}
5055

56+
// Griefing protection
5157
if (!registry.addIntegerFlag("tnt-disallow-time")) {
5258
Parallelutils.log(Level.WARNING, "Unable to create tnt-disallow-time flag. Will not use");
5359
}
5460

5561
if (!registry.addIntegerFlag("wither-skull-disallow-time")) {
5662
Parallelutils.log(Level.WARNING, "Unable to create wither-skull-disallow-time flag. Will not use");
5763
}
64+
65+
66+
// Custom armor deny
67+
if (!registry.addIntegerFlag("wearing-custom-helm-deny")) {
68+
Parallelutils.log(Level.WARNING, "Unable to create wearing-custom-helm-deny flag. Will not use");
69+
}
70+
71+
if (!registry.addIntegerFlag("wearing-custom-chestplate-deny")) {
72+
Parallelutils.log(Level.WARNING, "Unable to create wearing-custom-chestplate-deny flag. Will not use");
73+
}
74+
75+
if (!registry.addIntegerFlag("wearing-custom-leggings-deny")) {
76+
Parallelutils.log(Level.WARNING, "Unable to create wearing-custom-leggings-deny flag. Will not use");
77+
}
78+
79+
if (!registry.addIntegerFlag("wearing-custom-boots-deny")) {
80+
Parallelutils.log(Level.WARNING, "Unable to create wearing-custom-boots-deny flag. Will not use");
81+
}
82+
83+
if (!registry.addStringFlag("custom-armor-deny-message")) {
84+
Parallelutils.log(Level.WARNING, "Unable to create custom-armor-deny-message flag. Will not use");
85+
}
86+
87+
// Custom armor damage
88+
if (!registry.addIntegerFlag("wearing-custom-helm-damage")) {
89+
Parallelutils.log(Level.WARNING, "Unable to create wearing-custom-helm-damage flag. Will not use");
90+
}
91+
92+
if (!registry.addIntegerFlag("wearing-custom-chestplate-damage")) {
93+
Parallelutils.log(Level.WARNING, "Unable to create wearing-custom-chestplate-damage flag. Will not use");
94+
}
95+
96+
if (!registry.addIntegerFlag("wearing-custom-leggings-damage")) {
97+
Parallelutils.log(Level.WARNING, "Unable to create wearing-custom-leggings-damage flag. Will not use");
98+
}
99+
100+
if (!registry.addIntegerFlag("wearing-custom-boots-damage")) {
101+
Parallelutils.log(Level.WARNING, "Unable to create wearing-custom-boots-damage flag. Will not use");
102+
}
103+
104+
if (!registry.addStringFlag("custom-armor-damage-message")) {
105+
Parallelutils.log(Level.WARNING, "Unable to create custom-armor-damage-message flag. Will not use");
106+
}
107+
108+
if (!registry.addIntegerFlag("custom-armor-damage-amount")) {
109+
Parallelutils.log(Level.WARNING, "Unable to create custom-armor-damage-amount flag. Will not use");
110+
}
111+
112+
if (!registry.addIntegerFlag("custom-armor-damage-delay")) {
113+
Parallelutils.log(Level.WARNING, "Unable to create custom-armor-damage-delay flag. Will not use");
114+
}
115+
116+
if (!registry.addStringFlag("custom-armor-damage-death")) {
117+
Parallelutils.log(Level.WARNING, "Unable to create custom-armor-damage-death flag. Will not use");
118+
}
119+
120+
58121
} catch (NoClassDefFoundError e) {
59122
Parallelutils.log(Level.SEVERE, "Unable to load WorldGuard! Something is wrong!");
60123
}
@@ -78,12 +141,19 @@ public void onEnable() {
78141
return;
79142
}
80143

144+
SessionManager sessionManager = WorldGuard.getInstance().getPlatform().getSessionManager();
145+
146+
sessionManager.registerHandler(CustomArmorDeny.FACTORY, null);
147+
sessionManager.registerHandler(CustomArmorHealth.FACTORY, null);
148+
81149
manager.registerEvents(new ParallelFlagsInteractListener(), plugin);
82150
manager.registerEvents(new ParallelFlagsPlaceListener(), plugin);
151+
manager.registerEvents(new ParallelFlagsDeathListener(), plugin);
83152
}
84153

85154
@Override
86155
public void onDisable() {
87-
156+
SessionManager sessionManager = WorldGuard.getInstance().getPlatform().getSessionManager();
157+
sessionManager.unregisterHandler(CustomArmorDeny.FACTORY);
88158
}
89159
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package parallelmc.parallelutils.modules.parallelflags.events;
2+
3+
import net.kyori.adventure.text.Component;
4+
import org.bukkit.entity.Player;
5+
import org.bukkit.event.EventHandler;
6+
import org.bukkit.event.EventPriority;
7+
import org.bukkit.event.Listener;
8+
import org.bukkit.event.entity.EntityDamageEvent;
9+
import org.bukkit.event.entity.PlayerDeathEvent;
10+
import parallelmc.parallelutils.Parallelutils;
11+
12+
import java.util.HashMap;
13+
import java.util.logging.Level;
14+
15+
public class ParallelFlagsDeathListener implements Listener {
16+
17+
private static final HashMap<String, String> playerDeathMessages = new HashMap<>();
18+
19+
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
20+
public void onPlayerDeath(PlayerDeathEvent event) {
21+
Player player = event.getEntity();
22+
EntityDamageEvent lastDamage = player.getLastDamageCause();
23+
24+
Parallelutils.log(Level.INFO, "Died");
25+
26+
if (lastDamage != null && lastDamage.getCause() == EntityDamageEvent.DamageCause.CUSTOM) {
27+
Parallelutils.log(Level.INFO, "Custom Damage");
28+
String message = playerDeathMessages.get(player.getName());
29+
if (message == null) return;
30+
31+
playerDeathMessages.remove(player.getName());
32+
33+
event.deathMessage(Component.text(message));
34+
}
35+
}
36+
37+
public static void setPlayerDeathMessage(String playerName, String message) {
38+
playerDeathMessages.put(playerName, message);
39+
}
40+
}
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package parallelmc.parallelutils.modules.parallelflags.session;
2+
3+
import com.sk89q.worldedit.bukkit.BukkitPlayer;
4+
import com.sk89q.worldedit.util.Location;
5+
import com.sk89q.worldedit.world.World;
6+
import com.sk89q.worldguard.LocalPlayer;
7+
import com.sk89q.worldguard.WorldGuard;
8+
import com.sk89q.worldguard.commands.CommandUtils;
9+
import com.sk89q.worldguard.protection.ApplicableRegionSet;
10+
import com.sk89q.worldguard.protection.flags.Flags;
11+
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
12+
import com.sk89q.worldguard.session.MoveType;
13+
import com.sk89q.worldguard.session.Session;
14+
import com.sk89q.worldguard.session.handler.Handler;
15+
import org.bukkit.Bukkit;
16+
import org.bukkit.NamespacedKey;
17+
import org.bukkit.inventory.EquipmentSlot;
18+
import org.bukkit.inventory.ItemStack;
19+
import org.bukkit.persistence.PersistentDataType;
20+
import org.bukkit.plugin.Plugin;
21+
import org.bukkit.plugin.PluginManager;
22+
import parallelmc.parallelutils.Constants;
23+
import parallelmc.parallelutils.Parallelutils;
24+
import parallelmc.parallelutils.modules.parallelflags.CustomFlagRegistry;
25+
26+
import java.util.Set;
27+
import java.util.logging.Level;
28+
29+
public class CustomArmorDeny extends Handler {
30+
31+
public static final Factory FACTORY = new Factory();
32+
public static class Factory extends Handler.Factory<CustomArmorDeny> {
33+
@Override
34+
public CustomArmorDeny create(Session session) {
35+
return new CustomArmorDeny(session);
36+
}
37+
}
38+
39+
private static final long MESSAGE_THRESHOLD = 1000 * 2;
40+
41+
private final CustomFlagRegistry customFlagRegistry;
42+
private long lastMessage;
43+
44+
private final NamespacedKey key;
45+
46+
47+
public CustomArmorDeny(Session session) {
48+
super(session);
49+
customFlagRegistry = CustomFlagRegistry.getInstance();
50+
51+
PluginManager manager = Bukkit.getPluginManager();
52+
Plugin plugin = manager.getPlugin(Constants.PLUGIN_NAME);
53+
54+
if (plugin == null) {
55+
Parallelutils.log(Level.SEVERE, "Unable to initialize session handler. Plugin "
56+
+ Constants.PLUGIN_NAME + " does not exist!");
57+
key = null;
58+
return;
59+
}
60+
key = new NamespacedKey(plugin, "ParallelItem");
61+
}
62+
63+
@Override
64+
public boolean onCrossBoundary(LocalPlayer player, Location from, Location to, ApplicableRegionSet toSet, Set<ProtectedRegion> entered, Set<ProtectedRegion> exited, MoveType moveType) {
65+
66+
BukkitPlayer bukkitPlayer;
67+
68+
if (!(player instanceof BukkitPlayer) || key == null) {
69+
return false;
70+
}
71+
72+
if (entered.size() == 0) return true;
73+
74+
bukkitPlayer = (BukkitPlayer) player;
75+
76+
Integer helmVal = toSet.queryValue(player, customFlagRegistry.getIntegerFlag("wearing-custom-helm-deny"));
77+
78+
boolean allowed = true;
79+
80+
if (helmVal != null && helmVal > 0) { // The zero ensures that it exists. Setting it to null or 0 ignores
81+
ItemStack helm = bukkitPlayer.getPlayer().getInventory().getItem(EquipmentSlot.HEAD);
82+
Integer itemVal = -1;
83+
if (helm != null) {
84+
itemVal = helm.getItemMeta().getPersistentDataContainer().get(key, PersistentDataType.INTEGER);
85+
}
86+
87+
if (itemVal == null || !itemVal.equals(helmVal)) {
88+
allowed = false;
89+
}
90+
}
91+
92+
Integer chestVal = toSet.queryValue(player, customFlagRegistry.getIntegerFlag("wearing-custom-chestplate-deny"));
93+
if (allowed && chestVal != null && chestVal > 0) {
94+
ItemStack chest = bukkitPlayer.getPlayer().getInventory().getItem(EquipmentSlot.CHEST);
95+
96+
Integer itemVal = -1;
97+
if (chest != null) {
98+
itemVal = chest.getItemMeta().getPersistentDataContainer().get(key, PersistentDataType.INTEGER);
99+
}
100+
101+
if (itemVal == null || !itemVal.equals(chestVal)) {
102+
allowed = false;
103+
}
104+
}
105+
106+
Integer leggingsVal = toSet.queryValue(player, customFlagRegistry.getIntegerFlag("wearing-custom-leggings-deny"));
107+
if (allowed && leggingsVal != null && leggingsVal > 0) {
108+
ItemStack leggings = bukkitPlayer.getPlayer().getInventory().getItem(EquipmentSlot.LEGS);
109+
110+
Integer itemVal = -1;
111+
if (leggings != null) {
112+
itemVal = leggings.getItemMeta().getPersistentDataContainer().get(key, PersistentDataType.INTEGER);
113+
}
114+
if (itemVal == null || !itemVal.equals(leggingsVal)) {
115+
allowed = false;
116+
}
117+
}
118+
119+
Integer bootsVal = toSet.queryValue(player, customFlagRegistry.getIntegerFlag("wearing-custom-boots-deny"));
120+
if (allowed && bootsVal != null && bootsVal > 0) {
121+
ItemStack boots = bukkitPlayer.getPlayer().getInventory().getItem(EquipmentSlot.FEET);
122+
123+
Integer itemVal = -1;
124+
if (boots != null) {
125+
itemVal = boots.getItemMeta().getPersistentDataContainer().get(key, PersistentDataType.INTEGER);
126+
}
127+
if (itemVal == null || !itemVal.equals(bootsVal)) {
128+
allowed = false;
129+
}
130+
}
131+
132+
if (!getSession().getManager().hasBypass(player, (World) to.getExtent()) && !allowed && moveType.isCancellable()) {
133+
String message = toSet.queryValue(player, customFlagRegistry.getStringFlag("custom-armor-deny-message"));
134+
135+
if (message == null) {
136+
message = toSet.queryValue(player, Flags.ENTRY_DENY_MESSAGE);
137+
}
138+
139+
long now = System.currentTimeMillis();
140+
141+
if ((now - lastMessage) > MESSAGE_THRESHOLD && message != null && !message.isEmpty()) {
142+
player.printRaw(CommandUtils.replaceColorMacros(message));
143+
lastMessage = now;
144+
}
145+
146+
return false;
147+
} else {
148+
return true;
149+
}
150+
}
151+
152+
}

0 commit comments

Comments
 (0)