Skip to content

Commit b29b22e

Browse files
authored
Merge pull request #4 from Simplexity-Development/add-hat-command
Adds the ability to use /hat to put stuff on your head
2 parents b246882 + d97177f commit b29b22e

File tree

9 files changed

+116
-10
lines changed

9 files changed

+116
-10
lines changed

.github/workflows/maven.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ jobs:
2323
- uses: actions/checkout@v4
2424
- uses: actions/setup-java@v4
2525
with:
26-
java-version: '17'
27-
distribution: 'temurin'
26+
java-version: '21'
27+
distribution: 'corretto'
2828
cache: maven
2929

3030
# Build and verify
@@ -41,4 +41,4 @@ jobs:
4141

4242
# Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive
4343
- name: Update dependency graph
44-
uses: advanced-security/maven-dependency-submission-action@571e99aab1055c2e71a1e2309b9691de18d6b7d6
44+
uses: advanced-security/maven-dependency-submission-action@571e99aab1055c2e71a1e2309b9691de18d6b7d6

pom.xml

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

77
<groupId>simplexity</groupId>
88
<artifactId>AdminHax</artifactId>
9-
<version>1.2.1</version>
9+
<version>1.3.0</version>
1010
<packaging>jar</packaging>
1111

1212
<name>AdminHax</name>

src/main/java/simplexity/adminhax/AdminHax.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import net.kyori.adventure.text.minimessage.MiniMessage;
44
import org.bukkit.plugin.java.JavaPlugin;
55
import simplexity.adminhax.commands.basic.BroadcastMsg;
6+
import simplexity.adminhax.commands.basic.Hat;
67
import simplexity.adminhax.commands.basic.ReloadPlugin;
78
import simplexity.adminhax.commands.basic.RenameItem;
89
import simplexity.adminhax.commands.hax.Feed;
@@ -41,6 +42,7 @@ public void onEnable() {
4142
this.getCommand("broadcastmsg").setExecutor(new BroadcastMsg());
4243
this.getCommand("adminhaxreload").setExecutor(new ReloadPlugin());
4344
this.getCommand("rename").setExecutor(new RenameItem());
45+
this.getCommand("hat").setExecutor(new Hat());
4446
this.getServer().getPluginManager().registerEvents(new FlyListeners(), this);
4547
}
4648

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package simplexity.adminhax.commands.basic;
2+
3+
import org.bukkit.command.Command;
4+
import org.bukkit.command.CommandExecutor;
5+
import org.bukkit.command.CommandSender;
6+
import org.bukkit.enchantments.Enchantment;
7+
import org.bukkit.entity.Player;
8+
import org.bukkit.inventory.ItemStack;
9+
import org.jetbrains.annotations.NotNull;
10+
import simplexity.adminhax.config.ConfigHandler;
11+
import simplexity.adminhax.config.Message;
12+
13+
import java.util.HashMap;
14+
15+
public class Hat implements CommandExecutor {
16+
17+
18+
@Override
19+
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String s, @NotNull String[] args) {
20+
if (!(sender instanceof Player player)) {
21+
sender.sendRichMessage(Message.ERROR_MUST_BE_PLAYER.getMessage());
22+
return false;
23+
}
24+
ItemStack itemToHat = player.getInventory().getItemInMainHand();
25+
ItemStack previousHelm = player.getInventory().getHelmet();
26+
boolean userHadHelmetBefore = (previousHelm != null && !previousHelm.isEmpty());
27+
if (!userHadHelmetBefore && itemToHat.isEmpty()) {
28+
player.sendRichMessage(Message.ERROR_NO_HAT_ITEMS.getMessage());
29+
return false;
30+
}
31+
if (userHadHelmetBefore && (previousHelm.getItemMeta().hasEnchant(Enchantment.BINDING_CURSE) &&
32+
ConfigHandler.getInstance().shouldRespectBindingCurse())) {
33+
player.sendRichMessage(Message.ERROR_CURSE_OF_BINDING.getMessage());
34+
return false;
35+
}
36+
if (ConfigHandler.getInstance().getDisabledHatItems().contains(itemToHat.getType())) {
37+
player.sendRichMessage(Message.ERROR_HAT_NOT_ALLOWED.getMessage());
38+
return false;
39+
}
40+
if (itemToHat.isEmpty()) {
41+
player.getInventory().setHelmet(null);
42+
player.getInventory().setItemInMainHand(previousHelm);
43+
player.sendRichMessage(Message.HAT_REMOVED.getMessage());
44+
return true;
45+
}
46+
player.getInventory().setHelmet(itemToHat.asOne());
47+
itemToHat.subtract();
48+
if (!userHadHelmetBefore) {
49+
player.sendRichMessage(Message.HAT_SUCCESSFUL.getMessage());
50+
return true;
51+
}
52+
if (player.getInventory().getItemInMainHand().isEmpty()) {
53+
player.getInventory().setItemInMainHand(previousHelm);
54+
player.sendRichMessage(Message.HAT_SUCCESSFUL.getMessage());
55+
return true;
56+
}
57+
HashMap<Integer, ItemStack> leftover = player.getInventory().addItem(previousHelm);
58+
if (!leftover.isEmpty()) {
59+
for (Integer index : leftover.keySet()) {
60+
player.getWorld().dropItem(player.getLocation(), leftover.get(index));
61+
}
62+
}
63+
player.sendRichMessage(Message.HAT_SUCCESSFUL.getMessage());
64+
return true;
65+
}
66+
}

src/main/java/simplexity/adminhax/commands/basic/RenameItem.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
4040
}
4141
ItemStack heldItem = player.getInventory().getItemInMainHand();
4242
if (heldItem.isEmpty() || heldItem.getType().isEmpty()) {
43-
player.sendRichMessage(Message.ERROR_MUST_HOLD_ITEM.getMessage());
43+
player.sendRichMessage(Message.ERROR_MUST_HOLD_ITEM_TO_RENAME.getMessage());
4444
return false;
4545
}
4646
Component newName = parsedName(player, renameString);

src/main/java/simplexity/adminhax/config/ConfigHandler.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package simplexity.adminhax.config;
22

3+
import org.bukkit.Material;
34
import org.bukkit.configuration.file.FileConfiguration;
45
import simplexity.adminhax.AdminHax;
56

7+
import java.util.HashSet;
8+
import java.util.List;
69
import java.util.logging.Logger;
710

811
public class ConfigHandler {
@@ -19,8 +22,9 @@ public static ConfigHandler getInstance() {
1922

2023
private float maxWalkSpeed, minWalkSpeed, maxFlySpeed, minFlySpeed;
2124
private boolean sessionPersistentFlight, worldChangePersistentFlight, respawnPersistentFlight,
22-
gamemodeChangePersistentFlight;
25+
gamemodeChangePersistentFlight, respectBindingCurse;
2326
private int maxRenameCharacters;
27+
private static final HashSet<Material> disabledHatItems = new HashSet<>();
2428

2529
public void reloadConfigValues() {
2630
AdminHax.getInstance().reloadConfig();
@@ -34,6 +38,19 @@ public void reloadConfigValues() {
3438
respawnPersistentFlight = config.getBoolean("flight.persistent.respawn", true);
3539
gamemodeChangePersistentFlight = config.getBoolean("flight.persistent.gamemode-change", true);
3640
maxRenameCharacters = config.getInt("rename.max-characters", 50);
41+
respectBindingCurse = config.getBoolean("hat.respect-curse-of-binding", true);
42+
List<String> disabledItems = config.getStringList("hat.disabled-items");
43+
disabledHatItems.clear();
44+
if (!disabledItems.isEmpty()) {
45+
for (String disabledItem : disabledItems) {
46+
Material itemType = Material.getMaterial(disabledItem);
47+
if (itemType == null) {
48+
logger.info(disabledItem + " is not a valid material, please check your syntax");
49+
continue;
50+
}
51+
disabledHatItems.add(itemType);
52+
}
53+
}
3754
}
3855

3956
private float checkFloat(float defaultValue, String configPath, FileConfiguration config) {
@@ -91,4 +108,12 @@ public boolean isGamemodeChangePersistentFlight() {
91108
public int getMaxRenameCharacters() {
92109
return maxRenameCharacters;
93110
}
111+
112+
public boolean shouldRespectBindingCurse() {
113+
return respectBindingCurse;
114+
}
115+
116+
public HashSet<Material> getDisabledHatItems(){
117+
return disabledHatItems;
118+
}
94119
}

src/main/java/simplexity/adminhax/config/Message.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@ public enum Message {
77
ERROR_INVALID_NUMBER("error.invalid-number", "<red>Sorry, you did not enter a valid flyspeed, please try again</red>"),
88
ERROR_INVALID_PLAYER("error.invalid-player", "<red>That is not a valid player. Please check your spelling and try again</red>"),
99
ERROR_MUST_BE_PLAYER("error.must-be-player", "<red>You must be a player to run this command</red>"),
10-
ERROR_MUST_HOLD_ITEM("error.must-hold-item", "<red>You must be holding item to rename</red>"),
10+
ERROR_MUST_HOLD_ITEM_TO_RENAME("error.must-hold-item", "<red>You must be holding item to rename</red>"),
11+
ERROR_NO_HAT_ITEMS("error.no-hat-items", "<red>You must be holding an item or have an item in your helmet slot to use this command</red>"),
12+
ERROR_CURSE_OF_BINDING("error.curse-of-binding", "<red>You currently are wearing something with curse of binding! Sorry!</red>"),
13+
ERROR_HAT_NOT_ALLOWED("error.hat-not-allowed", "<red>Hats of this type are not allowed</red>"),
1114
ERROR_NAME_TOO_LONG("error.name-too-long", "<red>Sorry, that item name is too long! The max characters for an item name is <value></red>"),
1215
ERROR_NO_PERMISSION("error.no-permission", "<red>You do not have permission to run this command</red>"),
1316
ERROR_NOT_ENOUGH_ARGUMENTS("error.not-enough-arguments", "<red>You did not provide enough arguments. Please check your syntax and try again</red>"),
1417
ERROR_NOT_IN_RANGE("error.not-in-range", "<red>Sorry, you must provide a number between <min> and <max></red>"),
18+
HAT_SUCCESSFUL("hat.success", "<green>Enjoy your new hat!</green>"),
19+
HAT_REMOVED("hat.removed", "<gray>Your hat has been returned to your inventory</gray>"),
1520
FEED_OTHER("feed.other", "<green>You have fed <target></green>"),
1621
FEED_SELF("feed.self", "<green>You have been fed</green>"),
1722
FLY_SET_BY_OTHER("fly.by-other", "<green>Your fly has been <value></green>"),

src/main/resources/config.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@ flight:
1313
gamemode: true
1414
rename:
1515
max-characters: 50 #Going over 50 characters may cause unwanted visual effects such as the
16-
# tooltip going off the screen
16+
# tooltip going off the screen
17+
hat:
18+
respect-curse-of-binding: true
19+
disabled-items:
20+
- DEBUG_STICK

src/main/resources/plugin.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ author: Simplexity
66
api-version: '1.20'
77

88
commands:
9+
hat:
10+
permission: adminhax.commands.hat
11+
description: Allows players to put items on their helmet slot
912
flyspeed:
1013
permission: adminhax.commands.speed.fly
1114
description: Change your flight speed
@@ -53,7 +56,6 @@ permissions:
5356
adminhax.commands.godmode: true
5457
adminhax.commands.broadcast: true
5558
adminhax.commands.rename: true
56-
adminhax.reload: true
5759

5860
adminhax.commands.speed:
5961
description: Permissions for speed commands
@@ -149,7 +151,9 @@ permissions:
149151
adminhax.commands.rename.format.obfuscated:
150152
default: op
151153
description: Use obfuscated format
152-
154+
adminhax.commands.hat:
155+
default: op
156+
description: Allows a user to put items in their helmet slot with the /hat command
153157
adminhax.reload:
154158
default: op
155159
description: Reload the plugin

0 commit comments

Comments
 (0)