Skip to content

Commit

Permalink
2.13.5
Browse files Browse the repository at this point in the history
  • Loading branch information
Stonley890 committed Jun 21, 2024
1 parent afd26ed commit b16cf77
Show file tree
Hide file tree
Showing 14 changed files with 257 additions and 83 deletions.
2 changes: 1 addition & 1 deletion dreamvisitor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.stonley890</groupId>
<artifactId>dreamvisitor</artifactId>
<version>2.13.3</version>
<version>2.13.5</version>
<packaging>jar</packaging>

<name>Dreamvisitor</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public static JDA getJda() {
public static void sendLog(@NotNull String message) {
try {
if (!Dreamvisitor.botFailed &&!PLUGIN.getConfig().getBoolean("log-console")) Bot.getGameLogChannel().sendMessage(message).queue();
else gameLogChannel.sendMessage(message).queue();
} catch (InsufficientPermissionException e) {
Bukkit.getLogger().warning("Dreamvisitor bot does not have sufficient permissions to send messages in game log channel!");
} catch (IllegalArgumentException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.*;
import java.util.logging.Level;

/*
Expand Down Expand Up @@ -289,6 +287,42 @@ public void run() {
}
};

Runnable remindWarns = new BukkitRunnable() {
@Override
public void run() {
Dreamvisitor.debug("Checking warns to be reminded.");
Map<Long, List<Infraction>> infractions = Infraction.getAllInfractions();
Dreamvisitor.debug("Got list of " + infractions.keySet().size() + " members.");
for (Long l : infractions.keySet()) {
Dreamvisitor.debug("Checking infractions of user " + l);
List<Infraction> userInfractions = Infraction.getInfractions(l);
for (Infraction userInfraction : userInfractions) {
Dreamvisitor.debug("Attempting remind...");
userInfraction.remind(l);
}
}
}
};

Runnable checkBannedItems = new BukkitRunnable() {
@Override
public void run() {
for (Player player : Bukkit.getOnlinePlayers()) {
if (!player.isOp() && ItemBanList.badItems != null) {

for (ItemStack item : ItemBanList.badItems) {
if (item == null) continue;
for (ItemStack content : player.getInventory().getContents()) {
if (content == null || !content.isSimilar(item)) continue;
player.getInventory().remove(item);
Bot.sendLog("Removed " + item.getType().name() + " (" + Objects.requireNonNull(item.getItemMeta()).getDisplayName() + ") from " + player.getName());
}
}
}
}
}
};

Bukkit.getScheduler().runTaskTimer(this, tick, 0, 0);

// Push console every two seconds
Expand All @@ -297,6 +331,12 @@ public void run() {
// Check for scheduled restart every minute
Bukkit.getScheduler().runTaskTimer(this, scheduledRestarts, 200, 1200);

// Check for warns that need to be reminded every hour
Bukkit.getScheduler().runTaskTimer(this, remindWarns, 200, 20*60*60);

// Check for banned items every ten seconds
Bukkit.getScheduler().runTaskTimer(this, checkBannedItems, 40, 20*10);

debug("Enable finished.");
} catch (Exception e) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.hover.content.Text;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

public class CmdSandbox implements DVCommand {

Expand Down Expand Up @@ -50,10 +52,14 @@ public CommandAPICommand getCommand() {
HoverEvent tooltip = new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text("Click to remove."));

for (Player player : sandboxedPlayers) {
messageBuilder.append("[").color(ChatColor.WHITE)
.append(player.getName()).color(ChatColor.YELLOW)
Location location = player.getLocation();
messageBuilder.append(player.getName()).color(ChatColor.YELLOW)
.append(" [").color(ChatColor.WHITE)
.append("Remove").color(ChatColor.RED)
.event(tooltip).event(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/sandbox " + player.getName() + " off"))
.append("] ").color(ChatColor.WHITE).event((ClickEvent) null);
.append("]\n").color(ChatColor.WHITE).event((ClickEvent) null)
.append(String.valueOf(location.getBlockX())).append(", ").append(String.valueOf(location.getBlockY())).append(", ").append(String.valueOf(location.getBlockZ()))
.append(" in world ").append(Objects.requireNonNull(location.getWorld()).getName()).append(".\n\n");
}

sender.spigot().sendMessage(messageBuilder.create());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -725,8 +725,8 @@ public void useItem(int itemId) throws NullPointerException, ItemNotEnabledExcep
* @throws CoolDownException if this consumer cannot yet claim their
*/
public double claimDaily() throws CoolDownException {
if (gameData.lastDaily != null && !Objects.equals(gameData.lastDaily.plusHours(24).getDayOfYear(), LocalDateTime.now().getDayOfYear())) throw new CoolDownException();
gameData.updateStreak();
if (gameData.lastDaily != null && Objects.equals(gameData.lastDaily.toLocalDate(), LocalDate.now())) throw new CoolDownException();
double dailyBaseAmount = Economy.getDailyBaseAmount();
double reward = dailyBaseAmount + (gameData.getDailyStreak() * getDailyStreakMultiplier());
setBalance(balance + reward);
Expand Down Expand Up @@ -778,9 +778,6 @@ public CoolDownException() {
super();
}

public CoolDownException(String message) {
super(message);
}
}
}

Expand Down Expand Up @@ -826,7 +823,7 @@ public Duration timeUntilNextWork() {
}

public void updateStreak() {
if (lastDaily == null || LocalDate.from(lastDaily).plusDays(2).equals(LocalDate.now())) setDailyStreak(0);
if (lastDaily == null || !LocalDate.from(lastDaily).plusDays(1).equals(LocalDate.now())) setDailyStreak(0);
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.channel.concrete.Category;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.exceptions.InsufficientPermissionException;
import net.dv8tion.jda.api.interactions.components.buttons.Button;
import org.bukkit.BanList;
Expand All @@ -17,13 +18,15 @@
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.io.InvalidObjectException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.util.List;
import java.util.*;
import java.util.concurrent.TimeUnit;
Expand All @@ -42,6 +45,8 @@ public class Infraction implements ConfigurationSerializable {
@NotNull
private final LocalDateTime time;
private boolean expired = false;
@Nullable
private Long warnChannelID = null;

/**
* Creates and saves an infraction to disk.
Expand Down Expand Up @@ -107,6 +112,19 @@ private static void saveToDisk(@NotNull YamlConfiguration config) {
return infractions;
}

public static @NotNull Map<Long, List<Infraction>> getAllInfractions() {
Set<String> keys = getConfig().getKeys(false);
Map<Long, List<Infraction>> infractionList = new HashMap<>();
for (String key : keys) {
List<Map<?, ?>> infractionsMap = getConfig().getMapList(key);
List<Infraction> infractions = new ArrayList<>();
for (Map<?, ?> map : infractionsMap) infractions.add(deserialize((Map<String, Object>) map));
infractionList.put(Long.parseLong(key), infractions);
}

return infractionList;
}

/**
* Get the value of all of a member's infractions.
*
Expand Down Expand Up @@ -193,6 +211,32 @@ public static void execute(@NotNull Infraction infraction, @NotNull Member membe
return;
}

if (doBan) {
UUID uuid = AccountLink.getUuid(member.getIdLong());
if (uuid != null) Bukkit.getScheduler().runTask(Dreamvisitor.getPlugin(), bukkitTask -> {
String username = PlayerUtility.getUsernameOfUuid(uuid);
if (username != null) {
ProfileBanList banList = Bukkit.getBanList(BanList.Type.PROFILE);
if (!hasTempban)
banList.addBan(Bukkit.createPlayerProfile(uuid, username), infraction.reason, Instant.from(LocalDateTime.now().plusDays(7)), "Dreamvisitor");
else
banList.addBan(Bukkit.createPlayerProfile(uuid, username), infraction.reason, (Date) null, "Dreamvisitor");
}
});
}

if (banPoint) {
infraction.expire();
List<Infraction> disabledInfractions = new ArrayList<>();
for (Infraction existingInfraction : infractions) {
existingInfraction.expire();
disabledInfractions.add(existingInfraction);
}
setInfractions(disabledInfractions, member.getIdLong());
if (!hasTempban) setTempban(member.getIdLong(), true);
else setBan(member.getIdLong(), true);
}

if (!silent) {

Category category = Bot.getGameLogChannel().getGuild().getCategoryById(Dreamvisitor.getPlugin().getConfig().getLong("infractions-category-id"));
Expand All @@ -201,7 +245,7 @@ public static void execute(@NotNull Infraction infraction, @NotNull Member membe
category.createTextChannel("infraction-" + member.getUser().getName() + "-" + (totalInfractionCount + infraction.value)).queue(channel -> {

Button primary = Button.primary("warn-understand", "I understand");
Button secondary = Button.secondary("warn-explain", "I want an explanation");
Button secondary = Button.secondary("warn-explain", "I'm confused");

channel.upsertPermissionOverride(member).setAllowed(Permission.VIEW_CHANNEL).queue();

Expand Down Expand Up @@ -245,37 +289,11 @@ public static void execute(@NotNull Infraction infraction, @NotNull Member membe
embed.setTitle("Infraction Notice").setDescription(description).setFooter("See the #rules channel for more information about our rules system.").setColor(Color.getHSBColor(17, 100, 100));

channel.sendMessage(member.getAsMention()).setEmbeds(embed.build()).setActionRow(primary, secondary).queue();
infraction.warnChannelID = channel.getIdLong();
infraction.save(member.getIdLong());
}, throwable -> DCmdWarn.lastInteraction.editOriginal("There was a problem executing this command: " + throwable.getMessage()).queue());

}

if (doBan) {
UUID uuid = AccountLink.getUuid(member.getIdLong());
if (uuid != null) Bukkit.getScheduler().runTask(Dreamvisitor.getPlugin(), bukkitTask -> {
String username = PlayerUtility.getUsernameOfUuid(uuid);
if (username != null) {
ProfileBanList banList = Bukkit.getBanList(BanList.Type.PROFILE);
if (!hasTempban)
banList.addBan(Bukkit.createPlayerProfile(uuid, username), infraction.reason, Instant.from(LocalDateTime.now().plusDays(7)), "Dreamvisitor");
else
banList.addBan(Bukkit.createPlayerProfile(uuid, username), infraction.reason, (Date) null, "Dreamvisitor");
}
});
}

if (banPoint) {
infraction.expire();
List<Infraction> disabledInfractions = new ArrayList<>();
for (Infraction existingInfraction : infractions) {
existingInfraction.expire();
disabledInfractions.add(existingInfraction);
}
setInfractions(disabledInfractions, member.getIdLong());
if (!hasTempban) setTempban(member.getIdLong(), true);
else setBan(member.getIdLong(), true);
}

infraction.save(member.getIdLong());
} else infraction.save(member.getIdLong());

}

Expand All @@ -285,6 +303,7 @@ public static void execute(@NotNull Infraction infraction, @NotNull Member membe
public static @NotNull Infraction deserialize(@NotNull Map<String, Object> map) {
Infraction infraction = new Infraction(Byte.parseByte(String.valueOf((int) map.get("value"))), (String) map.get("reason"), LocalDateTime.parse((CharSequence) map.get("time")));
if (map.get("expired") != null && (boolean) map.get("expired")) infraction.expire();
infraction.warnChannelID = (Long) map.get("warnChannelID");
return infraction;
}

Expand Down Expand Up @@ -327,6 +346,29 @@ private void expireCheck() {
if (time.plusDays(expireTimeDays).isBefore(LocalDateTime.now())) expired = true;
}

@Nullable
public TextChannel getWarnChannel() {
if (warnChannelID == null) return null;
return Bot.getJda().getTextChannelById(warnChannelID);
}

public void remind(long user) {
Dreamvisitor.debug("Remind warn. warnChannelId: " + warnChannelID);
if (warnChannelID == null) return;
TextChannel warnChannel = getWarnChannel();
if (warnChannel == null) return;

Dreamvisitor.debug("Attempting to retrieve last message.");
warnChannel.retrieveMessageById(warnChannel.getLatestMessageId()).queue(message -> {
Dreamvisitor.debug("Retrieved last message.");
Dreamvisitor.debug("Message author is bot? " + message.getAuthor().equals(Bot.getJda().getSelfUser()));
Dreamvisitor.debug("Time is passed? " + message.getTimeCreated().plusDays(1).isBefore(OffsetDateTime.now()));
if (message.getAuthor().equals(Bot.getJda().getSelfUser()) && message.getTimeCreated().plusDays(1).isBefore(OffsetDateTime.now())) {
warnChannel.getGuild().retrieveMemberById(user).queue(member -> warnChannel.sendMessage(member.getAsMention() + ", you have not yet responded to this thread. On the first message in this thread, press **I understand** to close the thread or **I'm confused** if you're confused.").queue());
}
});
}

@NotNull
@Override
public Map<String, Object> serialize() {
Expand All @@ -336,7 +378,21 @@ public Map<String, Object> serialize() {
objectMap.put("reason", reason);
objectMap.put("time", time.toString());
objectMap.put("expired", expired);
objectMap.put("warnChannelID", warnChannelID);

return objectMap;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Infraction that = (Infraction) o;
return value == that.value && expired == that.expired && Objects.equals(reason, that.reason) && Objects.equals(time, that.time) && Objects.equals(warnChannelID, that.warnChannelID);
}

@Override
public int hashCode() {
return Objects.hash(value, reason, time, expired, warnChannelID);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public static void init() {
addList.add(new DCmdDaily());
addList.add(new DCmdWork());
addList.add(new DCmdBaltop());
addList.add(new DCmdSeen());

Dreamvisitor.debug("Ready to add to guild.");

Expand Down
Loading

0 comments on commit b16cf77

Please sign in to comment.