Skip to content

Commit

Permalink
Refactored ScenarioManager.getScenario().
Browse files Browse the repository at this point in the history
  • Loading branch information
PdeBot committed Jan 20, 2021
1 parent 2ba97a1 commit 18a78d7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 28 deletions.
22 changes: 7 additions & 15 deletions src/main/java/com/gmail/val59000mc/listeners/ItemsListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -465,17 +465,11 @@ private void handleScenarioInventory(InventoryClickEvent e){
return;
}

// Clicked scenario
Scenario scenario = scenarioManager.getScenario(meta.getDisplayName());

// Clicked item is not a scenario item
if (scenario == null){
return;
}

// Send scenario info
player.sendMessage(Lang.SCENARIO_GLOBAL_DESCRIPTION_HEADER.replace("%scenario%", scenario.getInfo().getName()));
scenario.getInfo().getDescription().forEach(s -> player.sendMessage(Lang.SCENARIO_GLOBAL_DESCRIPTION_PREFIX + s));
scenarioManager.getScenarioByName(meta.getDisplayName()).ifPresent(sce -> {
player.sendMessage(Lang.SCENARIO_GLOBAL_DESCRIPTION_HEADER.replace("%scenario%", sce.getInfo().getName()));
sce.getInfo().getDescription().forEach(s -> player.sendMessage(Lang.SCENARIO_GLOBAL_DESCRIPTION_PREFIX + s));
});
}else if (editInventory){
// Handle back item
if (item.getItemMeta().getDisplayName().equals(Lang.SCENARIO_GLOBAL_ITEM_BACK)) {
Expand All @@ -500,17 +494,15 @@ private void handleScenarioInventory(InventoryClickEvent e){
return;
}

// Clicked scenario
Scenario scenario = scenarioManager.getScenario(meta.getDisplayName());

// toggle scenario
scenarioManager.toggleScenario(scenario);
scenarioManager.getScenarioByName(meta.getDisplayName())
.ifPresent(scenarioManager::toggleScenario);

// Open edit inventory
player.openInventory(scenarioManager.getScenarioEditInventory(uhcPlayer.getBrowsingPage()));
}else if (voteInventory){
// Clicked scenario
Scenario scenario = scenarioManager.getScenario(meta.getDisplayName());
Scenario scenario = scenarioManager.getScenarioByName(meta.getDisplayName()).orElse(null);

// toggle scenario
if (uhcPlayer.getScenarioVotes().contains(scenario)){
Expand Down
6 changes: 0 additions & 6 deletions src/main/java/com/gmail/val59000mc/scenarios/Scenario.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,6 @@ public Class<? extends ScenarioListener> getListener() {
return listener;
}

public boolean equals(String name){
return name.contains(getInfo().getName()) ||
name.replace(" ", "").toLowerCase().equals(key) ||
name.replace(" ", "").toLowerCase().equals(key.replace("_", ""));
}

public ItemStack getScenarioItem() {
ItemStack item = new ItemStack(material);
ItemMeta meta = item.getItemMeta();
Expand Down
22 changes: 15 additions & 7 deletions src/main/java/com/gmail/val59000mc/scenarios/ScenarioManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,23 @@ public boolean toggleScenario(Scenario scenario){
* Used to obtain the scenario object matching a certain name.
* @param name Name of the scenario to be searched.
* @return Returns a scenario object matching the name, or null when not found.
* @deprecated Replaced by {@link #getScenarioByName(String)}
*/
@Nullable
public Scenario getScenario(String name){
for (Scenario scenario : registeredScenarios){
if (scenario.equals(name)){
return scenario;
}
}
return null;
@Deprecated
public Scenario getScenario(String name) {
return getScenarioByName(name).orElse(null);
}

/**
* Used to obtain the scenario object matching a certain name.
* @param name Name of the scenario to be searched.
* @return Returns a scenario object matching the name, or null when not found.
*/
public Optional<Scenario> getScenarioByName(String name) {
return registeredScenarios.stream()
.filter(s -> name.contains(s.getInfo().getName()))
.findFirst();
}

/**
Expand Down

0 comments on commit 18a78d7

Please sign in to comment.