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.minimessage.Template; 033 034import java.util.Collection; 035import java.util.HashMap; 036import java.util.List; 037import java.util.concurrent.atomic.AtomicInteger; 038 039public class CommentManager { 040 041 public static final HashMap<String, CommentInbox> inboxes = new HashMap<>(); 042 043 public static void sendTitle(final PlotPlayer<?> player, final Plot plot) { 044 if (!Settings.Enabled_Components.COMMENT_NOTIFIER || !plot.isOwner(player.getUUID())) { 045 return; 046 } 047 TaskManager.runTaskLaterAsync(() -> { 048 Collection<CommentInbox> boxes = CommentManager.inboxes.values(); 049 final AtomicInteger count = new AtomicInteger(0); 050 final AtomicInteger size = new AtomicInteger(boxes.size()); 051 for (final CommentInbox inbox : inboxes.values()) { 052 inbox.getComments(plot, new RunnableVal<>() { 053 @Override 054 public void run(List<PlotComment> value) { 055 int total; 056 if (value != null) { 057 int num = 0; 058 for (PlotComment comment : value) { 059 if (comment.timestamp > getTimestamp(player, inbox.toString())) { 060 num++; 061 } 062 } 063 total = count.addAndGet(num); 064 } else { 065 total = count.get(); 066 } 067 if ((size.decrementAndGet() == 0) && (total > 0)) { 068 player.sendTitle( 069 StaticCaption.of(""), 070 TranslatableCaption.of("comment.inbox_notification"), 071 Template.of("amount", Integer.toString(total)), 072 Template.of("command", "/plot inbox") 073 ); 074 } 075 } 076 }); 077 } 078 }, TaskTime.seconds(1L)); 079 } 080 081 /** 082 * @param player The player the inbox belongs to 083 * @param inbox the inbox 084 * @return the time in milliseconds when the player was last seen online 085 */ 086 public static long getTimestamp(PlotPlayer<?> player, String inbox) { 087 final MetaDataKey<Long> inboxKey = MetaDataKey.of(String.format("inbox:%s", inbox), new TypeLiteral<>() { 088 }); 089 try (final MetaDataAccess<Long> inboxAccess = player.accessTemporaryMetaData(inboxKey)) { 090 return inboxAccess.get().orElse(player.getLastPlayed()); 091 } 092 } 093 094 /** 095 * @param inbox the inbox to add 096 */ 097 public static void addInbox(CommentInbox inbox) { 098 inboxes.put(inbox.toString().toLowerCase(), inbox); 099 } 100 101 public static void registerDefaultInboxes() { 102 addInbox(new InboxReport()); 103 addInbox(new InboxPublic()); 104 addInbox(new InboxOwner()); 105 } 106 107}