From b5603fd1c3d9752ba75b38cc6c0fe72f02f79db3 Mon Sep 17 00:00:00 2001 From: TiagoFar78 Date: Tue, 23 Jul 2024 21:54:55 +0100 Subject: [PATCH] made sort mission size configurable and fixed small bug --- TestServer/plugins/TF_PrisonEscape/config.yml | 1 + .../prisonescape/managers/ConfigManager.java | 17 +++++- .../managers/MessageLanguageManager.java | 6 +- .../prisonescape/missions/SortMission.java | 56 ++++++++++++++----- src/main/resources/config.yml | 1 + 5 files changed, 62 insertions(+), 19 deletions(-) diff --git a/TestServer/plugins/TF_PrisonEscape/config.yml b/TestServer/plugins/TF_PrisonEscape/config.yml index 4a4243e..02a1701 100644 --- a/TestServer/plugins/TF_PrisonEscape/config.yml +++ b/TestServer/plugins/TF_PrisonEscape/config.yml @@ -40,6 +40,7 @@ MissionsPerDay: 3 MissionMoneyReward: 10 DifferencesAmount: 3 SequenceSize: 5 +SortSize: 5 # must be below 10 and bigger than 2 AvailableLanguages: - "english" DefaultLanguage: "english" diff --git a/src/main/java/net/tiagofar78/prisonescape/managers/ConfigManager.java b/src/main/java/net/tiagofar78/prisonescape/managers/ConfigManager.java index a877d81..7def609 100644 --- a/src/main/java/net/tiagofar78/prisonescape/managers/ConfigManager.java +++ b/src/main/java/net/tiagofar78/prisonescape/managers/ConfigManager.java @@ -4,6 +4,7 @@ import net.tiagofar78.prisonescape.dataobjects.ItemProbability; import net.tiagofar78.prisonescape.game.prisonbuilding.regions.Region; import net.tiagofar78.prisonescape.game.prisonbuilding.regions.SquaredRegion; +import net.tiagofar78.prisonescape.missions.SortMission; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -67,6 +68,7 @@ public static ConfigManager getInstance() { private int _missionMoneyReward; private int _differencesAmount; private int _sequenceSize; + private int _sortSize; private List _availableLanguages; private String _defaultLanguage; @@ -160,6 +162,7 @@ public ConfigManager() { _missionMoneyReward = config.getInt("MissionMoneyReward"); _differencesAmount = config.getInt("DifferencesAmount"); _sequenceSize = config.getInt("SequenceSize"); + _sortSize = config.getInt("SortSize"); _availableLanguages = config.getStringList("AvailableLanguages"); _defaultLanguage = config.getString("DefaultLanguage"); @@ -530,6 +533,10 @@ public int getSequenceSize() { return _sequenceSize; } + public int getSortSize() { + return _sortSize; + } + public List getAvailableLanguages() { return new ArrayList<>(_availableLanguages); } @@ -780,7 +787,7 @@ private List> createStringListListCopy(List> strings) // ######################################## private boolean isValid() { - return areMissionsRegionsValid(); + return areMissionsRegionsValid() && isSortMissionSizeValid(); } private boolean areMissionsRegionsValid() { @@ -800,4 +807,12 @@ private boolean areMissionsRegionsValid() { return true; } + private boolean isSortMissionSizeValid() { + if (2 <= _sortSize && _sortSize <= SortMission.ITEMS.length) { + throw new IllegalArgumentException("Sort size must be between 3 and " + SortMission.ITEMS.length); + } + + return true; + } + } diff --git a/src/main/java/net/tiagofar78/prisonescape/managers/MessageLanguageManager.java b/src/main/java/net/tiagofar78/prisonescape/managers/MessageLanguageManager.java index fc7479a..de34720 100644 --- a/src/main/java/net/tiagofar78/prisonescape/managers/MessageLanguageManager.java +++ b/src/main/java/net/tiagofar78/prisonescape/managers/MessageLanguageManager.java @@ -664,8 +664,8 @@ public String getDifferencesTitle(int current, int total) { return _differencesTitle.replace("{CURRENT}", sCurrent).replace("{TOTAL}", sTotal); } - public String getSortTitle(String correct) { - return _sortTitle.replace("{CORRECT}", correct); + public String getSortTitle(int correct) { + return _sortTitle.replace("{CORRECT}", Integer.toString(correct)); } public String getSequenceTitle() { @@ -673,7 +673,7 @@ public String getSequenceTitle() { } // ######################################## -// # Chat # +// # Chat # // ######################################## public String getGeneralMessage(String playerName, String message) { diff --git a/src/main/java/net/tiagofar78/prisonescape/missions/SortMission.java b/src/main/java/net/tiagofar78/prisonescape/missions/SortMission.java index c4ee3a5..10dbb8c 100644 --- a/src/main/java/net/tiagofar78/prisonescape/missions/SortMission.java +++ b/src/main/java/net/tiagofar78/prisonescape/missions/SortMission.java @@ -2,6 +2,7 @@ import net.tiagofar78.prisonescape.game.Guard; import net.tiagofar78.prisonescape.game.PEPlayer; +import net.tiagofar78.prisonescape.managers.ConfigManager; import net.tiagofar78.prisonescape.managers.MessageLanguageManager; import net.tiagofar78.prisonescape.menus.ClickReturnAction; import net.tiagofar78.prisonescape.menus.Clickable; @@ -18,16 +19,18 @@ public class SortMission extends Mission implements Clickable { - private static final Material[] ITEMS = { + public static final Material[] ITEMS = { Material.DANDELION, Material.POPPY, Material.BLUE_ORCHID, Material.ALLIUM, Material.AZURE_BLUET, Material.WHITE_TULIP, - Material.ORANGE_TULIP}; + Material.ORANGE_TULIP, + Material.OXEYE_DAISY, + Material.CORNFLOWER}; private static final int TEMP_SLOT = 9 * 1 + 4; - private static final int ITEMS_FIRST_SLOT = 9 * 3 + 4 - ITEMS.length / 2; + private static final int CENTER_SLOT = 9 * 3 + 4; private Guard _guard; private int _missionIndex; @@ -46,14 +49,14 @@ public void start(Guard guard, int missionIndex) { _guard = guard; _missionIndex = missionIndex; - _game = new SortGame(ITEMS.length); + _game = new SortGame(ConfigManager.getInstance().getSortSize()); _guard.openMenu(this); } @Override public Inventory toInventory(MessageLanguageManager messages) { - String title = messages.getSortTitle(""); + String title = messages.getSortTitle(_game.countCorrect()); int lines = 5; Inventory inv = Bukkit.createInventory(null, lines * 9, title); @@ -68,10 +71,11 @@ private void buildBaseInventory(Inventory inv, int lines) { inv.setItem(i, glass); } + int size = ConfigManager.getInstance().getSortSize(); inv.setItem(TEMP_SLOT, null); - for (int i = 0; i < ITEMS.length; i++) { + for (int i = 0; i < size; i++) { ItemStack item = new ItemStack(ITEMS[i]); - inv.setItem(ITEMS_FIRST_SLOT + i, item); + inv.setItem(toInventorySlot(i), item); } } @@ -92,8 +96,8 @@ public ClickReturnAction click(PEPlayer player, int slot, boolean isPlayerInv, C return ClickReturnAction.NOTHING; } - int index = slot - ITEMS_FIRST_SLOT; - if (index < 0 || index >= ITEMS.length) { + int index = toGameIndex(slot); + if (index == -1) { return ClickReturnAction.NOTHING; } @@ -116,7 +120,7 @@ public ClickReturnAction click(PEPlayer player, int slot, boolean isPlayerInv, C int correctPositions = _game.countCorrect(); MessageLanguageManager messages = MessageLanguageManager.getInstanceByPlayer(player.getName()); - player.updateViewTitle(messages.getSortTitle(Integer.toString(correctPositions))); + player.updateViewTitle(messages.getSortTitle(correctPositions)); _swapFunction = (inv) -> swapBetweenSlots(inv); player.updateView(); _tempItemIndex = -1; @@ -131,19 +135,41 @@ public void updateInventory(Inventory inv, PEPlayer player) { private void swapWithTemp(Inventory inv) { ItemStack tempItem = inv.getItem(TEMP_SLOT); - ItemStack item1 = inv.getItem(ITEMS_FIRST_SLOT + _tempItemIndex); + ItemStack item1 = inv.getItem(toInventorySlot(_tempItemIndex)); - inv.setItem(ITEMS_FIRST_SLOT + _tempItemIndex, tempItem); + inv.setItem(toInventorySlot(_tempItemIndex), tempItem); inv.setItem(TEMP_SLOT, item1); } private void swapBetweenSlots(Inventory inv) { ItemStack item1 = inv.getItem(TEMP_SLOT); - ItemStack item2 = inv.getItem(ITEMS_FIRST_SLOT + _itemToSwapIndex); + ItemStack item2 = inv.getItem(toInventorySlot(_itemToSwapIndex)); inv.setItem(TEMP_SLOT, null); - inv.setItem(ITEMS_FIRST_SLOT + _tempItemIndex, item2); - inv.setItem(ITEMS_FIRST_SLOT + _itemToSwapIndex, item1); + inv.setItem(toInventorySlot(_tempItemIndex), item2); + inv.setItem(toInventorySlot(_itemToSwapIndex), item1); + } + + private int toInventorySlot(int index) { + int size = ConfigManager.getInstance().getSortSize(); + int slot = CENTER_SLOT - size / 2 + index; + + return size % 2 == 0 && index >= size / 2 ? slot + 1 : slot; + } + + private int toGameIndex(int slot) { + int size = ConfigManager.getInstance().getSortSize(); + if (size % 2 == 0) { + if (slot == CENTER_SLOT) { + return -1; + } else if (slot > CENTER_SLOT) { + slot -= 1; + } + } + + int index = slot - (CENTER_SLOT - size / 2); + + return index < 0 || index >= size ? -1 : index; } private class SortGame { diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 8a8f749..8b17712 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -42,6 +42,7 @@ MissionsPerDay: 3 MissionMoneyReward: 10 DifferencesAmount: 3 SequenceSize: 5 +SortSize: 5 # must be below 10 and bigger than 2 AvailableLanguages: - "english" DefaultLanguage: "english"