Skip to content

Commit ce7982c

Browse files
committed
Changed Spigot Api version to prevent a dupe exploit in versions higher than 1.9,
Added a way to apply effects only if better than the effect that has the player
1 parent 74143a7 commit ce7982c

File tree

7 files changed

+86
-22
lines changed

7 files changed

+86
-22
lines changed

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>io.github.nik2143</groupId>
88
<artifactId>CustomGapple</artifactId>
9-
<version>1.2.1</version>
9+
<version>1.2.2</version>
1010
<packaging>jar</packaging>
1111

1212
<name>CustomGapple</name>
@@ -90,13 +90,13 @@
9090
<dependency>
9191
<groupId>org.spigotmc</groupId>
9292
<artifactId>spigot-api</artifactId>
93-
<version>1.8.8-R0.1-SNAPSHOT</version>
93+
<version>1.16.5-R0.1-SNAPSHOT</version>
9494
<scope>provided</scope>
9595
</dependency>
9696
<dependency>
9797
<groupId>de.tr7zw</groupId>
9898
<artifactId>item-nbt-api</artifactId>
99-
<version>2.5.0</version>
99+
<version>2.7.1</version>
100100
</dependency>
101101
</dependencies>
102102
</project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.github.nik2143.customgapple;
2+
3+
public class Configuration {
4+
5+
public int defaultAmount;
6+
public int defaultDuration;
7+
public boolean applyonlybetter;
8+
9+
public Configuration(){
10+
loadConfig();
11+
}
12+
13+
public void loadConfig(){
14+
defaultAmount = CustomGapple.getCustomGapple().getConfig().getInt("Default-Amount");
15+
defaultDuration = CustomGapple.getCustomGapple().getConfig().getInt("Default-Duration");
16+
applyonlybetter = CustomGapple.getCustomGapple().getConfig().getBoolean("apply-only-better",false);
17+
}
18+
19+
}

src/main/java/io/github/nik2143/customgapple/CustomGapple.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,20 @@
77

88
public final class CustomGapple extends JavaPlugin {
99

10+
private Configuration configuration;
11+
private static CustomGapple customGapple;
12+
1013
@Override
1114
public void onEnable() {
1215
saveDefaultConfig();
13-
this.getCommand("customgapple").setExecutor(new CustomGappleCommand(this));
14-
this.getCommand("customgapple").setTabCompleter(new CustomGappleCommand(this));
16+
customGapple = this;
17+
configuration = new Configuration();
18+
getCommand("customgapple").setExecutor(new CustomGappleCommand());
19+
getCommand("customgapple").setTabCompleter(new CustomGappleCommand());
1520
Bukkit.getPluginManager().registerEvents(new EatGappleListener(),this);
1621
}
22+
23+
public Configuration getConfiguration() { return configuration; }
24+
25+
public static CustomGapple getCustomGapple() { return customGapple; }
1726
}

src/main/java/io/github/nik2143/customgapple/commands/CustomGappleCommand.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,10 @@
2121

2222
public class CustomGappleCommand implements CommandExecutor, TabCompleter {
2323

24-
private final CustomGapple plugin;
25-
26-
public CustomGappleCommand (CustomGapple plugin){
27-
this.plugin = plugin;
28-
}
29-
3024
@Override
3125
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
3226
if (command.getName().equalsIgnoreCase("customgapple")) {
33-
Player player = null;
27+
Player player;
3428
if (!(sender instanceof Player)) {
3529
if (args.length == 0 || args.length == 1 || args.length == 2){
3630
sender.sendMessage("Wrong sintax. Use /customgapple <Player> <EffectsNumbers> <Effects> [Levels] [Durations] [Amount] [Enchanted (true/false)]");
@@ -40,7 +34,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
4034
player = Bukkit.getPlayerExact(args[0]);
4135
ArrayList<String> argslist = new ArrayList<>(Arrays.asList(args));
4236
argslist.remove(0);
43-
args = argslist.toArray(new String[argslist.size()]);
37+
args = argslist.toArray(new String[0]);
4438
} else {
4539
sender.sendMessage("Player "+ args[0] +" isn't online");
4640
return true;
@@ -102,7 +96,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
10296
}
10397
}else {
10498
for (int i = 0; i < effectsnumber; i++){
105-
durations.add(plugin.getConfig().getInt("Default-Duration"));
99+
durations.add(CustomGapple.getCustomGapple().getConfiguration().defaultDuration);
106100
}
107101
}
108102
if (args.length >= effectsnumber * 3 + 3){
@@ -115,7 +109,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
115109
}
116110
player.getInventory().addItem(CreateGapple(effectsnumber, effects, levels, durations, Integer.parseInt(args[effectsnumber * 3 + 1 ]),enchantedGapple));
117111
} else {
118-
player.getInventory().addItem(CreateGapple(effectsnumber, effects, levels, durations, plugin.getConfig().getInt("Default-Amount"),enchantedGapple));
112+
player.getInventory().addItem(CreateGapple(effectsnumber, effects, levels, durations, CustomGapple.getCustomGapple().getConfiguration().defaultAmount,enchantedGapple));
119113
}
120114
return true;
121115
}

src/main/java/io/github/nik2143/customgapple/listeners/EatGappleListener.java

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,72 @@
11
package io.github.nik2143.customgapple.listeners;
22

33
import de.tr7zw.changeme.nbtapi.NBTItem;
4+
import io.github.nik2143.customgapple.CustomGapple;
45
import org.bukkit.Bukkit;
56
import org.bukkit.Material;
7+
import org.bukkit.entity.Player;
68
import org.bukkit.event.EventHandler;
79
import org.bukkit.event.Listener;
810
import org.bukkit.event.player.PlayerItemConsumeEvent;
911
import org.bukkit.inventory.ItemStack;
1012
import org.bukkit.potion.PotionEffect;
1113
import org.bukkit.potion.PotionEffectType;
1214

15+
import java.util.Collection;
16+
import java.util.List;
17+
import java.util.Objects;
18+
1319
public class EatGappleListener implements Listener {
1420

1521
@EventHandler
1622
private void GappleEatEvent(PlayerItemConsumeEvent e){
1723
if (e.getItem().getType().equals(Material.GOLDEN_APPLE)){
1824
ItemStack item = e.getItem();
25+
ItemStack originalItem = e.getItem();
1926
NBTItem nbti = new NBTItem(item);
2027
if (nbti.getCompound("GappleEffects") != null){
2128
e.setCancelled(true);
22-
for(String key : nbti.getCompound("GappleEffects").getKeys()){
23-
e.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.getByName(nbti.getCompound("GappleEffects").getCompound(key).getString("Effect")),
24-
nbti.getCompound("GappleEffects").getCompound(key).getInteger("Duration"),
25-
nbti.getCompound("GappleEffects").getCompound(key).getInteger("Level")),true);
26-
}
29+
applyEffects(nbti,e.getPlayer());
2730
item.setAmount(item.getAmount() - 1);
2831
e.getPlayer().setFoodLevel(e.getPlayer().getFoodLevel()+4);
2932
e.getPlayer().setSaturation((float) (e.getPlayer().getSaturation()+1.2));
30-
e.getPlayer().getInventory().setItemInHand(item);
33+
34+
if(item.getAmount() <= 0) item = null;
35+
36+
try {
37+
ItemStack mainHand = e.getPlayer().getInventory().getItemInMainHand();
38+
ItemStack offHand = e.getPlayer().getInventory().getItemInOffHand();
39+
40+
if(mainHand.equals(originalItem)) e.getPlayer().getInventory().setItemInMainHand(item);
41+
else if(offHand.equals(originalItem)) e.getPlayer().getInventory().setItemInOffHand(item);
42+
else if(mainHand.getType() == Material.GOLDEN_APPLE)
43+
e.getPlayer().getInventory().setItemInMainHand(item);
44+
} catch (NoSuchMethodError error){
45+
e.getPlayer().getInventory().setItemInHand(item);
46+
}
47+
}
48+
}
49+
}
50+
51+
private void applyEffects(NBTItem nbti, Player p){
52+
Collection<PotionEffect> activeEffects = p.getActivePotionEffects();
53+
for(String key : nbti.getCompound("GappleEffects").getKeys()){
54+
boolean haseffect = false;
55+
PotionEffect pe = new PotionEffect(Objects.requireNonNull(PotionEffectType.getByName(nbti.getCompound("GappleEffects").getCompound(key).getString("Effect"))),
56+
nbti.getCompound("GappleEffects").getCompound(key).getInteger("Duration"),
57+
nbti.getCompound("GappleEffects").getCompound(key).getInteger("Level"));
58+
if (CustomGapple.getCustomGapple().getConfiguration().applyonlybetter){
59+
for (PotionEffect effect : activeEffects){
60+
if (effect.getType().equals(pe.getType())){
61+
haseffect = true;
62+
if (effect.getAmplifier()<pe.getAmplifier() || (effect.getDuration()<pe.getDuration() && effect.getAmplifier()==pe.getAmplifier()) ){
63+
p.addPotionEffect(pe,true);
64+
}
65+
}
66+
}
67+
}
68+
if (!haseffect){
69+
p.addPotionEffect(pe,true);
3170
}
3271
}
3372
}

src/main/resources/config.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#Amount used when isn't specified in command
22
Default-Amount: 64
33
#Duration in seconds used when isn't specified in command
4-
Default-Duration: 20
4+
Default-Duration: 20
5+
#Apply only effects better than the effects that has the player
6+
apply-only-better: false

src/main/resources/plugin.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ name: CustomGapple
22
version: ${project.version}
33
main: io.github.nik2143.customgapple.CustomGapple
44
prefix: CustomGapple
5+
api-version: 1.13
56
authors: [nik2143]
67
description: Allow to create and use custom golden apples with custom effects
78
commands:

0 commit comments

Comments
 (0)