Skip to content

Commit

Permalink
[Release] impl support armor enchantments & custom name also cooldown…
Browse files Browse the repository at this point in the history
… in form
  • Loading branch information
Josscoder committed Mar 20, 2023
1 parent 7c6efc0 commit 29c46a7
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 60 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# JKit

help plugin for YanyanQt

https://www.youtube.com/watch?v=ZLzqiZbvmz4
Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>josscoder.jkit</groupId>
<artifactId>JKit</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/josscoder/jkit/command/KitCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import josscoder.jkit.data.Kit;
import josscoder.jkit.helper.Helper;

import java.time.Duration;

public class KitCommand extends Command {

public KitCommand() {
Expand Down Expand Up @@ -92,11 +90,16 @@ private void selectKit(Player player) {

Helper helper = JKitPlugin.getInstance().getHelper();

helper.getKitList().values().forEach(kit -> windowForm.addButton(kit.getId(), String.format(
"%s\n%s",
kit.getName(),
(kit.isAvailable(player) ? TextFormat.DARK_GREEN + "Available" : TextFormat.DARK_RED + "No available")
), kit.getImage()));
helper.getKitList().values().forEach(kit -> {
String timeLeft = kit.getTimeLeftString(player);
String state = (kit.isAvailable(player) ? TextFormat.DARK_GREEN + "Available" : TextFormat.DARK_RED + (kit.hasCooldown(player) ? timeLeft : "No available"));

windowForm.addButton(kit.getId(), String.format(
"%s\n%s",
kit.getName(),
state
), kit.getImage());
});

windowForm.addHandler(event -> {
if (event.wasClosed()) {
Expand All @@ -122,11 +125,8 @@ private void selectKit(Player player) {
}

if (kit.hasCooldown(player) && !player.isOp()) {
int secondsLeft = kit.getTimeLeft(player) - helper.getCurrentSeconds();
Duration duration = Duration.ofSeconds(secondsLeft);
String durationString = JKitPlugin.getInstance().getHelper().formatDuration(duration);

player.sendMessage(TextFormat.RED + String.format("You have to wait %s to equip this kit again!", durationString));
String timeLeft = kit.getTimeLeftString(player);
player.sendMessage(TextFormat.RED + String.format("You have to wait %s to equip this kit again!", timeLeft));

return;
}
Expand Down
47 changes: 17 additions & 30 deletions src/main/java/josscoder/jkit/data/Kit.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import cn.nukkit.Player;
import cn.nukkit.inventory.PlayerInventory;
import cn.nukkit.item.Item;
import cn.nukkit.item.enchantment.Enchantment;
import cn.nukkit.utils.ConfigSection;
import cn.nukkit.utils.TextFormat;
import josscoder.jkit.JKitPlugin;
import josscoder.jkit.helper.Helper;
import lombok.Getter;

import java.time.Duration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Getter
Expand Down Expand Up @@ -40,43 +40,22 @@ public Kit(String id, String name, String permission, int cooldown, PlayerInvent
this.itemList.putAll(playerInventory.getContents());
}

public Kit(String id, ConfigSection mainSection) {
public Kit(String id, ConfigSection mainSection, Helper helper) {
this.id = id;
this.image = mainSection.getString("image", "textures/blocks/barrier.png");
this.name = mainSection.getString("name", "No name");
this.permission = mainSection.getString("permission", "kit.permission");
this.cooldown = mainSection.getInt("cooldown");
this.helmet = Item.get(Integer.parseInt(mainSection.getString("helmet")));
this.chestPlate = Item.get(Integer.parseInt(mainSection.getString("chestPlate")));
this.leggings = Item.get(Integer.parseInt(mainSection.getString("leggings")));
this.boots = Item.get(Integer.parseInt(mainSection.getString("boots")));

this.helmet = helper.readItemDataFromSection(mainSection.getSection("helmet"));
this.chestPlate = helper.readItemDataFromSection(mainSection.getSection("chestPlate"));
this.leggings = helper.readItemDataFromSection(mainSection.getSection("leggings"));
this.boots = helper.readItemDataFromSection(mainSection.getSection("boots"));

ConfigSection itemsSection = mainSection.getSection("items");
itemsSection.getSections().getKeys(false).forEach(key -> {
ConfigSection currentItemSection = itemsSection.getSection(key);
String[] itemData = currentItemSection.getString("data").split(":");

Item item = Item.get(Integer.parseInt(itemData[0]),
itemData.length >= 2 ? Integer.parseInt(itemData[1]) : 0,
itemData.length == 3 ? Integer.parseInt(itemData[2]) : 1
);

if (currentItemSection.exists("customName")) {
item.setCustomName(currentItemSection.getString("customName"));
}

List<String> enchantmentsList = currentItemSection.getStringList("enchantments");
if (!enchantmentsList.isEmpty()) {
enchantmentsList.forEach(enchantment -> {
String[] split = enchantment.split(":");
if (enchantment.length() > 0 && split.length > 0) {
Enchantment enchantment1 = Enchantment.getEnchantment(Integer.parseInt(split[0]))
.setLevel(split.length == 2 ? Integer.parseInt(split[1]) : 1, false);
item.addEnchantment(enchantment1);
}
});
}

Item item = helper.readItemDataFromSection(currentItemSection);
itemList.put(Integer.parseInt(key), item);
});
}
Expand All @@ -101,6 +80,14 @@ public boolean isAvailable(Player player) {
return !hasCooldown(player) || player.isOp() || player.hasPermission(permission);
}

public String getTimeLeftString(Player player) {
Helper helper = JKitPlugin.getInstance().getHelper();

int secondsLeft = getTimeLeft(player) - helper.getCurrentSeconds();
Duration duration = Duration.ofSeconds(secondsLeft);
return helper.formatDuration(duration);
}

public void give(Player player) {
PlayerInventory inventory = player.getInventory();

Expand Down
64 changes: 53 additions & 11 deletions src/main/java/josscoder/jkit/helper/Helper.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public Helper(Config config) {

public void loadKits() {
mainSection.getSections().getKeys(false).forEach(key ->
registerKit(new Kit(key, mainSection.getSection(key)))
registerKit(new Kit(key, mainSection.getSection(key), this))
);
}

Expand All @@ -51,20 +51,27 @@ public void registerKit(Kit kit, boolean serialize) {
mainSection.set("name", kit.getName());
mainSection.set("permission", kit.getPermission());
mainSection.set("cooldown", kit.getCooldown());
mainSection.set("helmet", String.valueOf(kit.getHelmet().getId()));
mainSection.set("chestPlate", String.valueOf(kit.getChestPlate().getId()));
mainSection.set("leggings", String.valueOf(kit.getLeggings().getId()));
mainSection.set("boots", String.valueOf(kit.getBoots().getId()));

ConfigSection helmet = new ConfigSection();
writeItemDataIntoSection(kit.getHelmet(), helmet);
mainSection.set("helmet", helmet);

ConfigSection chestPlate = new ConfigSection();
writeItemDataIntoSection(kit.getChestPlate(), chestPlate);
mainSection.set("chestPlate", chestPlate);

ConfigSection leggings = new ConfigSection();
writeItemDataIntoSection(kit.getLeggings(), leggings);
mainSection.set("leggings", leggings);

ConfigSection boots = new ConfigSection();
writeItemDataIntoSection(kit.getBoots(), boots);
mainSection.set("boots", boots);

ConfigSection itemsSection = new ConfigSection();
kit.getItemList().forEach((index, item) -> {
ConfigSection currentItemSection = new ConfigSection();
if (item.hasCustomName()) {
currentItemSection.set("customName", item.getCustomName());
}
currentItemSection.set("data", itemToString(item));
currentItemSection.set("enchantments", enchantmentsToStringList(item.getEnchantments()));

writeItemDataIntoSection(item, currentItemSection);
itemsSection.set(String.valueOf(index), currentItemSection);
});

Expand All @@ -74,6 +81,41 @@ public void registerKit(Kit kit, boolean serialize) {
config.save();
}

public void writeItemDataIntoSection(Item item, ConfigSection section) {
if (item.hasCustomName()) {
section.set("customName", item.getCustomName());
}
section.set("data", itemToString(item));
section.set("enchantments", enchantmentsToStringList(item.getEnchantments()));
}

public Item readItemDataFromSection(ConfigSection section) {
String[] itemData = section.getString("data").split(":");

Item item = Item.get(Integer.parseInt(itemData[0]),
itemData.length >= 2 ? Integer.parseInt(itemData[1]) : 0,
itemData.length == 3 ? Integer.parseInt(itemData[2]) : 1
);

if (section.exists("customName")) {
item.setCustomName(section.getString("customName"));
}

List<String> enchantmentsList = section.getStringList("enchantments");
if (!enchantmentsList.isEmpty()) {
enchantmentsList.forEach(enchantment -> {
String[] split = enchantment.split(":");
if (enchantment.length() > 0 && split.length > 0) {
Enchantment enchantment1 = Enchantment.getEnchantment(Integer.parseInt(split[0]))
.setLevel(split.length == 2 ? Integer.parseInt(split[1]) : 1, false);
item.addEnchantment(enchantment1);
}
});
}

return item;
}

public String itemToString(Item item) {
return String.format("%s:%s:%s", item.getId(), item.getDamage(), item.getCount());
}
Expand Down
24 changes: 20 additions & 4 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,26 @@ kits:
image: "textures/items/iron_sword.png"
permission: "gladiator.kit.permission"
cooldown: 60 #in seconds
helmet: 0
chestPlate: 0
leggings: 0
boots: 0
helmet:
data: "1:0:1"
enchantments:
- ""
- ""
chestPlate:
data: "1:0:1"
enchantments:
- ""
- ""
leggings:
data: "1:0:1"
enchantments:
- ""
- ""
boots:
data: "1:0:1"
enchantments:
- ""
- ""
items:
"0":
data: "1:0:1"
Expand Down

0 comments on commit 29c46a7

Please sign in to comment.