Skip to content

Commit

Permalink
AccountChest logic changes (#174)
Browse files Browse the repository at this point in the history
* Add basic database logic

* Add basic database logic

* Update to java 17

* Switch from cents to totalValue

* Pending changes

* Bump EBean from 2.8.1 to 15.4.0 + data migration support

* Fix NPE

* Improve AccountChests cache

* Event driven vault suppression & depend on Paper

* AccountChest cacheBalance tracking

* Register PendingOperations

* Apply PendingOperation

* Update version number

* Add jitpack.yml

* Jitpack update maven
  • Loading branch information
GroobleDierne authored Aug 30, 2024
1 parent f7ade10 commit cb98b65
Show file tree
Hide file tree
Showing 23 changed files with 716 additions and 203 deletions.
4 changes: 3 additions & 1 deletion .settings/org.eclipse.jdt.apt.core.prefs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
eclipse.preferences.version=1
org.eclipse.jdt.apt.aptEnabled=false
org.eclipse.jdt.apt.aptEnabled=true
org.eclipse.jdt.apt.genSrcDir=target/generated-sources/annotations
org.eclipse.jdt.apt.genTestSrcDir=target/generated-test-sources/test-annotations
14 changes: 7 additions & 7 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ org.eclipse.jdt.core.compiler.annotation.inheritNullAnnotations=disabled
org.eclipse.jdt.core.compiler.annotation.missingNonNullByDefaultAnnotation=ignore
org.eclipse.jdt.core.compiler.annotation.nonnull=javax.annotation.Nonnull
org.eclipse.jdt.core.compiler.annotation.nonnull.secondary=
org.eclipse.jdt.core.compiler.annotation.nonnullbydefault=org.eclipse.jdt.annotation.NonNullByDefault
org.eclipse.jdt.core.compiler.annotation.nonnullbydefault=javax.annotation.ParametersAreNonnullByDefault
org.eclipse.jdt.core.compiler.annotation.nonnullbydefault.secondary=
org.eclipse.jdt.core.compiler.annotation.nullable=javax.annotation.Nullable
org.eclipse.jdt.core.compiler.annotation.nullable.secondary=
Expand All @@ -42,9 +42,9 @@ org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.lambda.genericSignature=do not generate
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.shareCommonFinallyBlocks=disabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.compliance=17
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
Expand Down Expand Up @@ -110,7 +110,7 @@ org.eclipse.jdt.core.compiler.problem.nonnullTypeVariableFromLegacyInvocation=wa
org.eclipse.jdt.core.compiler.problem.nullAnnotationInferenceConflict=warning
org.eclipse.jdt.core.compiler.problem.nullReference=warning
org.eclipse.jdt.core.compiler.problem.nullSpecViolation=warning
org.eclipse.jdt.core.compiler.problem.nullUncheckedConversion=warning
org.eclipse.jdt.core.compiler.problem.nullUncheckedConversion=ignore
org.eclipse.jdt.core.compiler.problem.overridingMethodWithoutSuperInvocation=ignore
org.eclipse.jdt.core.compiler.problem.overridingPackageDefaultMethod=warning
org.eclipse.jdt.core.compiler.problem.parameterAssignment=ignore
Expand All @@ -131,7 +131,7 @@ org.eclipse.jdt.core.compiler.problem.staticAccessReceiver=warning
org.eclipse.jdt.core.compiler.problem.suppressOptionalErrors=disabled
org.eclipse.jdt.core.compiler.problem.suppressWarnings=enabled
org.eclipse.jdt.core.compiler.problem.suppressWarningsNotFullyAnalysed=info
org.eclipse.jdt.core.compiler.problem.syntacticNullAnalysisForFields=disabled
org.eclipse.jdt.core.compiler.problem.syntacticNullAnalysisForFields=enabled
org.eclipse.jdt.core.compiler.problem.syntheticAccessEmulation=ignore
org.eclipse.jdt.core.compiler.problem.tasks=warning
org.eclipse.jdt.core.compiler.problem.terminalDeprecation=warning
Expand Down Expand Up @@ -167,9 +167,9 @@ org.eclipse.jdt.core.compiler.problem.unusedTypeArgumentsForMethodInvocation=war
org.eclipse.jdt.core.compiler.problem.unusedTypeParameter=ignore
org.eclipse.jdt.core.compiler.problem.unusedWarningToken=warning
org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning
org.eclipse.jdt.core.compiler.processAnnotations=disabled
org.eclipse.jdt.core.compiler.processAnnotations=enabled
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8
org.eclipse.jdt.core.compiler.source=17
org.eclipse.jdt.core.compiler.storeAnnotations=disabled
org.eclipse.jdt.core.compiler.taskCaseSensitive=enabled
org.eclipse.jdt.core.compiler.taskPriorities=NORMAL,HIGH,NORMAL
Expand Down
7 changes: 7 additions & 0 deletions jitpack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
jdk:
- openjdk17
before_install:
- sdk install java 17.0.3-open
- sdk use java 17.0.3-open
- sdk install maven
- mvn -v
47 changes: 30 additions & 17 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
<modelVersion>4.0.0</modelVersion>
<groupId>minecraftwars</groupId>
<artifactId>gringotts</artifactId>
<version>2.12.7-SNAPSHOT</version>
<version>3.0.0-SNAPSHOT</version>

<properties>
<maven.deploy.skip>false</maven.deploy.skip>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

Expand Down Expand Up @@ -58,7 +58,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.0</version>
<version>3.6.0</version>
<configuration>
<artifactSet>
<excludes>
Expand Down Expand Up @@ -110,14 +110,25 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>io.repaint.maven</groupId>
<artifactId>tiles-maven-plugin</artifactId>
<version>2.40</version>
<extensions>true</extensions>
<configuration>
<tiles>
<tile>io.ebean.tile:enhancement:15.4.0</tile>
</tiles>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.18.2-R0.1-SNAPSHOT</version>
<groupId>io.papermc.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.20.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand All @@ -139,18 +150,24 @@
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.avaje</groupId>
<artifactId>ebean</artifactId>
<version>2.8.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.papermc</groupId>
<artifactId>paperlib</artifactId>
<version>1.0.8-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.ebean</groupId>
<artifactId>ebean-test</artifactId>
<version>15.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.ebean</groupId>
<artifactId>ebean-sqlite</artifactId>
<version>15.4.0</version>
<scope>provided</scope>
</dependency>
</dependencies>

<scm>
Expand All @@ -165,10 +182,6 @@
</issueManagement>

<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<repository>
<id>papermc</id>
<url>https://papermc.io/repo/repository/maven-public/</url>
Expand Down
68 changes: 56 additions & 12 deletions src/main/java/org/gestern/gringotts/AccountChest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.gestern.gringotts;

import io.papermc.lib.PaperLib;

import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.block.Block;
Expand All @@ -10,6 +11,7 @@
import org.bukkit.inventory.DoubleChestInventory;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.gestern.gringotts.data.EBeanPendingOperation;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -34,13 +36,15 @@ public class AccountChest {
*/
public final GringottsAccount account;

private long cachedBalance;

/**
* Create Account chest based on a sign marking its position and belonging to an account.
*
* @param sign the marker sign
* @param account the account
*/
public AccountChest(Sign sign, GringottsAccount account) {
public AccountChest(Sign sign, GringottsAccount account, long cachedBalance) {
if (sign == null || account == null) {
throw new IllegalArgumentException(String.format(
"null arguments to AccountChest() not allowed. args were: sign: %s, account: %s",
Expand All @@ -52,6 +56,7 @@ public AccountChest(Sign sign, GringottsAccount account) {
this.sign = sign;
this.account = account;
this.id = String.format("%s_%d_%d_%d", sign.getWorld().getUID(), sign.getX(), sign.getY(), sign.getZ());
this.cachedBalance = cachedBalance;
}

/**
Expand Down Expand Up @@ -120,17 +125,22 @@ private boolean updateInvalid() {
destroy();

return true;
} else {
return false;
}
return false;
}

/**
* Return balance of this chest.
*
* @return balance of this chest
* Equivalent to AccountChest#balance(false)
* @return balance of this chest or the cached balance if the chest is unloaded
*/
public long balance() {
return balance(false);
}

public long balance(boolean forceUpdate) {
if (!forceUpdate && !isChestLoaded()) return cachedBalance;

if (updateInvalid()) {
return 0;
}
Expand All @@ -152,6 +162,11 @@ public long balance() {
* @return amount actually added
*/
public long add(long value) {
setCachedBalance(cachedBalance + value);
if (!isChestLoaded()) {
emitPendingOperation(value);
return value;
}
if (updateInvalid()) {
return 0;
}
Expand All @@ -170,6 +185,12 @@ public long add(long value) {
* @return amount actually removed from this chest
*/
public long remove(long value) {
long subtracted = Math.min(value, cachedBalance);
setCachedBalance(cachedBalance - subtracted);
if (!isChestLoaded()) {
emitPendingOperation(-subtracted);
return subtracted;
}
if (updateInvalid()) {
return 0;
}
Expand All @@ -193,9 +214,9 @@ public boolean notValid() {
return true;
}

String[] lines = sign.getLines();
String line0 = ChatColor.stripColor(lines[0]).trim();

// Fetch the sign again to avoid strange bug where lines are blank
String[] lines = Util.getBlockStateAs(sign.getBlock(), Sign.class).get().getLines();
String line0 = ChatColor.stripColor(lines[0]).trim();

Matcher match = VAULT_PATTERN.matcher(line0);

Expand Down Expand Up @@ -233,10 +254,10 @@ public String toString() {
Location loc = sign.getLocation();

return "[vault] "
+ loc.getBlockX() + ", "
+ loc.getBlockY() + ", "
+ loc.getBlockZ() + ", "
+ loc.getWorld();
+ loc.getBlockX() + ", "
+ loc.getBlockY() + ", "
+ loc.getBlockZ() + ", "
+ loc.getWorld().getName();
}

/**
Expand Down Expand Up @@ -364,4 +385,27 @@ public void updateSign() {

this.sign.update();
}

/**
* Returns true if the chunk containing this account's chest is already loaded in memory
* (in fact this checks if vault's sign is loaded but we don't care)
* @return
*/
public boolean isChestLoaded() {
return sign.getWorld().isChunkLoaded(sign.getX()/16, sign.getZ()/16);
}

public void setCachedBalance(long amount) {
cachedBalance = amount;
Gringotts.instance.getDao().updateChestBalance(this, cachedBalance);
}

public long getCachedBalance() {
return cachedBalance;
}

private void emitPendingOperation(long amount) {
Gringotts.instance.getPendingOperationManager()
.registerNewOperation(new EBeanPendingOperation(this, amount));
}
}
Loading

0 comments on commit cb98b65

Please sign in to comment.