Skip to content

Commit

Permalink
Merge pull request #21 from r3back/develop
Browse files Browse the repository at this point in the history
Fixed Async task in TheBank, Fixed TheCrafting pagination
  • Loading branch information
r3back authored Jun 25, 2023
2 parents f808f8a + 52bf378 commit 280fa97
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.util.List;
import java.util.Optional;

@Scheduled(rate = MinecraftTimeEquivalent.SECOND, async = true)
@Scheduled(rate = MinecraftTimeEquivalent.SECOND)
public final class BankInterestHandler implements Runnable {
private @Inject Box box;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public Optional<TrxResponse> handle(final BankData bankData, final BankTransacti

private Optional<TrxResponse> handleSet(final TrxRequest request){
final BankData data = request.getBankData();

final double amount = request.getTransaction().getAmount();

data.setMoney(amount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,14 @@ public final class Inventories extends OkaeriConfig implements DefaultBackground
))
.lockedItem(ItemBuilder.of(XMaterial.GRAY_DYE, 4, 1, "&c???", Collections.singletonList("&7Locked Recipe")).build())
.unlockedItem(ItemBuilder.of(XMaterial.GRAY_DYE, 4, 1, "%crafting_recipe_result_item_displayname%", Arrays.asList("%crafting_recipe_result_item_lore%", "", "&eClick to view recipe")).build())

.categoryItem(ItemBuilder.of(XMaterial.STONE, 4, 1, "&a%category_recipe_displayname% Recipes", Arrays.asList("&7View all of the &a%category_recipe_displayname% Recipes",
"&7that you have unlocked!",
"",
"&7Recipe Unlocked: &e%recipes_percentage_progress%&6%",
"%recipes_progress_actionbar% &e%unlocked_recipes%&6/&e%recipes_total%")).build())
.goBack(ItemBuilder.of(XMaterial.ARROW, 48, 1, "&aGo Back", Arrays.asList("", "&e» &7Click to go back")).build())
.nextPage(ItemBuilder.of(XMaterial.BOOK, 53,1, "&dNext Page", Arrays.asList("", "&e» Click to go next Page!")).build())
.previousPage(ItemBuilder.of(XMaterial.BOOK, 46, 1, "&dPrevious Page", Arrays.asList("", "&e» Click to go to previous Page!")).build())
.build();

private Background getTableBackground() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.qualityplus.crafting.base.gui.book.sub;

import com.qualityplus.assistant.inventory.Item;
import com.qualityplus.assistant.lib.com.cryptomorin.xseries.XMaterial;
import com.qualityplus.assistant.util.StringUtils;
import com.qualityplus.assistant.util.inventory.InventoryUtils;
import com.qualityplus.assistant.util.itemstack.ItemBuilder;
import com.qualityplus.assistant.util.itemstack.ItemStackUtils;
import com.qualityplus.crafting.api.box.Box;
import com.qualityplus.crafting.api.edition.RecipeEdition;
Expand All @@ -18,8 +21,11 @@
import org.bukkit.inventory.Inventory;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -51,7 +57,17 @@ public RecipeBookSubGUI(Box box, UUID uuid, Category category, int page, RecipeE
getCategoryPlaceholders(category),
category));

for(CustomRecipe recipe : category.getRecipes().stream().filter(recipe -> recipe.getPage() == page).collect(Collectors.toList())) {
final List<CustomRecipe> recipes = getRecipesForPage(page);

if (hasNextPage()) {
setItem(getNextPage());
}

if (this.page > 1) {
setItem(getPreviousPage());
}

for(CustomRecipe recipe : recipes) {
if(recipe.hasPermission(Bukkit.getPlayer(uuid)))
inventory.setItem(recipe.getSlot(), ItemStackUtils.makeItem(config.getUnlockedItem(), CraftingPlaceholderUtils.getRecipePlaceholders(recipe), recipe.getResult()));
else
Expand All @@ -60,40 +76,70 @@ public RecipeBookSubGUI(Box box, UUID uuid, Category category, int page, RecipeE
recipeMap.put(recipe.getSlot(), recipe);
}


setItem(config.getGoBack());

setItem(config.getCloseGUI());

return inventory;
}

private boolean hasNextPage() {
return getRecipesForPage(page + 1).stream()
.findAny()
.isPresent();
}

private Item getNextPage() {
return Optional.ofNullable(config.getNextPage())
.orElse(ItemBuilder.of(XMaterial.BOOK, 53,1, "&dNext Page", Arrays.asList("", "&e» Click to go next Page!")).build());
}

private Item getPreviousPage() {
return Optional.ofNullable(config.getPreviousPage())
.orElse(ItemBuilder.of(XMaterial.BOOK, 46, 1, "&dPrevious Page", Arrays.asList("", "&e» Click to go to previous Page!")).build());
}

private List<CustomRecipe> getRecipesForPage(final int page) {
return category.getRecipes().stream()
.filter(recipe -> recipe.getPage() == page)
.collect(Collectors.toList());
}

@Override
public void onInventoryClick(InventoryClickEvent event) {
event.setCancelled(true);

if(!getTarget(event).equals(ClickTarget.INSIDE)) return;
if (!getTarget(event).equals(ClickTarget.INSIDE)) {
return;
}

int slot = event.getSlot();
final int slot = event.getSlot();

Player player = (Player) event.getWhoClicked();
final Player player = (Player) event.getWhoClicked();

if(isItem(slot, config.getCloseGUI())){
if (isItem(slot, config.getCloseGUI())) {
player.closeInventory();
}else if(recipeMap.containsKey(slot)){
} else if(recipeMap.containsKey(slot)) {
CustomRecipe recipe = recipeMap.getOrDefault(slot, null);

if(recipe == null) return;
if (recipe == null) {
return;
}

if(!recipe.hasPermission(Bukkit.getPlayer(uuid))){
if (!recipe.hasPermission(Bukkit.getPlayer(uuid))) {
player.sendMessage(StringUtils.color(box.files().messages().recipeMessages.notUnlockedYet));
return;
}

player.openInventory(new RecipePreviewGUI(box, recipe, edition).getInventory());

}else if(isItem(slot, config.getGoBack())){
} else if(isItem(slot, config.getGoBack())) {
player.openInventory(new RecipeBookMainGUI(box, uuid, edition).getInventory());
} else if(isItem(slot, config.getNextPage()) && hasNextPage()) {
player.openInventory(new RecipeBookSubGUI(box, uuid, category, page + 1, edition).getInventory());
} else if(isItem(slot, config.getPreviousPage()) && page > 1) {
player.openInventory(new RecipeBookSubGUI(box, uuid, category, page - 1, edition).getInventory());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@ public final class RecipeBookSubGUIConfig extends OkaeriConfig implements Simple
private Item unlockedItem;
private Item lockedItem;
private Item goBack;
private Item previousPage;
private Item nextPage;

@Builder
public RecipeBookSubGUIConfig(CommonGUI commonGUI, Item categoryItem, Item unlockedItem, Item lockedItem, Item goBack) {
public RecipeBookSubGUIConfig(CommonGUI commonGUI, Item categoryItem, Item unlockedItem, Item lockedItem, Item goBack,
Item previousPage, Item nextPage) {
this.commonGUI = commonGUI;
this.categoryItem = categoryItem;
this.unlockedItem = unlockedItem;
this.lockedItem = lockedItem;
this.goBack = goBack;
this.previousPage = previousPage;
this.nextPage = nextPage;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ public void targetSky() {
public void targetPlayer() {
this.rangeBest = 9000.0D;
this.rangeTime = System.currentTimeMillis();
Player player = TheDragon.getApi().getUserService().getUsers().get(RandomUtil.randomUpTo(TheDragon.getApi().getUserService().getUsers().size())).getPlayer();
final int randomIndex = Math.max(RandomUtil.randomUpTo(TheDragon.getApi().getUserService().getUsers().size() - 1), 0);
final Player player = TheDragon.getApi().getUserService().getUsers().get(randomIndex).getPlayer();
this.target = player.getLocation();
this.target.add(DragonVelocityUtil.getTrajectory(this.location, this.target).multiply(3.5D));
TheDragon.getApi().getPlugin().getServer().getPluginManager().callEvent(new DragonTargetEvent(player, this, TargetType.PLAYER));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void run() {
.forEach(this::callEvent);
}

private void callEvent(RefreshType type){
private void callEvent(RefreshType type) {
plugin.getServer().getPluginManager().callEvent(new DragonRefreshEvent(type));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

@Getter
public final class TheDragonEntityImpl extends OkaeriConfig implements TheDragonEntity {
private @Exclude EnderDragon enderDragon;
@Exclude
private EnderDragon enderDragon;
private final String displayName;
private final double maxHealth;
private final double chance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,20 @@ private Optional<DragonReward> getRewardByDamage(EventPlayer eventPlayer){

private Optional<DragonReward> getSpecificRewardByDamage(TheDragonEntity entity, EventPlayer eventPlayer){

if(rewards.rewardsPerEachDragon == null) return Optional.empty();
if (this.rewards.rewardsPerEachDragon == null) {
return Optional.empty();
}

final List<DragonReward> dragonRewards = new ArrayList<>(this.rewards.rewardsPerEachDragon.getOrDefault(entity.getId(), Collections.emptyList()));

List<DragonReward> dragonRewards = new ArrayList<>(rewards.rewardsPerEachDragon.getOrDefault(entity.getId(), Collections.emptyList()));
dragonRewards.sort((o1, o2) -> o2.getDamageDone() - o1.getDamageDone());

return dragonRewards.stream()
.filter(dragonReward -> eventPlayer.getDamage() >= dragonReward.getDamageDone())
.findFirst();
}

private void executeCommands(List<String> commands, List<IPlaceholder> placeholders){
private void executeCommands(final List<String> commands, final List<IPlaceholder> placeholders){
commands.forEach(command -> Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), StringUtils.processMulti(command, placeholders)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void refreshRanking(){

this.eventPlayerMap.clear();

int ranking = 1;
int ranking = 0;

for (EventPlayer eventPlayer : eventPlayers) {
this.eventPlayerMap.put(ranking, eventPlayer);
Expand All @@ -71,19 +71,39 @@ private String getRecordMessage(final EventPlayer player){
}

private List<IPlaceholder> getCommonPlaceholders(){
String notFound = messages.gameMessages.forbiddenPlayer;

return PlaceholderBuilder
.create(IntStream.of(0,1,2)
.boxed()
.map(number -> new Placeholder(String.format("the_dragon_player_top_%d_name", number + 1), getByRank(number).map(EventPlayer::getName).orElse(notFound)))
.map(this::getNameByTopNumber)
.collect(Collectors.toList()))
.with(IntStream.of(0,1,2)
.with(IntStream.of(0, 1, 2)
.boxed()
.map(number -> new Placeholder(String.format("the_dragon_player_top_%d_damage", number + 1), getByRank(number).map(EventPlayer::getDamage).orElse(0D)))
.map(this::getDamageByTopNumber)
.collect(Collectors.toList()))
.get();
}

private Placeholder getDamageByTopNumber(final int number) {
final String key = String.format("the_dragon_player_top_%d_damage", number + 1);
final double value = getByRank(number)
.map(EventPlayer::getDamage)
.orElse(0D);

return new Placeholder(key, value);
}

private Placeholder getNameByTopNumber(final int number) {
String notFound = messages.gameMessages.forbiddenPlayer;

final String key = String.format("the_dragon_player_top_%d_name", number + 1);
final String value = getByRank(number)
.map(EventPlayer::getName)
.orElse(notFound);

return new Placeholder(key, value);
}

private String getLastAttacker(){
return Optional.ofNullable(TheDragon.getApi().getUserService().getLast())
.filter(Objects::nonNull)
Expand All @@ -98,13 +118,13 @@ private double getDragonXp(){
.orElse(0D);
}

private Integer getRankByPlayer(EventPlayer eventPlayer) {
private Integer getRankByPlayer(final EventPlayer eventPlayer) {
return eventPlayerMap.entrySet()
.stream()
.filter(entry -> entry.getValue().getName().equals(eventPlayer.getName()))
.map(Map.Entry::getKey)
.findFirst()
.orElse(0);
.orElse(0) + 1;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
import org.bukkit.World;
import org.bukkit.block.Block;

@Getter
@AllArgsConstructor
public final class DragonAltarImpl extends OkaeriConfig implements DragonAltar {
private @Getter final Location location;
private @Getter boolean enderKey;
private final Location location;
private boolean enderKey;

@Override
public void removeStructure() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.qualityplus.dragon.base.game.structure;

import com.qualityplus.assistant.lib.eu.okaeri.configs.annotation.Exclude;
import com.qualityplus.assistant.util.StringUtils;
import com.qualityplus.dragon.TheDragon;
import com.qualityplus.dragon.api.game.structure.type.DragonCrystal;
Expand All @@ -16,8 +17,10 @@

@RequiredArgsConstructor
public final class DragonCrystalImpl extends OkaeriConfig implements DragonCrystal {
@Exclude
private EnderCrystal enderCrystal;
private @Getter final Location location;
@Getter
private final Location location;

@Override
public void removeStructure() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
import org.bukkit.Location;
import org.bukkit.World;

@Getter
@AllArgsConstructor
public final class DragonSpawnImpl extends OkaeriConfig implements DragonSpawn {
private @Getter Location location;
private Location location;

@Override
public void removeStructure() {
Expand Down

0 comments on commit 280fa97

Please sign in to comment.