Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 65af6f0

Browse files
authored
Merge pull request #9 from Asleeepp/1.5-test
Version 1.5
2 parents 170bda8 + 6a18fab commit 65af6f0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+839
-220
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ compileJava {
1515
}
1616

1717
group = 'me.Asleepp'
18-
version = '1.4.1'
18+
version = '1.5'
1919

2020
repositories {
2121
mavenCentral()
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package me.asleepp.SkriptItemsAdder;
2+
3+
import ch.njol.skript.util.Version;
4+
import com.google.gson.JsonObject;
5+
import com.google.gson.JsonParser;
6+
import org.bukkit.Bukkit;
7+
import org.bukkit.entity.Player;
8+
import org.bukkit.event.EventHandler;
9+
import org.bukkit.event.Listener;
10+
import org.bukkit.event.player.PlayerJoinEvent;
11+
import org.bukkit.plugin.java.JavaPlugin;
12+
13+
import java.io.InputStreamReader;
14+
import java.net.HttpURLConnection;
15+
import java.net.URL;
16+
import java.util.concurrent.CompletableFuture;
17+
import java.util.function.Consumer;
18+
19+
// Credits to ShaneBee, yoinked his Update Checker code from SkBee
20+
21+
public class UpdateChecker implements Listener {
22+
23+
private static Version UPDATE_VERSION;
24+
25+
public static void checkForUpdate(JavaPlugin plugin, String pluginVersion) {
26+
plugin.getLogger().info("Checking for update...");
27+
getLatestReleaseVersion(version -> {
28+
Version plugVer = new Version(pluginVersion);
29+
Version curVer = new Version(version);
30+
if (curVer.compareTo(plugVer) <= 0) {
31+
plugin.getLogger().info("Plugin is up to date!");
32+
} else {
33+
plugin.getLogger().warning("Plugin is not up to date!");
34+
plugin.getLogger().warning(" - Current version: " + pluginVersion);
35+
plugin.getLogger().warning(" - Available update: " + version);
36+
plugin.getLogger().warning(" - Download available at: https://github.com/Asleeepp/skript-itemsadder/releases");
37+
UPDATE_VERSION = curVer;
38+
}
39+
}, plugin);
40+
}
41+
42+
private static void getLatestReleaseVersion(final Consumer<String> consumer, JavaPlugin plugin) {
43+
try {
44+
URL url = new URL("https://api.github.com/repos/Asleeepp/skript-itemsadder/releases/latest");
45+
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
46+
connection.setRequestMethod("GET");
47+
JsonObject jsonObject = new JsonParser().parse(new InputStreamReader(connection.getInputStream())).getAsJsonObject();
48+
String tag_name = jsonObject.get("tag_name").getAsString();
49+
consumer.accept(tag_name);
50+
} catch (Exception e) {
51+
plugin.getLogger().severe("Checking for update failed: " + e.getMessage());
52+
}
53+
}
54+
55+
56+
private final SkriptItemsAdder plugin;
57+
58+
public UpdateChecker(SkriptItemsAdder plugin) {
59+
this.plugin = plugin;
60+
}
61+
62+
@EventHandler
63+
private void onJoin(PlayerJoinEvent event) {
64+
Player player = event.getPlayer();
65+
if (!player.hasPermission("skript-itemsadder.update.check")) return;
66+
67+
String currentVersion = this.plugin.getDescription().getVersion();
68+
CompletableFuture<Version> updateVersion = getUpdateVersion(currentVersion);
69+
70+
Bukkit.getScheduler().runTaskLater(this.plugin, () -> updateVersion.thenApply(version -> {
71+
player.sendMessage("[skript-itemsadder] update available: " + version);
72+
player.sendMessage("[skript-itemsadder] download at https://github.com/Asleeepp/skript-itemsadder/releases");
73+
return true;
74+
}), 30);
75+
}
76+
77+
private CompletableFuture<Version> getUpdateVersion(String currentVersion) {
78+
CompletableFuture<Version> future = new CompletableFuture<>();
79+
if (UPDATE_VERSION != null) {
80+
future.complete(UPDATE_VERSION);
81+
} else {
82+
Bukkit.getScheduler().runTaskAsynchronously(this.plugin, () -> getLatestReleaseVersion(version -> {
83+
Version plugVer = new Version(currentVersion);
84+
Version curVer = new Version(version);
85+
if (curVer.compareTo(plugVer) <= 0) {
86+
future.cancel(true);
87+
} else {
88+
UPDATE_VERSION = curVer;
89+
future.complete(UPDATE_VERSION);
90+
}
91+
}, this.plugin));
92+
}
93+
return future;
94+
}
95+
}

src/main/java/me/asleepp/SkriptItemsAdder/elements/conditions/CondGetAction.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import ch.njol.skript.doc.Description;
55
import ch.njol.skript.doc.Examples;
66
import ch.njol.skript.doc.Name;
7+
import ch.njol.skript.doc.RequiredPlugins;
78
import ch.njol.skript.doc.Since;
89
import ch.njol.skript.lang.Condition;
910
import ch.njol.skript.lang.Expression;
@@ -16,8 +17,12 @@
1617
import javax.annotation.Nullable;
1718
@Name("Is Action")
1819
@Description({"This condition checks if the player has interacted with a custom block with either a left or a right click."})
19-
@Examples({"on interact with custom block: /tif interact action is right click: /t/tkill event-player "})
20+
@Examples({
21+
"on interact with custom block:",
22+
"\tif interact action is right click:",
23+
"\t\tkill event-player"})
2024
@Since("1.4")
25+
@RequiredPlugins("ItemsAdder")
2126
public class CondGetAction extends Condition {
2227

2328
static {

src/main/java/me/asleepp/SkriptItemsAdder/elements/conditions/CondGetBlockClicked.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import ch.njol.skript.doc.Description;
55
import ch.njol.skript.doc.Examples;
66
import ch.njol.skript.doc.Name;
7+
import ch.njol.skript.doc.RequiredPlugins;
78
import ch.njol.skript.doc.Since;
89
import ch.njol.skript.lang.Condition;
910
import ch.njol.skript.lang.Expression;
@@ -15,8 +16,12 @@
1516
import javax.annotation.Nullable;
1617
@Name("Is Block Clicked")
1718
@Description({"This condition checks what block the player clicked."})
18-
@Examples({"on interact with custom block: /tif clicked block is \"diamond_tiles\" /t/tsend \"That's quite valuable.\" "})
19+
@Examples({
20+
"on interact with custom block:",
21+
"\tif clicked block is \"diamond_tiles\"",
22+
"\t\tsend \"That's quite valuable.\" "})
1923
@Since("1.4")
24+
@RequiredPlugins("ItemsAdder")
2025
public class CondGetBlockClicked extends Condition {
2126

2227
private Expression<String> block;

src/main/java/me/asleepp/SkriptItemsAdder/elements/conditions/CondGetBlockFace.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import ch.njol.skript.doc.Description;
55
import ch.njol.skript.doc.Examples;
66
import ch.njol.skript.doc.Name;
7+
import ch.njol.skript.doc.RequiredPlugins;
78
import ch.njol.skript.doc.Since;
89
import ch.njol.skript.lang.Condition;
910
import ch.njol.skript.lang.Expression;
@@ -16,8 +17,12 @@
1617
import javax.annotation.Nullable;
1718
@Name("Is Block Face")
1819
@Description({"This condition checks what face of a block the player has interacted with."})
19-
@Examples({"on interact with custom block: /tif clicked block face is south: /t/tsend \"Why are you doing that?\" "})
20+
@Examples({
21+
"on interact with custom block:",
22+
"\tif clicked block face is south:",
23+
"\t\tsend \"Why are you doing that?\""})
2024
@Since("1.4")
25+
@RequiredPlugins("ItemsAdder")
2126
public class CondGetBlockFace extends Condition {
2227

2328
static {

src/main/java/me/asleepp/SkriptItemsAdder/elements/conditions/CondIsCustomBlock.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import ch.njol.skript.doc.Description;
55
import ch.njol.skript.doc.Examples;
66
import ch.njol.skript.doc.Name;
7+
import ch.njol.skript.doc.RequiredPlugins;
8+
import ch.njol.skript.doc.Since;
79
import ch.njol.skript.lang.Condition;
810
import ch.njol.skript.lang.Expression;
911
import ch.njol.skript.lang.SkriptParser;
@@ -16,42 +18,55 @@
1618
@Name("Is ItemsAdder Block")
1719
@Description({"Checks if the block is an ItemsAdder block."})
1820
@Examples({
19-
"on break:",
20-
"\tif event-block is a custom block",
21-
"\t\tkill player"})
21+
"on break:",
22+
"\tif event-block is a custom block with id \"iasurvival:ruby_block\":",
23+
"\t\tkill player",
24+
"\telse if event-block isn't a custom block:",
25+
"\t\tsend \"Good Job!\" to player"})
26+
@Since("1.0, 1.5 (Negative Comparison)")
27+
@RequiredPlugins("ItemsAdder")
2228
public class CondIsCustomBlock extends Condition {
2329
private Expression<Block> block;
30+
private Expression<String> blockId;
2431

2532
static {
26-
Skript.registerCondition(CondIsCustomBlock.class, "%blocks% (is [a[n]]|are) (custom|ia|itemsadder) block[s]");
33+
Skript.registerCondition(CondIsCustomBlock.class, "%blocks% (is [a[n]]|are) (custom|ia|itemsadder) block[s] [[with id] %-string%]", "%blocks% (is[n't| not]) [a] (custom|ia|itemsadder) block[s] [[with id] %-string%]");
2734
}
2835

2936
@SuppressWarnings("unchecked")
3037
@Override
3138
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
3239
block = (Expression<Block>) exprs[0];
40+
blockId = (Expression<String>) exprs[1];
41+
setNegated(matchedPattern == 1);
3342
return true;
3443
}
3544

3645
@Override
3746
public boolean check(Event e) {
3847
Block[] blocks = block.getArray(e);
3948
if (blocks == null) {
40-
return false;
49+
return isNegated();
4150
}
4251

4352
for (Block b : blocks) {
4453
CustomBlock customBlock = CustomBlock.byAlreadyPlaced(b);
45-
if (customBlock != null) {
46-
return true;
54+
if (customBlock == null) {
55+
return isNegated();
56+
}
57+
if (blockId != null) {
58+
String id = blockId.getSingle(e);
59+
if (id == null || !customBlock.getId().equals(id)) {
60+
return isNegated();
61+
}
4762
}
4863
}
49-
return false;
64+
return !isNegated();
5065
}
5166

52-
5367
@Override
5468
public String toString(@Nullable Event e, boolean debug) {
55-
return block.toString(e, debug) + " is an ItemsAdder block";
69+
return block.toString(e, debug) + (isNegated() ? " isn't" : " is") + " a custom block" + (blockId != null ? " with id " + blockId.toString(e, debug) : "");
5670
}
5771
}
72+

src/main/java/me/asleepp/SkriptItemsAdder/elements/conditions/CondIsCustomEntity.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import ch.njol.skript.doc.Description;
55
import ch.njol.skript.doc.Examples;
66
import ch.njol.skript.doc.Name;
7+
import ch.njol.skript.doc.RequiredPlugins;
8+
import ch.njol.skript.doc.Since;
79
import ch.njol.skript.lang.Condition;
810
import ch.njol.skript.lang.Expression;
911
import ch.njol.skript.lang.SkriptParser;
@@ -16,37 +18,50 @@
1618
@Name("Is ItemsAdder Entity")
1719
@Description({"Checks if the entity is an ItemsAdder entity."})
1820
@Examples({
19-
"on damage:",
20-
"\tif event-entity is a custom entity",
21-
"\t\tsend \"how could you?\" to player"})
21+
"on damage:",
22+
"\tif event-entity is a custom entity:",
23+
"\t\tsend \"how could you?\" to player",
24+
"\tif event-entity is not a custom entity:",
25+
"\t\t send \"Incredible!\" to player "})
26+
@Since("1.0, 1.5 (Negative Comparison)")
27+
@RequiredPlugins("ItemsAdder")
2228
public class CondIsCustomEntity extends Condition {
2329

2430
private Expression<Entity> entities;
25-
31+
private Expression<String> entityId;
2632

2733
static {
28-
Skript.registerCondition(CondIsCustomEntity.class, new String[] {"%entities% (is [a[n]]|are) (custom|ia|itemsadder) entit(y|ies)"});
34+
Skript.registerCondition(CondIsCustomEntity.class, new String[] {"%entities% (is [a[n]]|are) (custom|ia|itemsadder) entit(y|ies) [[with id] %-string%]", "%entities% (is[n't| not]) [a] (custom|ia|itemsadder) entit(y|ies) [[with id] %-string%]"});
2935
}
3036

3137
@SuppressWarnings("unchecked")
3238
@Override
3339
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
3440
entities = (Expression<Entity>) exprs[0];
41+
entityId = (Expression<String>) exprs[1];
42+
setNegated(matchedPattern == 1);
3543
return true;
3644
}
3745

3846
@Override
3947
public boolean check(Event e) {
4048
for (Entity entity : entities.getArray(e)) {
41-
if (!CustomEntity.isCustomEntity(entity)) {
42-
return false;
49+
CustomEntity customEntity = CustomEntity.byAlreadySpawned(entity);
50+
if (customEntity == null) {
51+
return isNegated();
52+
}
53+
if (entityId != null) {
54+
String id = entityId.getSingle(e);
55+
if (id == null || !customEntity.getId().equals(id)) {
56+
return isNegated();
57+
}
4358
}
4459
}
45-
return true;
60+
return !isNegated();
4661
}
4762

4863
@Override
4964
public String toString(@Nullable Event e, boolean debug) {
50-
return entities.toString(e, debug) + " is an ItemsAdder entity";
65+
return entities.toString(e, debug) + (isNegated() ? " isn't" : " is") + " a custom entity" + (entityId != null ? " with id " + entityId.toString(e, debug) : "");
5166
}
5267
}

src/main/java/me/asleepp/SkriptItemsAdder/elements/conditions/CondIsCustomItem.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import ch.njol.skript.doc.Description;
66
import ch.njol.skript.doc.Examples;
77
import ch.njol.skript.doc.Name;
8+
import ch.njol.skript.doc.RequiredPlugins;
9+
import ch.njol.skript.doc.Since;
810
import ch.njol.skript.lang.Condition;
911
import ch.njol.skript.lang.Expression;
1012
import ch.njol.skript.lang.SkriptParser;
@@ -18,53 +20,55 @@
1820
@Name("Is ItemsAdder Item")
1921
@Description({"Checks if the item is an ItemsAdder item."})
2022
@Examples({"if player's tool is a custom item", "if player's tool is a custom item \"icon_arrow_chest\""})
23+
@Since("1.0, 1.5 (Negative comparison)")
24+
@RequiredPlugins("ItemsAdder")
2125
public class CondIsCustomItem extends Condition {
2226

2327
private Expression<ItemType> item;
2428
private Expression<String> itemId;
2529

2630
static{
27-
Skript.registerCondition(CondIsCustomItem.class, new String[] {"%itemtypes% (is [a[n]]|are) (custom|ia|itemsadder) item[s] [[with id] %-string%]"});
31+
Skript.registerCondition(CondIsCustomItem.class, new String[] {"%itemtypes% (is [a[n]]|are) (custom|ia|itemsadder) item[s] [[with id] %-string%]", "%itemtypes% (is[n't| not]) [a] (custom|ia|itemsadder) item[s] [[with id] %-string%]"});
2832
}
2933

3034
@SuppressWarnings("unchecked")
3135
@Override
3236
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
3337
item = (Expression<ItemType>) exprs[0];
3438
itemId = (Expression<String>) exprs[1];
39+
setNegated(matchedPattern == 1);
3540
return true;
3641
}
3742

3843
@Override
3944
public boolean check(Event e) {
4045
ItemType[] items = item.getArray(e);
4146
if (items == null) {
42-
return false;
47+
return isNegated();
4348
}
4449

4550
for (ItemType itemType : items) {
4651
ItemStack itemStack = itemType.getRandom();
4752
if (itemStack == null) {
48-
return false;
53+
return isNegated();
4954
}
5055
CustomStack customStack = CustomStack.byItemStack(itemStack);
5156
if (customStack == null) {
52-
return false;
57+
return isNegated();
5358
}
5459
if (itemId != null) {
5560
String id = itemId.getSingle(e);
5661
if (id == null || !customStack.getId().equals(id)) {
57-
return false;
62+
return isNegated();
5863
}
5964
}
6065
}
61-
return true;
66+
return !isNegated();
6267
}
6368

64-
6569
@Override
6670
public String toString(@Nullable Event e, boolean debug) {
67-
return item.toString(e, debug) + " is a custom item" + (itemId != null ? " with id " + itemId.toString(e, debug) : "");
71+
return item.toString(e, debug) + (isNegated() ? " isn't" : " is") + " a custom item" + (itemId != null ? " with id " + itemId.toString(e, debug) : "");
6872
}
69-
7073
}
74+

0 commit comments

Comments
 (0)