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

Merge pull request #1076 from LinoxGH/master

Fixes #253
This commit is contained in:
TheBusyBiscuit 2019-09-01 23:21:47 +02:00 committed by GitHub
commit 6dc24d0ff2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 73 additions and 40 deletions

View File

@ -261,6 +261,8 @@ public class SlimefunItem {
if (this instanceof VanillaItem) this.state = State.VANILLA; if (this instanceof VanillaItem) this.state = State.VANILLA;
else this.state = State.DISABLED; else this.state = State.DISABLED;
} }
postRegister();
} catch(Exception x) { } catch(Exception x) {
Slimefun.getLogger().log(Level.WARNING, "Registering the Item '" + id + "' for Slimefun " + Slimefun.getVersion() + " has failed", x); Slimefun.getLogger().log(Level.WARNING, "Registering the Item '" + id + "' for Slimefun " + Slimefun.getVersion() + " has failed", x);
} }
@ -506,6 +508,8 @@ public class SlimefunItem {
ChargableBlock.registerCapacitor(id, capacity); ChargableBlock.registerCapacitor(id, capacity);
} }
public void postRegister() {}
protected void setItem(ItemStack stack) { protected void setItem(ItemStack stack) {
this.item = stack; this.item = stack;
} }

View File

@ -1,11 +1,11 @@
package me.mrCookieSlime.Slimefun.Objects.SlimefunItem.items; package me.mrCookieSlime.Slimefun.Objects.SlimefunItem.items;
import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.Arrow; import org.bukkit.entity.Arrow;
import org.bukkit.entity.Bat; import org.bukkit.entity.Bat;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.bukkit.entity.Projectile;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionEffectType;
@ -19,46 +19,74 @@ import me.mrCookieSlime.Slimefun.Objects.Category;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SimpleSlimefunItem; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SimpleSlimefunItem;
import me.mrCookieSlime.Slimefun.Objects.handlers.ItemInteractionHandler; import me.mrCookieSlime.Slimefun.Objects.handlers.ItemInteractionHandler;
import me.mrCookieSlime.Slimefun.Setup.SlimefunManager; import me.mrCookieSlime.Slimefun.Setup.SlimefunManager;
import me.mrCookieSlime.Slimefun.api.Slimefun;
import me.mrCookieSlime.Slimefun.utils.Utilities; import me.mrCookieSlime.Slimefun.utils.Utilities;
import java.util.UUID;
public class GrapplingHook extends SimpleSlimefunItem<ItemInteractionHandler> { public class GrapplingHook extends SimpleSlimefunItem<ItemInteractionHandler> {
private long despawnTicks;
public GrapplingHook(Category category, ItemStack item, String id, RecipeType recipeType, ItemStack[] recipe) { public GrapplingHook(Category category, ItemStack item, String id, RecipeType recipeType, ItemStack[] recipe, String[] keys, Object[] values) {
super(category, item, id, recipeType, recipe); super(category, item, id, recipeType, recipe, keys, values);
} }
@Override @Override
public ItemInteractionHandler getItemHandler() { public ItemInteractionHandler getItemHandler() {
Utilities utilities = SlimefunPlugin.getUtilities(); Utilities utilities = SlimefunPlugin.getUtilities();
return (e, p, item) -> {
if (SlimefunManager.isItemSimiliar(item, SlimefunItems.GRAPPLING_HOOK, true)) {
if (e.getClickedBlock() == null && !utilities.jumpState.containsKey(p.getUniqueId())) {
e.setCancelled(true);
if (p.getInventory().getItemInOffHand().getType() == Material.BOW) {
// Cancel, to fix dupe #740
return false;
}
utilities.jumpState.put(p.getUniqueId(), p.getInventory().getItemInMainHand().getType() != Material.SHEARS);
if (p.getInventory().getItemInMainHand().getType() == Material.LEAD) PlayerInventory.consumeItemInHand(p);
Vector direction = p.getEyeLocation().getDirection().multiply(2.0); return (e, p, item) -> {
Projectile projectile = p.getWorld().spawn(p.getEyeLocation().add(direction.getX(), direction.getY(), direction.getZ()), Arrow.class); if (SlimefunManager.isItemSimiliar(item, SlimefunItems.GRAPPLING_HOOK, true)) {
projectile.setShooter(p); UUID uuid = p.getUniqueId();
projectile.setVelocity(direction); if (e.getClickedBlock() == null && !utilities.jumpState.containsKey(uuid)) {
Arrow arrow = (Arrow) projectile; e.setCancelled(true);
Bat b = (Bat) p.getWorld().spawnEntity(p.getLocation(), EntityType.BAT); if (p.getInventory().getItemInOffHand().getType() == Material.BOW) {
b.setCanPickupItems(false); // Cancel, to fix dupe #740
b.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 100000, 100000)); return false;
b.setLeashHolder(arrow); }
utilities.jumpState.put(uuid, p.getInventory().getItemInMainHand().getType() != Material.SHEARS);
if (p.getInventory().getItemInMainHand().getType() == Material.LEAD)
PlayerInventory.consumeItemInHand(p);
utilities.damage.add(p.getUniqueId()); Vector direction = p.getEyeLocation().getDirection().multiply(2.0);
utilities.remove.put(p.getUniqueId(), new Entity[] {b, arrow}); Arrow arrow = p.getWorld().spawn(p.getEyeLocation().add(direction.getX(), direction.getY(), direction.getZ()), Arrow.class);
} arrow.setShooter(p);
return true; arrow.setVelocity(direction);
}
else return false;
};
}
Bat b = (Bat) p.getWorld().spawnEntity(p.getLocation(), EntityType.BAT);
b.setCanPickupItems(false);
b.setAI(false);
b.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 100000, 100000));
b.setLeashHolder(arrow);
utilities.damage.add(uuid);
utilities.remove.put(uuid, new Entity[]{b, arrow});
// To fix issue #253
Bukkit.getScheduler().scheduleSyncDelayedTask(SlimefunPlugin.instance, () -> {
if (utilities.jumpState.containsKey(uuid)) {
utilities.arrows.remove(uuid);
for (Entity n : utilities.remove.get(uuid)) {
if (n.isValid()) n.remove();
}
Bukkit.getScheduler().scheduleSyncDelayedTask(SlimefunPlugin.instance, () -> {
utilities.damage.remove(uuid);
utilities.jumpState.remove(uuid);
utilities.remove.remove(uuid);
}, 20L);
}
}, despawnTicks);
}
return true;
}
else return false;
};
}
@Override
public void postRegister() {
despawnTicks = (int) Slimefun.getItemValue("GRAPPLING_HOOK", "despawn-seconds") * 20;
}
} }

View File

@ -611,7 +611,8 @@ public final class SlimefunSetup {
.register(true); .register(true);
new GrapplingHook(Categories.TOOLS, SlimefunItems.GRAPPLING_HOOK, "GRAPPLING_HOOK", RecipeType.ENHANCED_CRAFTING_TABLE, new GrapplingHook(Categories.TOOLS, SlimefunItems.GRAPPLING_HOOK, "GRAPPLING_HOOK", RecipeType.ENHANCED_CRAFTING_TABLE,
new ItemStack[] {null, SlimefunItems.HOOK, SlimefunItems.HOOK, null, SlimefunItems.CHAIN, SlimefunItems.HOOK, SlimefunItems.CHAIN, null, null}) new ItemStack[] {null, SlimefunItems.HOOK, SlimefunItems.HOOK, null, SlimefunItems.CHAIN, SlimefunItems.HOOK, SlimefunItems.CHAIN, null, null},
new String[] {"despawn-seconds"}, new Object[] {60})
.register(true); .register(true);
new MagicWorkbench().register(); new MagicWorkbench().register();

View File

@ -28,7 +28,7 @@ public class BowListener implements Listener {
plugin.getServer().getPluginManager().registerEvents(this, plugin); plugin.getServer().getPluginManager().registerEvents(this, plugin);
utilities = SlimefunPlugin.getUtilities(); utilities = SlimefunPlugin.getUtilities();
} }
@EventHandler @EventHandler
public void onBowUse(EntityShootBowEvent e) { public void onBowUse(EntityShootBowEvent e) {
if (!(e.getEntity() instanceof Player) || !(e.getProjectile() instanceof Arrow)) return; if (!(e.getEntity() instanceof Player) || !(e.getProjectile() instanceof Arrow)) return;
@ -39,7 +39,7 @@ public class BowListener implements Listener {
public void onArrowHit(final ProjectileHitEvent e) { public void onArrowHit(final ProjectileHitEvent e) {
Bukkit.getScheduler().scheduleSyncDelayedTask(SlimefunPlugin.instance, () -> { Bukkit.getScheduler().scheduleSyncDelayedTask(SlimefunPlugin.instance, () -> {
if (!e.getEntity().isValid()) return; if (!e.getEntity().isValid()) return;
if (utilities.arrows.containsKey(e.getEntity().getUniqueId())) utilities.arrows.remove(e.getEntity().getUniqueId()); utilities.arrows.remove(e.getEntity().getUniqueId());
if (e.getEntity() instanceof Arrow) handleGrapplingHook((Arrow) e.getEntity()); if (e.getEntity() instanceof Arrow) handleGrapplingHook((Arrow) e.getEntity());
}, 4L); }, 4L);
} }
@ -55,7 +55,7 @@ public class BowListener implements Listener {
else p.setVelocity(arrow.getLocation().toVector().subtract(p.getLocation().toVector())); else p.setVelocity(arrow.getLocation().toVector().subtract(p.getLocation().toVector()));
for (Entity n: utilities.remove.get(p.getUniqueId())) { for (Entity n: utilities.remove.get(p.getUniqueId())) {
n.remove(); if (n.isValid()) n.remove();
} }
Bukkit.getScheduler().scheduleSyncDelayedTask(SlimefunPlugin.instance, () -> { Bukkit.getScheduler().scheduleSyncDelayedTask(SlimefunPlugin.instance, () -> {
@ -84,7 +84,7 @@ public class BowListener implements Listener {
p.setVelocity(v); p.setVelocity(v);
for (Entity n: utilities.remove.get(p.getUniqueId())) { for (Entity n: utilities.remove.get(p.getUniqueId())) {
n.remove(); if (n.isValid()) n.remove();
} }
Bukkit.getScheduler().scheduleSyncDelayedTask(SlimefunPlugin.instance, () -> { Bukkit.getScheduler().scheduleSyncDelayedTask(SlimefunPlugin.instance, () -> {

View File

@ -16,7 +16,7 @@ public interface DamageableItem {
if (item == null || item.getType() == null || item.getType() == Material.AIR) { if (item == null || item.getType() == null || item.getType() == Material.AIR) {
return; return;
} }
else if (item.getAmount() > 0 && isDamageable() && !item.getEnchantments().containsKey(Enchantment.DURABILITY) || Math.random() * 100 <= (60 + 40 / (item.getEnchantmentLevel(Enchantment.DURABILITY) + 1))) { else if (item.getAmount() > 0 && isDamageable() && !item.getEnchantments().containsKey(Enchantment.DURABILITY) || Math.random() * 100 <= (60 + Math.floorDiv(40, (item.getEnchantmentLevel(Enchantment.DURABILITY) + 1)))) {
ItemMeta meta = item.getItemMeta(); ItemMeta meta = item.getItemMeta();
Damageable damageable = (Damageable) meta; Damageable damageable = (Damageable) meta;