001/* 002 * PlotSquared, a land and world management plugin for Minecraft. 003 * Copyright (C) IntellectualSites <https://intellectualsites.com> 004 * Copyright (C) IntellectualSites team and contributors 005 * 006 * This program is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with this program. If not, see <https://www.gnu.org/licenses/>. 018 */ 019package com.plotsquared.core.plot.comment; 020 021import com.google.inject.TypeLiteral; 022import com.plotsquared.core.configuration.Settings; 023import com.plotsquared.core.configuration.caption.StaticCaption; 024import com.plotsquared.core.configuration.caption.TranslatableCaption; 025import com.plotsquared.core.player.MetaDataAccess; 026import com.plotsquared.core.player.MetaDataKey; 027import com.plotsquared.core.player.PlotPlayer; 028import com.plotsquared.core.plot.Plot; 029import com.plotsquared.core.util.task.RunnableVal; 030import com.plotsquared.core.util.task.TaskManager; 031import com.plotsquared.core.util.task.TaskTime; 032import net.kyori.adventure.text.Component; 033import net.kyori.adventure.text.minimessage.tag.Tag; 034import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; 035 036import java.util.Collection; 037import java.util.HashMap; 038import java.util.List; 039import java.util.concurrent.atomic.AtomicInteger; 040 041public class CommentManager { 042 043 public static final HashMap<String, CommentInbox> inboxes = new HashMap<>(); 044 045 public static void sendTitle(final PlotPlayer<?> player, final Plot plot) { 046 if (!Settings.Enabled_Components.COMMENT_NOTIFIER || !plot.isOwner(player.getUUID())) { 047 return; 048 } 049 TaskManager.runTaskLaterAsync(() -> { 050 Collection<CommentInbox> boxes = CommentManager.inboxes.values(); 051 final AtomicInteger count = new AtomicInteger(0); 052 final AtomicInteger size = new AtomicInteger(boxes.size()); 053 for (final CommentInbox inbox : inboxes.values()) { 054 inbox.getComments(plot, new RunnableVal<>() { 055 @Override 056 public void run(List<PlotComment> value) { 057 int total; 058 if (value != null) { 059 int num = 0; 060 for (PlotComment comment : value) { 061 if (comment.timestamp() > getTimestamp(player, inbox.toString())) { 062 num++; 063 } 064 } 065 total = count.addAndGet(num); 066 } else { 067 total = count.get(); 068 } 069 if ((size.decrementAndGet() == 0) && (total > 0)) { 070 player.sendTitle( 071 StaticCaption.of(""), 072 TranslatableCaption.of("comment.inbox_notification"), 073 TagResolver.builder() 074 .tag("amount", Tag.inserting(Component.text(total))) 075 .tag("command", Tag.inserting(Component.text("/plot inbox"))) 076 .build() 077 ); 078 } 079 } 080 }); 081 } 082 }, TaskTime.seconds(1L)); 083 } 084 085 /** 086 * @param player The player the inbox belongs to 087 * @param inbox the inbox 088 * @return the time in milliseconds when the player was last seen online 089 */ 090 public static long getTimestamp(PlotPlayer<?> player, String inbox) { 091 final MetaDataKey<Long> inboxKey = MetaDataKey.of(String.format("inbox:%s", inbox), new TypeLiteral<>() { 092 }); 093 try (final MetaDataAccess<Long> inboxAccess = player.accessTemporaryMetaData(inboxKey)) { 094 return inboxAccess.get().orElse(player.getLastPlayed()); 095 } 096 } 097 098 /** 099 * @param inbox the inbox to add 100 */ 101 public static void addInbox(CommentInbox inbox) { 102 inboxes.put(inbox.toString().toLowerCase(), inbox); 103 } 104 105 public static void registerDefaultInboxes() { 106 addInbox(new InboxReport()); 107 addInbox(new InboxPublic()); 108 addInbox(new InboxOwner()); 109 } 110 111}