Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,12 @@ public void checkDirectlyDefinedRewardFiles() {

for (SubDirectlyDefinedReward direct : getSubDirectlyDefinedRewards()) {
directlyDefinedPaths.add(direct.getFullPath().replace(".", "_"));
}
for (Reward rewardFile : getRewards()) {
if (ArrayUtils.containsIgnoreCase(directlyDefinedPaths, rewardFile.getName())) {
plugin.getLogger().warning("Found reward file conflict: " + rewardFile.getName()
+ ", recommend deleting or renaming file to prevent issues");
}

for (Reward rewardFile : getRewards()) {
if (ArrayUtils.containsIgnoreCase(directlyDefinedPaths, rewardFile.getName())) {
plugin.getLogger().warning("Found reward file conflict: " + rewardFile.getName()
+ ", recommend deleting or renaming file to prevent issues");
}
}
}
Expand Down Expand Up @@ -344,13 +344,12 @@ public Reward getReward(String reward) {
return direct.getReward();
}
}

for (SubDirectlyDefinedReward direct : getSubDirectlyDefinedRewards()) {
if (direct.getFullPath().equalsIgnoreCase(reward)
|| direct.getFullPath().equalsIgnoreCase(reward.replaceAll("_", "."))) {
plugin.debug("Using subdirectlydefined reward for: " + reward);
return direct.getReward();
}

for (SubDirectlyDefinedReward direct : getSubDirectlyDefinedRewards()) {
if (matchesSubDirectlyDefined(direct, reward)) {
plugin.debug("Using subdirectlydefined reward for: " + reward);
return direct.getReward();
}
}

for (Reward rewardFile : getRewards()) {
Expand Down Expand Up @@ -450,13 +449,12 @@ public List<Reward> getRewards() {
return rewards;
}

public SubDirectlyDefinedReward getSubDirectlyDefined(String path) {
for (SubDirectlyDefinedReward direct : getSubDirectlyDefinedRewards()) {
if (direct.getFullPath().equalsIgnoreCase(path)
|| direct.getFullPath().equalsIgnoreCase(path.replaceAll("_", "."))) {
return direct;
}
}
public SubDirectlyDefinedReward getSubDirectlyDefined(String path) {
for (SubDirectlyDefinedReward direct : getSubDirectlyDefinedRewards()) {
if (matchesSubDirectlyDefined(direct, path)) {
return direct;
}
}
return null;
}

Expand Down Expand Up @@ -592,15 +590,20 @@ public boolean hasDirectRewardHandle(String reward) {
return true;
}
}

for (SubDirectlyDefinedReward direct : getSubDirectlyDefinedRewards()) {
if (direct.getFullPath().equalsIgnoreCase(reward)
|| direct.getFullPath().equalsIgnoreCase(reward.replaceAll("_", "."))) {
return true;
}
}
return false;
}

for (SubDirectlyDefinedReward direct : getSubDirectlyDefinedRewards()) {
if (matchesSubDirectlyDefined(direct, reward)) {
return true;
}
}
return false;
}

private boolean matchesSubDirectlyDefined(SubDirectlyDefinedReward direct, String reward) {
return direct.getFullPath().equalsIgnoreCase(reward)
|| direct.getFullPath().replace(".", "_").equalsIgnoreCase(reward)
|| direct.getFullPath().equalsIgnoreCase(reward.replaceAll("_", "."));
}

public boolean hasRewards(FileConfiguration data, String path) {
if (data.isList(path)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.bencodez.advancedcore.tests.rewards;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.File;
import java.util.logging.Logger;

import org.bukkit.configuration.ConfigurationSection;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import com.bencodez.advancedcore.AdvancedCorePlugin;
import com.bencodez.advancedcore.api.rewards.DirectlyDefinedReward;
import com.bencodez.advancedcore.api.rewards.RewardHandler;
import com.bencodez.advancedcore.api.rewards.SubDirectlyDefinedReward;

public class RewardHandlerTest {

@TempDir
File tempDir;

private RewardHandler rewardHandler;

@BeforeEach
public void setUp() {
AdvancedCorePlugin plugin = mock(AdvancedCorePlugin.class);
when(plugin.getDataFolder()).thenReturn(tempDir);
when(plugin.getLogger()).thenReturn(mock(Logger.class));

rewardHandler = new RewardHandler(plugin);
}

@AfterEach
public void tearDown() {
rewardHandler.getDelayedTimer().shutdownNow();
}

@Test
public void getSubDirectlyDefinedMatchesFileStylePathWithUnderscoresInParentName() {
rewardHandler.getSubDirectlyDefinedRewards()
.add(new SubDirectlyDefinedReward(directlyDefinedReward("Test_Rewards_Name"), "Rewards"));

assertNotNull(rewardHandler.getSubDirectlyDefined("Test_Rewards_Name_Rewards"));
}

@Test
public void hasDirectRewardHandleMatchesSubRewardWithUnderscoresInParentName() {
rewardHandler.getSubDirectlyDefinedRewards()
.add(new SubDirectlyDefinedReward(directlyDefinedReward("Test_Rewards_Name"), "Rewards"));

assertTrue(rewardHandler.hasDirectRewardHandle("Test_Rewards_Name_Rewards"));
}

private DirectlyDefinedReward directlyDefinedReward(String path) {
return new DirectlyDefinedReward(path) {

@Override
public void createSection(String key) {
}

@Override
public ConfigurationSection getFileData() {
return null;
}

@Override
public void save() {
}

@Override
public void setData(String path, Object value) {
}
};
}
}
Loading