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.Config;
|
||||||
import com.alttd.config.VillagerConfig;
|
import com.alttd.config.VillagerConfig;
|
||||||
import com.alttd.config.WorthConfig;
|
import com.alttd.config.WorthConfig;
|
||||||
|
import com.alttd.database.Queries;
|
||||||
import com.alttd.events.LoginEvent;
|
import com.alttd.events.LoginEvent;
|
||||||
import com.alttd.events.LogoutEvent;
|
import com.alttd.events.LogoutEvent;
|
||||||
import com.alttd.events.VillagerInteract;
|
import com.alttd.events.VillagerInteract;
|
||||||
|
import com.alttd.objects.EconUser;
|
||||||
import com.alttd.util.Logger;
|
import com.alttd.util.Logger;
|
||||||
import net.milkbowl.vault.economy.Economy;
|
import net.milkbowl.vault.economy.Economy;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.plugin.RegisteredServiceProvider;
|
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
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 {
|
public class VillagerUI extends JavaPlugin {
|
||||||
|
|
||||||
|
|
@ -39,11 +47,24 @@ public class VillagerUI extends JavaPlugin {
|
||||||
if (!setupEconomy())
|
if (!setupEconomy())
|
||||||
return;
|
return;
|
||||||
Database.getDatabase().init();
|
Database.getDatabase().init();
|
||||||
|
scheduleTasks();
|
||||||
Logger.info("--------------------------------------------------");
|
Logger.info("--------------------------------------------------");
|
||||||
Logger.info("Villager UI started");
|
Logger.info("Villager UI started");
|
||||||
Logger.info("--------------------------------------------------");
|
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() {
|
private void registerEvents() {
|
||||||
getServer().getPluginManager().registerEvents(new GUIListener(), this);
|
getServer().getPluginManager().registerEvents(new GUIListener(), this);
|
||||||
getServer().getPluginManager().registerEvents(new VillagerInteract(), this);
|
getServer().getPluginManager().registerEvents(new VillagerInteract(), this);
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,10 @@ import com.alttd.VillagerUI;
|
||||||
import com.alttd.database.Queries;
|
import com.alttd.database.Queries;
|
||||||
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
|
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
|
||||||
import org.bukkit.scheduler.BukkitRunnable;
|
import org.bukkit.scheduler.BukkitRunnable;
|
||||||
|
import org.jetbrains.annotations.Unmodifiable;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
|
|
@ -46,8 +48,8 @@ public class EconUser {
|
||||||
}.runTaskAsynchronously(VillagerUI.getInstance());
|
}.runTaskAsynchronously(VillagerUI.getInstance());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removePoints(int remove) {
|
private void removePoints(String villagerType, int points, int remove)
|
||||||
pointsMap.forEach((villagerType, points) -> {
|
{
|
||||||
if (points == 0)
|
if (points == 0)
|
||||||
return;
|
return;
|
||||||
if (points > 0)
|
if (points > 0)
|
||||||
|
|
@ -61,6 +63,20 @@ public class EconUser {
|
||||||
else
|
else
|
||||||
points += remove;
|
points += remove;
|
||||||
pointsMap.put(villagerType, points);
|
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;
|
||||||
|
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);
|
users.remove(uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void syncAllPoints() {
|
@Unmodifiable
|
||||||
Collections.unmodifiableMap(users).values().forEach(EconUser::syncPoints);
|
public static List<EconUser> getEconUsers() {
|
||||||
|
return Collections.unmodifiableList(users.values().stream().toList());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user