Skip to content

Commit

Permalink
Modified the invalid potion protection
Browse files Browse the repository at this point in the history
this is to prevent spawned invalid potions from being thrown before the
item scanner has a chance to delete them.

Moved the invalid potion check to its own class in Checks, and added a
new message since the detection is slightly different.
  • Loading branch information
dniym committed Mar 17, 2024
1 parent 119ce00 commit 2d79932
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 40 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ tasks.compileJava.configure {
options.release.set(8)
}

version = "2.9.10-SNAPSHOT-01"
version = "2.9.10-SNAPSHOT-02"

tasks.named<Copy>("processResources") {
filesMatching("plugin.yml") {
Expand Down
112 changes: 112 additions & 0 deletions src/main/java/main/java/me/dniym/checks/BadPotionCheck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package main.java.me.dniym.checks;

import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.entity.ThrownPotion;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionType;
import org.jetbrains.annotations.NotNull;

import main.java.me.dniym.IllegalStack;
import main.java.me.dniym.enums.Msg;
import main.java.me.dniym.enums.Protections;
import main.java.me.dniym.listeners.fListener;
import main.java.me.dniym.utils.NBTStuff;

public class BadPotionCheck {

public static void checkPotion(ItemStack is, Player p) {
if(!is.hasItemMeta())
return;
ItemMeta im = is.getItemMeta();
if (Protections.PreventInvalidPotions.isEnabled() && im instanceof PotionMeta) {
if (Protections.AllowBypass.isEnabled() && p.hasPermission("illegalstack.enchantbypass"))
return;


if(IllegalStack.isHasMCMMO() && NBTStuff.hasNbtTag("IllegalStack", is, "mcmmoitem", Protections.PreventInvalidPotions))
return;


PotionMeta potion = (PotionMeta) is.getItemMeta();
PotionData pd = potion.getBasePotionData();
if (pd.getType() == PotionType.UNCRAFTABLE || (potion.hasCustomEffects() && !potion
.getCustomEffects()
.isEmpty())) {

if (pd.getType() == PotionType.UNCRAFTABLE && potion.getCustomEffects().isEmpty())
return;


p.getInventory().remove(is);
StringBuilder efx = new StringBuilder();
for (PotionEffect ce : potion.getCustomEffects()) {
efx
.append(ce.getType().getName())
.append(" amplifier: ")
.append(ce.getAmplifier())
.append(
" duration: ")
.append(ce.getDuration())
.append(",");
}
fListener.getLog().append(Msg.InvalidPotionRemoved.getValue(p, efx.toString()), Protections.PreventInvalidPotions);

}

}



}

public static boolean isInvalidPotion(@NotNull Projectile proj) {

if(proj instanceof ThrownPotion) {
ThrownPotion tp = (ThrownPotion)proj;
Player p = null;
if(proj.getShooter() instanceof Player)
p = ((Player)proj.getShooter());

if (p != null && Protections.AllowBypass.isEnabled() && p.hasPermission("illegalstack.enchantbypass"))
return false;


PotionMeta potion = (PotionMeta) tp.getPotionMeta();
PotionData pd = potion.getBasePotionData();
if (pd.getType() == PotionType.UNCRAFTABLE || (potion.hasCustomEffects() && !potion
.getCustomEffects()
.isEmpty())) {

if (pd.getType() == PotionType.UNCRAFTABLE && potion.getCustomEffects().isEmpty())
return false;


//p.getInventory().remove(is);
StringBuilder efx = new StringBuilder();
for (PotionEffect ce : potion.getCustomEffects()) {
efx
.append(ce.getType().getName())
.append(" amplifier: ")
.append(ce.getAmplifier())
.append(
" duration: ")
.append(ce.getDuration())
.append(",");
}

fListener.getLog().append(Msg.InvalidThrownPotionRemoved.getValue(p, efx.toString()), Protections.PreventInvalidPotions);
return true;
}

}
return false;

}


}
2 changes: 2 additions & 0 deletions src/main/java/main/java/me/dniym/enums/Msg.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public enum Msg {
IllegalStackUnstack(
"Unstacked an illegal stack of: ~item~ (~amount~) triggered by player: ~name~. ~lost~ items were unable to fit and lost."),
InvalidPotionRemoved("Removed invalid potion from ~name~ had the following effects: ~effects~"),
InvalidThrownPotionRemoved("Removed an invalid potion thrown by ~name~ with the following effects: ~effects~"),

UnbreakableItemCleared("Removed Unbreakable flag from ~item~ found on player ~name~"),
CustomAttribsRemoved("Removed Custom Attributes on ~item~ held by ~name~ (~attributes~)"),
CustomAttribsRemoved2("Removed ~item~ with Custom Attributes worn by ~name~ (~attributes~)"),
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/main/java/me/dniym/listeners/Listener114.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

import io.netty.util.internal.ThreadLocalRandom;
import main.java.me.dniym.IllegalStack;
import main.java.me.dniym.checks.BadPotionCheck;
import main.java.me.dniym.enums.Msg;
import main.java.me.dniym.enums.Protections;
import main.java.me.dniym.timers.fTimer;
Expand Down Expand Up @@ -318,6 +319,9 @@ public void onProjectileLaunch(ProjectileLaunchEvent e) {
if (Protections.PreventProjectileExploit2.isEnabled(e.getEntity().getLocation())) {
fTimer.trackProjectile(e.getEntity());
}
if (Protections.PreventInvalidPotions.isEnabled() && e.getEntity() != null)
e.setCancelled(BadPotionCheck.isInvalidPotion(e.getEntity()));

}

@EventHandler// (ignoreCancelled = false, priority=EventPriority.LOWEST)
Expand Down
44 changes: 5 additions & 39 deletions src/main/java/main/java/me/dniym/timers/fTimer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.netty.util.internal.ThreadLocalRandom;
import main.java.me.dniym.IllegalStack;
import main.java.me.dniym.checks.BadAttributeCheck;
import main.java.me.dniym.checks.BadPotionCheck;
import main.java.me.dniym.enums.Msg;
import main.java.me.dniym.enums.Protections;
import main.java.me.dniym.listeners.fListener;
Expand Down Expand Up @@ -642,46 +643,11 @@ public void run() {
//NBTStuff.checkForBadCustomData(is, p, false);
BadAttributeCheck.checkForBadCustomData(is, p);
}

if (Protections.PreventInvalidPotions.isEnabled() && im instanceof PotionMeta) {
if (Protections.AllowBypass.isEnabled() && p.hasPermission("illegalstack.enchantbypass")) {
continue;
}

if (IllegalStack.isHasMCMMO()) {
if (NBTStuff.hasNbtTag("IllegalStack", is, "mcmmoitem", Protections.PreventInvalidPotions)) {
continue;
}
}
PotionMeta potion = (PotionMeta) is.getItemMeta();
PotionData pd = potion.getBasePotionData();
if (pd.getType() == PotionType.UNCRAFTABLE || (potion.hasCustomEffects() && !potion
.getCustomEffects()
.isEmpty())) {

if (pd.getType() == PotionType.UNCRAFTABLE && potion.getCustomEffects().isEmpty()) {
continue;
}

p.getInventory().remove(is);
StringBuilder efx = new StringBuilder();
for (PotionEffect ce : potion.getCustomEffects()) {
efx
.append(ce.getType().getName())
.append(" amplifier: ")
.append(ce.getAmplifier())
.append(
" duration: ")
.append(ce.getDuration())
.append(",");
}

fListener.getLog().append2(Msg.InvalidPotionRemoved.getValue(p, efx.toString()));
}

}

if (Protections.PreventInvalidPotions.isEnabled())
BadPotionCheck.checkPotion(is,p);

}

if (Protections.FixIllegalEnchantmentLevels.isEnabled() && !mcMMOListener.ismcMMOActive(p)) {
if (!Protections.OnlyFunctionInWorlds.getTxtSet().isEmpty()) //world list isn't empty
{
Expand Down

0 comments on commit 2d79932

Please sign in to comment.