forked from RypoFalem/ArmorStandEditor
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CORE] Ground Work for First menu being fully reimplemented
Signed-off-by: Wolfieheart <[email protected]>
- Loading branch information
1 parent
9d190a3
commit 503469c
Showing
5 changed files
with
313 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/main/java/io/github/rypofalem/armorstandeditor/Utils/Utils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package io.github.rypofalem.armorstandeditor.Utils; | ||
|
||
public abstract class Utils { | ||
|
||
public static final double FULL_CIRCLE = Math.PI * 2; | ||
|
||
public static <T extends Enum<?>> String getEnumList(Class<T> enumType) { | ||
return getEnumList(enumType, " | "); | ||
} | ||
|
||
public static <T extends Enum<?>> String getEnumList(Class<T> enumType, String delimiter) { | ||
StringBuilder list = new StringBuilder(); | ||
boolean put = false; | ||
for (Enum<?> e : enumType.getEnumConstants()) { | ||
list.append(e.toString()).append(delimiter); | ||
put = true; | ||
} | ||
if (put) list = new StringBuilder(list.substring(0, list.length() - delimiter.length())); | ||
return list.toString(); | ||
} | ||
|
||
public static double addAngle(double current, double angleChange) { | ||
current += angleChange; | ||
current = fixAngle(current, angleChange); | ||
return current; | ||
} | ||
|
||
public static double subAngle(double current, double angleChange) { | ||
current -= angleChange; | ||
current = fixAngle(current, angleChange); | ||
return current; | ||
} | ||
|
||
//clamps angle to 0 if it exceeds 2PI rad (360 degrees), is closer to 0 than angleChange value, or is closer to 2PI rad than 2PI rad - angleChange value. | ||
private static double fixAngle(double angle, double angleChange) { | ||
if (angle > FULL_CIRCLE) { | ||
return 0; | ||
} | ||
|
||
if (angle > 0 && angle < angleChange && angle < angleChange / 2) { | ||
return 0; | ||
} | ||
|
||
if (angle > FULL_CIRCLE - angle && angle > FULL_CIRCLE - (angleChange / 2)) { | ||
return 0; | ||
} | ||
|
||
return angle; | ||
} | ||
} |
145 changes: 145 additions & 0 deletions
145
src/main/java/io/github/rypofalem/armorstandeditor/menu/EquipmentMenu.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package io.github.rypofalem.armorstandeditor.menu; | ||
|
||
import io.github.rypofalem.armorstandeditor.api.events.editor.ArmorStandEditorOpenedEvent; | ||
import io.github.rypofalem.armorstandeditor.playereditor.PlayerEditor; | ||
|
||
import org.bukkit.Bukkit; | ||
import org.bukkit.Material; | ||
import org.bukkit.entity.ArmorStand; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.inventory.*; | ||
|
||
import net.kyori.adventure.text.Component; | ||
|
||
import org.bukkit.inventory.meta.ItemMeta; | ||
import org.bukkit.persistence.PersistentDataType; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.ArrayList; | ||
import java.util.EnumMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
public class EquipmentMenu implements EditorMenu{ | ||
|
||
Inventory menuInv; | ||
private ArmorStand armorStand; | ||
private PlayerEditor pe; | ||
static String menuName = "ArmorStand Eqiupment"; | ||
ItemStack helm, chest, pants, boots, rHand, lHand; | ||
|
||
public EquipmentMenu(PlayerEditor pe, ArmorStand as){ | ||
this.pe = pe; | ||
this.armorStand = as; | ||
menuName = pe.plugin.getLang().getMessage("equiptitle", "menutitle"); | ||
menuInv = Bukkit.createInventory(this, 18, Component.text(menuName)); | ||
} | ||
|
||
@Override | ||
public void open() { | ||
ArmorStandEditorOpenedEvent evt = new ArmorStandEditorOpenedEvent(pe.getPlayer(), this); | ||
Bukkit.getPluginManager().callEvent(evt); | ||
if(evt.isCancelled()) return; | ||
fillInventory(); | ||
pe.getPlayer().openInventory(this.menuInv); | ||
} | ||
|
||
@Override | ||
public ArmorStand getArmorStand() { | ||
return this.armorStand; | ||
} | ||
|
||
@Override | ||
public @NotNull Inventory getInventory() { | ||
return menuInv; | ||
} | ||
|
||
private void fillInventory() { | ||
menuInv.clear(); | ||
EntityEquipment equipment = armorStand.getEquipment(); | ||
ItemStack theHelmet = equipment.getHelmet(); | ||
ItemStack theChest = equipment.getChestplate(); | ||
ItemStack thePants = equipment.getLeggings(); | ||
ItemStack theFeetsies = equipment.getBoots(); | ||
ItemStack theRightHand = equipment.getItemInMainHand(); | ||
ItemStack theLeftHand = equipment.getItemInOffHand(); | ||
//equipment.clear(); | ||
|
||
ItemStack disabledSlot = new ItemStack(Material.BARRIER); | ||
ItemMeta meta = disabledSlot.getItemMeta(); | ||
meta.displayName(Component.text(pe.plugin.getLang().getMessage("disabled", "warn"))); | ||
meta.getPersistentDataContainer().set(pe.plugin.getIconKey(), PersistentDataType.STRING, "ase icon"); | ||
disabledSlot.setItemMeta(meta); | ||
|
||
ItemStack helmetIcon = createIcon(Material.LEATHER_HELMET, "helm"); | ||
ItemStack chestIcon = createIcon(Material.LEATHER_CHESTPLATE, "chest"); | ||
ItemStack pantsIcon = createIcon(Material.LEATHER_LEGGINGS, "pants"); | ||
ItemStack feetsiesIcon = createIcon(Material.LEATHER_BOOTS, "boots"); | ||
ItemStack rightHandIcon = createIcon(Material.WOODEN_SWORD, "rhand"); | ||
ItemStack leftHandIcon = createIcon(Material.SHIELD, "lhand"); | ||
ItemStack[] items = { | ||
helmetIcon, chestIcon, pantsIcon, feetsiesIcon, rightHandIcon, leftHandIcon, disabledSlot, disabledSlot, disabledSlot, | ||
theHelmet, theChest, thePants, theFeetsies, theRightHand, theLeftHand, disabledSlot, disabledSlot, disabledSlot | ||
}; | ||
menuInv.setContents(items); | ||
|
||
} | ||
|
||
private ItemStack createIcon(Material mat, String slot) { | ||
ItemStack icon = new ItemStack(mat); | ||
ItemMeta meta = icon.getItemMeta(); | ||
meta.getPersistentDataContainer().set(pe.plugin.getIconKey(), PersistentDataType.STRING, "ase icon"); | ||
meta.displayName(Component.text(pe.plugin.getLang().getMessage("equipslot", "iconname", slot))); //equipslot.msg <option> | ||
ArrayList<Component> loreList = new ArrayList<>(); | ||
loreList.add(Component.text(pe.plugin.getLang().getMessage("equipslot.description", "icondescription", slot))); //equioslot.description.msg <option> | ||
meta.lore(loreList); | ||
meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES); | ||
meta.addItemFlags(ItemFlag.HIDE_ADDITIONAL_TOOLTIP); | ||
icon.setItemMeta(meta); | ||
return icon; | ||
} | ||
|
||
public void equipArmorstand() { | ||
Map<EquipmentSlot, ItemStack> items = new EnumMap<>(EquipmentSlot.class); | ||
items.put(EquipmentSlot.HEAD, menuInv.getItem(9)); | ||
items.put(EquipmentSlot.CHEST, menuInv.getItem(10)); | ||
items.put(EquipmentSlot.LEGS, menuInv.getItem(11)); | ||
items.put(EquipmentSlot.FEET, menuInv.getItem(12)); | ||
items.put(EquipmentSlot.HAND, menuInv.getItem(13)); | ||
items.put(EquipmentSlot.OFF_HAND, menuInv.getItem(14)); | ||
|
||
List<ItemStack> returnItems = new ArrayList<>(); | ||
EntityEquipment equipment = requireNonNull(armorStand.getEquipment()); | ||
if (!armorStand.isValid() || armorStand.isDead()) { | ||
returnItems.addAll(items.values()); | ||
} else { | ||
for (EquipmentSlot slot : EquipmentSlot.values()) { | ||
ItemStack item = items.get(slot); | ||
if (item == null || item.getType().isAir()) continue; | ||
ItemStack oldItem = equipment.getItem(slot); | ||
if (!oldItem.getType().isAir() && oldItem != null) { | ||
returnItems.add(item); | ||
} else { | ||
equipment.setItem(slot, item); | ||
} | ||
} | ||
} | ||
|
||
Player player = pe.getPlayer(); | ||
for (ItemStack item : returnItems) { | ||
if (item == null || item.getType().isAir()) continue; | ||
for (ItemStack drop : player.getInventory().addItem(item).values()) { | ||
player.getWorld().dropItem(player.getEyeLocation(), drop).setPickupDelay(0); | ||
} | ||
} | ||
|
||
} | ||
|
||
public static String getName() { | ||
return menuName; | ||
} | ||
|
||
|
||
} |
18 changes: 15 additions & 3 deletions
18
src/main/java/io/github/rypofalem/armorstandeditor/playereditor/PlayerEditor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,30 @@ | ||
package io.github.rypofalem.armorstandeditor.playereditor; | ||
|
||
import io.github.rypofalem.armorstandeditor.ArmorStandEditorPlugin; | ||
import org.bukkit.entity.Player; | ||
|
||
import java.util.UUID; | ||
|
||
//TODO: To be Refactored | ||
public class PlayerEditor { | ||
|
||
private UUID uuid; | ||
public final ArmorStandEditorPlugin plugin; | ||
|
||
public PlayerEditor(ArmorStandEditorPlugin plugin) { | ||
public PlayerEditor(UUID uuid, ArmorStandEditorPlugin plugin) { | ||
this.plugin = plugin; | ||
this.uuid = uuid; | ||
} | ||
|
||
public PlayerEditorManager getPlayerEditorManager(){ | ||
return plugin.getEditorManager(); | ||
} | ||
|
||
public PlayerEditorManager getManager(){ | ||
return plugin.editorManager; | ||
public Player getPlayer() { | ||
return plugin.getServer().getPlayer(getUUID()); | ||
} | ||
|
||
public UUID getUUID() { | ||
return uuid; | ||
} | ||
} |
Oops, something went wrong.