1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-20 11:45:51 +00:00

Rebased and re-commital of Piglin barter recipe addition.

This commit is contained in:
dniym 2020-07-25 20:33:32 -04:00
parent 1fbacdc656
commit 9093e292a1
4 changed files with 65 additions and 6 deletions

View File

@ -69,6 +69,7 @@ public class SlimefunRegistry {
private final Set<String> tickers = new HashSet<>(); private final Set<String> tickers = new HashSet<>();
private final Set<SlimefunItem> radioactive = new HashSet<>(); private final Set<SlimefunItem> radioactive = new HashSet<>();
private final Set<String> activeChunks = ConcurrentHashMap.newKeySet(); private final Set<String> activeChunks = ConcurrentHashMap.newKeySet();
private final Set<ItemStack> barterDrops = new HashSet<>();
private final KeyMap<GEOResource> geoResources = new KeyMap<>(); private final KeyMap<GEOResource> geoResources = new KeyMap<>();
@ -199,6 +200,9 @@ public class SlimefunRegistry {
return mobDrops; return mobDrops;
} }
public Set<ItemStack> getBarterDrops() {
return barterDrops;
}
public Set<SlimefunItem> getRadioactiveItems() { public Set<SlimefunItem> getRadioactiveItems() {
return radioactive; return radioactive;
} }

View File

@ -2,7 +2,6 @@ package io.github.thebusybiscuit.slimefun4.core.attributes;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.entity.EntityDeathEvent;
import io.github.thebusybiscuit.slimefun4.implementation.items.misc.BasicCircuitBoard; import io.github.thebusybiscuit.slimefun4.implementation.items.misc.BasicCircuitBoard;
import io.github.thebusybiscuit.slimefun4.implementation.listeners.MobDropListener; import io.github.thebusybiscuit.slimefun4.implementation.listeners.MobDropListener;
@ -11,8 +10,8 @@ import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
/** /**
* This interface, when attached to a {@link SlimefunItem}, provides an easy method for adding * This interface, when attached to a {@link SlimefunItem}, provides an easy method for adding
* a % chance to drop for an {@link SlimefunItem} on {@link EntityDeathEvent}, this chance is 0-100 * a % chance to drop for an {@link SlimefunItem} on {@link entityDeathEvent} or {@link EntityItemDropEvent}, this chance is 0-100
* and used in conjunction with the MOB_DROP {@link RecipeType}. * and used in conjunction with the MOB_DROP {@link RecipeType} or the BARTER_DROP {@link RecipeType}.
* *
* @author dNiym * @author dNiym
* *
@ -26,7 +25,15 @@ public interface RandomMobDrop extends ItemAttribute {
/** /**
* Implement this method to make the object have a variable chance of being * Implement this method to make the object have a variable chance of being
* added to the dropList when {@link EntityType} (specified in the recipe) * added to the dropList when {@link EntityType} (specified in the recipe)
* is killed by the {@link Player} * is killed by the {@link Player} for a MOB_DROP {@link RecipeType}.
*
* Alternatively if the {@link RecipeType} is set to BARTER_DROP the item
* will then have its % chance to be dropped only by bartering with the
* Piglin creatures.
*
* It is recommended that this chance is kept reasonably low to feel like
* a vanilla drop as a 100% chance will completely override all piglin
* barter drops. (NOTE: this feature only exists in 1.16+)
* *
* @return The integer chance (0-100%) {@link SlimefunItem} has to drop. * @return The integer chance (0-100%) {@link SlimefunItem} has to drop.
*/ */

View File

@ -1,16 +1,22 @@
package io.github.thebusybiscuit.slimefun4.implementation.listeners; package io.github.thebusybiscuit.slimefun4.implementation.listeners;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.bukkit.entity.Item;
import org.bukkit.entity.Piglin; import org.bukkit.entity.Piglin;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDropItemEvent;
import org.bukkit.event.entity.EntityPickupItemEvent; import org.bukkit.event.entity.EntityPickupItemEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent; import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import io.github.thebusybiscuit.slimefun4.core.attributes.RandomMobDrop;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
@ -20,6 +26,18 @@ import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
* @author poma123 * @author poma123
* *
*/ */
/**
* Also {@link Listener} Listens to the EntityDropItemEvent event to inject a {@link RandomMobDrop} if its dropChance() check passes.
* this would only be possible if the {@link RecipeType} is of PiglinBarter type.
*
* @author dNiym
*
* @see getBarterDrops
* @see RandomMobDrop
*
*/
public class PiglinListener implements Listener { public class PiglinListener implements Listener {
public PiglinListener(SlimefunPlugin plugin) { public PiglinListener(SlimefunPlugin plugin) {
@ -38,6 +56,29 @@ public class PiglinListener implements Listener {
} }
} }
@EventHandler
public void onPiglinDropItem(EntityDropItemEvent e) {
if (e.getEntity() instanceof Piglin) {
Set<ItemStack> drops = SlimefunPlugin.getRegistry().getBarterDrops();
/*
* 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);
if (sfi instanceof RandomMobDrop && ((RandomMobDrop)sfi).getMobDropChance() >= ThreadLocalRandom.current().nextInt(100)) {
Item drop = e.getEntity().getWorld().dropItemNaturally(((Piglin)e.getEntity()).getEyeLocation(), sfi.getItem());
drop.setVelocity(e.getItemDrop().getVelocity());
e.getItemDrop().remove();
return;
}
}
}
}
@EventHandler @EventHandler
public void onInteractEntity(PlayerInteractEntityEvent e) { public void onInteractEntity(PlayerInteractEntityEvent e) {
if (!e.getRightClicked().isValid() || e.getRightClicked().getType() != EntityType.PIGLIN) { if (!e.getRightClicked().isValid() || e.getRightClicked().getType() != EntityType.PIGLIN) {

View File

@ -19,6 +19,7 @@ import org.bukkit.inventory.ItemStack;
import io.github.thebusybiscuit.cscorelib2.item.CustomItem; import io.github.thebusybiscuit.cscorelib2.item.CustomItem;
import io.github.thebusybiscuit.cscorelib2.recipes.MinecraftRecipe; import io.github.thebusybiscuit.cscorelib2.recipes.MinecraftRecipe;
import io.github.thebusybiscuit.slimefun4.api.MinecraftVersion;
import io.github.thebusybiscuit.slimefun4.core.multiblocks.MultiBlockMachine; import io.github.thebusybiscuit.slimefun4.core.multiblocks.MultiBlockMachine;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunItems; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunItems;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
@ -47,6 +48,7 @@ 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 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 HEATED_PRESSURE_CHAMBER = new RecipeType(new NamespacedKey(SlimefunPlugin.instance(), "heated_pressure_chamber"), SlimefunItems.HEATED_PRESSURE_CHAMBER); 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_FABRICATOR = new RecipeType(new NamespacedKey(SlimefunPlugin.instance(), "food_fabricator"), SlimefunItems.FOOD_FABRICATOR);
@ -141,6 +143,11 @@ public class RecipeType implements Keyed {
return key; return key;
} }
private static void registerBarterDrop(ItemStack[] recipe, ItemStack output) {
if (SlimefunPlugin.getMinecraftVersion().isAtLeast(MinecraftVersion.MINECRAFT_1_16))
SlimefunPlugin.getRegistry().getBarterDrops().add(output);
}
private static void registerMobDrop(ItemStack[] recipe, ItemStack output) { private static void registerMobDrop(ItemStack[] recipe, ItemStack output) {
String mob = ChatColor.stripColor(recipe[4].getItemMeta().getDisplayName()).toUpperCase(Locale.ROOT).replace(' ', '_'); String mob = ChatColor.stripColor(recipe[4].getItemMeta().getDisplayName()).toUpperCase(Locale.ROOT).replace(' ', '_');
EntityType entity = EntityType.valueOf(mob); EntityType entity = EntityType.valueOf(mob);