Remove points from user every 10 minutes and sync them at the same time
This commit is contained in:
parent
68978ad13c
commit
b3ae5e67d8
|
|
@ -6,14 +6,22 @@ import com.alttd.database.Database;
|
|||
import com.alttd.config.Config;
|
||||
import com.alttd.config.VillagerConfig;
|
||||
import com.alttd.config.WorthConfig;
|
||||
import com.alttd.database.Queries;
|
||||
import com.alttd.events.LoginEvent;
|
||||
import com.alttd.events.LogoutEvent;
|
||||
import com.alttd.events.VillagerInteract;
|
||||
import com.alttd.objects.EconUser;
|
||||
import com.alttd.util.Logger;
|
||||
import net.milkbowl.vault.economy.Economy;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
import org.bukkit.scheduler.BukkitScheduler;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class VillagerUI extends JavaPlugin {
|
||||
|
||||
|
|
@ -39,11 +47,24 @@ public class VillagerUI extends JavaPlugin {
|
|||
if (!setupEconomy())
|
||||
return;
|
||||
Database.getDatabase().init();
|
||||
scheduleTasks();
|
||||
Logger.info("--------------------------------------------------");
|
||||
Logger.info("Villager UI started");
|
||||
Logger.info("--------------------------------------------------");
|
||||
}
|
||||
|
||||
private void scheduleTasks() {
|
||||
new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
EconUser.getEconUsers().forEach(econUser -> {
|
||||
econUser.removePoints();
|
||||
econUser.syncPoints();
|
||||
});
|
||||
}
|
||||
}.runTaskTimerAsynchronously(getInstance(), 0L, 5 * 60 * 20L);
|
||||
}
|
||||
|
||||
private void registerEvents() {
|
||||
getServer().getPluginManager().registerEvents(new GUIListener(), this);
|
||||
getServer().getPluginManager().registerEvents(new VillagerInteract(), this);
|
||||
|
|
|
|||
|
|
@ -4,8 +4,10 @@ import com.alttd.VillagerUI;
|
|||
import com.alttd.database.Queries;
|
||||
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
import org.jetbrains.annotations.Unmodifiable;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
|
||||
|
|
@ -46,21 +48,35 @@ public class EconUser {
|
|||
}.runTaskAsynchronously(VillagerUI.getInstance());
|
||||
}
|
||||
|
||||
private void removePoints(String villagerType, int points, int remove)
|
||||
{
|
||||
if (points == 0)
|
||||
return;
|
||||
if (points > 0)
|
||||
if (points < remove)
|
||||
points = 0;
|
||||
else
|
||||
points -= remove;
|
||||
else
|
||||
if (-points < remove)
|
||||
points = 0;
|
||||
else
|
||||
points += remove;
|
||||
pointsMap.put(villagerType, points);
|
||||
}
|
||||
|
||||
public void removePoints(int remove) {
|
||||
pointsMap.forEach((villagerType, points) -> removePoints(villagerType, points, remove));
|
||||
}
|
||||
|
||||
public void removePoints() {
|
||||
pointsMap.forEach((villagerType, points) -> {
|
||||
if (points == 0)
|
||||
return;
|
||||
if (points > 0)
|
||||
if (points < remove)
|
||||
points = 0;
|
||||
else
|
||||
points -= remove;
|
||||
else
|
||||
if (-points < remove)
|
||||
points = 0;
|
||||
else
|
||||
points += remove;
|
||||
pointsMap.put(villagerType, points);
|
||||
int remove = points;
|
||||
if (remove < 0)
|
||||
remove *= -1;
|
||||
removePoints(villagerType, points, (int) (0.9 * remove) - 10);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -77,7 +93,8 @@ public class EconUser {
|
|||
users.remove(uuid);
|
||||
}
|
||||
|
||||
public static void syncAllPoints() {
|
||||
Collections.unmodifiableMap(users).values().forEach(EconUser::syncPoints);
|
||||
@Unmodifiable
|
||||
public static List<EconUser> getEconUsers() {
|
||||
return Collections.unmodifiableList(users.values().stream().toList());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user