Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ public ItemEditCommand() {

this.registerSubCommand(() -> new Damage(this));
this.registerSubCommand(() -> new Banner(this));
this.registerSubCommand(() -> VersionUtils.isVersionUpTo(1, 10) ?
new ColorOld(this) : new ColorSubcommand(this));
this.registerSubCommand(() -> new ColorSubcommand(this));
this.registerSubCommand(() -> new SkullOwner(this));
this.registerSubCommand(() -> new FireworkPower(this));
this.registerSubCommand(() -> new Firework(this));
Expand Down
93 changes: 0 additions & 93 deletions src/main/java/emanondev/itemedit/command/itemedit/ColorOld.java

This file was deleted.

155 changes: 70 additions & 85 deletions src/main/java/emanondev/itemedit/command/itemedit/ColorSubcommand.java
Original file line number Diff line number Diff line change
@@ -1,136 +1,121 @@
package emanondev.itemedit.command.itemedit;

import com.google.common.base.Preconditions;
import emanondev.itemedit.Util;
import emanondev.itemedit.command.ItemEditCommand;
import emanondev.itemedit.command.SubCmd;
import emanondev.itemedit.gui.ColorGui;
import emanondev.itemedit.utility.ItemUtils;
import org.bukkit.FireworkEffect;
import emanondev.itemedit.utility.ColoredMeta;
import emanondev.itemedit.utility.VersionUtils;
import org.bukkit.Color;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.FireworkEffectMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

import java.util.Collections;
import java.util.List;

public class ColorSubcommand extends SubCmd {

private final boolean supportsGui;
private final String tippedArrowPerm;
private final String potionPerm;
private final String leatherPerm;
private final String starsPerm;
private final String mapPerm;

public ColorSubcommand(@NotNull ItemEditCommand cmd) {
super("color", cmd, true, true);
supportsGui = VersionUtils.isVersionAfter(1, 10);
tippedArrowPerm = getPermission() + ".tipped_arrow";
potionPerm = getPermission() + ".potion";
leatherPerm = getPermission() + ".leather";
starsPerm = getPermission() + ".firework_star";
mapPerm = getPermission() + ".map";
}

@Override
public void onCommand(@NotNull CommandSender sender, @NotNull String alias, String[] args) {
Player p = (Player) sender;
ItemStack item = this.getItemInHand(p);
ItemMeta meta = ItemUtils.getMeta(item);
if ((meta instanceof PotionMeta)) {

if (item.getType() == Material.TIPPED_ARROW && !sender.hasPermission(tippedArrowPerm)) {
this.getCommand().sendPermissionLackMessage(tippedArrowPerm, sender);
return;
}
ItemStack item = getItemInHand(p);

if (item.getType().name().contains("POTION") && !sender.hasPermission(potionPerm)) {
this.getCommand().sendPermissionLackMessage(potionPerm, sender);
return;
}
PotionMeta potionMeta = (PotionMeta) meta;
try {
if (args.length == 1) {
p.openInventory(new ColorGui(p).getInventory());
return;
}

if (args.length != 4)
throw new IllegalArgumentException("Wrong param number");

org.bukkit.Color color = org.bukkit.Color.fromRGB(Integer.parseInt(args[1]),
Integer.parseInt(args[2]),
Integer.parseInt(args[3]));
potionMeta.setColor(color);
item.setItemMeta(potionMeta);
updateView(p);
} catch (Exception e) {
onFail(p, alias);
}
// Get meta type
// We use this wrapper to have a clean code afterward, without having to fear any
// missing class due to an older game version.
ColoredMeta meta = ColoredMeta.of(item);
if(meta == null) {
Util.sendMessage(p, this.getLanguageString("wrong-type", null, sender));
return;
}
if ((meta instanceof LeatherArmorMeta)) {
if (!sender.hasPermission(leatherPerm)) {
this.getCommand().sendPermissionLackMessage(leatherPerm, sender);
return;
}

LeatherArmorMeta leatherMeta = (LeatherArmorMeta) meta;
try {
if (args.length == 1) {
p.openInventory(new ColorGui(p).getInventory());
// Check permissions, according to type
switch (meta.getType()) {
case POTION:
if (item.getType() == Material.TIPPED_ARROW && !sender.hasPermission(tippedArrowPerm)) {
getCommand().sendPermissionLackMessage(tippedArrowPerm, sender);
return;
}
if (args.length != 4) {
throw new IllegalArgumentException("Wrong param number");
if (item.getType().name().contains("POTION") && !sender.hasPermission(potionPerm)) {
getCommand().sendPermissionLackMessage(potionPerm, sender);
return;
}

org.bukkit.Color color = org.bukkit.Color.fromRGB(Integer.parseInt(args[1]),
Integer.parseInt(args[2]),
Integer.parseInt(args[3]));
leatherMeta.setColor(color);
item.setItemMeta(leatherMeta);
updateView(p);
} catch (Exception e) {
onFail(p, alias);
}
return;
break;
case LEATHER_ARMOR:
if (!sender.hasPermission(leatherPerm)) {
getCommand().sendPermissionLackMessage(leatherPerm, sender);
return;
}
break;
case FIREWORK:
if (!sender.hasPermission(starsPerm)) {
getCommand().sendPermissionLackMessage(starsPerm, sender);
return;
}
break;
case FILLED_MAP:
if (!sender.hasPermission(mapPerm)) {
getCommand().sendPermissionLackMessage(mapPerm, sender);
return;
}
break;
}
if (meta instanceof FireworkEffectMeta) {
if (!sender.hasPermission(starsPerm)) {
this.getCommand().sendPermissionLackMessage(starsPerm, sender);

// Change the color or open a GUI, according to the type and args count.
try {
if (supportsGui && args.length == 1) {
p.openInventory(new ColorGui(p, item).getInventory());
return;
}

FireworkEffectMeta starMeta = (FireworkEffectMeta) meta;
try {
if (args.length != 4) {
throw new IllegalArgumentException("Wrong param number");
}
// Read color from args
meta.setColor(parseColor(args));

org.bukkit.Color color = org.bukkit.Color.fromRGB(Integer.parseInt(args[1]),
Integer.parseInt(args[2]),
Integer.parseInt(args[3]));
FireworkEffect oldEffect = starMeta.getEffect(); // may be null?
FireworkEffect.Builder newEffect = FireworkEffect.builder().flicker(oldEffect != null && oldEffect.hasFlicker())
.trail(oldEffect != null && oldEffect.hasTrail()).withColor(color);
if (oldEffect != null && oldEffect.getFadeColors() != null) {// may be null?
newEffect.withFade(oldEffect.getFadeColors());
}
starMeta.setEffect(newEffect.build());
item.setItemMeta(starMeta);
updateView(p);
} catch (Exception e) {
onFail(p, alias);
}
return;
// Apply and update
meta.setToItem(item);
updateView(p);
} catch(Exception e) {
onFail(p, alias);
}
Util.sendMessage(p, this.getLanguageString("wrong-type", null, sender));
}

/**
* Parse a bukkit Color from an argument array.
* @param args arguments array. Size must be > 3.
* @return a new color instance.
*/
@Contract("_ -> new")
private @NotNull Color parseColor(String @NotNull [] args) {
Preconditions.checkArgument(args.length >= 4, "Missing parameters.");
return Color.fromRGB(
Integer.parseInt(args[1]),
Integer.parseInt(args[2]),
Integer.parseInt(args[3])
);
}

// itemedit bookauthor <name>
@Override
public List<String> onComplete(@NotNull CommandSender sender, String[] args) {
return Collections.emptyList();
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/emanondev/itemedit/gui/ColorGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@ public class ColorGui implements Gui {
private final ItemMeta colorableMeta;
private final ItemMeta cleanColorableMeta;


public ColorGui(@NotNull Player target) {
public ColorGui(@NotNull Player target, @NotNull ItemStack item) {
String title = getLanguageMessage(subPath + "title");
this.inventory = Bukkit.createInventory(this, (6) * 9, title);
this.target = target;
this.colorable = ItemUtils.getHandItem(getTargetPlayer());
this.colorableMeta = ItemUtils.getMeta(this.colorable);
this.colorable = item;
this.colorableMeta = ItemUtils.getMeta(item);
cleanColorableMeta = colorableMeta.clone();
cleanColorableMeta.addItemFlags(ItemFlag.values());
cleanColorableMeta.setDisplayName(null);
Expand All @@ -50,6 +49,7 @@ public ColorGui(@NotNull Player target) {
}

@Override
@SuppressWarnings("deprecation")
public void onClose(InventoryCloseEvent event) {
try {
target.getInventory().setItemInMainHand(colorable);
Expand All @@ -59,7 +59,7 @@ public void onClose(InventoryCloseEvent event) {
}

@Override
public void onClick(InventoryClickEvent event) {
public void onClick(@NotNull InventoryClickEvent event) {
Color original = ItemUtils.getColor(colorableMeta);
int[] colors = fromColor(original);
switch (event.getSlot()) {
Expand Down
Loading