Skip to content

Commit

Permalink
Per Shop Setting and necessary bug fixes
Browse files Browse the repository at this point in the history
- Remove Debugger from utils to prevent early access
- Add in server default and user-editable settings for per-shop settings including hopper import/export and no-cost
- Update old values into new locations including no-cost-text, no-cost-amount, and existing hopper settings
- Fix for improper mapped settings writing
- Remove hopper settings from Metrics, need to add back before 2.6.0
- Add in asType settings to ObjectHolder so conversion code doesn't need to be added after retrieval.
- Changed old makeReadable Code to utilize WordUtils.
- Removed getString from ShopType to avoid early setting calls, replace with Setting enum value
- Add try/catch to skippableHoppers boolean updater to avoid early settings call
- Call updateSkippableHoppers a second time after settings has been fully initiated
- Add ShopSettingKeys
- Add Edit Settings GUI to `/ts edit` menu
- Added skippableShop cache so shops can be handled with a different time values and be removed when  their settings are changed and should be reprocessed.
- Added try/catch for IlleaglWorldException to PlayerSetting#getInvolvedStatus methods to prevent error when trying to load shops from unloaded worlds
- Fix for null appearing on shop sign lines 2/3 due to color being added
- Change availableTradeAmount for iTrade Shops to be 999
- Change updateStatus to change to OPEN when availableTrade > 0
- Change getFullTradeCount to check if costs can be traded on BiTrade Shops when no Product is in stock
- Change Hopper protection to not skip shop hoppers(code is commented out dependent on input)
- Add delayed Shop sign and available trade update on inventory close(when it is a shop inventory)
- Changed trade and trade check code to support new noCost setting
  • Loading branch information
KillerOfPie committed Jun 23, 2022
1 parent 2322fea commit 28f2c1b
Show file tree
Hide file tree
Showing 24 changed files with 759 additions and 397 deletions.
2 changes: 2 additions & 0 deletions src/main/java/org/shanerx/tradeshop/TradeShop.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ public void onEnable() {
getSettingManager().reload();
getMessageManager().reload();

getSettingManager().updateSkipHoppers();

getDebugger();

Permissions.registerPermissions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public void reload() {
try {
plugin.getDataStorage().reload(DataType.valueOf(Setting.DATA_STORAGE_TYPE.getString().toUpperCase()));
} catch (IllegalArgumentException iae) {
debugger.log("Config value for data storage set to an invalid value: " + Setting.DATA_STORAGE_TYPE.getString(), DebugLevels.DATA_ERROR);
debugger.log("TradeShop will now disable...", DebugLevels.DATA_ERROR);
PLUGIN.getDebugger().log("Config value for data storage set to an invalid value: " + Setting.DATA_STORAGE_TYPE.getString(), DebugLevels.DATA_ERROR);
PLUGIN.getDebugger().log("TradeShop will now disable...", DebugLevels.DATA_ERROR);
plugin.getServer().getPluginManager().disablePlugin(plugin);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

package org.shanerx.tradeshop.commands.commandrunners;

import de.themoep.inventorygui.GuiElement;
import de.themoep.inventorygui.GuiElementGroup;
import de.themoep.inventorygui.GuiStateElement;
import de.themoep.inventorygui.InventoryGui;
Expand All @@ -40,8 +41,12 @@
import org.shanerx.tradeshop.player.ShopRole;
import org.shanerx.tradeshop.player.ShopUser;
import org.shanerx.tradeshop.shop.Shop;
import org.shanerx.tradeshop.shop.ShopSettingKeys;
import org.shanerx.tradeshop.utils.objects.ObjectHolder;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
Expand All @@ -55,7 +60,8 @@ public class EditCommand extends GUICommand {
private InventoryGui mainMenu,
userEdit,
costEdit,
productEdit;
productEdit,
settingEdit;


public EditCommand(TradeShop instance, CommandPass command) {
Expand All @@ -82,7 +88,87 @@ public void edit() {
mainMenu.setFiller(new ItemStack(Material.LIGHT_GRAY_STAINED_GLASS_PANE, 1));

// ShopUser edit menu, currently can only change/remove. Adding only available through commands
mainMenu.addElement(new StaticGuiElement('a', new ItemStack(Material.PLAYER_HEAD), click -> {
mainMenu.addElement(editUserMenu('a'));

mainMenu.addElement(editCostMenu('b'));

mainMenu.addElement(editProductMenu('c'));

mainMenu.addElement(editSettingsMenu('d'));

mainMenu.show(pSender);
}

private GuiElement editSettingsMenu(char slotChar) {
return new StaticGuiElement(slotChar, new ItemStack(Material.CRAFTING_TABLE), click -> {
Map<ShopSettingKeys, ObjectHolder<?>> changedSettings = new HashMap<>();

settingEdit = new InventoryGui(plugin, "Edit Shop Settings", SETTING_LAYOUT);
GuiElementGroup viewGroup = new GuiElementGroup('g'),
changeGroup = new GuiElementGroup('h');

shop.getShopSettings().forEach((key, value) -> {
viewGroup.addElement(new StaticGuiElement('e', key.getDisplayItem(), key.makeReadable(), value.toString()));

ObjectHolder<?> state = shop.getShopSetting(key);
if (!(state == null || state.getObject() == null)) {
if (state.isBoolean()) {
if (key.isUserEditable(shop.getShopType())) {
changeGroup.addElement(new GuiStateElement('e',
state.asBoolean().toString(),
new GuiStateElement.State(change -> {
if (!state.asBoolean())
changedSettings.put(key, new ObjectHolder<>(true));
else
changedSettings.remove(key);
},
"true",
getBooleanItem(true),
key.makeReadable(),
"State: True"
),
new GuiStateElement.State(change -> {
if (state.asBoolean())
changedSettings.put(key, new ObjectHolder<>(false));
else
changedSettings.remove(key);
},
"false",
getBooleanItem(false),
key.makeReadable(),
"State: False"
)));
} else {
changeGroup.addElement(new StaticGuiElement('e', getBooleanItem(state.asBoolean()), key.makeReadable(), value.toString()));
}


} // Add else-if later if other type settings are added...
}
});

// Add Cancel Button - Cancels changes and Goes to previous screen
settingEdit.addElement(getBackButton(true));

// Add Save Button - Saves and Goes to previous menu
settingEdit.addElement(new StaticGuiElement('s', new ItemStack(Material.ANVIL), click3 -> {
shop.setShopSettings(changedSettings);
shop.saveShop(changedSettings.size() > 0);
InventoryGui.goBack(pSender);
return true;
}, "Save Changes"));

settingEdit.addElement(new StaticGuiElement('a', new ItemStack(Material.YELLOW_STAINED_GLASS_PANE), " "));

settingEdit.addElements(getNextButton(), getPrevButton(), viewGroup, changeGroup);

settingEdit.show(pSender);
return true;
}, colorize("&eEdit Shop Settings"));
}

private GuiElement editUserMenu(char slotChar) {
return new StaticGuiElement(slotChar, new ItemStack(Material.PLAYER_HEAD), click -> {
userEdit = new InventoryGui(plugin, "Edit Users", EDIT_LAYOUT);
Set<ShopUser> shopUsers = new HashSet<>();
GuiElementGroup userGroup = new GuiElementGroup('g');
Expand Down Expand Up @@ -155,9 +241,11 @@ public void edit() {
userEdit.addElement(userGroup);
userEdit.show(pSender);
return true;
}, "Edit Shop Users"));
}, colorize("&eEdit Shop Users"));
}

mainMenu.addElement(new StaticGuiElement('b', new ItemStack(Material.GOLD_NUGGET), click -> {
private GuiElement editCostMenu(char slotChar) {
return new StaticGuiElement(slotChar, new ItemStack(Material.GOLD_NUGGET), click -> {
costEdit = new InventoryGui(plugin, "Edit Costs", EDIT_LAYOUT);
if (costItems.isEmpty()) {
for (ShopItemStack item : shop.getSideList(ShopItemSide.COST)) {
Expand Down Expand Up @@ -196,9 +284,11 @@ public void edit() {
costEdit.addElement(costGroup);
costEdit.show(pSender);
return true;
}, "Edit Shop Costs"));
}, colorize("&eEdit Shop Costs"));
}

mainMenu.addElement(new StaticGuiElement('c', new ItemStack(Material.GRASS_BLOCK), click -> {
private GuiElement editProductMenu(char slotChar) {
return new StaticGuiElement(slotChar, new ItemStack(Material.GRASS_BLOCK), click -> {
productEdit = new InventoryGui(plugin, "Edit Products", EDIT_LAYOUT);
if (productItems.isEmpty()) {
for (ShopItemStack item : shop.getSideList(ShopItemSide.PRODUCT)) {
Expand Down Expand Up @@ -237,8 +327,6 @@ public void edit() {
productEdit.addElement(productGroup);
productEdit.show(pSender);
return true;
}, "Edit Shop Products"));

mainMenu.show(pSender);
}, colorize("&eEdit Shop Products"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ public class GUICommand extends CommandRunner {
//------------------------------------------------------------------------------------------------------------------


protected final String[] MENU_LAYOUT = {"a b c"},
protected final String[] MENU_LAYOUT = {"ad bc"},
EDIT_LAYOUT = {"aggggggga", "ap c s na"},
ITEM_LAYOUT = {"u ggggggg", "j hhhhhhh", "ap cbs na"},
SETTING_LAYOUT = {"ggggggggg", "hhhhhhhhh", "ap cbs na"},
WHAT_MENU = {"141125333", "1qqq2eee3", "11p123n33"};
protected List<ShopItemStack> costItems = new ArrayList<>(),
productItems = new ArrayList<>();
Expand Down Expand Up @@ -99,11 +100,11 @@ protected StaticGuiElement itemSettingMenu(int index, ShopItemSide side, boolean
GuiElementGroup itemGroup = new GuiElementGroup('g'),
settingGroup = new GuiElementGroup('h');

// Cancel and Back
itemEdit.addElement(getBackButton(true));
// Add Cancel button when editable and Back button when not - Goes to previous screen without saving changes
itemEdit.addElement(getBackButton(editable));

// Save and Back
itemEdit.addElement(new StaticGuiElement('s', new ItemStack(Material.ANVIL), click3 -> {
// Add Save button only when editable - Saves and Goes to previous screen
if (editable) itemEdit.addElement(new StaticGuiElement('s', new ItemStack(Material.ANVIL), click3 -> {
(side.equals(ShopItemSide.COST) ? costItems : productItems).set(index, item);
InventoryGui.goBack(pSender);
return true;
Expand Down
26 changes: 21 additions & 5 deletions src/main/java/org/shanerx/tradeshop/data/config/ConfigManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.shanerx.tradeshop.TradeShop;
import org.shanerx.tradeshop.shop.ShopSettingKeys;
import org.shanerx.tradeshop.shop.ShopType;

import java.io.File;
import java.io.FileOutputStream;
Expand Down Expand Up @@ -113,11 +115,25 @@ public void reload() {

save(hasUpgraded.contains(true));

PLUGIN.setSkipHopperProtection(
Setting.BITRADESHOP_HOPPER_EXPORT.getBoolean() &&
Setting.BITRADESHOP_HOPPER_IMPORT.getBoolean() &&
Setting.TRADESHOP_HOPPER_IMPORT.getBoolean() &&
Setting.TRADESHOP_HOPPER_EXPORT.getBoolean());
updateSkipHoppers();
}

public void updateSkipHoppers() {
try {
Set<Boolean> skipCheck = new HashSet<>();
skipCheck.add(ShopSettingKeys.HOPPER_EXPORT.getDefaultValue(ShopType.TRADE).asBoolean());
skipCheck.add(ShopSettingKeys.HOPPER_IMPORT.getDefaultValue(ShopType.TRADE).asBoolean());
skipCheck.add(ShopSettingKeys.HOPPER_EXPORT.getDefaultValue(ShopType.BITRADE).asBoolean());
skipCheck.add(ShopSettingKeys.HOPPER_IMPORT.getDefaultValue(ShopType.BITRADE).asBoolean());
skipCheck.add(!ShopSettingKeys.HOPPER_EXPORT.isUserEditable(ShopType.TRADE));
skipCheck.add(!ShopSettingKeys.HOPPER_IMPORT.isUserEditable(ShopType.TRADE));
skipCheck.add(!ShopSettingKeys.HOPPER_EXPORT.isUserEditable(ShopType.BITRADE));
skipCheck.add(!ShopSettingKeys.HOPPER_IMPORT.isUserEditable(ShopType.BITRADE));
skipCheck.remove(true);

PLUGIN.setSkipHopperProtection(!skipCheck.contains(false));
} catch (NullPointerException ignored) {
}
}

public boolean setDefaults() {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/shanerx/tradeshop/data/config/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public final void sendItemMultiLineMessage(Player player, Map<Variable, List<Ite
boolean isJson = getString().startsWith("#json ");
String message = getPrefixed().replaceFirst("#json ", "");

Debug debug = new Utils().debugger;
Debug debug = new Utils().PLUGIN.getDebugger();

for (Map.Entry<Variable, List<ItemStack>> entry : itemsToFill.entrySet()) {
Pattern pattern = Pattern.compile(MULTILINEREGEX.replace("&V&", entry.getKey().toString()));
Expand Down Expand Up @@ -340,7 +340,7 @@ public final void sendUserEditMultiLineMessage(Player player, Map<Variable, Map<
boolean isJson = getString().startsWith("#json ");
String message = getPrefixed().replaceFirst("#json ", "");

Debug debug = new Utils().debugger;
Debug debug = new Utils().PLUGIN.getDebugger();

for (Map.Entry<Variable, Map<String, String>> entry : valuesToFill.entrySet()) {
Pattern pattern = Pattern.compile(MULTILINEREGEX.replace("&V&", entry.getKey().toString()));
Expand Down
Loading

0 comments on commit 28f2c1b

Please sign in to comment.