Skip to content
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

[Chore] Prevent Specific Slimefun Items from being made Unbreakable #82

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/me/gallowsdove/foxymachines/FoxyMachines.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import me.gallowsdove.foxymachines.commands.QuestCommand;
import me.gallowsdove.foxymachines.commands.SacrificialAltarCommand;
import me.gallowsdove.foxymachines.commands.SummonCommand;
import me.gallowsdove.foxymachines.implementation.consumables.UnbreakableRune;
import me.gallowsdove.foxymachines.implementation.machines.ForcefieldDome;
import me.gallowsdove.foxymachines.implementation.tools.BerryBushTrimmer;
import me.gallowsdove.foxymachines.listeners.*;
Expand Down Expand Up @@ -51,6 +52,7 @@ public void enable() {

QuestUtils.init();
AbstractWand.init();
UnbreakableRune.init();
ItemSetup.INSTANCE.init();
ResearchSetup.INSTANCE.init();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package me.gallowsdove.foxymachines.implementation.consumables;

import io.github.mooy1.infinitylib.common.Scheduler;
import io.github.mooy1.infinitylib.core.AddonConfig;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem;
import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType;
import io.github.thebusybiscuit.slimefun4.core.handlers.ItemDropHandler;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunItems;
import io.github.thebusybiscuit.slimefun4.implementation.items.SimpleSlimefunItem;
import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils;
import me.gallowsdove.foxymachines.FoxyMachines;
import me.gallowsdove.foxymachines.Items;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
Expand All @@ -20,12 +24,34 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.logging.Level;

public class UnbreakableRune extends SimpleSlimefunItem<ItemDropHandler> {

private static final Map<String, Set<String>> BLACKLIST = new HashMap<>();
private static final double RANGE = 1.5;

public static void init() {
if (!BLACKLIST.isEmpty()) {
FoxyMachines.log(Level.WARNING, "Attempted to initialize UnbreakableRune after already initialized!");
return;
}

AddonConfig config = FoxyMachines.getInstance().getConfig();
ConfigurationSection blacklist = config.getConfigurationSection("unbreakable-rune-blacklist");
if (blacklist == null) {
return;
}

for (String addon : blacklist.getKeys(false)) {
BLACKLIST.put(addon, new HashSet<>(blacklist.getStringList(addon)));
}
}

public UnbreakableRune() {
super(Items.TOOLS_ITEM_GROUP, Items.UNBREAKABLE_RUNE, RecipeType.ANCIENT_ALTAR, new ItemStack[] {
Expand Down Expand Up @@ -59,7 +85,7 @@ private void activate(@Nonnull Player p, @Nonnull Item rune) {
}

Location l = rune.getLocation();
Collection<Entity> entities = l.getWorld().getNearbyEntities(l, RANGE, RANGE, RANGE, this::findCompatibleItem);
Collection<Entity> entities = l.getWorld().getNearbyEntities(l, RANGE, RANGE, RANGE, entity -> findCompatibleItem(p, entity));
Optional<Entity> optional = entities.stream().findFirst();

if (optional.isPresent()) {
Expand Down Expand Up @@ -92,9 +118,10 @@ private void activate(@Nonnull Player p, @Nonnull Item rune) {
}
}

private boolean findCompatibleItem(Entity entity) {
private boolean findCompatibleItem(Player player, Entity entity) {
if (entity instanceof Item item) {
return !isUnbreakable(item.getItemStack()) && !isItem(item.getItemStack());
ItemStack itemStack = item.getItemStack();
return !isUnbreakable(itemStack) && !isItem(itemStack) && !isDisallowed(player, itemStack);
}

return false;
Expand All @@ -103,7 +130,7 @@ private boolean findCompatibleItem(Entity entity) {
public static void setUnbreakable(@Nullable ItemStack item) {
if (item != null && item.getType() != Material.AIR) {

if (!isUnbreakable(item)) {
if (!isUnbreakable(item) && item.hasItemMeta()) {
ItemMeta meta = item.getItemMeta();

meta.setUnbreakable(true);
Expand All @@ -123,4 +150,19 @@ public static boolean isUnbreakable(@Nullable ItemStack item) {
return false;
}
}

public static boolean isDisallowed(Player player, ItemStack itemStack) {
final SlimefunItem slimefunItem = SlimefunItem.getByItem(itemStack);
if (slimefunItem == null) {
return false;
}

final String id = slimefunItem.getId();
final String addon = slimefunItem.getAddon().getName();
if (BLACKLIST.containsKey(addon) && BLACKLIST.get(addon).contains(id)) {
player.sendMessage(ChatColor.LIGHT_PURPLE + "You can't make this item unbreakable!");
return true;
}
return false;
}
}
11 changes: 10 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
#
# quest-mobs
# All mob types that can be required for a quest, for a full list of them go to: https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/entity/EntityType.html
#
# unbreakable-rune-blacklist
# Any items from addons to blacklist from being usable with the unbreakable rune
# for ex, by default the strainers from InfinityExpansion are disabled as they do not respect the unbreakable setting

auto-update: true
max-chunk-loaders: 8
Expand Down Expand Up @@ -101,4 +105,9 @@ quest-mobs:
- ZOGLIN
- ZOMBIE
- ZOMBIFIED_PIGLIN
- FOX
- FOX
unbreakable-rune-blacklist:
InfinityExpansion:
- BASIC_STRAINER
- ADVANCED_STRAINER
- REINFORCED_STRAINER
Loading