Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sort misssion #330

Merged
merged 1 commit into from
Jul 24, 2024
Merged
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
1 change: 1 addition & 0 deletions TestServer/plugins/TF_PrisonEscape/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -67,6 +68,7 @@ public static ConfigManager getInstance() {
private int _missionMoneyReward;
private int _differencesAmount;
private int _sequenceSize;
private int _sortSize;

private List<String> _availableLanguages;
private String _defaultLanguage;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -530,6 +533,10 @@ public int getSequenceSize() {
return _sequenceSize;
}

public int getSortSize() {
return _sortSize;
}

public List<String> getAvailableLanguages() {
return new ArrayList<>(_availableLanguages);
}
Expand Down Expand Up @@ -780,7 +787,7 @@ private List<List<String>> createStringListListCopy(List<List<String>> strings)
// ########################################

private boolean isValid() {
return areMissionsRegionsValid();
return areMissionsRegionsValid() && isSortMissionSizeValid();
}

private boolean areMissionsRegionsValid() {
Expand All @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -664,16 +664,16 @@ 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() {
return _sequenceTitle;
}

// ########################################
// # Chat #
// # Chat #
// ########################################

public String getGeneralMessage(String playerName, String message) {
Expand Down
56 changes: 41 additions & 15 deletions src/main/java/net/tiagofar78/prisonescape/missions/SortMission.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);

Expand All @@ -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);
}
}

Expand All @@ -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;
}

Expand All @@ -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;
Expand All @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading