Skip to content

Commit

Permalink
[CORE] Commit everything for the Size Menu on /attribute command to now
Browse files Browse the repository at this point in the history
Signed-off-by: Wolfieheart <[email protected]>
  • Loading branch information
Wolfieheart committed Dec 8, 2024
1 parent 22df724 commit 1833e32
Show file tree
Hide file tree
Showing 8 changed files with 365 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>io.github.rypofalem.armorstandeditor</groupId>
<artifactId>armorstandeditor</artifactId>
<packaging>jar</packaging>
<version>1.21.1-47.2</version>
<version>1.21.4-48</version>
<name>armorstandeditor</name>
<url>https://maven.apache.org</url>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ public class ArmorStandEditorPlugin extends JavaPlugin {
List<?> allowedWorldList = null;
boolean allowCustomModelData = false;
Integer customModelDataInt = Integer.MIN_VALUE;
double maxScaleValue;
double minScaleValue;

//GUI Settings
boolean requireSneaking = false;
Expand Down Expand Up @@ -220,6 +222,10 @@ public void onEnable() {
coarseRot = getConfig().getDouble("coarse");
fineRot = getConfig().getDouble("fine");

// Scale Values for Size
maxScaleValue = getConfig().getDouble("maxScaleValue");
minScaleValue = getConfig().getDouble("minScaleValue");

//Set Tool to be used in game
toolType = getConfig().getString("tool");
if (toolType != null) {
Expand Down Expand Up @@ -580,6 +586,10 @@ public void performReload() {
coarseRot = getConfig().getDouble("coarse");
fineRot = getConfig().getDouble("fine");

// Scale Values for Size
maxScaleValue = getConfig().getDouble("maxScaleValue");
minScaleValue = getConfig().getDouble("minScaleValue");

//Set Tool to be used in game
toolType = getConfig().getString("tool");
if (toolType != null) {
Expand Down Expand Up @@ -745,4 +755,12 @@ public boolean isDebug() {
public void debugMsgHandler(String msg) {
if (isDebug()) getServer().getLogger().log(Level.WARNING, "[ASE-DEBUG]: {0}", msg);
}

public double getMinScaleValue() {
return minScaleValue;
}

public double getMaxScaleValue() {
return maxScaleValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package io.github.rypofalem.armorstandeditor;

import io.github.rypofalem.armorstandeditor.menu.SizeMenu;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;

Expand Down Expand Up @@ -69,6 +70,7 @@ public class PlayerEditor {
int frameTargetIndex = 0;
EquipmentMenu equipMenu;
PresetArmorPosesMenu presetPoseMenu;
SizeMenu sizeModificationMenu;
long lastCancelled = 0;

public PlayerEditor(UUID uuid, ArmorStandEditorPlugin plugin) {
Expand Down Expand Up @@ -139,7 +141,7 @@ public void editArmorStand(ArmorStand armorStand) {
toggleArms(armorStand);
break;
case SIZE:
toggleSize(armorStand);
chooseSize(armorStand);
break;
case INVISIBLE:
toggleVisible(armorStand);
Expand Down Expand Up @@ -225,6 +227,25 @@ private void choosePreset(ArmorStand armorStand) {
presetPoseMenu.openMenu();
}

//Size Menu Refactor
/* void toggleSize(ArmorStand armorStand) {
if (getPlayer().hasPermission("asedit.togglesize")) {
armorStand.setSmall(!armorStand.isSmall());
} else {
sendMessage("nopermoption", "warn", "size");
}
}
*/
private void chooseSize(ArmorStand armorStand){
if(!getPlayer().hasPermission("asedit.togglesize")){
sendMessage("nopermoption", "warn", "size");
return;
} else {
sizeModificationMenu = new SizeMenu(this, armorStand);
sizeModificationMenu.openMenu();
}
}

public void reverseEditArmorStand(ArmorStand armorStand) {
if (!getPlayer().hasPermission("asedit.basic")) return;

Expand Down Expand Up @@ -476,13 +497,6 @@ void toggleItemFrameVisible(ItemFrame itemFrame) {
}
}

void toggleSize(ArmorStand armorStand) {
if (getPlayer().hasPermission("asedit.togglesize")) {
armorStand.setSmall(!armorStand.isSmall());
} else {
sendMessage("nopermoption", "warn", "size");
}
}

void cycleAxis(int i) {
int index = axis.ordinal();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class PlayerEditorManager implements Listener {
private ASEHolder menuHolder = new ASEHolder(); //Inventory holder that owns the main ase menu inventories for the plugin
private ASEHolder equipmentHolder = new ASEHolder(); //Inventory holder that owns the equipment menu
private ASEHolder presetHolder = new ASEHolder(); //Inventory Holder that owns the PresetArmorStand Post Menu
private ASEHolder sizeMenuHolder = new ASEHolder(); //Inventory Holder that owns the PresetArmorStand Post Menu
double coarseAdj;
double fineAdj;
double coarseMov;
Expand Down Expand Up @@ -432,6 +433,17 @@ void onPlayerMenuSelect(InventoryClickEvent e) {
pe.presetPoseMenu.handlePresetPose(itemName, player);
}
}

if (e.getInventory().getHolder() == sizeMenuHolder){
e.setCancelled(true);
ItemStack item = e.getCurrentItem();
if(item != null && item.hasItemMeta()){
Player player = (Player) e.getWhoClicked();
String itemName = item.getItemMeta().getDisplayName();
PlayerEditor pe = players.get(player.getUniqueId());
pe.sizeModificationMenu.handleAttributeScaling(itemName,player);
}
}
}


Expand Down Expand Up @@ -472,6 +484,10 @@ public ASEHolder getEquipmentHolder() {
return equipmentHolder;
}

public ASEHolder getSizeMenuHolder() {
return sizeMenuHolder;
}

public ASEHolder getPresetHolder() {
return presetHolder;
}
Expand Down
233 changes: 233 additions & 0 deletions src/main/java/io/github/rypofalem/armorstandeditor/menu/SizeMenu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
package io.github.rypofalem.armorstandeditor.menu;

import io.github.rypofalem.armorstandeditor.ArmorStandEditorPlugin;
import io.github.rypofalem.armorstandeditor.PlayerEditor;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.attribute.Attribute;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;

import java.util.ArrayList;
import java.util.Map;

public class SizeMenu extends ASEHolder {

public ArmorStandEditorPlugin plugin = ArmorStandEditorPlugin.instance();
Inventory menuInv;
private PlayerEditor pe;
private ArmorStand as;
static String name = "Size Menu";

public SizeMenu(PlayerEditor pe, ArmorStand as){
this.pe = pe;
this.as = as;
name = pe.plugin.getLang().getMessage("sizeMenu", "menutitle");
menuInv = Bukkit.createInventory(pe.getManager().getSizeMenuHolder(), 27 , name);
}

//PRESET NAMES
final String SCALE1 = plugin.getLang().getMessage("scale1").replace("§6","§2§n");
final String SCALE2 = plugin.getLang().getMessage("scale2").replace("§6","§2§n");
final String SCALE3 = plugin.getLang().getMessage("scale3").replace("§6","§2§n");
final String SCALE4 = plugin.getLang().getMessage("scale4").replace("§6","§2§n");
final String SCALE5 = plugin.getLang().getMessage("scale5").replace("§6","§2§n");
final String SCALE6 = plugin.getLang().getMessage("scale6").replace("§6","§2§n");
final String SCALE7 = plugin.getLang().getMessage("scale7").replace("§6","§2§n");
final String SCALE8 = plugin.getLang().getMessage("scale8").replace("§6","§2§n");
final String SCALE9 = plugin.getLang().getMessage("scale9").replace("§6","§2§n");
final String SCALE10 = plugin.getLang().getMessage("scale10").replace("§6","§2§n");
final String SCALEPLUS12 = plugin.getLang().getMessage("scaleadd12").replace("§6","§2§n");
final String SCALEMINUS12= plugin.getLang().getMessage("scaleremove12").replace("§6","§2§n");
final String SCALEPLUS110 = plugin.getLang().getMessage("scaleadd110").replace("§6","§2§n");
final String SCALEMINUS110 = plugin.getLang().getMessage("scaleremove110").replace("§6","§2§n");

//Menu Stuff
final String BACKTOMENU = plugin.getLang().getMessage("backtomenu").replace("§6","§2§n");
final String RESET = plugin.getLang().getMessage("reset").replace("§6","§2§n");


private void fillInventory(){
menuInv.clear();
ItemStack blankSlot= createIcon(new ItemStack(Material.BLACK_STAINED_GLASS_PANE, 1), "blankslot");
ItemStack base10 = createIcon(new ItemStack(Material.RED_CONCRETE, 1), "scale1");
ItemStack base20 = createIcon(new ItemStack(Material.RED_CONCRETE, 2), "scale2");
ItemStack base30 = createIcon(new ItemStack(Material.RED_CONCRETE, 3), "scale3");
ItemStack base40 = createIcon(new ItemStack(Material.RED_CONCRETE, 4), "scale4");
ItemStack base50 = createIcon(new ItemStack(Material.RED_CONCRETE, 5), "scale5");
ItemStack base60 = createIcon(new ItemStack(Material.RED_CONCRETE, 6), "scale6");
ItemStack base70 = createIcon(new ItemStack(Material.RED_CONCRETE, 7), "scale7");
ItemStack base80 = createIcon(new ItemStack(Material.RED_CONCRETE, 8), "scale8");
ItemStack base90 = createIcon(new ItemStack(Material.RED_CONCRETE, 9), "scale9");
ItemStack base100 = createIcon(new ItemStack(Material.RED_CONCRETE, 10), "scale10");
ItemStack add12toBase = createIcon(new ItemStack(Material.ORANGE_CONCRETE, 1), "scaleadd12");
ItemStack remove12fromBase = createIcon(new ItemStack(Material.GREEN_CONCRETE ,1), "scaleremove12");
ItemStack add110fromBase = createIcon(new ItemStack(Material.ORANGE_CONCRETE ,2), "scaleadd110");
ItemStack remove110fromBase = createIcon(new ItemStack(Material.GREEN_CONCRETE ,2), "scaleremove110");
ItemStack backToMenu = createIcon(new ItemStack(Material.RED_WOOL, 1), "backtomenu");
ItemStack resetIcon = createIcon(new ItemStack(Material.NETHER_STAR, 1), "reset");

ItemStack[] items = {
backToMenu,blankSlot,base10,base20,base30,base40,base50,base60,blankSlot,
resetIcon,blankSlot,base70,base80,base90,base100,blankSlot,add12toBase,remove12fromBase,
blankSlot,blankSlot,blankSlot,blankSlot,blankSlot,blankSlot,blankSlot,add110fromBase,remove110fromBase
};

menuInv.setContents(items);
}

private ItemStack createIcon(ItemStack icon, String path) {
return createIcon(icon, path, null);
}

private ItemStack createIcon(ItemStack icon, String path, String option) {
ItemMeta meta = icon.getItemMeta();
assert meta != null;
meta.getPersistentDataContainer().set(ArmorStandEditorPlugin.instance().getIconKey(), PersistentDataType.STRING, "ase " + option);
meta.setDisplayName(getIconName(path, option));
ArrayList<String> loreList = new ArrayList<>();
loreList.add(getIconDescription(path, option));
meta.setLore(loreList);
meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
icon.setItemMeta(meta);
return icon;
}

private String getIconName(String path, String option) {
return pe.plugin.getLang().getMessage(path, "iconname", option);
}


private String getIconDescription(String path, String option) {
return pe.plugin.getLang().getMessage(path + ".description", "icondescription", option);
}


public void handleAttributeScaling(String itemName, Player player) {
if (itemName == null || player == null) return;

// Separate maps for positive and negative scaling options
Map<String, Double> positiveScaleMap = Map.ofEntries(
Map.entry(SCALE1, 1.0),
Map.entry(SCALE2, 2.0),
Map.entry(SCALE3, 3.0),
Map.entry(SCALE4, 4.0),
Map.entry(SCALE5, 5.0),
Map.entry(SCALE6, 6.0),
Map.entry(SCALE7, 7.0),
Map.entry(SCALE8, 8.0),
Map.entry(SCALE9, 9.0),
Map.entry(SCALE10, 10.0),
Map.entry(SCALEPLUS12, 0.5),
Map.entry(SCALEPLUS110, 0.1)
);

Map<String, Double> negativeScaleMap = Map.ofEntries(
Map.entry(SCALEMINUS12, 0.5), // Changed value to negative for decrement
Map.entry(SCALEMINUS110, 0.1) // Changed value to negative for decrement
);

if (positiveScaleMap.containsKey(itemName)) {
handleScaleChange(player, itemName, positiveScaleMap.get(itemName));
} else if (negativeScaleMap.containsKey(itemName)) {
handleScaleChange(player, itemName, negativeScaleMap.get(itemName));
} else if (itemName.equals(BACKTOMENU)) {
handleBackToMenu(player);
} else if (itemName.equals(RESET)) {
handleReset(player);
}
}


private void handleScaleChange(Player player, String itemName, double scale) {
setArmorStandScale(player, itemName, scale);
playChimeSound(player);
player.closeInventory();
}

private void handleBackToMenu(Player player) {
player.playSound(player.getLocation(), Sound.BLOCK_COMPARATOR_CLICK, 1, 1);
player.closeInventory();
pe.openMenu();
}

private void handleReset(Player player) {
setArmorStandScale(player, RESET, 1);
playChimeSound(player);
player.closeInventory();
}

private void playChimeSound(Player player) {
player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_CHIME, 1, 1);
}

private void setArmorStandScale(Player player, String itemName, double scaleValue) {

double currentScaleValue; double newScaleValue;

for (Entity theArmorStand : player.getNearbyEntities(1,1,1)){
if(theArmorStand instanceof ArmorStand as){

// Permission Check
if (!player.hasPermission("asedit.togglesize")) return;

// Can be overwritten
currentScaleValue = 0;

// Basically go from 0 to ItemSize
if(itemName.equals(SCALE1) || itemName.equals(SCALE2) || itemName.equals(SCALE3)
|| itemName.equals(SCALE4)|| itemName.equals(SCALE5)|| itemName.equals(SCALE6)
|| itemName.equals(SCALE7)|| itemName.equals(SCALE8)|| itemName.equals(SCALE9)
|| itemName.equals(SCALE10)){
newScaleValue = currentScaleValue + scaleValue;
if(newScaleValue >= plugin.getMaxScaleValue()){
pe.plugin.getLang().getMessage("scalemaxwarn","warn");
return;
} else if(newScaleValue <= plugin.getMinScaleValue()){
pe.plugin.getLang().getMessage("scaleminwarn","warn");
return;
} else {
as.getAttribute(Attribute.GENERIC_SCALE).setBaseValue(newScaleValue);
}
} else if(itemName.equals(SCALEPLUS12) || itemName.equals(SCALEPLUS110)){
currentScaleValue = as.getAttribute(Attribute.GENERIC_SCALE).getBaseValue();
newScaleValue = currentScaleValue + scaleValue; // Add for increments
if(newScaleValue >= plugin.getMaxScaleValue()){
pe.plugin.getLang().getMessage("scalemaxwarn", "warn");
return;
}
as.getAttribute(Attribute.GENERIC_SCALE).setBaseValue(newScaleValue);
} else if(itemName.equals(SCALEMINUS12) || itemName.equals(SCALEMINUS110)){
currentScaleValue = as.getAttribute(Attribute.GENERIC_SCALE).getBaseValue();
newScaleValue = currentScaleValue - scaleValue; // Subtract for decrements
if(newScaleValue <= plugin.getMinScaleValue()){
pe.plugin.getLang().getMessage("scaleminwarn", "warn");
return;
}
as.getAttribute(Attribute.GENERIC_SCALE).setBaseValue(newScaleValue);
}else if(itemName.equals(RESET)){
newScaleValue = 1;
as.getAttribute(Attribute.GENERIC_SCALE).setBaseValue(newScaleValue);
}


}
}


}

public void openMenu() {
if(pe.getPlayer().hasPermission("asedit.togglesize")){
fillInventory();
pe.getPlayer().openInventory(menuInv);
}
}
}
Loading

0 comments on commit 1833e32

Please sign in to comment.