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

Java Edition 1.21 #55

Merged
merged 11 commits into from
Jun 14, 2024
Merged
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
4 changes: 2 additions & 2 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
@@ -2,9 +2,9 @@ name: Java CI with Gradle

on:
push:
branches: [ "main/1.20.x", "next/1.20.x" ]
branches: [ "main/1.20.x", "next/1.20.x", "main/1.21.x", "next/1.21.x" ]
pull_request:
branches: [ "main/1.20.x", "next/1.20.x" ]
branches: [ "main/1.20.x", "next/1.20.x", "main/1.21.x", "next/1.21.x" ]

permissions:
contents: read
6 changes: 1 addition & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -15,11 +15,7 @@ Did your server crashed? Is it because of Stonks? Did you found a duplication gl
## Contribute to documentations
Some people are not insanely smart to figure out how to use Stonks. That's why I want some poeple to help with documentations.

### User's documentations (a.k.a Wiki)
Currently, [Wiki](https://github.com/nahkd123/stonks/wiki) can only edited by collaborators. I'm planning to use GitHub Pages for all kind of documentations.

### Developer's documentations
As of today, you can fork this repository, make changes in ``/docs/`` and create a new pull request to contribute to developer's documentations. Once GitHub Pages is available for this project, you have to create pull requests to ``docs`` branch of this repository.
All documentations are maintained in `docs/<version>` branches. For example, documentations for Stonks for Minecraft 1.21 are stored in `docs/1.21.x`. Please note that only the branch whose version is equals to main branch's version (eg: main branch is `main/1.21.x` then the docs branch must be `docs/1.21.x`) is used for https://nahkd123.github.io/stonks/ page. Other branches are meant for historical/archival purpose (so if you are still using Stonks for MC 1.20.6, you can checkout `docs/1.20.x`).

## Contribute code
You can contribute code by creating pull request to this repository.
2 changes: 1 addition & 1 deletion HEADER
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 nahkd
* Copyright (c) 2023-2024 nahkd
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
![Build Status Badge](https://img.shields.io/github/actions/workflow/status/nahkd123/stonks/gradle.yml)
![License Badge](https://img.shields.io/github/license/nahkd123/stonks)
![Dynamic Version Badge](https://img.shields.io/github/v/release/nahkd123/stonks)
![Minecraft Version Badge](https://img.shields.io/badge/minecraft-1.20.5-red)
![Minecraft Version Badge](https://img.shields.io/badge/minecraft-1.21_rc1-red)

Weird little stocks/items market for block game.

@@ -13,12 +13,12 @@ Weird little stocks/items market for block game.
All releases can be found in [Releases page](https://github.com/nahkd123/stonks/releases).

### Development builds
You can grab development builds [here](https://github.com/nahkd123/stonks/actions/workflows/gradle.yml?query=branch:main/1.20.x+is:success). Click on latest workflow run, scroll down to Artifacts section and click on ``Fabric Artifacts`` to download. Then copy ``stonks-fabric-[MOD VERSION]+[GAME VERSION].jar`` to your ``mods/`` folder/directory.
You can grab development builds [here](https://github.com/nahkd123/stonks/actions/workflows/gradle.yml?query=branch:main/1.21.x+is:success). Click on latest workflow run, scroll down to Artifacts section and click on ``Fabric Artifacts`` to download. Then copy ``stonks-fabric-[MOD VERSION]+[GAME VERSION].jar`` to your ``mods/`` folder/directory.

> **Small note**: The link above includes filter for success development builds only. Click on "Actions" tab to see everything.
## Documentations
For documentations, please [see here](https://nahkd123.github.io/stonks/).
For documentations, please [see here](https://nahkd123.github.io/stonks/). Documentations are maintained in `docs/<version>` branches.

## Contributing
All contributions are welcomed! This includes code, documentations, finding bugs and even helping other people using Stonks!
@@ -29,8 +29,8 @@ Please take a look at [Code of Conduct](./CODE_OF_CONDUCT.md) before contributin
After that, see [Contributing guide](./CONTRIBUTING.md) to get started.

## Wait... why 2.0.0?
Most people doesn't know this, but Stonks was originally created as a [Bukkit plugin](https://github.com/MangoPlex/stonks). It was created to showcase what FeatherPowder's capability, but ended up abandoning it because the code base is too hard to maintain.
## A story behind the version 2
Most people doesn't know this, but Stonks was originally created as a [Bukkit plugin](https://github.com/MangoPlex/stonks). It was created to showcase the FeatherPowder's capability, but ended up abandoning it because the code base is too hard to maintain.

The first [Stonks for Fabric](https://github.com/phomc/stonks) port (let's call this "Stonks 1.1") was released under PhoMC organization. This one includes MongoDB support, but then I realized SQL databases are the better candidate for storing players' offer data.

4 changes: 0 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -33,10 +33,6 @@ subprojects {
return "com.github.nahkd123.nahara-toolkit:${moduleName}:$rootProject.nahara_toolkit_version"
}

dependencies.ext.naharaModkit = { moduleName ->
return "com.github.nahkd123.nahara-modkit:${moduleName}:$rootProject.nahara_modkit_version"
}

java {
withSourcesJar()

2 changes: 1 addition & 1 deletion core/src/main/java/stonks/core/caching/Cached.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 nahkd
* Copyright (c) 2023-2024 nahkd
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
132 changes: 132 additions & 0 deletions core/src/main/java/stonks/core/caching/FutureCache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Copyright (c) 2023-2024 nahkd
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package stonks.core.caching;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.function.Supplier;

public class FutureCache<T> {
private Supplier<CompletableFuture<T>> invoker;
private long lastFetch = -1;
private CompletableFuture<T> fetchingTask = null;
private T result = null;

// Options
private long maxCacheTime = 5000L;

public FutureCache(Supplier<CompletableFuture<T>> invoker, T defaultResult) {
this.invoker = invoker;
this.result = defaultResult;
}

public FutureCache(Supplier<CompletableFuture<T>> invoker) {
this(invoker, null);
}

/**
* <p>
* Configure the cache time, which is the minimum amount of time the object is
* allowed to stay in this cache. Default cache time is 5000 milliseconds.
* </p>
*
* @param maxCacheTime The maximum cache time.
* @return The cache.
*/
public FutureCache<T> withMaxCacheTime(long maxCacheTime) {
this.maxCacheTime = maxCacheTime;
return this;
}

/**
* <p>
* Return the state that determine if {@link #forceFetch()} should be called
* again.
* </p>
*
* @return The state.
*/
public boolean shouldFetch() {
return fetchingTask != null || lastFetch == -1L || (System.currentTimeMillis() - lastFetch > maxCacheTime);
}

/**
* <p>
* Force this cache to fetch the value. If there is already a task running, it
* will return that task.
* </p>
*
* @return The fetch task.
*/
public CompletableFuture<T> forceFetch() {
if (fetchingTask != null) return fetchingTask;
return fetchingTask = invoker.get().handle((r, t) -> {
lastFetch = System.currentTimeMillis();
result = r;
fetchingTask = null;

if (t != null) throw new CompletionException(t);
return r;
});
}

/**
* <p>
* Get the object that is still under cache retention wrapped as immediately
* completed future, or future of the fetching task that is still running.
* </p>
*
* @return The future.
*/
public CompletableFuture<T> get() {
if (fetchingTask != null && fetchingTask.isDone()) {
if (!fetchingTask.isCompletedExceptionally()) try {
result = fetchingTask.get();
} catch (Throwable e) {
e.printStackTrace();
}

fetchingTask = null;
}

if (!shouldFetch()) {
if (fetchingTask != null) return fetchingTask;
return CompletableFuture.completedFuture(result);
}

return forceFetch();
}

/**
* <p>
* Get the value immediately, regardless the cache state. If the value need to
* be fetched again, it will call the {@link #forceFetch()} method and return
* previous value.
* </p>
*
* @return The cached value.
*/
public T getNow() {
if (shouldFetch()) forceFetch();
return result;
}
}
80 changes: 80 additions & 0 deletions core/src/main/java/stonks/core/caching/StonksCache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright (c) 2023-2024 nahkd
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package stonks.core.caching;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.WeakHashMap;
import java.util.concurrent.CompletableFuture;

import stonks.core.market.Offer;
import stonks.core.market.ProductMarketOverview;
import stonks.core.product.Category;
import stonks.core.product.Product;
import stonks.core.service.StonksService;

public class StonksCache {
private StonksService service;
private CompletableFuture<List<Category>> categories;
private Map<String, Category> categoryIdMap = null;

// Expiring cache
private Map<UUID, FutureCache<List<Offer>>> offers = new WeakHashMap<>();
private Map<Product, FutureCache<ProductMarketOverview>> overviews = new WeakHashMap<>();

public StonksCache(StonksService service) {
this.service = service;
}

public StonksService getService() { return service; }

public CompletableFuture<List<Category>> getAllCategories() {
if (categories == null) return categories = service.queryAllCategoriesAsync()
.thenApply(categories -> {
categoryIdMap = new HashMap<>();
for (var category : categories) { categoryIdMap.put(category.getCategoryId(), category); }
return categories;
});
return categories;
}

public CompletableFuture<Category> getCategoryById(String id) {
if (categoryIdMap != null) return CompletableFuture.completedFuture(categoryIdMap.get(id));
return getAllCategories().thenApply($ -> { return categoryIdMap.get(id); });
}

public FutureCache<List<Offer>> getOffers(UUID offerer) {
var cachedList = offers.get(offerer);
if (cachedList == null)
offers.put(offerer, cachedList = new FutureCache<>(() -> getService().getOffersFromUserAsync(offerer)));
return cachedList;
}

public FutureCache<ProductMarketOverview> getOverview(Product product) {
var cachedOverview = overviews.get(product);
if (cachedOverview == null) overviews.put(product,
cachedOverview = new FutureCache<>(() -> getService().queryMarketOverviewAsync(product)));
return cachedOverview;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 nahkd
* Copyright (c) 2023-2024 nahkd
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -34,6 +34,7 @@
import stonks.core.product.Product;
import stonks.core.service.StonksService;

@Deprecated(forRemoval = true)
public class StonksServiceCache {
private StonksService service;
private Task<List<Category>> categories;
32 changes: 32 additions & 0 deletions core/src/main/java/stonks/core/dynamic/Dynamic.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2023-2024 nahkd
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package stonks.core.dynamic;

/**
* <p>
* A {@link Dynamic} is an interface that proxies the underlying dynamic object
* structure, such as JSON or NBT.
* </p>
*/
public interface Dynamic {
public DynamicFactory getFactory();
}
Loading