Skip to content

Commit

Permalink
FEAT:
Browse files Browse the repository at this point in the history
- add offline support
- add offline checking to prevent a spigot offlineplayer() bug from not updating player balances
- added offline login messages for offline sales
FIX:
 - fixed balances occasionally not being handed out due to spigot incompatibility
  • Loading branch information
Adainish committed Sep 6, 2023
1 parent 6f4a48c commit 36d7eb2
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 11 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ buildscript {
apply plugin: 'net.minecraftforge.gradle'

group = 'io.github.adainish'
version = '1.0.0-SNAPSHOT'
version = '1.0.1-SNAPSHOT'

java {
archivesBaseName = 'ReturnGTS'
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/github/adainish/returngts/ReturnGTS.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class ReturnGTS {
public static ReturnGTS instance;

public static final String MOD_NAME = "ReturnGTS";
public static final String VERSION = "1.0.0";
public static final String VERSION = "1.0.1";
public static final String AUTHORS = "Winglet";
public static final String YEAR = "2023";
public static final Logger log = LogManager.getLogger(MOD_NAME);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.adainish.returngts.listeners;

import io.github.adainish.returngts.ReturnGTS;
import io.github.adainish.returngts.obj.player.GTSPlayer;
import io.github.adainish.returngts.storage.PlayerStorage;
import net.minecraft.entity.player.ServerPlayerEntity;
Expand All @@ -23,6 +24,10 @@ public void onPlayerLoggedInEvent(PlayerEvent.PlayerLoggedInEvent event)
if (player != null) {
player.setUsername(serverPlayer.getName().getString());
player.updateCache();
if (ReturnGTS.gts != null)
{
ReturnGTS.gts.updatePlayerOfflineSales(player);
}
}
}
}
Expand Down
33 changes: 27 additions & 6 deletions src/main/java/io/github/adainish/returngts/obj/GTS.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,12 @@
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.*;

public class GTS
{
public List<GTSItem> gtsItems = new ArrayList<>();

public HashMap<UUID, OfflineSaleHandler> offlineSaleMap = new HashMap<>();
public GTS()
{

Expand All @@ -59,6 +56,17 @@ public void wipeExpiredItems()
gtsItems.removeAll(expired);
}

public void updatePlayerOfflineSales(GTSPlayer player)
{
if (offlineSaleMap != null) {
if (offlineSaleMap.containsKey(player.uuid)) {
OfflineSaleHandler handler = offlineSaleMap.get(player.uuid);
handler.handout();
offlineSaleMap.put(player.uuid, new OfflineSaleHandler());
}
}
}

public boolean buyItem(GTSPlayer gtsPlayer, GTSItem gtsItem)
{
if (gtsItems.isEmpty())
Expand All @@ -68,7 +76,20 @@ public boolean buyItem(GTSPlayer gtsPlayer, GTSItem gtsItem)
if (gtsItem.hasSold())
return false;

EconomyUtil.giveBalance(gtsItem.seller, gtsItem.askingPrice);
if (gtsItem.isSellerOffline())
{
if (this.offlineSaleMap == null)
this.offlineSaleMap = new HashMap<>();
OfflineSaleHandler offlineSaleHandler = null;
if (this.offlineSaleMap.containsKey(gtsItem.seller))
{
offlineSaleHandler = this.offlineSaleMap.get(gtsItem.seller);
} else offlineSaleHandler = new OfflineSaleHandler();
offlineSaleHandler.add(gtsItem);
this.offlineSaleMap.put(gtsItem.seller, offlineSaleHandler);
} else {
EconomyUtil.giveBalance(gtsItem.seller, gtsItem.askingPrice);
}
this.gtsItems.remove(gtsItem);
gtsItem.buyer = gtsPlayer.uuid;

Expand Down
15 changes: 12 additions & 3 deletions src/main/java/io/github/adainish/returngts/obj/GTSItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ public String displayTitle() {
return s;
}

public boolean isSellerOffline()
{
return ReturnGTS.getServer().getPlayerList().getPlayerByUUID(this.seller) == null;
}

public GooeyButton menuButton(GTSPlayer player) {
return GooeyButton.builder()
.title(Util.formattedString(displayTitle()))
Expand Down Expand Up @@ -132,13 +137,17 @@ public void sendToSellerStorage() {
}
}

public void sendToBuyer() {
GTSPlayer seller = PlayerStorage.getPlayer(this.seller);
GTSPlayer buyer = PlayerStorage.getPlayer(this.buyer);
public String itemString() {
String itemString = "";
if (this.pokemon != null)
itemString = this.pokemon.getSpecies().getName();
else itemString = Util.getItemStackName(this.itemStack);
return itemString;
}
public void sendToBuyer() {
GTSPlayer seller = PlayerStorage.getPlayer(this.seller);
GTSPlayer buyer = PlayerStorage.getPlayer(this.buyer);
String itemString = itemString();

if (buyer != null) {
if (buyer.getOptionalServerPlayer().isPresent()) {
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/io/github/adainish/returngts/obj/OfflineSale.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.github.adainish.returngts.obj;

import io.github.adainish.returngts.obj.player.GTSPlayer;
import io.github.adainish.returngts.storage.PlayerStorage;
import io.github.adainish.returngts.util.EconomyUtil;

import java.util.UUID;

public class OfflineSale
{
public GTSItem gtsItem;

public OfflineSale(GTSItem gtsItem)
{
this.gtsItem = gtsItem;
}

public void updateSeller()
{
GTSPlayer seller = PlayerStorage.getPlayer(gtsItem.seller);
if (seller != null)
{
seller.sendMessage("&6Your GTS listing of %item% sold for %amount% while you were offline"
.replace("%item%", gtsItem.itemString())
.replace("%amount%", String.valueOf(gtsItem.askingPrice)));
seller.sendMessage("&aWe've updated your balance due to your sale while you were offline!");
}
EconomyUtil.giveBalance(gtsItem.seller, gtsItem.askingPrice);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.github.adainish.returngts.obj;

import io.github.adainish.returngts.ReturnGTS;

import java.util.ArrayList;
import java.util.List;

public class OfflineSaleHandler
{
public List<OfflineSale> offlineSaleList = new ArrayList<>();

public OfflineSaleHandler()
{

}

public void add(GTSItem item)
{
this.offlineSaleList.add(new OfflineSale(item));
ReturnGTS.log.warn("Added an offline sale to storage for uuid :" + item.seller);
}

public void handout()
{

if (!offlineSaleList.isEmpty())
{
this.offlineSaleList.forEach(OfflineSale::updateSeller);
this.offlineSaleList.clear();
}
}
}

0 comments on commit 36d7eb2

Please sign in to comment.