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

Did the requested changes + improvements.

This commit is contained in:
LinoxGH 2020-06-27 15:58:36 +03:00
parent 73973a00b7
commit b5ef8ff019
6 changed files with 178 additions and 50 deletions

View File

@ -0,0 +1,48 @@
package io.github.thebusybiscuit.slimefun4.core.attributes;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.EntityDamageEvent;
import io.github.thebusybiscuit.slimefun4.implementation.items.armor.HazmatArmorPiece;
import io.github.thebusybiscuit.slimefun4.implementation.items.armor.SlimefunArmorPiece;
/**
* Implement this interface for any {@link SlimefunArmorPiece} to prevent
* the {@link Player} wearing that {@link SlimefunArmorPiece}
*
* <b>Important</b>: This will not cancel any {@link EntityDamageEvent}.
* It will simply prevent Slimefun from ever applying {@link ProtectionType}
* to this {@link SlimefunArmorPiece}'s wearer.
*
* @author Linox
*
* @see SlimefunArmorPiece
* @see HazmatArmorPiece
* @see ItemAttribute
*
*/
public interface CustomProtection extends ItemAttribute {
/**
* This returns the {@link ProtectionType}s this {@link ItemAttribute}
* prevents the assigned {@link SlimefunArmorPiece} to be damaged by.
*
* @return The {@link ProtectionType}s
*/
ProtectionType[] getProtectionTypes();
/**
* This returns the {@link ProtectionType}s this {@link ItemAttribute}
* prevents the assigned {@link SlimefunArmorPiece} to be damaged by.
*
* @return The {@link ProtectionType}s
*/
boolean requireFullSet();
enum ProtectionType {
RADIATION,
BEES;
}
}

View File

@ -0,0 +1,30 @@
package io.github.thebusybiscuit.slimefun4.implementation.items.armor;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import io.github.thebusybiscuit.slimefun4.core.attributes.CustomProtection;
import me.mrCookieSlime.Slimefun.Lists.RecipeType;
import me.mrCookieSlime.Slimefun.Objects.Category;
import me.mrCookieSlime.Slimefun.api.SlimefunItemStack;
public class HazmatArmorPiece extends SlimefunArmorPiece implements CustomProtection {
private final ProtectionType[] types;
public HazmatArmorPiece(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, PotionEffect[] effects, String setID) {
super(category, item, recipeType, recipe, effects, setID);
types = new ProtectionType[] {ProtectionType.BEES, ProtectionType.RADIATION};
}
@Override
public ProtectionType[] getProtectionTypes() {
return types;
}
@Override
public boolean requireFullSet() {
return true;
}
}

View File

@ -11,6 +11,7 @@ import me.mrCookieSlime.Slimefun.api.SlimefunItemStack;
public class SlimefunArmorPiece extends SlimefunItem {
private String setID = null;
private final PotionEffect[] effects;
public SlimefunArmorPiece(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, PotionEffect[] effects) {
@ -19,6 +20,12 @@ public class SlimefunArmorPiece extends SlimefunItem {
this.effects = effects == null ? new PotionEffect[0] : effects;
}
public SlimefunArmorPiece(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, PotionEffect[] effects, String setID) {
this(category, item, recipeType, recipe, effects);
this.setID = setID;
}
/**
* An Array of {@link PotionEffect PotionEffects} which get applied to a {@link Player} wearing
* this {@link SlimefunArmorPiece}.
@ -29,4 +36,12 @@ public class SlimefunArmorPiece extends SlimefunItem {
return effects;
}
/**
* This returns the armor set ID of this {@link SlimefunArmorPiece}.
*
* @return The set ID, <code>null</code> if no set ID is found.
*/
public String getSetID() {
return setID;
}
}

View File

@ -2,6 +2,7 @@ package io.github.thebusybiscuit.slimefun4.implementation.listeners;
import java.util.Optional;
import io.github.thebusybiscuit.slimefun4.core.attributes.CustomProtection;
import org.bukkit.entity.Bee;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@ -12,6 +13,7 @@ import org.bukkit.inventory.ItemStack;
import io.github.thebusybiscuit.cscorelib2.inventory.ItemUtils;
import io.github.thebusybiscuit.slimefun4.api.items.HashedArmorpiece;
import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile;
import io.github.thebusybiscuit.slimefun4.implementation.items.armor.HazmatArmorPiece;
import io.github.thebusybiscuit.slimefun4.implementation.items.armor.SlimefunArmorPiece;
import me.mrCookieSlime.Slimefun.SlimefunPlugin;
@ -30,39 +32,55 @@ public class BeeListener implements Listener {
@EventHandler
public void onDamage(EntityDamageByEntityEvent e) {
if (e.getDamager() instanceof Bee) {
if (e.getEntity() instanceof Player) {
if (e.getDamager() instanceof Bee && e.getEntity() instanceof Player) {
Player p = (Player) e.getEntity();
PlayerProfile.get(p, profile -> {
Optional<PlayerProfile> optional = PlayerProfile.find(p);
if (!optional.isPresent()) {
PlayerProfile.request(p);
return;
}
PlayerProfile profile = optional.get();
HashedArmorpiece[] armors = profile.getArmor();
if (hasFullHazmat(armors)) {
if (shouldProtect(armors)) {
for (ItemStack armor : p.getInventory().getArmorContents()) {
ItemUtils.damageItem(armor, 1, false);
}
e.setDamage(0D);
}
});
}
}
}
private boolean hasFullHazmat(HashedArmorpiece[] armors) {
int hazmatCount = 0;
private boolean shouldProtect(HashedArmorpiece[] armors) {
int armorCount = 0;
boolean first = true;
// Check for a Hazmat Suit
String setID = null;
for (HashedArmorpiece armor : armors) {
Optional<SlimefunArmorPiece> armorPiece = armor.getItem();
if (!armorPiece.isPresent()) return false;
if (armorPiece.get().getID().equals("SCUBA_HELMET") ||
armorPiece.get().getID().equals("HAZMAT_CHESTPLATE") ||
armorPiece.get().getID().equals("HAZMAT_LEGGINGS") ||
armorPiece.get().getID().equals("RUBBER_BOOTS")) {
hazmatCount++;
if (armorPiece.get() instanceof CustomProtection) {
CustomProtection protectedArmor = (CustomProtection) armorPiece.get();
if (first) {
if (protectedArmor.requireFullSet()) setID = armorPiece.get().getSetID();
first = false;
}
for (CustomProtection.ProtectionType protectionType : protectedArmor.getProtectionTypes()) {
if (protectionType == CustomProtection.ProtectionType.BEES) {
if (setID == null) {
return true;
}
armorCount++;
}
}
return hazmatCount == 4;
}
}
return armorCount == 4;
}
}

View File

@ -3,6 +3,7 @@ package io.github.thebusybiscuit.slimefun4.implementation.setup;
import java.util.ArrayList;
import java.util.List;
import io.github.thebusybiscuit.slimefun4.implementation.items.armor.HazmatArmorPiece;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
@ -903,22 +904,22 @@ public final class SlimefunItemSetup {
SlimefunItems.GILDED_IRON_HELMET, SlimefunItems.GILDED_IRON_CHESTPLATE, SlimefunItems.GILDED_IRON_LEGGINGS, SlimefunItems.GILDED_IRON_BOOTS
}, "GILDED_IRON", false, plugin);
new SlimefunArmorPiece(categories.armor, SlimefunItems.SCUBA_HELMET, RecipeType.ARMOR_FORGE,
new HazmatArmorPiece(categories.armor, SlimefunItems.SCUBA_HELMET, RecipeType.ARMOR_FORGE,
new ItemStack[] {new ItemStack(Material.ORANGE_WOOL), new ItemStack(Material.ORANGE_WOOL), new ItemStack(Material.ORANGE_WOOL), new ItemStack(Material.BLACK_WOOL), new ItemStack(Material.GLASS_PANE), new ItemStack(Material.BLACK_WOOL), null, null, null},
new PotionEffect[] {new PotionEffect(PotionEffectType.WATER_BREATHING, 300, 1)})
new PotionEffect[] {new PotionEffect(PotionEffectType.WATER_BREATHING, 300, 1)}, "HAZMAT_SUIT")
.register(plugin);
new SlimefunArmorPiece(categories.armor, SlimefunItems.HAZMAT_CHESTPLATE, RecipeType.ARMOR_FORGE,
new HazmatArmorPiece(categories.armor, SlimefunItems.HAZMAT_CHESTPLATE, RecipeType.ARMOR_FORGE,
new ItemStack[] {new ItemStack(Material.ORANGE_WOOL), null, new ItemStack(Material.ORANGE_WOOL), new ItemStack(Material.ORANGE_WOOL), new ItemStack(Material.ORANGE_WOOL), new ItemStack(Material.ORANGE_WOOL), new ItemStack(Material.BLACK_WOOL), new ItemStack(Material.BLACK_WOOL), new ItemStack(Material.BLACK_WOOL)},
new PotionEffect[] {new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 300, 1)})
new PotionEffect[] {new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 300, 1)}, "HAZMAT_SUIT")
.register(plugin);
new SlimefunArmorPiece(categories.armor, SlimefunItems.HAZMAT_LEGGINGS, RecipeType.ARMOR_FORGE,
new ItemStack[] {new ItemStack(Material.BLACK_WOOL), new ItemStack(Material.BLACK_WOOL), new ItemStack(Material.BLACK_WOOL), new ItemStack(Material.ORANGE_WOOL), null, new ItemStack(Material.ORANGE_WOOL), new ItemStack(Material.ORANGE_WOOL), null, new ItemStack(Material.ORANGE_WOOL)}, null)
new HazmatArmorPiece(categories.armor, SlimefunItems.HAZMAT_LEGGINGS, RecipeType.ARMOR_FORGE,
new ItemStack[] {new ItemStack(Material.BLACK_WOOL), new ItemStack(Material.BLACK_WOOL), new ItemStack(Material.BLACK_WOOL), new ItemStack(Material.ORANGE_WOOL), null, new ItemStack(Material.ORANGE_WOOL), new ItemStack(Material.ORANGE_WOOL), null, new ItemStack(Material.ORANGE_WOOL)}, null, "HAZMAT_SUIT")
.register(plugin);
new SlimefunArmorPiece(categories.armor, SlimefunItems.RUBBER_BOOTS, RecipeType.ARMOR_FORGE,
new ItemStack[] {null, null, null, new ItemStack(Material.BLACK_WOOL), null, new ItemStack(Material.BLACK_WOOL), new ItemStack(Material.BLACK_WOOL), null, new ItemStack(Material.BLACK_WOOL)}, null)
new HazmatArmorPiece(categories.armor, SlimefunItems.RUBBER_BOOTS, RecipeType.ARMOR_FORGE,
new ItemStack[] {null, null, null, new ItemStack(Material.BLACK_WOOL), null, new ItemStack(Material.BLACK_WOOL), new ItemStack(Material.BLACK_WOOL), null, new ItemStack(Material.BLACK_WOOL)}, null, "HAZMAT_SUIT")
.register(plugin);
new SlimefunItem(categories.misc, SlimefunItems.CRUSHED_ORE, RecipeType.ORE_CRUSHER,

View File

@ -15,6 +15,7 @@ import org.bukkit.potion.PotionEffectType;
import io.github.thebusybiscuit.slimefun4.api.items.HashedArmorpiece;
import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile;
import io.github.thebusybiscuit.slimefun4.core.attributes.CustomProtection;
import io.github.thebusybiscuit.slimefun4.core.attributes.Radioactive;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunItems;
import io.github.thebusybiscuit.slimefun4.implementation.items.armor.SlimefunArmorPiece;
@ -64,7 +65,7 @@ public class ArmorTask implements Runnable {
checkForSolarHelmet(p);
}
checkForRadiation(profile);
checkForRadiation(p, profile);
});
}
}
@ -120,35 +121,19 @@ public class ArmorTask implements Runnable {
return (world.getTime() < 12300 || world.getTime() > 23850) && p.getEyeLocation().getBlock().getLightFromSky() == 15;
}
private void checkForRadiation(PlayerProfile profile) {
private void checkForRadiation(Player p, PlayerProfile profile) {
HashedArmorpiece[] armor = profile.getArmor();
Player p = profile.getPlayer();
// Check for a Hazmat Suit
boolean hasHazmat = false;
for (HashedArmorpiece armorPiece : armor) {
Optional<SlimefunArmorPiece> sfArmor = armorPiece.getItem();
if (!sfArmor.isPresent()) continue;
if (sfArmor.get().getID().equals("SCUBA_HELMET") ||
sfArmor.get().getID().equals("HAZMAT_CHESTPLATE") ||
sfArmor.get().getID().equals("HAZMAT_LEGGINGS") ||
sfArmor.get().getID().equals("RUBBER_BOOTS")) {
hasHazmat = true;
}
}
if (!hasHazmat) {
if (!shouldProtect(armor)) {
for (ItemStack item : p.getInventory()) {
if (isRadioactive(p, item)) {
if (checkAndApplyRadioactive(p, item)) {
break;
}
}
}
}
private boolean isRadioactive(Player p, ItemStack item) {
private boolean checkAndApplyRadioactive(Player p, ItemStack item) {
for (SlimefunItem radioactiveItem : SlimefunPlugin.getRegistry().getRadioactiveItems()) {
if (radioactiveItem.isItem(item) && Slimefun.isEnabled(p, radioactiveItem, true)) {
// If the item is enabled in the world, then make radioactivity do its job
@ -166,4 +151,35 @@ public class ArmorTask implements Runnable {
return false;
}
private boolean shouldProtect(HashedArmorpiece[] armors) {
int armorCount = 0;
boolean first = true;
String setID = null;
for (HashedArmorpiece armor : armors) {
Optional<SlimefunArmorPiece> armorPiece = armor.getItem();
if (!armorPiece.isPresent()) return false;
if (armorPiece.get() instanceof CustomProtection) {
CustomProtection protectedArmor = (CustomProtection) armorPiece.get();
if (first) {
if (protectedArmor.requireFullSet()) setID = armorPiece.get().getSetID();
first = false;
}
for (CustomProtection.ProtectionType protectionType : protectedArmor.getProtectionTypes()) {
if (protectionType == CustomProtection.ProtectionType.RADIATION) {
if (setID == null) {
return true;
}
armorCount++;
}
}
}
}
return armorCount == 4;
}
}