diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/SlimefunRegistry.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/SlimefunRegistry.java index a9dad52e0..b6bc8f953 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/SlimefunRegistry.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/SlimefunRegistry.java @@ -13,7 +13,9 @@ import java.util.concurrent.ConcurrentHashMap; import org.bukkit.Location; import org.bukkit.Server; +import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; +import org.bukkit.entity.Piglin; import org.bukkit.inventory.ItemStack; import io.github.thebusybiscuit.cscorelib2.collections.KeyMap; @@ -70,7 +72,7 @@ public class SlimefunRegistry { private final Set radioactive = new HashSet<>(); private final Set activeChunks = ConcurrentHashMap.newKeySet(); private final Set barterDrops = new HashSet<>(); - + private final KeyMap geoResources = new KeyMap<>(); private final Map profiles = new ConcurrentHashMap<>(); @@ -196,14 +198,26 @@ public class SlimefunRegistry { return layouts.get(layout); } + /** + * This returns a {@link Map} connecting the {@link EntityType} with a {@link Set} + * of {@link ItemStack ItemStacks} which would be dropped when an {@link Entity} of that type was killed. + * + * @return The {@link Map} of custom mob drops + */ public Map> getMobDrops() { return mobDrops; } - public Set getBarterDrops() { + /** + * This returns a {@link Set} of {@link ItemStack ItemStacks} which can be obtained by bartering + * with {@link Piglin Piglins}. + * + * @return A {@link Set} of bartering drops + */ + public Set getBarteringDrops() { return barterDrops; } - + public Set getRadioactiveItems() { return radioactive; } @@ -271,7 +285,7 @@ public class SlimefunRegistry { public Map getAutomatedCraftingChamberRecipes() { return automatedCraftingChamberRecipes; } - + public boolean logDuplicateBlockEntries() { return logDuplicateBlockEntries; } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/attributes/PiglinBarterDrop.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/attributes/PiglinBarterDrop.java index 12649e29d..7cf46be4d 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/attributes/PiglinBarterDrop.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/attributes/PiglinBarterDrop.java @@ -1,37 +1,34 @@ package io.github.thebusybiscuit.slimefun4.core.attributes; -import org.bukkit.entity.EntityType; -import org.bukkit.entity.Player; -import org.bukkit.event.entity.EntityDeathEvent; -import org.bukkit.event.entity.EntityDropItemEvent; +import org.bukkit.entity.Piglin; -import io.github.thebusybiscuit.slimefun4.implementation.items.misc.BasicCircuitBoard; -import io.github.thebusybiscuit.slimefun4.implementation.listeners.MobDropListener; +import io.github.thebusybiscuit.slimefun4.implementation.listeners.PiglinListener; import me.mrCookieSlime.Slimefun.Lists.RecipeType; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; /** * This interface, when attached to a {@link SlimefunItem}, provides a variable (0-100%) chance for - * a {@link SlimefunItem} to be dropped by a {@link Piglin} on {@link EntityItemDropEvent}. + * a {@link SlimefunItem} to be dropped by a {@link Piglin} on {@link EntityItemDropEvent}. * * @author dNiym * * @see PiglinListener + * @see RandomMobDrop * */ @FunctionalInterface public interface PiglinBarterDrop extends ItemAttribute { /** - * Implement this method to make this {@link SlimefunItem} have a variable chance - * of being dropped by a {@link Piglin} when bartering with them. This interface + * Implement this method to make this {@link SlimefunItem} have a variable chance + * of being dropped by a {@link Piglin} when bartering with them. This interface * should be used with the {@link RecipeType#BARTER_DROP}. * * It is recommended that this chance is kept reasonably low to feel like - * a vanilla drop as a 100% chance will completely override all {@link Piglin} - * barter drops. (NOTE: this feature only exists in 1.16+) + * a vanilla drop as a 100% chance will completely override all {@link Piglin} + * barter drops. (NOTE: this feature only exists in 1.16+) * - * @return The integer chance (0-100%) this {@link SlimefunItem} has to drop. + * @return The integer chance (1-99%) this {@link SlimefunItem} has to drop. */ int getBarteringLootChance(); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/attributes/RandomMobDrop.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/attributes/RandomMobDrop.java index a19f6ca8e..136ccb96c 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/attributes/RandomMobDrop.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/attributes/RandomMobDrop.java @@ -3,7 +3,6 @@ package io.github.thebusybiscuit.slimefun4.core.attributes; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import org.bukkit.event.entity.EntityDeathEvent; -import org.bukkit.event.entity.EntityDropItemEvent; import io.github.thebusybiscuit.slimefun4.implementation.items.misc.BasicCircuitBoard; import io.github.thebusybiscuit.slimefun4.implementation.listeners.MobDropListener; @@ -19,6 +18,7 @@ import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; * * @see BasicCircuitBoard * @see MobDropListener + * @see PiglinBarterDrop * */ @FunctionalInterface @@ -26,7 +26,7 @@ public interface RandomMobDrop extends ItemAttribute { /** * Implement this method to make the object have a variable chance of being - * added to the dropList when {@link EntityType} specified in + * added to the dropList when {@link EntityType} specified in * the {@link RecipeType#MOB_DROP} is killed by the {@link Player}. * * @return The integer chance (0-100%) {@link SlimefunItem} has to drop. diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/PiglinListener.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/PiglinListener.java index 5ff5ddc2c..1e5e34f91 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/PiglinListener.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/PiglinListener.java @@ -5,7 +5,6 @@ import java.util.concurrent.ThreadLocalRandom; import org.bukkit.Material; import org.bukkit.entity.EntityType; -import org.bukkit.entity.Item; import org.bukkit.entity.Piglin; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -17,19 +16,19 @@ import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemStack; import io.github.thebusybiscuit.slimefun4.core.attributes.PiglinBarterDrop; -import io.github.thebusybiscuit.slimefun4.core.attributes.RandomMobDrop; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; /** - * This {@link Listener} prevents a {@link Piglin} from bartering with a {@link SlimefunItem} as well as - * listens to the {@link EntityDropItemEvent} to inject a {@link PiglinBarterDrop} if a dropChance() check passes. + * This {@link Listener} prevents a {@link Piglin} from bartering with a + * {@link SlimefunItem}. + * It also listens to the {@link EntityDropItemEvent} to + * inject a {@link PiglinBarterDrop} if the chance check passes. * * @author poma123 * @author dNiym * */ - public class PiglinListener implements Listener { public PiglinListener(SlimefunPlugin plugin) { @@ -51,26 +50,33 @@ public class PiglinListener implements Listener { @EventHandler public void onPiglinDropItem(EntityDropItemEvent e) { if (e.getEntity() instanceof Piglin) { - Piglin piglin = (Piglin) e.getEntity(); - Set drops = SlimefunPlugin.getRegistry().getBarterDrops(); - + Set drops = SlimefunPlugin.getRegistry().getBarteringDrops(); + /* - * NOTE: Getting a new random number each iteration because multiple items could have the same - * % chance to drop, and if one fails all items with that number will fail. Getting a new random number - * will allow multiple items with the same % chance to drop. + * NOTE: Getting a new random number each iteration because multiple items could have the same + * % chance to drop, and if one fails all items with that number will fail. + * Getting a new random number will allow multiple items with the same % chance to drop. */ - + for (ItemStack is : drops) { SlimefunItem sfi = SlimefunItem.getByItem(is); - //Check the getBarteringLootChance and compare against a random number 1-100, if the random number is greater then replace the item. - if (sfi instanceof PiglinBarterDrop && ((PiglinBarterDrop)sfi).getBarteringLootChance() > ThreadLocalRandom.current().nextInt(100)) { - e.getItemDrop().setItemStack(sfi.getItem().clone()); - return; - } + // Check the getBarteringLootChance and compare against a random number 0-100, + // if the random number is greater then replace the item. + if (sfi instanceof PiglinBarterDrop) { + int chance = ((PiglinBarterDrop) sfi).getBarteringLootChance(); + + if (chance < 1 || chance >= 100) { + sfi.warn("The Piglin Bartering chance must be between 1-99%"); + } + else if (chance > ThreadLocalRandom.current().nextInt(100)) { + e.getItemDrop().setItemStack(sfi.getRecipeOutput()); + return; + } + } } } } - + @EventHandler public void onInteractEntity(PlayerInteractEntityEvent e) { if (!e.getRightClicked().isValid() || e.getRightClicked().getType() != EntityType.PIGLIN) { diff --git a/src/main/java/me/mrCookieSlime/Slimefun/Lists/RecipeType.java b/src/main/java/me/mrCookieSlime/Slimefun/Lists/RecipeType.java index d0c3572bb..8b62a1692 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/Lists/RecipeType.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/Lists/RecipeType.java @@ -48,8 +48,8 @@ public class RecipeType implements Keyed { }); public static final RecipeType MOB_DROP = new RecipeType(new NamespacedKey(SlimefunPlugin.instance(), "mob_drop"), new CustomItem(Material.IRON_SWORD, "&bMob Drop"), RecipeType::registerMobDrop, "", "&rKill the specified Mob to obtain this Item"); - public static final RecipeType BARTER_DROP = new RecipeType(new NamespacedKey(SlimefunPlugin.instance(), "barter_drop"), new CustomItem(Material.GOLD_INGOT, "&bBarter Drop"), RecipeType::registerBarterDrop, "&aBarter with piglins for a chance", "&a to obtain this item"); - + public static final RecipeType BARTER_DROP = new RecipeType(new NamespacedKey(SlimefunPlugin.instance(), "barter_drop"), new CustomItem(Material.GOLD_INGOT, "&bBarter Drop"), RecipeType::registerBarterDrop, "&aBarter with piglins for a chance", "&ato obtain this item"); + public static final RecipeType HEATED_PRESSURE_CHAMBER = new RecipeType(new NamespacedKey(SlimefunPlugin.instance(), "heated_pressure_chamber"), SlimefunItems.HEATED_PRESSURE_CHAMBER); public static final RecipeType FOOD_FABRICATOR = new RecipeType(new NamespacedKey(SlimefunPlugin.instance(), "food_fabricator"), SlimefunItems.FOOD_FABRICATOR); public static final RecipeType FOOD_COMPOSTER = new RecipeType(new NamespacedKey(SlimefunPlugin.instance(), "food_composter"), SlimefunItems.FOOD_COMPOSTER); @@ -144,10 +144,11 @@ public class RecipeType implements Keyed { } private static void registerBarterDrop(ItemStack[] recipe, ItemStack output) { - if (SlimefunPlugin.getMinecraftVersion().isAtLeast(MinecraftVersion.MINECRAFT_1_16)) - SlimefunPlugin.getRegistry().getBarterDrops().add(output); + if (SlimefunPlugin.getMinecraftVersion().isAtLeast(MinecraftVersion.MINECRAFT_1_16)) { + SlimefunPlugin.getRegistry().getBarteringDrops().add(output); + } } - + private static void registerMobDrop(ItemStack[] recipe, ItemStack output) { String mob = ChatColor.stripColor(recipe[4].getItemMeta().getDisplayName()).toUpperCase(Locale.ROOT).replace(' ', '_'); EntityType entity = EntityType.valueOf(mob); diff --git a/src/main/resources/languages/recipes_en.yml b/src/main/resources/languages/recipes_en.yml index 9f82f5b76..fc7045efd 100644 --- a/src/main/resources/languages/recipes_en.yml +++ b/src/main/resources/languages/recipes_en.yml @@ -141,6 +141,12 @@ slimefun: - 'Craft this Item as shown' - 'using a Refinery' + barter_drop: + name: 'Piglin Bartering Drop' + lore: + - 'Barter with Piglins using' + - 'Gold Ingots to obtain this Item' + minecraft: shaped: