Skip to content

Commit

Permalink
Changed Spigot Api version to prevent a dupe exploit in versions high…
Browse files Browse the repository at this point in the history
…er than 1.9,

Added a way to apply effects only if better than the effect that has the player
  • Loading branch information
nik2143 committed Apr 17, 2021
1 parent 74143a7 commit ce7982c
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 22 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.nik2143</groupId>
<artifactId>CustomGapple</artifactId>
<version>1.2.1</version>
<version>1.2.2</version>
<packaging>jar</packaging>

<name>CustomGapple</name>
Expand Down Expand Up @@ -90,13 +90,13 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.8.8-R0.1-SNAPSHOT</version>
<version>1.16.5-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>de.tr7zw</groupId>
<artifactId>item-nbt-api</artifactId>
<version>2.5.0</version>
<version>2.7.1</version>
</dependency>
</dependencies>
</project>
19 changes: 19 additions & 0 deletions src/main/java/io/github/nik2143/customgapple/Configuration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.github.nik2143.customgapple;

public class Configuration {

public int defaultAmount;
public int defaultDuration;
public boolean applyonlybetter;

public Configuration(){
loadConfig();
}

public void loadConfig(){
defaultAmount = CustomGapple.getCustomGapple().getConfig().getInt("Default-Amount");
defaultDuration = CustomGapple.getCustomGapple().getConfig().getInt("Default-Duration");
applyonlybetter = CustomGapple.getCustomGapple().getConfig().getBoolean("apply-only-better",false);
}

}
13 changes: 11 additions & 2 deletions src/main/java/io/github/nik2143/customgapple/CustomGapple.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,20 @@

public final class CustomGapple extends JavaPlugin {

private Configuration configuration;
private static CustomGapple customGapple;

@Override
public void onEnable() {
saveDefaultConfig();
this.getCommand("customgapple").setExecutor(new CustomGappleCommand(this));
this.getCommand("customgapple").setTabCompleter(new CustomGappleCommand(this));
customGapple = this;
configuration = new Configuration();
getCommand("customgapple").setExecutor(new CustomGappleCommand());
getCommand("customgapple").setTabCompleter(new CustomGappleCommand());
Bukkit.getPluginManager().registerEvents(new EatGappleListener(),this);
}

public Configuration getConfiguration() { return configuration; }

public static CustomGapple getCustomGapple() { return customGapple; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,10 @@

public class CustomGappleCommand implements CommandExecutor, TabCompleter {

private final CustomGapple plugin;

public CustomGappleCommand (CustomGapple plugin){
this.plugin = plugin;
}

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (command.getName().equalsIgnoreCase("customgapple")) {
Player player = null;
Player player;
if (!(sender instanceof Player)) {
if (args.length == 0 || args.length == 1 || args.length == 2){
sender.sendMessage("Wrong sintax. Use /customgapple <Player> <EffectsNumbers> <Effects> [Levels] [Durations] [Amount] [Enchanted (true/false)]");
Expand All @@ -40,7 +34,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
player = Bukkit.getPlayerExact(args[0]);
ArrayList<String> argslist = new ArrayList<>(Arrays.asList(args));
argslist.remove(0);
args = argslist.toArray(new String[argslist.size()]);
args = argslist.toArray(new String[0]);
} else {
sender.sendMessage("Player "+ args[0] +" isn't online");
return true;
Expand Down Expand Up @@ -102,7 +96,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
}
}else {
for (int i = 0; i < effectsnumber; i++){
durations.add(plugin.getConfig().getInt("Default-Duration"));
durations.add(CustomGapple.getCustomGapple().getConfiguration().defaultDuration);
}
}
if (args.length >= effectsnumber * 3 + 3){
Expand All @@ -115,7 +109,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
}
player.getInventory().addItem(CreateGapple(effectsnumber, effects, levels, durations, Integer.parseInt(args[effectsnumber * 3 + 1 ]),enchantedGapple));
} else {
player.getInventory().addItem(CreateGapple(effectsnumber, effects, levels, durations, plugin.getConfig().getInt("Default-Amount"),enchantedGapple));
player.getInventory().addItem(CreateGapple(effectsnumber, effects, levels, durations, CustomGapple.getCustomGapple().getConfiguration().defaultAmount,enchantedGapple));
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,72 @@
package io.github.nik2143.customgapple.listeners;

import de.tr7zw.changeme.nbtapi.NBTItem;
import io.github.nik2143.customgapple.CustomGapple;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerItemConsumeEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

import java.util.Collection;
import java.util.List;
import java.util.Objects;

public class EatGappleListener implements Listener {

@EventHandler
private void GappleEatEvent(PlayerItemConsumeEvent e){
if (e.getItem().getType().equals(Material.GOLDEN_APPLE)){
ItemStack item = e.getItem();
ItemStack originalItem = e.getItem();
NBTItem nbti = new NBTItem(item);
if (nbti.getCompound("GappleEffects") != null){
e.setCancelled(true);
for(String key : nbti.getCompound("GappleEffects").getKeys()){
e.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.getByName(nbti.getCompound("GappleEffects").getCompound(key).getString("Effect")),
nbti.getCompound("GappleEffects").getCompound(key).getInteger("Duration"),
nbti.getCompound("GappleEffects").getCompound(key).getInteger("Level")),true);
}
applyEffects(nbti,e.getPlayer());
item.setAmount(item.getAmount() - 1);
e.getPlayer().setFoodLevel(e.getPlayer().getFoodLevel()+4);
e.getPlayer().setSaturation((float) (e.getPlayer().getSaturation()+1.2));
e.getPlayer().getInventory().setItemInHand(item);

if(item.getAmount() <= 0) item = null;

try {
ItemStack mainHand = e.getPlayer().getInventory().getItemInMainHand();
ItemStack offHand = e.getPlayer().getInventory().getItemInOffHand();

if(mainHand.equals(originalItem)) e.getPlayer().getInventory().setItemInMainHand(item);
else if(offHand.equals(originalItem)) e.getPlayer().getInventory().setItemInOffHand(item);
else if(mainHand.getType() == Material.GOLDEN_APPLE)
e.getPlayer().getInventory().setItemInMainHand(item);
} catch (NoSuchMethodError error){
e.getPlayer().getInventory().setItemInHand(item);
}
}
}
}

private void applyEffects(NBTItem nbti, Player p){
Collection<PotionEffect> activeEffects = p.getActivePotionEffects();
for(String key : nbti.getCompound("GappleEffects").getKeys()){
boolean haseffect = false;
PotionEffect pe = new PotionEffect(Objects.requireNonNull(PotionEffectType.getByName(nbti.getCompound("GappleEffects").getCompound(key).getString("Effect"))),
nbti.getCompound("GappleEffects").getCompound(key).getInteger("Duration"),
nbti.getCompound("GappleEffects").getCompound(key).getInteger("Level"));
if (CustomGapple.getCustomGapple().getConfiguration().applyonlybetter){
for (PotionEffect effect : activeEffects){
if (effect.getType().equals(pe.getType())){
haseffect = true;
if (effect.getAmplifier()<pe.getAmplifier() || (effect.getDuration()<pe.getDuration() && effect.getAmplifier()==pe.getAmplifier()) ){
p.addPotionEffect(pe,true);
}
}
}
}
if (!haseffect){
p.addPotionEffect(pe,true);
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#Amount used when isn't specified in command
Default-Amount: 64
#Duration in seconds used when isn't specified in command
Default-Duration: 20
Default-Duration: 20
#Apply only effects better than the effects that has the player
apply-only-better: false
1 change: 1 addition & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ name: CustomGapple
version: ${project.version}
main: io.github.nik2143.customgapple.CustomGapple
prefix: CustomGapple
api-version: 1.13
authors: [nik2143]
description: Allow to create and use custom golden apples with custom effects
commands:
Expand Down

0 comments on commit ce7982c

Please sign in to comment.