-
-
Notifications
You must be signed in to change notification settings - Fork 44
Rename locking #893
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MrJeremyFisher
wants to merge
4
commits into
CivMC:main
Choose a base branch
from
MrJeremyFisher:rename-locking
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+168
−1
Open
Rename locking #893
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
...er/src/main/java/com/programmerdan/minecraft/simpleadminhacks/hacks/basic/NameLocker.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| package com.programmerdan.minecraft.simpleadminhacks.hacks.basic; | ||
|
|
||
| import com.programmerdan.minecraft.simpleadminhacks.SimpleAdminHacks; | ||
| import com.programmerdan.minecraft.simpleadminhacks.framework.BasicHack; | ||
| import com.programmerdan.minecraft.simpleadminhacks.framework.BasicHackConfig; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
| import net.kyori.adventure.text.Component; | ||
| import net.kyori.adventure.text.format.NamedTextColor; | ||
| import net.minecraft.world.inventory.AnvilMenu; | ||
| import org.bukkit.Material; | ||
| import org.bukkit.NamespacedKey; | ||
| import org.bukkit.entity.Player; | ||
| import org.bukkit.event.EventHandler; | ||
| import org.bukkit.event.inventory.InventoryClickEvent; | ||
| import org.bukkit.event.inventory.InventoryType; | ||
| import org.bukkit.event.inventory.PrepareAnvilEvent; | ||
| import org.bukkit.inventory.ItemStack; | ||
| import org.bukkit.inventory.meta.ItemMeta; | ||
| import org.bukkit.persistence.PersistentDataContainer; | ||
| import org.bukkit.persistence.PersistentDataType; | ||
|
|
||
| public class NameLocker extends BasicHack { | ||
|
|
||
| final NamespacedKey lockedKey = new NamespacedKey(plugin, "name-locked"); | ||
|
|
||
| public NameLocker(SimpleAdminHacks plugin, BasicHackConfig config) { | ||
| super(plugin, config); | ||
| } | ||
|
|
||
| @Override | ||
| public void onEnable() { | ||
| super.onEnable(); | ||
| if (plugin.getServer().getPluginManager().getPlugin("FactoryMod") != null) { | ||
| plugin.getServer() | ||
| .getPluginManager() | ||
| .registerEvents( | ||
| new NameLockerFMListener(lockedKey), plugin); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onDisable() { | ||
| super.onDisable(); | ||
| } | ||
|
|
||
| @EventHandler | ||
| public void onPrepareLock(PrepareAnvilEvent event) { | ||
| if (event.getInventory().getSecondItem() == null || !event.getInventory().getSecondItem().getType().equals(Material.HONEYCOMB)) | ||
| return; | ||
|
|
||
| ItemStack itemToBeLocked = event.getInventory().getFirstItem(); | ||
|
|
||
| if (itemToBeLocked == null) return; | ||
|
|
||
| ItemMeta itemMeta = itemToBeLocked.getItemMeta(); | ||
| PersistentDataContainer pdc = itemMeta.getPersistentDataContainer(); | ||
|
|
||
| if (pdc.has(lockedKey) && Boolean.TRUE.equals(pdc.get(lockedKey, PersistentDataType.BOOLEAN))) { // Item is locked | ||
| event.getView().setRepairCost(AnvilMenu.DEFAULT_DENIED_COST); // Cheap trick to get the X because it's nice. | ||
| event.setResult(ItemStack.empty()); | ||
| } else if (!pdc.has(lockedKey)) { | ||
| // We can't put honeycomb on anything so we have to manually do the result setup. | ||
| ItemStack result = event.getInventory().getFirstItem().clone(); | ||
| result.editMeta(meta -> { | ||
| meta.setDisplayName(event.getView().getRenameText()); | ||
| meta.getPersistentDataContainer().set(lockedKey, PersistentDataType.BOOLEAN, true); | ||
| meta.lore(List.of(Component.text().content("Name-locked").build())); // Just to make it clear. If we can change the message in the anvil this should be removed. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we want to wipe the lore for these kinds of items? |
||
| }); | ||
| event.getView().setRepairCost(1); // We need to do this so the client will let the user withdraw the item | ||
| event.setResult(result); | ||
| } | ||
| } | ||
|
|
||
| @EventHandler | ||
| public void onExecuteLock(InventoryClickEvent event) { | ||
| if (event.getInventory().getType() != InventoryType.ANVIL || event.getRawSlot() != 2) return; | ||
| ItemStack honeyComb = event.getInventory().getItem(1); | ||
| if (honeyComb == null || !honeyComb.getType().equals(Material.HONEYCOMB)) | ||
| return; | ||
|
|
||
| if (honeyComb.getAmount() > 1) { | ||
| honeyComb.setAmount(honeyComb.getAmount() - 1); | ||
| ((Player) event.getWhoClicked()).give(honeyComb); // Items in the second slot of an anvil are deleted AFTER InventoryClickEvent so we can't decrement the stack size | ||
| } | ||
| } | ||
|
|
||
| @EventHandler | ||
| public void onTryRename(PrepareAnvilEvent event) { | ||
| ItemStack itemToBeRenamed = event.getInventory().getFirstItem(); | ||
| ItemStack resultItem = event.getInventory().getResult(); | ||
| if (itemToBeRenamed == null | ||
| || resultItem == null) | ||
| return; | ||
|
|
||
| ItemMeta itemMeta = itemToBeRenamed.getItemMeta(); | ||
| PersistentDataContainer pdc = itemMeta.getPersistentDataContainer(); | ||
| checkItem(event, itemToBeRenamed, resultItem, pdc); | ||
|
|
||
| // Now check the other way. This only applies for tools where you can combine them | ||
| itemToBeRenamed = event.getInventory().getSecondItem(); | ||
| if (itemToBeRenamed == null) | ||
| return; | ||
|
|
||
| itemMeta = itemToBeRenamed.getItemMeta(); | ||
| pdc = itemMeta.getPersistentDataContainer(); | ||
| checkItem(event, itemToBeRenamed, resultItem, pdc); | ||
| } | ||
|
|
||
| private void checkItem(PrepareAnvilEvent event, ItemStack itemToBeRenamed, ItemStack resultItem, PersistentDataContainer pdc) { | ||
| if (pdc.has(lockedKey) && Boolean.TRUE.equals(pdc.get(lockedKey, PersistentDataType.BOOLEAN))) { // Item is locked | ||
| if (Objects.equals(itemToBeRenamed.effectiveName().compact(), resultItem.effectiveName().compact())) | ||
| return; // No name change, no enchant change (text colour white->aqua) | ||
| if (Objects.equals(itemToBeRenamed.effectiveName().compact().color(NamedTextColor.AQUA).compact(), resultItem.effectiveName().compact())) | ||
| return; // No name change, but we are adding an enchantment so the text colour changes | ||
| event.getView().setRepairCost(AnvilMenu.DEFAULT_DENIED_COST); | ||
| event.setResult(ItemStack.empty()); | ||
| // Can we change the text? Like "Too Expensive!" - > "Can't Rename!"? | ||
| } | ||
| } | ||
| } | ||
43 changes: 43 additions & 0 deletions
43
...n/java/com/programmerdan/minecraft/simpleadminhacks/hacks/basic/NameLockerFMListener.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package com.programmerdan.minecraft.simpleadminhacks.hacks.basic; | ||
|
|
||
| import com.github.igotyou.FactoryMod.events.FactoryActivateEvent; | ||
| import com.github.igotyou.FactoryMod.factories.FurnCraftChestFactory; | ||
| import net.kyori.adventure.text.Component; | ||
| import net.kyori.adventure.text.format.NamedTextColor; | ||
| import org.bukkit.NamespacedKey; | ||
| import org.bukkit.event.EventHandler; | ||
| import org.bukkit.event.Listener; | ||
| import org.bukkit.inventory.ItemStack; | ||
| import org.bukkit.inventory.meta.ItemMeta; | ||
| import org.bukkit.persistence.PersistentDataContainer; | ||
| import org.bukkit.persistence.PersistentDataType; | ||
|
|
||
| public class NameLockerFMListener implements Listener { | ||
|
|
||
| NamespacedKey lockedKey; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this package private? Should be private and final ideally |
||
|
|
||
| public NameLockerFMListener(NamespacedKey lockedKey) { | ||
| this.lockedKey = lockedKey; | ||
| } | ||
|
|
||
| @EventHandler | ||
| public void onTryWordbank(FactoryActivateEvent event) { | ||
| if (!(event.getFactory() instanceof FurnCraftChestFactory fac)) return; | ||
| if (!fac.getCurrentRecipe().getTypeIdentifier().equals("WORDBANK")) return; | ||
|
|
||
| ItemStack itemToBeRenamed = fac.getInputInventory().getItem(0); | ||
|
|
||
| if (itemToBeRenamed == null) | ||
| return; | ||
|
|
||
| ItemMeta itemMeta = itemToBeRenamed.getItemMeta(); | ||
| PersistentDataContainer pdc = itemMeta.getPersistentDataContainer(); | ||
|
|
||
| if (pdc.has(lockedKey) && Boolean.TRUE.equals(pdc.get(lockedKey, PersistentDataType.BOOLEAN))) { // Item is locked | ||
| event.getActivator().sendMessage(Component.text() | ||
| .color(NamedTextColor.RED) | ||
| .content("You cannot wordbank name-locked items")); | ||
| event.setCancelled(true); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
package private?