Skip to content

Commit

Permalink
Use PotionEffects to slow the user with Weight (Closes #13 )
Browse files Browse the repository at this point in the history
Also added some anti-abuse code that I might use for other enchantments.
  • Loading branch information
Geolykt committed Oct 19, 2020
1 parent ad07cc4 commit 4863d54
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package de.geolykt.enchantments_plus.enchantments;

import org.bukkit.NamespacedKey;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.potion.PotionEffectType;

import de.geolykt.enchantments_plus.CustomEnchantment;
import de.geolykt.enchantments_plus.Storage;
Expand All @@ -13,11 +16,16 @@
import de.geolykt.enchantments_plus.util.Tool;
import de.geolykt.enchantments_plus.util.Utilities;

import static org.bukkit.potion.PotionEffectType.INCREASE_DAMAGE;

public class Weight extends CustomEnchantment {

public static final int ID = 67;

/**
* Is put on the PDC of a player to mark that the player has the Enchantment active (the slowness was made by the plugin).
* Used to prevent abuse so players cannot remove permanent slowness effects.
* @since 2.1.3
*/
public static final NamespacedKey ACTIVE = new NamespacedKey(Storage.plugin, "weight_active");

@Override
public Builder<Weight> defaults() {
Expand Down Expand Up @@ -66,11 +74,21 @@ public boolean onBeingHit(EntityDamageByEntityEvent evt, int level, boolean used
}

@Override
public boolean onScan(Player player, int level, boolean usedHand
) {
player.setWalkSpeed((float) (.164f - level * power * .014f));
Utilities.addPotion(player, INCREASE_DAMAGE, 610, (int) Math.round(power * level));
player.setMetadata("ze.speed", new FixedMetadataValue(Storage.plugin, System.currentTimeMillis()));
public boolean onPlayerDeath(PlayerDeathEvent evt, int level, boolean usedHand) {
evt.getEntity().getPersistentDataContainer().remove(ACTIVE);
return true;
}

@Override
public boolean onScan(Player player, int level, boolean usedHand) {
player.setWalkSpeed((float) (.164f - level * power * .014f));
if (player.hasPotionEffect(PotionEffectType.SLOW)) {
return false;
} else {
player.addPotionEffect(PotionEffectType.SLOW.createEffect(Integer.MAX_VALUE, (int) (level*power)));
player.addPotionEffect(PotionEffectType.INCREASE_DAMAGE.createEffect(Integer.MAX_VALUE, (int) (level*power)));
player.getPersistentDataContainer().set(ACTIVE, PersistentDataType.BYTE, (byte) 1);
return true;
}
}
}
7 changes: 6 additions & 1 deletion src/main/java/de/geolykt/enchantments_plus/evt/Watcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.potion.PotionEffectType;

import de.geolykt.enchantments_plus.Config;
Expand Down Expand Up @@ -286,7 +287,11 @@ public void onInventoryClick(InventoryClickEvent evt) {
player.removePotionEffect(PotionEffectType.NIGHT_VISION);
break;
case WEIGHT:
player.removePotionEffect(PotionEffectType.INCREASE_DAMAGE);
if (player.getPersistentDataContainer().has(Weight.ACTIVE, PersistentDataType.BYTE)) {
player.removePotionEffect(PotionEffectType.INCREASE_DAMAGE);
player.removePotionEffect(PotionEffectType.SLOW);
player.getPersistentDataContainer().remove(Weight.ACTIVE);
}
break;
default:
break;
Expand Down

0 comments on commit 4863d54

Please sign in to comment.