1+ package nu .nerd .modreq ;
2+
3+ import com .j256 .ormlite .dao .Dao ;
4+ import com .j256 .ormlite .dao .DaoManager ;
5+ import com .j256 .ormlite .jdbc .JdbcPooledConnectionSource ;
6+ import com .j256 .ormlite .support .ConnectionSource ;
7+ import com .j256 .ormlite .table .TableUtils ;
8+ import net .kyori .adventure .text .logger .slf4j .ComponentLogger ;
9+ import nu .nerd .modreq .commands .*;
10+ import nu .nerd .modreq .database .Note ;
11+ import nu .nerd .modreq .database .NoteTable ;
12+ import nu .nerd .modreq .database .Request ;
13+ import nu .nerd .modreq .database .RequestTable ;
14+ import org .bukkit .Bukkit ;
15+ import org .bukkit .ChatColor ;
16+ import org .bukkit .Location ;
17+ import org .bukkit .command .Command ;
18+ import org .bukkit .command .CommandSender ;
19+ import org .bukkit .command .ConsoleCommandSender ;
20+ import org .bukkit .entity .Player ;
21+ import org .bukkit .plugin .java .JavaPlugin ;
22+ import org .bukkit .scheduler .BukkitScheduler ;
23+
24+ import java .io .File ;
25+ import java .sql .SQLException ;
26+ import java .util .*;
27+ import java .util .concurrent .CompletableFuture ;
28+ import java .util .concurrent .Executor ;
29+ import java .util .concurrent .atomic .AtomicInteger ;
30+ import java .util .logging .Level ;
31+
32+ import static nu .nerd .modreq .utils .DataUtils .stringToLocation ;
33+ import static nu .nerd .modreq .utils .DataUtils .timestampToDateString ;
34+ import static nu .nerd .modreq .utils .MessageUtils .*;
35+ import static nu .nerd .modreq .utils .RequestUtils .*;
36+
37+ public class ModReq extends JavaPlugin {
38+
39+ /**
40+ * The JDBC URL for the SQLite DB.
41+ */
42+ private static final String DATABASE_URL = "jdbc:sqlite:plugins/ModReq/modreq.db" ;
43+ /**
44+ * The ConnectionSource for database transactions.
45+ */
46+ private ConnectionSource connectionSource ;
47+ /**
48+ * The DAO object representing the Request object.
49+ */
50+ private Dao <Request , Integer > requestDao ;
51+ /**
52+ * The DAO object representing the Note object.
53+ */
54+ private Dao <Note , Integer > noteDao ;
55+ /**
56+ * Idk lol!
57+ */
58+ private CompletableFuture <Void > future = CompletableFuture .completedFuture (null );
59+ /**
60+ * The logger for outputting to the console.
61+ */
62+ private ComponentLogger componentLogger = this .getComponentLogger ();
63+ /**
64+ * Map from UUID of staff member to integer ID of most recently claimed request.
65+ * <p>
66+ * This acts as the default request ID when handling "/check -", "/tp-id -" and "/tpinfo -", and synonyms.
67+ * <p>
68+ * This collection is saved in most-recent-claims.yml across restarts.
69+ */
70+ private Map <UUID , Integer > claimedIds = new HashMap <>();
71+ /**
72+ * The file where recent claims are placed to persist through restarts.
73+ */
74+ private File claimsFile ;
75+ /**
76+ * An instance of the RequestTable class. Used for database transactions involving the Request object.
77+ */
78+ private RequestTable reqTable ;
79+ /**
80+ * An instance of the NoteTable class. Used for database transactions involving the Note object.
81+ */
82+ private NoteTable noteTable ;
83+ private ModReqListener listener ;
84+ private Configuration config = new Configuration (this );
85+ private Map <String , String > environment = new HashMap <>();
86+
87+ // Commands
88+ CommandRegistry registry ;
89+ CheckCommand checkCommand ;
90+ ClaimCommand claimCommand ;
91+ DoneCommand doneCommand ;
92+ ModreqCommand modreqCommand ;
93+ MRNoteCommand mrNoteCommand ;
94+ MRResetCommand mrResetCommand ;
95+ ReopenCommand reopenCommand ;
96+ TPClaimCommand tpClaimCommand ;
97+ TPIdCommand tpIdCommand ;
98+ UnclaimCommand unclaimCommand ;
99+ ElevateCommand elevateCommand ;
100+ TPInfoCommand tpInfoCommand ;
101+
102+
103+
104+ @ Override
105+ public void onEnable () {
106+ setupDatabase ();
107+ File configFile = new File (this .getDataFolder (), "config.yml" );
108+ if (!configFile .exists ()) {
109+ getConfig ().options ().copyDefaults (true );
110+ saveConfig ();
111+ }
112+
113+ config .load ();
114+
115+ claimsFile = new File (getDataFolder (), "most-recent-claims.yml" );
116+ loadClaimedIds (claimedIds , claimsFile , componentLogger );
117+
118+ reqTable = new RequestTable (this );
119+ noteTable = new NoteTable (this );
120+ listener = new ModReqListener (this );
121+ getServer ().getPluginManager ().registerEvents (listener , this );
122+
123+ // Command Initialization
124+ registry = new CommandRegistry ();
125+ checkCommand = new CheckCommand (this );
126+ claimCommand = new ClaimCommand (this );
127+ doneCommand = new DoneCommand (this );
128+ modreqCommand = new ModreqCommand (this );
129+ mrNoteCommand = new MRNoteCommand (this );
130+ mrResetCommand = new MRResetCommand (this );
131+ reopenCommand = new ReopenCommand (this );
132+ tpIdCommand = new TPIdCommand (this );
133+ unclaimCommand = new UnclaimCommand (this );
134+ elevateCommand = new ElevateCommand (this , unclaimCommand );
135+ tpInfoCommand = new TPInfoCommand (this , tpIdCommand , checkCommand );
136+ tpClaimCommand = new TPClaimCommand (this , claimCommand , tpIdCommand , checkCommand );
137+
138+ // Command Registration
139+ registry .register ("check" , checkCommand );
140+ registry .register ("claim" , claimCommand );
141+ registry .register ("done" , doneCommand );
142+ registry .register ("elevate" , elevateCommand );
143+ registry .register ("modreq" , modreqCommand );
144+ registry .register ("mr-note" , mrNoteCommand );
145+ registry .register ("mr-reset" , mrResetCommand );
146+ registry .register ("reopen" , reopenCommand );
147+ registry .register ("tpc" , tpClaimCommand );
148+ registry .register ("tp-id" , tpIdCommand );
149+ registry .register ("tpinfo" , tpInfoCommand );
150+ registry .register ("unclaim" , unclaimCommand );
151+ }
152+
153+ @ Override
154+ public void onDisable () {
155+ saveClaimedIds (claimedIds , claimsFile , componentLogger );
156+ }
157+
158+ public void setupDatabase () {
159+ try {
160+ connectionSource = new JdbcPooledConnectionSource (DATABASE_URL );
161+ TableUtils .createTableIfNotExists (connectionSource , Request .class );
162+ TableUtils .createTableIfNotExists (connectionSource , Note .class );
163+
164+ requestDao = DaoManager .createDao (connectionSource , Request .class );
165+ noteDao = DaoManager .createDao (connectionSource , Note .class );
166+
167+ } catch (SQLException ex ) {
168+ getLogger ().log (Level .INFO , "First run, please initialize database manually. Run the following:" );
169+ getLogger ().log (Level .INFO , """
170+ CREATE TABLE modreq_notes (
171+ id integer primary key,
172+ player_uuid varchar(40) not null,
173+ player varchar(255) not null,
174+ request_id integer not null,
175+ note_body varchar(255)
176+ );
177+ """ );
178+ getLogger ().log (Level .INFO , """
179+ CREATE TABLE modreq_requests (
180+ id integer primary key,
181+ player_uuid varchar(40) not null,
182+ player_name varchar(255) not null,
183+ assigned_mod_uuid varchar(40),
184+ assigned_mod varchar(255),
185+ request varchar(255),
186+ request_time bigint not null,
187+ status integer not null,
188+ request_location varchar(255) not null,
189+ close_message varchar(255),
190+ close_time bigint,
191+ close_seen_by_user integer(1),
192+ flag_for_admin integer(1),
193+ constraint ck_modreq_requests_status check (status in (0,1,2))
194+ );
195+ """ );
196+ }
197+
198+ }
199+
200+ public Dao <Request , Integer > getRequestDao () {
201+ return requestDao ;
202+ }
203+
204+ public Dao <Note , Integer > getNoteDao () {
205+ return noteDao ;
206+ }
207+
208+ public RequestTable getReqTable () {
209+ return reqTable ;
210+ }
211+
212+ public NoteTable getNoteTable () {
213+ return noteTable ;
214+ }
215+
216+ /**
217+ * The getter for the plugin's config instance
218+ * @return An instance of the plugin's configuration
219+ */
220+ public Configuration getConfiguration () {
221+ return config ;
222+ }
223+
224+ /**
225+ * The getter for the plugin's environment variables
226+ * @return An instance of the plugin's environment variables
227+ */
228+ public Map <String , String > getEnvironment () {
229+ return environment ;
230+ }
231+
232+ /**
233+ * The getter for the plugin's CompleteableFuture instance
234+ * @return An instance of the plugin's CompleteableFuture
235+ */
236+ public CompletableFuture <Void > getCompleteableFuture () {
237+ return future ;
238+ }
239+
240+ /**
241+ * The getter for the plugin's claimedIds hashmap
242+ * @return An instance of the plugin's claimedIds hashmap
243+ */
244+ public Map <UUID , Integer > getClaimedIds () {
245+ return claimedIds ;
246+ }
247+
248+ /**
249+ * The getter for the plugin's DATABASE_URL instance. This is a JDBC URL for database connectivity
250+ * @return The plugin's DATABASE_URL instance
251+ */
252+ public String getDatabaseUrl () {
253+ return DATABASE_URL ;
254+ }
255+
256+ // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
257+
258+ @ Override
259+ public boolean onCommand (CommandSender sender , Command command , String name , String [] args ) {
260+ if (!(sender instanceof Player )) {
261+ return false ;
262+ }
263+ CommandHandler handler = registry .getHandler (command .getName ());
264+ if (handler != null ) {
265+ environment .clear ();
266+ return handler .execute ((Player ) sender , name , args );
267+ }
268+ return false ;
269+ }
270+ }
0 commit comments