-
Notifications
You must be signed in to change notification settings - Fork 31
Ajout de la feature de l'ouverture de dimension #888
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
Draft
AxenoDev
wants to merge
1
commit into
ServerOpenMC:master
Choose a base branch
from
AxenoDev:feat/open-dims
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/main/java/fr/openmc/core/features/dimsopener/DimensionConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package fr.openmc.core.features.dimsopener; | ||
|
|
||
| import org.bukkit.inventory.ItemStack; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public record DimensionConfig(String dimensionKey, String name, ItemStack icon, List<DimensionStep> steps) { | ||
| @Override | ||
| public @NotNull String toString() { | ||
| return "DimensionConfig{" + | ||
| "dimensionKey='" + dimensionKey + '\'' + | ||
| ", name='" + name + '\'' + | ||
| ", icon=" + icon + | ||
| ", steps=" + steps + | ||
| '}'; | ||
| } | ||
| } |
4 changes: 4 additions & 0 deletions
4
src/main/java/fr/openmc/core/features/dimsopener/DimensionStep.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| package fr.openmc.core.features.dimsopener; | ||
|
|
||
| public record DimensionStep(String itemType, int count, int nextStepDelay) { | ||
| } |
179 changes: 179 additions & 0 deletions
179
src/main/java/fr/openmc/core/features/dimsopener/DimsOpenerManager.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| package fr.openmc.core.features.dimsopener; | ||
|
|
||
| import com.j256.ormlite.dao.Dao; | ||
| import com.j256.ormlite.dao.DaoManager; | ||
| import com.j256.ormlite.support.ConnectionSource; | ||
| import com.j256.ormlite.table.TableUtils; | ||
| import fr.openmc.core.OMCPlugin; | ||
| import fr.openmc.core.features.dimsopener.event.DimensionUnlockedEvent; | ||
| import fr.openmc.core.features.dimsopener.model.DimensionProgress; | ||
| import fr.openmc.core.items.CustomItemRegistry; | ||
| import org.bukkit.Bukkit; | ||
| import org.bukkit.Material; | ||
| import org.bukkit.configuration.ConfigurationSection; | ||
| import org.bukkit.configuration.file.YamlConfiguration; | ||
| import org.bukkit.inventory.ItemStack; | ||
|
|
||
| import java.io.File; | ||
| import java.sql.SQLException; | ||
| import java.util.*; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| public class DimsOpenerManager { | ||
|
|
||
| private static final Map<String, DimensionConfig> dimensionConfigs = new HashMap<>(); | ||
| private static final Map<String, DimensionProgress> progressCache = new HashMap<>(); | ||
| private static Dao<DimensionProgress, Integer> dao; | ||
|
|
||
| public static void initDB(ConnectionSource connectionSource) throws SQLException { | ||
| TableUtils.createTableIfNotExists(connectionSource, DimensionProgress.class); | ||
| dao = DaoManager.createDao(connectionSource, DimensionProgress.class); | ||
|
|
||
| List<DimensionProgress> allProgress = dao.queryForAll(); | ||
| allProgress.forEach(progress -> progressCache.put(progress.getDimensionKey(), progress)); | ||
| } | ||
|
|
||
| public static void initConfig() { | ||
| File configFile = new File(OMCPlugin.getInstance().getDataFolder(), "data/dimsopener.yml"); | ||
|
|
||
| if (!configFile.exists()) { | ||
| OMCPlugin.getInstance().saveResource("data/dimsopener.yml", false); | ||
| } | ||
|
|
||
| YamlConfiguration config = YamlConfiguration.loadConfiguration(configFile); | ||
| ConfigurationSection dimsSection = config.getConfigurationSection("dims"); | ||
|
|
||
| if (dimsSection == null) return; | ||
|
|
||
| for (String dimKey : dimsSection.getKeys(false)) { | ||
| ConfigurationSection dimSection = dimsSection.getConfigurationSection(dimKey); | ||
| if (dimSection == null) continue; | ||
|
|
||
| List<?> stepsList = dimSection.getList("steps"); | ||
| if (stepsList == null) continue; | ||
|
|
||
| List<DimensionStep> steps = new ArrayList<>(); | ||
|
|
||
| for (Object stepObj : stepsList) { | ||
| if (!(stepObj instanceof Map<?, ?> stepMap)) continue; | ||
|
|
||
| System.out.println("Processing step map: " + stepMap); | ||
|
|
||
| for (Map.Entry<?, ?> entry : stepMap.entrySet()) { | ||
| String itemType = entry.getKey().toString(); | ||
| if (!(entry.getValue() instanceof Map<?, ?> itemData)) continue; | ||
|
|
||
| int requiredAmount = itemData.containsKey("count") ? ((Number) itemData.get("count")).intValue() : 0; | ||
| int nextStepDelay = itemData.containsKey("next_step_delay") ? ((Number) itemData.get("next_step_delay")).intValue() : 0; | ||
|
|
||
| if (requiredAmount == 0 && nextStepDelay == 0) continue; | ||
|
|
||
| steps.add(new DimensionStep(itemType, requiredAmount, nextStepDelay)); | ||
| } | ||
| } | ||
|
|
||
| String dimensionName = dimSection.getString("name", dimKey); | ||
| String dimensionIconStr = dimSection.getString("icon", "minecraft:grass_block"); | ||
| ItemStack dimensionIcon; | ||
|
|
||
| System.out.println(dimensionIconStr.split(":")[0]); | ||
|
|
||
| if (dimensionIconStr.split(":")[0].equals("minecraft")) { | ||
| dimensionIcon = new ItemStack(Material.matchMaterial(dimensionIconStr.split(":")[1]) != null ? Material.matchMaterial(dimensionIconStr.split(":")[1]) : Material.GRASS_BLOCK); | ||
| } else { | ||
| System.out.println("Loading custom item for dimension icon: " + dimensionIconStr); | ||
| dimensionIcon = CustomItemRegistry.getByName(dimensionIconStr).getBest(); | ||
| } | ||
|
|
||
| System.out.println("Loaded dimension: " + dimKey + " with name: " + dimensionName + " and icon: " + dimensionIcon); | ||
|
|
||
| dimensionConfigs.put(dimKey, new DimensionConfig(dimKey, dimensionName, dimensionIcon, steps)); | ||
|
|
||
| if (!progressCache.containsKey(dimKey)) { | ||
| DimensionProgress progress = new DimensionProgress(dimKey); | ||
| progressCache.put(dimKey, progress); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static DimensionProgress getProgress(String dimensionKey) { | ||
| return progressCache.get(dimensionKey); | ||
| } | ||
|
|
||
| public static DimensionConfig getConfig(String dimensionKey) { | ||
| return dimensionConfigs.get(dimensionKey); | ||
| } | ||
|
|
||
| public static boolean contributeItems(String dimensionKey, ItemStack item) { | ||
| DimensionProgress progress = progressCache.get(dimensionKey); | ||
| DimensionConfig config = dimensionConfigs.get(dimensionKey); | ||
|
|
||
|
|
||
| if (progress == null || config == null || progress.isUnlocked() || !progress.canProgressToNextStep()) return false; | ||
|
|
||
| List<DimensionStep> steps = config.steps(); | ||
| int currentStepIndex = progress.getCurrentStep(); | ||
|
|
||
| if (currentStepIndex >= steps.size()) { | ||
| return false; | ||
| } | ||
|
|
||
| DimensionStep currentStep = steps.get(currentStepIndex); | ||
| if (!item.getType().toString().equals(currentStep.itemType())) return false; | ||
|
|
||
| int toAdd = Math.min(item.getAmount(), currentStep.count() - progress.getItemsCollected()); | ||
| item.setAmount(item.getAmount() - toAdd); | ||
|
|
||
| if (progress.getItemsCollected() >= currentStep.count()) | ||
| completeCurrentStep(progress, currentStep); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| private static void completeCurrentStep(DimensionProgress progress, DimensionStep currentStep) { | ||
| progress.setItemsCollected(0); | ||
| progress.setCurrentStep(progress.getCurrentStep() + 1); | ||
|
|
||
| DimensionConfig config = dimensionConfigs.get(currentStep.itemType()); | ||
| if (config == null) return; | ||
|
|
||
| if (progress.getCurrentStep() >= config.steps().size()) { | ||
| progress.setUnlocked(true); | ||
| progress.setNextStepUnlockTime(0); | ||
|
|
||
| Bukkit.getPluginManager().callEvent( | ||
| new DimensionUnlockedEvent(progress.getDimensionKey()) | ||
| ); | ||
| } else { | ||
| long unlockTime = System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(currentStep.nextStepDelay()); | ||
| progress.setNextStepUnlockTime(unlockTime); | ||
| } | ||
| } | ||
|
|
||
| private static void save() { | ||
| try { | ||
| for (DimensionProgress progress : progressCache.values()) { | ||
| dao.createOrUpdate(progress); | ||
| } | ||
| } catch (SQLException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
| public static String getFormattedTimeRemaining(String dimensionKey) { | ||
| DimensionProgress progress = progressCache.get(dimensionKey); | ||
| if (progress == null || progress.isUnlocked() || progress.canProgressToNextStep()) { | ||
| return "Disponible maintenant"; | ||
| } | ||
|
|
||
| long remain = progress.getCurrentStep() - System.currentTimeMillis(); | ||
| long hours = TimeUnit.MILLISECONDS.toHours(remain); | ||
| long minutes = TimeUnit.MILLISECONDS.toMinutes(remain) % 60; | ||
|
|
||
| return String.format("Disponible dans %dh %dm", hours, minutes); | ||
| } | ||
|
|
||
| public static Map<String, DimensionConfig> getAllConfigs() { | ||
| return Collections.unmodifiableMap(dimensionConfigs); | ||
| } | ||
| } | ||
16 changes: 16 additions & 0 deletions
16
src/main/java/fr/openmc/core/features/dimsopener/command/DimensionCommands.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package fr.openmc.core.features.dimsopener.command; | ||
|
|
||
| import fr.openmc.core.features.dimsopener.menus.DimensionListMenu; | ||
| import org.bukkit.entity.Player; | ||
| import revxrsal.commands.annotation.Command; | ||
| import revxrsal.commands.annotation.DefaultFor; | ||
|
|
||
| @Command("dimension") | ||
| public class DimensionCommands { | ||
|
|
||
| @DefaultFor("~") | ||
| public void dimension(Player player) { | ||
| new DimensionListMenu(player).open(); | ||
| } | ||
|
|
||
| } |
28 changes: 28 additions & 0 deletions
28
src/main/java/fr/openmc/core/features/dimsopener/event/DimensionUnlockedEvent.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package fr.openmc.core.features.dimsopener.event; | ||
|
|
||
| import lombok.Getter; | ||
| import org.bukkit.event.Event; | ||
| import org.bukkit.event.HandlerList; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| @Getter | ||
| public class DimensionUnlockedEvent extends Event { | ||
|
|
||
| private static final HandlerList HANDLERS = new HandlerList(); | ||
|
|
||
| private final String dimensionKey; | ||
|
|
||
| public DimensionUnlockedEvent(String dimensionKey) { | ||
| this.dimensionKey = dimensionKey; | ||
| } | ||
|
|
||
| @NotNull | ||
| @Override | ||
| public HandlerList getHandlers() { | ||
| return HANDLERS; | ||
| } | ||
|
|
||
| public static HandlerList getHandlerList() { | ||
| return HANDLERS; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Utilise DateUtils.getFormatted bidule la