154 Commits
Author SHA1 Message Date
DarkChromaandClaude Opus 5.5 dffa04fc4c Recover DuckDB entity UUID conflicts without dropping or stalling (#1039)
A DuckDB unique-index conflict on co_entity_spawn.uuid can happen when the
identity row was committed after the consumer transaction's snapshot began
(or a delete is still visible to an older reader).

- Entity removals: on the exact duplicate-UUID constraint error, roll back,
  start a fresh transaction and retry once; reuse the identity that is now
  visible. If it is still unresolved, drop only that removal and continue,
  instead of retrying the same event forever and blocking the queue.
- Entity interactions: same refresh-and-retry, loading the existing identity
  so the interaction is recorded instead of dropped (#1002 only dropped it).
- Match the conflict on the event's own UUID, so other constraint errors are
  still retried and reported.
- Reopen the entity_spawn prepared statement when DuckDB closed it after a
  constraint violation.
- Summarize skipped records in one console warning every 5 minutes instead of
  a stack trace per event.

Refs #998

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 12:10:24 -06:00
Intelli 2d484dffff Cleanup 2026-10-18 12:29:19 -06:00
Intelli 38b6d87220 Fix DuckDB lookup memory usage and failure recovery 2026-10-18 12:29:06 -06:00
Intelli bc46794bba Fix shutdown queue draining and lookup pause coordination 2026-10-14 14:39:00 -06:00
Intelli 9d93f9ca44 Fix v24 patch dispatch for migrated DuckDB databases 2026-10-14 13:17:23 -06:00
Intelli 0e3577da0d Widen MySQL art columns to TEXT 2026-10-14 13:06:53 -06:00
Antti Koponen 61f0282596 Fix ClickHouse queue stalling from per-entity UUID lookups (#1038)
* Reload entity spawn lookups in bulk after each database commit

* Trim consumed prefixes after successful commit
2026-10-14 12:38:14 -06:00
Antti Koponen 55576f9c3a Fix memory leaks in CacheHandler due to iteration failures (#1037)
* Fix memory leaks in CacheHandler due to iteration failures

* Revert type cleanup
2026-10-14 12:36:22 -06:00
CloudMinde 5407e582a1 fixed co near permission check (#1036) 2026-10-12 16:28:00 -06:00
Intelli d76b254af4 Close DuckDB recovery gaps for allocation failures 2026-10-12 16:13:17 -06:00
Intelli e24d36bc82 Restore legacy interaction queue overload for addon compatibility 2026-10-12 15:54:28 -06:00
Intelli 616ed04ca7 Merge branch 'master' of github.com:PlayPro/CoreProtect 2026-10-12 15:48:04 -06:00
Intelli 2dc66e1464 Preserve LONGBLOB entity data during v25 migration 2026-10-12 15:47:40 -06:00
4b1e9ab334 Reduced entity kill data by skipping default attributes (#1028)
* Reduced entity kill data by skipping default attributes

EntityDeathListener stored every registered attribute of a killed mob,
about 23 entries on 1.21+, although almost all hold default values. The
attribute list made up about 88% of each kill blob.

Store an attribute only when its base value differs from the default or
it has modifiers. Kill data now always carries index 7 (UUID or null),
index 8 (kill location slot, null for mobs) and a format marker at index
9. When restore sees the marker, it first resets every attribute of the
respawned entity to its default base value and removes spawn-time
modifiers, then applies the stored attributes, so rollback reproduces
the logged entity exactly. Rows without the marker restore as before.

Capture and restore both use AttributeInstance#getDefaultValue. It is
deprecated since 26.1 for returning the server default rather than the
entity type default, but it exists on every supported version (1.16.5+)
and the same value is used on both sides.

Zombie kill blob: 2036 to 1027 bytes in the legacy format, and 250 to
141 bytes with EntityDataCodec (DuckDB and ClickHouse today, SQLite and
MySQL after the v26 migration). On SQLite that is 4096 to 1365 bytes per
kill on disk now, or 158 bytes after v26.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PdEuKbjcVoVinVQJq4te1f

* Restored left-out attributes from a frozen baseline

Restoring sparse kill data reset every attribute of the respawned entity
to the restoring server's defaults. An attribute the logging server did
not have yet was reset as well: a horse logged before 1.20.5 and
restored on 26.1 got the global step height, safe fall distance and fall
damage multiplier instead of the horse values.

AttributeUtils now holds a frozen baseline of 40 attribute values, taken
from the 26.2 registry defaults. Kill data leaves out an attribute only
when it is in the baseline, has the baseline value and has no modifiers,
and sets that attribute's bit in a long stored after the format marker
at index 10. Restore sets exactly the attributes with a bit to their
baseline values and leaves every other attribute as spawned. Entries
are only ever appended, so stored bits keep their meaning, and the
restoring server's defaults are never read. Rows without the marker
restore as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: TonyJamesStark <tonyjamesstark1@gmail.com>
2026-10-05 17:48:37 -06:00
d5db22c23e Entity radius purge (#1030)
* Fixed world purges leaving orphaned entity data

co_entity has no world column, so /co purge r:#world deleted the kill
rows in co_block but kept every co_entity row. Those blobs became
orphans that no later purge removed.

- SQLite: copy only the co_entity rows that a retained kill row still
  references. This also drops orphans left by earlier purges.
- MySQL/DuckDB: delete the co_entity rows of the kill rows a world purge
  removes, before co_block is purged. MySQL uses a join so MariaDB and
  MySQL 5.7 do not run a dependent subquery. Global purges keep the
  cheaper time-based delete, which is equivalent. If MySQL stops between
  the two deletes, running the same purge again removes the remaining
  kill rows.

Player kills use the kill action with type 0 and a user id in data, so
they are never treated as co_entity references.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PdEuKbjcVoVinVQJq4te1f

* Fixed SQLite purges with world and block filters purging other tables

On SQLite, /co purge r:#world i:<block> built the retain condition for
world-scoped tables without checking the block restriction, so it purged
co_container, co_chat and the other world-scoped tables in that world
although a block restriction should leave them untouched. The
entity_container/entity_interaction branch and the MySQL path already
check it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PdEuKbjcVoVinVQJq4te1f

* Fixed console purges with a radius purging all worlds

/co purge rejects a numeric radius, but the check only saw the radius
when the sender had a location. From the console, /co purge t:30d r:50
parsed no radius and no world, so it ran as a server-wide purge. The
radius is now also parsed against a placeholder location, so it is
rejected for every sender. r:#world is unaffected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PdEuKbjcVoVinVQJq4te1f

* Added orphaned entity data cleanup to MySQL #optimize purges

MySQL deletes in place, so co_entity rows orphaned by earlier world
purges stay until something removes them. With #optimize, delete every
co_entity row that no kill row references, through a temporary table of
referenced ids (avoids an anti-join on the unindexed co_block.data), then
let OPTIMIZE reclaim the space. Failures are reported like the table
loop, so the entity_spawn link cleanup still runs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PdEuKbjcVoVinVQJq4te1f

* Moved purge conditions into PurgeFilter

PurgeFilter now builds the purge condition for every table, and the
SQLite copy, the SQLite recovery path and the MySQL/DuckDB delete all use
it instead of three hand-built copies. Behavior is unchanged; the block
restriction check that the previous commit added to the SQLite copy is
part of the shared condition.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PdEuKbjcVoVinVQJq4te1f

* Added entity kill filters to purge command

/co purge rejected entity types and actions, so mob farm kill logs could
only be removed together with all other data. New arguments on SQLite,
MySQL and DuckDB:

- a:kill purges only entity kill rows.
- i:<entity> purges kills of those entity types; it can be combined with
  block types.
- e:<entity> keeps kills of those entity types while purging the rest.

Each purged kill also removes its co_entity row. a:kill with block types,
entity types in both i: and e:, e: with only block types in i:,
non-entity exclusions and entity filters on ClickHouse are rejected. Any
a: value other than a kill alias is rejected instead of falling through
to an unrestricted purge.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PdEuKbjcVoVinVQJq4te1f

* Added radius support to purge command

/co purge rejected any numeric radius, so kills of one entity type could
only be purged across a whole world. A radius is now accepted when the
purge is restricted to co_block rows with i: or a:kill, for example
/co purge t:30d r:50 i:zombie. r:50x10 also limits the height.

The radius becomes an x/y/z range in the co_block condition. co_entity
rows follow the purged kill rows, so no entity data is orphaned, and the
(wid,x,z,time) index already covers the range. Other tables are left
untouched, as with any restricted purge.

Rejected: a radius without i: or a:kill, WorldEdit selections, radius
purges on ClickHouse, and a numeric radius from the console (it has no
location, as in the console radius fix merged into this branch).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PdEuKbjcVoVinVQJq4te1f

* Made MySQL entity data purges safe against concurrent writers

The #optimize orphan sweep snapshots the co_entity ids that kill rows
reference, then deletes every row missing from the snapshot. Another
installation sharing the database could commit a kill between the two
statements and lose its entity data. The sweep now only deletes rows
inside the purge time range. A kill always writes its co_entity row with
the current time, and a purge range ends at least 24 hours ago, so a
concurrent kill never matches.

A world purge deletes the entity data of the purged kills and
then the kill rows. On MySQL a failure or cancellation between the two
left kept kill rows without their rollback data. Both deletes now run in
one transaction.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Limited SQLite entity data cleanup to the purge time range

A SQLite purge dropped every co_entity row no kill referenced, including
rows newer than the purge range, and counted them as deleted. It now only
removes unreferenced rows inside the range, like the MySQL #optimize sweep.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Rejected purge include and exclude lists without a value

/co purge t:30d e: passed validation, parsed to no exclusions, and ran
as an unrestricted time purge. e:, and e:,,, did the same, as did an
e: at the end of the command and an empty i:. The argument check now
follows each include or exclude list across its continuation tokens and
rejects a list that ends without a value. Space-separated lists such as
e:zombie, villager still work.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Matched purge entity filters to kills logged under renamed types

Bukkit resolves a legacy name such as zombie_pigman to the renamed type,
so the filter only matched the id of the new name. e:zombie_pigman then
purged the old kills it was meant to keep, and i:zombie_pigman missed
them. The filter now covers every entity map id that resolves to the type.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Rejected purges with more than one radius argument

/co purge t:30d r:50 r:#world_nether i:zombie ignored the explicit world
and purged around the player in their current world. Two r: arguments
always conflict, so the second one is now rejected before the purge
starts, whether it is a world, #global or another radius.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Applied max-radius to radius purges

A radius purge accepted any value. A radius past the int range saturated,
the area overflowed, and the purge reported success with nothing deleted.
It now stops at max-radius like lookups and rollbacks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Checked radius arguments inside purge include and exclude lists

/co purge t:30d i:zombie, r:50 r:#world_nether passed the second-radius
check: the validator took r:50 as a value of the include list, then saw
r:#world_nether as the only radius. The radius and world parsers read
both. A list now continues only with plain or minecraft: namespaced
values, so a radius after a trailing comma is counted and the second one
is rejected. This is the same change as 39deec3 on entity-kill-purge-filters.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LFQuzYmYun57wJi2a5mqkM

---------

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: TonyJamesStark <tonyjamesstark1@gmail.com>
2026-10-05 17:46:22 -06:00
Intelli 1490dc7c89 Reduce ClickHouse consumer payload retention 2026-10-04 11:05:16 -06:00
Intelli af78ea00d4 Added a:sign support for rollbacks/restores 2026-10-02 17:29:11 -06:00
Intelli 4c8038cd26 Fixed BatchUpdateException for 'meta' column on outdated MySQL schemas 2026-10-02 15:50:02 -06:00
tonyjamesstarkandClaude 29d22d5a58 Fixed console purges with a radius purging all worlds (#1026)
/co purge rejects a numeric radius, but the check only saw the radius
when the sender had a location. From the console, /co purge t:30d r:50
parsed no radius and no world, so it ran as a server-wide purge. The
radius is now also parsed against a placeholder location, so it is
rejected for every sender. r:#world is unaffected.


Claude-Session: https://claude.ai/code/session_01PdEuKbjcVoVinVQJq4te1f

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-02 13:58:53 -06:00
tonyjamesstarkandClaude 4e70d63978 Fixed SQLite purges with world and block filters purging other tables (#1025)
On SQLite, /co purge r:#world i:<block> built the retain condition for
world-scoped tables without checking the block restriction, so it purged
co_container, co_chat and the other world-scoped tables in that world
although a block restriction should leave them untouched. The
entity_container/entity_interaction branch and the MySQL path already
check it.


Claude-Session: https://claude.ai/code/session_01PdEuKbjcVoVinVQJq4te1f

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-02 13:58:17 -06:00
CloudMindeandIntelli 00e7b3819b Add #sign block-group (#1024)
* Add #sign block-group

* Fix #sign group: use singular name, version-aware detection, remove TRACK_ANY

---------

Co-authored-by: Intelli <6790859+Intelli@users.noreply.github.com>
2026-10-02 13:55:11 -06:00
Sage Kummer 079b97405b Stop threads and subscribers that outlive a plugin disable (#1020)
* Stop threads and subscribers that outlive a plugin disable

The WorldEdit subscriber, the cache sweeper thread, per-click inspector threads and the bStats scheduler all kept running after CoreProtect was disabled, and the Folia shutdown checkpoint could wait 30 seconds on tasks that never run. Shutdown now unregisters and stops each of them, inspector lookups use a small named pool, and the checkpoint waits 5 seconds.

* Recreate inspector executor on enable and account for shutdown leftovers

The inspector executor was a static final that stayed shut down after a disable, so a same-instance enable could never run another lookup. It is now created on each enable. At shutdown, lookups still queued after the drain window release their throttle slot, since shutdownNow() returns them without running their finally block, and running lookups that survive the interrupt are logged by thread and player after a bounded wait. The Folia checkpoint timeout change is reverted to keep this change to thread cleanup.
2026-10-02 13:34:34 -06:00
Sage Kummer 48c9bf3c0f Apply Folia inventory rollbacks on each player's scheduler (#1018)
* Apply Folia inventory rollbacks on each player's scheduler

Inventory rollbacks run in the region that owns chunk 0,0 and changed every player's inventory from there, which Folia does not allow. On Folia the rows are now grouped per player and applied on that player's entity scheduler, the batch waits for those tasks, and the rollback counters are updated atomically. Paper and Spigot are unchanged.

* Run Folia inventory tasks as mutations of the rollback context

The per-player inventory tasks now go through the rollback's entity context. A task checks for cancellation before it changes anything, runs as an active mutation so cleanup waits for it to finish, and adds its item count to that context instead of the shared counter for the sender, so a late task cannot change a rollback that already ended or a newer one from the same sender. The counter helpers go back to their upstream form since player tasks no longer write to them.
2026-10-02 13:34:22 -06:00
Sage Kummer 804e398eb1 Read bisected halves through the WorldEdit edit, not the live world (#1016)
* Read bisected halves through the WorldEdit edit, not the live world

When an edit removes a door or tall plant, the other half was read from the live world on the edit thread, which does not own the chunk and can see blocks the edit already changed. The half is now read through the edit extent, or the FAWE processor's chunk arrays, and carried on WorldEditBlockState. The type list Queue checked inline moves to BlockGroup.LOGGED_BY_LOWER_HALF so both sides use the same one.

* Capture the other bisected half before the WorldEdit change applies

CoreProtect's extent sits at BEFORE_CHANGE, below WorldEdit's batching, so its reads reach the live world, and both the lower half and the removed partner were read after setBlock(). Read the other half before setBlock() instead, and only use it when it is the same material with the opposite half, so an unrelated block next to a removed half is no longer logged from block data alone. A door or tall flower is already logged by its lower half, so its partner no longer gets a second, duplicate row. The FAWE lower half is validated the same way.
2026-10-02 13:34:11 -06:00
Sage Kummer 18643de5a1 Make /co reload swap world configs and leave block groups alone (#1015)
* Make /co reload swap world configs and leave block groups alone

The per-world config map was a plain HashMap that getConfig wrote into on a miss, and reload cleared it before refilling, so a listener hitting that window could leave a world on the global config until the next reload. The map is now concurrent and read-only for listeners, and reload swaps new configs in without a gap. Reload also stops rebuilding the version adapters and block groups, which only depend on the server version, while listeners read them.

* Rebuild adapters and block groups on /co reload again

Several block groups are filled from datapack tags (buttons, pressure
plates, doors, saplings, flowers, carpets, logs and more), so they are
not fixed by the server version. Running the adapters and
BlockGroup.initialize() only at startup would leave those groups stale
after a datapack reload until the server restarts. Restore the previous
behaviour and keep this change to the world config map.
2026-10-02 13:34:00 -06:00
Sage Kummer 2863de7f30 Fix races on the pending item transaction lists (#1013)
* Fix races on the pending item transaction lists

Item listeners appended to the shared per-player lists with get, add and put while the consumer read and then cleared them, so items could be logged twice or not at all, and the anvil path replaced pending items outright. Listeners now append inside ConcurrentHashMap.compute and the consumer takes each list with remove. The prepared copy is kept for every database type so a retried relational batch still writes its rows.

* Close discard and clone-failure gaps in pending item transactions

Listeners now append their item and register its generation in one
Queue.addPendingItems call that holds the Queue lock, the same lock
ItemTransactionProcess.discard takes, so discard can no longer clear an
item whose transaction has not been queued yet. ItemLogger.prepare now
only detaches the pending lists; cloning and merging move into
PreparedTransaction.log, which runs after the prepared transaction is
stored for the batch, so a cloning failure leaves the detached items
available to the retried transaction.
2026-10-02 13:32:56 -06:00
Sage Kummer 80caab5938 Restore paintings and item frames on Folia without teleporting (#1011)
* Restore paintings and item frames on Folia without teleporting

Rollback spawns a painting or item frame and then teleports it into place, but Folia always throws from Entity#teleport, so paintings are lost and frames never get their recorded facing or item. On Folia the entity is now spawned directly at its recorded block with its facing, art or item set in the pre-spawn consumer. Paper and Spigot are unchanged.

* Spawn Folia hangings through the adapter on 1.19.4 and 1.20.1

Compiling against the current API binds World#spawn(Location, Class,
Consumer) to the java.util.function.Consumer overload, which only exists
from Bukkit 1.20.2. Folia 1.19.4 and 1.20.1 only have the
org.bukkit.util.Consumer overload, so restoring a painting or item frame
there threw NoSuchMethodError. The spawn now goes through
BukkitAdapter.spawn, which calls the java.util.function.Consumer overload
when World has it and otherwise invokes the org.bukkit.util.Consumer
overload by reflection.
2026-10-02 13:32:46 -06:00
Sage Kummer 4274f88d0a Log armor stand breaks from the retired callback on Folia (#1010)
* Log armor stand breaks from the retired callback on Folia

When an armor stand dies from non-player damage, the break and its equipment are logged from a task on the stand's scheduler. On Folia the stand is removed in the same tick, so the task is retired instead of run and nothing is logged. The same runnable is now passed as the retired callback, so exactly one of them logs the break.

* Log armor stand breaks from EntityDeathEvent instead of isDead

The damage listeners now only capture the attacker and the stand's
contents, and EntityDeathEvent logs the break when the stand actually
dies. The scheduled task and Folia's retired callback just discard a
capture that was never used, so a stand that survived the hit and was
unloaded or otherwise removed before the callback ran no longer
produces break and container rows, and the retired callback never
touches the world.
2026-10-02 13:32:30 -06:00
Sage Kummer 2f50109bdd Materialize the MySQL sign text filter as a derived table (#1008)
* Materialize the MySQL sign text filter as a derived table

MySQL cannot semi-join the rowid IN (UNION ...) subquery the sign text filter builds, so it runs it as a dependent subquery, re-probing all eight branches for every candidate row and never using the line_N prefix indexes. Wrapping the union in a derived table lets MySQL materialize it once through those indexes. Other databases build the same query as before.

* Inline the MySQL sign text filter so restricted lookups stay indexed

The derived table materialized every sign row matching the text across the whole table before the location, time and user restrictions were applied, so a common prefix at one sign location scanned the full table. MySQL now gets the same inline line predicates DuckDB already uses: restricted lookups filter the rows found through the wid, time or user index, and unrestricted lookups still reach the line prefix indexes through an index merge.

* Split the sign filter comment into two sentences
2026-10-02 13:32:05 -06:00
Intelli 6e9c8302fa Name DuckDB workers to avoid Spark profile misattribution 2026-10-02 13:17:27 -06:00
Intelli 7582ab4d8e Added logging for cushions (fixes #1023) 2026-09-28 22:27:54 -06:00
Intelli ac94648185 Fixed copper golem transaction logging on newer Paper versions (fixes #1033) 2026-09-28 18:52:32 -06:00
Intelli 6a27fbfaba Fixed ClickHouse 26.9+ initialization failure caused by server response compression (fixes #1032) 2026-09-28 18:29:18 -06:00
Intelli d5cad31d6c Optimize ClickHouse player lookups with a metadata index (fixes #1031) 2026-09-27 00:18:18 -06:00
Intelli e518b2ae02 Optimized ClickHouse lookups with safe PREWHERE filters (fixes #1031) 2026-09-25 20:51:18 -06:00
Intelli 0d96edbf2b Changed required ClickHouse version to v26.1 2026-09-25 15:40:28 -06:00
Sage Kummer bdc53a1250 Key the explosion scan by packed block position (#1021)
The explosion scan cloned a Location for every block and built five more per block to find attached neighbours, hashing each one on the region thread. It now keys the map by a packed long position and walks the neighbours from an offset table. The same blocks are logged.
2026-09-25 15:12:42 -06:00
Sage Kummer 35f64f09a4 Close lookup ResultSets on exception paths (#1019)
Lookup.countLookupRows and LookupRaw.performLookupRaw closed their ResultSet as the last statement of the try, so any error while reading rows left it, and the statement behind it, open until the connection was returned. Both now close it on every path.
2026-09-25 15:11:38 -06:00
Sage Kummer fc5a12c0b7 Key populated chunks by world and drop them after 240 seconds (#1017)
populatedChunks had no world in its key, so a chunk generated in one world suppressed water, lava and vine rows at the same chunk coordinates in every other world, and entries were only removed when a later fluid row happened to land there. It is now kept per world and swept after 240 seconds, the longest window BlockPlaceLogger checks.
2026-09-25 15:10:55 -06:00
Sage Kummer 335fec2036 Skip a null tamed owner name when restoring an entity (#1012)
A tamed animal whose owner has no known name is stored with a null owner, and restoring it throws a NullPointerException at the owner step, which drops the rest of the restore. A null owner name is now skipped like an empty one.
2026-09-25 15:10:27 -06:00
Sage Kummer 72d5655ee9 Skip matchMaterial's regex for enum names and cache blockdata encodings (#1022)
The consumer ran Material.matchMaterial's two regex passes on names they cannot change, and re-encoded the same blockdata strings into ids on every row. Plain enum names now go straight to getMaterial, and stringToByteData caches its results per string, bounded, reset when the id maps reload and skipped on ClickHouse.
2026-09-25 15:08:11 -06:00
Sage Kummer 97fced4cb6 Allocate world, material, blockdata, art and entity ids under one lock (#1009)
The id allocators read the counter, add one and store it without a lock, so two threads that see new names at the same time (region threads on Folia, lookup and API threads everywhere) can give the same id to two different names. Allocation now holds the ConfigHandler monitor reloadAndGetId already uses, with a re-check, and queues the insert after releasing it.
2026-09-25 14:56:30 -06:00
Sage Kummer 96eec2b768 Skip block entity snapshots on the dispenser logging path (#1007)
Every dispense copied the dispenser's block entity twice on the region thread (block.getState() in the dispense listeners and inventory.getHolder() in onInventoryInteract), and both copies were only used for their type, location and live inventory. Paper can hand out non-snapshot states for this, so the dispense path now asks for those through a new PaperInterface.getBlockState(Block, boolean), falling back to a normal snapshot on Spigot.
2026-09-25 14:12:34 -06:00
Intelli bba6f9f522 Improved DuckDB startup exception 2026-09-25 13:22:08 -06:00
Intelli 3314d993a6 Bump API documentation to v13 2026-09-24 15:57:17 -06:00
Intelli 68739a2596 Target Paper 26.3 2026-09-23 22:37:10 -06:00
Intelli 4e5ddd2334 Improve rollback diagnostics and abort failed database initialization 2026-09-23 20:12:06 -06:00
Intelli bbe5917ec5 Fix entity container rollbacks aborting on missing items or full inventories 2026-09-23 17:42:44 -06:00
Intelli 78fbf12d9e CoreProtect Community Edition v24.1 2026-09-23 14:37:50 -06:00
Intelli 56ba90b58b Align ClickHouse lookup pagination with DuckDB 2026-09-23 13:59:32 -06:00
Intelli 91f08590d0 Align ClickHouse entity inspection pruning with DuckDB 2026-09-23 13:39:58 -06:00
Intelli 3af1079b51 v13 API documentation improvements 2026-09-21 13:15:49 -06:00
Intelli 37be5ebf15 Updated v13 API documentation 2026-09-21 12:59:07 -06:00
Mykhailo Alipa 5db696103a feat(api): add message prefix filters to typed lookups (#1001)
* feat(api): add message prefix filters to typed lookups

* refactor(api): reuse existing message filter helper directly

* chore: remove test file from message filter PR

* feat(api): make message prefix exclusions explicit

* refactor(api): name text prefix filters by their predicates
2026-09-21 12:35:23 -06:00
Intelli 2f719054eb Fixed BatchUpdateException / sign data truncation for MySQL databases 2026-09-19 12:52:18 -06:00
Intelli 46b56d577a Fix DuckDB entity interaction failure recovery 2026-09-19 12:49:46 -06:00
DarkChroma 0ec76561dc Prevent DuckDB entity interaction queue stalls (#1002) 2026-09-19 12:37:10 -06:00
Intelli 9cf12ee5bf Prevent DuckDB entity UUID conflicts with active readers 2026-09-18 14:14:40 -06:00
Intelli 5318bd2031 Fix duplicate right-click inspections triggering database busy messages (#1000) 2026-09-18 13:33:49 -06:00
Intelli 94ecdb3427 Prevent duplicate UUID inserts during delayed entity spawn logging 2026-09-17 17:14:27 -06:00
Intelli d664ece2a8 Fixed UUID cache gap 2026-09-17 14:07:47 -06:00
Intelli 11b4795bf3 Updated documentation 2026-09-16 15:36:25 -06:00
Mykhailo Alipa 45da5eee0c feat(api): extend typed block lookups and add entity lookup (#995)
* feat(api): add material filters to typed block lookup

* feat(api): support scoped typed block lookups

* refactor(api): simplify block material predicates

* feat(api): add typed entity lookup and filters

* fix(api): separate block history and optimize entity scopes

* fix(api): cast ClickHouse entity tracking IDs safely
2026-09-15 13:13:52 -06:00
Intelli 645bdb52d8 config.yml cleanup 2026-09-14 19:17:47 -06:00
Thoria 0ac4761a3a Add dispensers config option to disable dispenser logging (fixes #727) (#989)
* Added dispensers config option to disable dispenser logging (fixes #727)

* Only suppress dispenser-caused bone meal and ignition

* Only track bone meal applied by a dispenser

* Identify dispenser fireball ignition by its shooter

* Clarify the dispensers config header
2026-09-14 19:14:57 -06:00
DHEnry-FMCRAFT 678b27c714 fix: 修复CoreProtect与Slimefun物品meta空指针报错 (#886) 2026-09-14 17:24:28 -06:00
Intelli 5e036528a9 Extend big_dripleaf fix to redstone ores 2026-09-14 16:25:54 -06:00
Intelli dbcdbd853d Merge branch 'master' of github.com:PlayPro/CoreProtect 2026-09-14 16:14:08 -06:00
Intelli 9e3a1ec546 Hardened legacy metadata decoding 2026-09-14 16:13:35 -06:00
Mykhailo Alipa 58587ed671 feat(api): add block and session action filters (#985) 2026-09-14 15:14:15 -06:00
Intelli 79ba793626 Fixed #summary not always returning correct row counts (fixes #991) 2026-09-14 13:07:01 -06:00
Intelli 852574db6b Merge branch 'master' of github.com:PlayPro/CoreProtect 2026-09-14 12:35:27 -06:00
Intelli 2765bf502a Don't require time value if radius is specified (fixes #992) 2026-09-14 12:35:15 -06:00
Splosh 1db3a39711 Recognize modern Paper platform marker (#990) 2026-09-12 21:27:03 -06:00
Intelli 6b1de1b947 Improved shutdown logic for container transaction logging 2026-09-12 21:21:14 -06:00
Intelli 2251c758ac Additional attribute metadata improvements 2026-09-11 15:06:05 -06:00
Intelli 9dd653c72b Improved legacy attribute metadata handling (fixes #986) 2026-09-10 16:51:50 -06:00
Intelli ac20d00d62 Avoid getState calls in PlayerInteractListener to improve performance (fixes #984) 2026-09-10 16:16:21 -06:00
Emir a27026c7a6 Avoid unnecessary regex compilation in hot paths (#951) 2026-09-10 12:41:34 -06:00
Intelli 3e04b389f2 Updated en.yml 2026-09-09 19:11:56 -06:00
Intelli d93827f184 Added exclusion support to lookup filters 2026-09-09 18:40:49 -06:00
Intelli ce556227c0 WorldEdit logging improvements 2026-09-09 18:08:15 -06:00
Intelli 6a40286a59 FAWE performance improvements 2026-09-09 17:57:30 -06:00
Intelli f55bb32cbf Fixed a:+item not returning lookup results (fixes #962) 2026-09-09 17:00:05 -06:00
Intelli 5b0e4c3f7a Disabled unnecessary logging of big_dripleaf state changes (fixes #982) 2026-09-09 16:49:13 -06:00
Intelli 17a9cb5a4a Fixed hopper minecarts logging incorrect item amounts 2026-09-09 16:32:27 -06:00
Intelli d58bf0732d Fixed left-click inspector lookups on boats/minecarts 2026-09-09 15:42:41 -06:00
Intelli 9e1b74a49c Fixed entity name formatting in lookups 2026-09-09 15:22:51 -06:00
Intelli 8f6de4d2af Updated en.yml 2026-09-09 15:16:14 -06:00
dependabot[bot] af4296c1e6 Bump org.apache.logging.log4j:log4j-api from 2.25.4 to 2.25.5 (#955)
Bumps org.apache.logging.log4j:log4j-api from 2.25.4 to 2.25.5.

---
updated-dependencies:
- dependency-name: org.apache.logging.log4j:log4j-api
  dependency-version: 2.25.5
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2026-09-09 15:15:39 -06:00
Mykhailo Alipa 06863f197c feat(api): add action filters to typed transaction lookups (#983)
* feat(api): add typed transaction action filters

* docs(api): clarify action filters for API consumers
2026-09-09 14:08:54 -06:00
Intelli e263556f70 Additional item_name improvements 2026-09-08 23:37:20 -06:00
Daniel Škopek 867fb3d0f3 Added support for item_name (#963) 2026-09-08 23:30:01 -06:00
Intelli 37a67f2594 Fixed ClickHouse location query pruning while preserving NULL values 2026-09-08 23:07:14 -06:00
Intelli 36b30bc5f3 Improved lookup performance on empty DDB pages 2026-09-08 22:47:22 -06:00
Intelli 5b012504a5 Improved metadata conversion handling 2026-09-08 22:46:43 -06:00
Intelli 0944d31357 Fixed command throttling logic 2026-09-08 20:47:09 -06:00
Intelli 464aabbd1e Improved custom database filename logic 2026-09-08 20:33:04 -06:00
Dainsleif f0f355f1ba Add hidden sqlite-database config option to customize the SQLite database filename. (#956) 2026-09-08 20:24:26 -06:00
Intelli 7dd70c45d5 Fixed ClickHouse user ownership and consumer failure handling 2026-09-08 20:23:41 -06:00
Intelli 548acc16dd Fixed ClickHouse event retention and identity reuse, and DuckDB indexing 2026-09-08 17:54:59 -06:00
Intelli 6a5c46673c Fixed mixed-case UUID intersections in username API lookups 2026-09-08 17:13:11 -06:00
Mykhailo Alipa edea5f105b feat(api): add user filters to typed lookups (#980)
* feat(api): add user filters to typed lookups

* docs(api): consolidate lookup filter options

* refactor(api): extract username inclusion filter resolution
2026-09-08 17:07:12 -06:00
Mykhailo Alipa e97878927e feat(api): add material filters to typed lookups (#979)
* feat(api): add typed material lookup filters

* fix(api): match normalized lookup materials

* fix(api): share stored material name resolution
2026-09-08 14:22:39 -06:00
Linwendong83 9b57624726 Fix 30-second shutdown delay on Folia (#978) 2026-09-08 14:15:33 -06:00
Intelli d9f728d917 Additional lookup optimizations 2026-09-08 13:38:15 -06:00
Intelli 6138dffe7a Fixed inspector not preventing damage to boats/minecarts 2026-09-08 13:19:56 -06:00
Intelli 2405e7cf76 Merge branch 'master' of github.com:PlayPro/CoreProtect 2026-09-08 13:18:43 -06:00
Intelli d2fb0bd092 Looking performance improvements 2026-09-08 13:18:32 -06:00
Mitra b95fb65dde chore(build.yml): update actions/checkout and actions/setup-java to latest versions (#975) 2026-09-05 07:39:09 -06:00
Intelli 335e1deca1 Fixed FAWE bulk logging compatibility and ClickHouse NULL locations 2026-09-03 16:09:35 -06:00
CheekyNox 6d74208f6a Fix ClickHouse lookup pagination (#958) 2026-09-03 15:56:14 -06:00
Telesphoreo fa7ed2a397 Reduce ClickHouse lookup memory use and scan time (#966)
* Optimize ClickHouse lookup queries

* Separate ClickHouse inspector queries

* Keep inspector queries on compatibility views

* Fix WorldEdit bulk change logging
2026-09-03 15:54:51 -06:00
Intelli aa788e454c Added logging for zombie-nautilus variant (fixes #968) 2026-09-03 14:48:45 -06:00
Intelli eb3866cc28 Harden DuckDB indexing and recovery 2026-09-02 23:34:45 -06:00
Intelli 834281d5f3 Fixed ClickHouse sign write failure (fixes #961) 2026-08-25 16:34:09 -06:00
Intelli b1edbbbf02 Fixed SQLite rowid issues 2026-08-20 15:58:26 -06:00
Ben e3be1520fa Set prefer_column_name_to_alias on ClickHouse connections (#948)
ClickHouse resolves identifiers to select-list aliases before table
columns by default, so lookup queries that reuse a column name as an
alias (e.g. 'data as metadata, 0 as data' in the item lookup) return
the alias literal instead of the column. Item lookups then return
metadata as UInt8 0, mixed lookup unions produce
Variant(Array(Int8), UInt8), and the JDBC client fails every /co
lookup with 'Column is not of array type'.

Setting prefer_column_name_to_alias=1 restores the alias semantics
these queries rely on under SQLite and MySQL.

Fixes #947
2026-08-18 13:46:57 -06:00
Intelli 29f25cf1de Fixed CraftLegacy initialization when using CraftEngine (fixes #957) 2026-08-18 13:23:54 -06:00
Intelli 8adde82882 Ensure user UUID data is never outdated 2026-08-17 13:33:46 -06:00
Intelli f2eb0b0331 Improved multi-server support for columnar storage 2026-08-16 14:46:15 -06:00
Intelli 0bb1d5dcb7 Cleanup 2026-08-11 13:44:55 -06:00
Intelli d47457fa90 Custom block metadata serialization 2026-08-11 13:17:42 -06:00
Intelli 078adf535f Improved entity serialization 2026-08-10 19:18:57 -06:00
Intelli 49262d9e28 Custom entity serialization (WIP) 2026-08-06 21:41:44 -06:00
Intelli 5ff8072ddc Fixed NoSuchMethodError on PurPur servers 2026-08-05 19:33:24 -06:00
Intelli a34e56e9d6 Lookup performance improvements 2026-08-01 10:25:58 -06:00
Intelli b745a6099a Added logging for chicken, cow, and pig visual variants 2026-07-31 15:57:29 -06:00
Intelli 2a9f1234b3 Cap villager level at 5 during rollbacks 2026-07-28 16:27:48 -06:00
Intelli eefdd95776 Fixed race when processing container transactions 2026-07-28 15:46:31 -06:00
Intelli b0b14cd270 Fixed NoClassDefFoundError on Spigot 26.2 servers 2026-07-20 17:16:28 -06:00
Intelli a95c0f93b0 Improved shutdown handling 2026-07-19 23:09:57 -06:00
Intelli ccb7d5d85c Fixed entity lifecycle race 2026-07-19 17:59:08 -06:00
Intelli 79d904af88 Additional entity identity persistence improvements 2026-07-19 16:45:24 -06:00
Intelli 43a8986ca8 Fixed race condition and shutdown hang 2026-07-19 15:37:43 -06:00
Intelli 6d1a4ac520 Harden entity interaction persistence and identity tracking 2026-07-19 09:02:05 -06:00
Intelli 438d6680a8 Fix entity interaction persistence and identity tracking 2026-07-18 14:43:30 -06:00
Intelli 3cb2f205d9 Fixed dispenser logging creating false block-break logs 2026-07-17 14:43:07 -06:00
Intelli fda960a18c Improved internal logic for migration handling 2026-07-16 20:21:57 -06:00
Intelli 6731831283 Added columnar storage options 2026-07-16 14:11:40 -06:00
purpurcofandIntelli 130bb803e3 Update and polish Russian localization (#929)
* i18n(ru): update and polish Russian localization

* i18n(ru): fix typography and restore standard hyphens

* Reorder lookup header in Russian translations

---------

Co-authored-by: Intelli <6790859+Intelli@users.noreply.github.com>
2026-07-12 22:48:32 -06:00
Intelli bca5fe5305 Fixed #count being accepted for rollbacks/restores 2026-07-12 22:46:23 -06:00
Intelli aa2d0a1e46 Added rollback-state lookup filters 2026-07-12 22:26:35 -06:00
Intelli 197e5b34a4 Additional hopper transaction logging hardening 2026-07-12 18:57:11 -06:00
Intelli 3d1594e116 Added entity interaction logging 2026-07-12 17:32:35 -06:00
Intelli c25621622e Dropped unnecessary guards for #count 2026-07-12 14:13:23 -06:00
Intelli 7ad4a77149 Hardened hopper transaction logging 2026-07-12 13:05:52 -06:00
Intelli e4c0ea88df Added filtering support for a:sign 2026-07-12 13:04:14 -06:00
Intelli 39f66412d6 Enhanced #count to display summary information (implements #926) 2026-07-12 01:09:18 -06:00
Intelli bab8dca58a Fixed missing parameters in tab completion 2026-07-11 19:31:22 -06:00
Intelli d95e70eebb Fixed risk of NPE when logging container transactions 2026-07-11 19:00:24 -06:00
Intelli 3151a25f3e Added entity spawn, movement, & transaction logging 2026-07-11 16:21:01 -06:00
Intelli 52a0e45c61 Minor improvements to filter logic 2026-07-09 17:04:02 -06:00
Intelli 956828b5d0 Added message filtering 2026-07-09 15:48:12 -06:00
311 changed files with 35680 additions and 3862 deletions

No files matched your search

+2 -2
View File
@@ -10,11 +10,11 @@ jobs:
name: Build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v7.0.1
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: Set up JDK 25
uses: actions/setup-java@v4
uses: actions/setup-java@v6
with:
java-version: '25'
distribution: 'temurin'
+1
View File
@@ -181,6 +181,7 @@ CLAUDE.md
.clinerules
context.json
context.yml
issues.md
run.sh
run_*.sh
no-log4j2.xml
+2 -2
View File
@@ -41,7 +41,7 @@ Maven:
<dependency>
<groupId>net.coreprotect</groupId>
<artifactId>coreprotect</artifactId>
<version>24.0</version>
<version>24.1</version>
<scope>provided</scope>
</dependency>
```
@@ -65,7 +65,7 @@ To support the project and obtain a donation key, visit [coreprotect.net/donate]
* Rollbacks and restores with lag-free processing.
* Multi-threaded to ensure server performance is never impacted.
* No configuration required. Install the plugin, and you're good to go.
* SQLite storage by default, with optional MySQL support.
* Embedded DuckDB columnar storage by default, with optional ClickHouse, SQLite, and MySQL support.
* Supports Bukkit, Spigot, Paper, Folia, MultiPaper, and more.
* Permission system support and advanced lookup permissions.
* Easy-to-use commands and inspector.
+3 -3
View File
@@ -4,8 +4,8 @@ The CoreProtect API enables you to log your own block changes, perform lookups,
| API Details | |
| --- | --- |
| **API Version:** | 12 |
| **Plugin Version:** | v24.0+ |
| **API Version:** | 13 |
| **Plugin Version:** | v25.0+ |
| **Maven:** | [maven.playpro.com](https://maven.playpro.com) |
*Documentation for the API version 12 can be found [here](/api/version/v12/).*
*Documentation for the API version 13 can be found [here](/api/version/v13/).*
+964
View File
@@ -0,0 +1,964 @@
# API Version 13
The CoreProtect API enables plugins to log changes, look up recorded activity, and perform rollbacks and restores.
| API Details | |
| --- | --- |
| **API Version** | 13 |
| **Plugin Version** | v25.0+ |
| **Maven** | [maven.playpro.com](https://maven.playpro.com) |
## Upgrading from API v12
All API v12 methods remain available. API v13 adds the following:
| Addition | Description |
| --- | --- |
| `blockLookup(LookupOptions)` | Searches block breaks, placements, and interactions at an exact location, within a radius, in a world, or across all worlds. |
| `entityLookup(LookupOptions)` and `EntityResult` | Searches entity spawns and kills, with action and entity-type filters. |
| `LookupOptions.world(World)` | Selects a whole world for spatial typed lookups. |
| User and material lists | Adds `users`, `excludeUsers`, `includeMaterials`, and `excludeMaterials` to typed lookup options. |
| Typed action filters | Adds `blockActions`, `entityActions`, `containerActions`, `itemActions`, `inventoryActions`, and `sessionActions`. |
| Text prefix filters | Adds `textStartsWithAny` and `textStartsWithNone` for chat, command, and sign lookups. |
| Entity-spawn action | Adds `CoreProtectAction.ENTITY_SPAWN` with ID `13`. Legacy lookup, rollback, and restore methods accept this action explicitly. |
| Entity result parsing | `BlockResult` and `ParseResult` expose `getEntityType()` for entity kills and spawns. Their material and block-data getters return `null` for entity rows. |
| Tracked containers | Container and inventory lookups include transactions in tracked boats and minecarts. |
| Pre-log events | Adds `CoreProtectPreLogEvent.Action.ENTITY_SPAWN` and `ENTITY_INTERACTION`. |
The existing `blockLookup(Block, LookupOptions)` overload retains entity events for compatibility. The new `blockLookup(LookupOptions)` overload returns only block events; use `entityLookup` for entities.
Entity and tracked-container searches can match persisted current/final positions as well as original event positions. See [entity lookups](#entity-lookups) and [container lookups](#container-lookups) for the coordinates returned by each method.
## Getting Started
CoreProtect 25.0 or higher is required to use API v13.
### Add the dependency
Compile against a CoreProtect build that provides API v13. You can add the CoreProtect jar to your IDE or use the Maven repository:
```xml
<repositories>
<repository>
<id>playpro</id>
<url>https://maven.playpro.com</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>net.coreprotect</groupId>
<artifactId>CoreProtect</artifactId>
<version>25.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
```
Use the published version matching the CoreProtect build you target. For prerelease builds, use that build's published version instead of `25.0`. CoreProtect runs as a separate server plugin; do not package its classes inside your plugin.
Add CoreProtect to your plugin's `plugin.yml` so it loads first:
```yaml
depend: [CoreProtect]
```
Use `softdepend: [CoreProtect]` instead if your plugin also works without CoreProtect, and only initialize the integration when CoreProtect is present.
### Obtain the API
The following helper belongs in your `JavaPlugin` class:
```java
import org.bukkit.plugin.Plugin;
import net.coreprotect.CoreProtect;
import net.coreprotect.CoreProtectAPI;
private CoreProtectAPI getCoreProtect() {
Plugin plugin = getServer().getPluginManager().getPlugin("CoreProtect");
if (!(plugin instanceof CoreProtect)) {
return null;
}
CoreProtectAPI api = ((CoreProtect) plugin).getAPI();
if (!api.isEnabled() || api.APIVersion() < 13) {
return null;
}
return api;
}
```
Call it during your plugin's initialization:
```java
CoreProtectAPI api = getCoreProtect();
if (api != null) {
api.testAPI();
}
```
Options and action enums are in `net.coreprotect.api`. Typed result classes are in `net.coreprotect.api.result`. Events are in `net.coreprotect.event`.
### Threading and return values
Database lookups run synchronously on the calling thread. Run database searches asynchronously to avoid blocking the server thread. Both `performRollback` and `performRestore` **must** be called asynchronously; CoreProtect schedules the required world updates internally.
Capture locations and any required Bukkit state on the appropriate server thread before starting database work. Logging methods that inspect live blocks, players, or inventories should run on that server thread. A `BlockState` overload does not make every logging operation safe to call asynchronously.
Typed lookups and the legacy `blockLookup`, `performLookup`, `performPartialLookup`, `performRollback`, and `performRestore` methods return `null` when the API is disabled. `queueLookup` and the legacy `sessionLookup(String, int)` instead return an empty list. Legacy searches can also return `null` for invalid search parameters. An empty list can mean no matching records or an operation that could not proceed; it is not a separate success status. Logging methods return a boolean indicating whether the request was accepted, not whether it has already been written to the database.
API rollback and restore methods perform no changes when database maintenance or another conflicting operation prevents them from starting. This includes an active database purge.
## API Overview
### Available methods
The table below lists the supported methods on `CoreProtectAPI`. Click a method to jump to its parameters and behavior.
| Method | Return type | Purpose |
| --- | --- | --- |
| [`APIVersion()`](#apiversion) | `int` | Returns `13` for this API version. |
| [`isEnabled()`](#isenabled) | `boolean` | Checks whether the API is enabled in CoreProtect's configuration. |
| [`testAPI()`](#testapi) | `void` | Prints an API test message to the console. |
| [`blockLookup(Block, int)`](#legacy-block-session-and-queue-lookups) | `List<String[]>` | Legacy lookup at a block. |
| [`blockLookup(Block, LookupOptions)`](#block-lookups) | `List<BlockResult>` | Typed lookup at a block, retaining legacy entity events. |
| [`blockLookup(LookupOptions)`](#block-lookups) | `List<BlockResult>` | Typed block-only lookup. |
| [`entityLookup(LookupOptions)`](#entity-lookups) | `List<EntityResult>` | Entity spawn and kill history. |
| [`containerLookup(Location, int)`](#container-lookups) | `List<ContainerResult>` | Container history at a location. |
| [`containerLookup(LookupOptions)`](#container-lookups) | `List<ContainerResult>` | Container history with shared filters. |
| [`itemLookup(LookupOptions)`](#item-lookups) | `List<ItemResult>` | World item transactions. |
| [`inventoryLookup(LookupOptions)`](#inventory-lookups) | `List<InventoryResult>` | Transactions affecting player inventories. |
| [`chatLookup(String, int)`](#chat-and-command-lookups) / [`chatLookup(LookupOptions)`](#chat-and-command-lookups) | `List<MessageResult>` | Chat history. |
| [`commandLookup(String, int)`](#chat-and-command-lookups) / [`commandLookup(LookupOptions)`](#chat-and-command-lookups) | `List<MessageResult>` | Command history. |
| [`signLookup(Location, int)`](#sign-lookups) / [`signLookup(LookupOptions)`](#sign-lookups) | `List<SignResult>` | Written sign text. |
| [`sessionLookup(String, int)`](#legacy-block-session-and-queue-lookups) | `List<String[]>` | Legacy login/logout history. |
| [`sessionLookup(LookupOptions)`](#session-lookups) | `List<SessionResult>` | Typed login/logout history. |
| [`usernameLookup(String, int)`](#username-lookups) / [`usernameLookup(LookupOptions)`](#username-lookups) | `List<UsernameResult>` | Username history. |
| [`queueLookup(Block)`](#legacy-block-session-and-queue-lookups) | `List<String[]>` | Pending block changes in the consumer queue. |
| [`parseResult(String[])`](#parsing-legacy-rows) | `CoreProtectAPI.ParseResult` | Parses a legacy result row. |
| [`performLookup(...)`](#full-and-partial-searches) / [`performPartialLookup(...)`](#full-and-partial-searches) | `List<String[]>` | Legacy block/entity searches, with optional pagination. |
| [`performRollback(...)`](#rollback-and-restore-behavior) / [`performRestore(...)`](#rollback-and-restore-behavior) | `List<String[]>` | Rolls back or restores matching changes. |
| [`hasPlaced(String, Block, int, int)`](#placement-and-removal-checks) / [`hasRemoved(String, Block, int, int)`](#placement-and-removal-checks) | `boolean` | Checks a player's persisted block history. |
| [`logChat(Player, String)`](#chat-and-commands) / [`logCommand(Player, String)`](#chat-and-commands) | `boolean` | Queues player messages or commands. |
| [`logPlacement(...)`](#block-placement-and-removal) / [`logRemoval(...)`](#block-placement-and-removal) | `boolean` | Queues block changes. |
| [`logContainerTransaction(String, Location)`](#container-transactions) | `boolean` | Captures a block inventory before your changes. |
| [`logInteraction(String, Location)`](#block-interactions) | `boolean` | Queues a block interaction. |
| [`performPurge(int)`](#purging-history) | `void` | Dispatches a database purge command. |
### Shared typed lookup options
Create options with `LookupOptions.builder()` and finish with `build()`:
```java
LookupOptions options = LookupOptions.builder()
.user("Notch")
.time(3600)
.radius(location, 5)
.limit(0, 100)
.build();
```
| Builder method | Behavior |
| --- | --- |
| `user(String user)` | Restricts the search to one user. `null`, an empty string, or `#global` imposes no single-user restriction. |
| `users(List<String> users)` | Includes any matching user in the list. Combines with `user` as an additional restriction. |
| `excludeUsers(List<String> users)` | Excludes matching users, including users selected by either inclusion option. |
| `time(int seconds)` | Searches back this many seconds. Omitted, zero, or negative values impose no time restriction for typed lookups. |
| `location(Location location)` | Searches the exact block coordinates and world. |
| `radius(Location location, int radius)` | A positive radius searches an inclusive X/Z square around the location, at all heights. Zero or a negative radius searches the exact block coordinates. |
| `world(World world)` | Searches the entire selected world. |
| `limit(int offset, int count)` | Skips `offset` matching rows and returns at most `count`. The offset is zero-based. Both values must be nonnegative for the limit to apply; a count of zero returns no rows. |
| `includeMaterials(List<Material> materials)` | Includes any matching logged block/item material in supported lookup types. |
| `excludeMaterials(List<Material> materials)` | Excludes matching logged block/item materials. |
| `includeEntities(List<EntityType> entities)` | Includes any matching entity type in `entityLookup`. |
| `excludeEntities(List<EntityType> entities)` | Excludes matching entity types in `entityLookup`. |
| `blockActions(List<BlockAction> actions)` | Selects block actions. |
| `entityActions(List<EntityAction> actions)` | Selects entity actions. |
| `containerActions(List<ContainerAction> actions)` | Selects container transaction actions. |
| `itemActions(List<ItemAction> actions)` | Selects world item transaction actions. |
| `inventoryActions(List<InventoryAction> actions)` | Selects source-specific inventory transactions. |
| `sessionActions(List<SessionAction> actions)` | Selects login/logout actions. |
| `textStartsWithAny(List<String> prefixes)` | Requires text to begin with at least one supplied prefix. |
| `textStartsWithNone(List<String> prefixes)` | Rejects text beginning with any supplied prefix. |
The last call to `world`, `location`, or `radius` determines the search area. With no area specified, spatial typed lookups search all worlds. The `blockLookup(Block, LookupOptions)` overload always uses the supplied block's location instead.
All filters that apply to a lookup are combined. Empty filter lists retain the default behavior. List setters copy their inputs and reject null lists or null entries. A built options object's lists cannot be modified. Passing `null` instead of a `LookupOptions` object uses the defaults.
Within `users`, an empty string or `#global` removes that list's user restriction. Within `excludeUsers`, either value excludes everyone. Use an empty list when you want no exclusion.
Results are returned in descending event order. Limits apply after filtering; omitting a limit permits all matching rows. Use a bounded time range and pagination for large histories. Offset pagination does not hold a snapshot across separate API calls, so new records can shift later pages.
| Lookup | User/time/limit | Spatial filters | Material filters | Additional filters |
| --- | --- | --- | --- | --- |
| Block | Yes | Yes; supplied `Block` overrides options | Yes | `blockActions` |
| Entity | Yes | Yes | No | `entityActions`, `includeEntities`, `excludeEntities` |
| Container | Yes | Yes | Yes | `containerActions` |
| Item | Yes | Yes | Yes | `itemActions` |
| Inventory | Yes | Yes | Yes | `inventoryActions` |
| Chat / command | Yes | Yes | No | Text prefixes |
| Sign | Yes | Yes | No | Text prefixes |
| Session | Yes | Yes | No | `sessionActions` |
| Username | Yes | No | No | None |
Filters outside a lookup's scope are ignored. Username history has no location, radius, or world.
### Text prefix filters
`textStartsWithAny` matches any included prefix. `textStartsWithNone` rejects any excluded prefix, so exclusions take precedence. Either option can be used alone.
The API uses the same SQL matching and escaping as the in-game `f:` / `filter:` option, including the database's case-handling rules. Each API list entry is already one literal prefix: commas, leading `-`, and leading/trailing spaces are preserved. `%`, `_`, `~`, and `*` are not wildcards. The API does not parse command-style comma separators or exclusion markers.
Prefixes must contain at least three Unicode code points. Shorter prefixes throw `IllegalArgumentException` when the builder method is called. Empty lists impose no restriction.
Sign filters check each line on the logged face. An inclusion match on any line includes the entry; an exclusion match on any line excludes the whole entry. Text on the opposite face does not affect the match.
### Action filters
Use the enum associated with the lookup. Multiple values match any listed action; an omitted or empty list retains that lookup's defaults.
| Lookup | Enum values and IDs | Default selection |
| --- | --- | --- |
| Block | `BlockAction.BREAK` = `0`, `PLACE` = `1`, `INTERACTION` = `2` | All three for `blockLookup(options)`; the supplied-block overload also retains entity events unless narrowed. |
| Entity | `EntityAction.KILL` = `3`, `SPAWN` = `13` | Both. |
| Container | `ContainerAction.REMOVE` = `0`, `ADD` = `1` | Both. |
| Session | `SessionAction.LOGOUT` = `0`, `LOGIN` = `1` | Both. |
| Item | See below. | `DROP`, `PICKUP`, `REMOVE_ENDER`, `ADD_ENDER`, `THROW`, `SHOOT`. |
| Inventory | See below. | Block placements, container transactions, and all supported item transactions. |
| `ItemAction` | ID | Result action string |
| --- | --- | --- |
| `DROP` | 2 | `drop` |
| `PICKUP` | 3 | `pickup` |
| `REMOVE_ENDER` | 4 | `withdraw` |
| `ADD_ENDER` | 5 | `deposit` |
| `THROW` | 6 | `throw` |
| `SHOOT` | 7 | `shoot` |
| `BREAK` | 8 | `break` |
| `DESTROY` | 9 | `destroy` |
| `CREATE` | 10 | `create` |
| `SELL` | 11 | `sell` |
| `BUY` | 12 | `buy` |
`BREAK`, `DESTROY`, `CREATE`, `SELL`, and `BUY` require explicit selection in `itemLookup`. They are included by default in `inventoryLookup`.
`InventoryAction` selects both a source and its underlying action:
| Source | `InventoryAction` values |
| --- | --- |
| Block | `BLOCK_PLACE` |
| Container | `CONTAINER_REMOVE`, `CONTAINER_ADD` |
| Item | `ITEM_DROP`, `ITEM_PICKUP`, `ITEM_REMOVE_ENDER`, `ITEM_ADD_ENDER`, `ITEM_THROW`, `ITEM_SHOOT`, `ITEM_BREAK`, `ITEM_DESTROY`, `ITEM_CREATE`, `ITEM_SELL`, `ITEM_BUY` |
Each action enum exposes `id()`. `InventoryAction` also exposes `sourceId()`. Container remove/add describes the container's contents: taking an item out of a container adds it to the player's inventory, and depositing an item removes it from the player's inventory.
### CoreProtectAction
`CoreProtectAction` names the general lookup categories. It exposes `id()`, `actionString()`, and `fromId(int)`; an unrecognized ID maps to `UNKNOWN`.
| Constant | ID | Action string |
| --- | --- | --- |
| `BLOCK_BREAK` | 0 | `break` |
| `BLOCK_PLACE` | 1 | `place` |
| `INTERACTION` | 2 | `click` |
| `ENTITY_KILL` | 3 | `kill` |
| `CONTAINER` | 4 | `container` |
| `CHAT` | 6 | `chat` |
| `COMMAND` | 7 | `command` |
| `SESSION` | 8 | `session` |
| `USERNAME` | 9 | `username` |
| `SIGN` | 10 | `sign` |
| `ITEM` | 11 | `item` |
| `ENTITY_SPAWN` | 13 | `spawn` |
| `UNKNOWN` | -1 | `unknown` |
The legacy `performLookup`, `performPartialLookup`, `performRollback`, and `performRestore` methods accept only block/entity action IDs `0`, `1`, `2`, `3`, and `13`. Use the dedicated typed methods for the other categories.
A result's `getActionId()` uses that result type's action values. For example, `SessionResult` uses `0`/`1` for logout/login, and `InventoryResult` uses `0`/`1` for removal/addition to the player's inventory. Do not interpret every result ID through `CoreProtectAction.fromId()`.
## API Status Methods
### APIVersion()
`int APIVersion()` returns the API version supported by the installed CoreProtect plugin. This version returns `13`. Check for `13` or higher before using API v13 methods; the API version is separate from the plugin's release version, such as `25.0`.
### isEnabled()
`boolean isEnabled()` returns `true` when the server has the CoreProtect API enabled in its configuration, and `false` when it is disabled. Check this before using the API.
### testAPI()
`void testAPI()` prints an API test message to the server console. With the default English language configuration, the message is:
```text
[CoreProtect] API test successful.
```
The message follows CoreProtect's language configuration. This method returns no value and does not test database reads or writes; use it to confirm that your plugin can call the API.
## Typed Lookup Methods
For convenience overloads taking `int time`, the argument is how many seconds to search back. As with `LookupOptions.time`, zero or negative values impose no time restriction.
### Block lookups
| Signature | Returns |
| --- | --- |
| `blockLookup(LookupOptions options)` | `List<BlockResult>` |
| `blockLookup(Block block, LookupOptions options)` | `List<BlockResult>` |
The options-only method searches logged block breaks, placements, and interactions. It supports exact locations, X/Z radius searches, whole-world searches, and searches across all worlds.
The supplied-block method always searches that block's coordinates and world. Location, radius, and world options are ignored. It retains entity kills and spawns stored at those coordinates unless an explicit `blockActions` filter narrows the search.
Material filters compare the logged material, regardless of the block currently at the location. Including materials selects matching block events. Excluding materials preserves entity events in the supplied-block overload. Use `getEntityType()` to read entity rows; `getType()` and `getBlockData()` return `null` for them.
### Entity lookups
| Signature | Returns |
| --- | --- |
| `entityLookup(LookupOptions options)` | `List<EntityResult>` |
Returns entity spawns and kills. Use `entityActions` to select `SPAWN` or `KILL`, and `includeEntities` / `excludeEntities` to filter Bukkit `EntityType` values. Empty lists select both actions and all entity types.
For spawn events, location/radius filters match either the original spawn location or the persisted current/final location. Whole-world filters similarly match either world. Kill events match the location where the kill was logged. Results always expose the **original event coordinates and world**, even when a tracked position matched the filter.
These synchronous lookups use persisted tracking data and do not wait for a live-entity scan. Material filters and other lookup-specific action filters do not affect entity lookups.
The in-game `a:block` filters present placed and destroyed boats/minecarts as block changes. That command/display alias does not change API action IDs: their spawn and kill rows remain `ENTITY_SPAWN` (`13`) and `ENTITY_KILL` (`3`).
### Container lookups
| Signature | Returns |
| --- | --- |
| `containerLookup(Location location, int time)` | `List<ContainerResult>` |
| `containerLookup(LookupOptions options)` | `List<ContainerResult>` |
Searches item additions to and removals from containers. The location/time overload searches an exact location. The options overload also supports radius, world, user, material, action, and pagination filters.
Both methods include tracked boat and minecart container transactions. Their location filters match either the transaction's original location or the entity's persisted current/final location. Typed results for these tracked containers expose the **persisted current/final coordinates and world**. Ordinary block containers expose the transaction location.
Tracked positions are checkpointed when transactions are logged and during relevant entity lifecycle changes. Lookups do not wait for a live-entity scan. `containerActions` applies to both block containers and tracked entity containers.
### Item lookups
| Signature | Returns |
| --- | --- |
| `itemLookup(LookupOptions options)` | `List<ItemResult>` |
Searches world item transactions, including drops, pickups, ender-chest transfers, throws, and shots by default. Use `itemActions` to select specific actions, including item breaks, destruction, creation, sales, and purchases. Those last five categories are not included in the default item lookup.
### Inventory lookups
| Signature | Returns |
| --- | --- |
| `inventoryLookup(LookupOptions options)` | `List<InventoryResult>` |
Searches transactions affecting player inventories. The default combines block placements, container transactions, and all supported world item transactions. Use `inventoryActions` to select source-specific events.
Results normalize the action to removal from (`0`) or addition to (`1`) the player's inventory. Use `getTransactionActionId()` and `getSourceId()` together to identify the underlying event. For example, a container removal is an inventory addition.
Tracked boat and minecart transactions follow the location rules described under container lookups and remain part of the public `container` source.
### Chat and command lookups
| Signature | Returns |
| --- | --- |
| `chatLookup(String user, int time)` | `List<MessageResult>` |
| `chatLookup(LookupOptions options)` | `List<MessageResult>` |
| `commandLookup(String user, int time)` | `List<MessageResult>` |
| `commandLookup(LookupOptions options)` | `List<MessageResult>` |
Searches logged chat or commands. The user/time overloads accept `null` or `#global` for all users. The options overloads also support spatial filters, user lists, pagination, and [text prefix filters](#text-prefix-filters).
Commands include their leading `/`. `MessageResult.getActionId()` is `6` for chat and `7` for commands.
### Sign lookups
| Signature | Returns |
| --- | --- |
| `signLookup(Location location, int time)` | `List<SignResult>` |
| `signLookup(LookupOptions options)` | `List<SignResult>` |
Searches logged written sign text. Returned rows use the placed/written action (`1`) and contain nonempty text; this method is not a listing of sign breaks or all sign state changes.
The location/time overload searches an exact location. The options overload supports user, spatial, text-prefix, and pagination filters. Results contain both sides' saved lines and identify which face was logged.
### Session lookups
| Signature | Returns |
| --- | --- |
| `sessionLookup(LookupOptions options)` | `List<SessionResult>` |
Searches player logins and logouts. Both are included by default. Use `sessionActions` to select `LOGIN` or `LOGOUT`, and shared options for user, time, spatial, and pagination filters.
The older `sessionLookup(String user, int time)` overload returns raw `String[]` rows; see [legacy block, session, and queue lookups](#legacy-block-session-and-queue-lookups).
### Username lookups
| Signature | Returns |
| --- | --- |
| `usernameLookup(String user, int time)` | `List<UsernameResult>` |
| `usernameLookup(LookupOptions options)` | `List<UsernameResult>` |
Searches the usernames recorded for player UUIDs. User filters accept current usernames, historical usernames, or UUID strings and resolve the associated username history. `null` or `#global` selects all users.
Use `getUsername()` for the historical logged name and `getUuid()` for its UUID. `getPlayer()` returns the current known username when available, with the logged name as a fallback.
Only user, time, and limit filters apply. Location, radius, world, material, entity, action, and text filters are ignored.
## Reading Results
### Shared fields
All typed result classes and `ParseResult` implement `CoreProtectResult`:
| Method | Return type | Meaning |
| --- | --- | --- |
| `getPlayer()` | `String` | Username associated with the event. See username history for its current-name behavior. |
| `getTimestamp()` | `long` | Unix timestamp in **milliseconds**. Lookup `time` parameters are in **seconds**. |
| `getX()`, `getY()`, `getZ()` | `int` | Block coordinates, with the tracked-location behavior described above. |
| `worldName()` | `String` | World name. Username results return an empty string and zero coordinates. |
| `getActionId()` | `int` | Action ID in the result type's action family. |
| `getActionString()` | `String` | Action name, such as `break`, `spawn`, `add`, or `login`. |
### BlockResult
| Additional method | Return type | Meaning |
| --- | --- | --- |
| `getType()` | `Material` | Logged block material; `null` for entity rows or an unresolved material. |
| `getBlockData()` | `BlockData` | Logged block state; `null` for entity rows. |
| `getEntityType()` | `EntityType` | Entity type for kill/spawn rows; `null` for block rows. |
| `getData()` | `int` | Deprecated legacy block data value. Prefer `getBlockData()`. |
| `isRolledBack()` | `boolean` | Whether the block/entity change is currently rolled back. |
Action strings are `break`, `place`, `click`, `kill`, or `spawn` as applicable.
### EntityResult
| Additional method | Return type | Meaning |
| --- | --- | --- |
| `getEntityType()` | `EntityType` | Logged entity type, including `PLAYER` for player-kill records. |
| `isRolledBack()` | `boolean` | Whether the entity change is currently rolled back. |
Actions are `KILL` (`3`, `kill`) and `SPAWN` (`13`, `spawn`).
### ContainerResult and ItemResult
| Additional method | Return type | Meaning |
| --- | --- | --- |
| `getType()` | `Material` | Logged item material, or `null` if unresolved. |
| `getAmount()` | `int` | Number of items. |
| `getMetadata()` | `byte[]` | Copy of the raw item metadata, or `null` when absent. |
| `isRolledBack()` | `boolean` | Whether the transaction is currently rolled back. |
| `ContainerResult.getData()` | `int` | Stored item data value. |
Container actions are `0` / `remove` and `1` / `add`. Item actions use the IDs and strings in the [item action table](#action-filters).
### InventoryResult
| Additional method | Return type | Meaning |
| --- | --- | --- |
| `getType()` | `Material` | Item material, with block sources normalized to an inventory item. |
| `getAmount()` | `int` | Number of items. |
| `getMetadata()` | `byte[]` | Copy of raw item metadata, or `null`. |
| `getData()` | `int` | Deprecated legacy item data value. |
| `getTransactionActionId()` | `int` | Underlying transaction action. Interpret together with its source. |
| `getTransactionActionString()` | `String` | Transaction action string, such as `remove`, `drop`, or `buy`. |
| `getSourceId()` | `int` | Public source: `0` = block, `1` = container, `2` = item. |
| `getSource()` | `String` | `block`, `container`, or `item`. |
| `isRolledBack()` | `boolean` | Whether the inventory change is currently rolled back. |
The shared `getActionId()` / `getActionString()` report `0` / `remove` or `1` / `add` from the player's inventory perspective. Tracked entity-container transactions use source `1` / `container`.
### MessageResult
`getMessage()` returns the chat message or command as a `String`. Actions are `6` / `chat` and `7` / `command`.
### SignResult
| Additional method | Return type | Meaning |
| --- | --- | --- |
| `getLine(int line)` | `String` | Zero-based saved line: `0`–`3` front, `4`–`7` back. An out-of-range index returns an empty string. |
| `getLines()` | `String[]` | Copy of all eight saved lines. |
| `getMessage()` | `String` | Nonempty lines on the logged face joined with spaces. Nonempty messages end with a space; a face with no text returns an empty string. |
| `isFront()` | `boolean` | Whether the logged face is the front. |
| `getColor()` | `int` | Stored front text color. |
| `getColorSecondary()` | `int` | Stored back text color. |
| `getData()` | `int` | Raw sign-state flags. |
| `isFrontGlowing()` / `isBackGlowing()` | `boolean` | Whether the corresponding side glows. |
| `isWaxed()` | `boolean` | Whether the sign is waxed. |
The returned action is `1` / `place`.
### SessionResult
Uses the shared fields. Actions are `0` / `logout` and `1` / `login`.
### UsernameResult
`getUsername()` returns the logged name and `getUuid()` returns the UUID as a `String`. The shared action is `9` / `username`. These results have no world or coordinates.
## Legacy Lookups, Rollbacks, and Restores
### Full and partial searches
`performLookup` searches persisted block and entity history using the supplied filters. `performPartialLookup` performs the same search with an offset and maximum result count for pagination. Use `parseResult` to read each returned row.
The following methods return `List<String[]>`:
```text
performLookup(time, restrictUsers, excludeUsers, restrictBlocks, excludeBlocks, actionList, radius, radiusLocation)
performPartialLookup(time, restrictUsers, excludeUsers, restrictBlocks, excludeBlocks, actionList, radius, radiusLocation, limitOffset, limitCount)
performRollback(time, restrictUsers, excludeUsers, restrictBlocks, excludeBlocks, actionList, radius, radiusLocation)
performRestore(time, restrictUsers, excludeUsers, restrictBlocks, excludeBlocks, actionList, radius, radiusLocation)
```
| Parameter | Type | Behavior |
| --- | --- | --- |
| `time` | `int` | How far back to search in seconds. Use a positive value; these legacy methods do not share the typed options' zero-means-all-time convention. |
| `restrictUsers` | `List<String>` | Users to include. `null` or an empty list selects all users and requires a positive radius and location. |
| `excludeUsers` | `List<String>` | Users to exclude; `null` imposes no exclusion. |
| `restrictBlocks` | `List<Object>` | Bukkit `Material` or `EntityType` values to include; `null` imposes no type restriction. |
| `excludeBlocks` | `List<Object>` | Bukkit `Material` or `EntityType` values to exclude; `null` imposes no type exclusion. |
| `actionList` | `List<Integer>` | Block/entity action IDs. Accepted IDs are `0`, `1`, `2`, `3`, and `13`. |
| `radius` | `int` | Positive X/Z radius at all heights. Zero or a negative value disables the radius. |
| `radiusLocation` | `Location` | Center of a positive radius. May be `null` for user-restricted searches without a radius. |
| `limitOffset` | `int` | Zero-based matching-row offset for `performPartialLookup`. |
| `limitCount` | `int` | Maximum rows for `performPartialLookup`. Use nonnegative offset/count values. |
With no explicit actions or type restrictions, the action list defaults to block breaks and placements. Material restrictions infer block breaks/placements; entity-type restrictions infer entity kills. To include entity spawns, explicitly select `CoreProtectAction.ENTITY_SPAWN.id()`.
Unsupported explicit action IDs are ignored. If none remain, the method returns an empty list. An unrestricted global search without a positive radius, or a positive radius without a location, returns `null`.
For legacy lookups, a supplied location also restricts the world even when the radius is disabled. Use typed options for a clear whole-world or exact-location search.
### Rollback and restore behavior
`performRollback` reverses the selected changes. `performRestore` reapplies previously rolled-back changes. Both require an asynchronous caller and return raw result rows. They do not accept `LookupOptions`.
Entity spawns are opt-in through action ID `13`. This preserves legacy default block behavior; in-game rollbacks/restores with no action filter can include entity spawns when `rollback-entities` is enabled. Spawn lookup selection can match original or tracked positions, while rollback/restore world selection uses the tracked current/final world.
An empty result can also indicate that CoreProtect could not start the operation, including during a purge, a conflicting database operation, or when called from the primary server thread. Do not treat it as a guaranteed successful rollback of zero rows.
### Legacy block, session, and queue lookups
| Signature | Returns | Behavior |
| --- | --- | --- |
| `blockLookup(Block block, int time)` | `List<String[]>` | Searches persisted block-table history at the supplied block coordinates, including entity rows. `time` is seconds to look back. |
| `sessionLookup(String user, int time)` | `List<String[]>` | Searches persisted login/logout history for the user. `time` is seconds to look back. |
| `queueLookup(Block block)` | `List<String[]>` | Searches pending block breaks and placements at the block's location that have not yet been persisted. |
The legacy block and session overloads impose no time restriction when `time` is zero or negative. The legacy session overload requires a concrete username; `null`, an empty string, or `#global` returns no results. Use typed session options to search all users.
Queue lookups cover pending block changes, not every logging category. A newly queued event might not yet appear in a database lookup. Queue result timestamps reflect the time of the queue lookup, not the original event time, so they cannot reliably enforce an event-age filter.
### Parsing legacy rows
Call `api.parseResult(row)` for rows returned by these legacy methods. It returns `CoreProtectAPI.ParseResult`, which extends `net.coreprotect.api.result.ParseResult` and preserves the existing nested-class import.
`ParseResult` implements the [shared result fields](#shared-fields) and adds:
| Method | Return type | Meaning |
| --- | --- | --- |
| `getType()` | `Material` | Logged block material; `null` for entity or session rows. |
| `getBlockData()` | `BlockData` | Logged block data; `null` for entity or session rows. |
| `getEntityType()` | `EntityType` | Entity type for entity rows; `null` for block or session rows. |
| `isRolledBack()` | `boolean` | Whether the block/entity change is rolled back; false for session rows. |
| `getData()` | `int` | Deprecated legacy data value. |
| `getTime()` | `int` | Deprecated Unix timestamp in seconds. Prefer `getTimestamp()` for milliseconds. |
Block/entity action strings are lowercase `break`, `place`, `click`, `kill`, and `spawn`. Session rows use `logout` and `login`. Use the typed result classes for their corresponding lookup methods instead of passing typed results to `parseResult`.
## Logging and Utility Methods
### Block placement and removal
All of these overloads return `boolean`:
| Signature | Data recorded |
| --- | --- |
| `logPlacement(String user, BlockState blockState)` | Placement using the supplied block-state snapshot. |
| `logPlacement(String user, Location location, Material type, BlockData blockData)` | Placement of the supplied material and optional block data. |
| `logPlacement(String user, Location location, BlockData blockData)` | Placement using the current block's material and supplied non-null block data. |
| `logRemoval(String user, BlockState blockState)` | Removal using the supplied block-state snapshot. |
| `logRemoval(String user, Location location, Material type, BlockData blockData)` | Removal of the supplied material and optional block data, with container-break handling. |
| `logRemoval(String user, Location location, BlockData blockData)` | Removal using the current block's material and supplied non-null block data, with container-break handling. |
`user` identifies the player or plugin responsible for the change. Capture removal data **before** changing the block. For placements, supply the state/material being placed. The explicit-material overloads allow `null` block data; the `Location, BlockData` overloads require non-null data.
Location-based removal checks the existing block for container contents. The `BlockState` overload does not perform that separate container-break check. Call methods that inspect live blocks on the appropriate server thread.
Deprecated placement/removal overloads taking `(String user, Location location, Material type, byte data)` remain for older integrations. Use `BlockData` for new code.
### Chat and commands
| Signature | Behavior |
| --- | --- |
| `logChat(Player player, String message)` | Queues a nonempty chat message. Messages beginning with `/` are rejected. |
| `logCommand(Player player, String command)` | Queues a nonempty command, which must begin with `/`. |
Both return `boolean`, require the API to be enabled, and honor the world's corresponding player-message or player-command logging setting. Supply a valid player with a world.
### Container transactions
`boolean logContainerTransaction(String user, Location location)` captures a block inventory so CoreProtect can detect your subsequent changes. Call it **immediately before** modifying the inventory, on the appropriate server thread.
`user` is the username or plugin actor credited with adding or removing the items. `location` is the block container's location.
This method is for a block container. Tracked entity-container lookup support does not add an entity overload to this logging method.
### Block interactions
`boolean logInteraction(String user, Location location)` records an interaction with the block currently at the location. It reads the block's material and block data.
`user` is the username or plugin actor credited with the interaction. `location` identifies the affected block.
### Placement and removal checks
| Signature | Behavior |
| --- | --- |
| `hasPlaced(String user, Block block, int time, int offset)` | Checks whether the user placed a block at these coordinates during the requested window. |
| `hasRemoved(String user, Block block, int time, int offset)` | Checks whether the user removed a block at these coordinates during the requested window. |
Both return `boolean` and compare usernames without case sensitivity. `time` is how many seconds to search back; `offset` excludes the most recent seconds from that window. Use `offset = 0` to include the latest persisted records. These methods search persisted history; use `queueLookup` separately for pending block changes.
### Purging history
`void performPurge(int time)` dispatches the console command `co purge t:<time>s`. The argument is the age in seconds beyond which history should be purged: `120` selects records older than two minutes.
Call this command-dispatching method on the appropriate server thread. It returns no completion result and uses the normal command's purge behavior and restrictions.
## Available Events
### CoreProtectPreLogEvent
`net.coreprotect.event.CoreProtectPreLogEvent` is a cancellable **asynchronous** event emitted by participating loggers before they write a record. Keep listeners safe for asynchronous execution and avoid accessing live Bukkit world state from the listener.
| Getter | Meaning | Setter |
| --- | --- | --- |
| `getUser()` | User under which the action will be logged. | `setUser(String)` |
| `getLocation()` | Location associated with the record. | `setLocation(Location)` |
| `getAction()` | `CoreProtectPreLogEvent.Action` category. | None |
| `getActionId()` | Logger-specific action value, or `-1` when not applicable. | None |
| `getMaterial()` | Material when applicable; otherwise `null`. | None |
| `getEntityType()` | Entity type when applicable; otherwise `null`. | None |
| `getMessage()` | Message metadata when applicable; otherwise `null`. | None |
| `isCancelled()` | Whether this record has been cancelled. | `setCancelled(boolean)` |
`setUser` rejects null or empty names. `setLocation` rejects null. Cancelling prevents the record from being logged; it does not undo the gameplay action.
The action enum contains:
| Category | Meaning |
| --- | --- |
| `UNKNOWN` | Unspecified action, including compatibility constructors. |
| `BLOCK_BREAK`, `BLOCK_PLACE` | Block removal or placement. |
| `PLAYER_INTERACTION` | Player interaction with a block. |
| `ENTITY_KILL`, `PLAYER_KILL` | Entity or player death. |
| `ENTITY_SPAWN` | Entity spawn. |
| `ENTITY_INTERACTION` | Interaction with a tracked entity. |
| `SIGN_TEXT` | Sign text logging. |
| `CONTAINER_TRANSACTION` | Container item transaction. |
| `ITEM_TRANSACTION` | World item transaction. |
| `PLAYER_COMMAND` | Player command. |
Use `getAction()` to identify the category before interpreting the action ID. Container, item, and sign events use their own stored action values; player-command events use `-1`. Command events expose their command as the message. Entity-interaction events expose the interaction action name. Sign-text events do not provide their lines through `getMessage()`.
The event is not emitted for every logging category; for example, chat and session logging do not emit it.
The metadata constructor is:
```text
CoreProtectPreLogEvent(String user, Location location, Action action, int actionId, Material material, EntityType entityType, String message)
```
`CoreProtectPreLogEvent(String user, Location location)` remains available. The older `CoreProtectPreLogEvent(String user)` constructor is deprecated and supplies a null location. Listeners should tolerate absent optional metadata.
## Examples
These examples use an enabled API v13 instance named `api` and your plugin instance named `plugin`. Import `java.util.List`, the required Bukkit types, and the relevant classes from `net.coreprotect.api` and `net.coreprotect.api.result`.
Capture `location`, `world`, and other live Bukkit values before starting asynchronous work. In the examples that show only a lookup body, run that body asynchronously as demonstrated below. Database queries return lists directly; they do not create a background task themselves.
### Search blocks asynchronously
Find the first 100 block changes by Notch in the last minute, excluding dirt and grass blocks:
```java
LookupOptions options = LookupOptions.builder()
.user("Notch")
.time(60)
.excludeMaterials(List.of(Material.DIRT, Material.GRASS_BLOCK))
.limit(0, 100)
.build();
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> {
List<BlockResult> results = api.blockLookup(options);
if (results != null) {
for (BlockResult result : results) {
plugin.getLogger().info(result.getPlayer() + " " + result.getActionString()
+ " " + result.getType() + " at " + result.worldName()
+ " " + result.getX() + "," + result.getY() + "," + result.getZ());
}
}
});
```
Use `.radius(location, 5)` to restrict this search to an X/Z area, or `.world(world)` for a whole world. To use the exact-block compatibility overload:
```java
List<BlockResult> results = api.blockLookup(block, LookupOptions.builder()
.time(60)
.blockActions(List.of(BlockAction.BREAK, BlockAction.PLACE))
.limit(0, 100)
.build());
```
### Find entity spawns
Find zombie and skeleton spawns within 50 blocks of a captured location:
```java
LookupOptions options = LookupOptions.builder()
.time(3600)
.radius(location, 50)
.entityActions(List.of(EntityAction.SPAWN))
.includeEntities(List.of(EntityType.ZOMBIE, EntityType.SKELETON))
.limit(0, 100)
.build();
List<EntityResult> results = api.entityLookup(options);
```
The search can match a tracked position, but each result still reports its original spawn coordinates.
### Find container deposits
Find diamonds added to containers within five blocks of a location:
```java
LookupOptions options = LookupOptions.builder()
.time(3600)
.radius(location, 5)
.includeMaterials(List.of(Material.DIAMOND))
.containerActions(List.of(ContainerAction.ADD))
.limit(0, 100)
.build();
List<ContainerResult> results = api.containerLookup(options);
```
For an exact-location lookup without additional filters, use `api.containerLookup(location, 60)`.
### Find item and inventory transactions
Look up dropped and picked-up items:
```java
LookupOptions options = LookupOptions.builder()
.user("Notch")
.time(3600)
.itemActions(List.of(ItemAction.DROP, ItemAction.PICKUP))
.limit(0, 100)
.build();
List<ItemResult> results = api.itemLookup(options);
```
Find inventory removals caused by container deposits or block placements:
```java
LookupOptions options = LookupOptions.builder()
.user("Notch")
.time(3600)
.inventoryActions(List.of(InventoryAction.CONTAINER_ADD, InventoryAction.BLOCK_PLACE))
.limit(0, 100)
.build();
List<InventoryResult> results = api.inventoryLookup(options);
if (results != null) {
for (InventoryResult result : results) {
plugin.getLogger().info(result.getActionString() + " " + result.getAmount()
+ " " + result.getType() + " via " + result.getSource()
+ ":" + result.getTransactionActionString());
}
}
```
### Filter commands by prefix
Find `/shop ...` and `/market ...` commands from the last hour, excluding commands beginning with `/shop admin`:
```java
LookupOptions options = LookupOptions.builder()
.time(3600)
.textStartsWithAny(List.of("/shop ", "/market "))
.textStartsWithNone(List.of("/shop admin"))
.limit(0, 100)
.build();
List<MessageResult> results = api.commandLookup(options);
```
The trailing spaces in the inclusion prefixes are significant: bare `/shop` and `/market` commands do not match. Use `chatLookup(options)` to apply the same kind of filters to chat, or `chatLookup("Notch", 60)` for an unfiltered user/time search.
### Find sign text
Find signs whose logged face has a line beginning with `Diamond Shop`:
```java
LookupOptions options = LookupOptions.builder()
.time(3600)
.radius(location, 10)
.textStartsWithAny(List.of("Diamond Shop"))
.limit(0, 100)
.build();
List<SignResult> results = api.signLookup(options);
if (results != null) {
for (SignResult result : results) {
plugin.getLogger().info((result.isFront() ? "Front: " : "Back: ") + result.getMessage());
}
}
```
Use `api.signLookup(location, 60)` for an exact-location search without additional filters.
### Read sessions and username history
Find logins for Notch in the last day:
```java
LookupOptions options = LookupOptions.builder()
.user("Notch")
.time(24 * 60 * 60)
.sessionActions(List.of(SessionAction.LOGIN))
.limit(0, 100)
.build();
List<SessionResult> results = api.sessionLookup(options);
```
Read username history for the same period:
```java
List<UsernameResult> results = api.usernameLookup("Notch", 24 * 60 * 60);
if (results != null) {
for (UsernameResult result : results) {
plugin.getLogger().info(result.getUuid() + " used " + result.getUsername());
}
}
```
### Parse a legacy lookup
Read the last minute of Notch's block history and parse its coordinates:
```java
List<String[]> rows = api.performLookup(60, List.of("Notch"), null,
null, null, null, 0, null);
if (rows != null) {
for (String[] row : rows) {
CoreProtectAPI.ParseResult result = api.parseResult(row);
plugin.getLogger().info(result.getActionString() + " at "
+ result.getX() + "," + result.getY() + "," + result.getZ());
}
}
```
For a page of results within five blocks of a location:
```java
List<String[]> rows = api.performPartialLookup(60, null, null,
null, null, List.of(CoreProtectAction.BLOCK_BREAK.id(), CoreProtectAction.BLOCK_PLACE.id()),
5, location, 0, 100);
```
The deprecated single-user overloads of `performLookup`, `performPartialLookup`, `performRollback`, and `performRestore` remain available. Prefer the list-based signatures above for new integrations.
### Roll back changes asynchronously
Reverse Notch's block changes and entity spawns from the last minute within five blocks of a captured location:
```java
List<Integer> actions = List.of(CoreProtectAction.BLOCK_BREAK.id(),
CoreProtectAction.BLOCK_PLACE.id(), CoreProtectAction.ENTITY_SPAWN.id());
Location center = location.clone();
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> {
List<String[]> rows = api.performRollback(60, List.of("Notch"), null,
null, null, actions, 5, center);
if (rows != null) {
plugin.getLogger().info("Rollback returned " + rows.size() + " rows.");
}
});
```
To restore changes, use `performRestore` with the same parameter structure in an asynchronous task. An empty result alone does not distinguish no matches from an operation that could not start.
### Include pending block changes in a placement check
```java
boolean placed = api.hasPlaced("Notch", block, 60, 0);
if (!placed) {
List<String[]> pending = api.queueLookup(block);
if (pending != null) {
for (String[] row : pending) {
CoreProtectAPI.ParseResult result = api.parseResult(row);
if (result.getActionId() == CoreProtectAction.BLOCK_PLACE.id()
&& result.getPlayer().equalsIgnoreCase("Notch")) {
placed = true;
break;
}
}
}
}
plugin.getLogger().info("Recent placement found: " + placed);
```
The persisted check covers the last 60 seconds; the pending check cannot enforce that age because queue rows use the lookup time as their timestamp. Use `hasRemoved` and `CoreProtectAction.BLOCK_BREAK.id()` for removal checks. A nonzero `offset` on the persisted check can exclude the most recent seconds when needed by your integration.
### Log a block replacement
Run this on the appropriate server thread, while the original block still exists:
```java
api.logRemoval("Notch", block.getLocation(), block.getType(), block.getBlockData());
block.setType(Material.STONE);
api.logPlacement("Notch", block.getState());
```
Use the actual responsible username or a plugin-specific actor name. If CoreProtect already records the gameplay event, additional API logging can create duplicate history.
### Log inventory changes
For a block-backed `inventory`, capture its contents immediately before changing them:
```java
api.logContainerTransaction("Notch", inventory.getLocation());
inventory.addItem(itemStack);
```
### Listen for pre-log events
This listener cancels block-placement records attributed to a plugin's preview actor:
```java
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import net.coreprotect.event.CoreProtectPreLogEvent;
public final class PreviewLogListener implements Listener {
@EventHandler
public void onPreLog(CoreProtectPreLogEvent event) {
if (event.getAction() == CoreProtectPreLogEvent.Action.BLOCK_PLACE
&& "MyPluginPreview".equals(event.getUser())) {
event.setCancelled(true);
}
}
}
```
Register it during plugin initialization:
```java
plugin.getServer().getPluginManager().registerEvents(new PreviewLogListener(), plugin);
```
The listener executes asynchronously. Cancelling it suppresses the log record, not the original block placement.
+8 -4
View File
@@ -2,7 +2,7 @@
Automatic purging removes old CoreProtect data on a daily schedule, helping keep database growth under control without requiring manual `/co purge` runs.
> **Note:** Automatic purging is not enabled by default. This feature is exclusive to CoreProtect 24.0+ [Patreon builds](http://patreon.com/coreprotect).
> **Note:** Automatic purging is not enabled by default. This feature is exclusive to CoreProtect 24.0+ [Patreon builds](https://patreon.com/coreprotect).
## Configuration
@@ -29,11 +29,13 @@ When automatic purging is enabled, CoreProtect logs the next scheduled run when
## How It Works
Automatic purging runs in the background and removes old data incrementally in small chunks, with short pauses between database work. The server can continue to be used normally while it runs.
Automatic purging runs in the background. SQLite, MySQL, and DuckDB remove old rows incrementally in small chunks with short pauses between database work. ClickHouse uses its columnar retention path, dropping fully covered monthly partitions for an unfiltered time purge and synchronously removing rows from any partially covered partition.
Automatic purging uses the same CoreProtect data tables as manual purging, but it does not rebuild the SQLite database or optimize MySQL tables. It deletes old rows in place, which helps maintain the current database size over time but may not immediately reduce the database file size or table size on disk.
Automatic purging does not rebuild SQLite, optimize MySQL, or run a ClickHouse `OPTIMIZE FINAL`. DuckDB performs a checkpoint after cleanup. Deleting rows helps control future growth, but it may not immediately reduce the database file or table size on disk.
Only one automatic purge can run at a time. If the server shuts down, a manual purge starts, a database migration or conversion starts, or the consumer is manually paused, the automatic purge stops safely and can continue during the next scheduled run.
Only one automatic purge can run per CoreProtect instance at a time. If the server shuts down, a manual purge starts, a database migration or conversion starts, or the consumer is manually paused, the automatic purge stops safely and can continue during the next scheduled run.
ClickHouse rejects automatic and manual purges while `database-lock` is disabled. For a shared namespace, stop every installation, enable `database-lock` on one installation, restart or reload it, and finish the purge before restoring shared-writer mode.
## Important Notes
@@ -59,6 +61,8 @@ For MySQL, add `#optimize` if you want to reclaim disk space during the initial
This lets the manual purge reduce the existing database size, while automatic purging helps keep the database from growing beyond the configured retention period afterward.
ClickHouse normally reclaims complete old partitions without `#optimize`. A manual ClickHouse purge accepts `#optimize`, but it runs the expensive `OPTIMIZE TABLE ... FINAL` operation and is generally unnecessary. DuckDB checkpoints automatically after a manual or automatic purge; `#optimize` has no additional effect for DuckDB.
## Status
Use `/co status` to see how many rows have been automatically purged since the last server restart.
+57 -15
View File
@@ -16,7 +16,7 @@ ___
| [/co reload](#co-reload) | Reload the configuration file |
| [/co status](#co-status) | View the plugin status |
| [/co consumer](#co-consumer) | Toggle consumer processing |
| [/co migrate-db](#co-migrate-db) | Migrate between SQLite and MySQL |
| [/co migrate-db](#co-migrate-db) | Migrate between database backends |
### Alias Commands
@@ -56,6 +56,7 @@ Perform a lookup. Nearly all of the parameters are optional.
| [`a:<action>`](#aaction) | Restrict the lookup to a certain action. |
| [`i:<include>`](#iinclude) | Include specific blocks/entities in the lookup. |
| [`e:<exclude>`](#eexclude) | Exclude blocks/entities from the lookup. |
| [`f:<filter>`](#ffilter) | Include or exclude chat, command, or sign text by prefix. |
| [`#<hashtag>`](#hashtag) | Add a hashtag to perform additional actions. |
#### Pagination
@@ -94,7 +95,7 @@ Purge old block data. Useful for freeing up space on your HDD if you don't need
| Command | Parameters |
| --- | --- |
| /co purge | `t:<time> r:<world> i:<include>` |
| /co purge | `t:<time> r:<world/radius> i:<include> e:<exclude> a:kill` |
For example, `/co purge t:30d` will delete all data older than one month, and only keep the last 30 days of data.
@@ -105,15 +106,34 @@ For example, `/co purge t:30d` will delete all data older than one month, and on
You can optionally specify a world in CoreProtect v19+.
For example, `/co purge t:30d r:#world_nether` will delete all data older than one month in the Nether, without removing data in any other worlds.
**Purging by Radius**
A radius can limit a purge of specific block types or entity kills to the area around you. It only applies to block and entity kill data, so it must be combined with `i:` or `a:kill`, and it can only be used in-game. A radius always applies to the world you are in, so it cannot be combined with a world such as `r:#world_nether`. The radius is limited by `max-radius` in the config, like other commands.
For example, `/co purge t:30d r:50 i:zombie` will delete zombie kills older than one month within 50 blocks of you, without removing any other data. Use `r:50x10` to also limit the height to 10 blocks above and below you.
**Purging Blocks**
You can optionally specify block types in CoreProtect v23+.
For example, `/co purge t:30d i:stone,dirt` will delete all stone and dirt data older than one month, without removing other block data.
**MySQL Optimization**
In CoreProtect v2.15+, adding "#optimize" to the end of the command (e.g. `/co purge t:30d #optimize`) will also optimize your tables and reclaim disk space.
This option is only available when using MySQL, as SQLite purges do this by default.
**Purging Entity Kills**
Entity kill logs can be purged separately from other data on SQLite, MySQL, and DuckDB. Each kill also removes the saved entity data used to restore the mob on rollback.
*Please note adding the #optimize option will significantly slow down your purge, and is generally unnecessary.*
* `/co purge t:30d a:kill` deletes all entity kills older than one month, and keeps all other data.
* `/co purge t:30d i:zombie,skeleton` deletes zombie and skeleton kills older than one month. Entity and block types can be combined in `i:`.
* `/co purge t:30d e:villager,wolf` deletes all data older than one month, but keeps villager and wolf kills.
* `/co purge t:30d a:kill e:villager r:#world` deletes entity kills older than one month in the overworld, except villager kills.
`e:` only accepts entity types. It cannot be combined with entity types in `i:`, or with only block types in `i:`, because a block restriction already keeps every kill. `a:kill` cannot be combined with block types in `i:`.
**Database Optimization**
In CoreProtect v2.15+, adding `#optimize` to the end of the command (for example, `/co purge t:30d #optimize`) will also optimize supported database tables and reclaim unused disk space. How this option is handled depends on the database backend:
* SQLite already rebuilds the database from retained data and reclaims unused file space as part of a manual purge, so `#optimize` is not needed.
* MySQL normally deletes matching rows. Adding `#optimize` also removes saved entity data in the purged time range that no entity kill references anymore (this requires the `CREATE TEMPORARY TABLES` privilege), then optimizes its tables to reclaim unused space.
* DuckDB deletes matching rows in one transaction and checkpoints afterward. `#optimize` has no additional effect.
* ClickHouse drops fully covered monthly partitions for an unfiltered time purge and synchronously removes rows from partial or filtered partitions. Adding `#optimize` also runs `OPTIMIZE TABLE ... FINAL`.
`#optimize` can significantly slow MySQL and ClickHouse purges and is generally unnecessary.
___
@@ -130,17 +150,21 @@ Console command to pause or resume consumer queue processing.
___
### /co migrate-db
Migrate your CoreProtect database between SQLite and MySQL. Console-only command.
Migrate data from the active database backend to a different backend. This is a console-only command.
| Command | Parameters |
| --- | --- |
| /co migrate-db | `<sqlite|mysql>` |
| /co migrate-db | `<sqlite|mysql|duckdb|clickhouse> [--full-validation]` |
By default, migration validates all table statistics and compares approximately 1% of each large history table, with full comparisons for small and reference tables. Add `--full-validation` to compare every copied row.
The target namespace must contain no CoreProtect data; a DuckDB target must use a new database file, and `database-lock` must remain enabled. After a successful migration, CoreProtect automatically updates `database-type` in `config.yml` before queued writes resume.
> **Note:** This feature is exclusive to CoreProtect 23.0+ Patreon builds for Patron supporters.
> **Note:** Migrations between SQLite and MySQL require a CoreProtect 23.0+ Patreon build. Any migration involving DuckDB or ClickHouse requires CoreProtect 25.0+.
For complete migration instructions, safety guidelines, and troubleshooting information, see the [Database Migration documentation](/database-migration/).
___
For complete migration instructions, safety guidelines, and troubleshooting information, see the [Database Migration documentation](/database-migration/).
___
## Parameter Details
@@ -201,9 +225,10 @@ ___
| `a:-inventory` | items removed from player inventories |
| `a:item` | items dropped, thrown, picked up, deposited, or withdrawn by players |
| `a:+item` | items picked up or withdrawn by players |
| `a:-item` | items dropped, thrown, or deposited by players |
| `a:kill` | mobs/animals killed |
| `a:session` | player logins/logouts |
| `a:-item` | items dropped, thrown, or deposited by players |
| `a:kill` | mobs/animals killed |
| `a:spawn` | entities placed or spawned by players |
| `a:session` | player logins/logouts |
| `a:+session` | player logins |
| `a:-session` | player logouts |
| `a:sign` | messages written on signs |
@@ -231,6 +256,20 @@ ___
---
### `f:<filter>`
*Filter message prefixes when using `a:chat`, `a:command`, or `a:sign`.*
* Example: `a:command f:/ban` *(commands starting with "/ban")*
* Example: `a:command f:/ban,/kick` *(commands starting with either prefix)*
* Example: `a:command f:/ban,-/banlist` *(include "/ban" and exclude "/banlist")*
* Example: `a:chat f:-hello` *(exclude messages starting with "hello")*
* Example: `a:sign f:Diamond Shop,-Diamond Shop Closed` *(filter prefixes on the recorded sign face)*
> Separate filters with commas. Included prefixes are alternatives; any matching excluded prefix removes the result. Exclusions can be used on their own. Each prefix requires at least three characters, excluding the leading `-` marker. Spaces are allowed, and `*`, `%`, `_`, and `~` are matched literally.
---
### `#<hashtag>`
Add a hashtag to the end of your command to perform additional actions.
@@ -242,6 +281,7 @@ Add a hashtag to the end of your command to perform additional actions.
| --- | --- |
| `#preview` | Preview a rollback/restore |
| `#count` | Return the number of rows found in a lookup query |
| `#summary` | Return the row count and per-user material totals |
| `#verbose` | Display additional information during a rollback/restore |
| `#silent` | Display minimal information during a rollback/restore |
@@ -294,5 +334,7 @@ Lookup commands are generally the same as rollback commands. The primary differe
*(lookup all logins ever done by Notch)*
* `/co lookup u:Notch a:username`
*(lookup previous usernames used by Notch)*
* `/co lookup r:50 t:7d a:block #summary`
*(show the matching row count and material totals for each user within the same area and time range)*
___
+53
View File
@@ -2,6 +2,59 @@
The CoreProtect configuration file can be found within the CoreProtect folder, at `config.yml`.
## Database Storage
New installations use embedded DuckDB by default. Upgrading an existing SQLite or MySQL installation does not switch it to DuckDB: if `database-type` is absent, CoreProtect derives it from the existing legacy `use-mysql` setting and automatically writes the matching `sqlite` or `mysql` value. No manual change is required. Once present, `database-type` takes precedence over `use-mysql`, and changes take effect after `/co reload` or a restart.
| Value | Storage | Requirements |
| --- | --- | --- |
| `duckdb` | Embedded columnar database; the default for new installations | CoreProtect 25.0+ |
| `clickhouse` | External columnar database | CoreProtect 25.0+ and a reachable ClickHouse 26.1+ server |
| `sqlite` | Embedded legacy relational database | None beyond CoreProtect |
| `mysql` | External legacy relational database | A reachable MySQL server |
The server downloads the DuckDB JDBC driver automatically as a plugin library, while the ClickHouse JDBC driver is packaged inside CoreProtect; neither requires manual driver installation. DuckDB includes its database engine, while ClickHouse requires a separate server.
Changing `database-type` selects a separate dataset; it does not copy existing data. CoreProtect 23.0+ Patreon builds can migrate between SQLite and MySQL; any migration involving DuckDB or ClickHouse requires CoreProtect 25.0+. The target namespace must contain no CoreProtect data; DuckDB requires a new target file, and `database-lock` must remain enabled. See the [database migration guide](/database-migration/) for preparation and switchover instructions.
### DuckDB
The default settings are suitable for most servers:
```yaml
database-type: duckdb
duckdb-memory-limit: 512MB
duckdb-threads: 3
duckdb-max-temp-directory-size: 10GB
```
DuckDB stores its data in `plugins/CoreProtect/database.duckdb`; CoreProtect manages it directly, so no DuckDB service is needed. `duckdb-memory-limit` controls the buffer manager, although total native memory can be higher, and `duckdb-threads` limits query threads. `duckdb-max-temp-directory-size` caps spill data without preallocating space or limiting the database file itself. Queries that exceed both memory and available spill space can fail.
On a pristine installation where the CoreProtect data folder contains no existing files, CoreProtect verifies that DuckDB's native library can run before creating the database file. If the operating system, architecture, or native runtime is incompatible, CoreProtect records `database-type: sqlite` in the generated `config.yml`, logs a warning, and starts with SQLite instead. Existing or preconfigured installations and explicitly configured DuckDB databases never fall back automatically; an initialization error stops database startup so CoreProtect cannot silently open a separate empty history.
### ClickHouse
Configure the ClickHouse HTTP endpoint and credentials before selecting it:
```yaml
database-type: clickhouse
table-prefix: co_
clickhouse-host: 127.0.0.1
clickhouse-port: 8123
clickhouse-database: default
clickhouse-username: default
clickhouse-password: ""
clickhouse-tls: false
```
Create the configured database first with a database engine that provides persistent UUID-backed table identities. `Atomic` is the normal choice for self-hosted ClickHouse and is the default there; for example, `CREATE DATABASE coreprotect ENGINE = Atomic`. CoreProtect checks this capability before creating any tables, then creates and validates its prefixed tables and views. The account needs permission to read `system.databases` and `system.tables`, create and read/write the CoreProtect objects, run mutations and partition drops, and drop or truncate purge-target tables. Server-wide database-creation permission is not required; `#optimize` additionally requires table optimization permission.
To share a ClickHouse database and prefix, disable `database-lock` on every installation. Use the same CoreProtect version and direct endpoint to one physical ClickHouse server, with separate CoreProtect data directories. Load balancing across independent nodes, replicated tables, and distributed tables are unsupported. Synchronize system clocks; events from different installations within the same second have no guaranteed order. Use globally unique world names unless their coordinate histories should be combined.
Players must log in with a UUID before UUID-less activity under a changed or reassigned name. UUIDs preserve account IDs across name changes and distinguish successive owners of a reused name. Name lookups use the latest UUID-bearing observation; ownership is unspecified when observations conflict within one second. Simultaneous name changes are unsupported. Cached names from other servers may remain until the player is observed locally or CoreProtect reloads.
Purges require `database-lock`. Stop all installations sharing the database and prefix, enable the lock on the installation performing the purge, then restart or reload it before purging. Also stop all installations before migration. Migration updates only the initiating installation's configuration; update the others before restarting them.
## Per-World Configuration
If you'd like to modify the logging settings for a specific world, simply do the following:
+68 -99
View File
@@ -1,145 +1,114 @@
# Database Migration (SQLite ↔ MySQL)
# Database Migration
CoreProtect 23.0+ includes the ability to migrate your database between SQLite and MySQL without losing any data.
CoreProtect 23.0+ can migrate data between SQLite and MySQL while retaining the source history. CoreProtect 25.0+ adds DuckDB and ClickHouse as migration sources and destinations. Changing `database-type` by itself does not migrate any data.
> **Note:** Database migration functionality is exclusive to CoreProtect 23.0+ [Patreon builds](http://patreon.com/coreprotect) for Patron supporters.
> **Note:** Database migration is available only in [Patreon builds](https://patreon.com/coreprotect) for Patron supporters.
## Overview
The `/co migrate-db` command allows you to seamlessly transfer all your CoreProtect data from SQLite to MySQL or vice versa. This is particularly useful when:
* Starting with SQLite and wanting to upgrade to MySQL for better performance
* Moving from MySQL back to SQLite for simpler server setups
* Transferring data between different server configurations
Database migration can be used to move from SQLite or DuckDB to an external database, return to an embedded database for simpler operation, or switch between any of the supported storage engines. The active backend is always the source, and the command argument selects the destination.
## Command Usage
| Command | Parameters | Description |
| --- | --- | --- |
| `/co migrate-db` | `<sqlite|mysql>` | Migrate to the specified database type |
| `/co migrate-db` | `<sqlite|mysql|duckdb|clickhouse> [--full-validation]` | Migrate the active database to the selected backend |
**Examples:**
The command must be run from the server console, and the destination must differ from the active source.
* `/co migrate-db mysql` - Migrate from SQLite to MySQL
* `/co migrate-db sqlite` - Migrate from MySQL to SQLite
For example:
> **Console Only:** This command can only be executed from the server console, not from in-game.
```text
co migrate-db duckdb
co migrate-db clickhouse
co migrate-db duckdb --full-validation
```
---
## Before You Start
## Migration Process
1. Stop the server and make a normal backup of the source database and `config.yml`.
2. Leave `database-type` set to the source backend and keep `database-lock: true`.
3. Configure the target while the server is stopped, then start the server with the source backend still selected. MySQL and ClickHouse connection settings must be loaded before the migration starts.
4. Use a target namespace that contains no CoreProtect data. Unrelated tables in the same external database are not affected.
5. Verify that the target has enough storage and that its account has permission to create and modify the required tables.
6. Run the migration during a quiet maintenance period, with no purge, rollback, reload, or other database operation in progress.
### Step 1: Preparation
Prepare the target as follows:
**Before starting the migration:**
| Target | Preparation |
| --- | --- |
| SQLite | Move or archive an existing `plugins/CoreProtect/database.db` if it contains CoreProtect data. CoreProtect creates the file and schema when needed. |
| DuckDB | `plugins/CoreProtect/database.duckdb` must be a new database file so its row ID sequences can be initialized from the source. |
| MySQL | Configure the `mysql-*` options and create the configured database and account. The selected table-prefix namespace must contain no CoreProtect data. |
| ClickHouse | Use ClickHouse 26.1 or newer, create the configured database with persistent UUID-backed table identities (`Atomic` is the normal self-hosted choice), create the account, configure the `clickhouse-*` options, and use a table-prefix namespace containing no CoreProtect data. |
1. Ensure your server is running with your current CoreProtect database
2. Configure your new database in `config.yml` (ensure `use-mysql` is updated correctly)
3. **Important:** Do NOT restart your server or use `/co reload` after modifying the config
`table-prefix` is shared by MySQL and ClickHouse. If the source is MySQL or ClickHouse, leave its prefix unchanged; an external target will use that same prefix. If the source is SQLite or DuckDB, you may select the external target prefix before starting the migration session. Embedded SQLite and DuckDB targets always use `co_`.
### Step 2: Execute Migration
## During Migration
#### 1. **Run the migration command from your server console:**
```
co migrate-db <mysql|sqlite>
```
CoreProtect pauses database persistence, waits for active database work to finish, and copies data in bounded batches. The console shows a throttled copy progress bar with the number of successfully written records, overall percentage, current table, transfer rate, and estimated copy time remaining. It also reports each non-empty table as its copy completes. Verification, activation, and the automatic `config.yml` update follow the copy, so the ETA does not include those final stages.
#### 2. **Monitor the progress:**
* The migration will display detailed progress information
* Large databases may take considerable time to complete
* Progress bars and speed indicators will show current status
New events logged while migration is running remain in the in-memory consumer queue. After successful activation, they are written to the target; if migration fails and the source is restored, persistence resumes there instead. Queued events are not stored for recovery after a process restart, and database-backed commands may report that the database is busy. Large migrations can take hours and use significant CPU, memory, disk, and network bandwidth, so keep server activity low and do not stop or restart the server until the command reports success or failure.
#### 3. **Do not interrupt the process:**
* Avoid restarting your server during migration
* Let the process complete naturally
Do not run `/co reload`, start a purge or rollback, or otherwise change database configuration while migration is running. CoreProtect normally rejects these conflicting operations, but they should not be used as part of the migration procedure.
### Step 3: Post-Migration
## Data Integrity
**After successful completion:**
The migration copies all logical CoreProtect history and reference data, including rollback state and entity tracking. It preserves copied row IDs. MySQL, DuckDB, and ClickHouse targets also preserve allocator high-water marks; SQLite continues after the largest surviving row ID, matching its existing allocation behavior.
1. **Automatic switchover:** CoreProtect will automatically begin using the new database
2. **Verify configuration:** Ensure `use-mysql` is set correctly in your `config.yml` as per step 1
3. **Test functionality:** Perform basic CoreProtect operations to verify everything works
4. **Clean up:** Once satisfied, you may manually delete your old database files
CoreProtect checks every table's row count, minimum and maximum row IDs, and applicable allocator high-water marks before activation. By default, it also compares approximately 1% of rows in each large history table, including their complete metadata. Tables with at most 100,000 rows and reference tables (`*_map`, `user`, `world`, and `version`) receive full comparisons.
---
Samples are random contiguous ranges distributed across each million rows actually copied, rather than across the numeric row-ID space. Deleted-ID gaps therefore do not bias selection. Only selected ranges are reread for content verification, and the console reports the validation mode and progress against the number of rows selected for comparison. A small temporary file containing sample boundaries is removed after success or failure.
## Important Considerations
Sampled validation can miss differences outside the selected ranges. Add `--full-validation` to compare every copied row instead. Any detected mismatch prevents activation in either mode. Sampling reduces content comparisons, but storage-engine scans and statistics checks can still make verification expensive.
### Migration Safety
Database-lock metadata is initialized separately; ClickHouse targets also initialize their own current version row. For a ClickHouse source, the current logical version of each row is copied; superseded internal ClickHouse revisions are storage-engine history rather than separate CoreProtect records.
* **Non-destructive process:** The migration does not modify or delete your existing database
* **Interrupted migration:** If something goes wrong, simply delete the new database and continue using the old one
* **Data verification:** The process includes automatic verification to ensure data integrity
Source history rows are not deleted or replaced. Migration may update operational metadata such as database-lock heartbeats and allocator state.
### Performance & Requirements
## After a Successful Migration
* **Processing time:** Large databases with millions of records may take hours to migrate
* **Resource usage:** The migration process is resource-intensive and may impact server performance
* **Parallel processing:** SQLite to MySQL migrations use optimized parallel processing for better speed
1. CoreProtect atomically changes `database-type` in `config.yml` to the target before queued writes resume. If the legacy `use-mysql` option exists, it is updated at the same time (`true` only for MySQL). Comments and unrelated settings are preserved.
2. The running server immediately begins persisting queued and new events to the target. Keep the server in maintenance mode until the restart check below succeeds.
3. Review the completion message and server log, then test `/co status` and a representative lookup.
4. Restart once and verify that CoreProtect reconnects to the same target and can still read old and new records. No manual database selector edit is required.
5. Resume normal activity and test a newly logged change.
6. Keep the source backup until the migrated database has been verified. Archive or remove it only when you are satisfied that the migration is complete.
### Restrictions & Limitations
When a ClickHouse source or target namespace is shared, stop every peer first and run the migration from exactly one installation with `database-lock` enabled. Before restarting any peer that will use the migrated target namespace, update its database selector, connection, and prefix to match the migrated installation.
* **Console only:** Cannot be executed from in-game chat
* **Interruption handling:** If interrupted, the target database must be manually wiped before restarting
* **Patreon exclusive:** Only available in CoreProtect 23.0+ [Patreon builds](http://patreon.com/coreprotect)
## Failure and Recovery
---
The command does not resume a partial migration. A failed or interrupted attempt can leave rows, schema objects, or allocator state in the target, so clean only the failed target namespace before retrying. For DuckDB, stop the server and remove the failed target file before starting over. Never clean or replace the source database as part of retrying.
If target activation or the atomic `config.yml` update fails after verification, CoreProtect restores the source when possible and does not resume writes on the target. The selector is changed only after the target has initialized successfully, so an interruption before that change restarts on the source, while an interruption after it restarts on the verified target. If neither the target nor source can be initialized, persistence is halted to avoid writing to an unknown database state. Correct the connection or configuration problem and restart the server; any events still queued only in memory cannot survive that restart.
## Troubleshooting
### Common Issues
### Migration Does Not Start
**Migration won't start:**
* Confirm that the command is being run from the console on a CoreProtect 23.0+ Patreon build, or 25.0+ if DuckDB or ClickHouse is involved.
* Confirm that the selected target differs from the active source backend.
* Keep `database-lock` enabled and wait for any purge, rollback, reload, or previous migration to finish.
* Check that the target namespace contains no CoreProtect data. DuckDB must use a new database file.
* Verify you're using a CoreProtect 23.0+ [Patreon build](http://patreon.com/coreprotect)
* Ensure you're not trying to migrate to the same database type
* Check that no other CoreProtect operations are running
### Connection or Schema Failure
**Migration interrupted:**
* Confirm that the target settings were loaded while `database-type` still selected the source.
* Verify the target host, port, database, credentials, table prefix, and available storage.
* Confirm that the target account can create, read, insert, update or mutate, and delete or drop the required objects.
* For ClickHouse, confirm the server is version 26.1 or newer, no other process is using the same CoreProtect data directory, and every other CoreProtect installation sharing the source or target namespace is stopped.
* Manually delete/wipe the target database
* Verify server stability before restarting migration
* Consider migrating during low-activity periods
### Verification Failure or Interruption
**Performance issues:**
* Review the server log for the table and row reported by the failure.
* Keep the source database and backup intact.
* Clean the partial target namespace, use a new DuckDB file when applicable, and retry during a quieter maintenance period.
* Monitor server resources during migration
* Consider temporarily reducing server activity
* Large tables may cause temporary slowdowns
### Automatic Configuration Update Fails
**Data verification failures:**
Check that `plugins/CoreProtect/config.yml` is a regular, writable file on a filesystem that supports atomic replacement. CoreProtect reports the migration as failed and restores the source before persistence resumes. The verified target still contains the copied data, so clean only that failed target namespace before retrying; do not manually select it or delete the source.
* Check database connectivity and permissions
* Review server logs for specific error messages
* Ensure sufficient disk space on target database
## Getting Help
### Getting Help
If you encounter issues with database migration:
1. Check server logs for detailed error messages
2. Verify database permissions and connectivity
3. Ensure adequate system resources (RAM, CPU, disk space)
4. Contact support through the CoreProtect Discord with specific error details
---
## Technical Details
The migration process includes several advanced features:
* Batch processing with dynamic sizing based on performance
* Automatic retry mechanisms for temporary failures
* Data integrity verification comparing source and target records
* Progress tracking with estimated completion times
* Graceful error handling with detailed logging
The migration handles all CoreProtect data types including:
* Block changes and rollbacks
* Container transactions
* Player interactions and sessions
* Chat messages and commands
* User data and statistics
* All metadata and configuration data
If a migration fails, keep both databases intact and review the complete error and surrounding server log. When asking for help in the [CoreProtect Discord](https://discord.gg/b4DZ4jy), include your CoreProtect version, source and destination database types, the complete migration error, and the relevant log excerpt.
+1 -1
View File
@@ -21,7 +21,7 @@ A list of all language codes that can be used with CoreProtect.
Log your own block changes, perform lookups, rollbacks, restores, and more.
[**Database Migration**](/database-migration/)
Migrate your CoreProtect database between SQLite and MySQL.
Migrate your CoreProtect database between supported backends.
[**Automatic Purging**](/auto-purge/)
Automatically remove old CoreProtect data on a daily schedule.
+4 -1
View File
@@ -75,6 +75,9 @@ The following permissions can be used to restrict functionality within the plugi
* **coreprotect.lookup.kill** *(default: op)*
Can be optionally used as a negative permission to prevent entity kill lookups.
&nbsp;
* **coreprotect.lookup.spawn** *(default: op)*
Can be optionally used as a negative permission to prevent entity spawn lookups.
&nbsp;
* **coreprotect.lookup.near** *(default: op)*
Can be optionally used as a negative permission to prevent lookups via the near command.
&nbsp;
@@ -98,4 +101,4 @@ The following permissions can be used to restrict functionality within the plugi
Allows access to the CoreProtect "/core" command.
&nbsp;
* **coreprotect.coreprotect** *(default: false)*
Allows access to the CoreProtect "/coreprotect" command.
Allows access to the CoreProtect "/coreprotect" command.
+23 -3
View File
@@ -17,11 +17,18 @@ CONSUMER_TOGGLED: "Consumer queue processing has been {paused|resumed}."
CONTAINER_HEADER: "Container Transactions"
CPU_CORES: "CPU cores."
DATABASE_BUSY: "Database busy. Please try again later."
DATABASE_FALLBACK: "Unable to load {0}. Using {1}; config.yml updated."
DATABASE_INDEX_ERROR: "Unable to validate database indexes."
DATABASE_LOCKED_1: "Database locked. Waiting up to 15 seconds..."
DATABASE_LOCKED_2: "Database is already in use. Please try again."
DATABASE_LOCKED_3: "To disable database locking, set \"database-lock: false\"."
DATABASE_LOCKED_4: "Disabling database locking can result in data corruption."
DATABASE_PERSISTENCE_HALTED: "Database writes halted. Please restart the server."
DATABASE_QUEUE_LOSS: "Queued data will be lost when the server stops."
DATABASE_RECOVERY_FAILED: "Database recovery failed after {0} attempts."
DATABASE_RECOVERY_STARTED: "Database became unavailable. Attempting database recovery."
DATABASE_RECOVERY_SUCCESS: "Database recovery completed. Queued writes will resume."
DATABASE_TYPE_OVERRIDE: "Option \"database-type\" overrides \"use-mysql\"."
DATABASE_UNREACHABLE: "Database is unreachable. Discarding data and shutting down."
DEVELOPMENT_BRANCH: "Development branch detected, skipping patch scripts."
DIRT_BLOCK: "Placed a temporary safety block under you."
@@ -36,10 +43,12 @@ FIRST_VERSION: "Initial DB: {0}"
GLOBAL_LOOKUP: "Don't specify a radius to do a global lookup."
GLOBAL_ROLLBACK: "Use \"{0}\" to do a global {rollback|restore}"
HELP_ACTION_1: "Restrict the lookup to a certain action."
HELP_ACTION_2: "Examples: [a:block], [a:+block], [a:-block] [a:click], [a:container], [a:inventory], [a:item], [a:kill], [a:chat], [a:command], [a:sign], [a:session], [a:username]"
HELP_ACTION_2: "Examples: [a:block], [a:+block], [a:-block] [a:click], [a:container], [a:inventory], [a:item], [a:kill], [a:spawn], [a:chat], [a:command], [a:sign], [a:session], [a:username]"
HELP_COMMAND: "Display more info for that command."
HELP_EXCLUDE_1: "Exclude blocks/users."
HELP_EXCLUDE_2: "Examples: [e:stone], [e:Notch], [e:stone,Notch]"
HELP_FILTER_1: "Filter chat, command, or sign text."
HELP_FILTER_2: "Examples: [a:command f:/co], [a:sign f:Shop]"
HELP_HEADER: "{0} Help"
HELP_INCLUDE_1: "Include specific blocks/entities."
HELP_INCLUDE_2: "Examples: [i:stone], [i:zombie], [i:stone,wood,bedrock]"
@@ -67,8 +76,10 @@ HELP_PARAMS_7: "Exclude blocks/users from the {lookup|rollback|restore}."
HELP_PURGE_1: "Delete data older than specified time."
HELP_PURGE_2: "For example, \"{0}\" will delete all data older than one month, and only keep the last 30 days of data."
HELP_PURGE_3: "Specify the amount of time to purge."
HELP_PURGE_4: "Specify a world to limit the purge to."
HELP_PURGE_5: "Include specific blocks in the purge."
HELP_PURGE_4: "Specify a world or radius to limit the purge to."
HELP_PURGE_5: "Include specific blocks or entity kills in the purge."
HELP_PURGE_6: "Keep entity kills of these types."
HELP_PURGE_7: "Only purge entity kills."
HELP_PURGE_COMMAND: "Delete old block data."
HELP_RADIUS_1: "Specify a radius area."
HELP_RADIUS_2: "Examples: [r:10] (Only make changes within 10 blocks of you)"
@@ -113,6 +124,10 @@ LOGGING_ITEMS: "{0} items left to log. Please wait..."
LOGGING_TIME_LIMIT: "Logging time limit reached. Discarding data and shutting down."
LOOKUP_BLOCK: "{0} {placed|broke} {1}."
LOOKUP_CONTAINER: "{0} {added|removed} {1} {2}."
LOOKUP_ENTITY_INTERACTION: "{0} {clicked|sheared|leashed|unleashed} {1}."
LOOKUP_ENTITY_INTERACTION_ORIGIN: "Origin location"
LOOKUP_ENTITY_ORIGIN: "Spawn location"
LOOKUP_ENTITY_SPAWN: "{0} spawned {1}."
LOOKUP_HEADER: "{0} Lookup Results"
LOOKUP_INTERACTION: "{0} {clicked|killed} {1}."
LOOKUP_ITEM: "{0} {picked up|dropped} {1} {2}."
@@ -124,7 +139,9 @@ LOOKUP_SEARCHING: "Lookup searching. Please wait..."
LOOKUP_STORAGE: "{0} {deposited|withdrew} {1} {2}."
LOOKUP_TIME: "{0} ago"
LOOKUP_USERNAME: "{0} logged in as {1}."
MAXIMUM_PURGE_RADIUS: "The maximum purge radius is {0}."
MAXIMUM_RADIUS: "The maximum {lookup|rollback|restore} radius is {0}."
MESSAGE_FILTER_TOO_SHORT: "Minimum message filter length is {0} characters."
MISSING_ACTION_USER: "To use that action, please specify a user."
MISSING_LOOKUP_TIME: "Please specify the amount of time to {lookup|rollback|restore}."
MISSING_LOOKUP_USER: "Please specify a user or {block|radius} to lookup."
@@ -170,6 +187,7 @@ PURGE_ROWS: "{0} {row|rows} of data deleted."
PURGE_STARTED: "Data purge started on \"{0}\"."
PURGE_SUCCESS: "Data purge successful."
RAM_STATS: "{0}GB / {1}GB RAM"
RELOAD_FAILED: "Database reload failed. Please try again."
RELOAD_STARTED: "Reloading configuration - please wait."
RELOAD_SUCCESS: "Configuration successfully reloaded."
ROLLBACK_ABORTED: "Rollback or restore aborted."
@@ -190,6 +208,7 @@ SIGN_HEADER: "Sign Messages"
STATUS_AUTO_PURGE: "Cleanup: {0} {row|rows} auto purged since restart."
STATUS_CONSUMER: "Consumer: {0} {item|items} in queue."
STATUS_DATABASE: "Database: Using {0}."
STATUS_DATABASE_STATE: "({writes halted|unreachable})"
STATUS_INTEGRATION: "{0}: Integration {enabled|disabled}."
STATUS_LICENSE: "License: {0}"
STATUS_SYSTEM: "System: {0}"
@@ -211,6 +230,7 @@ UPDATE_NOTICE: "Notice: {0} is now available."
UPGRADE_IN_PROGRESS: "Upgrade in progress. Please try again later."
USER_NOT_FOUND: "User \"{0}\" not found."
USER_OFFLINE: "The user \"{0}\" is not online."
USING_DATABASE: "Using {0} for data storage."
USING_MYSQL: "Using MySQL for data storage."
USING_SQLITE: "Using SQLite for data storage."
VALID_DONATION_KEY: "Valid donation key."
+144 -126
View File
@@ -1,202 +1,220 @@
# CoreProtect Language File (ru)
ACTION_NOT_SUPPORTED: "Данное действие не поддерживается этой командой."
ACTION_NOT_SUPPORTED: "Это действие не поддерживается данной командой."
AMOUNT_BLOCK: "{0} {блок|блоков}"
AMOUNT_CHUNK: "{0} {чанк|чанков}"
AMOUNT_ENTITY: "{0} {существо|существ}"
AMOUNT_ITEM: "{0} {предмет|предметов}"
API_TEST: "Тест API прошёл успешно!"
CACHE_ERROR: "ВНИМАНИЕ: Ошибка при проверке кеша {0}."
CACHE_RELOAD: "Принудительная перезагрузка кеша {отображения|мира} из базы данных."
API_TEST: "Тестирование API завершено успешно!"
CACHE_ERROR: "ВНИМАНИЕ: Ошибка при проверке кэша {0}."
CACHE_RELOAD: "Принудительная перезагрузка кэша {сопоставлений|мира} из базы данных."
CHECK_CONFIG: "Пожалуйста, проверьте config.yml"
COMMAND_CONSOLE: "Пожалуйста, запустите команду из консоли."
COMMAND_CONSOLE: "Пожалуйста, запустите эту команду из консоли."
COMMAND_NOT_FOUND: "Команда «{0}» не найдена."
COMMAND_THROTTLED: "Подождите немного и попробуйте еще раз."
CONSUMER_ERROR: "Обработка потребительской очереди уже {приостановлено|возобновлено}."
CONSUMER_TOGGLED: "Обработка очереди потребителей была {приостановлено|возобновлено}."
CONTAINER_HEADER: "Контейнерные транзакции"
COMMAND_THROTTLED: "Пожалуйста, подождите немного и попробуйте ещё раз."
CONSUMER_ERROR: "Обработка очереди потребителя уже {приостановлена|возобновлена}."
CONSUMER_TOGGLED: "Обработка очереди потребителя была {приостановлена|возобновлена}."
CONTAINER_HEADER: "Транзакции контейнеров"
CPU_CORES: "Ядер процессора."
DATABASE_BUSY: "База данных занята. Пожалуйста, повторите попытку позже."
DATABASE_INDEX_ERROR: "Невозможно проверить индексы базы данных."
DATABASE_LOCKED_1: "База данных заблокирована. Подождите около 15 секунд..."
DATABASE_LOCKED_2: "База данных уже используется. Пожалуйста, попробуйте еще раз."
DATABASE_INDEX_ERROR: "Не удалось проверить индексы базы данных."
DATABASE_LOCKED_1: "База данных заблокирована. Ожидание до 15 секунд..."
DATABASE_LOCKED_2: "База данных уже используется. Пожалуйста, попробуйте ещё раз."
DATABASE_LOCKED_3: "Чтобы отключить блокировку базы данных, установите «database-lock: false»."
DATABASE_LOCKED_4: "Отключение блокировки базы данных может привести к повреждению данных."
DATABASE_UNREACHABLE: "База данных недоступна. Удаление данных и завершение работы."
DEVELOPMENT_BRANCH: "Обнаружена ветвь разработки, скрипты исправлений пропущены."
DIRT_BLOCK: "Под вами установлен блок грязи."
DISABLE_SUCCESS: "{0} был успешно отключён!"
ENABLE_FAILED: "{0} не удалось запустить."
ENABLE_SUCCESS: "{0} был успешно запущен!"
DATABASE_UNREACHABLE: "База данных недоступна. Сброс данных и завершение работы."
DEVELOPMENT_BRANCH: "Обнаружена ветка разработки, выполнение скриптов обновления пропущено."
DIRT_BLOCK: "Под вами размещён временный безопасный блок."
DISABLE_SUCCESS: "Успешно! {0} отключён."
DONATION_KEY_REQUIRED: "Для этой команды требуется действующий ключ пожертвования."
ENABLE_FAILED: "Не удалось запустить {0}."
ENABLE_SUCCESS: "{0} успешно запущен!"
ENJOY_COREPROTECT: "Нравится {0}? Присоединяйтесь к нашему Discord!"
FINISHING_CONVERSION: "Завершение преобразования данных. Пожалуйста, подождите..."
FINISHING_LOGGING: "Завершение регистрации данных. Пожалуйста, подождите..."
FINISHING_LOGGING: "Завершение записи данных. Пожалуйста, подождите..."
FIRST_VERSION: "Исходная БД: {0}"
GLOBAL_LOOKUP: "Не указывайте радиус для глобального поиска."
GLOBAL_ROLLBACK: "Используйте «{0}», чтобы сделать {откат|восстановление} глобально"
HELP_ACTION_1: "Ограничьте поиск определенным действием."
GLOBAL_ROLLBACK: "Используйте «{0}» для глобального {отката|восстановления}."
HELP_ACTION_1: "Ограничить поиск определённым действием."
HELP_ACTION_2: "Например: [a:block], [a:+block], [a:-block] [a:click], [a:container], [a:inventory], [a:item], [a:kill], [a:chat], [a:command], [a:sign], [a:session], [a:username]"
HELP_COMMAND: "Показать дополнительную информацию."
HELP_EXCLUDE_1: "Исключите блоки/пользователей."
HELP_COMMAND: "Показать подробную информацию о команде."
HELP_EXCLUDE_1: "Исключить блоки или пользователей."
HELP_EXCLUDE_2: "Например: [e:stone], [e:Notch], [e:stone,Notch]"
HELP_HEADER: "{0} Помощь"
HELP_INCLUDE_1: "Включите определенные блоки/сущности."
HELP_HEADER: "{0} Справка"
HELP_INCLUDE_1: "Включить определённые блоки или сущности."
HELP_INCLUDE_2: "Например: [i:stone], [i:zombie], [i:stone,wood,bedrock]"
HELP_INSPECT_1: "С включенным режимом инспектора вы можете сделать следующее:"
HELP_INSPECT_2: "Щелкните левой кнопкой мыши по блоку, чтобы увидеть, кто разместил этот блок.."
HELP_INSPECT_3: "Щелкните блок правой кнопкой мыши, чтобы увидеть, какой соседний блок был сломан.."
HELP_INSPECT_4: "Поместите блок, чтобы увидеть, какой блок был сломан в этом месте."
HELP_INSPECT_5: "Поместите блок в жидкость (и т.п.), чтобы увидеть, кто его поместил."
HELP_INSPECT_6: "Щелкните правой кнопкой мыши дверь, сундук и т.д., чтобы узнать, кто последний использовал их."
HELP_INSPECT_7: "Подсказка: Вы можете использовать «/co i» для быстрого переключения режима инспектора."
HELP_INSPECT_COMMAND: "Включает или выключает режим инспектора блоков."
HELP_LIST: "Отображает список всех команд."
HELP_LOOKUP_1: "Команда быстрого доступа."
HELP_LOOKUP_2: "Используйте после проверки блока для просмотра журналов."
HELP_LOOKUP_COMMAND: "Глобальный поиск данных блоков."
HELP_INSPECT_1: "С включённым инспектором вы можете делать следующее:"
HELP_INSPECT_2: "Нажмите ЛКМ по блоку, чтобы узнать, кто его установил."
HELP_INSPECT_3: "Нажмите ПКМ по блоку, чтобы узнать, какой соседний блок был сломан."
HELP_INSPECT_4: "Установите блок, чтобы узнать, какой блок был сломан в этом месте."
HELP_INSPECT_5: "Установите блок в жидкость (и т.п.), чтобы узнать, кто её разлил."
HELP_INSPECT_6: "Нажмите ПКМ по двери, сундуку и т.д., чтобы узнать, кто последний их использовал."
HELP_INSPECT_7: "Подсказка: используйте «/co i» для быстрого доступа."
HELP_INSPECT_COMMAND: "Включает или выключает инспектор блоков."
HELP_LIST: "Отобразить список всех команд."
HELP_LOOKUP_1: "Быстрый доступ к команде."
HELP_LOOKUP_2: "Используйте после проверки блока для просмотра логов."
HELP_LOOKUP_COMMAND: "Расширенный поиск данных блоков."
HELP_NO_INFO: "Информация о команде «{0}» не найдена."
HELP_PARAMETER: "Пожалуйста, просмотрите «{0}» для получения подробной информации о параметрах."
HELP_PARAMS_1: "Выполните {поиск|откат|восстановление}."
HELP_PARAMS_2: "Укажите пользователей для {поиска|отката|восстановления}."
HELP_PARAMS_3: "Укажите время для {поиска|отката|восстановления}."
HELP_PARAMS_4: "Укажите область радиуса, чтобы ограничить {поиск|откат|восстановление}."
HELP_PARAMS_5: "Ограничьте {поиск|откат|восстановление} определенным действием."
HELP_PARAMS_6: "Включите определенные блоки/объекты в {поиск|откат|восстановление}."
HELP_PARAMS_7: "Исключите блоки/пользователей из {поиска|отката|восстановления}."
HELP_PARAMETER: "Пожалуйста, обратитесь к «{0}» для получения подробной информации о параметрах."
HELP_PARAMS_1: "Выполнить {поиск|откат|восстановление}."
HELP_PARAMS_2: "Указать пользователей для {поиска|отката|восстановления}."
HELP_PARAMS_3: "Указать промежуток времени для {поиска|отката|восстановления}."
HELP_PARAMS_4: "Указать радиус области, чтобы ограничить {поиск|откат|восстановление}."
HELP_PARAMS_5: "Ограничить {поиск|откат|восстановление} определённым действием."
HELP_PARAMS_6: "Включить определённые блоки/сущности в {поиск|откат|восстановление}."
HELP_PARAMS_7: "Исключить блоки/пользователей из {поиска|отката|восстановления}."
HELP_PURGE_1: "Удалить данные старше указанного времени."
HELP_PURGE_2: "Например, «{0}» удалит все данные старше одного месяца и сохранит данные только за последние 30 дней."
HELP_PURGE_COMMAND: "Удалит старые данные блока."
HELP_RADIUS_1: "Укажите радиус области."
HELP_RADIUS_2: "Например: [r:10] (Внесёт изменения только в пределах 10 блоков от вас)"
HELP_RELOAD_COMMAND: "Перезагружает файл конфигурации."
HELP_RESTORE_COMMAND: "Восстановит данные блока."
HELP_ROLLBACK_COMMAND: "Откатит данные блока."
HELP_PURGE_2: "Например, «{0}» удалит все данные старше одного месяца, оставив данные только за последние 30 дней."
HELP_PURGE_3: "Указать время для очистки."
HELP_PURGE_4: "Указать мир, чтобы ограничить очистку."
HELP_PURGE_5: "Включить определённые блоки в очистку."
HELP_PURGE_COMMAND: "Удаление старых данных блоков."
HELP_RADIUS_1: "Указать радиус области."
HELP_RADIUS_2: "Например: [r:10] (изменения только в радиусе 10 блоков от вас)"
HELP_RELOAD_COMMAND: "Перезагружает конфигурационный файл."
HELP_RESTORE_COMMAND: "Восстановление данных блоков."
HELP_ROLLBACK_COMMAND: "Откат данных блоков."
HELP_STATUS: "Просмотр статуса плагина и информации о версии."
HELP_STATUS_COMMAND: "Отображает статус плагина."
HELP_TELEPORT: "Телепорт в локацию."
HELP_TIME_1: "Укажите время поиска."
HELP_TELEPORT: "Телепортироваться к локации."
HELP_TIME_1: "Указать время для поиска."
HELP_TIME_2: "Например: [t:2w,5d,7h,2m,10s], [t:5d2h], [t:2.50h]"
HELP_USER_1: "Укажите пользовател(я/ей) для поиска."
HELP_USER_1: "Указать пользователей для поиска."
HELP_USER_2: "Например: [u:Notch], [u:Notch,#enderman]"
INCOMPATIBLE_ACTION: "«{0}» нельзя использовать с этим действием."
INSPECTOR_ERROR: "Режим инспектора уже {включён|выключен}."
INSPECTOR_TOGGLED: "Режим инспектора теперь {включён|выключен}."
INTEGRATION_ERROR: "Невозможно {инициализировать|отключить} регистрацию {0}."
INTEGRATION_SUCCESS: "{0} регистрация успешно {инициализирована|отключена}."
INTEGRATION_VERSION: "Обнаружена недопустимая версия {0}."
INTERACTIONS_HEADER: "Взаимодействие с игроками"
INCOMPATIBLE_ACTION: "Параметр «{0}» нельзя использовать с этим действием."
INSPECTOR_ERROR: "Инспектор уже {включён|выключен}."
INSPECTOR_TOGGLED: "Инспектор теперь {включён|выключен}."
INTEGRATION_ERROR: "Не удалось {инициализировать|отключить} логирование {0}."
INTEGRATION_SUCCESS: "Логирование {0} успешно {инициализировано|отключено}."
INTEGRATION_VERSION: "Обнаружена несовместимая версия {0}."
INTERACTIONS_HEADER: "Взаимодействие игроков"
INVALID_ACTION: "Это недопустимое действие."
INVALID_BRANCH_1: "Неверная версия плагина (ветка не задана)."
INVALID_BRANCH_1: "Некорректная версия плагина (ветка не задана)."
INVALID_BRANCH_2: "Чтобы продолжить, установите ветку проекта на «development»."
INVALID_BRANCH_3: "Запуск кода который находится в разработке может привести к повреждению данных."
INVALID_CONTAINER: "Пожалуйста, сначала проверьте действующий контейнер."
INVALID_BRANCH_3: "Запуск кода из ветки разработки может привести к повреждению данных."
INVALID_CONTAINER: "Пожалуйста, сначала осмотрите корректный контейнер."
INVALID_DONATION_KEY: "Недействительный ключ пожертвования."
INVALID_INCLUDE: "«{0}» неверное имя блока/сущности."
INVALID_INCLUDE_COMBO: "Это недопустимая комбинация блока/сущности."
INVALID_RADIUS: "Пожалуйста, введите допустимый радиус."
INVALID_SELECTION: "Выбор {0} не найден."
INVALID_USERNAME: "«{0}» неверное имя пользователя."
INVALID_WORLD: "Укажите действительный мир."
INVALID_INCLUDE: "«{0}» - некорректное имя блока/сущности."
INVALID_INCLUDE_COMBO: "Некорректная комбинация блока/сущности."
INVALID_ITEM_ID: "Пожалуйста, введите корректный ID предмета."
INVALID_PARAMETER: "Параметр «{0}» не поддерживается."
INVALID_RADIUS: "Пожалуйста, введите корректный радиус."
INVALID_SELECTION: "Выделение {0} не найдено."
INVALID_USERNAME: "«{0}» - некорректное имя пользователя."
INVALID_WORLD: "Пожалуйста, укажите существующий мир."
LATEST_VERSION: "Последняя версия: {0}"
LINK_DISCORD: "Discord: {0}"
LINK_DOWNLOAD: "Загрузить: {0}"
LINK_DOWNLOAD: "Скачать: {0}"
LINK_PATREON: "Patreon: {0}"
LINK_WIKI_BLOCK: "Названия блоков: {0}"
LINK_WIKI_ENTITY: "Названия сущностей: {0}"
LOGGING_ITEMS: "{0} предметы регистрируются. Пожалуйста, подождите..."
LOGGING_TIME_LIMIT: "Достигнуто ограничение по времени регистрации. Удаление данных и завершение работы."
LOOKUP_BLOCK: "{0} {поставил|сломал} {1}."
LOGGING_ITEMS: "Осталось записать предметов: {0}. Пожалуйста, подождите..."
LOGGING_TIME_LIMIT: "Превышен лимит времени записи. Сброс данных и завершение работы."
LOOKUP_BLOCK: "{0} {установил|сломал} {1}."
LOOKUP_CONTAINER: "{0} {добавил|забрал} {1} {2}."
LOOKUP_HEADER: "{0} Результаты поиска"
LOOKUP_INTERACTION: "{0} {нажал|убил} {1}."
LOOKUP_ITEM: "{0} {подобрал|выкинул} {1} {2}."
LOOKUP_INTERACTION: "{0} {взаимодействовал с|убил} {1}."
LOOKUP_ITEM: "{0} {подобрал|выбросил} {1} {2}."
LOOKUP_LOGIN: "{0} {вошёл|вышел}."
LOOKUP_PAGE: "Страница {0}"
LOOKUP_PROJECTILE: "{0} {бросил|выстрелил} {1} {2}."
LOOKUP_ROWS_FOUND: "{0} {строка|строк} найдено."
LOOKUP_SEARCHING: "Ведётся поиск. Пожалуйста подождите..."
LOOKUP_STORAGE: "{0} {положил|забрал} {1} {2}."
LOOKUP_PROJECTILE: "{0} {бросил|выпустил} {1} {2}."
LOOKUP_ROWS_FOUND: "Найдено строк: {0}."
LOOKUP_SEARCHING: "Выполняется поиск. Пожалуйста, подождите..."
LOOKUP_STORAGE: "{0} {поместил|забрал} {1} {2}."
LOOKUP_TIME: "{0} назад"
LOOKUP_USERNAME: "{0} зарегистрирован как {1}."
LOOKUP_USERNAME: "{0} вошёл как {1}."
MAXIMUM_RADIUS: "Максимальный радиус для {поиска|отката|восстановления} - {0}."
MISSING_ACTION_USER: "Чтобы использовать это действие, укажите пользователя."
MISSING_LOOKUP_TIME: "Пожалуйста, укажите количество времени, чтобы использовать {поиск|откат|восстановление}."
MISSING_LOOKUP_USER: "Укажите пользователя или {блок|радиус} для поиска."
MISSING_PARAMETERS: "Пожалуйста используйте «{0}»."
MISSING_ROLLBACK_RADIUS: "Вы не указали радиус {отката|восстановления}."
MISSING_LOOKUP_TIME: "Пожалуйста, укажите промежуток времени для {поиска|отката|восстановления}."
MISSING_LOOKUP_USER: "Пожалуйста, укажите пользователя или {блок|радиус} для поиска."
MISSING_PARAMETERS: "Пожалуйста, используйте «{0}»."
MISSING_ROLLBACK_RADIUS: "Вы не указали радиус для {отката|восстановления}."
MISSING_ROLLBACK_USER: "Вы не указали пользователя для {отката|восстановления}."
MYSQL_UNAVAILABLE: "Невозможно подключиться к серверу MySQL."
MYSQL_UNAVAILABLE: "Не удалось подключиться к серверу MySQL."
NETWORK_CONNECTION: "Подключение от {0} {успешно|не удалось}. Используется {1} {2}."
NETWORK_TEST: "Тестовые данные сети успешно отправлены."
NO_DATA: "Данные не найдены в {0}."
NO_DATA_LOCATION: "В этом месте {данные|транзакции|взаимодействия|сообщения} не обнаружены.."
NO_PERMISSION: "У вас нет прав чтобы сделать это."
NO_DATA_LOCATION: "В этом месте {данные|транзакции|взаимодействия|сообщения} не найдены."
NO_PERMISSION: "У вас недостаточно прав для выполнения этого действия."
NO_RESULTS: "Результаты не найдены."
NO_RESULTS_PAGE: "Для этой страницы {результатов|данных} не найдено."
NO_ROLLBACK: "{Ожидаемый|Предыдущий} откат/восстановление не найдены."
PATCH_INTERRUPTED: "Обновление прервано. Попробую снова при перезапуске."
NO_ROLLBACK: "Не найдено {ожидающих|предыдущих} откатов/восстановлений."
PATCH_INTERRUPTED: "Обновление прервано. Попытка будет повторена при перезапуске."
PATCH_OUTDATED_1: "Невозможно обновить базы данных старше {0}."
PATCH_OUTDATED_2: "Пожалуйста, обновите поддерживаемую версию CoreProtect."
PATCH_OUTDATED_2: "Пожалуйста, обновите CoreProtect до поддерживаемой версии."
PATCH_PROCESSING: "Обработка новых данных. Пожалуйста, подождите..."
PATCH_SKIP_UPDATE: "{Пропущена таблица|Пропущен индекс} {обновление|создание|удаление} в {0}."
PATCH_SKIP_UPDATE: "{Таблица|Индекс}: пропущено {обновление|создание|удаление} в {0}."
PATCH_STARTED: "Выполняется обновление {0}. Пожалуйста, подождите..."
PATCH_SUCCESS: "Успешно обновлен до {0}."
PATCH_SUCCESS: "Успешно обновлено до {0}."
PATCH_TABLE_COMPLETED: "Таблица {0} успешно обновлена."
PATCH_TABLE_STARTED: "Обновление таблицы {0}. Пожалуйста, подождите..."
PATCH_UPGRADING: "Выполняется обновление базы данных. Пожалуйста, подождите..."
PLEASE_SELECT: "Пожалуйста выберите: «{0}» или «{1}»."
PREVIEW_CANCELLED: "Предварительный просмотр отменен."
PREVIEW_CANCELLING: "Отмена предварительного просмотра..."
PREVIEW_IN_GAME: "Предварительный просмотр откатов доступен только в игре."
PREVIEW_TRANSACTION: "Вы не можете просмотреть {контейнер|инвентарь} транзакции."
PLEASE_SELECT: "Пожалуйста, выберите: «{0}» или «{1}»."
PREVIEW_CANCELLED: "Предпросмотр отменён."
PREVIEW_CANCELLING: "Отмена предпросмотра..."
PREVIEW_IN_GAME: "Предпросмотр откатов доступен только в игре."
PREVIEW_TRANSACTION: "{Контейнер|Инвентарь}: предпросмотр транзакции недоступен."
PRIMARY_THREAD_ERROR: "Этот метод API нельзя использовать в основном потоке."
PURGE_ABORTED: "Очистка не удалась. База данных может быть повреждена."
PURGE_ERROR: "Невозможно обработать данные {0}!"
PURGE_ERROR: "Не удалось обработать данные {0}!"
PURGE_FAILED: "Очистка не удалась. Пожалуйста, повторите попытку позже."
PURGE_IN_PROGRESS: "Выполняется чистка. Пожалуйста, повторите попытку позже."
PURGE_MINIMUM_TIME: "Удалить можно только данные старше {0} {дней|часов}."
PURGE_NOTICE_1: "Учтите, что это может занять некоторое время."
PURGE_NOTICE_2: "Не перезагружайте сервер до завершения."
PURGE_IN_PROGRESS: "Выполняется очистка. Пожалуйста, повторите попытку позже."
PURGE_MINIMUM_TIME: "Вы можете очистить только данные старше {0} {дней|часов}."
PURGE_NOTICE_1: "Пожалуйста, учтите, что это может занять некоторое время."
PURGE_NOTICE_2: "Не перезапускайте сервер до завершения процесса."
PURGE_OPTIMIZING: "Оптимизация базы данных. Пожалуйста, подождите..."
PURGE_PROCESSING: "Обработка данных {0}..."
PURGE_REPAIRING: "Попытка исправления. Это может занять некоторое время..."
PURGE_ROWS: "{0} {строка|строки} данных очищены."
PURGE_STARTED: "Очистка данных началась «{0}»."
PURGE_SUCCESS: "Очистка данных прошла успешно."
RELOAD_STARTED: "Перезагрузка конфигурации - подождите."
PURGE_ROWS: "{0} {строка|строк} данных удалено."
PURGE_STARTED: "Начата очистка данных для «{0}»."
PURGE_SUCCESS: "Очистка данных успешно завершена."
RAM_STATS: "{0} ГБ / {1} ГБ ОЗУ"
RELOAD_STARTED: "Перезагрузка конфигурации - пожалуйста, подождите."
RELOAD_SUCCESS: "Конфигурация успешно перезагружена."
ROLLBACK_ABORTED: "Откат или восстановление прерваны."
ROLLBACK_CHUNKS_FOUND: "Найдено {0} {чанк|чанков} для изменения."
ROLLBACK_CHUNKS_MODIFIED: "Изменено {0}/{1} {чанк|чанков}."
ROLLBACK_COMPLETED: "{Откат|Восстановление|Предварительный просмотр} завершен для «{0}»."
ROLLBACK_EXCLUDED_USERS: "{Исключен пользователь|Исключены пользователи}: «{0}»."
ROLLBACK_INCLUDE: "{Включён|Исключён} {блок|сущность|цель} {типа|типов}: «{0}»."
ROLLBACK_IN_PROGRESS: "Откат/восстановление уже выполняется."
ROLLBACK_LENGTH: "Заняло времени: {0} {секунду|секунд}."
ROLLBACK_MODIFIED: "{Изменено|Изменены} {0}."
ROLLBACK_COMPLETED: "{Откат|Восстановление|Предпросмотр} завершён для «{0}»."
ROLLBACK_EXCLUDED_USERS: "{Исключённый пользователь|Исключённые пользователи}: «{0}»."
ROLLBACK_INCLUDE: "{Включено|Исключено} ({блок|сущность|цель}) {типа|типов}: «{0}»."
ROLLBACK_IN_PROGRESS: "Откат или восстановление уже выполняется."
ROLLBACK_LENGTH: "Затрачено времени: {0} {секунда|секунд}."
ROLLBACK_MODIFIED: "{Изменено|Изменяется} {0}."
ROLLBACK_RADIUS: "Радиус: {0} {блок|блоков}."
ROLLBACK_SELECTION: "Радиус установлен «{0}»."
ROLLBACK_STARTED: "{Откат|Восстановление|Предварительный просмотр} начат «{0}»."
ROLLBACK_SELECTION: "Радиус установлен на «{0}»."
ROLLBACK_STARTED: "{Откат|Восстановление|Предпросмотр} запущен для «{0}»."
ROLLBACK_TIME: "Временной интервал: {0}."
ROLLBACK_WORLD_ACTION: "Только {мир|действие} «{0}»."
SIGN_HEADER: "Сообщения на табличке"
STATUS_CONSUMER: "Потребитель: {0} {предмет|предметы} в очереди."
STATUS_DATABASE: "База данных: Использует {0}."
STATUS_INTEGRATION: "{0}: Интеграция {включена|отключена}."
ROLLBACK_WORLD_ACTION: "Ограничено {миром|действием} «{0}»."
SIGN_HEADER: "Сообщения на табличках"
STATUS_AUTO_PURGE: "Очистка: автоудалено {0} {строка|строк} с момента перезапуска."
STATUS_CONSUMER: "Очередь потребителя: {0} {предмет|предметов}."
STATUS_DATABASE: "База данных: используется {0}."
STATUS_INTEGRATION: "{0}: интеграция {включена|отключена}."
STATUS_LICENSE: "Лицензия: {0}"
STATUS_SYSTEM: "Система: {0}"
STATUS_VERSION: "Версия: {0}"
TELEPORTED: "Телепортирование в {0}."
TELEPORTED: "Телепортация в {0}."
TELEPORTED_SAFETY: "Телепортируем вас в безопасное место."
TELEPORT_PLAYERS: "Команду телепортации могут использовать только игроки."
TIME_DAYS: "{0} {дня|дней}"
TIME_DAYS: "{0} {день|дней}"
TIME_HOURS: "{0} {час|часов}"
TIME_MINUTES: "{0} {минута|минут}"
TIME_MONTHS: "{0} {месяц|месяцев}"
TIME_SECONDS: "{0} {секунда|секунд}"
TIME_UNITS: "{/m|/h|/d}"
TIME_WEEKS: "{0} {неделя|недель}"
TIME_YEARS: "{0} {год|лет}"
UPDATE_ERROR: "Произошла ошибка при проверке обновлений."
UPDATE_HEADER: "{0} Обновление"
UPDATE_NOTICE: "Примечание: теперь доступен {0}."
UPDATE_NOTICE: "Примечание: теперь доступна версия {0}."
UPGRADE_IN_PROGRESS: "Выполняется обновление. Пожалуйста, повторите попытку позже."
USER_NOT_FOUND: "Пользователь «{0}» не найден."
USER_OFFLINE: "Пользователь \"{0}\" не в сети."
USER_OFFLINE: "Пользователь «{0}» не в сети."
USING_MYSQL: "Для хранения данных используется MySQL."
USING_SQLITE: "Для хранения данных используется SQLite."
VALID_DONATION_KEY: "Действующий ключ пожертвования."
VERSION_INCOMPATIBLE: "{0} {1} не поддерживается."
VERSION_NOTICE: "Версия {0} уже доступна!"
VERSION_REQUIRED: "{0} {1} или выше требуется."
VERSION_REQUIRED: "Требуется {0} {1} или выше."
WORLD_NOT_FOUND: "Мир «{0}» не найден."
Executable → Regular
+32 -7
View File
@@ -2,13 +2,14 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.coreprotect</groupId>
<artifactId>CoreProtect</artifactId>
<version>24.0</version>
<version>24.1</version>
<properties>
<project.branch></project.branch>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<skipTests>true</skipTests>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.release>11</maven.compiler.release>
<clickhouse.version>0.10.0</clickhouse.version>
<duckdb.version>1.4.5.0</duckdb.version>
</properties>
<build>
<resources>
@@ -28,8 +29,7 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.15.0</version>
<configuration>
<source>11</source>
<target>11</target>
<release>${maven.compiler.release}</release>
</configuration>
<executions>
<execution>
@@ -51,6 +51,7 @@
<configuration>
<archive>
<manifestEntries>
<Multi-Release>true</Multi-Release>
<paperweight-mappings-namespace>mojang</paperweight-mappings-namespace>
</manifestEntries>
</archive>
@@ -218,7 +219,7 @@
<dependency>
<groupId>io.papermc.paper</groupId>
<artifactId>paper-api</artifactId>
<version>26.2.build.48-alpha</version> <!-- https://repo.papermc.io/repository/maven-public/ -->
<version>26.3.build.38-alpha</version> <!-- https://repo.papermc.io/repository/maven-public/io/papermc/paper/paper-api/ -->
<scope>provided</scope>
</dependency>
<dependency>
@@ -250,6 +251,24 @@
<artifactId>HikariCP</artifactId>
<version>7.0.2</version>
</dependency>
<dependency>
<groupId>org.duckdb</groupId>
<artifactId>duckdb_jdbc</artifactId>
<version>${duckdb.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.clickhouse</groupId>
<artifactId>clickhouse-jdbc</artifactId>
<version>${clickhouse.version}</version>
<classifier>all</classifier>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
@@ -258,7 +277,7 @@
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.25.4</version>
<version>2.25.5</version>
</dependency>
<dependency>
<groupId>com.github.DeadSilenceIV</groupId>
@@ -309,6 +328,12 @@
<version>3.53.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>9.6.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
@@ -6,6 +6,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
import org.bukkit.Bukkit;
import org.bukkit.Location;
@@ -18,6 +19,7 @@ import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import net.coreprotect.api.BlockAPI;
import net.coreprotect.api.EntityAPI;
import net.coreprotect.api.InventoryAPI;
import net.coreprotect.api.ItemAPI;
import net.coreprotect.api.LookupOptions;
@@ -28,6 +30,7 @@ import net.coreprotect.api.SignAPI;
import net.coreprotect.api.UsernameAPI;
import net.coreprotect.api.result.BlockResult;
import net.coreprotect.api.result.ContainerResult;
import net.coreprotect.api.result.EntityResult;
import net.coreprotect.api.result.InventoryResult;
import net.coreprotect.api.result.ItemResult;
import net.coreprotect.api.result.MessageResult;
@@ -35,12 +38,15 @@ import net.coreprotect.api.result.SignResult;
import net.coreprotect.api.result.SessionResult;
import net.coreprotect.api.result.UsernameResult;
import net.coreprotect.config.Config;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.consumer.Consumer;
import net.coreprotect.consumer.Queue;
import net.coreprotect.database.Database;
import net.coreprotect.database.Lookup;
import net.coreprotect.database.rollback.Rollback;
import net.coreprotect.language.Phrase;
import net.coreprotect.listener.player.InventoryChangeListener;
import net.coreprotect.model.action.LookupActions;
import net.coreprotect.utility.Chat;
import net.coreprotect.utility.MaterialUtils;
import net.coreprotect.utility.ErrorReporter;
@@ -56,7 +62,8 @@ public class CoreProtectAPI extends Queue {
/**
* Current version of the API
*/
private static final int API_VERSION = 12;
private static final int API_VERSION = 13;
private static final AtomicLong API_ROLLBACK_SEQUENCE = new AtomicLong();
public static class ParseResult extends net.coreprotect.api.result.ParseResult {
@@ -96,6 +103,10 @@ public class CoreProtectAPI extends Queue {
return result;
}
private static boolean isSupportedLegacyAction(Integer action) {
return action != null && (action == LookupActions.BLOCK_BREAK || action == LookupActions.BLOCK_PLACE || action == LookupActions.INTERACTION || action == LookupActions.ENTITY_KILL || action == LookupActions.ENTITY_SPAWN);
}
/**
* Returns the current API version.
*
@@ -127,7 +138,7 @@ public class CoreProtectAPI extends Queue {
* @param block
* The block to look up
* @param options
* Lookup options. User, time, and limit are applied; location and radius are ignored because the block supplies the exact location.
* Lookup options. World, location, and radius are ignored because the block supplies the exact location.
* @return List of results or null if API is disabled
*/
public List<BlockResult> blockLookup(Block block, LookupOptions options) {
@@ -137,6 +148,34 @@ public class CoreProtectAPI extends Queue {
return null;
}
/**
* Performs a typed lookup of block breaks, placements, and interactions using shared lookup options. Entity events are excluded.
*
* @param options
* Lookup options
* @return List of results or null if API is disabled
*/
public List<BlockResult> blockLookup(LookupOptions options) {
if (isEnabled()) {
return BlockAPI.performLookup(options);
}
return null;
}
/**
* Performs a typed lookup of entity spawn and kill events.
*
* @param options
* Lookup options
* @return List of results in an EntityResult format
*/
public List<EntityResult> entityLookup(LookupOptions options) {
if (isEnabled()) {
return EntityAPI.performLookup(options);
}
return null;
}
/**
* Performs a lookup on the queue data for the specified block.
*
@@ -495,7 +534,8 @@ public class CoreProtectAPI extends Queue {
return false;
}
Queue.queuePlayerInteraction(user, location.getBlock().getState(), location.getBlock().getType());
Block block = location.getBlock();
Queue.queuePlayerInteraction(user, block.getLocation(), block.getType(), block.getBlockData().getAsString());
return true;
}
@@ -991,16 +1031,15 @@ public class CoreProtectAPI extends Queue {
List<String[]> result = new ArrayList<>();
List<String> uuids = new ArrayList<>();
if (restrictUsers == null) {
restrictUsers = new ArrayList<>();
}
if (excludeUsers == null) {
excludeUsers = new ArrayList<>();
}
if (actionList == null) {
actionList = new ArrayList<>();
restrictUsers = restrictUsers == null ? new ArrayList<>() : new ArrayList<>(restrictUsers);
excludeUsers = excludeUsers == null ? new ArrayList<>() : new ArrayList<>(excludeUsers);
boolean explicitActions = actionList != null && !actionList.isEmpty();
actionList = actionList == null ? new ArrayList<>() : new ArrayList<>(actionList);
if (explicitActions) {
actionList.removeIf(actionListItem -> !isSupportedLegacyAction(actionListItem));
if (actionList.isEmpty()) {
return result;
}
}
List<Object> restrictBlocks = new ArrayList<>(restrictBlocksMap.keySet());
@@ -1010,24 +1049,22 @@ public class CoreProtectAPI extends Queue {
for (Object argBlock : restrictBlocks) {
if (argBlock instanceof Material && !addedMaterial) {
actionList.add(0);
actionList.add(1);
actionList.add(LookupActions.BLOCK_BREAK);
actionList.add(LookupActions.BLOCK_PLACE);
addedMaterial = true;
}
else if (argBlock instanceof EntityType && !addedEntity) {
actionList.add(3);
actionList.add(LookupActions.ENTITY_KILL);
addedEntity = true;
}
}
}
if (actionList.isEmpty()) {
actionList.add(0);
actionList.add(1);
actionList.add(LookupActions.BLOCK_BREAK);
actionList.add(LookupActions.BLOCK_PLACE);
}
actionList.removeIf(actionListItem -> actionListItem > 3);
if (restrictUsers.isEmpty()) {
restrictUsers.add("#global");
}
@@ -1084,8 +1121,19 @@ public class CoreProtectAPI extends Queue {
}
else {
if (!Bukkit.isPrimaryThread()) {
boolean verbose = false;
result = Rollback.performRollbackRestore(statement, null, uuids, restrictUsers, null, restrictBlocks, excludeBlocks, excludeUsers, actionList, location, argRadius, startTime, endTime, restrictWorld, false, verbose, action, 0);
String rollbackKey = "#api-" + API_ROLLBACK_SEQUENCE.incrementAndGet();
if (Consumer.claimRollback(rollbackKey) == Consumer.OperationStartResult.STARTED) {
try {
boolean verbose = false;
result = Rollback.performRollbackRestore(statement, null, uuids, restrictUsers, null, restrictBlocks, excludeBlocks, excludeUsers, actionList, location, argRadius, startTime, endTime, restrictWorld, false, verbose, action, 0);
}
finally {
Consumer.releaseRollback(rollbackKey);
}
}
else { // only print exception on development branch
ErrorReporter.report(new IllegalStateException("API rollback skipped: another database operation is active or persistence is halted"), ConfigHandler.EDITION_BRANCH.contains("-dev"));
}
}
else {
Chat.console(Phrase.build(Phrase.PRIMARY_THREAD_ERROR));
+93 -68
View File
@@ -15,8 +15,11 @@ import net.coreprotect.api.result.ContainerResult;
import net.coreprotect.config.Config;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.Database;
import net.coreprotect.database.DuckDBLookupQuery;
import net.coreprotect.database.LocationQuery;
import net.coreprotect.database.statement.UserStatement;
import net.coreprotect.utility.BlockUtils;
import net.coreprotect.utility.DatabaseUtils;
import net.coreprotect.utility.StringUtils;
import net.coreprotect.utility.WorldUtils;
import net.coreprotect.utility.ErrorReporter;
@@ -71,7 +74,9 @@ public class BlockAPI {
}
try (Statement statement = connection.createStatement()) {
String query = "SELECT time,user,action,type,data,blockdata,rolled_back FROM " + ConfigHandler.prefix + "block " + WorldUtils.getWidIndex("block") + "WHERE wid = '" + worldId + "' AND x = '" + x + "' AND z = '" + z + "' AND y = '" + y + "' AND time > '" + checkTime + "' ORDER BY rowid DESC";
String table = DuckDBLookupQuery.spatialTable(connection, "block", worldId, x, x, z, z, "spatial_rows");
String index = ConfigHandler.databaseType.isDuckDB() ? "" : WorldUtils.getWidIndex("block");
String query = "SELECT time," + ConfigHandler.databaseType.getUserColumn() + ",action,type,data,blockdata,rolled_back FROM " + table + " " + index + "WHERE " + LocationQuery.predicate("wid", " = " + worldId) + " AND " + LocationQuery.predicate("x", " = " + x) + " AND " + LocationQuery.predicate("z", " = " + z) + " AND y = " + y + " AND time > " + checkTime + " ORDER BY " + ConfigHandler.getDescendingEventOrder();
try (ResultSet results = statement.executeQuery(query)) {
while (results.next()) {
@@ -80,14 +85,10 @@ public class BlockAPI {
String resultAction = results.getString("action");
int resultType = results.getInt("type");
String resultData = results.getString("data");
byte[] resultBlockData = results.getBytes("blockdata");
byte[] resultBlockData = DatabaseUtils.getBytes(results, "blockdata");
String resultRolledBack = results.getString("rolled_back");
if (ConfigHandler.playerIdCacheReversed.get(resultUserId) == null) {
UserStatement.loadName(connection, resultUserId);
}
String resultUser = ConfigHandler.playerIdCacheReversed.get(resultUserId);
String resultUser = UserStatement.getName(connection, resultUserId);
String blockData = BlockUtils.byteDataToString(resultBlockData, resultType);
String[] lookupData = new String[] { resultTime, resultUser, String.valueOf(x), String.valueOf(y), String.valueOf(z), String.valueOf(resultType), resultData, resultAction, resultRolledBack, String.valueOf(worldId), blockData };
@@ -110,20 +111,43 @@ public class BlockAPI {
* @param block
* The block to look up
* @param options
* Lookup options. User, time, and limit are applied; location and radius are ignored because the block supplies the exact location.
* Lookup options. World, location, and radius are ignored because the block supplies the exact location.
* @return List of results in a BlockResult format
*/
public static List<BlockResult> performLookup(Block block, LookupOptions options) {
if (!Config.getGlobal().API_ENABLED || block == null || block.getWorld() == null) {
return new ArrayList<>();
}
if (options == null) {
options = LookupOptions.builder().build();
}
return performLookup(LookupOptions.builder().location(block.getLocation())
.user(options.getUser()).users(options.getUsers()).excludeUsers(options.getExcludeUsers())
.time(options.getTime()).limit(options.getLimitOffset(), options.getLimitCount())
.includeMaterials(options.getIncludeMaterials()).excludeMaterials(options.getExcludeMaterials())
.blockActions(options.getBlockActions()).build(), false);
}
/**
* Performs a typed lookup of block-related actions using shared lookup options.
*
* @param options
* Lookup options
* @return List of results in a BlockResult format
*/
public static List<BlockResult> performLookup(LookupOptions options) {
return performLookup(options, true);
}
private static List<BlockResult> performLookup(LookupOptions options, boolean blocksOnly) {
List<BlockResult> result = new ArrayList<>();
if (!Config.getGlobal().API_ENABLED) {
return result;
}
if (block == null || block.getWorld() == null) {
return result;
}
if (options == null) {
options = LookupOptions.builder().build();
}
@@ -133,46 +157,31 @@ public class BlockAPI {
return result;
}
Integer userId = MessageAPI.getUserId(connection, options.getUser());
if (userId != null && userId == -1) {
LookupFilter filter = LookupFilter.fromOptions(connection, options);
if (filter.hasInvalidUser() || filter.hasInvalidLocation()) {
return result;
}
int checkTime = 0;
if (options.getTime() > 0) {
checkTime = (int) (System.currentTimeMillis() / 1000L) - options.getTime();
}
int x = block.getX();
int y = block.getY();
int z = block.getZ();
String worldName = block.getWorld().getName();
int worldId = WorldUtils.getWorldId(worldName);
StringBuilder query = new StringBuilder("SELECT time,user,action,type,data,blockdata,rolled_back,wid,x,y,z FROM ");
query.append(ConfigHandler.prefix).append("block ").append(WorldUtils.getWidIndex("block"));
query.append("WHERE wid = ? AND x = ? AND z = ? AND y = ? AND time > ?");
if (userId != null) {
query.append(" AND user = ?");
}
query.append(" ORDER BY rowid DESC");
if (options.hasLimit()) {
query.append(" LIMIT ").append(options.getLimitOffset()).append(", ").append(options.getLimitCount());
StringBuilder query = new StringBuilder("SELECT time," + ConfigHandler.databaseType.getUserColumn() + ",action,type,data,blockdata,rolled_back,wid,x,y,z FROM ");
query.append(filter.table(connection, "block", "")).append(' ');
if (filter.hasLocation() && !ConfigHandler.databaseType.isDuckDB()) {
query.append(WorldUtils.getWidIndex("block"));
}
filter.appendWhere(query);
filter.appendBlockMaterialWhere(query);
int[] actions = blocksOnly && options.getBlockActions().isEmpty()
? new int[] { BlockAction.BREAK.id(), BlockAction.PLACE.id(), BlockAction.INTERACTION.id() }
: options.getBlockActions().stream().mapToInt(BlockAction::id).toArray();
LookupFilter.appendActionWhere(query, "", actions);
query.append(" ORDER BY ").append(ConfigHandler.getDescendingEventOrder());
filter.appendLimit(query);
try (PreparedStatement statement = connection.prepareStatement(query.toString())) {
statement.setInt(1, worldId);
statement.setInt(2, x);
statement.setInt(3, z);
statement.setInt(4, y);
statement.setInt(5, checkTime);
if (userId != null) {
statement.setInt(6, userId);
}
filter.bind(statement);
try (ResultSet results = statement.executeQuery()) {
while (results.next()) {
result.add(parseBlockResult(connection, results, worldName));
result.add(parseBlockResult(connection, results, WorldUtils.getWorldName(results.getInt("wid"))));
}
}
}
@@ -205,7 +214,8 @@ public class BlockAPI {
* Performs a lookup of container transactions using shared lookup options.
*
* @param options
* Lookup options
* Lookup options. Tracked entity-container rows match their original or persisted current/final location and return the persisted
* current/final location.
* @return List of results in a ContainerResult format
*/
public static List<ContainerResult> performContainerLookup(LookupOptions options) {
@@ -215,6 +225,10 @@ public class BlockAPI {
return result;
}
if (options == null) {
options = LookupOptions.builder().build();
}
try (Connection connection = Database.getConnection(false, 1000)) {
if (connection == null) {
return result;
@@ -225,24 +239,41 @@ public class BlockAPI {
return result;
}
StringBuilder query = new StringBuilder("SELECT time,user,wid,x,y,z,action,type,data,amount,metadata,rolled_back FROM ");
query.append(ConfigHandler.prefix).append("container ");
if (filter.hasLocation()) {
query.append(WorldUtils.getWidIndex("container"));
}
filter.appendWhere(query);
query.append(" ORDER BY rowid DESC");
filter.appendLimit(query);
boolean snapshot = filter.beginDuckDBSnapshot(connection);
try {
StringBuilder containerWhere = new StringBuilder();
filter.appendWhere(containerWhere, "container_rows");
filter.appendMaterialWhere(containerWhere, "container_rows");
int[] actions = options.getContainerActions().stream().mapToInt(ContainerAction::id).toArray();
LookupFilter.appendActionWhere(containerWhere, "container_rows", actions);
StringBuilder entityWhere = new StringBuilder();
filter.appendEntityContainerWhere(entityWhere, "entity_rows", "spawn_rows");
filter.appendMaterialWhere(entityWhere, "entity_rows");
LookupFilter.appendActionWhere(entityWhere, "entity_rows", actions);
try (PreparedStatement statement = connection.prepareStatement(query.toString())) {
filter.bind(statement);
StringBuilder query = new StringBuilder("SELECT * FROM (");
query.append("SELECT 0 AS source,container_rows.rowid AS id,container_rows.time,container_rows.").append(ConfigHandler.databaseType.getUserColumn()).append(",container_rows.wid,container_rows.x,container_rows.y,container_rows.z,container_rows.action,container_rows.type,container_rows.data,container_rows.amount,container_rows.metadata,container_rows.rolled_back FROM ")
.append(filter.table(connection, "container", "container_rows")).append(' ').append(containerWhere);
query.append(" UNION ALL ");
query.append("SELECT 1 AS source,entity_rows.rowid AS id,entity_rows.time,entity_rows.").append(ConfigHandler.databaseType.getUserColumn()).append(",spawn_rows.current_wid AS wid,spawn_rows.x,spawn_rows.y,spawn_rows.z,entity_rows.action,entity_rows.type,entity_rows.data,entity_rows.amount,entity_rows.metadata,entity_rows.rolled_back FROM ")
.append(filter.entityContainerTable(connection, "entity_rows")).append(" JOIN ").append(ConfigHandler.prefix).append("entity_spawn spawn_rows ON spawn_rows.rowid=entity_rows.entity_spawn_rowid ").append(entityWhere);
query.append(") AS container_lookup ORDER BY time DESC,source DESC,id DESC");
filter.appendLimit(query);
try (ResultSet results = statement.executeQuery()) {
while (results.next()) {
result.add(parseContainerResult(connection, results));
try (PreparedStatement statement = connection.prepareStatement(query.toString())) {
int parameterIndex = filter.bind(statement);
filter.bindEntityContainer(statement, parameterIndex);
try (ResultSet results = statement.executeQuery()) {
while (results.next()) {
result.add(parseContainerResult(connection, results));
}
}
}
}
finally {
filter.endDuckDBSnapshot(connection, snapshot);
}
}
catch (Exception e) {
ErrorReporter.report(e);
@@ -253,27 +284,21 @@ public class BlockAPI {
private static ContainerResult parseContainerResult(Connection connection, ResultSet results) throws Exception {
int resultUserId = results.getInt("user");
String resultUser = ConfigHandler.playerIdCacheReversed.get(resultUserId);
if (resultUser == null) {
resultUser = UserStatement.loadName(connection, resultUserId);
}
String resultUser = UserStatement.getName(connection, resultUserId);
return new ContainerResult(
results.getLong("time"), resultUser, WorldUtils.getWorldName(results.getInt("wid")), results.getInt("x"), results.getInt("y"), results.getInt("z"),
results.getInt("type"), results.getInt("data"), results.getInt("amount"), results.getBytes("metadata"),
results.getLong("time"), resultUser, WorldUtils.getWorldName(results.getInt("wid")), (int) Math.floor(results.getDouble("x")), (int) Math.floor(results.getDouble("y")), (int) Math.floor(results.getDouble("z")),
results.getInt("type"), results.getInt("data"), results.getInt("amount"), DatabaseUtils.getBytes(results, "metadata"),
results.getInt("action"), results.getInt("rolled_back")
);
}
private static BlockResult parseBlockResult(Connection connection, ResultSet results, String worldName) throws Exception {
int resultUserId = results.getInt("user");
String resultUser = ConfigHandler.playerIdCacheReversed.get(resultUserId);
if (resultUser == null) {
resultUser = UserStatement.loadName(connection, resultUserId);
}
String resultUser = UserStatement.getName(connection, resultUserId);
int resultType = results.getInt("type");
String blockData = BlockUtils.byteDataToString(results.getBytes("blockdata"), resultType);
String blockData = BlockUtils.byteDataToString(DatabaseUtils.getBytes(results, "blockdata"), resultType);
return new BlockResult(
results.getLong("time"), resultUser, worldName, results.getInt("x"), results.getInt("y"), results.getInt("z"),
@@ -0,0 +1,22 @@
package net.coreprotect.api;
import net.coreprotect.model.action.LookupActions;
/**
* Block actions used by typed block lookup filters.
*/
public enum BlockAction {
BREAK(LookupActions.BLOCK_BREAK),
PLACE(LookupActions.BLOCK_PLACE),
INTERACTION(LookupActions.INTERACTION);
private final int id;
BlockAction(int id) {
this.id = id;
}
public int id() {
return id;
}
}
@@ -0,0 +1,21 @@
package net.coreprotect.api;
import net.coreprotect.model.item.ItemTransactionActions;
/**
* Container transaction actions used by typed container lookup filters.
*/
public enum ContainerAction {
REMOVE(ItemTransactionActions.REMOVE),
ADD(ItemTransactionActions.ADD);
private final int id;
ContainerAction(int id) {
this.id = id;
}
public int id() {
return id;
}
}
@@ -17,7 +17,8 @@ public enum CoreProtectAction {
USERNAME(LookupActions.USERNAME),
SIGN(LookupActions.SIGN),
ITEM(LookupActions.ITEM),
UNKNOWN(-1);
UNKNOWN(-1),
ENTITY_SPAWN(LookupActions.ENTITY_SPAWN);
private final int id;
@@ -0,0 +1,110 @@
package net.coreprotect.api;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.StringJoiner;
import org.bukkit.entity.EntityType;
import net.coreprotect.api.result.EntityResult;
import net.coreprotect.config.Config;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.Database;
import net.coreprotect.database.statement.UserStatement;
import net.coreprotect.model.action.LookupActions;
import net.coreprotect.utility.ErrorReporter;
import net.coreprotect.utility.WorldUtils;
/**
* Provides API methods for looking up entity spawn and kill events.
*/
public class EntityAPI {
private EntityAPI() {
throw new IllegalStateException("API class");
}
/**
* Performs a typed entity lookup, matching original or persisted current/final spawn locations.
*
* @param options
* Lookup options
* @return List of entity results at their original event locations
*/
public static List<EntityResult> performLookup(LookupOptions options) {
List<EntityResult> result = new ArrayList<>();
if (!Config.getGlobal().API_ENABLED) {
return result;
}
if (options == null) {
options = LookupOptions.builder().build();
}
try (Connection connection = Database.getConnection(false, 1000)) {
if (connection == null) {
return result;
}
LookupFilter filter = LookupFilter.fromOptions(connection, options);
if (filter.hasInvalidUser() || filter.hasInvalidLocation()) {
return result;
}
boolean snapshot = filter.beginDuckDBSnapshot(connection);
try {
StringBuilder query = new StringBuilder("SELECT entity_rows.time,entity_rows." + ConfigHandler.databaseType.getUserColumn() + ",entity_rows.wid,entity_rows.x,entity_rows.y,entity_rows.z,entity_rows.type,entity_rows.action,entity_rows.rolled_back FROM ");
query.append(filter.entityTable(connection, "entity_rows"));
query.append(' ');
filter.appendEntityWhere(connection, query, "entity_rows");
int[] actions = options.getEntityActions().isEmpty()
? new int[] { LookupActions.ENTITY_KILL, LookupActions.ENTITY_SPAWN }
: options.getEntityActions().stream().mapToInt(EntityAction::id).toArray();
LookupFilter.appendActionWhere(query, "entity_rows", actions);
if (!options.getIncludeEntities().isEmpty()) {
query.append(" AND ").append(entityPredicate(options.getIncludeEntities()));
}
if (!options.getExcludeEntities().isEmpty()) {
query.append(" AND NOT ").append(entityPredicate(options.getExcludeEntities()));
}
query.append(" ORDER BY ").append(ConfigHandler.getDescendingEventOrder().replace("time", "entity_rows.time").replace("rowid", "entity_rows.rowid"));
filter.appendLimit(query);
try (PreparedStatement statement = connection.prepareStatement(query.toString())) {
filter.bindTrackedEntity(statement, 1);
try (ResultSet results = statement.executeQuery()) {
while (results.next()) {
result.add(new EntityResult(
results.getLong("time"), UserStatement.getName(connection, results.getInt("user")), WorldUtils.getWorldName(results.getInt("wid")),
results.getInt("x"), results.getInt("y"), results.getInt("z"), results.getInt("type"), results.getInt("action"), results.getInt("rolled_back")
));
}
}
}
}
finally {
filter.endDuckDBSnapshot(connection, snapshot);
}
}
catch (Exception e) {
ErrorReporter.report(e);
}
return result;
}
private static String entityPredicate(List<EntityType> entities) {
StringJoiner names = new StringJoiner(",");
for (EntityType entity : entities) {
String name = entity.name().toLowerCase(Locale.ROOT);
names.add("'" + name + "'");
names.add("'minecraft:" + name + "'");
}
String predicate = "entity_rows.type IN (SELECT id FROM " + ConfigHandler.prefix + "entity_map WHERE LOWER(entity) IN (" + names + "))";
if (entities.contains(EntityType.PLAYER)) {
predicate += " OR (entity_rows.action=" + LookupActions.ENTITY_KILL + " AND entity_rows.type=0)";
}
return "(" + predicate + ")";
}
}
@@ -0,0 +1,21 @@
package net.coreprotect.api;
import net.coreprotect.model.action.LookupActions;
/**
* Entity actions used by typed entity lookup filters.
*/
public enum EntityAction {
KILL(LookupActions.ENTITY_KILL),
SPAWN(LookupActions.ENTITY_SPAWN);
private final int id;
EntityAction(int id) {
this.id = id;
}
public int id() {
return id;
}
}
@@ -5,17 +5,21 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;
import net.coreprotect.api.result.InventoryResult;
import net.coreprotect.config.Config;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.Database;
import net.coreprotect.database.statement.UserStatement;
import net.coreprotect.model.item.InventorySources;
import net.coreprotect.utility.WorldUtils;
import net.coreprotect.utility.DatabaseUtils;
import net.coreprotect.utility.ErrorReporter;
/**
* Provides API methods for normalized player inventory transaction lookups.
* Provides API methods for normalized player inventory transaction lookups. Tracked entity-container rows match their original or persisted
* current/final location and expose the persisted current/final location as container-source results.
*/
public class InventoryAPI {
@@ -44,26 +48,49 @@ public class InventoryAPI {
return result;
}
StringBuilder whereBuilder = new StringBuilder();
filter.appendWhere(whereBuilder);
String where = whereBuilder.toString();
String query = buildQuery(where, options.hasLimit());
boolean snapshot = filter.beginDuckDBSnapshot(connection);
try {
StringBuilder whereBuilder = new StringBuilder();
filter.appendWhere(whereBuilder);
StringBuilder blockWhereBuilder = new StringBuilder(whereBuilder);
filter.appendMaterialWhere(blockWhereBuilder, "", true);
filter.appendMaterialWhere(whereBuilder);
String where = whereBuilder.toString();
StringBuilder entityWhereBuilder = new StringBuilder();
filter.appendEntityContainerWhere(entityWhereBuilder, "entity_rows", "spawn_rows");
filter.appendMaterialWhere(entityWhereBuilder, "entity_rows");
String query = buildQuery(
blockWhereBuilder.toString(),
where,
entityWhereBuilder.toString(),
options.getInventoryActions(),
options.hasLimit(),
filter.table(connection, "block", ""),
filter.table(connection, "container", ""),
filter.entityContainerTable(connection, "entity_rows"),
filter.table(connection, "item", "")
);
try (PreparedStatement statement = connection.prepareStatement(query)) {
int parameterIndex = filter.bind(statement);
parameterIndex = filter.bind(statement, parameterIndex);
parameterIndex = filter.bind(statement, parameterIndex);
if (options.hasLimit()) {
statement.setInt(parameterIndex++, options.getLimitOffset());
statement.setInt(parameterIndex, options.getLimitCount());
}
try (PreparedStatement statement = connection.prepareStatement(query)) {
int parameterIndex = filter.bind(statement);
parameterIndex = filter.bind(statement, parameterIndex);
parameterIndex = filter.bindEntityContainer(statement, parameterIndex);
parameterIndex = filter.bind(statement, parameterIndex);
if (options.hasLimit()) {
statement.setInt(parameterIndex++, options.getLimitCount());
statement.setInt(parameterIndex, options.getLimitOffset());
}
try (ResultSet results = statement.executeQuery()) {
while (results.next()) {
result.add(parseInventoryResult(connection, results));
try (ResultSet results = statement.executeQuery()) {
while (results.next()) {
result.add(parseInventoryResult(connection, results));
}
}
}
}
finally {
filter.endDuckDBSnapshot(connection, snapshot);
}
}
catch (Exception e) {
ErrorReporter.report(e);
@@ -72,36 +99,57 @@ public class InventoryAPI {
return result;
}
private static String buildQuery(String where, boolean hasLimit) {
private static String buildQuery(String blockWhere, String where, String entityWhere, List<InventoryAction> actions, boolean hasLimit, String blockTable, String containerTable, String entityContainerTable, String itemTable) {
StringBuilder query = new StringBuilder("SELECT * FROM (");
query.append("SELECT 0 AS source,rowid AS id,time,user,wid,x,y,z,type,data,1 AS amount,meta AS metadata,action,rolled_back FROM ")
.append(ConfigHandler.prefix).append("block ").append(where).append(" AND action = 1");
query.append("SELECT 0 AS source,rowid AS id,time,").append(ConfigHandler.databaseType.getUserColumn()).append(",wid,x,y,z,type,data,1 AS amount,meta AS metadata,action,rolled_back FROM ")
.append(blockTable).append(' ').append(blockWhere).append(" AND action = 1");
query.append(" UNION ALL ");
query.append("SELECT 1 AS source,rowid AS id,time,user,wid,x,y,z,type,data,amount,metadata,action,rolled_back FROM ")
.append(ConfigHandler.prefix).append("container ").append(where);
query.append("SELECT 1 AS source,rowid AS id,time,").append(ConfigHandler.databaseType.getUserColumn()).append(",wid,x,y,z,type,data,amount,metadata,action,rolled_back FROM ")
.append(containerTable).append(' ').append(where);
query.append(" UNION ALL ");
query.append("SELECT 2 AS source,rowid AS id,time,user,wid,x,y,z,type,0 AS data,amount,data AS metadata,action,rolled_back FROM ")
.append(ConfigHandler.prefix).append("item ").append(where);
query.append(") AS inventory_lookup ORDER BY time DESC, source DESC, id DESC");
query.append("SELECT ").append(InventorySources.ENTITY_CONTAINER).append(" AS source,entity_rows.rowid AS id,entity_rows.time,entity_rows.").append(ConfigHandler.databaseType.getUserColumn()).append(",spawn_rows.current_wid AS wid,spawn_rows.x,spawn_rows.y,spawn_rows.z,entity_rows.type,entity_rows.data,entity_rows.amount,entity_rows.metadata,entity_rows.action,entity_rows.rolled_back FROM ")
.append(entityContainerTable).append(" JOIN ").append(ConfigHandler.prefix).append("entity_spawn spawn_rows ON spawn_rows.rowid=entity_rows.entity_spawn_rowid ").append(entityWhere);
query.append(" UNION ALL ");
query.append("SELECT 2 AS source,rowid AS id,time,").append(ConfigHandler.databaseType.getUserColumn()).append(",wid,x,y,z,type,0 AS data,amount,data AS metadata,action,rolled_back FROM ")
.append(itemTable).append(' ').append(where);
query.append(") AS inventory_lookup");
appendActionWhere(query, actions);
query.append(" ORDER BY time DESC, source DESC, id DESC");
if (hasLimit) {
query.append(" LIMIT ?, ?");
query.append(" LIMIT ? OFFSET ?");
}
return query.toString();
}
private static void appendActionWhere(StringBuilder query, List<InventoryAction> actions) {
if (!actions.isEmpty()) {
StringJoiner predicates = new StringJoiner(" OR ");
for (InventoryAction action : actions) {
String sources = String.valueOf(action.sourceId());
if (action.sourceId() == InventorySources.CONTAINER) {
sources += "," + InventorySources.ENTITY_CONTAINER;
}
predicates.add("(source IN (" + sources + ") AND action = " + action.id() + ")");
}
query.append(" WHERE (").append(predicates).append(")");
}
}
private static InventoryResult parseInventoryResult(Connection connection, ResultSet results) throws Exception {
int userId = results.getInt("user");
String username = ConfigHandler.playerIdCacheReversed.get(userId);
if (username == null) {
username = UserStatement.loadName(connection, userId);
String username = UserStatement.getName(connection, userId);
int source = results.getInt("source");
if (source == InventorySources.ENTITY_CONTAINER) {
source = InventorySources.CONTAINER;
}
return new InventoryResult(
results.getLong("time"), username, WorldUtils.getWorldName(results.getInt("wid")),
results.getInt("x"), results.getInt("y"), results.getInt("z"),
results.getInt("type"), results.getInt("data"), results.getInt("amount"), results.getBytes("metadata"),
results.getInt("action"), results.getInt("rolled_back"), results.getInt("source")
(int) Math.floor(results.getDouble("x")), (int) Math.floor(results.getDouble("y")), (int) Math.floor(results.getDouble("z")),
results.getInt("type"), results.getInt("data"), results.getInt("amount"), DatabaseUtils.getBytes(results, "metadata"),
results.getInt("action"), results.getInt("rolled_back"), source
);
}
}
@@ -0,0 +1,42 @@
package net.coreprotect.api;
import net.coreprotect.model.action.LookupActions;
import net.coreprotect.model.item.InventorySources;
import net.coreprotect.model.item.ItemTransactionActions;
/**
* Source-specific transaction actions used by typed inventory lookup filters.
* Container actions include tracked entity-container transactions.
*/
public enum InventoryAction {
BLOCK_PLACE(InventorySources.BLOCK, LookupActions.BLOCK_PLACE),
CONTAINER_REMOVE(InventorySources.CONTAINER, ItemTransactionActions.REMOVE),
CONTAINER_ADD(InventorySources.CONTAINER, ItemTransactionActions.ADD),
ITEM_DROP(InventorySources.ITEM, ItemTransactionActions.DROP),
ITEM_PICKUP(InventorySources.ITEM, ItemTransactionActions.PICKUP),
ITEM_REMOVE_ENDER(InventorySources.ITEM, ItemTransactionActions.REMOVE_ENDER),
ITEM_ADD_ENDER(InventorySources.ITEM, ItemTransactionActions.ADD_ENDER),
ITEM_THROW(InventorySources.ITEM, ItemTransactionActions.THROW),
ITEM_SHOOT(InventorySources.ITEM, ItemTransactionActions.SHOOT),
ITEM_BREAK(InventorySources.ITEM, ItemTransactionActions.BREAK),
ITEM_DESTROY(InventorySources.ITEM, ItemTransactionActions.DESTROY),
ITEM_CREATE(InventorySources.ITEM, ItemTransactionActions.CREATE),
ITEM_SELL(InventorySources.ITEM, ItemTransactionActions.SELL),
ITEM_BUY(InventorySources.ITEM, ItemTransactionActions.BUY);
private final int sourceId;
private final int id;
InventoryAction(int sourceId, int id) {
this.sourceId = sourceId;
this.id = id;
}
public int sourceId() {
return sourceId;
}
public int id() {
return id;
}
}
+23 -15
View File
@@ -13,6 +13,7 @@ import net.coreprotect.database.Database;
import net.coreprotect.database.logger.ItemLogger;
import net.coreprotect.database.statement.UserStatement;
import net.coreprotect.utility.WorldUtils;
import net.coreprotect.utility.DatabaseUtils;
import net.coreprotect.utility.ErrorReporter;
/**
@@ -31,6 +32,10 @@ public class ItemAPI {
return result;
}
if (options == null) {
options = LookupOptions.builder().build();
}
try (Connection connection = Database.getConnection(false, 1000)) {
if (connection == null) {
return result;
@@ -41,19 +46,25 @@ public class ItemAPI {
return result;
}
StringBuilder query = new StringBuilder("SELECT time,user,wid,x,y,z,type,data,amount,action,rolled_back FROM ");
query.append(ConfigHandler.prefix).append("item ");
if (filter.hasLocation()) {
StringBuilder query = new StringBuilder("SELECT time," + ConfigHandler.databaseType.getUserColumn() + ",wid,x,y,z,type,data,amount,action,rolled_back FROM ");
query.append(filter.table(connection, "item", "")).append(' ');
if (filter.hasLocation() && !ConfigHandler.databaseType.isDuckDB()) {
query.append(WorldUtils.getWidIndex("item"));
}
filter.appendWhere(query);
query.append(" AND action NOT IN (")
.append(ItemLogger.ITEM_BREAK).append(",")
.append(ItemLogger.ITEM_DESTROY).append(",")
.append(ItemLogger.ITEM_CREATE).append(",")
.append(ItemLogger.ITEM_SELL).append(",")
.append(ItemLogger.ITEM_BUY).append(")");
query.append(" ORDER BY rowid DESC");
filter.appendMaterialWhere(query);
if (options.getItemActions().isEmpty()) {
query.append(" AND action NOT IN (")
.append(ItemLogger.ITEM_BREAK).append(",")
.append(ItemLogger.ITEM_DESTROY).append(",")
.append(ItemLogger.ITEM_CREATE).append(",")
.append(ItemLogger.ITEM_SELL).append(",")
.append(ItemLogger.ITEM_BUY).append(")");
}
else {
LookupFilter.appendActionWhere(query, "", options.getItemActions().stream().mapToInt(ItemAction::id).toArray());
}
query.append(" ORDER BY ").append(ConfigHandler.getDescendingEventOrder());
filter.appendLimit(query);
try (PreparedStatement statement = connection.prepareStatement(query.toString())) {
@@ -75,15 +86,12 @@ public class ItemAPI {
private static ItemResult parseItemResult(Connection connection, ResultSet results) throws Exception {
int userId = results.getInt("user");
String username = ConfigHandler.playerIdCacheReversed.get(userId);
if (username == null) {
username = UserStatement.loadName(connection, userId);
}
String username = UserStatement.getName(connection, userId);
return new ItemResult(
results.getLong("time"), username, WorldUtils.getWorldName(results.getInt("wid")),
results.getInt("x"), results.getInt("y"), results.getInt("z"),
results.getInt("type"), results.getInt("amount"), results.getBytes("data"),
results.getInt("type"), results.getInt("amount"), DatabaseUtils.getBytes(results, "data"),
results.getInt("action"), results.getInt("rolled_back")
);
}
@@ -0,0 +1,30 @@
package net.coreprotect.api;
import net.coreprotect.model.item.ItemTransactionActions;
/**
* Item transaction actions used by typed item lookup filters.
*/
public enum ItemAction {
DROP(ItemTransactionActions.DROP),
PICKUP(ItemTransactionActions.PICKUP),
REMOVE_ENDER(ItemTransactionActions.REMOVE_ENDER),
ADD_ENDER(ItemTransactionActions.ADD_ENDER),
THROW(ItemTransactionActions.THROW),
SHOOT(ItemTransactionActions.SHOOT),
BREAK(ItemTransactionActions.BREAK),
DESTROY(ItemTransactionActions.DESTROY),
CREATE(ItemTransactionActions.CREATE),
SELL(ItemTransactionActions.SELL),
BUY(ItemTransactionActions.BUY);
private final int id;
ItemAction(int id) {
this.id = id;
}
public int id() {
return id;
}
}
@@ -2,26 +2,57 @@ package net.coreprotect.api;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.StringJoiner;
import java.util.stream.Collectors;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.DuckDBLookupQuery;
import net.coreprotect.database.DuckDBSpatialIndex;
import net.coreprotect.database.LocationQuery;
import net.coreprotect.model.action.LookupActions;
import net.coreprotect.utility.ItemUtils;
import net.coreprotect.utility.MaterialUtils;
import net.coreprotect.utility.StringUtils;
import net.coreprotect.utility.WorldUtils;
final class LookupFilter {
private final Integer userId;
private final int checkTime;
private final Location location;
private final World world;
private final int radius;
private final int limitOffset;
private final int limitCount;
private final List<Material> includeMaterials;
private final List<Material> excludeMaterials;
private final Map<Integer, Material> materialTypes;
private final String includeUserIds;
private final String excludeUserIds;
private LookupFilter(Integer userId, int checkTime, Location location, int radius, int limitOffset, int limitCount) {
private LookupFilter(Integer userId, int checkTime, Location location, World world, int radius, int limitOffset, int limitCount, List<Material> includeMaterials, List<Material> excludeMaterials, Map<Integer, Material> materialTypes, String includeUserIds, String excludeUserIds) {
this.userId = userId;
this.checkTime = checkTime;
this.location = location;
this.world = location == null ? world : location.getWorld();
this.radius = radius;
this.limitOffset = limitOffset;
this.limitCount = limitCount;
this.includeMaterials = includeMaterials;
this.excludeMaterials = excludeMaterials;
this.materialTypes = materialTypes;
this.includeUserIds = includeUserIds;
this.excludeUserIds = excludeUserIds;
}
static LookupFilter fromOptions(Connection connection, LookupOptions options) throws Exception {
@@ -35,7 +66,19 @@ final class LookupFilter {
checkTime = (int) (System.currentTimeMillis() / 1000L) - options.getTime();
}
return new LookupFilter(userId, checkTime, options.getLocation(), options.getRadius(), options.getLimitOffset(), options.getLimitCount());
Map<Integer, Material> materialTypes = new HashMap<>();
if (!options.getIncludeMaterials().isEmpty() || !options.getExcludeMaterials().isEmpty()) {
try (PreparedStatement statement = connection.prepareStatement("SELECT id, material FROM " + ConfigHandler.prefix + "material_map");
ResultSet results = statement.executeQuery()) {
while (results.next()) {
materialTypes.put(results.getInt("id"), MaterialUtils.getTypeFromStoredName(results.getString("material")));
}
}
}
return new LookupFilter(userId, checkTime, options.getLocation(), options.getWorld(), options.getRadius(), options.getLimitOffset(), options.getLimitCount(),
options.getIncludeMaterials(), options.getExcludeMaterials(), materialTypes,
userIds(connection, options.getUsers()), userIds(connection, options.getExcludeUsers()));
}
boolean hasInvalidUser() {
@@ -50,27 +93,292 @@ final class LookupFilter {
return location != null;
}
boolean beginDuckDBSnapshot(Connection connection) throws Exception {
if (!ConfigHandler.databaseType.isDuckDB() || location == null || !connection.getAutoCommit()) {
return false;
}
connection.setAutoCommit(false);
return true;
}
void endDuckDBSnapshot(Connection connection, boolean started) throws Exception {
if (!started) {
return;
}
try {
connection.rollback();
}
finally {
connection.setAutoCommit(true);
}
}
void appendWhere(StringBuilder query) {
query.append("WHERE time > ?");
appendWhere(query, "");
}
void appendWhere(StringBuilder query, String alias) {
String qualifier = alias.isEmpty() ? "" : alias + ".";
query.append("WHERE ").append(qualifier).append("time > ?");
if (userId != null) {
query.append(" AND user = ?");
query.append(" AND ").append(qualifier).append(ConfigHandler.databaseType.getUserColumn()).append(" = ?");
}
appendUserWhere(query, alias, includeUserIds, excludeUserIds);
if (world != null) {
query.append(" AND ").append(LocationQuery.predicate(qualifier + "wid", " = ?"));
}
if (location != null) {
query.append(" AND wid = ?");
if (radius > 0) {
query.append(" AND x >= ? AND x <= ? AND z >= ? AND z <= ?");
query.append(" AND ").append(LocationQuery.predicate(qualifier + "x", " >= ?"))
.append(" AND ").append(LocationQuery.predicate(qualifier + "x", " <= ?"))
.append(" AND ").append(LocationQuery.predicate(qualifier + "z", " >= ?"))
.append(" AND ").append(LocationQuery.predicate(qualifier + "z", " <= ?"));
}
else {
query.append(" AND x = ? AND y = ? AND z = ?");
query.append(" AND ").append(LocationQuery.predicate(qualifier + "x", " = ?"))
.append(" AND ").append(qualifier).append("y = ? AND ").append(LocationQuery.predicate(qualifier + "z", " = ?"));
}
}
}
void appendEntityContainerWhere(StringBuilder query, String transactionAlias, String entityAlias) {
appendTrackedEntityWhere(query, transactionAlias, entityAlias, null);
}
void appendEntityWhere(Connection connection, StringBuilder query, String alias) {
appendTrackedEntityWhere(query, alias, "spawn_rows", table(connection, "block", "original_rows"));
}
private void appendTrackedEntityWhere(StringBuilder query, String transactionAlias, String entityAlias, String originalTable) {
String transaction = transactionAlias + ".";
String entity = entityAlias + ".";
query.append("WHERE ").append(transaction).append("time > ?");
if (userId != null) {
query.append(" AND ").append(transaction).append(ConfigHandler.databaseType.getUserColumn()).append(" = ?");
}
appendUserWhere(query, transactionAlias, includeUserIds, excludeUserIds);
if (world == null) {
return;
}
String original = originalTable == null ? transaction : "original_rows.";
String tracked = ") OR (";
String ending = "))";
if (originalTable != null) {
String trackingId = ConfigHandler.databaseType.isClickHouse() ? "accurateCastOrNull(linked_rows.data, 'UInt64')" : "linked_rows.data";
String trackedRows = "SELECT " + entity + "block_rowid FROM " + ConfigHandler.prefix + "entity_spawn " + entityAlias
+ " INNER JOIN " + ConfigHandler.prefix + "block linked_rows ON linked_rows.rowid=" + entity + "block_rowid AND " + trackingId + "=" + entity + "rowid"
+ " AND linked_rows.action=" + LookupActions.ENTITY_SPAWN + " WHERE (";
if (location == null) {
query.append(" AND (").append(LocationQuery.predicate(transaction + "wid", " = ?"))
.append(" OR ").append(transaction).append("rowid IN (").append(trackedRows).append(entity).append("current_wid = ?)))");
return;
}
// Separate location candidates keep both spatial indexes usable, including on MySQL.
query.append(" AND ").append(transaction).append("rowid IN (SELECT rowid FROM (SELECT original_rows.rowid FROM ").append(originalTable).append(" WHERE (");
tracked = ") UNION ALL " + trackedRows;
ending = ")) entity_locations)";
}
else {
query.append(" AND ((");
}
query.append(LocationQuery.predicate(original + "wid", " = ?"));
if (location == null) {
query.append(tracked).append(entity).append("current_wid = ?").append(ending);
return;
}
if (radius > 0) {
query.append(" AND ").append(LocationQuery.predicate(original + "x", " >= ?"))
.append(" AND ").append(LocationQuery.predicate(original + "x", " <= ?"))
.append(" AND ").append(LocationQuery.predicate(original + "z", " >= ?"))
.append(" AND ").append(LocationQuery.predicate(original + "z", " <= ?"));
}
else {
query.append(" AND ").append(LocationQuery.predicate(original + "x", " = ?"))
.append(" AND ").append(original).append("y = ? AND ").append(LocationQuery.predicate(original + "z", " = ?"));
}
query.append(tracked).append(entity).append("current_wid = ?");
if (radius > 0) {
query.append(" AND ").append(entity).append("x >= ? AND ").append(entity).append("x < ? AND ").append(entity).append("z >= ? AND ").append(entity).append("z < ?");
}
else {
query.append(" AND ").append(entity).append("x >= ? AND ").append(entity).append("x < ? AND ").append(entity).append("y >= ? AND ").append(entity).append("y < ? AND ").append(entity).append("z >= ? AND ").append(entity).append("z < ?");
}
query.append(ending);
}
static void appendActionWhere(StringBuilder query, String alias, int[] actions) {
if (actions.length > 0) {
StringJoiner ids = new StringJoiner(",");
for (int action : actions) {
ids.add(String.valueOf(action));
}
query.append(" AND ").append(alias.isEmpty() ? "" : alias + ".").append("action IN (").append(ids).append(")");
}
}
void appendMaterialWhere(StringBuilder query) {
appendMaterialWhere(query, "");
}
void appendMaterialWhere(StringBuilder query, String alias) {
appendMaterialWhere(query, alias, false);
}
void appendMaterialWhere(StringBuilder query, String alias, boolean inventoryBlock) {
String qualifier = alias.isEmpty() ? "" : alias + ".";
if (!includeMaterials.isEmpty()) {
query.append(" AND ").append(qualifier).append("type IN (").append(materialIds(includeMaterials, inventoryBlock)).append(")");
}
if (!excludeMaterials.isEmpty()) {
query.append(" AND ").append(qualifier).append("type NOT IN (").append(materialIds(excludeMaterials, inventoryBlock)).append(")");
}
}
void appendBlockMaterialWhere(StringBuilder query) {
String entityActions = LookupActions.ENTITY_KILL + "," + LookupActions.ENTITY_SPAWN;
if (!includeMaterials.isEmpty()) {
query.append(" AND action NOT IN (").append(entityActions).append(") AND ").append(blockMaterialPredicate(includeMaterials));
}
if (!excludeMaterials.isEmpty()) {
query.append(" AND (action IN (").append(entityActions).append(") OR NOT ").append(blockMaterialPredicate(excludeMaterials)).append(")");
}
}
private String blockMaterialPredicate(List<Material> materials) {
List<Material> blockMaterials = new ArrayList<>(materials);
blockMaterials.removeAll(List.of(Material.STONE));
return "(type IN (" + materialIds(blockMaterials, false) + ") OR " + legacyStonePredicate(materials) + ")";
}
private String legacyStonePredicate(List<Material> materials) {
StringJoiner stoneData = new StringJoiner(",");
for (int data = 1; data <= 6; data++) {
Material material = Material.getMaterial(StringUtils.nameFilter("stone", data).toUpperCase(Locale.ROOT));
if (materials.contains(material)) {
stoneData.add(String.valueOf(data));
}
}
StringJoiner predicates = new StringJoiner(" OR ");
if (materials.contains(Material.STONE)) {
predicates.add("COALESCE(data,0) NOT BETWEEN 1 AND 6");
}
if (stoneData.length() > 0) {
predicates.add("COALESCE(data,0) IN (" + stoneData + ")");
}
if (predicates.length() == 0) {
return "1 = 0";
}
return "(type IN (" + materialIds(List.of(Material.STONE), false) + ") AND (" + predicates + "))";
}
String table(Connection connection, String table, String alias) {
if (location == null) {
return ConfigHandler.prefix + table + alias(alias);
}
int x = location.getBlockX();
int z = location.getBlockZ();
int minimumX = radius > 0 ? MessageAPI.clampToInt((long) x - radius) : x;
int maximumX = radius > 0 ? MessageAPI.clampToInt((long) x + radius) : x;
int minimumZ = radius > 0 ? MessageAPI.clampToInt((long) z - radius) : z;
int maximumZ = radius > 0 ? MessageAPI.clampToInt((long) z + radius) : z;
int worldId = WorldUtils.getWorldId(location.getWorld().getName());
return DuckDBLookupQuery.spatialTable(connection, table, worldId, minimumX, maximumX, minimumZ, maximumZ, alias);
}
String entityContainerTable(Connection connection, String alias) throws Exception {
return trackedEntityTable(connection, "entity_container", alias);
}
String entityTable(Connection connection, String alias) throws Exception {
return trackedEntityTable(connection, "block", alias);
}
private String trackedEntityTable(Connection connection, String table, String alias) throws Exception {
if (location == null || !ConfigHandler.databaseType.isDuckDB()) {
return ConfigHandler.prefix + table + alias(alias);
}
int x = location.getBlockX();
int z = location.getBlockZ();
int minimumX = radius > 0 ? MessageAPI.clampToInt((long) x - radius) : x;
int maximumX = radius > 0 ? MessageAPI.clampToInt((long) x + radius) : x;
int minimumZ = radius > 0 ? MessageAPI.clampToInt((long) z - radius) : z;
int maximumZ = radius > 0 ? MessageAPI.clampToInt((long) z + radius) : z;
int worldId = WorldUtils.getWorldId(location.getWorld().getName());
boolean block = table.equals("block");
List<Long> rowIds = loadCurrentEntityRowIds(connection, block ? "block_rowid" : "rowid");
return DuckDBSpatialIndex.tableExpression(
connection,
ConfigHandler.prefix,
table,
worldId,
minimumX,
maximumX,
minimumZ,
maximumZ,
block ? Collections.emptySet() : rowIds.stream().map(Long::intValue).collect(Collectors.toList()),
block ? rowIds : Collections.emptySet(),
alias
);
}
private List<Long> loadCurrentEntityRowIds(Connection connection, String column) throws Exception {
List<Long> rowIds = new ArrayList<>();
StringBuilder query = new StringBuilder("SELECT ").append(column).append(" FROM ").append(ConfigHandler.prefix).append("entity_spawn WHERE current_wid=?");
if (radius > 0) {
query.append(" AND x>=? AND x<? AND z>=? AND z<?");
}
else {
query.append(" AND x>=? AND x<? AND y>=? AND y<? AND z>=? AND z<?");
}
try (PreparedStatement statement = connection.prepareStatement(query.toString())) {
int parameterIndex = 1;
int x = location.getBlockX();
int y = location.getBlockY();
int z = location.getBlockZ();
statement.setInt(parameterIndex++, WorldUtils.getWorldId(location.getWorld().getName()));
if (radius > 0) {
int minimumX = MessageAPI.clampToInt((long) x - radius);
int maximumX = MessageAPI.clampToInt((long) x + radius);
int minimumZ = MessageAPI.clampToInt((long) z - radius);
int maximumZ = MessageAPI.clampToInt((long) z + radius);
statement.setInt(parameterIndex++, minimumX);
statement.setLong(parameterIndex++, (long) maximumX + 1L);
statement.setInt(parameterIndex++, minimumZ);
statement.setLong(parameterIndex, (long) maximumZ + 1L);
}
else {
statement.setInt(parameterIndex++, x);
statement.setLong(parameterIndex++, (long) x + 1L);
statement.setInt(parameterIndex++, y);
statement.setLong(parameterIndex++, (long) y + 1L);
statement.setInt(parameterIndex++, z);
statement.setLong(parameterIndex, (long) z + 1L);
}
try (ResultSet resultSet = statement.executeQuery()) {
while (resultSet.next() && rowIds.size() <= 4_096) {
long rowId = resultSet.getLong(1);
if (!resultSet.wasNull()) {
rowIds.add(rowId);
}
}
}
}
return rowIds;
}
void appendLimit(StringBuilder query) {
if (limitOffset >= 0 && limitCount >= 0) {
query.append(" LIMIT ").append(limitOffset).append(", ").append(limitCount);
query.append(" LIMIT ").append(limitCount).append(" OFFSET ").append(limitOffset);
}
}
@@ -85,12 +393,14 @@ final class LookupFilter {
statement.setInt(parameterIndex++, userId);
}
if (world != null) {
statement.setInt(parameterIndex++, WorldUtils.getWorldId(world.getName()));
}
if (location != null) {
int x = location.getBlockX();
int y = location.getBlockY();
int z = location.getBlockZ();
statement.setInt(parameterIndex++, WorldUtils.getWorldId(location.getWorld().getName()));
if (radius > 0) {
statement.setInt(parameterIndex++, MessageAPI.clampToInt((long) x - radius));
statement.setInt(parameterIndex++, MessageAPI.clampToInt((long) x + radius));
@@ -106,4 +416,100 @@ final class LookupFilter {
return parameterIndex;
}
int bindEntityContainer(PreparedStatement statement, int parameterIndex) throws Exception {
return bindTrackedEntity(statement, parameterIndex);
}
int bindTrackedEntity(PreparedStatement statement, int parameterIndex) throws Exception {
statement.setInt(parameterIndex++, checkTime);
if (userId != null) {
statement.setInt(parameterIndex++, userId);
}
if (world == null) {
return parameterIndex;
}
if (location == null) {
int worldId = WorldUtils.getWorldId(world.getName());
statement.setInt(parameterIndex++, worldId);
statement.setInt(parameterIndex++, worldId);
return parameterIndex;
}
int x = location.getBlockX();
int y = location.getBlockY();
int z = location.getBlockZ();
int worldId = WorldUtils.getWorldId(location.getWorld().getName());
statement.setInt(parameterIndex++, worldId);
if (radius > 0) {
int minimumX = MessageAPI.clampToInt((long) x - radius);
int maximumX = MessageAPI.clampToInt((long) x + radius);
int minimumZ = MessageAPI.clampToInt((long) z - radius);
int maximumZ = MessageAPI.clampToInt((long) z + radius);
statement.setInt(parameterIndex++, minimumX);
statement.setInt(parameterIndex++, maximumX);
statement.setInt(parameterIndex++, minimumZ);
statement.setInt(parameterIndex++, maximumZ);
statement.setInt(parameterIndex++, worldId);
statement.setInt(parameterIndex++, minimumX);
statement.setLong(parameterIndex++, (long) maximumX + 1L);
statement.setInt(parameterIndex++, minimumZ);
statement.setLong(parameterIndex++, (long) maximumZ + 1L);
}
else {
statement.setInt(parameterIndex++, x);
statement.setInt(parameterIndex++, y);
statement.setInt(parameterIndex++, z);
statement.setInt(parameterIndex++, worldId);
statement.setInt(parameterIndex++, x);
statement.setLong(parameterIndex++, (long) x + 1L);
statement.setInt(parameterIndex++, y);
statement.setLong(parameterIndex++, (long) y + 1L);
statement.setInt(parameterIndex++, z);
statement.setLong(parameterIndex++, (long) z + 1L);
}
return parameterIndex;
}
static String userIds(Connection connection, List<String> users) throws Exception {
StringJoiner result = new StringJoiner(",");
for (String user : users) {
Integer id = MessageAPI.getUserId(connection, user);
if (id == null) {
// An empty name or #global matches every user.
return null;
}
result.add(String.valueOf(id));
}
return result.toString();
}
static void appendUserWhere(StringBuilder query, String alias, String includeUserIds, String excludeUserIds) {
String column = (alias.isEmpty() ? "" : alias + ".") + ConfigHandler.databaseType.getUserColumn();
if (includeUserIds != null && !includeUserIds.isEmpty()) {
query.append(" AND ").append(column).append(" IN (").append(includeUserIds).append(")");
}
if (excludeUserIds == null) {
query.append(" AND 1 = 0");
}
else if (!excludeUserIds.isEmpty()) {
query.append(" AND ").append(column).append(" NOT IN (").append(excludeUserIds).append(")");
}
}
private String materialIds(List<Material> materials, boolean inventoryBlock) {
StringJoiner result = new StringJoiner(",");
for (Map.Entry<Integer, Material> entry : materialTypes.entrySet()) {
Material material = inventoryBlock ? ItemUtils.itemFilter(entry.getValue(), true) : entry.getValue();
if (material != null && materials.contains(material)) {
result.add(String.valueOf(entry.getKey()));
}
}
return result.length() == 0 ? "-1" : result.toString();
}
private static String alias(String alias) {
return alias == null || alias.isEmpty() ? "" : " AS " + alias;
}
}
@@ -1,6 +1,11 @@
package net.coreprotect.api;
import java.util.List;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.EntityType;
/**
* Shared options for typed lookup API methods.
@@ -10,16 +15,46 @@ public final class LookupOptions {
private final int time;
private final int radius;
private final Location location;
private final World world;
private final int limitOffset;
private final int limitCount;
private final List<Material> includeMaterials;
private final List<Material> excludeMaterials;
private final List<String> users;
private final List<String> excludeUsers;
private final List<String> textStartsWithAny;
private final List<String> textStartsWithNone;
private final List<ContainerAction> containerActions;
private final List<ItemAction> itemActions;
private final List<InventoryAction> inventoryActions;
private final List<BlockAction> blockActions;
private final List<SessionAction> sessionActions;
private final List<EntityAction> entityActions;
private final List<EntityType> includeEntities;
private final List<EntityType> excludeEntities;
private LookupOptions(Builder builder) {
this.user = builder.user;
this.time = builder.time;
this.radius = builder.radius;
this.location = builder.location;
this.world = builder.world;
this.limitOffset = builder.limitOffset;
this.limitCount = builder.limitCount;
this.includeMaterials = builder.includeMaterials;
this.excludeMaterials = builder.excludeMaterials;
this.users = builder.users;
this.excludeUsers = builder.excludeUsers;
this.textStartsWithAny = builder.textStartsWithAny;
this.textStartsWithNone = builder.textStartsWithNone;
this.containerActions = builder.containerActions;
this.itemActions = builder.itemActions;
this.inventoryActions = builder.inventoryActions;
this.blockActions = builder.blockActions;
this.sessionActions = builder.sessionActions;
this.entityActions = builder.entityActions;
this.includeEntities = builder.includeEntities;
this.excludeEntities = builder.excludeEntities;
}
public static Builder builder() {
@@ -42,6 +77,10 @@ public final class LookupOptions {
return location;
}
public World getWorld() {
return world;
}
public int getLimitOffset() {
return limitOffset;
}
@@ -54,13 +93,84 @@ public final class LookupOptions {
return limitOffset >= 0 && limitCount >= 0;
}
public List<Material> getIncludeMaterials() {
return includeMaterials;
}
public List<Material> getExcludeMaterials() {
return excludeMaterials;
}
public List<String> getUsers() {
return users;
}
public List<String> getExcludeUsers() {
return excludeUsers;
}
public List<String> getTextStartsWithAny() {
return textStartsWithAny;
}
public List<String> getTextStartsWithNone() {
return textStartsWithNone;
}
public List<ContainerAction> getContainerActions() {
return containerActions;
}
public List<ItemAction> getItemActions() {
return itemActions;
}
public List<InventoryAction> getInventoryActions() {
return inventoryActions;
}
public List<BlockAction> getBlockActions() {
return blockActions;
}
public List<SessionAction> getSessionActions() {
return sessionActions;
}
public List<EntityAction> getEntityActions() {
return entityActions;
}
public List<EntityType> getIncludeEntities() {
return includeEntities;
}
public List<EntityType> getExcludeEntities() {
return excludeEntities;
}
public static final class Builder {
private String user;
private int time;
private int radius = -1;
private Location location;
private World world;
private int limitOffset = -1;
private int limitCount = -1;
private List<Material> includeMaterials = List.of();
private List<Material> excludeMaterials = List.of();
private List<String> users = List.of();
private List<String> excludeUsers = List.of();
private List<String> textStartsWithAny = List.of();
private List<String> textStartsWithNone = List.of();
private List<ContainerAction> containerActions = List.of();
private List<ItemAction> itemActions = List.of();
private List<InventoryAction> inventoryActions = List.of();
private List<BlockAction> blockActions = List.of();
private List<SessionAction> sessionActions = List.of();
private List<EntityAction> entityActions = List.of();
private List<EntityType> includeEntities = List.of();
private List<EntityType> excludeEntities = List.of();
private Builder() {
}
@@ -77,22 +187,121 @@ public final class LookupOptions {
public Builder location(Location location) {
this.location = location;
this.world = null;
this.radius = 0;
return this;
}
public Builder radius(Location location, int radius) {
this.location = location;
this.world = null;
this.radius = radius;
return this;
}
public Builder world(World world) {
this.world = world;
this.location = null;
this.radius = -1;
return this;
}
public Builder limit(int offset, int count) {
this.limitOffset = offset;
this.limitCount = count;
return this;
}
public Builder includeMaterials(List<Material> materials) {
this.includeMaterials = List.copyOf(materials);
return this;
}
public Builder excludeMaterials(List<Material> materials) {
this.excludeMaterials = List.copyOf(materials);
return this;
}
public Builder users(List<String> users) {
this.users = List.copyOf(users);
return this;
}
public Builder excludeUsers(List<String> users) {
this.excludeUsers = List.copyOf(users);
return this;
}
/**
* Includes chat, command and sign text matching any literal prefix.
* Empty means no inclusion restriction. Other lookup types ignore this option.
* @throws IllegalArgumentException if a prefix has fewer than three Unicode code points
*/
public Builder textStartsWithAny(List<String> prefixes) {
this.textStartsWithAny = textPrefixes(prefixes);
return this;
}
/**
* Excludes chat, command and sign text matching any literal prefix.
* Exclusions take precedence over inclusions. A leading '-' is literal.
* @throws IllegalArgumentException if a prefix has fewer than three Unicode code points
*/
public Builder textStartsWithNone(List<String> prefixes) {
this.textStartsWithNone = textPrefixes(prefixes);
return this;
}
private static List<String> textPrefixes(List<String> prefixes) {
List<String> copy = List.copyOf(prefixes);
for (String prefix : copy) {
if (prefix.codePointCount(0, prefix.length()) < 3) {
throw new IllegalArgumentException("Text filter prefixes must contain at least three code points");
}
}
return copy;
}
public Builder containerActions(List<ContainerAction> actions) {
this.containerActions = List.copyOf(actions);
return this;
}
public Builder itemActions(List<ItemAction> actions) {
this.itemActions = List.copyOf(actions);
return this;
}
public Builder inventoryActions(List<InventoryAction> actions) {
this.inventoryActions = List.copyOf(actions);
return this;
}
public Builder blockActions(List<BlockAction> actions) {
this.blockActions = List.copyOf(actions);
return this;
}
public Builder sessionActions(List<SessionAction> actions) {
this.sessionActions = List.copyOf(actions);
return this;
}
public Builder entityActions(List<EntityAction> actions) {
this.entityActions = List.copyOf(actions);
return this;
}
public Builder includeEntities(List<EntityType> entities) {
this.includeEntities = List.copyOf(entities);
return this;
}
public Builder excludeEntities(List<EntityType> entities) {
this.excludeEntities = List.copyOf(entities);
return this;
}
public LookupOptions build() {
return new LookupOptions(this);
}
@@ -10,9 +10,10 @@ import net.coreprotect.api.result.MessageResult;
import net.coreprotect.config.Config;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.Database;
import net.coreprotect.database.LookupRaw;
import net.coreprotect.database.statement.UserStatement;
import net.coreprotect.utility.WorldUtils;
import net.coreprotect.utility.ErrorReporter;
import net.coreprotect.utility.WorldUtils;
/**
* Provides API methods for player chat and command lookups.
@@ -55,17 +56,24 @@ public class MessageAPI {
return result;
}
StringBuilder query = new StringBuilder("SELECT time,user,wid,x,y,z,message FROM ");
query.append(ConfigHandler.prefix).append(table).append(" ");
if (filter.hasLocation()) {
StringBuilder query = new StringBuilder("SELECT time," + ConfigHandler.databaseType.getUserColumn() + ",wid,x,y,z,message FROM ");
query.append(filter.table(connection, table, "")).append(' ');
if (filter.hasLocation() && !ConfigHandler.databaseType.isDuckDB()) {
query.append(WorldUtils.getWidIndex(table));
}
filter.appendWhere(query);
query.append(" ORDER BY rowid DESC");
List<String> messageBindings = new ArrayList<>();
query = new StringBuilder(LookupRaw.appendMessageFilters(query.toString(),
options == null ? List.of() : options.getTextStartsWithAny(),
options == null ? List.of() : options.getTextStartsWithNone(), table, messageBindings));
query.append(" ORDER BY ").append(ConfigHandler.getDescendingEventOrder());
filter.appendLimit(query);
try (PreparedStatement statement = connection.prepareStatement(query.toString())) {
filter.bind(statement);
int parameterIndex = filter.bind(statement);
for (String binding : messageBindings) {
statement.setString(parameterIndex++, binding);
}
try (ResultSet results = statement.executeQuery()) {
while (results.next()) {
@@ -86,33 +94,12 @@ public class MessageAPI {
return null;
}
Integer cachedId = ConfigHandler.playerIdCache.get(user.toLowerCase(java.util.Locale.ROOT));
if (cachedId != null) {
return cachedId;
}
String collate = Config.getGlobal().MYSQL ? "" : " COLLATE NOCASE";
try (PreparedStatement statement = connection.prepareStatement("SELECT rowid FROM " + ConfigHandler.prefix + "user WHERE user = ?" + collate + " ORDER BY rowid ASC LIMIT 0, 1")) {
statement.setString(1, user);
try (ResultSet results = statement.executeQuery()) {
if (results.next()) {
int id = results.getInt("rowid");
ConfigHandler.playerIdCache.put(user.toLowerCase(java.util.Locale.ROOT), id);
ConfigHandler.playerIdCacheReversed.put(id, user);
return id;
}
}
}
return -1;
return UserStatement.findId(connection, user);
}
private static MessageResult parseMessageResult(Connection connection, ResultSet results, int actionId) throws Exception {
int userId = results.getInt("user");
String username = ConfigHandler.playerIdCacheReversed.get(userId);
if (username == null) {
username = UserStatement.loadName(connection, userId);
}
String username = UserStatement.getName(connection, userId);
return new MessageResult(
results.getLong("time"), username, WorldUtils.getWorldName(results.getInt("wid")),
@@ -2,6 +2,7 @@ package net.coreprotect.api;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -53,24 +54,27 @@ public class QueueLookup extends Queue {
}
try {
// Determine total count of actions in the consumer queues
int consumerCount = calculateConsumerCount();
if (consumerCount == 0) {
return result;
ArrayList<Object[]> consumerData;
Map<Integer, String[]> users;
Map<Integer, Object> consumerObject;
synchronized (Queue.class) {
synchronized (Consumer.consumer_id) {
int currentConsumer = Consumer.currentConsumer;
consumerData = new ArrayList<>(Consumer.consumer.get(currentConsumer));
users = new HashMap<>(Consumer.consumerUsers.get(currentConsumer));
consumerObject = new HashMap<>(Consumer.consumerObjects.get(currentConsumer));
}
}
// Get data from the current consumer
int currentConsumer = Consumer.currentConsumer;
ArrayList<Object[]> consumerData = Consumer.consumer.get(currentConsumer);
Map<Integer, String[]> users = Consumer.consumerUsers.get(currentConsumer);
Map<Integer, Object> consumerObject = Consumer.consumerObjects.get(currentConsumer);
if (consumerData.isEmpty()) {
return result;
}
// Current block location for comparison with actions in the queue
Location blockLocation = block.getLocation();
// Check for block actions in the processing queue
for (Object[] data : consumerData.toArray(Object[][]::new)) {
for (Object[] data : consumerData) {
int id = (int) data[0];
int action = (int) data[1];
@@ -110,22 +114,6 @@ public class QueueLookup extends Queue {
return result;
}
/**
* Calculates the total count of actions in the consumer queues.
*
* @return The total count of actions in the consumer queues
*/
private static int calculateConsumerCount() {
int currentConsumerSize = Process.getCurrentConsumerSize();
if (currentConsumerSize == 0) {
return Consumer.getConsumerSize(0) + Consumer.getConsumerSize(1);
}
else {
int consumerId = (Consumer.currentConsumer == 1) ? 1 : 0;
return Consumer.getConsumerSize(consumerId) + currentConsumerSize;
}
}
/**
* Determines if an action in the queue pertains to the specified block location.
*
@@ -0,0 +1,21 @@
package net.coreprotect.api;
import net.coreprotect.model.action.SessionActions;
/**
* Session actions used by typed session lookup filters.
*/
public enum SessionAction {
LOGOUT(SessionActions.LOGOUT),
LOGIN(SessionActions.LOGIN);
private final int id;
SessionAction(int id) {
this.id = id;
}
public int id() {
return id;
}
}
@@ -6,7 +6,6 @@ import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import net.coreprotect.api.result.SessionResult;
import net.coreprotect.config.Config;
@@ -66,8 +65,10 @@ public class SessionLookup {
int time = (int) (System.currentTimeMillis() / 1000L);
int checkTime = calculateCheckTime(time, offset);
// Get user ID from cache or load it
int userId = getUserId(connection, user);
Integer userId = MessageAPI.getUserId(connection, user);
if (userId == null || userId < 0) {
return result;
}
// Query session data from database
try (Statement statement = connection.createStatement()) {
@@ -102,6 +103,10 @@ public class SessionLookup {
return result;
}
if (options == null) {
options = LookupOptions.builder().build();
}
try (Connection connection = Database.getConnection(false, 1000)) {
if (connection == null) {
return result;
@@ -112,13 +117,14 @@ public class SessionLookup {
return result;
}
StringBuilder query = new StringBuilder("SELECT time,user,wid,x,y,z,action FROM ");
query.append(ConfigHandler.prefix).append("session ");
if (filter.hasLocation()) {
StringBuilder query = new StringBuilder("SELECT time," + ConfigHandler.databaseType.getUserColumn() + ",wid,x,y,z,action FROM ");
query.append(filter.table(connection, "session", "")).append(' ');
if (filter.hasLocation() && !ConfigHandler.databaseType.isDuckDB()) {
query.append(WorldUtils.getWidIndex("session"));
}
filter.appendWhere(query);
query.append(" ORDER BY rowid DESC");
LookupFilter.appendActionWhere(query, "", options.getSessionActions().stream().mapToInt(SessionAction::id).toArray());
query.append(" ORDER BY ").append(ConfigHandler.getDescendingEventOrder());
filter.appendLimit(query);
try (PreparedStatement statement = connection.prepareStatement(query.toString())) {
@@ -151,26 +157,6 @@ public class SessionLookup {
return (offset > 0) ? currentTime - offset : 0;
}
/**
* Gets the user ID for the specified username.
* If the user ID is not in the cache, it will be loaded from the database.
*
* @param connection
* The database connection
* @param username
* The username to get the ID for
* @return The user ID
*/
private static int getUserId(Connection connection, String username) {
String lowerUsername = username.toLowerCase(Locale.ROOT);
if (ConfigHandler.playerIdCache.get(lowerUsername) == null) {
UserStatement.loadId(connection, username, null);
}
return ConfigHandler.playerIdCache.get(lowerUsername);
}
/**
* Builds the SQL query for retrieving session data.
*
@@ -181,7 +167,7 @@ public class SessionLookup {
* @return The SQL query string
*/
private static String buildSessionQuery(int userId, int checkTime) {
return "SELECT time,user,wid,x,y,z,action FROM " + ConfigHandler.prefix + "session WHERE user = '" + userId + "' AND time > '" + checkTime + "' ORDER BY rowid DESC";
return "SELECT time," + ConfigHandler.databaseType.getUserColumn() + ",wid,x,y,z,action FROM " + ConfigHandler.prefix + "session WHERE " + ConfigHandler.databaseType.getUserColumn() + " = " + userId + " AND time > " + checkTime + " ORDER BY " + ConfigHandler.getDescendingEventOrder();
}
/**
@@ -206,11 +192,7 @@ public class SessionLookup {
String resultZ = results.getString("z");
String resultAction = results.getString("action");
// Get username from cache or load it
if (ConfigHandler.playerIdCacheReversed.get(resultUserId) == null) {
UserStatement.loadName(connection, resultUserId);
}
String resultUser = ConfigHandler.playerIdCacheReversed.get(resultUserId);
String resultUser = UserStatement.getName(connection, resultUserId);
// Create and return the session data array
return new String[] { resultTime, resultUser, resultX, resultY, resultZ, resultWorldId, type, resultAction };
@@ -218,10 +200,7 @@ public class SessionLookup {
private static SessionResult parseSessionResult(Connection connection, ResultSet results) throws Exception {
int userId = results.getInt("user");
String username = ConfigHandler.playerIdCacheReversed.get(userId);
if (username == null) {
username = UserStatement.loadName(connection, userId);
}
String username = UserStatement.getName(connection, userId);
return new SessionResult(
results.getLong("time"), username, WorldUtils.getWorldName(results.getInt("wid")),
+14 -9
View File
@@ -12,6 +12,7 @@ import net.coreprotect.api.result.SignResult;
import net.coreprotect.config.Config;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.Database;
import net.coreprotect.database.LookupRaw;
import net.coreprotect.database.statement.UserStatement;
import net.coreprotect.model.action.SignActions;
import net.coreprotect.utility.WorldUtils;
@@ -51,18 +52,25 @@ public class SignAPI {
return result;
}
StringBuilder query = new StringBuilder("SELECT time,user,wid,x,y,z,action,color,color_secondary,data,waxed,face,line_1,line_2,line_3,line_4,line_5,line_6,line_7,line_8 FROM ");
query.append(ConfigHandler.prefix).append("sign ");
if (filter.hasLocation()) {
StringBuilder query = new StringBuilder("SELECT time," + ConfigHandler.databaseType.getUserColumn() + ",wid,x,y,z,action,color,color_secondary,data,waxed,face,line_1,line_2,line_3,line_4,line_5,line_6,line_7,line_8 FROM ");
query.append(filter.table(connection, "sign", "")).append(' ');
if (filter.hasLocation() && !ConfigHandler.databaseType.isDuckDB()) {
query.append(WorldUtils.getWidIndex("sign"));
}
filter.appendWhere(query);
List<String> messageBindings = new ArrayList<>();
query = new StringBuilder(LookupRaw.appendMessageFilters(query.toString(),
options == null ? List.of() : options.getTextStartsWithAny(),
options == null ? List.of() : options.getTextStartsWithNone(), "sign", messageBindings));
query.append(" AND action = '").append(SignActions.PLACE).append("' AND (LENGTH(line_1) > 0 OR LENGTH(line_2) > 0 OR LENGTH(line_3) > 0 OR LENGTH(line_4) > 0 OR LENGTH(line_5) > 0 OR LENGTH(line_6) > 0 OR LENGTH(line_7) > 0 OR LENGTH(line_8) > 0)");
query.append(" ORDER BY rowid DESC");
query.append(" ORDER BY ").append(ConfigHandler.getDescendingEventOrder());
filter.appendLimit(query);
try (PreparedStatement statement = connection.prepareStatement(query.toString())) {
filter.bind(statement);
int parameterIndex = filter.bind(statement);
for (String binding : messageBindings) {
statement.setString(parameterIndex++, binding);
}
try (ResultSet results = statement.executeQuery()) {
while (results.next()) {
@@ -80,10 +88,7 @@ public class SignAPI {
private static SignResult parseSignResult(Connection connection, ResultSet results) throws Exception {
int userId = results.getInt("user");
String username = ConfigHandler.playerIdCacheReversed.get(userId);
if (username == null) {
username = UserStatement.loadName(connection, userId);
}
String username = UserStatement.getName(connection, userId);
String[] lines = new String[] {
valueOrEmpty(results.getString("line_1")),
@@ -6,13 +6,14 @@ import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import net.coreprotect.api.result.UsernameResult;
import net.coreprotect.config.Config;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.Database;
import net.coreprotect.database.statement.UserStatement;
import net.coreprotect.utility.DatabaseUtils;
import net.coreprotect.utility.ErrorReporter;
/**
@@ -49,21 +50,31 @@ public class UsernameAPI {
return result;
}
Set<String> includedUuids = getUuids(connection, options.getUsers());
if (includedUuids != null && includedUuids.isEmpty() && !options.getUsers().isEmpty()) {
return result;
}
Set<String> excludedUuids = getUuids(connection, options.getExcludeUsers());
if (excludedUuids == null) {
return result;
}
// A NULL in NOT IN would also exclude unrelated users.
excludedUuids.remove(null);
int checkTime = 0;
if (options.getTime() > 0) {
checkTime = (int) (System.currentTimeMillis() / 1000L) - options.getTime();
}
StringBuilder query = new StringBuilder("SELECT time,uuid,user FROM ");
StringBuilder query = new StringBuilder("SELECT time,uuid," + ConfigHandler.databaseType.getUserColumn() + " FROM ");
query.append(ConfigHandler.prefix).append("username_log WHERE time > ?");
if (!uuids.isEmpty()) {
query.append(" AND uuid IN (");
appendPlaceholders(query, uuids.size());
query.append(")");
}
query.append(" ORDER BY rowid DESC");
appendUuidFilter(query, uuids, false);
appendUuidFilter(query, includedUuids, false);
appendUuidFilter(query, excludedUuids, true);
query.append(" ORDER BY ").append(ConfigHandler.getDescendingEventOrder());
if (options.hasLimit()) {
query.append(" LIMIT ").append(options.getLimitOffset()).append(", ").append(options.getLimitCount());
query.append(" LIMIT ").append(options.getLimitCount()).append(" OFFSET ").append(options.getLimitOffset());
}
try (PreparedStatement statement = connection.prepareStatement(query.toString())) {
@@ -72,10 +83,18 @@ public class UsernameAPI {
for (String uuid : uuids) {
statement.setString(parameterIndex++, uuid);
}
if (includedUuids != null) {
for (String uuid : includedUuids) {
statement.setString(parameterIndex++, uuid);
}
}
for (String uuid : excludedUuids) {
statement.setString(parameterIndex++, uuid);
}
try (ResultSet results = statement.executeQuery()) {
while (results.next()) {
result.add(parseUsernameResult(results));
result.add(parseUsernameResult(connection, results));
}
}
}
@@ -87,13 +106,18 @@ public class UsernameAPI {
return result;
}
private static void appendPlaceholders(StringBuilder query, int count) {
for (int index = 0; index < count; index++) {
private static void appendUuidFilter(StringBuilder query, Set<String> uuids, boolean excluded) {
if (uuids == null || uuids.isEmpty()) {
return;
}
query.append(excluded ? " AND uuid NOT IN (" : " AND uuid IN (");
for (int index = 0; index < uuids.size(); index++) {
if (index > 0) {
query.append(",");
}
query.append("?");
}
query.append(")");
}
private static Set<String> getUuids(Connection connection, String user) throws Exception {
@@ -103,13 +127,8 @@ public class UsernameAPI {
return result;
}
String cachedUuid = ConfigHandler.uuidCache.get(user.toLowerCase(Locale.ROOT));
if (cachedUuid != null) {
result.add(cachedUuid);
}
String collate = Config.getGlobal().MYSQL ? "" : " COLLATE NOCASE";
try (PreparedStatement statement = connection.prepareStatement("SELECT uuid FROM " + ConfigHandler.prefix + "user WHERE user = ?" + collate)) {
String userMatch = DatabaseUtils.caseInsensitiveEquals(ConfigHandler.databaseType.getUserColumn());
try (PreparedStatement statement = connection.prepareStatement("SELECT uuid FROM " + ConfigHandler.prefix + "user WHERE " + userMatch)) {
statement.setString(1, user);
try (ResultSet results = statement.executeQuery()) {
while (results.next()) {
@@ -118,7 +137,7 @@ public class UsernameAPI {
}
}
try (PreparedStatement statement = connection.prepareStatement("SELECT uuid FROM " + ConfigHandler.prefix + "username_log WHERE user = ?" + collate)) {
try (PreparedStatement statement = connection.prepareStatement("SELECT uuid FROM " + ConfigHandler.prefix + "username_log WHERE " + userMatch)) {
statement.setString(1, user);
try (ResultSet results = statement.executeQuery()) {
while (results.next()) {
@@ -134,14 +153,29 @@ public class UsernameAPI {
return result.isEmpty() ? null : result;
}
private static Set<String> getUuids(Connection connection, List<String> users) throws Exception {
Set<String> result = new LinkedHashSet<>();
for (String user : users) {
Set<String> matches = getUuids(connection, user);
if (matches != null) {
if (matches.isEmpty()) {
// An empty name or #global matches every user.
return null;
}
result.addAll(matches);
}
}
return result;
}
private static boolean looksLikeUuid(String value) {
return value.length() == 36 && value.charAt(8) == '-' && value.charAt(13) == '-' && value.charAt(18) == '-' && value.charAt(23) == '-';
}
private static UsernameResult parseUsernameResult(ResultSet results) throws Exception {
private static UsernameResult parseUsernameResult(Connection connection, ResultSet results) throws Exception {
String uuid = results.getString("uuid");
String username = results.getString("user");
String player = ConfigHandler.uuidCacheReversed.get(uuid);
String player = UserStatement.getNameByUuid(connection, uuid);
if (player == null) {
player = username;
}
@@ -66,7 +66,7 @@ public class BlockResult implements CoreProtectResult {
}
public Material getType() {
if (actionId == LookupActions.ENTITY_KILL) {
if (isEntityAction()) {
return null;
}
@@ -80,11 +80,11 @@ public class BlockResult implements CoreProtectResult {
}
public EntityType getEntityType() {
if (actionId != LookupActions.ENTITY_KILL) {
if (!isEntityAction()) {
return null;
}
if (type == 0) {
if (actionId == LookupActions.ENTITY_KILL && type == 0) {
return EntityType.PLAYER;
}
@@ -92,7 +92,7 @@ public class BlockResult implements CoreProtectResult {
}
public BlockData getBlockData() {
if (actionId == LookupActions.ENTITY_KILL) {
if (isEntityAction()) {
return null;
}
@@ -125,6 +125,10 @@ public class BlockResult implements CoreProtectResult {
return rolledBack == 1 || rolledBack == 3;
}
private boolean isEntityAction() {
return actionId == LookupActions.ENTITY_KILL || actionId == LookupActions.ENTITY_SPAWN;
}
public String worldName() {
return world;
}
@@ -0,0 +1,76 @@
package net.coreprotect.api.result;
import org.bukkit.entity.EntityType;
import net.coreprotect.model.action.LookupActions;
import net.coreprotect.utility.EntityUtils;
/**
* Represents a logged entity spawn or kill at its original event location.
*/
public class EntityResult implements CoreProtectResult {
private final long time;
private final String username;
private final String world;
private final int x;
private final int y;
private final int z;
private final int type;
private final int actionId;
private final int rolledBack;
public EntityResult(long time, String username, String world, int x, int y, int z, int type, int actionId, int rolledBack) {
this.time = time;
this.username = username;
this.world = world;
this.x = x;
this.y = y;
this.z = z;
this.type = type;
this.actionId = actionId;
this.rolledBack = rolledBack;
}
public int getActionId() {
return actionId;
}
public String getActionString() {
return LookupActions.getActionString(actionId);
}
public String getPlayer() {
return username;
}
public long getTimestamp() {
return time * 1000L;
}
public EntityType getEntityType() {
if (actionId == LookupActions.ENTITY_KILL && type == 0) {
return EntityType.PLAYER;
}
return EntityUtils.getEntityType(type);
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getZ() {
return z;
}
public boolean isRolledBack() {
return rolledBack == 1 || rolledBack == 3;
}
public String worldName() {
return world;
}
}
@@ -5,10 +5,12 @@ import java.util.Locale;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.EntityType;
import net.coreprotect.api.SessionLookup;
import net.coreprotect.model.action.LookupActions;
import net.coreprotect.model.action.SessionActions;
import net.coreprotect.model.item.InventorySources;
import net.coreprotect.utility.BlockUtils;
import net.coreprotect.utility.BlockTypeUtils;
import net.coreprotect.utility.EntityUtils;
@@ -60,22 +62,18 @@ public class ParseResult implements CoreProtectResult {
}
int actionID = this.getActionId();
if (isEntityAction(actionID)) {
return null;
}
int type = Integer.parseInt(parse[5]);
String typeName;
if (actionID == 3) {
typeName = EntityUtils.getEntityType(type).name();
}
else {
Material material = MaterialUtils.getType(type);
if (material == null) {
return null;
}
typeName = material.name().toLowerCase(Locale.ROOT);
typeName = StringUtils.nameFilter(typeName, this.getData());
Material material = MaterialUtils.getType(type);
if (material == null) {
return null;
}
String typeName = material.name().toLowerCase(Locale.ROOT);
typeName = StringUtils.nameFilter(typeName, this.getData());
return MaterialUtils.getType(typeName);
}
@@ -84,6 +82,10 @@ public class ParseResult implements CoreProtectResult {
return null;
}
if (isEntityAction(this.getActionId())) {
return null;
}
String blockData = parse[12];
if (blockData == null || blockData.length() == 0) {
Material type = getType();
@@ -98,6 +100,19 @@ public class ParseResult implements CoreProtectResult {
return result != null ? result : Bukkit.getServer().createBlockData(blockData);
}
public EntityType getEntityType() {
if (parse.length < 13 || !isEntityAction(this.getActionId())) {
return null;
}
int type = Integer.parseInt(parse[5]);
if (this.getActionId() == LookupActions.ENTITY_KILL && type == 0) {
return EntityType.PLAYER;
}
return EntityUtils.getEntityType(type);
}
public int getX() {
return Integer.parseInt(parse[2]);
}
@@ -121,4 +136,11 @@ public class ParseResult implements CoreProtectResult {
public String worldName() {
return WorldUtils.getWorldName(Integer.parseInt(parse.length < 13 ? parse[5] : parse[9]));
}
private boolean isEntityAction(int actionId) {
if (actionId == LookupActions.ENTITY_KILL || actionId == LookupActions.ENTITY_SPAWN) {
return true;
}
return actionId == LookupActions.INTERACTION && parse.length > 14 && parse[13] != null && Integer.parseInt(parse[13]) == InventorySources.ENTITY_INTERACTION;
}
}
@@ -1,6 +1,8 @@
package net.coreprotect.bukkit;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
@@ -8,8 +10,10 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import org.bukkit.Art;
import org.bukkit.Chunk;
import org.bukkit.Color;
import org.bukkit.DyeColor;
import org.bukkit.Location;
@@ -29,6 +33,7 @@ import org.bukkit.entity.EntityType;
import org.bukkit.entity.ItemFrame;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Painting;
import org.bukkit.entity.Player;
import org.bukkit.entity.Villager;
import org.bukkit.event.Event;
import org.bukkit.event.block.BlockExplodeEvent;
@@ -66,6 +71,8 @@ public class BukkitAdapter implements BukkitInterface {
public static final int BUKKIT_V1_19 = 19;
public static final int BUKKIT_V1_20 = 20;
public static final int BUKKIT_V1_21 = 21;
public static final int BUKKIT_V1_21_5 = 21005;
public static final int BUKKIT_V1_21_11 = 21011;
public static final int BUKKIT_V26_0 = 26000;
public static final int BUKKIT_V26_1 = 26010;
public static final int BUKKIT_V26_2 = 26020;
@@ -75,7 +82,11 @@ public class BukkitAdapter implements BukkitInterface {
}
public static int getAdapterVersion(int major, int minor, int patch) {
return major == 1 ? minor : (major * 1000) + (minor * 10) + patch;
if (major == 1) {
return minor == 21 && patch >= 5 ? (minor * 1000) + patch : minor;
}
return (major * 1000) + (minor * 10) + patch;
}
/**
@@ -83,36 +94,33 @@ public class BukkitAdapter implements BukkitInterface {
* This method should be called during plugin initialization.
*/
public static void loadAdapter() {
switch (ConfigHandler.SERVER_VERSION) {
case BUKKIT_V1_13:
case BUKKIT_V1_14:
case BUKKIT_V1_15:
case BUKKIT_V1_16:
ADAPTER = new BukkitAdapter();
break;
case BUKKIT_V1_17:
ADAPTER = new Bukkit_v1_17();
break;
case BUKKIT_V1_18:
ADAPTER = new Bukkit_v1_18();
break;
case BUKKIT_V1_19:
ADAPTER = new Bukkit_v1_19();
break;
case BUKKIT_V1_20:
ADAPTER = new Bukkit_v1_20();
break;
case BUKKIT_V1_21:
case BUKKIT_V26_0:
case BUKKIT_V26_1:
ADAPTER = new Bukkit_v1_21();
break;
case BUKKIT_V26_2:
ADAPTER = new Bukkit_v26_2();
break;
default:
ADAPTER = ConfigHandler.SERVER_VERSION >= BUKKIT_V26_2 ? new Bukkit_v26_2() : new Bukkit_v1_21();
break;
int bukkitVersion = ConfigHandler.SERVER_VERSION;
if (bukkitVersion >= BUKKIT_V26_2) {
ADAPTER = new Bukkit_v26_2();
}
else if (bukkitVersion >= BUKKIT_V1_21_11) {
ADAPTER = new Bukkit_v1_21_11();
}
else if (bukkitVersion >= BUKKIT_V1_21_5) {
ADAPTER = new Bukkit_v1_21_5();
}
else if (bukkitVersion >= BUKKIT_V1_21) {
ADAPTER = new Bukkit_v1_21();
}
else if (bukkitVersion >= BUKKIT_V1_20) {
ADAPTER = new Bukkit_v1_20();
}
else if (bukkitVersion >= BUKKIT_V1_19) {
ADAPTER = new Bukkit_v1_19();
}
else if (bukkitVersion >= BUKKIT_V1_18) {
ADAPTER = new Bukkit_v1_18();
}
else if (bukkitVersion >= BUKKIT_V1_17) {
ADAPTER = new Bukkit_v1_17();
}
else {
ADAPTER = new BukkitAdapter();
}
}
@@ -131,6 +139,11 @@ public class BukkitAdapter implements BukkitInterface {
// -------------------- Entity methods --------------------
@Override
public boolean isChunkEntitiesLoaded(Chunk chunk) {
return true;
}
@Override
public boolean getEntityMeta(LivingEntity entity, List<Object> info) {
return false;
@@ -141,6 +154,59 @@ public class BukkitAdapter implements BukkitInterface {
return false;
}
/**
* Accesses registry-backed entity variants without exposing the entity interface to legacy
* server bytecode remapping.
*/
public static boolean getRegistryVariant(BukkitInterface adapter, Entity entity, List<Object> info, String getterName) {
try {
Object variant = entity.getClass().getMethod(getterName).invoke(entity);
if (variant == null) {
return false;
}
info.add(adapter.getRegistryKey(variant));
return true;
}
catch (ReflectiveOperationException | LinkageError e) {
return false;
}
}
/**
* Restores registry-backed entity variants without exposing the entity interface to legacy
* server bytecode remapping.
*/
public static boolean setRegistryVariant(BukkitInterface adapter, Entity entity, Object value, String getterName, String setterName) {
try {
Class<?> variantClass = entity.getClass().getMethod(getterName).getReturnType();
Object variant = value instanceof String ? adapter.getRegistryValue((String) value, variantClass) : value;
if (!variantClass.isInstance(variant)) {
return false;
}
entity.getClass().getMethod(setterName, variantClass).invoke(entity, variant);
return true;
}
catch (ReflectiveOperationException | LinkageError e) {
return false;
}
}
public static Object getLegacyEnumValue(Class<?> type, String name) {
if (!type.isEnum()) {
return null;
}
try {
@SuppressWarnings({ "rawtypes", "unchecked" })
Object value = Enum.valueOf((Class<? extends Enum>) type.asSubclass(Enum.class), name);
return value;
}
catch (IllegalArgumentException | LinkageError exception) {
return null;
}
}
@Override
public void addMerchantRecipeMeta(MerchantRecipe recipe, List<Object> recipeData) {
}
@@ -192,6 +258,11 @@ public class BukkitAdapter implements BukkitInterface {
return false;
}
@Override
public String getItemName(ItemMeta itemMeta) {
return "";
}
@Override
public Material getPlantSeeds(Material material) {
switch (material) {
@@ -328,6 +399,43 @@ public class BukkitAdapter implements BukkitInterface {
return ItemFrame.class;
}
@Override
public <T extends Entity> T spawn(World world, Location location, Class<T> entityClass, Consumer<? super T> function) {
// Bukkit 1.20.1 and earlier only have World#spawn(Location, Class, org.bukkit.util.Consumer), which is deprecated for removal in the current API
try {
Class<?> consumerClass = Class.forName("org.bukkit.util.Consumer");
Object consumer = Proxy.newProxyInstance(consumerClass.getClassLoader(), new Class<?>[] { consumerClass }, (proxy, method, args) -> {
if (method.getDeclaringClass() == Object.class) {
switch (method.getName()) {
case "equals":
return proxy == args[0];
case "hashCode":
return System.identityHashCode(proxy);
default:
return consumerClass.getName();
}
}
function.accept(entityClass.cast(args[0]));
return null;
});
Method spawnMethod = World.class.getMethod("spawn", Location.class, Class.class, consumerClass);
return entityClass.cast(spawnMethod.invoke(world, location, entityClass, consumer));
}
catch (InvocationTargetException e) {
Throwable cause = e.getCause();
if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
}
if (cause instanceof Error) {
throw (Error) cause;
}
throw new IllegalStateException(cause);
}
catch (ReflectiveOperationException e) {
throw new IllegalStateException(e);
}
}
@Override
public boolean isDecoratedPot(Material material) {
return false;
@@ -386,6 +494,11 @@ public class BukkitAdapter implements BukkitInterface {
return true;
}
@Override
public Boolean getSignInteractionSide(Sign sign, Player player) {
return true;
}
@Override
public boolean isGlowing(Sign sign, boolean isFront) {
return false;
@@ -3,8 +3,10 @@ package net.coreprotect.bukkit;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import org.bukkit.Art;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
@@ -18,6 +20,7 @@ import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Painting;
import org.bukkit.entity.Player;
import org.bukkit.entity.Villager;
import org.bukkit.event.Event;
import org.bukkit.event.block.BlockExplodeEvent;
@@ -251,6 +254,8 @@ public interface BukkitInterface {
*/
boolean setItemMeta(Material rowType, ItemStack itemstack, List<Map<String, Object>> map);
String getItemName(ItemMeta itemMeta);
/**
* Gets a book from a chiseled bookshelf.
*
@@ -277,6 +282,15 @@ public interface BukkitInterface {
// Entity methods
// --------------------------------------------------------------------------
/**
* Checks whether a loaded chunk's entity data is ready to query.
*
* @param chunk
* The loaded chunk
* @return true when entity data is ready
*/
boolean isChunkEntitiesLoaded(Chunk chunk);
/**
* Gets metadata from a living entity.
*
@@ -365,6 +379,21 @@ public interface BukkitInterface {
*/
Class<?> getFrameClass(Material material);
/**
* Spawns an entity, running a function on it before it is added to the world.
*
* @param world
* The world
* @param location
* The spawn location
* @param entityClass
* The entity class
* @param function
* The function to run before the entity is added
* @return The spawned entity
*/
<T extends Entity> T spawn(World world, Location location, Class<T> entityClass, Consumer<? super T> function);
// --------------------------------------------------------------------------
// Sign methods
// --------------------------------------------------------------------------
@@ -466,6 +495,8 @@ public interface BukkitInterface {
*/
boolean isSignFront(SignChangeEvent event);
Boolean getSignInteractionSide(Sign sign, Player player);
/**
@@ -7,6 +7,7 @@ import java.util.List;
import java.util.Map;
import org.bukkit.Material;
import org.bukkit.Chunk;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
@@ -110,9 +111,16 @@ public class Bukkit_v1_17 extends BukkitAdapter {
if (entity instanceof Axolotl) {
Axolotl axolotl = (Axolotl) entity;
if (count == 0) {
org.bukkit.entity.Axolotl.Variant variant = (org.bukkit.entity.Axolotl.Variant) value;
axolotl.setVariant(variant);
return true;
try {
org.bukkit.entity.Axolotl.Variant variant = value instanceof String
? org.bukkit.entity.Axolotl.Variant.valueOf((String) value)
: (org.bukkit.entity.Axolotl.Variant) value;
axolotl.setVariant(variant);
return true;
}
catch (IllegalArgumentException | ClassCastException exception) {
return false;
}
}
}
else if (entity instanceof Goat) {
@@ -246,4 +254,9 @@ public class Bukkit_v1_17 extends BukkitAdapter {
public boolean isInvisible(Material material) {
return material.isAir() || material == Material.LIGHT;
}
@Override
public boolean isChunkEntitiesLoaded(Chunk chunk) {
return chunk.isEntitiesLoaded();
}
}
@@ -3,6 +3,7 @@ package net.coreprotect.bukkit;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import org.bukkit.Material;
import org.bukkit.entity.Entity;
@@ -196,7 +197,16 @@ public class Bukkit_v1_19 extends Bukkit_v1_18 {
if (count == 0) {
// Convert string registry key to variant if needed
if (value instanceof String) {
value = BukkitAdapter.ADAPTER.getRegistryValue((String) value, Frog.Variant.class);
String key = (String) value;
value = BukkitAdapter.ADAPTER.getRegistryValue(key, Frog.Variant.class);
if (value == null) {
int separator = key.indexOf(':');
String name = separator < 0 ? key : key.substring(separator + 1).toUpperCase(Locale.ROOT).replace('.', '_').replace('-', '_');
value = BukkitAdapter.getLegacyEnumValue(Frog.Variant.class, name);
}
if (!(value instanceof Frog.Variant)) {
return false;
}
}
frog.setVariant((Frog.Variant) value);
return true;
@@ -4,21 +4,27 @@ import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;
import org.bukkit.Color;
import org.bukkit.DyeColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Tag;
import org.bukkit.World;
import org.bukkit.block.BlockState;
import org.bukkit.block.ChiseledBookshelf;
import org.bukkit.block.Sign;
import org.bukkit.block.sign.Side;
import org.bukkit.entity.Arrow;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockExplodeEvent;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.MerchantRecipe;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionType;
@@ -37,7 +43,10 @@ import net.coreprotect.model.BlockGroup;
public class Bukkit_v1_20 extends Bukkit_v1_19 {
private Boolean hasClickedPosition;
private Boolean hasSignTargetSide;
private Boolean hasBasePotionType;
private Boolean hasItemName;
private Boolean hasSpawnFunction;
/**
* Initializes the Bukkit_v1_20 adapter with 1.20-specific block groups and mappings.
@@ -118,6 +127,21 @@ public class Bukkit_v1_20 extends Bukkit_v1_19 {
}
}
@Override
public Boolean getSignInteractionSide(Sign sign, Player player) {
if (hasSignTargetSide == null) {
try {
Sign.class.getMethod("getTargetSide", Player.class);
hasSignTargetSide = true;
}
catch (NoSuchMethodException e) {
hasSignTargetSide = false;
}
}
return hasSignTargetSide ? sign.getTargetSide(player) == sign.getSide(Side.FRONT) : null;
}
@Override
public void setGlowing(Sign sign, boolean isFront, boolean isGlowing) {
Side side = isFront ? Side.FRONT : Side.BACK;
@@ -264,6 +288,37 @@ public class Bukkit_v1_20 extends Bukkit_v1_19 {
return event.getSide().equals(Side.FRONT);
}
@Override
public String getItemName(ItemMeta itemMeta) {
if (hasItemName == null) {
try {
ItemMeta.class.getMethod("getItemName");
hasItemName = true;
}
catch (NoSuchMethodException e) {
hasItemName = false;
}
}
return hasItemName && itemMeta.hasItemName() ? itemMeta.getItemName() : "";
}
@Override
public <T extends Entity> T spawn(World world, Location location, Class<T> entityClass, Consumer<? super T> function) {
if (hasSpawnFunction == null) {
try {
// Check if World#spawn accepts java.util.function.Consumer (Bukkit 1.20.2+)
World.class.getMethod("spawn", Location.class, Class.class, Consumer.class);
hasSpawnFunction = true;
}
catch (NoSuchMethodException e) {
hasSpawnFunction = false;
}
}
return hasSpawnFunction ? world.spawn(location, entityClass, function) : super.spawn(world, location, entityClass, function);
}
@Override
public ItemStack getArrowMeta(Arrow arrow, ItemStack itemStack) {
try {
@@ -0,0 +1,50 @@
package net.coreprotect.bukkit;
import java.util.List;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.ZombieNautilus;
/**
* Bukkit adapter implementation for Minecraft 1.21.11.
*/
public class Bukkit_v1_21_11 extends Bukkit_v1_21_5 {
@Override
public boolean getEntityMeta(LivingEntity entity, List<Object> info) {
if (entity instanceof ZombieNautilus) {
ZombieNautilus nautilus = (ZombieNautilus) entity;
info.add(getRegistryKey(nautilus.getVariant()));
return true;
}
return super.getEntityMeta(entity, info);
}
@Override
public boolean setEntityMeta(Entity entity, Object value, int count) {
if (entity instanceof ZombieNautilus && count == 0) {
ZombieNautilus.Variant variant = getZombieNautilusVariant(value);
if (variant == null) {
return false;
}
ZombieNautilus nautilus = (ZombieNautilus) entity;
nautilus.setVariant(variant);
return true;
}
return super.setEntityMeta(entity, value, count);
}
private ZombieNautilus.Variant getZombieNautilusVariant(Object value) {
try {
Object variant = value instanceof String ? getRegistryValue((String) value, ZombieNautilus.Variant.class) : value;
return variant instanceof ZombieNautilus.Variant ? (ZombieNautilus.Variant) variant : null;
}
catch (IllegalArgumentException exception) {
return null;
}
}
}
@@ -0,0 +1,72 @@
package net.coreprotect.bukkit;
import java.util.List;
import org.bukkit.entity.Chicken;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Pig;
/**
* Bukkit adapter implementation for Minecraft 1.21.5.
*/
public class Bukkit_v1_21_5 extends Bukkit_v1_21 {
@Override
public boolean getEntityMeta(LivingEntity entity, List<Object> info) {
if (entity instanceof Chicken) {
Chicken chicken = (Chicken) entity;
info.add(getRegistryKey(chicken.getVariant()));
return true;
}
else if (entity.getType() == EntityType.COW) {
return BukkitAdapter.getRegistryVariant(this, entity, info, "getVariant");
}
else if (entity instanceof Pig) {
Pig pig = (Pig) entity;
info.add(getRegistryKey(pig.getVariant()));
return true;
}
return super.getEntityMeta(entity, info);
}
@Override
public boolean setEntityMeta(Entity entity, Object value, int count) {
if (entity instanceof Chicken && count == 0) {
Chicken.Variant variant = getChickenVariant(value);
if (variant == null) {
return false;
}
Chicken chicken = (Chicken) entity;
chicken.setVariant(variant);
return true;
}
else if (entity.getType() == EntityType.COW && count == 0) {
return BukkitAdapter.setRegistryVariant(this, entity, value, "getVariant", "setVariant");
}
else if (entity instanceof Pig && count == 1) {
Pig.Variant variant = getPigVariant(value);
if (variant == null) {
return false;
}
Pig pig = (Pig) entity;
pig.setVariant(variant);
return true;
}
return super.setEntityMeta(entity, value, count);
}
private Chicken.Variant getChickenVariant(Object value) {
Object variant = value instanceof String ? getRegistryValue((String) value, Chicken.Variant.class) : value;
return variant instanceof Chicken.Variant ? (Chicken.Variant) variant : null;
}
private Pig.Variant getPigVariant(Object value) {
Object variant = value instanceof String ? getRegistryValue((String) value, Pig.Variant.class) : value;
return variant instanceof Pig.Variant ? (Pig.Variant) variant : null;
}
}
@@ -1,17 +1,13 @@
package net.coreprotect.bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.type.Speleothem;
import net.coreprotect.model.BlockGroup;
/**
* Bukkit adapter implementation for Minecraft 26.2.
*/
public class Bukkit_v26_2 extends Bukkit_v1_21 {
public class Bukkit_v26_2 extends Bukkit_v1_21_11 {
public Bukkit_v26_2() {
initializeBlockGroups();
@@ -21,15 +17,4 @@ public class Bukkit_v26_2 extends Bukkit_v1_21 {
BlockGroup.TRACK_TOP_BOTTOM.add(Material.SULFUR_SPIKE);
}
@Override
public boolean isAttached(Block block, Block scanBlock, BlockData blockData, int scanMin) {
if (blockData instanceof Speleothem) {
Speleothem speleothem = (Speleothem) blockData;
BlockFace blockFace = speleothem.getVerticalDirection();
return scanBlock.getRelative(blockFace.getOppositeFace()).getLocation().equals(block.getLocation());
}
return super.isAttached(block, scanBlock, blockData, scanMin);
}
}
@@ -55,7 +55,7 @@ public class CommandHandler implements CommandExecutor {
else if (user.hasPermission("coreprotect.purge") && corecommand.equals("purge")) {
permission = true;
}
else if (user.hasPermission("coreprotect.lookup") && (corecommand.equals("l") || corecommand.equals("lookup") || corecommand.equals("page") || corecommand.equals("near"))) {
else if (user.hasPermission("coreprotect.lookup") && (corecommand.equals("l") || corecommand.equals("lookup") || corecommand.equals("page"))) {
permission = true;
}
else if (user.hasPermission("coreprotect.lookup.near") && corecommand.equals("near")) {
@@ -38,6 +38,22 @@ public class CommandParser {
return ActionParser.parseAction(inputArguments);
}
protected static List<Integer> parseAction(String[] inputArguments, boolean allowMultiple) {
return ActionParser.parseAction(inputArguments, allowMultiple);
}
protected static ActionParser.ParseResult parseActions(String[] inputArguments, boolean allowMultiple) {
return ActionParser.parseActions(inputArguments, allowMultiple);
}
protected static MessageFilterParser.ParseResult parseMessageFilters(String[] inputArguments) {
return MessageFilterParser.parse(inputArguments);
}
protected static RollbackStateParser.ParseResult parseRollbackState(String[] inputArguments) {
return RollbackStateParser.parse(inputArguments);
}
/**
* Parse coordinates from command arguments
*
@@ -64,6 +80,10 @@ public class CommandParser {
return ActionParser.parseCount(inputArguments);
}
protected static boolean parseSummary(String[] inputArguments) {
return ActionParser.parseSummary(inputArguments);
}
/**
* Parse excluded materials and entities from command arguments
*
@@ -38,6 +38,7 @@ public class HelpCommand {
Chat.sendMessage(player, Color.DARK_AQUA + "| " + Color.GREY + "a:<action> " + Color.WHITE + "- " + Phrase.build(Phrase.HELP_PARAMS_5, Selector.FIRST));
Chat.sendMessage(player, Color.DARK_AQUA + "| " + Color.GREY + "i:<include> " + Color.WHITE + "- " + Phrase.build(Phrase.HELP_PARAMS_6, Selector.FIRST));
Chat.sendMessage(player, Color.DARK_AQUA + "| " + Color.GREY + "e:<exclude> " + Color.WHITE + "- " + Phrase.build(Phrase.HELP_PARAMS_7, Selector.FIRST));
Chat.sendMessage(player, Color.DARK_AQUA + "| " + Color.GREY + "f:<filter> " + Color.WHITE + "- " + Phrase.build(Phrase.HELP_FILTER_1));
Chat.sendMessage(player, Color.GREY + Color.ITALIC + Phrase.build(Phrase.HELP_PARAMETER, "/co help <param>"));
}
else if (helpcommand.equals("rollback") || helpcommand.equals("rollbacks") || helpcommand.equals("rb") || helpcommand.equals("ro")) {
@@ -69,8 +70,10 @@ public class HelpCommand {
else if (helpcommand.equals("purge") || helpcommand.equals("purges")) {
Chat.sendMessage(player, Color.DARK_AQUA + "/co purge " + Color.GREY + "<params> " + Color.WHITE + "- " + Phrase.build(Phrase.HELP_PURGE_1));
Chat.sendMessage(player, Color.DARK_AQUA + "| " + Color.GREY + "t:<time> " + Color.WHITE + "- " + Phrase.build(Phrase.HELP_PURGE_3));
Chat.sendMessage(player, Color.DARK_AQUA + "| " + Color.GREY + "r:<world> " + Color.WHITE + "- " + Phrase.build(Phrase.HELP_PURGE_4));
Chat.sendMessage(player, Color.DARK_AQUA + "| " + Color.GREY + "r:<world/radius> " + Color.WHITE + "- " + Phrase.build(Phrase.HELP_PURGE_4));
Chat.sendMessage(player, Color.DARK_AQUA + "| " + Color.GREY + "i:<include> " + Color.WHITE + "- " + Phrase.build(Phrase.HELP_PURGE_5));
Chat.sendMessage(player, Color.DARK_AQUA + "| " + Color.GREY + "e:<exclude> " + Color.WHITE + "- " + Phrase.build(Phrase.HELP_PURGE_6));
Chat.sendMessage(player, Color.DARK_AQUA + "| " + Color.GREY + "a:kill " + Color.WHITE + "- " + Phrase.build(Phrase.HELP_PURGE_7));
Chat.sendMessage(player, Color.GREY + Color.ITALIC + Phrase.build(Phrase.HELP_PURGE_2, "/co purge t:30d"));
}
else if (helpcommand.equals("reload")) {
@@ -98,6 +101,10 @@ public class HelpCommand {
Chat.sendMessage(player, Color.DARK_AQUA + "/co lookup a:<action> " + Color.WHITE + "- " + Phrase.build(Phrase.HELP_ACTION_1));
Chat.sendMessage(player, Color.GREY + Color.ITALIC + Phrase.build(Phrase.HELP_ACTION_2));
}
else if (helpcommand.equals("f") || helpcommand.equals("filter") || helpcommand.equals("ffilter")) {
Chat.sendMessage(player, Color.DARK_AQUA + "/co lookup a:<chat|command|sign> f:<filter> " + Color.WHITE + "- " + Phrase.build(Phrase.HELP_FILTER_1));
Chat.sendMessage(player, Color.GREY + Color.ITALIC + Phrase.build(Phrase.HELP_FILTER_2));
}
else if (helpcommand.equals("i") || helpcommand.equals("include") || helpcommand.equals("iinclude") || helpcommand.equals("b") || helpcommand.equals("block") || helpcommand.equals("blocks") || helpcommand.equals("bblock") || helpcommand.equals("bblocks")) {
Chat.sendMessage(player, Color.DARK_AQUA + "/co lookup i:<include> " + Color.WHITE + "- " + Phrase.build(Phrase.HELP_INCLUDE_1));
Chat.sendMessage(player, Color.GREY + Color.ITALIC + Phrase.build(Phrase.HELP_INCLUDE_2));
@@ -1,5 +1,6 @@
package net.coreprotect.command;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@@ -17,12 +18,19 @@ import org.bukkit.entity.Player;
import net.coreprotect.command.lookup.BlockLookupThread;
import net.coreprotect.command.lookup.ChestTransactionLookupThread;
import net.coreprotect.command.lookup.EntityInteractionLookupThread;
import net.coreprotect.command.lookup.StandardLookupThread;
import net.coreprotect.command.parser.ActionParser;
import net.coreprotect.command.parser.MessageFilterParser;
import net.coreprotect.command.parser.RollbackStateParser;
import net.coreprotect.config.Config;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.language.Phrase;
import net.coreprotect.language.Selector;
import net.coreprotect.model.action.EntityActionFilter;
import net.coreprotect.model.action.LookupActions;
import net.coreprotect.model.lookup.LookupOutputMode;
import net.coreprotect.model.lookup.LookupRollbackState;
import net.coreprotect.utility.Chat;
import net.coreprotect.utility.ChatMessage;
import net.coreprotect.utility.Color;
@@ -32,13 +40,18 @@ import net.coreprotect.utility.ErrorReporter;
public class LookupCommand {
public static void runCommand(CommandSender player, Command command, boolean permission, String[] args) {
int resultc = args.length;
MessageFilterParser.ParseResult messageFilterResult = CommandParser.parseMessageFilters(args);
args = messageFilterResult.getArguments();
List<String> argFilters = messageFilterResult.getFilters();
args = CommandParser.parsePage(args);
Location lo = CommandParser.parseLocation(player, args);
// List<String> arg_uuids = new ArrayList<String>();
List<String> argUsers = CommandParser.parseUsers(args);
Integer[] argRadius = CommandParser.parseRadius(args, player, lo);
int argNoisy = CommandParser.parseNoisy(args);
List<Integer> argAction = CommandParser.parseAction(args);
ActionParser.ParseResult actionResult = CommandParser.parseActions(args, true);
List<Integer> argAction = actionResult.getActions();
EntityActionFilter argEntityActionFilter = actionResult.getEntityActionFilter();
List<Object> argBlocks = CommandParser.parseRestricted(player, args, argAction);
Map<Object, Boolean> argExclude = CommandParser.parseExcluded(player, args, argAction);
List<String> argExcludeUsers = CommandParser.parseExcludedUsers(player, args);
@@ -48,7 +61,11 @@ public class LookupCommand {
long endTime = argTime[1];
int argWid = CommandParser.parseWorld(args, true, true);
int parseRows = CommandParser.parseRows(args);
boolean count = CommandParser.parseCount(args);
boolean summary = CommandParser.parseSummary(args);
LookupOutputMode outputMode = summary ? LookupOutputMode.SUMMARY
: CommandParser.parseCount(args) ? LookupOutputMode.COUNT : LookupOutputMode.DETAIL;
RollbackStateParser.ParseResult rollbackStateResult = CommandParser.parseRollbackState(args);
LookupRollbackState rollbackState = rollbackStateResult.getState();
boolean worldedit = CommandParser.parseWorldEdit(args);
boolean forceglobal = CommandParser.parseForceGlobal(args);
boolean pageLookup = false;
@@ -57,6 +74,11 @@ public class LookupCommand {
return;
}
if (rollbackStateResult.isConflicting()) {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.INVALID_PARAMETER, "#rolledback + #restored"));
return;
}
if (args[0].toLowerCase(Locale.ROOT).equals("page") && (args.length != 2 || !args[1].equals(args[1].replaceAll("[^0-9]", "")))) {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.MISSING_PARAMETERS, Color.WHITE, "/co page <page>"));
return;
@@ -76,8 +98,9 @@ public class LookupCommand {
hasEntity = true;
if (argAction.size() == 0) {
argAction.add(LookupActions.ENTITY_KILL);
argAction.add(LookupActions.ENTITY_SPAWN);
}
else if (!argAction.contains(LookupActions.ENTITY_KILL)) {
else if (!argEntityActionFilter.includesAnyEntity(argAction, true) && !argAction.contains(LookupActions.INTERACTION)) {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.INVALID_INCLUDE_COMBO));
return;
}
@@ -93,8 +116,9 @@ public class LookupCommand {
hasEntity = true;
if (argAction.size() == 0) {
argAction.add(LookupActions.ENTITY_KILL);
argAction.add(LookupActions.ENTITY_SPAWN);
}
else if (!argAction.contains(LookupActions.ENTITY_KILL)) {
else if (!argEntityActionFilter.includesAnyEntity(argAction, true) && !argAction.contains(LookupActions.INTERACTION)) {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.INVALID_INCLUDE_COMBO));
return;
}
@@ -171,6 +195,22 @@ public class LookupCommand {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.INVALID_ACTION));
return;
}
if (rollbackState != LookupRollbackState.ANY && (argAction.contains(LookupActions.CHAT) || argAction.contains(LookupActions.COMMAND) || argAction.contains(LookupActions.SESSION) || argAction.contains(LookupActions.USERNAME) || argAction.contains(LookupActions.SIGN))) {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.INCOMPATIBLE_ACTION, rollbackState == LookupRollbackState.ROLLED_BACK ? "#rolledback" : "#restored"));
return;
}
if (messageFilterResult.isSpecified()) {
if (messageFilterResult.hasInvalidLength()) {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.MESSAGE_FILTER_TOO_SHORT, Integer.toString(MessageFilterParser.MINIMUM_FILTER_CODE_POINTS)));
return;
}
boolean messageLookup = !argAction.isEmpty() && argAction.stream().allMatch(action -> action == LookupActions.CHAT || action == LookupActions.COMMAND);
boolean signLookup = !argAction.isEmpty() && argAction.stream().allMatch(action -> action == LookupActions.SIGN);
if (!messageLookup && !signLookup) {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.INCOMPATIBLE_ACTION, "f:<filter>"));
return;
}
}
if (worldedit && argRadius == null) {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.INVALID_SELECTION, "WorldEdit"));
return;
@@ -191,7 +231,7 @@ public class LookupCommand {
allPermission = true;
}
if (!allPermission) {
if (!pageLookup && (argAction.size() == 0 || (argAction.size() == 1 && (argAction.contains(LookupActions.BLOCK_BREAK) || argAction.contains(LookupActions.BLOCK_PLACE)))) && !player.hasPermission("coreprotect.lookup.block")) {
if (!pageLookup && (argAction.isEmpty() || argAction.contains(LookupActions.BLOCK_BREAK) || argAction.contains(LookupActions.BLOCK_PLACE)) && !player.hasPermission("coreprotect.lookup.block")) {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.NO_PERMISSION));
return;
}
@@ -203,6 +243,10 @@ public class LookupCommand {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.NO_PERMISSION));
return;
}
if (argAction.contains(LookupActions.ENTITY_SPAWN) && !player.hasPermission("coreprotect.lookup.spawn")) {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.NO_PERMISSION));
return;
}
if (argAction.contains(LookupActions.CONTAINER) && !argAction.contains(LookupActions.ITEM) && !player.hasPermission("coreprotect.lookup.container")) {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.NO_PERMISSION));
return;
@@ -251,7 +295,7 @@ public class LookupCommand {
}
}
if (startTime <= 0 && !pageLookup && type == 4 && (argBlocks.size() > 0 || argUsers.size() > 0)) {
if (startTime <= 0 && argRadius == null && !pageLookup && type == 4 && (argBlocks.size() > 0 || argUsers.size() > 0)) {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.MISSING_LOOKUP_TIME, Selector.FIRST));
return;
}
@@ -338,7 +382,7 @@ public class LookupCommand {
Thread thread = new Thread(runnable);
thread.start();
}
else if (type == 2 || type == 3 || type == 7 || type == 8) {
else if (type == 2 || type == 3 || type == 7 || type == 8 || type == 9) {
boolean defaultRe = true;
int page = 1;
int re = 7;
@@ -380,6 +424,13 @@ public class LookupCommand {
}
// String bc = x+"."+y+"."+z+"."+wid+"."+rstring+"."+lookup_user;
if (type == 9) {
Runnable runnable = new EntityInteractionLookupThread(player, command, page, re);
Thread thread = new Thread(runnable);
thread.start();
return;
}
String lcommand = ConfigHandler.lookupCommand.get(player.getName());
String[] data = lcommand.split("\\.");
int x = Integer.parseInt(data[0]);
@@ -392,21 +443,25 @@ public class LookupCommand {
}
String bc = x + "." + y + "." + z + "." + wid + "." + lookupType + "." + re;
Integer entitySpawnRowId = type == 2 && data.length > 6 ? Integer.valueOf(data[6]) : null;
if (entitySpawnRowId != null) {
bc += "." + entitySpawnRowId;
}
ConfigHandler.lookupCommand.put(player.getName(), bc);
String world = WorldUtils.getWorldName(wid);
final Block block = Bukkit.getServer().getWorld(world).getBlockAt(x, y, z);
final BlockState blockState = block.getState();
Runnable runnable = new BlockLookupThread(player, command, block, blockState, page, re, type);
Runnable runnable = new BlockLookupThread(player, command, block, blockState, page, re, type, entitySpawnRowId);
Thread thread = new Thread(runnable);
thread.start();
}
else if (type == 4 || type == 5) {
boolean defaultRe = true;
int pa = 1;
int re = 4;
if (argAction.contains(LookupActions.CHAT) || argAction.contains(LookupActions.COMMAND) || argAction.contains(LookupActions.USERNAME) || LookupActions.isInventoryLookup(argAction)) {
int re = outputMode == LookupOutputMode.SUMMARY ? 8 : 4;
if (outputMode != LookupOutputMode.SUMMARY && (argAction.contains(LookupActions.CHAT) || argAction.contains(LookupActions.COMMAND) || argAction.contains(LookupActions.USERNAME) || LookupActions.isInventoryLookup(argAction))) {
re = 7;
}
if (parseRows > 0) {
@@ -519,8 +574,12 @@ public class LookupCommand {
argExclude = ConfigHandler.lookupElist.get(player.getName());
argExcludeUsers = ConfigHandler.lookupEUserlist.get(player.getName());
argAction = ConfigHandler.lookupAlist.get(player.getName());
argEntityActionFilter = ConfigHandler.lookupEntityActionFilter.getOrDefault(player.getName(), EntityActionFilter.DEFAULT);
argFilters = ConfigHandler.lookupFlist.getOrDefault(player.getName(), Collections.emptyList());
argRadius = ConfigHandler.lookupRadius.get(player.getName());
ts = ConfigHandler.lookupTime.get(player.getName());
outputMode = ConfigHandler.lookupOutputMode.getOrDefault(player.getName(), LookupOutputMode.DETAIL);
rollbackState = ConfigHandler.lookupRollbackState.getOrDefault(player.getName(), LookupRollbackState.ANY);
startTime = 1;
endTime = 0;
}
@@ -580,8 +639,6 @@ public class LookupCommand {
}
try {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Color.ITALIC + Phrase.build(Phrase.LOOKUP_SEARCHING));
if (timeStart == -1) {
if (startTime <= 0) {
timeStart = 0;
@@ -597,7 +654,9 @@ public class LookupCommand {
}
}
Runnable runnable = new StandardLookupThread(player, command, rollbackusers, argBlocks, argExclude, argExcludeUsers, argAction, argRadius, lo, x, y, z, wid, argWid, timeStart, timeEnd, argNoisy, argExcluded, argRestricted, pa, re, type, ts, count);
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Color.ITALIC + Phrase.build(Phrase.LOOKUP_SEARCHING));
Runnable runnable = new StandardLookupThread(player, command, rollbackusers, argBlocks, argExclude, argExcludeUsers, argAction, argEntityActionFilter, argFilters, argRadius, lo, x, y, z, wid, argWid, timeStart, timeEnd, argNoisy, argExcluded, argRestricted, pa, re, type, ts, outputMode, rollbackState);
Thread thread = new Thread(runnable);
thread.start();
}
@@ -624,4 +683,5 @@ public class LookupCommand {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.MISSING_PARAMETERS, "/co l <params>"));
}
}
}
File diff suppressed because it is too large. Load diff
@@ -1,16 +1,25 @@
package net.coreprotect.command;
import java.util.concurrent.CancellationException;
import org.bukkit.command.CommandSender;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.consumer.Consumer;
import net.coreprotect.database.Database;
import net.coreprotect.database.DuckDBRecovery;
import net.coreprotect.language.Phrase;
import net.coreprotect.thread.NetworkHandler;
import net.coreprotect.utility.Chat;
import net.coreprotect.utility.Color;
import net.coreprotect.utility.EntitySpawnTracking;
import net.coreprotect.utility.ErrorReporter;
import net.coreprotect.utility.LookupThrottle;
public class ReloadCommand {
private static final long CONNECTION_DRAIN_TIMEOUT_MILLIS = 60_000L;
protected static void runCommand(final CommandSender player, boolean permission, String[] args) {
if (permission) {
if (ConfigHandler.converterRunning) {
@@ -21,47 +30,91 @@ public class ReloadCommand {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.PURGE_IN_PROGRESS));
return;
}
if (ConfigHandler.lookupThrottle.get(player.getName()) != null) {
Object[] lookupThrottle = ConfigHandler.lookupThrottle.get(player.getName());
if ((boolean) lookupThrottle[0] || ((System.currentTimeMillis() - (long) lookupThrottle[1])) < 100) {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.DATABASE_BUSY));
return;
}
if (!LookupThrottle.tryAcquire(player.getName(), 100)) {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.DATABASE_BUSY));
return;
}
ConfigHandler.lookupThrottle.put(player.getName(), new Object[] { true, System.currentTimeMillis() });
class BasicThread implements Runnable {
@Override
public void run() {
try {
if (Consumer.isPaused) {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.RELOAD_STARTED));
}
while (Consumer.isPaused) {
Thread.sleep(1);
}
Consumer.isPaused = true;
ConfigHandler.performInitialization(false);
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.RELOAD_SUCCESS));
Thread networkHandler = new Thread(new NetworkHandler(false, false));
networkHandler.start();
performReload(player);
}
catch (Exception e) {
ErrorReporter.report(e);
}
Consumer.isPaused = false;
ConfigHandler.lookupThrottle.put(player.getName(), new Object[] { false, System.currentTimeMillis() });
finally {
LookupThrottle.release(player.getName());
}
}
}
Runnable runnable = new BasicThread();
Thread thread = new Thread(runnable);
thread.start();
try {
Runnable runnable = new BasicThread();
Thread thread = new Thread(runnable);
thread.start();
}
catch (RuntimeException | Error e) {
LookupThrottle.release(player.getName());
throw e;
}
}
else {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.NO_PERMISSION));
}
}
static synchronized boolean performReload(CommandSender player) throws InterruptedException {
final boolean resumePersistenceAfterCancellation = !Consumer.isDatabaseReloadPaused();
boolean resumePersistence = resumePersistenceAfterCancellation;
Consumer.OperationStartResult startResult = Consumer.beginDatabaseReload();
if (startResult != Consumer.OperationStartResult.STARTED) {
Phrase phrase = startResult == Consumer.OperationStartResult.PURGE_RUNNING ? Phrase.PURGE_IN_PROGRESS
: startResult == Consumer.OperationStartResult.ROLLBACK_RUNNING ? Phrase.ROLLBACK_IN_PROGRESS
: startResult == Consumer.OperationStartResult.PERSISTENCE_HALTED ? Phrase.DATABASE_PERSISTENCE_HALTED : Phrase.DATABASE_BUSY;
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(phrase));
return false;
}
long deadline = System.nanoTime() + CONNECTION_DRAIN_TIMEOUT_MILLIS * 1_000_000L;
try {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.RELOAD_STARTED));
if (!Consumer.lockDatabaseReload(CONNECTION_DRAIN_TIMEOUT_MILLIS)) {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.RELOAD_FAILED));
return false;
}
if (!Database.awaitConnectionDrain(remainingMillis(deadline))) {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.RELOAD_FAILED));
return false;
}
resumePersistence = false;
boolean initialized;
try {
initialized = ConfigHandler.performInitialization(false);
}
catch (CancellationException e) {
resumePersistence = resumePersistenceAfterCancellation;
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.RELOAD_FAILED));
return false;
}
if (!initialized) {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.RELOAD_FAILED));
return false;
}
EntitySpawnTracking.invalidateDatabaseVerification();
DuckDBRecovery.reset();
resumePersistence = true;
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.RELOAD_SUCCESS));
Thread networkHandler = new Thread(new NetworkHandler(false, false));
networkHandler.start();
return true;
}
finally {
Consumer.endDatabaseReload(resumePersistence);
}
}
private static long remainingMillis(long deadline) {
return Math.max(0L, (deadline - System.nanoTime()) / 1_000_000L);
}
}
@@ -19,28 +19,40 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import net.coreprotect.command.parser.ActionParser;
import net.coreprotect.config.Config;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.consumer.Consumer;
import net.coreprotect.database.ContainerRollback;
import net.coreprotect.database.Database;
import net.coreprotect.database.lookup.PlayerLookup;
import net.coreprotect.database.rollback.Rollback;
import net.coreprotect.language.Phrase;
import net.coreprotect.language.Selector;
import net.coreprotect.model.action.EntityActionFilter;
import net.coreprotect.model.action.LookupActions;
import net.coreprotect.utility.Chat;
import net.coreprotect.utility.Color;
import net.coreprotect.utility.WorldUtils;
import net.coreprotect.utility.ErrorReporter;
import net.coreprotect.utility.LookupThrottle;
public class RollbackRestoreCommand {
public static void runCommand(CommandSender player, Command command, boolean permission, String[] args, Location argLocation, long forceStart, long forceEnd) {
String lookupOnlyFlag = findLookupOnlyFlag(args);
if (lookupOnlyFlag != null) {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.INVALID_PARAMETER, lookupOnlyFlag));
return;
}
Location lo = (argLocation != null ? argLocation : CommandParser.parseLocation(player, args));
List<String> argUuids = new ArrayList<>();
List<String> argUsers = CommandParser.parseUsers(args);
Integer[] argRadius = CommandParser.parseRadius(args, player, lo);
int argNoisy = CommandParser.parseNoisy(args);
List<Integer> argAction = CommandParser.parseAction(args);
ActionParser.ParseResult actionResult = CommandParser.parseActions(args, false);
List<Integer> argAction = actionResult.getActions();
EntityActionFilter argEntityActionFilter = actionResult.getEntityActionFilter();
List<Object> argBlocks = CommandParser.parseRestricted(player, args, argAction);
Map<Object, Boolean> argExclude = CommandParser.parseExcluded(player, args, argAction);
List<String> argExcludeUsers = CommandParser.parseExcludedUsers(player, args);
@@ -49,7 +61,6 @@ public class RollbackRestoreCommand {
long startTime = argTime[0];
long endTime = argTime[1];
int argWid = CommandParser.parseWorld(args, true, true);
boolean count = CommandParser.parseCount(args);
boolean worldedit = CommandParser.parseWorldEdit(args);
boolean forceglobal = CommandParser.parseForceGlobal(args);
int preview = CommandParser.parsePreview(args);
@@ -70,8 +81,9 @@ public class RollbackRestoreCommand {
hasEntity = true;
if (argAction.size() == 0) {
argAction.add(LookupActions.ENTITY_KILL);
argAction.add(LookupActions.ENTITY_SPAWN);
}
else if (!argAction.contains(LookupActions.ENTITY_KILL)) {
else if (!argEntityActionFilter.includesAnyEntity(argAction, true)) {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.INVALID_INCLUDE_COMBO));
return;
}
@@ -87,8 +99,9 @@ public class RollbackRestoreCommand {
hasEntity = true;
if (argAction.size() == 0) {
argAction.add(LookupActions.ENTITY_KILL);
argAction.add(LookupActions.ENTITY_SPAWN);
}
else if (!argAction.contains(LookupActions.ENTITY_KILL)) {
else if (!argEntityActionFilter.includesAnyEntity(argAction, true)) {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.INVALID_INCLUDE_COMBO));
return;
}
@@ -100,10 +113,6 @@ public class RollbackRestoreCommand {
return;
}
if (count) {
LookupCommand.runCommand(player, command, permission, args);
return;
}
if (ConfigHandler.converterRunning) {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.UPGRADE_IN_PROGRESS));
return;
@@ -121,6 +130,10 @@ public class RollbackRestoreCommand {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.PREVIEW_IN_GAME));
return;
}
if (preview > 0 && argAction.contains(LookupActions.SIGN)) {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.ACTION_NOT_SUPPORTED));
return;
}
if (argAction.contains(-1)) {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.INVALID_ACTION));
return;
@@ -156,7 +169,8 @@ public class RollbackRestoreCommand {
final int finalAction = a;
int DEFAULT_RADIUS = Config.getGlobal().DEFAULT_RADIUS;
if ((player instanceof Player || player instanceof BlockCommandSender) && argRadius == null && DEFAULT_RADIUS > 0 && !forceglobal && !argAction.contains(LookupActions.ITEM)) {
boolean exactEntityContainer = argUsers.contains("#container") && ConfigHandler.lookupEntityContainer.get(player.getName()) != null;
if ((player instanceof Player || player instanceof BlockCommandSender) && argRadius == null && DEFAULT_RADIUS > 0 && !forceglobal && !argAction.contains(LookupActions.ITEM) && !argAction.contains(5) && !exactEntityContainer) {
Location location = lo;
int xmin = location.getBlockX() - DEFAULT_RADIUS;
int xmax = location.getBlockX() + DEFAULT_RADIUS;
@@ -194,7 +208,7 @@ public class RollbackRestoreCommand {
argExclude.put(Material.FARMLAND, false);
argExcludeUsers.add("#hopper");
}
else if (!argAction.contains(LookupActions.CONTAINER) && Config.getGlobal().EXCLUDE_TNT && !argExclude.containsKey(Material.TNT) && !argBlocks.contains(Material.TNT)) {
else if (!argAction.contains(LookupActions.CONTAINER) && !argAction.contains(LookupActions.SIGN) && Config.getGlobal().EXCLUDE_TNT && !argExclude.containsKey(Material.TNT) && !argBlocks.contains(Material.TNT)) {
argExclude.put(Material.TNT, true);
}
@@ -219,7 +233,7 @@ public class RollbackRestoreCommand {
return;
}
}
if (argAction.contains(LookupActions.SESSION) || (argAction.contains(LookupActions.ITEM) && !argAction.contains(LookupActions.CONTAINER)) || (!argAction.contains(LookupActions.BLOCK_BREAK) && !argAction.contains(LookupActions.BLOCK_PLACE) && !argAction.contains(LookupActions.ENTITY_KILL) && !argAction.contains(LookupActions.CONTAINER))) {
if (argAction.contains(LookupActions.SESSION) || (argAction.contains(LookupActions.ITEM) && !argAction.contains(LookupActions.CONTAINER)) || (!argAction.contains(LookupActions.BLOCK_BREAK) && !argAction.contains(LookupActions.BLOCK_PLACE) && !argAction.contains(LookupActions.ENTITY_KILL) && !argAction.contains(LookupActions.ENTITY_SPAWN) && !argAction.contains(LookupActions.CONTAINER) && !argAction.contains(LookupActions.SIGN))) {
if (finalAction == 0) {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.ACTION_NOT_SUPPORTED));
}
@@ -293,19 +307,21 @@ public class RollbackRestoreCommand {
argRadius = null;
argWid = 0;
lo = new Location(Bukkit.getServer().getWorld(WorldUtils.getWorldName(wid)), x, y, z);
Block block = lo.getBlock();
if (block.getState() instanceof Chest) {
BlockFace[] blockFaces = new BlockFace[] { BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST };
for (BlockFace face : blockFaces) {
if (block.getRelative(face, 1).getState() instanceof Chest) {
Block relative = block.getRelative(face, 1);
int x2 = relative.getX();
int z2 = relative.getZ();
double newX = (x + x2) / 2.0;
double newZ = (z + z2) / 2.0;
lo.setX(newX);
lo.setZ(newZ);
break;
if (!exactEntityContainer) {
Block block = lo.getBlock();
if (block.getState() instanceof Chest) {
BlockFace[] blockFaces = new BlockFace[] { BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST };
for (BlockFace face : blockFaces) {
if (block.getRelative(face, 1).getState() instanceof Chest) {
Block relative = block.getRelative(face, 1);
int x2 = relative.getX();
int z2 = relative.getZ();
double newX = (x + x2) / 2.0;
double newZ = (z + z2) / 2.0;
lo.setX(newX);
lo.setZ(newZ);
break;
}
}
}
}
@@ -340,16 +356,28 @@ public class RollbackRestoreCommand {
final Location locationFinal = lo;
final int finalArgWid = argWid;
final List<Integer> finalArgAction = argAction;
final EntityActionFilter finalEntityActionFilter = argEntityActionFilter;
final String[] finalArgs = args;
final int finalPreview = preview;
ConfigHandler.activeRollbacks.put(player.getName(), true);
Consumer.OperationStartResult startResult = Consumer.claimRollback(player.getName(), finalPreview == 0);
if (startResult != Consumer.OperationStartResult.STARTED) {
Phrase phrase = startResult == Consumer.OperationStartResult.PURGE_RUNNING ? Phrase.PURGE_IN_PROGRESS
: startResult == Consumer.OperationStartResult.PERSISTENCE_HALTED ? Phrase.DATABASE_PERSISTENCE_HALTED
: startResult == Consumer.OperationStartResult.RELOAD_RUNNING ? Phrase.DATABASE_BUSY : Phrase.ROLLBACK_IN_PROGRESS;
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(phrase));
return;
}
class BasicThread2 implements Runnable {
@Override
public void run() {
if (!LookupThrottle.tryAcquire(player.getName(), 100)) {
Consumer.releaseRollback(player.getName());
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.DATABASE_BUSY));
return;
}
try (Connection connection = Database.getConnection(false, 1000)) {
ConfigHandler.lookupThrottle.put(player.getName(), new Object[] { true, System.currentTimeMillis() });
int action = finalAction;
Location location = locationFinal;
if (connection != null) {
@@ -426,13 +454,14 @@ public class RollbackRestoreCommand {
Chat.sendMessage(player2, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.ROLLBACK_STARTED, users, Selector.SECOND));
}
if (finalArgAction.contains(5)) {
ContainerRollback.performContainerRollbackRestore(statement, player2, uuidList, rollbackusers2, rtime, blist, elist, euserlist, finalArgAction, location, radius, finalTimeStart, finalTimeEnd, restrictWorld, false, verbose, action);
boolean completed = true;
if (finalArgAction.contains(5) && ConfigHandler.lookupEntityContainer.get(player2.getName()) == null) {
completed = ContainerRollback.performContainerRollbackRestore(statement, player2, uuidList, rollbackusers2, rtime, blist, elist, euserlist, finalArgAction, location, radius, finalTimeStart, finalTimeEnd, restrictWorld, false, verbose, action);
}
else {
Rollback.performRollbackRestore(statement, player2, uuidList, rollbackusers2, rtime, blist, elist, euserlist, finalArgAction, location, radius, finalTimeStart, finalTimeEnd, restrictWorld, false, verbose, action, finalPreview);
completed = Rollback.performRollbackRestore(statement, player2, uuidList, rollbackusers2, rtime, blist, elist, euserlist, finalArgAction, finalEntityActionFilter, location, radius, finalTimeStart, finalTimeEnd, restrictWorld, false, verbose, action, finalPreview) != null;
}
if (finalPreview < 2) {
if (completed && finalPreview < 2) {
List<Object> list = new ArrayList<>();
list.add(finalTimeStart);
list.add(finalTimeEnd);
@@ -453,15 +482,21 @@ public class RollbackRestoreCommand {
catch (Exception e) {
ErrorReporter.report(e);
}
if (ConfigHandler.activeRollbacks.get(player2.getName()) != null) {
ConfigHandler.activeRollbacks.remove(player2.getName());
finally {
Consumer.releaseRollback(player2.getName());
LookupThrottle.release(player2.getName());
}
ConfigHandler.lookupThrottle.put(player2.getName(), new Object[] { false, System.currentTimeMillis() });
}
}
Runnable runnable = new BasicThread2();
Thread thread = new Thread(runnable);
thread.start();
try {
thread.start();
}
catch (Exception e) {
Consumer.releaseRollback(player.getName());
throw e;
}
}
catch (Exception e) {
ErrorReporter.report(e);
@@ -489,4 +524,15 @@ public class RollbackRestoreCommand {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.NO_PERMISSION));
}
}
private static String findLookupOnlyFlag(String[] args) {
for (int index = 1; index < args.length; index++) {
String raw = args[index];
String argument = raw.trim().toLowerCase(Locale.ROOT).replace("\\", "").replace("'", "");
if (argument.equals("#count") || argument.equals("#sum") || argument.equals("#summary") || argument.equals("count") || argument.equals("sum")) {
return raw.trim();
}
}
return null;
}
}
@@ -67,16 +67,18 @@ public class StatusCommand {
// Using MySQL/SQLite (Database Size: 587MB)
String firstVersion = Patch.getFirstVersion();
String firstVersion = ConfigHandler.databaseType.isClickHouse() ? "" : Patch.getFirstVersion();
if (firstVersion.length() > 0) {
firstVersion = " (" + Phrase.build(Phrase.FIRST_VERSION, firstVersion) + ")";
}
if (Config.getGlobal().MYSQL) {
Chat.sendMessage(player, Color.DARK_AQUA + Phrase.build(Phrase.STATUS_DATABASE, Color.WHITE, "MySQL") + firstVersion);
String databaseName = ConfigHandler.databaseType.getDisplayName();
if (Consumer.isPersistenceHalted()) {
databaseName += " " + Color.RED + Phrase.build(Phrase.STATUS_DATABASE_STATE, Selector.FIRST) + Color.WHITE;
}
else {
Chat.sendMessage(player, Color.DARK_AQUA + Phrase.build(Phrase.STATUS_DATABASE, Color.WHITE, "SQLite") + firstVersion);
else if (!ConfigHandler.databaseReachable) {
databaseName += " " + Color.RED + Phrase.build(Phrase.STATUS_DATABASE_STATE, Selector.SECOND) + Color.WHITE;
}
Chat.sendMessage(player, Color.DARK_AQUA + Phrase.build(Phrase.STATUS_DATABASE, Color.WHITE, databaseName) + firstVersion);
if (ConfigHandler.worldeditEnabled) {
Chat.sendMessage(player, Color.DARK_AQUA + Phrase.build(Phrase.STATUS_INTEGRATION, Color.WHITE, "WorldEdit", Selector.FIRST));
@@ -17,14 +17,15 @@ import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import org.bukkit.util.StringUtil;
import net.coreprotect.command.parser.RollbackStateParser;
import net.coreprotect.config.ConfigHandler;
public class TabHandler implements TabCompleter {
// private static String[] COMMANDS = new String[] { "help", "inspect", "rollback", "restore", "lookup", "purge", "reload", "status", "near", "undo" }; // max 10!
private static final String[] HELP = new String[] { "inspect", "rollback", "restore", "lookup", "purge", "teleport", "status", "params", "users", "time", "radius", "action", "include", "exclude" };
private static final String[] PARAMS = new String[] { "user:", "time:", "radius:", "action:", "include:", "exclude:", "#container" };
private static final String[] ACTIONS = new String[] { "block", "+block", "-block", "click", "kill", "+container", "-container", "container", "chat", "command", "+inventory", "-inventory", "inventory", "item", "+item", "-item", "sign", "session", "+session", "-session", "username" };
private static final String[] HELP = new String[] { "inspect", "rollback", "restore", "lookup", "purge", "teleport", "status", "params", "users", "time", "radius", "action", "include", "exclude", "filter" };
private static final String[] PARAMS = new String[] { "user:", "time:", "radius:", "action:", "include:", "exclude:", "filter:", "#container" };
private static final String[] ACTIONS = new String[] { "block", "+block", "-block", "click", "kill", "spawn", "+container", "-container", "container", "chat", "command", "+inventory", "-inventory", "inventory", "item", "+item", "-item", "sign", "session", "+session", "-session", "username" };
private static final String[] NUMBERS = new String[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
private static final String[] TIMES = new String[] { "w", "d", "h", "m", "s" };
private static ArrayList<String> materials = null;
@@ -43,7 +44,7 @@ public class TabHandler implements TabCompleter {
String currentArg = args[args.length - 1].toLowerCase(Locale.ROOT).trim();
String lastArg = args.length > 1 ? args[args.length - 2].toLowerCase(Locale.ROOT).trim() : "";
ParamState paramState = getParamState(args);
ParamState paramState = getParamState(sender, args);
// Handle param-specific completions
if (isActionParam(lastArg, currentArg) && hasLookupPermission(sender)) {
@@ -150,20 +151,24 @@ public class TabHandler implements TabCompleter {
boolean hasAction;
boolean hasInclude;
boolean hasExclude;
boolean hasFilter;
boolean hasRadius;
boolean hasTime;
boolean hasContainer;
boolean hasCount;
boolean hasSummary;
boolean hasRollbackState;
boolean hasPreview;
boolean hasPage;
boolean validContainer;
boolean pageLookup;
}
private ParamState getParamState(String[] args) {
private ParamState getParamState(CommandSender sender, String[] args) {
ParamState state = new ParamState();
String senderName = sender.getName();
if (ConfigHandler.lookupType.get(args[0]) != null && ConfigHandler.lookupPage.get(args[0]) != null) {
if (ConfigHandler.lookupType.get(senderName) != null && ConfigHandler.lookupPage.get(senderName) != null) {
state.pageLookup = true;
}
@@ -175,9 +180,18 @@ public class TabHandler implements TabCompleter {
else if (arg.equals("#count") || arg.equals("#sum")) {
state.hasCount = true;
}
else if (arg.equals("#summary")) {
state.hasSummary = true;
}
else if (RollbackStateParser.isModifier(arg)) {
state.hasRollbackState = true;
}
else if (arg.equals("#preview")) {
state.hasPreview = true;
}
else if (arg.startsWith("f:") || arg.startsWith("filter:")) {
state.hasFilter = true;
}
else if ((!arg.contains(":") && !args[i - 1].contains(":") && args.length > (i + 1)) || arg.contains("u:") || arg.contains("user:") || arg.contains("users:") || arg.contains("p:")) {
state.hasUser = true;
}
@@ -202,13 +216,14 @@ public class TabHandler implements TabCompleter {
}
if (!state.hasContainer) {
if (ConfigHandler.lookupType.get(args[0]) != null) {
int lookupType = ConfigHandler.lookupType.get(args[0]);
Integer lookupType = ConfigHandler.lookupType.get(senderName);
if (lookupType != null) {
if (lookupType == 1) {
state.validContainer = true;
}
else if (lookupType == 5) {
if (ConfigHandler.lookupUlist.get(args[0]).contains("#container")) {
List<String> lookupUsers = ConfigHandler.lookupUlist.get(senderName);
if (lookupUsers != null && lookupUsers.contains("#container")) {
state.validContainer = true;
}
}
@@ -432,7 +447,7 @@ public class TabHandler implements TabCompleter {
return StringUtil.copyPartialMatches(argument1, completions, new ArrayList<>(completions.size()));
}
else if (argument0.equals("purge") && sender.hasPermission("coreprotect.purge")) {
List<String> completions = new ArrayList<>(Arrays.asList("t:", "r:", "i:"));
List<String> completions = new ArrayList<>(Arrays.asList("t:", "r:", "i:", "e:", "a:kill"));
return StringUtil.copyPartialMatches(argument1, completions, new ArrayList<>(completions.size()));
}
else if (hasLookupCommand(argument0, sender)) {
@@ -449,10 +464,10 @@ public class TabHandler implements TabCompleter {
String argument2 = arg2.toLowerCase(Locale.ROOT);
if (argument1.startsWith("t:")) {
List<String> completions = new ArrayList<>(Arrays.asList("r:", "i:"));
List<String> completions = new ArrayList<>(Arrays.asList("r:", "i:", "e:", "a:kill"));
return StringUtil.copyPartialMatches(argument2, completions, new ArrayList<>(completions.size()));
}
else if (argument1.startsWith("r:") || argument1.startsWith("i:")) {
else if (argument1.startsWith("r:") || argument1.startsWith("i:") || argument1.startsWith("e:") || argument1.startsWith("a:")) {
List<String> completions = new ArrayList<>(Arrays.asList("t:"));
return StringUtil.copyPartialMatches(argument2, completions, new ArrayList<>(completions.size()));
}
@@ -480,6 +495,9 @@ public class TabHandler implements TabCompleter {
else if (param.equals("exclude:") && !state.hasExclude) {
params.add(param);
}
else if (param.equals("filter:") && !state.hasFilter && (lastArgument.equals("l") || lastArgument.equals("lookup"))) {
params.add(param);
}
else if (param.equals("radius:") && !state.hasRadius) {
params.add(param);
}
@@ -493,9 +511,14 @@ public class TabHandler implements TabCompleter {
if (firstParam && state.pageLookup && (lastArgument.equals("l") || lastArgument.equals("lookup"))) {
params.add("page:");
}
else if (!firstParam && argument.startsWith("#")) {
if (!state.hasCount) {
if (argument.startsWith("#")) {
if (!state.hasCount && !state.hasSummary && (lastArgument.equals("l") || lastArgument.equals("lookup"))) {
params.add("#count");
params.add("#summary");
}
if (!state.hasRollbackState && (lastArgument.equals("l") || lastArgument.equals("lookup"))) {
params.add("#rolledback");
params.add("#restored");
}
if (!state.hasPreview) {
params.add("#preview");
@@ -8,7 +8,6 @@ import org.bukkit.block.BlockState;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.Database;
import net.coreprotect.database.lookup.BlockLookup;
import net.coreprotect.database.lookup.InteractionLookup;
@@ -17,6 +16,7 @@ import net.coreprotect.language.Phrase;
import net.coreprotect.utility.Chat;
import net.coreprotect.utility.Color;
import net.coreprotect.utility.ErrorReporter;
import net.coreprotect.utility.LookupThrottle;
public class BlockLookupThread implements Runnable {
private final CommandSender player;
@@ -26,8 +26,9 @@ public class BlockLookupThread implements Runnable {
private final int page;
private final int limit;
private final int type;
private final Integer entitySpawnRowId;
public BlockLookupThread(CommandSender player, Command command, Block block, BlockState blockState, int page, int limit, int type) {
public BlockLookupThread(CommandSender player, Command command, Block block, BlockState blockState, int page, int limit, int type, Integer entitySpawnRowId) {
this.player = player;
this.command = command;
this.block = block;
@@ -35,12 +36,17 @@ public class BlockLookupThread implements Runnable {
this.page = page;
this.limit = limit;
this.type = type;
this.entitySpawnRowId = entitySpawnRowId;
}
@Override
public void run() {
if (!LookupThrottle.tryAcquire(player.getName(), 50)) {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.DATABASE_BUSY));
return;
}
try (Connection connection = Database.getConnection(true)) {
ConfigHandler.lookupThrottle.put(player.getName(), new Object[] { true, System.currentTimeMillis() });
if (connection != null) {
Statement statement = connection.createStatement();
if (type == 8) {
@@ -65,7 +71,7 @@ public class BlockLookupThread implements Runnable {
blockdata = InteractionLookup.performLookup(command.getName(), statement, block, player, 0, page, limit);
}
else {
blockdata = BlockLookup.performLookup(command.getName(), statement, blockState, player, 0, page, limit);
blockdata = BlockLookup.performLookup(command.getName(), statement, blockState, player, 0, page, limit, entitySpawnRowId);
}
if (blockdata.contains("\n")) {
for (String b : blockdata.split("\n")) {
@@ -85,7 +91,8 @@ public class BlockLookupThread implements Runnable {
catch (Exception e) {
ErrorReporter.report(e);
}
ConfigHandler.lookupThrottle.put(player.getName(), new Object[] { false, System.currentTimeMillis() });
finally {
LookupThrottle.release(player.getName());
}
}
}
@@ -15,6 +15,7 @@ import net.coreprotect.language.Phrase;
import net.coreprotect.utility.Chat;
import net.coreprotect.utility.Color;
import net.coreprotect.utility.ErrorReporter;
import net.coreprotect.utility.LookupThrottle;
public class ChestTransactionLookupThread implements Runnable {
private final CommandSender player;
@@ -33,11 +34,16 @@ public class ChestTransactionLookupThread implements Runnable {
@Override
public void run() {
if (!LookupThrottle.tryAcquire(player.getName(), 50)) {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.DATABASE_BUSY));
return;
}
try (Connection connection = Database.getConnection(true)) {
ConfigHandler.lookupThrottle.put(player.getName(), new Object[] { true, System.currentTimeMillis() });
if (connection != null) {
Statement statement = connection.createStatement();
List<String> blockData = ChestTransactionLookup.performLookup(command.getName(), statement, location, player, page, limit, false);
Integer entitySpawnRowId = ConfigHandler.lookupEntityContainer.get(player.getName());
List<String> blockData = ChestTransactionLookup.performLookup(command.getName(), statement, location, player, page, limit, false, entitySpawnRowId);
for (String data : blockData) {
Chat.sendComponent(player, data);
}
@@ -50,7 +56,8 @@ public class ChestTransactionLookupThread implements Runnable {
catch (Exception e) {
ErrorReporter.report(e);
}
ConfigHandler.lookupThrottle.put(player.getName(), new Object[] { false, System.currentTimeMillis() });
finally {
LookupThrottle.release(player.getName());
}
}
}
@@ -0,0 +1,60 @@
package net.coreprotect.command.lookup;
import java.sql.Connection;
import java.sql.Statement;
import java.util.List;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.Database;
import net.coreprotect.database.lookup.EntityInteractionLookup;
import net.coreprotect.language.Phrase;
import net.coreprotect.utility.Chat;
import net.coreprotect.utility.Color;
import net.coreprotect.utility.ErrorReporter;
import net.coreprotect.utility.LookupThrottle;
public final class EntityInteractionLookupThread implements Runnable {
private final CommandSender player;
private final Command command;
private final int page;
private final int limit;
public EntityInteractionLookupThread(CommandSender player, Command command, int page, int limit) {
this.player = player;
this.command = command;
this.page = page;
this.limit = limit;
}
@Override
public void run() {
if (!LookupThrottle.tryAcquire(player.getName(), 50)) {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.DATABASE_BUSY));
return;
}
try (Connection connection = Database.getConnection(true)) {
if (connection == null) {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.DATABASE_BUSY));
return;
}
Integer entitySpawnRowId = ConfigHandler.lookupEntityInteraction.get(player.getName());
try (Statement statement = connection.createStatement()) {
List<String> results = EntityInteractionLookup.performLookup(command.getName(), statement, player, page, limit, entitySpawnRowId);
for (String result : results) {
Chat.sendComponent(player, result);
}
}
}
catch (Exception e) {
ErrorReporter.report(e);
}
finally {
LookupThrottle.release(player.getName());
}
}
}
@@ -5,9 +5,15 @@ import java.sql.Connection;
import java.sql.Statement;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import org.bukkit.Bukkit;
import org.bukkit.Location;
@@ -20,26 +26,43 @@ import com.google.common.base.Strings;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.Database;
import net.coreprotect.database.Lookup;
import net.coreprotect.database.lookup.EntityInteractionLookup;
import net.coreprotect.database.lookup.PlayerLookup;
import net.coreprotect.database.statement.EntitySpawnStatement;
import net.coreprotect.database.statement.UserStatement;
import net.coreprotect.language.Phrase;
import net.coreprotect.language.Selector;
import net.coreprotect.listener.channel.PluginChannelHandshakeListener;
import net.coreprotect.listener.channel.PluginChannelListener;
import net.coreprotect.model.action.EntityActionFilter;
import net.coreprotect.model.action.LookupActions;
import net.coreprotect.model.action.SessionActions;
import net.coreprotect.model.entity.EntitySpawnRecord;
import net.coreprotect.model.item.InventorySources;
import net.coreprotect.model.item.ItemTransactionActions;
import net.coreprotect.model.lookup.EntityLookupContext;
import net.coreprotect.model.lookup.LookupCursor;
import net.coreprotect.model.lookup.LookupOutputMode;
import net.coreprotect.model.lookup.LookupPage;
import net.coreprotect.model.lookup.LookupRollbackState;
import net.coreprotect.model.lookup.LookupSummaryPage;
import net.coreprotect.model.lookup.LookupSummaryRow;
import net.coreprotect.utility.Chat;
import net.coreprotect.utility.ChatUtils;
import net.coreprotect.utility.Color;
import net.coreprotect.utility.EntityUtils;
import net.coreprotect.utility.EntitySpawnTracking;
import net.coreprotect.utility.ItemUtils;
import net.coreprotect.utility.MaterialUtils;
import net.coreprotect.utility.StringUtils;
import net.coreprotect.utility.WorldUtils;
import net.coreprotect.utility.ErrorReporter;
import net.coreprotect.utility.LookupThrottle;
public class StandardLookupThread implements Runnable {
private static final int SUMMARY_QUERY_TIMEOUT_SECONDS = 30;
private static final AtomicBoolean SUMMARY_LOOKUP_ACTIVE = new AtomicBoolean(false);
private final CommandSender player;
private final Command command;
private final List<String> rollbackUsers;
@@ -47,6 +70,8 @@ public class StandardLookupThread implements Runnable {
private final Map<Object, Boolean> excludedBlocks;
private final List<String> excludedUsers;
private final List<Integer> actions;
private final EntityActionFilter entityActionFilter;
private final List<String> messageFilters;
private final Integer[] radius;
private final Location location;
private final int x;
@@ -63,9 +88,10 @@ public class StandardLookupThread implements Runnable {
private final int displayResults;
private final int typeLookup;
private final String rtime;
private final boolean count;
private final LookupOutputMode outputMode;
private final LookupRollbackState rollbackState;
public StandardLookupThread(CommandSender player, Command command, List<String> rollbackUsers, List<Object> blockList, Map<Object, Boolean> excludedBlocks, List<String> excludedUsers, List<Integer> actions, Integer[] radius, Location location, int x, int y, int z, int worldId, int argWorldId, long timeStart, long timeEnd, int noisy, int excluded, int restricted, int page, int displayResults, int typeLookup, String rtime, boolean count) {
public StandardLookupThread(CommandSender player, Command command, List<String> rollbackUsers, List<Object> blockList, Map<Object, Boolean> excludedBlocks, List<String> excludedUsers, List<Integer> actions, EntityActionFilter entityActionFilter, List<String> messageFilters, Integer[] radius, Location location, int x, int y, int z, int worldId, int argWorldId, long timeStart, long timeEnd, int noisy, int excluded, int restricted, int page, int displayResults, int typeLookup, String rtime, LookupOutputMode outputMode, LookupRollbackState rollbackState) {
this.player = player;
this.command = command;
this.rollbackUsers = rollbackUsers;
@@ -73,6 +99,8 @@ public class StandardLookupThread implements Runnable {
this.excludedBlocks = excludedBlocks;
this.excludedUsers = excludedUsers;
this.actions = actions;
this.entityActionFilter = entityActionFilter;
this.messageFilters = messageFilters;
this.radius = radius;
this.location = location;
this.x = x;
@@ -89,15 +117,33 @@ public class StandardLookupThread implements Runnable {
this.displayResults = displayResults;
this.typeLookup = typeLookup;
this.rtime = rtime;
this.count = count;
this.outputMode = outputMode;
this.rollbackState = rollbackState;
}
@Override
public void run() {
try (Connection connection = Database.getConnection(true)) {
ConfigHandler.lookupThrottle.put(player.getName(), new Object[] { true, System.currentTimeMillis() });
boolean summaryLookup = outputMode == LookupOutputMode.SUMMARY;
if (summaryLookup && !SUMMARY_LOOKUP_ACTIVE.compareAndSet(false, true)) {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.DATABASE_BUSY));
return;
}
if (!LookupThrottle.tryAcquire(player.getName(), 50)) {
if (summaryLookup) {
SUMMARY_LOOKUP_ACTIVE.set(false);
}
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.DATABASE_BUSY));
return;
}
try (Connection connection = Database.getConnection(true)) {
List<String> uuidList = new ArrayList<>();
Integer entityContainerId = actions.contains(5) ? ConfigHandler.lookupEntityContainer.get(player.getName()) : null;
ConfigHandler.lookupEntityInteraction.remove(player.getName());
if (!actions.contains(5)) {
ConfigHandler.lookupEntityContainer.remove(player.getName());
}
Location finalLocation = location;
boolean exists = false;
String bc = x + "." + y + "." + z + "." + worldId + "." + timeStart + "." + timeEnd + "." + noisy + "." + excluded + "." + restricted + "." + argWorldId + "." + displayResults;
@@ -110,10 +156,20 @@ public class StandardLookupThread implements Runnable {
ConfigHandler.lookupBlist.put(player.getName(), blockList);
ConfigHandler.lookupUlist.put(player.getName(), rollbackUsers);
ConfigHandler.lookupAlist.put(player.getName(), actions);
ConfigHandler.lookupEntityActionFilter.put(player.getName(), entityActionFilter);
ConfigHandler.lookupFlist.put(player.getName(), messageFilters);
ConfigHandler.lookupRadius.put(player.getName(), radius);
ConfigHandler.lookupOutputMode.put(player.getName(), outputMode == LookupOutputMode.COUNT ? LookupOutputMode.DETAIL : outputMode);
ConfigHandler.lookupRollbackState.put(player.getName(), rollbackState);
if (typeLookup != 5 || outputMode != LookupOutputMode.DETAIL || !ConfigHandler.databaseType.isColumnar()) {
ConfigHandler.lookupCursor.remove(player.getName());
}
if (connection != null) {
Statement statement = connection.createStatement();
if (summaryLookup) {
statement.setQueryTimeout(SUMMARY_QUERY_TIMEOUT_SECONDS);
}
String baduser = "";
for (String check : rollbackUsers) {
if ((!check.equals("#global") && !check.equals("#container")) || actions.contains(LookupActions.USERNAME)) {
@@ -123,8 +179,8 @@ public class StandardLookupThread implements Runnable {
break;
}
else if (actions.contains(LookupActions.USERNAME)) {
if (ConfigHandler.uuidCache.get(check.toLowerCase(Locale.ROOT)) != null) {
String uuid = ConfigHandler.uuidCache.get(check.toLowerCase(Locale.ROOT));
String uuid = UserStatement.getUuid(connection, check);
if (uuid != null) {
uuidList.add(uuid);
}
}
@@ -171,15 +227,34 @@ public class StandardLookupThread implements Runnable {
finalLocation = new Location(Bukkit.getServer().getWorld(WorldUtils.getWorldName(worldId)), x, y, z);
}
Long[] rowData = new Long[] { 0L, 0L, 0L, 0L };
EntityLookupContext entityContext = EntityLookupContext.legacy(Collections.emptySet(), Collections.emptySet());
boolean includeEntitySpawns = entityActionFilter.includesAnySpawn(actions, true);
boolean includeEntityContainers = entityContainerId != null || actions.contains(LookupActions.CONTAINER) || LookupActions.isInventoryLookup(actions) || actions.isEmpty();
boolean includeEntityInteractions = actions.isEmpty() || actions.contains(LookupActions.INTERACTION);
if (!ConfigHandler.databaseType.isDuckDB() && (includeEntitySpawns || includeEntityContainers || includeEntityInteractions) && radius != null && finalLocation != null && finalLocation.getWorld() != null) {
Set<UUID> databaseCandidates = includeEntityContainers || includeEntityInteractions ? EntitySpawnStatement.loadActiveUuids(connection, finalLocation, radius) : EntitySpawnStatement.loadActiveUuids(connection, finalLocation, radius, timeStart, timeEnd);
EntitySpawnTracking.LoadedEntityRadius loadedEntities = EntitySpawnTracking.findLoadedEntities(finalLocation, radius, databaseCandidates);
entityContext = EntityLookupContext.legacy(loadedEntities.getInside(), loadedEntities.getLoadedCandidates());
}
Long[] rowData = new Long[] { 0L, 0L, 0L, 0L, 0L };
long rowMax = (long) page * displayResults;
long pageStart = rowMax - displayResults;
long rows = 0L;
long recordRows = 0L;
boolean checkRows = true;
LookupPage lookupPage = null;
List<LookupSummaryRow> summaryRows = null;
if (typeLookup == 5 && page > 1) {
rowData = ConfigHandler.lookupRows.get(player.getName());
rows = rowData[3];
if (rowData == null || rowData.length < 5) {
rowData = new Long[] { 0L, 0L, 0L, 0L, 0L };
}
rows = rowData[4];
if (outputMode == LookupOutputMode.SUMMARY) {
recordRows = rowData[0];
}
if (pageStart < rows) {
checkRows = false;
@@ -187,16 +262,100 @@ public class StandardLookupThread implements Runnable {
}
if (checkRows) {
rows = Lookup.countLookupRows(statement, player, uuidList, userList, blockList, excludedBlocks, excludedUsers, actions, finalLocation, radius, rowData, timeStart, timeEnd, restrict_world, true);
rowData[3] = rows;
if (outputMode == LookupOutputMode.SUMMARY) {
rows = 0L;
recordRows = Lookup.countLookupRows(statement, player, uuidList, userList, blockList, excludedBlocks, excludedUsers, actions, entityActionFilter, messageFilters, entityContext, finalLocation, radius, rowData, timeStart, timeEnd, restrict_world, true, entityContainerId, rollbackState);
if (recordRows > 0 && pageStart == 0 && Lookup.supportsSummaryWindowFunctions(statement)) {
LookupSummaryPage summaryPage = Lookup.performSummaryLookupPage(statement, player, uuidList, userList, blockList, excludedBlocks, excludedUsers, actions, entityActionFilter, entityContext, finalLocation, radius, timeStart, timeEnd, (int) pageStart, displayResults, restrict_world, entityContainerId, rollbackState);
rows = summaryPage.getTotalRows();
summaryRows = summaryPage.getRows();
}
else if (recordRows > 0) {
rows = Lookup.countSummaryRows(statement, player, uuidList, userList, blockList, excludedBlocks, excludedUsers, actions, entityActionFilter, entityContext, finalLocation, radius, timeStart, timeEnd, restrict_world, entityContainerId, rollbackState);
}
rowData[0] = recordRows;
rowData[1] = 0L;
rowData[2] = 0L;
rowData[3] = 0L;
}
else {
if (pageStart == 0 && outputMode == LookupOutputMode.DETAIL && ConfigHandler.databaseType.isDuckDB()) {
lookupPage = Lookup.performDuckDBLookupPage(statement, player, uuidList, userList, blockList, excludedBlocks, excludedUsers, actions, entityActionFilter, messageFilters, entityContext, finalLocation, radius, rowData, timeStart, timeEnd, displayResults, restrict_world, true, entityContainerId, rollbackState);
rows = lookupPage.getTotalRows();
}
else {
rows = Lookup.countLookupRows(statement, player, uuidList, userList, blockList, excludedBlocks, excludedUsers, actions, entityActionFilter, messageFilters, entityContext, finalLocation, radius, rowData, timeStart, timeEnd, restrict_world, true, entityContainerId, rollbackState);
}
}
rowData[4] = rows;
ConfigHandler.lookupRows.put(player.getName(), rowData);
}
if (count) {
String row_format = NumberFormat.getInstance().format(rows);
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.LOOKUP_ROWS_FOUND, row_format, (rows == 1 ? Selector.FIRST : Selector.SECOND)));
if (lookupPage == null && outputMode == LookupOutputMode.DETAIL && ConfigHandler.databaseType.isColumnar() && pageStart < rows) {
LookupCursor cursor = ConfigHandler.lookupCursor.get(player.getName());
lookupPage = Lookup.performLookupPage(statement, player, uuidList, userList, blockList, excludedBlocks, excludedUsers, actions, entityActionFilter, messageFilters, entityContext, finalLocation, radius, rowData, timeStart, timeEnd, (int) pageStart, displayResults, rows, cursor, restrict_world, true, entityContainerId, rollbackState);
}
if (lookupPage != null) {
LookupCursor nextCursor = lookupPage.getNextCursor();
if (nextCursor == null) {
ConfigHandler.lookupCursor.remove(player.getName());
}
else {
ConfigHandler.lookupCursor.put(player.getName(), nextCursor);
}
}
if (outputMode == LookupOutputMode.COUNT) {
outputRowCount(rows);
}
else if (outputMode == LookupOutputMode.SUMMARY && (pageStart < rows || (pageStart == 0 && recordRows > 0))) {
if (summaryRows == null) {
summaryRows = rows > 0
? Lookup.performSummaryLookup(statement, player, uuidList, userList, blockList, excludedBlocks, excludedUsers, actions, entityActionFilter, entityContext, finalLocation, radius, timeStart, timeEnd, (int) pageStart, displayResults, restrict_world, entityContainerId, rollbackState)
: Collections.emptyList();
}
outputSummary(connection, summaryRows, rows, recordRows);
}
else if (pageStart < rows) {
List<String[]> lookupList = Lookup.performPartialLookup(statement, player, uuidList, userList, blockList, excludedBlocks, excludedUsers, actions, finalLocation, radius, rowData, timeStart, timeEnd, (int) pageStart, displayResults, restrict_world, true);
List<String[]> lookupList = lookupPage == null
? Lookup.performPartialLookup(statement, player, uuidList, userList, blockList, excludedBlocks, excludedUsers, actions, entityActionFilter, messageFilters, entityContext, finalLocation, radius, rowData, timeStart, timeEnd, (int) pageStart, displayResults, restrict_world, true, entityContainerId, rollbackState)
: lookupPage.getRows();
if (lookupList == null) {
return;
}
Map<Integer, EntitySpawnRecord> entitySpawnRecords = Collections.emptyMap();
Map<UUID, Location> loadedEntityLocations = Collections.emptyMap();
Set<Integer> entitySpawnRowIds = new HashSet<>();
for (String[] data : lookupList) {
if (data.length > 7 && data[6] != null && data[7] != null && Integer.parseInt(data[7]) == LookupActions.ENTITY_SPAWN) {
entitySpawnRowIds.add(Integer.parseInt(data[6]));
}
if (data.length > 14 && data[13] != null && data[14] != null && Integer.parseInt(data[13]) == InventorySources.ENTITY_CONTAINER) {
entitySpawnRowIds.add(Integer.parseInt(data[14]));
}
if (data.length > 14 && data[13] != null && data[14] != null && Integer.parseInt(data[13]) == InventorySources.ENTITY_INTERACTION) {
entitySpawnRowIds.add(Integer.parseInt(data[14]));
}
}
if (!entitySpawnRowIds.isEmpty()) {
entitySpawnRecords = EntitySpawnStatement.loadLocationRecords(connection, entitySpawnRowIds);
Set<UUID> entitySpawnUuids = new HashSet<>();
for (EntitySpawnRecord record : entitySpawnRecords.values()) {
entitySpawnUuids.add(record.getUuid());
}
try {
loadedEntityLocations = EntitySpawnTracking.findLoadedLocations(entitySpawnUuids);
}
catch (Exception e) {
ErrorReporter.report(e);
}
}
Map<String[], EntityDisplayLocation> entityDisplayLocations = new IdentityHashMap<>();
for (String[] data : lookupList) {
EntityDisplayLocation displayLocation = resolveEntityDisplayLocation(data, entitySpawnRecords, loadedEntityLocations);
if (displayLocation != null) {
entityDisplayLocations.put(data, displayLocation);
}
}
Chat.sendMessage(player, Color.WHITE + "----- " + Color.DARK_AQUA + Phrase.build(Phrase.LOOKUP_HEADER, "CoreProtect" + Color.WHITE + " | " + Color.DARK_AQUA) + Color.WHITE + " -----");
if (actions.contains(LookupActions.CHAT) || actions.contains(LookupActions.COMMAND)) {
@@ -243,7 +402,7 @@ public class StandardLookupThread implements Runnable {
else if (actions.contains(LookupActions.USERNAME)) {
for (String[] data : lookupList) {
String time = data[0];
String user = ConfigHandler.uuidCacheReversed.get(data[1]);
String user = UserStatement.getNameByUuid(connection, data[1]);
String username = data[2];
String timeago = ChatUtils.getTimeSince(Integer.parseInt(time), unixtimestamp, true);
Chat.sendComponent(player, timeago + " " + Color.WHITE + "- " + Phrase.build(Phrase.LOOKUP_USERNAME, Color.DARK_AQUA + user + Color.WHITE, Color.DARK_AQUA + username + Color.WHITE));
@@ -286,7 +445,14 @@ public class StandardLookupThread implements Runnable {
int dataX = Integer.parseInt(data[2]);
int dataY = Integer.parseInt(data[3]);
int dataZ = Integer.parseInt(data[4]);
String rbd = ((Integer.parseInt(data[8]) == 2 || Integer.parseInt(data[8]) == 3) ? Color.STRIKETHROUGH : "");
EntityDisplayLocation entityLocation = entityDisplayLocations.get(data);
if (entityLocation != null) {
wid = entityLocation.worldId;
dataX = entityLocation.x;
dataY = entityLocation.y;
dataZ = entityLocation.z;
}
String rbd = MaterialUtils.rolledBack(Integer.parseInt(data[8]), true) == 1 ? Color.STRIKETHROUGH : "";
String timeago = ChatUtils.getTimeSince(Integer.parseInt(time), unixtimestamp, true);
Material blockType = ItemUtils.itemFilter(MaterialUtils.getType(dtype), (Integer.parseInt(data[13]) == 0));
String dname = StringUtils.nameFilter(blockType.name().toLowerCase(Locale.ROOT), ddata);
@@ -321,7 +487,8 @@ public class StandardLookupThread implements Runnable {
tag = (daction == ItemTransactionActions.REMOVE ? Color.GREEN + "+" : Color.RED + "-");
}
Chat.sendComponent(player, timeago + " " + tag + " " + Phrase.build(Phrase.LOOKUP_CONTAINER, Color.DARK_AQUA + rbd + dplayer + Color.WHITE + rbd, "x" + amount, ChatUtils.createTooltip(Color.DARK_AQUA + rbd + dname, tooltip) + ChatUtils.filterComponent(player.hasPermission("coreprotect.give"), ChatUtils.createGiveItemComponent(Color.GREY + "(↓)", command.getName(), itemId)) + Color.WHITE, selector));
String coordinateInfo = entityLocation == null ? "" : entityLocation.getOriginTooltip();
Chat.sendComponent(player, timeago + " " + tag + " " + Phrase.build(Phrase.LOOKUP_CONTAINER, Color.DARK_AQUA + rbd + dplayer + Color.WHITE + rbd, "x" + amount, ChatUtils.createTooltip(Color.DARK_AQUA + rbd + dname, tooltip) + coordinateInfo + ChatUtils.filterComponent(player.hasPermission("coreprotect.give"), ChatUtils.createGiveItemComponent(Color.GREY + "(↓)", command.getName(), itemId)) + Color.WHITE, selector));
PluginChannelListener.getInstance().sendData(player, Integer.parseInt(time), Phrase.LOOKUP_CONTAINER, selector, dplayer, dname, amount, dataX, dataY, dataZ, wid, rbd, true, tag.contains("+"));
}
}
@@ -329,7 +496,7 @@ public class StandardLookupThread implements Runnable {
for (String[] data : lookupList) {
int drb = Integer.parseInt(data[8]);
String rbd = "";
if (drb == 1 || drb == 3) {
if (MaterialUtils.rolledBack(drb, actions.contains(LookupActions.ITEM)) == 1) {
rbd = Color.STRIKETHROUGH;
}
@@ -341,8 +508,18 @@ public class StandardLookupThread implements Runnable {
int dtype = Integer.parseInt(data[5]);
int ddata = Integer.parseInt(data[6]);
int daction = Integer.parseInt(data[7]);
boolean entityInteraction = data[13] != null && Integer.parseInt(data[13]) == InventorySources.ENTITY_INTERACTION;
boolean placedEntitySpawn = daction == LookupActions.ENTITY_SPAWN && EntitySpawnTracking.isPlacedEntityType(EntityUtils.getEntityType(dtype));
boolean placedEntityKill = daction == LookupActions.ENTITY_KILL && EntitySpawnTracking.isPlacedEntityType(EntityUtils.getEntityType(dtype));
int wid = Integer.parseInt(data[9]);
int amount = Integer.parseInt(data[10]);
EntityDisplayLocation entityLocation = entityDisplayLocations.get(data);
if (entityLocation != null) {
wid = entityLocation.worldId;
dataX = entityLocation.x;
dataY = entityLocation.y;
dataZ = entityLocation.z;
}
String tag = Color.WHITE + "-";
String timeago = ChatUtils.getTimeSince(Integer.parseInt(time), unixtimestamp, true);
@@ -357,12 +534,12 @@ public class StandardLookupThread implements Runnable {
String dname = "";
boolean isPlayer = false;
if (daction == LookupActions.ENTITY_KILL && !actions.contains(LookupActions.ITEM) && amount == -1) {
if (dtype == 0) {
if (ConfigHandler.playerIdCacheReversed.get(ddata) == null) {
UserStatement.loadName(connection, ddata);
}
dname = ConfigHandler.playerIdCacheReversed.get(ddata);
if (entityInteraction) {
dname = EntityInteractionLookup.entityName(dtype);
}
else if ((daction == LookupActions.ENTITY_KILL || daction == LookupActions.ENTITY_SPAWN) && !actions.contains(LookupActions.ITEM) && amount == -1) {
if (daction == LookupActions.ENTITY_KILL && dtype == 0) {
dname = UserStatement.getName(connection, ddata);
isPlayer = true;
}
else {
@@ -421,11 +598,30 @@ public class StandardLookupThread implements Runnable {
PluginChannelListener.getInstance().sendData(player, Integer.parseInt(time), phrase, selector, dplayer, dname, (tag.contains("+") ? 1 : -1), dataX, dataY, dataZ, wid, rbd, action.contains("container"), tag.contains("+"));
}
else {
if (daction == LookupActions.INTERACTION || daction == LookupActions.ENTITY_KILL) {
phrase = Phrase.LOOKUP_INTERACTION; // {clicked|killed}
selector = (daction != LookupActions.ENTITY_KILL ? Selector.FIRST : Selector.SECOND);
tag = (daction != LookupActions.ENTITY_KILL ? Color.WHITE + "-" : Color.RED + "-");
action = (daction == LookupActions.INTERACTION ? "a:click" : "a:kill");
if (entityInteraction) {
phrase = Phrase.LOOKUP_ENTITY_INTERACTION;
selector = EntityInteractionLookup.actionSelector(ddata);
action = "a:click";
}
else if (daction == LookupActions.ENTITY_SPAWN) {
phrase = placedEntitySpawn ? Phrase.LOOKUP_BLOCK : Phrase.LOOKUP_ENTITY_SPAWN;
selector = Selector.FIRST;
tag = Color.GREEN + "+";
action = placedEntitySpawn ? "a:block" : "a:spawn";
}
else if (daction == LookupActions.INTERACTION || daction == LookupActions.ENTITY_KILL) {
if (placedEntityKill) {
phrase = Phrase.LOOKUP_BLOCK;
selector = Selector.SECOND;
tag = Color.RED + "-";
action = "a:block";
}
else {
phrase = Phrase.LOOKUP_INTERACTION; // {clicked|killed}
selector = (daction != LookupActions.ENTITY_KILL ? Selector.FIRST : Selector.SECOND);
tag = (daction != LookupActions.ENTITY_KILL ? Color.WHITE + "-" : Color.RED + "-");
action = (daction == LookupActions.INTERACTION ? "a:click" : "a:kill");
}
}
else {
phrase = Phrase.LOOKUP_BLOCK; // {placed|broke}
@@ -438,7 +634,9 @@ public class StandardLookupThread implements Runnable {
}
action = (actions.size() == 0 ? " (" + action + ")" : "");
Chat.sendComponent(player, Color.WHITE + leftPadding + Color.GREY + "^ " + ChatUtils.getCoordinates(command.getName(), wid, dataX, dataY, dataZ, true, true) + Color.GREY + Color.ITALIC + action);
String coordinates = ChatUtils.getCoordinates(command.getName(), wid, dataX, dataY, dataZ, true, true);
String coordinateInfo = entityLocation == null ? "" : entityLocation.getOriginTooltip();
Chat.sendComponent(player, Color.WHITE + leftPadding + Color.GREY + "^ " + coordinates + Color.GREY + Color.ITALIC + action + coordinateInfo);
}
}
if (rows > displayResults) {
@@ -449,7 +647,7 @@ public class StandardLookupThread implements Runnable {
Chat.sendComponent(player, ChatUtils.getPageNavigation(command.getName(), page, total_pages));
}
}
else if (rows > 0) {
else if (rows > 0 || (outputMode == LookupOutputMode.SUMMARY && recordRows > 0)) {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.NO_RESULTS_PAGE, Selector.FIRST));
}
else {
@@ -468,7 +666,131 @@ public class StandardLookupThread implements Runnable {
catch (Exception e) {
ErrorReporter.report(e);
}
finally {
if (summaryLookup) {
SUMMARY_LOOKUP_ACTIVE.set(false);
}
LookupThrottle.release(player.getName());
}
}
ConfigHandler.lookupThrottle.put(player.getName(), new Object[] { false, System.currentTimeMillis() });
private void outputRowCount(long rows) {
String formattedRows = NumberFormat.getInstance().format(rows);
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.LOOKUP_ROWS_FOUND, formattedRows, rows == 1 ? Selector.FIRST : Selector.SECOND));
}
private void outputSummary(Connection connection, List<LookupSummaryRow> summaryRows, long totalRows, long recordRows) {
if (summaryRows.isEmpty()) {
if (page == 1 && recordRows > 0) {
outputRowCount(recordRows);
}
else {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.NO_RESULTS_PAGE, Selector.FIRST));
}
return;
}
NumberFormat numberFormat = NumberFormat.getInstance();
String rowsFound = Phrase.build(Phrase.LOOKUP_ROWS_FOUND, numberFormat.format(recordRows), recordRows == 1 ? Selector.FIRST : Selector.SECOND);
Chat.sendMessage(player, Color.WHITE + "----- " + Color.DARK_AQUA + "CoreProtect" + Color.WHITE + " | " + Color.DARK_AQUA + rowsFound + Color.WHITE + " -----");
for (LookupSummaryRow row : summaryRows) {
String userName = UserStatement.getName(connection, row.getUserId());
if (userName == null || userName.isEmpty()) {
userName = "unknown";
}
String materialName = MaterialUtils.getBlockDisplayName(row.getMaterialId(), 0);
if (materialName == null || materialName.isEmpty()) {
materialName = MaterialUtils.getBlockNameShort(row.getMaterialId());
}
if (materialName == null || materialName.isEmpty()) {
materialName = "#" + row.getMaterialId();
}
if (materialName.startsWith("minecraft:")) {
materialName = materialName.substring("minecraft:".length());
}
long removedAmount = row.getRemovedAmount();
long placedAmount = row.getPlacedAmount();
long netAmount = row.getAmount();
String formattedNetAmount = (netAmount >= 0 ? "+" : "") + numberFormat.format(netAmount);
Chat.sendComponent(player, Color.DARK_AQUA + userName + Color.WHITE + ": " + Color.RED + "-" + numberFormat.format(removedAmount)
+ Color.GREY + " / " + Color.GREEN + "+" + numberFormat.format(placedAmount) + Color.WHITE + " = " + formattedNetAmount
+ Color.WHITE + " " + Color.DARK_AQUA + materialName + Color.WHITE);
}
if (totalRows > displayResults) {
int totalPages = (int) Math.ceil(totalRows / (displayResults + 0.0));
Chat.sendComponent(player, ChatUtils.getPageNavigation(command.getName(), page, totalPages));
}
}
private static EntityDisplayLocation resolveEntityDisplayLocation(String[] data, Map<Integer, EntitySpawnRecord> records, Map<UUID, Location> loadedLocations) {
if (data.length <= 14 || data[7] == null) {
return null;
}
int trackingRowId = 0;
if (data[13] != null && (Integer.parseInt(data[13]) == InventorySources.ENTITY_CONTAINER || Integer.parseInt(data[13]) == InventorySources.ENTITY_INTERACTION) && data[14] != null) {
trackingRowId = Integer.parseInt(data[14]);
}
else if (data[6] != null && Integer.parseInt(data[7]) == LookupActions.ENTITY_SPAWN) {
trackingRowId = Integer.parseInt(data[6]);
}
if (trackingRowId == 0) {
return null;
}
EntitySpawnRecord record = records.get(trackingRowId);
if (record == null) {
return null;
}
int currentWorldId = record.getWorldId();
int currentX = (int) Math.floor(record.getX());
int currentY = (int) Math.floor(record.getY());
int currentZ = (int) Math.floor(record.getZ());
Location loadedLocation = loadedLocations.get(record.getUuid());
if (loadedLocation != null && loadedLocation.getWorld() != null) {
currentWorldId = WorldUtils.getWorldId(loadedLocation.getWorld().getName());
currentX = loadedLocation.getBlockX();
currentY = loadedLocation.getBlockY();
currentZ = loadedLocation.getBlockZ();
}
boolean interactionOrigin = data[13] != null && Integer.parseInt(data[13]) == InventorySources.ENTITY_INTERACTION;
return new EntityDisplayLocation(currentWorldId, currentX, currentY, currentZ, Integer.parseInt(data[9]), Integer.parseInt(data[2]), Integer.parseInt(data[3]), Integer.parseInt(data[4]), interactionOrigin);
}
private static final class EntityDisplayLocation {
private final int worldId;
private final int x;
private final int y;
private final int z;
private final int originWorldId;
private final int originX;
private final int originY;
private final int originZ;
private final boolean interactionOrigin;
private EntityDisplayLocation(int worldId, int x, int y, int z, int originWorldId, int originX, int originY, int originZ, boolean interactionOrigin) {
this.worldId = worldId;
this.x = x;
this.y = y;
this.z = z;
this.originWorldId = originWorldId;
this.originX = originX;
this.originY = originY;
this.originZ = originZ;
this.interactionOrigin = interactionOrigin;
}
private String getOriginTooltip() {
if (worldId == originWorldId && x == originX && y == originY && z == originZ) {
return "";
}
Phrase originPhrase = interactionOrigin ? Phrase.LOOKUP_ENTITY_INTERACTION_ORIGIN : Phrase.LOOKUP_ENTITY_ORIGIN;
return ChatUtils.getCoordinateTooltip(originWorldId, originX, originY, originZ, Phrase.build(originPhrase), true);
}
}
}
@@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import net.coreprotect.model.action.EntityActionFilter;
import net.coreprotect.model.action.LookupActions;
import net.coreprotect.model.action.SessionActions;
import net.coreprotect.model.item.ItemTransactionActions;
@@ -36,8 +37,17 @@ public class ActionParser {
* @return A list of action integers
*/
public static List<Integer> parseAction(String[] inputArguments) {
return parseAction(inputArguments, false);
}
public static List<Integer> parseAction(String[] inputArguments, boolean allowMultiple) {
return parseActions(inputArguments, allowMultiple).getActions();
}
public static ParseResult parseActions(String[] inputArguments, boolean allowMultiple) {
String[] argumentArray = inputArguments.clone();
List<Integer> result = new ArrayList<>();
EntityActionFilter entityActionFilter = EntityActionFilter.DEFAULT;
int count = 0;
int next = 0;
for (String argument : argumentArray) {
@@ -46,25 +56,43 @@ public class ActionParser {
argument = argument.replaceAll("\\\\", "");
argument = argument.replaceAll("'", "");
if (next == 1 && MessageFilterParser.isLookupTerminator(argument)) {
next = 0;
}
if (argument.equals("a:") || argument.equals("action:")) {
next = 1;
}
else if (next == 1 || argument.startsWith("a:") || argument.startsWith("action:")) {
result.clear();
entityActionFilter = EntityActionFilter.NONE;
argument = argument.replaceAll("action:", "");
argument = argument.replaceAll("a:", "");
if (argument.startsWith("#")) {
argument = argument.replaceFirst("#", "");
}
if (allowMultiple && argument.contains(",")) {
for (String action : argument.split(",", -1)) {
if (!action.isEmpty()) {
ParseResult parsedAction = parseActions(new String[] { "lookup", "a:" + action }, false);
result.addAll(parsedAction.getActions());
entityActionFilter = entityActionFilter.merge(parsedAction.getEntityActionFilter());
}
}
next = 0;
continue;
}
if (argument.equals("broke") || argument.equals("break") || argument.equals("remove") || argument.equals("destroy") || argument.equals("block-break") || argument.equals("block-remove") || argument.equals("-block") || argument.equals("-blocks") || argument.equals("block-")) {
result.add(LookupActions.BLOCK_BREAK);
entityActionFilter = EntityActionFilter.ALIASED;
}
else if (argument.equals("placed") || argument.equals("place") || argument.equals("block-place") || argument.equals("+block") || argument.equals("+blocks") || argument.equals("block+")) {
result.add(LookupActions.BLOCK_PLACE);
entityActionFilter = EntityActionFilter.ALIASED;
}
else if (argument.equals("block") || argument.equals("blocks") || argument.equals("block-change") || argument.equals("change") || argument.equals("changes")) {
result.add(LookupActions.BLOCK_BREAK);
result.add(LookupActions.BLOCK_PLACE);
entityActionFilter = EntityActionFilter.ALIASED;
}
else if (argument.equals("click") || argument.equals("clicks") || argument.equals("interact") || argument.equals("interaction") || argument.equals("player-interact") || argument.equals("player-interaction") || argument.equals("player-click")) {
result.add(LookupActions.INTERACTION);
@@ -72,6 +100,10 @@ public class ActionParser {
else if (argument.equals("death") || argument.equals("deaths") || argument.equals("entity-death") || argument.equals("entity-deaths") || argument.equals("kill") || argument.equals("kills") || argument.equals("entity-kill") || argument.equals("entity-kills")) {
result.add(LookupActions.ENTITY_KILL);
}
else if (argument.equals("spawn") || argument.equals("spawns") || argument.equals("entity-spawn") || argument.equals("entity-spawns")) {
result.add(LookupActions.ENTITY_SPAWN);
entityActionFilter = EntityActionFilter.SPAWNED;
}
else if (argument.equals("container") || argument.equals("container-change") || argument.equals("containers") || argument.equals("chest") || argument.equals("transaction") || argument.equals("transactions")) {
result.add(LookupActions.CONTAINER);
}
@@ -83,7 +115,7 @@ public class ActionParser {
result.add(LookupActions.CONTAINER);
result.add(ItemTransactionActions.ADD);
}
else if (argument.equals("chat") || argument.equals("chats")) {
else if (argument.equals("chat") || argument.equals("chats") || argument.equals("message") || argument.equals("messages")) {
result.add(LookupActions.CHAT);
}
else if (argument.equals("command") || argument.equals("commands")) {
@@ -142,7 +174,26 @@ public class ActionParser {
}
count++;
}
return result;
return new ParseResult(result, entityActionFilter);
}
public static final class ParseResult {
private final List<Integer> actions;
private final EntityActionFilter entityActionFilter;
private ParseResult(List<Integer> actions, EntityActionFilter entityActionFilter) {
this.actions = actions;
this.entityActionFilter = entityActionFilter;
}
public List<Integer> getActions() {
return actions;
}
public EntityActionFilter getEntityActionFilter() {
return entityActionFilter;
}
}
/**
@@ -171,6 +222,23 @@ public class ActionParser {
return result;
}
/**
* Parse summary output flag from command arguments
*
* @param inputArguments
* The command arguments
* @return true if the summary flag is present
*/
public static boolean parseSummary(String[] inputArguments) {
for (int index = 1; index < inputArguments.length; index++) {
String argument = inputArguments[index].trim().toLowerCase(Locale.ROOT).replace("\\", "").replace("'", "");
if (argument.equals("#summary")) {
return true;
}
}
return false;
}
/**
* Parse noisy flag from command arguments
*
@@ -4,6 +4,7 @@ import java.util.Locale;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@@ -95,18 +96,21 @@ public class LocationParser {
}
}
if (cCount > 1) {
if (location == null && worldId > 0) {
if (worldId > 0) {
location = new Location(Bukkit.getWorld(WorldUtils.getWorldName(worldId)), 0, 0, 0);
}
if (location != null) {
int worldMaxHeight = location.getWorld().getMaxHeight() - 1;
int worldMinHeight = BukkitAdapter.ADAPTER.getMinHeight(location.getWorld());
World world = location.getWorld();
if (world != null) {
int worldMaxHeight = world.getMaxHeight() - 1;
int worldMinHeight = BukkitAdapter.ADAPTER.getMinHeight(world);
if (y < worldMinHeight) {
y = Double.valueOf(worldMinHeight);
}
if (y > worldMaxHeight) {
y = Double.valueOf(worldMaxHeight);
if (y < worldMinHeight) {
y = Double.valueOf(worldMinHeight);
}
if (y > worldMaxHeight) {
y = Double.valueOf(worldMaxHeight);
}
}
location.setX(x);
@@ -225,6 +225,7 @@ public class MaterialParser {
public static Map<String, Set<Material>> getTags() {
Map<String, Set<Material>> tagMap = new HashMap<>();
tagMap.put("#button", BlockGroup.BUTTONS);
tagMap.put("#sign", BlockGroup.SIGNS);
tagMap.put("#container", BlockGroup.CONTAINERS);
tagMap.put("#door", BlockGroup.DOORS);
tagMap.put("#natural", BlockGroup.NATURAL_BLOCKS);
@@ -0,0 +1,168 @@
package net.coreprotect.command.parser;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Set;
public final class MessageFilterParser {
public static final int MINIMUM_FILTER_CODE_POINTS = 3;
private static final List<String> VALUE_KEYS = List.of(
"a:",
"action:",
"b:",
"block:",
"blocks:",
"c:",
"coord:",
"coords:",
"cord:",
"cords:",
"coordinate:",
"coordinates:",
"cordinate:",
"cordinates:",
"e:",
"exclude:",
"f:",
"filter:",
"i:",
"include:",
"item:",
"items:",
"location:",
"p:",
"page:",
"position:",
"r:",
"radius:",
"rows:",
"t:",
"time:",
"u:",
"user:",
"users:"
);
private MessageFilterParser() {
throw new IllegalStateException("Parser class");
}
public static ParseResult parse(String[] inputArguments) {
if (inputArguments == null || inputArguments.length == 0) {
return new ParseResult(new String[0], Collections.emptyList(), false);
}
List<String> arguments = new ArrayList<>(inputArguments.length);
Set<String> filters = new LinkedHashSet<>();
boolean specified = false;
for (int index = 0; index < inputArguments.length; index++) {
String argument = Objects.toString(inputArguments[index], "").trim();
if (!isFilterParameter(argument)) {
arguments.add(argument);
continue;
}
specified = true;
StringBuilder collected = new StringBuilder(argument);
while (index + 1 < inputArguments.length && !isLookupTerminator(inputArguments[index + 1])) {
String next = Objects.toString(inputArguments[++index], "").trim();
if (!next.isEmpty()) {
collected.append(' ').append(next);
}
}
String merged = collected.toString();
arguments.add(merged);
addFilters(filters, value(merged));
}
return new ParseResult(arguments.toArray(new String[0]), new ArrayList<>(filters), specified);
}
public static boolean isLookupTerminator(String raw) {
String normalized = normalize(raw);
if (isControl(normalized)) {
return true;
}
for (String key : VALUE_KEYS) {
if (normalized.startsWith(key)) {
return true;
}
}
return false;
}
private static boolean isFilterParameter(String raw) {
String normalized = normalize(raw);
return normalized.equals("f:") || normalized.startsWith("f:") || normalized.equals("filter:") || normalized.startsWith("filter:");
}
private static boolean isControl(String normalized) {
return normalized.equals("#count") || normalized.equals("#sum") || normalized.equals("count") || normalized.equals("sum") || normalized.equals("#summary")
|| normalized.equals("n") || normalized.equals("noisy") || normalized.equals("v") || normalized.equals("verbose")
|| normalized.equals("#v") || normalized.equals("#verbose") || normalized.equals("#silent")
|| normalized.equals("#container") || normalized.equals("#preview") || normalized.equals("#preview_cancel") || normalized.equals("#preview-cancel")
|| RollbackStateParser.isModifier(normalized);
}
private static String normalize(String raw) {
return Objects.toString(raw, "").trim().toLowerCase(Locale.ROOT).replace("\\", "").replace("'", "");
}
private static String value(String argument) {
int separator = argument.indexOf(':');
return separator < 0 ? "" : argument.substring(separator + 1);
}
private static void addFilters(Set<String> filters, String value) {
for (String part : value.split(",", -1)) {
String filter = part.trim();
if (!filter.isEmpty()) {
filters.add(filter);
}
}
}
public static final class ParseResult {
private final String[] arguments;
private final List<String> filters;
private final boolean specified;
private ParseResult(String[] arguments, List<String> filters, boolean specified) {
this.arguments = arguments;
this.filters = Collections.unmodifiableList(filters);
this.specified = specified;
}
public String[] getArguments() {
return arguments.clone();
}
public List<String> getFilters() {
return filters;
}
public boolean isSpecified() {
return specified;
}
public boolean hasInvalidLength() {
if (filters.isEmpty()) {
return true;
}
for (String filter : filters) {
int start = filter.startsWith("-") ? 1 : 0;
if (filter.codePointCount(start, filter.length()) < MINIMUM_FILTER_CODE_POINTS) {
return true;
}
}
return false;
}
}
}
@@ -0,0 +1,71 @@
package net.coreprotect.command.parser;
import java.util.Locale;
import net.coreprotect.model.lookup.LookupRollbackState;
public final class RollbackStateParser {
private RollbackStateParser() {
throw new IllegalStateException("Parser class");
}
public static ParseResult parse(String[] inputArguments) {
boolean rolledBack = false;
boolean restored = false;
for (int index = 1; index < inputArguments.length; index++) {
String argument = normalize(inputArguments[index]);
if (isRolledBackModifier(argument)) {
rolledBack = true;
}
else if (isRestoredModifier(argument)) {
restored = true;
}
}
LookupRollbackState state = LookupRollbackState.ANY;
if (rolledBack) {
state = LookupRollbackState.ROLLED_BACK;
}
else if (restored) {
state = LookupRollbackState.NOT_ROLLED_BACK;
}
return new ParseResult(state, rolledBack && restored);
}
public static boolean isModifier(String argument) {
String normalized = normalize(argument);
return isRolledBackModifier(normalized) || isRestoredModifier(normalized);
}
private static boolean isRolledBackModifier(String argument) {
return argument.equals("#rolledback") || argument.equals("#rollbacked");
}
private static boolean isRestoredModifier(String argument) {
return argument.equals("#restored");
}
private static String normalize(String argument) {
return argument.trim().toLowerCase(Locale.ROOT).replace("\\", "").replace("'", "");
}
public static final class ParseResult {
private final LookupRollbackState state;
private final boolean conflicting;
private ParseResult(LookupRollbackState state, boolean conflicting) {
this.state = state;
this.conflicting = conflicting;
}
public LookupRollbackState getState() {
return state;
}
public boolean isConflicting() {
return conflicting;
}
}
}
@@ -146,7 +146,7 @@ public class UserParser {
* The user string to process
*/
private static void parseUser(List<String> users, String user) {
List<String> badUsers = Arrays.asList("n", "noisy", "v", "verbose", "#v", "#verbose", "#silent", "#preview", "#preview_cancel", "#count", "#sum");
List<String> badUsers = Arrays.asList("n", "noisy", "v", "verbose", "#v", "#verbose", "#silent", "#preview", "#preview_cancel", "#count", "#sum", "#summary", "#rolledback", "#rollbacked", "#restored");
String check = user.replaceAll("[\\s'\"]", "");
if (check.equals(user) && check.length() > 0) {
if (user.equalsIgnoreCase("#global")) {
@@ -45,7 +45,8 @@ public class WorldParser {
else if (inputProcessed.startsWith("#")) {
world_id = WorldUtils.matchWorld(inputProcessed);
if (world_id == -1 && !requireLoaded) {
world_id = ConfigHandler.worlds.getOrDefault(argument.replaceFirst("#", ""), -1);
String worldName = argument.replaceFirst("#", "");
world_id = ConfigHandler.resolveIdentifierId(ConfigHandler.CacheType.WORLDS, worldName, false);
}
}
next = 0;
@@ -12,12 +12,16 @@ import java.nio.file.Files;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;
import org.bukkit.Bukkit;
import org.bukkit.World;
import net.coreprotect.CoreProtect;
import net.coreprotect.consumer.Consumer;
import net.coreprotect.language.Language;
import net.coreprotect.thread.Scheduler;
import net.coreprotect.utility.VersionUtils;
@@ -26,8 +30,9 @@ public class Config extends Language {
private static final Map<String, String[]> HEADERS = new HashMap<>();
private static final Map<String, String> DEFAULT_VALUES = new LinkedHashMap<>();
private static final Map<String, Config> CONFIG_BY_WORLD_NAME = new HashMap<>();
private static final Map<String, Config> CONFIG_BY_WORLD_NAME = new ConcurrentHashMap<>();
private static final String DEFAULT_FILE_HEADER = "# CoreProtect Config";
private static final Pattern NON_DIGIT_PATTERN = Pattern.compile("[^0-9]");
public static final String LINE_SEPARATOR = "\n";
private static final Config GLOBAL = new Config();
@@ -35,6 +40,14 @@ public class Config extends Language {
private Config defaults;
public String DONATION_KEY;
public String DATABASE_TYPE;
public String CLICKHOUSE_HOST;
public String CLICKHOUSE_DATABASE;
public String CLICKHOUSE_USERNAME;
public String CLICKHOUSE_PASSWORD;
public String DUCKDB_MEMORY_LIMIT;
public String DUCKDB_MAX_TEMP_DIRECTORY_SIZE;
public String SQLITE_DATABASE;
public String PREFIX;
public String MYSQL_HOST;
public String MYSQL_DATABASE;
@@ -44,6 +57,7 @@ public class Config extends Language {
public String AUTO_PURGE;
public String AUTO_PURGE_TIME;
public boolean ENABLE_SSL;
public boolean CLICKHOUSE_TLS;
public boolean DISABLE_WAL;
public boolean HOVER_EVENTS;
public boolean DATABASE_LOCK;
@@ -65,12 +79,14 @@ public class Config extends Language {
public boolean NATURAL_BREAK;
public boolean BLOCK_MOVEMENT;
public boolean PISTONS;
public boolean DISPENSERS;
public boolean BLOCK_BURN;
public boolean BLOCK_IGNITE;
public boolean FIRE_EXTINGUISH;
public boolean EXPLOSIONS;
public boolean ENTITY_CHANGE;
public boolean ENTITY_KILLS;
public boolean ENTITY_SPAWNS;
public boolean SIGN_TEXT;
public boolean BUCKETS;
public boolean LEAF_DECAY;
@@ -94,19 +110,31 @@ public class Config extends Language {
public boolean USERNAME_CHANGES;
public boolean WORLDEDIT;
public int MAXIMUM_POOL_SIZE;
public int CLICKHOUSE_PORT;
public int CLICKHOUSE_CONSUMER_DELAY;
public int MYSQL_PORT;
public int DEFAULT_RADIUS;
public int DUCKDB_THREADS;
public int MAX_RADIUS;
static {
DEFAULT_VALUES.put("donation-key", "");
DEFAULT_VALUES.put("use-mysql", "false");
DEFAULT_VALUES.put("database-type", "duckdb");
DEFAULT_VALUES.put("table-prefix", "co_");
DEFAULT_VALUES.put("mysql-host", "127.0.0.1");
DEFAULT_VALUES.put("mysql-port", "3306");
DEFAULT_VALUES.put("mysql-database", "database");
DEFAULT_VALUES.put("mysql-username", "root");
DEFAULT_VALUES.put("mysql-password", "");
DEFAULT_VALUES.put("clickhouse-host", "127.0.0.1");
DEFAULT_VALUES.put("clickhouse-port", "8123");
DEFAULT_VALUES.put("clickhouse-database", "default");
DEFAULT_VALUES.put("clickhouse-username", "default");
DEFAULT_VALUES.put("clickhouse-password", "");
DEFAULT_VALUES.put("clickhouse-tls", "false");
DEFAULT_VALUES.put("duckdb-memory-limit", "512MB");
DEFAULT_VALUES.put("duckdb-threads", "3");
DEFAULT_VALUES.put("duckdb-max-temp-directory-size", "10GB");
DEFAULT_VALUES.put("language", "en");
DEFAULT_VALUES.put("auto-purge", "false");
DEFAULT_VALUES.put("check-updates", "true");
@@ -123,12 +151,14 @@ public class Config extends Language {
DEFAULT_VALUES.put("natural-break", "true");
DEFAULT_VALUES.put("block-movement", "true");
DEFAULT_VALUES.put("pistons", "true");
DEFAULT_VALUES.put("dispensers", "true");
DEFAULT_VALUES.put("block-burn", "true");
DEFAULT_VALUES.put("block-ignite", "true");
DEFAULT_VALUES.put("fire-extinguish", "false");
DEFAULT_VALUES.put("explosions", "true");
DEFAULT_VALUES.put("entity-change", "true");
DEFAULT_VALUES.put("entity-kills", "true");
DEFAULT_VALUES.put("entity-spawns", "true");
DEFAULT_VALUES.put("sign-text", "true");
DEFAULT_VALUES.put("buckets", "true");
DEFAULT_VALUES.put("leaf-decay", "true");
@@ -152,7 +182,10 @@ public class Config extends Language {
DEFAULT_VALUES.put("worldedit", "true");
HEADERS.put("donation-key", new String[] { "# CoreProtect is donationware. Obtain a donation key from coreprotect.net/donate/" });
HEADERS.put("use-mysql", new String[] { "# MySQL is optional and not required.", "# If you prefer to use MySQL, enable the following and fill out the fields." });
HEADERS.put("database-type", new String[] { "# Database engine used by CoreProtect. Valid values are duckdb, clickhouse, sqlite, and mysql.", "# Run /co reload or restart the server after changing the database engine or connection target." });
HEADERS.put("mysql-host", new String[] { "# Connection settings for MySQL." });
HEADERS.put("clickhouse-host", new String[] { "# Connection settings for ClickHouse 26.1 or newer.", "# The configured database must already exist; CoreProtect creates its prefixed tables and views.", "# Multiple writers require database-lock disabled, the same version and prefix, separate data directories, and direct connections to one physical server.", "# Keep writer clocks synchronized; a shared prefix is one logical namespace for worlds and players.", "# Do not reassign usernames, and record UUID-bearing logins before UUID-less activity under a changed name.", "# Stop every writer before migration or purge; purge requires database-lock on the remaining server.", "# Replicated, distributed, and load-balanced independent ClickHouse nodes are unsupported." });
HEADERS.put("duckdb-memory-limit", new String[] { "# Resource limits for the embedded DuckDB database.", "# The memory limit controls DuckDB's buffer manager; the temporary limit caps spill data and is not preallocated." });
HEADERS.put("language", new String[] { "# If modified, will automatically attempt to translate languages phrases.", "# List of language codes: https://coreprotect.net/languages/" });
HEADERS.put("auto-purge", new String[] { "# Automatically purge data older than the configured time.", "# Examples: 30d, 12w, 6mo. Set to false to disable." });
HEADERS.put("check-updates", new String[] { "# If enabled, CoreProtect will check for updates when your server starts up.", "# If an update is available, you'll be notified via your server console.", });
@@ -162,19 +195,21 @@ public class Config extends Language {
HEADERS.put("default-radius", new String[] { "# If no radius is specified in a rollback or restore, this value will be", "# used as the radius. Set to \"0\" to disable automatically adding a radius." });
HEADERS.put("max-radius", new String[] { "# The maximum radius that can be used in a command. Set to \"0\" to disable.", "# To run a rollback or restore without a radius, you can use \"r:#global\"." });
HEADERS.put("rollback-items", new String[] { "# If enabled, items taken from containers (etc) will be included in rollbacks." });
HEADERS.put("rollback-entities", new String[] { "# If enabled, entities, such as killed animals, will be included in rollbacks." });
HEADERS.put("rollback-entities", new String[] { "# If enabled, entity kills and player-attributed entity spawns will be included in rollbacks." });
HEADERS.put("skip-generic-data", new String[] { "# If enabled, generic data, like zombies burning in daylight, won't be logged." });
HEADERS.put("block-place", new String[] { "# Logs blocks placed by players." });
HEADERS.put("block-break", new String[] { "# Logs blocks broken by players." });
HEADERS.put("natural-break", new String[] { "# Logs blocks that break off of other blocks; for example, a sign or torch", "# falling off of a dirt block that a player breaks. This is required for", "# beds/doors to properly rollback." });
HEADERS.put("block-movement", new String[] { "# Properly track block movement, such as sand or gravel falling." });
HEADERS.put("pistons", new String[] { "# Properly track blocks moved by pistons." });
HEADERS.put("dispensers", new String[] { "# Logs block placement and removal events caused by dispensers." });
HEADERS.put("block-burn", new String[] { "# Logs blocks that burn up in a fire." });
HEADERS.put("block-ignite", new String[] { "# Logs when a block naturally ignites, such as from fire spreading." });
HEADERS.put("fire-extinguish", new String[] { "# Logs when fire naturally extinguishes." });
HEADERS.put("explosions", new String[] { "# Logs explosions, such as TNT and Creepers." });
HEADERS.put("entity-change", new String[] { "# Track when an entity changes a block, such as an Enderman destroying blocks." });
HEADERS.put("entity-kills", new String[] { "# Logs killed entities, such as killed cows and enderman." });
HEADERS.put("entity-spawns", new String[] { "# Logs entities placed or spawned by players, such as boats and spawn-egg mobs." });
HEADERS.put("sign-text", new String[] { "# Logs text on signs. If disabled, signs will be blank when rolled back." });
HEADERS.put("buckets", new String[] { "# Logs lava and water sources placed/removed by players who are using buckets." });
HEADERS.put("leaf-decay", new String[] { "# Logs natural tree leaf decay." });
@@ -211,6 +246,15 @@ public class Config extends Language {
this.UNKNOWN_LOGGING = this.getBoolean("unknown-logging", false);
this.MAXIMUM_POOL_SIZE = this.getInt("maximum-pool-size", 10);
this.DONATION_KEY = this.getString("donation-key");
this.DATABASE_TYPE = this.getString("database-type");
this.CLICKHOUSE_HOST = this.getString("clickhouse-host");
this.CLICKHOUSE_PORT = this.getInt("clickhouse-port");
this.CLICKHOUSE_CONSUMER_DELAY = Math.max(500,
this.getInt("clickhouse-consumer-delay", 2500));
this.CLICKHOUSE_DATABASE = this.getString("clickhouse-database");
this.CLICKHOUSE_USERNAME = this.getString("clickhouse-username");
this.CLICKHOUSE_PASSWORD = this.getString("clickhouse-password");
this.CLICKHOUSE_TLS = this.getBoolean("clickhouse-tls");
this.MYSQL = this.getBoolean("use-mysql");
this.PREFIX = this.getString("table-prefix");
this.MYSQL_HOST = this.getString("mysql-host");
@@ -218,6 +262,10 @@ public class Config extends Language {
this.MYSQL_DATABASE = this.getString("mysql-database");
this.MYSQL_USERNAME = this.getString("mysql-username");
this.MYSQL_PASSWORD = this.getString("mysql-password");
this.DUCKDB_MEMORY_LIMIT = this.getString("duckdb-memory-limit");
this.DUCKDB_THREADS = this.getInt("duckdb-threads", 3);
this.DUCKDB_MAX_TEMP_DIRECTORY_SIZE = this.getString("duckdb-max-temp-directory-size");
this.SQLITE_DATABASE = this.getString("sqlite-database");
this.LANGUAGE = this.getString("language");
this.AUTO_PURGE = this.getString("auto-purge");
this.AUTO_PURGE_TIME = this.getString("auto-purge-time");
@@ -235,12 +283,14 @@ public class Config extends Language {
this.NATURAL_BREAK = this.getBoolean("natural-break");
this.BLOCK_MOVEMENT = this.getBoolean("block-movement");
this.PISTONS = this.getBoolean("pistons");
this.DISPENSERS = this.getBoolean("dispensers");
this.BLOCK_BURN = this.getBoolean("block-burn");
this.BLOCK_IGNITE = this.getBoolean("block-ignite");
this.FIRE_EXTINGUISH = this.getBoolean("fire-extinguish");
this.EXPLOSIONS = this.getBoolean("explosions");
this.ENTITY_CHANGE = this.getBoolean("entity-change");
this.ENTITY_KILLS = this.getBoolean("entity-kills");
this.ENTITY_SPAWNS = this.getBoolean("entity-spawns");
this.SIGN_TEXT = this.getBoolean("sign-text");
this.BUCKETS = this.getBoolean("buckets");
this.LEAF_DECAY = this.getBoolean("leaf-decay");
@@ -279,12 +329,7 @@ public class Config extends Language {
}
public static Config getConfig(final String worldName) {
Config ret = CONFIG_BY_WORLD_NAME.get(worldName);
if (ret == null) {
ret = CONFIG_BY_WORLD_NAME.getOrDefault(worldName, GLOBAL);
CONFIG_BY_WORLD_NAME.put(worldName, ret);
}
return ret;
return CONFIG_BY_WORLD_NAME.getOrDefault(worldName, GLOBAL);
}
public Config() {
@@ -332,7 +377,7 @@ public class Config extends Language {
return dfl;
}
configured = configured.replaceAll("[^0-9]", "");
configured = NON_DIGIT_PATTERN.matcher(configured).replaceAll("");
return configured.isEmpty() ? dfl : Integer.parseInt(configured);
}
@@ -346,6 +391,10 @@ public class Config extends Language {
this.config.clear();
}
public boolean hasOption(String key) {
return this.config.containsKey(key);
}
public void loadDefaults() {
this.clearConfig();
this.readValues();
@@ -386,6 +435,7 @@ public class Config extends Language {
final Config temp = new Config();
temp.loadDefaults();
temp.addMissingOptions(globalFile);
map.put("config", Files.readAllBytes(globalFile.toPath()));
}
for (final File worldConfigFile : configFolder.listFiles((File file) -> file.getName().endsWith(".yml"))) {
@@ -406,8 +456,12 @@ public class Config extends Language {
// we call reloads asynchronously
// for now this solution is good enough to ensure we only modify on the main thread
final CompletableFuture<Void> complete = new CompletableFuture<>();
final CompletableFuture<Void> shutdown = Consumer.databaseReloadShutdownSignal();
Scheduler.runTask(CoreProtect.getInstance(), () -> {
if (shutdown.isDone()) {
return;
}
try {
parseConfig(data);
}
@@ -421,12 +475,15 @@ public class Config extends Language {
complete.complete(null);
});
CompletableFuture.anyOf(complete, shutdown).join();
if (!complete.isDone()) {
complete.cancel(false);
throw new CancellationException("Configuration reload cancelled for shutdown");
}
complete.join();
return;
}
CONFIG_BY_WORLD_NAME.clear();
// we need to load global first since it is used for config defaults
final byte[] defaultData = data.get("config");
if (defaultData != null) {
@@ -441,6 +498,7 @@ public class Config extends Language {
GLOBAL.loadDefaults();
}
final Map<String, Config> worldConfigs = new HashMap<>();
for (final Map.Entry<String, byte[]> entry : data.entrySet()) {
final String worldName = entry.getKey();
if (worldName.equals("config")) {
@@ -458,8 +516,12 @@ public class Config extends Language {
throw new RuntimeException(ex); // shouldn't happen
}
CONFIG_BY_WORLD_NAME.put(worldName, config);
worldConfigs.put(worldName, config);
}
// Replace entries instead of clearing first, so a configured world never reads the global config mid reload
CONFIG_BY_WORLD_NAME.putAll(worldConfigs);
CONFIG_BY_WORLD_NAME.keySet().retainAll(worldConfigs.keySet());
}
public void addMissingOptions(final File file) throws IOException {
@@ -473,7 +535,7 @@ public class Config extends Language {
for (final Map.Entry<String, String> entry : DEFAULT_VALUES.entrySet()) {
final String key = entry.getKey();
final String defaultValue = entry.getValue();
String defaultValue = entry.getValue();
final String configuredValue = this.config.get(key);
@@ -483,6 +545,9 @@ public class Config extends Language {
if (key.equals("auto-purge") && VersionUtils.isCommunityEdition()) {
continue;
}
if (key.equals("database-type") && !writeHeader) {
defaultValue = this.getBoolean("use-mysql") ? "mysql" : "sqlite";
}
final String[] header = HEADERS.get(key);
@@ -4,20 +4,28 @@ import java.io.File;
import java.io.RandomAccessFile;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.IntConsumer;
import java.util.function.IntSupplier;
import java.util.function.Supplier;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
@@ -28,16 +36,28 @@ import com.zaxxer.hikari.HikariDataSource;
import net.coreprotect.bukkit.BukkitAdapter;
import net.coreprotect.consumer.Queue;
import net.coreprotect.consumer.process.Process;
import net.coreprotect.database.ConsumerWriteBatch;
import net.coreprotect.database.ConsumerWriteBatch.ReferenceKind;
import net.coreprotect.database.Database;
import net.coreprotect.database.DatabaseType;
import net.coreprotect.database.DuckDBNativeSupport;
import net.coreprotect.database.clickhouse.ClickHouseDatabase;
import net.coreprotect.database.clickhouse.ClickHouseJdbcConfig;
import net.coreprotect.database.statement.UserStatement;
import net.coreprotect.language.Phrase;
import net.coreprotect.listener.ListenerHandler;
import net.coreprotect.model.BlockGroup;
import net.coreprotect.model.action.EntityActionFilter;
import net.coreprotect.model.lookup.LookupCursor;
import net.coreprotect.model.lookup.LookupOutputMode;
import net.coreprotect.model.lookup.LookupRollbackState;
import net.coreprotect.paper.PaperAdapter;
import net.coreprotect.patch.Patch;
import net.coreprotect.spigot.SpigotAdapter;
import net.coreprotect.utility.Chat;
import net.coreprotect.utility.Color;
import net.coreprotect.utility.EntitySpawnTracking;
import net.coreprotect.utility.ErrorReporter;
import net.coreprotect.utility.SystemUtils;
import net.coreprotect.utility.VersionUtils;
@@ -45,7 +65,27 @@ import net.coreprotect.utility.VersionUtils;
public class ConfigHandler extends Queue {
public enum CacheType {
MATERIALS, BLOCKDATA, ART, ENTITIES, WORLDS
MATERIALS(
ReferenceKind.MATERIAL
),
BLOCKDATA(
ReferenceKind.BLOCK_DATA
),
ART(
ReferenceKind.ART
),
ENTITIES(
ReferenceKind.ENTITY
),
WORLDS(
ReferenceKind.WORLD
);
private final ReferenceKind kind;
CacheType(ReferenceKind kind) {
this.kind = kind;
}
}
public static int SERVER_VERSION = 0;
@@ -55,10 +95,15 @@ public class ConfigHandler extends Queue {
public static final String COMMUNITY_EDITION = "Community Edition";
public static final String JAVA_VERSION = "11.0";
public static final String MINECRAFT_VERSION = "1.16.5";
public static final String PATCH_VERSION = "24.0";
public static final String LATEST_VERSION = "26.2";
public static final String PATCH_VERSION = "24.1";
public static final String LATEST_VERSION = "26.3";
private static final String DEFAULT_SQLITE_DATABASE = "database.db";
public static String path = "plugins/CoreProtect/";
public static String sqlite = "database.db";
public static String sqlite = DEFAULT_SQLITE_DATABASE;
public static String duckdb = "database.duckdb";
public static String duckdbMemoryLimit = "512MB";
public static String duckdbMaxTempDirectorySize = "10GB";
public static int duckdbThreads = 3;
public static String host = "127.0.0.1";
public static int port = 3306;
public static String database = "database";
@@ -67,6 +112,15 @@ public class ConfigHandler extends Queue {
public static String prefix = "co_";
public static String prefixConfig = "co_";
public static int maximumPoolSize = 10;
public static DatabaseType databaseType = DatabaseType.SQLITE;
public static String getDescendingEventOrder() {
return databaseType.isClickHouse() ? "time DESC,rowid DESC" : "rowid DESC";
}
public static String getAscendingEventOrder() {
return databaseType.isClickHouse() ? "time ASC,rowid ASC" : "rowid ASC";
}
public static final String BLACKLIST_COMMENT_SEPARATOR = ";";
public static final String BLACKLIST_FILTER_SEPARATOR = "@";
@@ -78,6 +132,7 @@ public class ConfigHandler extends Queue {
public static final boolean isPaper = VersionUtils.isPaper();
public static final boolean isFolia = VersionUtils.isFolia();
public static volatile boolean serverRunning = false;
public static volatile boolean shutdownDrainRunning = false;
public static volatile boolean converterRunning = false;
public static volatile boolean purgeRunning = false;
public static volatile boolean migrationRunning = false;
@@ -90,21 +145,99 @@ public class ConfigHandler extends Queue {
public static volatile int entityId = 0;
public static volatile int artId = 0;
public static final AtomicLong autoPurgeRowsPurged = new AtomicLong(0);
private static boolean duckDBFallbackAllowed = false;
private static <K, V> Map<K, V> syncMap() {
return Collections.synchronizedMap(new HashMap<>());
}
public static Map<String, Integer> worlds = syncMap();
public static Map<Integer, String> worldsReversed = syncMap();
public static Map<String, Integer> materials = syncMap();
public static Map<Integer, String> materialsReversed = syncMap();
public static Map<String, Integer> blockdata = syncMap();
public static Map<Integer, String> blockdataReversed = syncMap();
public static Map<String, Integer> entities = syncMap();
public static Map<Integer, String> entitiesReversed = syncMap();
public static Map<String, Integer> art = syncMap();
public static Map<Integer, String> artReversed = syncMap();
private static <K, V> Map<K, V> syncMap(Map<K, V> values) {
return Collections.synchronizedMap(new HashMap<>(values));
}
private static IdentifierCache loadIdentifierCache(Statement statement, String table, String valueColumn) throws SQLException {
Map<String, Integer> values = new HashMap<>();
Map<Integer, String> reversed = new HashMap<>();
int maximumId = 0;
try (ResultSet resultSet = statement.executeQuery("SELECT id," + valueColumn + " FROM " + ConfigHandler.prefix + table)) {
while (resultSet.next()) {
int id = resultSet.getInt("id");
String value = resultSet.getString(valueColumn);
values.put(value, id);
reversed.put(id, value);
maximumId = Math.max(maximumId, id);
}
}
return new IdentifierCache(values, reversed, maximumId);
}
private static final class IdentifierCache {
private final Map<String, Integer> values;
private final Map<Integer, String> reversed;
private int maximumId;
private IdentifierCache(Map<String, Integer> values, Map<Integer, String> reversed, int maximumId) {
this.values = values;
this.reversed = reversed;
this.maximumId = maximumId;
}
}
private static final class IdentifierStore {
private final ReferenceKind kind;
private final Supplier<Map<String, Integer>> values;
private final Supplier<Map<Integer, String>> reversed;
private final IntSupplier maximum;
private final IntConsumer maximumSetter;
private IdentifierStore(ReferenceKind kind, Supplier<Map<String, Integer>> values, Supplier<Map<Integer, String>> reversed, IntSupplier maximum, IntConsumer maximumSetter) {
this.kind = kind;
this.values = values;
this.reversed = reversed;
this.maximum = maximum;
this.maximumSetter = maximumSetter;
}
private boolean store(int id, String value) {
if (!canStore(id, value)) {
return false;
}
Map<String, Integer> names = values.get();
Map<Integer, String> identifiers = reversed.get();
names.put(value, id);
identifiers.put(id, value);
maximumSetter.accept(Math.max(maximum.getAsInt(), id));
return true;
}
private boolean canStore(int id, String value) {
if (id < 1) {
return false;
}
Map<String, Integer> names = values.get();
Map<Integer, String> identifiers = reversed.get();
Integer storedId = names.get(value);
String storedValue = identifiers.get(id);
if ((storedId != null && storedId != id) || (storedValue != null && !storedValue.equals(value))) {
throw new IllegalStateException("CoreProtect " + kind.name().toLowerCase(Locale.ROOT) + " identifier " + id + "='" + value + "' conflicts with cached " + storedId + "='" + storedValue + "'");
}
return true;
}
}
public static volatile Map<String, Integer> worlds = syncMap();
public static volatile Map<Integer, String> worldsReversed = syncMap();
public static volatile Map<String, Integer> materials = syncMap();
public static volatile Map<Integer, String> materialsReversed = syncMap();
public static volatile Map<String, Integer> blockdata = syncMap();
public static volatile Map<Integer, String> blockdataReversed = syncMap();
public static volatile Map<String, Integer> entities = syncMap();
public static volatile Map<Integer, String> entitiesReversed = syncMap();
public static volatile Map<String, Integer> art = syncMap();
public static volatile Map<Integer, String> artReversed = syncMap();
private static final Map<ReferenceKind, IdentifierStore> IDENTIFIER_STORES = identifierStores();
public static Map<String, int[]> rollbackHash = syncMap();
public static Map<String, Boolean> inspecting = syncMap();
public static Map<String, Boolean> blacklist = syncMap();
@@ -131,15 +264,22 @@ public class ConfigHandler extends Queue {
public static Map<String, Object[]> lookupThrottle = syncMap();
public static Map<String, Object[]> teleportThrottle = syncMap();
public static Map<String, Integer> lookupPage = syncMap();
public static Map<String, LookupOutputMode> lookupOutputMode = syncMap();
public static Map<String, LookupRollbackState> lookupRollbackState = syncMap();
public static Map<String, String> lookupCommand = syncMap();
public static Map<String, Integer> lookupEntityContainer = syncMap();
public static Map<String, Integer> lookupEntityInteraction = syncMap();
public static Map<String, List<Object>> lookupBlist = syncMap();
public static Map<String, Map<Object, Boolean>> lookupElist = syncMap();
public static Map<String, List<String>> lookupEUserlist = syncMap();
public static Map<String, List<String>> lookupUlist = syncMap();
public static Map<String, List<Integer>> lookupAlist = syncMap();
public static Map<String, EntityActionFilter> lookupEntityActionFilter = syncMap();
public static Map<String, List<String>> lookupFlist = syncMap();
public static Map<String, Integer[]> lookupRadius = syncMap();
public static Map<String, String> lookupTime = syncMap();
public static Map<String, Long[]> lookupRows = syncMap();
public static Map<String, LookupCursor> lookupCursor = syncMap();
public static Map<String, String> uuidCache = syncMap();
public static Map<String, String> uuidCacheReversed = syncMap();
public static Map<String, Integer> playerIdCache = syncMap();
@@ -147,13 +287,32 @@ public class ConfigHandler extends Queue {
public static Map<String, List<Object>> lastRollback = syncMap();
public static Map<String, Boolean> activeRollbacks = syncMap();
public static Map<String, Object[]> entityBlockMapper = new ConcurrentHashMap<>();
public static ConcurrentHashMap<Long, Long> populatedChunks = new ConcurrentHashMap<>();
public static ConcurrentHashMap<UUID, ConcurrentHashMap<Long, Long>> populatedChunks = new ConcurrentHashMap<>();
public static ConcurrentHashMap<String, String> language = new ConcurrentHashMap<>();
public static List<String> databaseTables = new ArrayList<>();
public static void checkPlayers(Connection connection) {
ConfigHandler.playerIdCache.clear();
ConfigHandler.playerIdCacheReversed.clear();
if (ConfigHandler.databaseType.isClickHouse()) {
if (Bukkit.getServer().getOnlinePlayers().isEmpty()) {
return;
}
try (ConsumerWriteBatch batch = Database.openConsumerWriteBatch(connection)) {
batch.begin();
int time = (int) (System.currentTimeMillis() / 1_000L);
for (Player player : Bukkit.getServer().getOnlinePlayers()) {
batch.recordUsername(player.getName(), player.getUniqueId().toString(), 0, time);
}
if (!batch.commit()) {
throw new IllegalStateException("Unable to initialize ClickHouse player records");
}
}
catch (Exception exception) {
throw new IllegalStateException("Unable to initialize ClickHouse player records", exception);
}
return;
}
for (Player player : Bukkit.getServer().getOnlinePlayers()) {
if (ConfigHandler.playerIdCache.get(player.getName().toLowerCase(Locale.ROOT)) == null) {
UserStatement.loadId(connection, player.getName(), player.getUniqueId().toString());
@@ -231,38 +390,105 @@ public class ConfigHandler extends Queue {
}
}
private static void loadConfig() {
try {
Config.init();
ConfigFile.init(ConfigFile.LANGUAGE); // load user phrases
ConfigFile.init(ConfigFile.LANGUAGE_CACHE); // load translation cache
private static void loadConfig() throws Exception {
File configFolder = new File(ConfigHandler.path);
File configFile = new File(configFolder, ConfigFile.CONFIG);
File[] existingFiles = configFolder.listFiles();
duckDBFallbackAllowed = !configFile.exists() && (!configFolder.exists() || (existingFiles != null && existingFiles.length == 0));
// Enforce "co_" table prefix if using SQLite.
if (!Config.getGlobal().MYSQL) {
ConfigHandler.prefixConfig = Config.getGlobal().PREFIX;
Config.getGlobal().PREFIX = "co_";
Config.init();
ConfigFile.init(ConfigFile.LANGUAGE); // load user phrases
ConfigFile.init(ConfigFile.LANGUAGE_CACHE); // load translation cache
Config global = Config.getGlobal();
if (global.hasOption("database-type")) {
ConfigHandler.databaseType = DatabaseType.parse(global.DATABASE_TYPE, global.MYSQL);
if (global.hasOption("use-mysql") && global.MYSQL != ConfigHandler.databaseType.isMySQL()) {
Chat.sendConsoleMessage(Color.YELLOW + "[CoreProtect] " + Phrase.build(Phrase.DATABASE_TYPE_OVERRIDE));
}
ConfigHandler.host = Config.getGlobal().MYSQL_HOST;
ConfigHandler.port = Config.getGlobal().MYSQL_PORT;
ConfigHandler.database = Config.getGlobal().MYSQL_DATABASE;
ConfigHandler.username = Config.getGlobal().MYSQL_USERNAME;
ConfigHandler.password = Config.getGlobal().MYSQL_PASSWORD;
ConfigHandler.maximumPoolSize = Config.getGlobal().MAXIMUM_POOL_SIZE;
ConfigHandler.prefix = Config.getGlobal().PREFIX;
ConfigHandler.loadBlacklist(); // Load the blacklist file if it exists.
}
catch (Exception e) {
ErrorReporter.report(e);
else {
ConfigHandler.databaseType = global.MYSQL ? DatabaseType.MYSQL : DatabaseType.SQLITE;
}
global.MYSQL = ConfigHandler.databaseType.isMySQL();
ConfigHandler.prefixConfig = global.PREFIX;
// Embedded databases use the fixed prefix expected by maintenance operations.
if (ConfigHandler.databaseType.isEmbedded()) {
global.PREFIX = "co_";
}
ConfigHandler.host = global.MYSQL_HOST;
ConfigHandler.port = global.MYSQL_PORT;
ConfigHandler.database = global.MYSQL_DATABASE;
ConfigHandler.username = global.MYSQL_USERNAME;
ConfigHandler.password = global.MYSQL_PASSWORD;
ConfigHandler.maximumPoolSize = global.MAXIMUM_POOL_SIZE;
ConfigHandler.duckdbMemoryLimit = global.DUCKDB_MEMORY_LIMIT;
ConfigHandler.duckdbThreads = Math.max(1, global.DUCKDB_THREADS);
ConfigHandler.duckdbMaxTempDirectorySize = global.DUCKDB_MAX_TEMP_DIRECTORY_SIZE;
ConfigHandler.prefix = global.PREFIX;
// Optional custom SQLite database filename (hidden option) inside the CoreProtect data folder.
String sqliteDatabase = global.SQLITE_DATABASE.trim();
ConfigHandler.sqlite = sqliteDatabase.isEmpty() ? DEFAULT_SQLITE_DATABASE : sqliteDatabase;
ConfigHandler.loadBlacklist(); // Load the blacklist file if it exists.
}
public static void loadDatabase() {
// close old pool when we reload the database, e.g. in purge command
Database.closeConnection();
loadDatabase(null, false);
}
if (!Config.getGlobal().MYSQL) {
public static void loadMigrationDatabase(ClickHouseDatabase preparedClickHouse) {
if (preparedClickHouse == null) {
throw new IllegalArgumentException("Prepared ClickHouse database cannot be null");
}
loadDatabase(preparedClickHouse, true);
}
private static void loadDatabase(ClickHouseDatabase preparedClickHouse, boolean migrationActivation) {
if (preparedClickHouse != null && !ConfigHandler.databaseType.isClickHouse()) {
throw new IllegalArgumentException("A prepared ClickHouse database can only activate a ClickHouse target");
}
ConfigHandler.databaseReachable = false;
Database.closeConnection();
try {
if (preparedClickHouse != null) {
Database.installClickHouseDatabase(preparedClickHouse);
}
else {
initializeDatabase();
}
if (!migrationActivation && Database.hasIncompleteMigrationMarker()) {
throw new IllegalStateException("Database contains an incomplete CoreProtect migration and cannot be activated");
}
ConfigHandler.databaseReachable = true;
}
catch (Exception exception) {
Database.closeConnection();
ConfigHandler.databaseReachable = false;
if (exception instanceof RuntimeException) {
throw (RuntimeException) exception;
}
throw new IllegalStateException("Failed to initialize database", exception);
}
}
private static void initializeDatabase() {
if (ConfigHandler.databaseType.isClickHouse()) {
try {
Config global = Config.getGlobal();
ClickHouseJdbcConfig config = new ClickHouseJdbcConfig(global.CLICKHOUSE_HOST, global.CLICKHOUSE_PORT, global.CLICKHOUSE_DATABASE, global.CLICKHOUSE_USERNAME, global.CLICKHOUSE_PASSWORD, global.CLICKHOUSE_TLS);
Database.initializeClickHouse(config, ConfigHandler.prefix);
}
catch (Exception exception) {
throw new IllegalStateException("Failed to initialize ClickHouse", exception);
}
return;
}
if (ConfigHandler.databaseType.isEmbedded()) {
try {
File tempFile = File.createTempFile("CoreProtect_" + System.currentTimeMillis(), ".tmp");
tempFile.setExecutable(true);
@@ -286,10 +512,30 @@ public class ConfigHandler extends Queue {
tempFile.delete();
Class.forName("org.sqlite.JDBC");
if (ConfigHandler.databaseType.isDuckDB() && duckDBFallbackAllowed) {
try {
DuckDBNativeSupport.verifyAvailable();
}
catch (Throwable failure) {
if (!DuckDBNativeSupport.isNativeUnavailable(failure)) {
throw new IllegalStateException("Unable to verify DuckDB on this system. For native-library extraction or loading errors, check free space and permissions in the JVM temporary directory. " + "To select another directory, add -Djava.io.tmpdir=/absolute/path/to/cache before -jar and fully restart the server. " + "The directory must already exist, have sufficient space, and permit writing and native-library loading.", failure);
}
DatabaseConfigWriter.persistDatabaseType(DatabaseType.SQLITE);
ConfigHandler.databaseType = DatabaseType.SQLITE;
Config global = Config.getGlobal();
global.DATABASE_TYPE = DatabaseType.SQLITE.name().toLowerCase(Locale.ROOT);
global.MYSQL = false;
Chat.sendConsoleMessage(Color.YELLOW + "[CoreProtect] " + Phrase.build(Phrase.DATABASE_FALLBACK, DatabaseType.DUCKDB.getDisplayName(), DatabaseType.SQLITE.getDisplayName()));
}
finally {
duckDBFallbackAllowed = false;
}
}
Class.forName(ConfigHandler.databaseType.isDuckDB() ? "org.duckdb.DuckDBDriver" : "org.sqlite.JDBC");
}
catch (Exception e) {
ErrorReporter.report(e);
throw new IllegalStateException("Failed to initialize " + ConfigHandler.databaseType.getDisplayName(), e);
}
}
else {
@@ -306,11 +552,17 @@ public class ConfigHandler extends Queue {
config.setUsername(ConfigHandler.username);
config.setPassword(ConfigHandler.password);
config.setMaximumPoolSize(ConfigHandler.maximumPoolSize);
config.setConnectionTimeout(10000);
// Keep connection age below short limits used by some managed MySQL hosts.
config.setMaxLifetime(60000);
config.setKeepaliveTime(0);
config.addDataSourceProperty("characterEncoding", "UTF-8");
config.addDataSourceProperty("connectionTimeout", "10000");
config.addDataSourceProperty("connectTimeout", "10000");
/* https://github.com/brettwooldridge/HikariCP/wiki/MySQL-Configuration */
/* https://cdn.oreillystatic.com/en/assets/1/event/21/Connector_J%20Performance%20Gems%20Presentation.pdf */
/*
* https://cdn.oreillystatic.com/en/assets/1/event/21/Connector_J%20Performance%
* 20Gems%20Presentation.pdf
*/
config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("prepStmtCacheSize", "250");
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
@@ -326,202 +578,310 @@ public class ConfigHandler extends Queue {
ConfigHandler.hikariDataSource = new HikariDataSource(config);
}
Database.createDatabaseTables(ConfigHandler.prefix, false, null, Config.getGlobal().MYSQL, false);
Database.createDatabaseTables(ConfigHandler.prefix, false, null, ConfigHandler.databaseType, false);
}
public static void loadMaterials(Statement statement) {
public static boolean loadMaterials(Statement statement) {
try {
String query = "SELECT id,material FROM " + ConfigHandler.prefix + "material_map";
ResultSet rs = statement.executeQuery(query);
ConfigHandler.materials.clear();
ConfigHandler.materialsReversed.clear();
materialId = 0;
while (rs.next()) {
int id = rs.getInt("id");
String material = rs.getString("material");
ConfigHandler.materials.put(material, id);
ConfigHandler.materialsReversed.put(id, material);
if (id > materialId) {
materialId = id;
}
}
rs.close();
IdentifierCache cache = loadIdentifierCache(statement, "material_map", "material");
ConfigHandler.materials = syncMap(cache.values);
ConfigHandler.materialsReversed = syncMap(cache.reversed);
materialId = cache.maximumId;
return true;
}
catch (Exception e) {
ErrorReporter.report(e);
return false;
}
}
public static void loadBlockdata(Statement statement) {
public static boolean loadBlockdata(Statement statement) {
try {
String query = "SELECT id,data FROM " + ConfigHandler.prefix + "blockdata_map";
ResultSet rs = statement.executeQuery(query);
ConfigHandler.blockdata.clear();
ConfigHandler.blockdataReversed.clear();
blockdataId = 0;
while (rs.next()) {
int id = rs.getInt("id");
String data = rs.getString("data");
ConfigHandler.blockdata.put(data, id);
ConfigHandler.blockdataReversed.put(id, data);
if (id > blockdataId) {
blockdataId = id;
}
}
rs.close();
IdentifierCache cache = loadIdentifierCache(statement, "blockdata_map", "data");
ConfigHandler.blockdata = syncMap(cache.values);
ConfigHandler.blockdataReversed = syncMap(cache.reversed);
blockdataId = cache.maximumId;
return true;
}
catch (Exception e) {
ErrorReporter.report(e);
return false;
}
}
public static void loadArt(Statement statement) {
public static boolean loadArt(Statement statement) {
try {
String query = "SELECT id,art FROM " + ConfigHandler.prefix + "art_map";
ResultSet rs = statement.executeQuery(query);
ConfigHandler.art.clear();
ConfigHandler.artReversed.clear();
artId = 0;
while (rs.next()) {
int id = rs.getInt("id");
String art = rs.getString("art");
ConfigHandler.art.put(art, id);
ConfigHandler.artReversed.put(id, art);
if (id > artId) {
artId = id;
}
}
rs.close();
IdentifierCache cache = loadIdentifierCache(statement, "art_map", "art");
ConfigHandler.art = syncMap(cache.values);
ConfigHandler.artReversed = syncMap(cache.reversed);
artId = cache.maximumId;
return true;
}
catch (Exception e) {
ErrorReporter.report(e);
return false;
}
}
public static void loadEntities(Statement statement) {
public static boolean loadEntities(Statement statement) {
try {
String query = "SELECT id,entity FROM " + ConfigHandler.prefix + "entity_map";
ResultSet rs = statement.executeQuery(query);
ConfigHandler.entities.clear();
ConfigHandler.entitiesReversed.clear();
entityId = 0;
while (rs.next()) {
int id = rs.getInt("id");
String entity = rs.getString("entity");
ConfigHandler.entities.put(entity, id);
ConfigHandler.entitiesReversed.put(id, entity);
if (id > entityId) {
entityId = id;
}
}
rs.close();
IdentifierCache cache = loadIdentifierCache(statement, "entity_map", "entity");
ConfigHandler.entities = syncMap(cache.values);
ConfigHandler.entitiesReversed = syncMap(cache.reversed);
entityId = cache.maximumId;
return true;
}
catch (Exception e) {
ErrorReporter.report(e);
return false;
}
}
public static void loadTypes(Statement statement) {
loadMaterials(statement);
loadBlockdata(statement);
loadArt(statement);
loadEntities(statement);
public static boolean loadTypes(Statement statement) {
boolean loaded = loadMaterials(statement);
loaded &= loadBlockdata(statement);
loaded &= loadArt(statement);
loaded &= loadEntities(statement);
if (loaded && databaseType.isClickHouse() && !Config.getGlobal().DATABASE_LOCK) {
for (EntityType type : EntityType.values()) {
if (EntitySpawnTracking.isPlacedEntityType(type)) {
resolveIdentifierId(CacheType.ENTITIES, type.name().toLowerCase(Locale.ROOT), true);
}
}
}
return loaded;
}
/**
* Unified method to reload cache from database when DATABASE_LOCK is false (multi-server setup)
*
* Reloads an identifier cache when multi-server cache refresh is permitted.
*
* @param type
* The type of cache to reload
* @param name
* The name to look up after reload
* @return The ID if found after reload, or -1 if not found
*/
public static int reloadAndGetId(CacheType type, String name) {
// Only reload if DATABASE_LOCK is false (multi-server setup)
if (Config.getGlobal().DATABASE_LOCK) {
public static synchronized int reloadAndGetId(CacheType type, String name) {
if (Config.getGlobal().DATABASE_LOCK || !reloadIdentifierCache(type.kind)) {
return -1;
}
return identifierStore(type.kind).values.get().getOrDefault(name, -1);
}
public static int resolveIdentifierId(CacheType type, String value, boolean create) {
IdentifierStore store = identifierStore(type.kind);
Integer cached = store.values.get().get(value);
return cached != null ? cached : resolveMissingIdentifierId(store, value, create);
}
private static synchronized int resolveMissingIdentifierId(IdentifierStore store, String value, boolean create) {
Integer cached = store.values.get().get(value);
if (cached != null) {
return cached;
}
if (databaseType.isClickHouse()) {
int id;
try {
id = Database.findClickHouseIdentifierId(store.kind, value);
}
catch (Exception exception) {
throw new IllegalStateException("Unable to resolve ClickHouse " + store.kind.name().toLowerCase(Locale.ROOT) + " identifier for " + value, exception);
}
if (id > 0) {
store.store(id, value);
return id;
}
}
else if (create && !Config.getGlobal().DATABASE_LOCK && reloadIdentifierCache(store.kind)) {
cached = store.values.get().get(value);
if (cached != null) {
return cached;
}
}
if (!create) {
return -1;
}
try (Connection connection = Database.getConnection(true)) {
if (connection != null) {
Statement statement = connection.createStatement();
// Reload appropriate cache based on type
switch (type) {
case MATERIALS:
loadMaterials(statement);
statement.close();
return materials.getOrDefault(name, -1);
case BLOCKDATA:
loadBlockdata(statement);
statement.close();
return blockdata.getOrDefault(name, -1);
case ART:
loadArt(statement);
statement.close();
return art.getOrDefault(name, -1);
case ENTITIES:
loadEntities(statement);
statement.close();
return entities.getOrDefault(name, -1);
case WORLDS:
loadWorlds(statement);
statement.close();
return worlds.getOrDefault(name, -1);
default:
statement.close();
return -1;
}
int id = allocateIdentifierId(store, value, store.maximum.getAsInt());
boolean clickHouse = databaseType.isClickHouse();
if (id == -1 || !store.canStore(id, value)) {
if (clickHouse) {
throw new IllegalStateException("Unable to resolve ClickHouse " + store.kind.name().toLowerCase(Locale.ROOT) + " identifier for " + value);
}
return -1;
}
if (clickHouse) {
if (!queueIdentifier(store.kind, id, value)) {
throw new IllegalStateException("Database persistence halted before the ClickHouse identifier could be queued");
}
}
catch (Exception e) {
ErrorReporter.report(e);
store.store(id, value);
if (!clickHouse) {
queueIdentifier(store.kind, id, value);
}
return -1;
return id;
}
public static void loadWorlds(Statement statement) {
private static boolean queueIdentifier(ReferenceKind kind, int id, String value) {
switch (kind) {
case ART:
return tryQueueIdentifierInsert(Process.ART_INSERT, id, value);
case BLOCK_DATA:
return tryQueueIdentifierInsert(Process.BLOCKDATA_INSERT, id, value);
case ENTITY:
return tryQueueIdentifierInsert(Process.ENTITY_INSERT, id, value);
case MATERIAL:
return tryQueueIdentifierInsert(Process.MATERIAL_INSERT, id, value);
case WORLD:
return tryQueueIdentifierInsert(Process.WORLD_INSERT, id, value);
default:
throw new IllegalArgumentException("Unsupported identifier cache: " + kind);
}
}
public static synchronized void uncacheIdentifier(ReferenceKind kind, int id, String value) {
IdentifierStore store = identifierStore(kind);
store.values.get().remove(value, Integer.valueOf(id));
store.reversed.get().remove(Integer.valueOf(id), value);
}
public static String getIdentifierValue(CacheType type, int id) {
IdentifierStore store = identifierStore(type.kind);
String value = store.reversed.get().get(id);
if (value != null || id < 1 || !databaseType.isClickHouse()) {
return value;
}
return loadMissingIdentifierValue(store, id);
}
private static synchronized String loadMissingIdentifierValue(IdentifierStore store, int id) {
String value = store.reversed.get().get(id);
if (value != null) {
return value;
}
try {
String query = "SELECT id,world FROM " + ConfigHandler.prefix + "world";
ResultSet rs = statement.executeQuery(query);
ConfigHandler.worlds.clear();
ConfigHandler.worldsReversed.clear();
worldId = 0;
value = Database.findClickHouseIdentifierValue(store.kind, id);
}
catch (Exception exception) {
throw new IllegalStateException("Unable to resolve ClickHouse " + store.kind.name().toLowerCase(Locale.ROOT) + " identifier " + id, exception);
}
if (value != null) {
store.store(id, value);
}
return value;
}
while (rs.next()) {
int id = rs.getInt("id");
String world = rs.getString("world");
ConfigHandler.worlds.put(world, id);
ConfigHandler.worldsReversed.put(id, world);
if (id > worldId) {
worldId = id;
}
private static int allocateIdentifierId(IdentifierStore store, String value, int currentMaximum) {
if (!ConfigHandler.databaseType.isClickHouse()) {
return currentMaximum + 1;
}
try {
return Database.nextClickHouseIdentifierId(store.kind, value, currentMaximum);
}
catch (Exception exception) {
throw new IllegalStateException("Unable to allocate ClickHouse " + store.kind.name().toLowerCase(Locale.ROOT) + " identifier for " + value, exception);
}
}
private static IdentifierStore identifierStore(ReferenceKind kind) {
IdentifierStore store = IDENTIFIER_STORES.get(kind);
if (store == null) {
throw new IllegalArgumentException("Unsupported identifier cache: " + kind);
}
return store;
}
private static Map<ReferenceKind, IdentifierStore> identifierStores() {
Map<ReferenceKind, IdentifierStore> stores = new EnumMap<>(ReferenceKind.class);
stores.put(ReferenceKind.MATERIAL, new IdentifierStore(ReferenceKind.MATERIAL, () -> materials, () -> materialsReversed, () -> materialId, id -> materialId = id));
stores.put(ReferenceKind.BLOCK_DATA, new IdentifierStore(ReferenceKind.BLOCK_DATA, () -> blockdata, () -> blockdataReversed, () -> blockdataId, id -> blockdataId = id));
stores.put(ReferenceKind.ART, new IdentifierStore(ReferenceKind.ART, () -> art, () -> artReversed, () -> artId, id -> artId = id));
stores.put(ReferenceKind.ENTITY, new IdentifierStore(ReferenceKind.ENTITY, () -> entities, () -> entitiesReversed, () -> entityId, id -> entityId = id));
stores.put(ReferenceKind.WORLD, new IdentifierStore(ReferenceKind.WORLD, () -> worlds, () -> worldsReversed, () -> worldId, id -> worldId = id));
return stores;
}
private static boolean reloadIdentifierCache(ReferenceKind kind) {
try (Connection connection = Database.getConnection(true)) {
if (connection == null) {
return false;
}
rs.close();
List<World> worlds = Bukkit.getServer().getWorlds();
for (World world : worlds) {
String worldname = world.getName();
if (ConfigHandler.worlds.get(worldname) == null) {
int id = worldId + 1;
ConfigHandler.worlds.put(worldname, id);
ConfigHandler.worldsReversed.put(id, worldname);
worldId = id;
Queue.queueWorldInsert(id, worldname);
try (Statement statement = connection.createStatement()) {
switch (kind) {
case MATERIAL:
return loadMaterials(statement);
case BLOCK_DATA:
return loadBlockdata(statement);
case ART:
return loadArt(statement);
case ENTITY:
return loadEntities(statement);
case WORLD:
return loadWorlds(statement);
default:
return false;
}
}
}
catch (Exception e) {
ErrorReporter.report(e);
return false;
}
}
public static boolean loadWorlds(Statement statement) {
try {
IdentifierCache cache = loadIdentifierCache(statement, "world", "world");
List<World> worlds = Bukkit.getServer().getWorlds();
Map<Integer, String> queuedWorlds = new HashMap<>();
for (World world : worlds) {
String worldname = world.getName();
if (!cache.values.containsKey(worldname)) {
int id = allocateIdentifierId(identifierStore(ReferenceKind.WORLD), worldname, cache.maximumId);
if (id == -1) {
return false;
}
cache.maximumId = Math.max(cache.maximumId, id);
cache.values.put(worldname, id);
cache.reversed.put(id, worldname);
queuedWorlds.put(id, worldname);
}
}
boolean clickHouse = databaseType.isClickHouse();
if (clickHouse) {
if (!queueIdentifiers(ReferenceKind.WORLD, queuedWorlds)) {
throw new IllegalStateException("Database persistence halted before the ClickHouse world identifiers could be queued");
}
}
ConfigHandler.worlds = syncMap(cache.values);
ConfigHandler.worldsReversed = syncMap(cache.reversed);
worldId = cache.maximumId;
if (!clickHouse) {
queueIdentifiers(ReferenceKind.WORLD, queuedWorlds);
}
return true;
}
catch (Exception e) {
if (databaseType.isClickHouse() && e instanceof IllegalStateException) {
throw (IllegalStateException) e;
}
ErrorReporter.report(e);
return false;
}
}
private static boolean queueIdentifiers(ReferenceKind kind, Map<Integer, String> identifiers) {
for (Map.Entry<Integer, String> identifier : identifiers.entrySet()) {
if (!queueIdentifier(kind, identifier.getKey(), identifier.getValue())) {
return false;
}
}
return true;
}
private static boolean checkDatabaseLock(Statement statement) {
try {
if (Config.getGlobal().DATABASE_LOCK) {
@@ -533,7 +893,7 @@ public class ConfigHandler extends Queue {
locked = false;
unixtimestamp = (int) (System.currentTimeMillis() / 1000L);
int checkTime = unixtimestamp - 15;
String query = "SELECT * FROM " + ConfigHandler.prefix + "database_lock WHERE rowid='1' AND status='1' AND time >= '" + checkTime + "' LIMIT 1";
String query = "SELECT * FROM " + ConfigHandler.prefix + "database_lock WHERE rowid='1' AND (status='" + Database.DATABASE_LOCK_MIGRATION_INCOMPLETE + "' OR (status='" + Database.DATABASE_LOCK_ACTIVE + "' AND time >= '" + checkTime + "')) LIMIT 1";
ResultSet rs = statement.executeQuery(query);
while (rs.next()) {
if (unixtimestamp < waitTime) {
@@ -577,16 +937,23 @@ public class ConfigHandler extends Queue {
ListenerHandler.registerNetworking(); // Register channels for networking API
}
}
catch (CancellationException e) {
throw e;
}
catch (Exception e) {
ErrorReporter.report(e);
return false;
}
try (Connection connection = Database.getConnection(true, 0)) {
Statement statement = connection.createStatement();
ConfigHandler.checkPlayers(connection);
ConfigHandler.loadWorlds(statement); // Load world ID's into memory.
ConfigHandler.loadTypes(statement); // Load material ID's into memory.
boolean worldsLoaded = ConfigHandler.loadWorlds(statement); // Load world ID's into memory.
boolean typesLoaded = ConfigHandler.loadTypes(statement); // Load material ID's into memory.
if (ConfigHandler.databaseType.isColumnar() && (!worldsLoaded || !typesLoaded)) {
throw new IllegalStateException("Unable to initialize columnar database identifier caches");
}
// Initialize WorldEdit logging
if (VersionUtils.checkWorldEdit()) {
@@ -600,7 +967,9 @@ public class ConfigHandler extends Queue {
VersionUtils.unloadWorldEdit();
}
ConfigHandler.serverRunning = true; // Set as running before patching
if (startup) {
ConfigHandler.serverRunning = true; // Set as running before patching
}
boolean validVersion = Patch.versionCheck(statement); // Minor upgrades & version check
boolean databaseLock = true;
if (startup) {
@@ -0,0 +1,237 @@
package net.coreprotect.config;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.AtomicMoveNotSupportedException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.AclFileAttributeView;
import java.nio.file.attribute.DosFileAttributeView;
import java.nio.file.attribute.DosFileAttributes;
import java.nio.file.attribute.FileOwnerAttributeView;
import java.nio.file.attribute.PosixFileAttributeView;
import java.nio.file.attribute.PosixFileAttributes;
import java.util.Arrays;
import java.util.Locale;
import net.coreprotect.database.DatabaseType;
public final class DatabaseConfigWriter {
private DatabaseConfigWriter() {
throw new IllegalStateException("Utility class");
}
public static synchronized void persistDatabaseType(DatabaseType type) throws IOException {
Path configFile = Paths.get(ConfigHandler.path).resolve(ConfigFile.CONFIG);
if (!Files.isRegularFile(configFile, LinkOption.NOFOLLOW_LINKS)) {
throw new IOException("CoreProtect config is not a regular file: " + configFile);
}
byte[] original = Files.readAllBytes(configFile);
byte[] updated = updateDatabaseType(original, type);
if (Arrays.equals(original, updated)) {
return;
}
Path directory = configFile.toAbsolutePath().getParent();
if (directory == null) {
throw new IOException("CoreProtect config has no parent directory: " + configFile);
}
Path temporary = Files.createTempFile(directory, ".config.yml.", ".tmp");
boolean moved = false;
try {
Files.write(temporary, updated, StandardOpenOption.TRUNCATE_EXISTING);
try (FileChannel channel = FileChannel.open(temporary, StandardOpenOption.WRITE)) {
channel.force(true);
}
copyAttributes(configFile, temporary);
try {
Files.move(temporary, configFile, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
}
catch (AtomicMoveNotSupportedException exception) {
throw new IOException("The CoreProtect config filesystem does not support atomic replacement", exception);
}
moved = true;
forceDirectory(directory);
}
finally {
if (!moved) {
Files.deleteIfExists(temporary);
}
}
}
private static byte[] updateDatabaseType(byte[] data, DatabaseType type) {
String content = new String(data, StandardCharsets.UTF_8);
String databaseType = type.name().toLowerCase(Locale.ROOT);
String lineSeparator = detectLineSeparator(content);
boolean terminalLineSeparator = content.endsWith("\n") || content.endsWith("\r");
boolean foundDatabaseType = false;
StringBuilder updated = new StringBuilder(content.length() + 32);
int offset = 0;
while (offset < content.length()) {
int lineEnd = offset;
while (lineEnd < content.length() && content.charAt(lineEnd) != '\r' && content.charAt(lineEnd) != '\n') {
lineEnd++;
}
int nextOffset = lineEnd;
if (nextOffset < content.length()) {
char separator = content.charAt(nextOffset++);
if (separator == '\r' && nextOffset < content.length() && content.charAt(nextOffset) == '\n') {
nextOffset++;
}
}
String line = content.substring(offset, lineEnd);
String replaced = replaceScalar(line, "database-type", databaseType);
if (replaced != null) {
foundDatabaseType = true;
updated.append(replaced);
}
else {
replaced = replaceScalar(line, "use-mysql", Boolean.toString(type.isMySQL()));
updated.append(replaced == null ? line : replaced);
}
updated.append(content, lineEnd, nextOffset);
offset = nextOffset;
}
if (!foundDatabaseType) {
if (updated.length() > 0 && updated.charAt(updated.length() - 1) != '\n' && updated.charAt(updated.length() - 1) != '\r') {
updated.append(lineSeparator);
}
updated.append("database-type: ").append(databaseType);
if (terminalLineSeparator) {
updated.append(lineSeparator);
}
}
return updated.toString().getBytes(StandardCharsets.UTF_8);
}
private static String replaceScalar(String line, String expectedKey, String value) {
int first = 0;
while (first < line.length() && Character.isWhitespace(line.charAt(first))) {
first++;
}
if (first == line.length() || line.charAt(first) == '#') {
return null;
}
int colon = line.indexOf(':', first);
if (colon < 0 || !line.substring(first, colon).trim().equalsIgnoreCase(expectedKey)) {
return null;
}
int valueStart = colon + 1;
while (valueStart < line.length() && Character.isWhitespace(line.charAt(valueStart))) {
valueStart++;
}
int comment = findComment(line, valueStart);
int valueEnd = comment < 0 ? line.length() : comment;
while (valueEnd > valueStart && Character.isWhitespace(line.charAt(valueEnd - 1))) {
valueEnd--;
}
String replacement = value;
if (valueStart < valueEnd && (line.charAt(valueStart) == '\'' || line.charAt(valueStart) == '"')) {
char quote = line.charAt(valueStart);
replacement = quote + value + quote;
}
String whitespace = line.substring(colon + 1, valueStart);
if (whitespace.isEmpty()) {
whitespace = " ";
}
String suffix = line.substring(valueEnd);
if (comment == valueStart && suffix.startsWith("#")) {
suffix = " " + suffix;
}
return line.substring(0, colon + 1) + whitespace + replacement + suffix;
}
private static int findComment(String line, int start) {
boolean singleQuoted = false;
boolean doubleQuoted = false;
boolean escaped = false;
for (int index = start; index < line.length(); index++) {
char character = line.charAt(index);
if (doubleQuoted && character == '\\' && !escaped) {
escaped = true;
continue;
}
if (character == '"' && !singleQuoted && !escaped) {
doubleQuoted = !doubleQuoted;
}
else if (character == '\'' && !doubleQuoted) {
singleQuoted = !singleQuoted;
}
else if (character == '#' && !singleQuoted && !doubleQuoted && (index == start || Character.isWhitespace(line.charAt(index - 1)))) {
return index;
}
escaped = false;
}
return -1;
}
private static String detectLineSeparator(String content) {
int newline = content.indexOf('\n');
if (newline >= 0) {
return newline > 0 && content.charAt(newline - 1) == '\r' ? "\r\n" : "\n";
}
return content.indexOf('\r') >= 0 ? "\r" : System.lineSeparator();
}
private static void copyAttributes(Path source, Path target) throws IOException {
boolean ownerCopied = false;
PosixFileAttributeView sourcePosix = Files.getFileAttributeView(source, PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
PosixFileAttributeView targetPosix = Files.getFileAttributeView(target, PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
if (sourcePosix != null && targetPosix != null) {
PosixFileAttributes attributes = sourcePosix.readAttributes();
targetPosix.setOwner(attributes.owner());
targetPosix.setGroup(attributes.group());
targetPosix.setPermissions(attributes.permissions());
ownerCopied = true;
}
AclFileAttributeView sourceAcl = Files.getFileAttributeView(source, AclFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
AclFileAttributeView targetAcl = Files.getFileAttributeView(target, AclFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
if (sourceAcl != null && targetAcl != null) {
targetAcl.setOwner(sourceAcl.getOwner());
targetAcl.setAcl(sourceAcl.getAcl());
ownerCopied = true;
}
if (!ownerCopied) {
FileOwnerAttributeView sourceOwner = Files.getFileAttributeView(source, FileOwnerAttributeView.class, LinkOption.NOFOLLOW_LINKS);
FileOwnerAttributeView targetOwner = Files.getFileAttributeView(target, FileOwnerAttributeView.class, LinkOption.NOFOLLOW_LINKS);
if (sourceOwner != null && targetOwner != null) {
targetOwner.setOwner(sourceOwner.getOwner());
}
}
DosFileAttributeView sourceDos = Files.getFileAttributeView(source, DosFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
DosFileAttributeView targetDos = Files.getFileAttributeView(target, DosFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
if (sourceDos != null && targetDos != null) {
DosFileAttributes attributes = sourceDos.readAttributes();
targetDos.setArchive(attributes.isArchive());
targetDos.setHidden(attributes.isHidden());
targetDos.setSystem(attributes.isSystem());
targetDos.setReadOnly(attributes.isReadOnly());
}
}
private static void forceDirectory(Path directory) {
try (FileChannel channel = FileChannel.open(directory, StandardOpenOption.READ)) {
channel.force(true);
}
catch (IOException | UnsupportedOperationException ignored) {
}
}
}
@@ -5,7 +5,12 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.bukkit.Bukkit;
import org.bukkit.block.BlockState;
@@ -14,16 +19,43 @@ import org.bukkit.inventory.ItemStack;
import net.coreprotect.CoreProtect;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.consumer.process.Process;
import net.coreprotect.database.Database;
import net.coreprotect.database.DuckDBRecovery;
import net.coreprotect.language.Phrase;
import net.coreprotect.utility.Chat;
import net.coreprotect.utility.Color;
import net.coreprotect.utility.ErrorReporter;
public class Consumer extends Process implements Runnable, Thread.UncaughtExceptionHandler {
public enum OperationStartResult {
STARTED,
PURGE_RUNNING,
ROLLBACK_RUNNING,
RELOAD_RUNNING,
PERSISTENCE_HALTED,
INTERRUPTED
}
private static Thread consumerThread = null;
private static final Object pauseMonitor = new Object();
private static boolean operationPaused = false;
private static boolean maintenancePaused = false;
private static final ReentrantReadWriteLock databaseLifecycle = new ReentrantReadWriteLock(true);
private static final Object rollbackPurgeGate = new Object();
private static long pendingRollbackPublications = 0;
private static volatile boolean backgroundPurgeRunning = false;
private static volatile boolean backgroundPurgePausesPersistence = false;
private static volatile boolean databaseReloadPaused = false;
private static volatile boolean databaseReloadRunning = false;
private static volatile boolean databaseReloadBlockedForShutdown = false;
private static CompletableFuture<Void> databaseReloadShutdownSignal = new CompletableFuture<>();
public static volatile int currentConsumer = 0;
public static volatile boolean isPaused = false;
public static volatile boolean transacting = false;
public static volatile boolean interrupt = false;
protected static volatile boolean pausedSuccess = false;
private static volatile boolean persistenceHalted = false;
public static ConcurrentHashMap<Integer, ArrayList<Object[]>> consumer = new ConcurrentHashMap<>(4, 0.75f, 2);
// public static ConcurrentHashMap<Integer, Integer[]> consumer_id = new ConcurrentHashMap<>();
@@ -56,10 +88,29 @@ public class Consumer extends Process implements Runnable, Thread.UncaughtExcept
}
}
protected static int newConsumerId(int consumer) {
int id = Consumer.consumer_id.get(consumer)[0];
Consumer.consumer_id.put(consumer, new Integer[] { id + 1, 1 });
return id;
protected static long reserveConsumer() {
return reserveConsumers(1);
}
protected static long reserveConsumers(int count) {
synchronized (Consumer.consumer_id) {
int consumer = Consumer.currentConsumer;
Integer[] state = Consumer.consumer_id.get(consumer);
int id = state[0];
Consumer.consumer_id.put(consumer, new Integer[] { id + count, state[1] + count });
return ((long) consumer << 32) | (id & 0xffffffffL);
}
}
protected static void completeReservation(long reservation, int count) {
int consumer = (int) (reservation >>> 32);
synchronized (Consumer.consumer_id) {
Integer[] state = Consumer.consumer_id.get(consumer);
if (count <= 0 || state == null || state[1] < count) {
throw new IllegalStateException("Invalid consumer reservation completion");
}
Consumer.consumer_id.put(consumer, new Integer[] { state[0], state[1] - count });
}
}
public static int getConsumerSize(int id) {
@@ -70,7 +121,39 @@ public class Consumer extends Process implements Runnable, Thread.UncaughtExcept
return 0;
}
public static boolean hasPendingConsumerData() {
synchronized (consumer_id) {
for (int id = 0; id < 2; id++) {
Integer[] state = consumer_id.get(id);
ArrayList<Object[]> data = consumer.get(id);
if ((state != null && state[1] > 0) || (data != null && !data.isEmpty())) {
return true;
}
}
return false;
}
}
public static void initialize() {
synchronized (rollbackPurgeGate) {
persistenceHalted = false;
pendingRollbackPublications = 0;
databaseReloadBlockedForShutdown = false;
databaseReloadShutdownSignal.complete(null);
databaseReloadShutdownSignal = new CompletableFuture<>();
}
synchronized (pauseMonitor) {
operationPaused = false;
maintenancePaused = false;
isPaused = false;
pauseMonitor.notifyAll();
}
databaseReloadPaused = false;
databaseReloadRunning = false;
backgroundPurgeRunning = false;
backgroundPurgePausesPersistence = false;
resetPreparationFailures();
DuckDBRecovery.reset();
Consumer.consumer.put(0, new ArrayList<>());
Consumer.consumer.put(1, new ArrayList<>());
Consumer.consumer_id.put(0, new Integer[] { 0, 0 });
@@ -99,43 +182,450 @@ public class Consumer extends Process implements Runnable, Thread.UncaughtExcept
return consumerThread != null && consumerThread.isAlive();
}
private static void pauseConsumer(int process_id) {
public static boolean isPersistenceHalted() {
return persistenceHalted;
}
public static boolean claimLookupPause() throws InterruptedException {
synchronized (pauseMonitor) {
while (isPaused && !persistenceHalted) {
pauseMonitor.wait(1L);
}
if (persistenceHalted) {
return false;
}
operationPaused = true;
isPaused = true;
return true;
}
}
public static boolean tryClaimConsumerPause() {
synchronized (pauseMonitor) {
if (isPaused || persistenceHalted) {
return false;
}
operationPaused = true;
isPaused = true;
return true;
}
}
public static void releaseOperationPause() {
synchronized (pauseMonitor) {
operationPaused = false;
isPaused = maintenancePaused;
pauseMonitor.notifyAll();
}
}
public static void setMaintenancePaused(boolean paused) {
synchronized (pauseMonitor) {
maintenancePaused = paused;
isPaused = operationPaused || maintenancePaused;
pauseMonitor.notifyAll();
}
}
public static void lockDatabaseAccess() {
databaseLifecycle.readLock().lock();
}
public static void unlockDatabaseAccess() {
databaseLifecycle.readLock().unlock();
}
public static void lockDatabaseMaintenance() {
databaseLifecycle.writeLock().lock();
}
public static void lockDatabaseMaintenanceInterruptibly() throws InterruptedException {
databaseLifecycle.writeLock().lockInterruptibly();
}
public static void unlockDatabaseMaintenance() {
databaseLifecycle.writeLock().unlock();
}
public static boolean isDatabaseReloadBlocked() {
return databaseReloadPaused && !databaseLifecycle.isWriteLockedByCurrentThread();
}
public static boolean isDatabaseReloadPaused() {
return databaseReloadPaused;
}
public static boolean isDatabaseReloadRunning() {
return databaseReloadRunning;
}
public static boolean isBackgroundPurgeRunning() {
return backgroundPurgeRunning;
}
public static void requireDatabaseReload() {
synchronized (rollbackPurgeGate) {
databaseReloadPaused = true;
}
}
public static OperationStartResult beginDatabaseReload() {
return beginDatabaseReload(false);
}
public static OperationStartResult beginDatabaseRecovery() {
return beginDatabaseReload(true);
}
private static OperationStartResult beginDatabaseReload(boolean allowActiveRollbacks) {
synchronized (rollbackPurgeGate) {
if (databaseReloadBlockedForShutdown) {
return OperationStartResult.INTERRUPTED;
}
if (persistenceHalted) {
return OperationStartResult.PERSISTENCE_HALTED;
}
if (databaseReloadRunning) {
return OperationStartResult.RELOAD_RUNNING;
}
if (ConfigHandler.purgeRunning || backgroundPurgeRunning) {
return OperationStartResult.PURGE_RUNNING;
}
if (!allowActiveRollbacks && (!ConfigHandler.activeRollbacks.isEmpty() || pendingRollbackPublications > 0)) {
return OperationStartResult.ROLLBACK_RUNNING;
}
databaseReloadRunning = true;
databaseReloadPaused = true;
}
return OperationStartResult.STARTED;
}
public static void blockDatabaseReloadForShutdown() {
synchronized (rollbackPurgeGate) {
databaseReloadBlockedForShutdown = true;
databaseReloadShutdownSignal.complete(null);
}
}
public static CompletableFuture<Void> databaseReloadShutdownSignal() {
synchronized (rollbackPurgeGate) {
return databaseReloadShutdownSignal.copy();
}
}
public static void lockDatabaseReload() {
databaseLifecycle.writeLock().lock();
}
public static boolean lockDatabaseReload(long timeoutMillis) throws InterruptedException {
return databaseLifecycle.writeLock().tryLock(Math.max(0L, timeoutMillis), java.util.concurrent.TimeUnit.MILLISECONDS);
}
public static void endDatabaseReload(boolean resumePersistence) {
try {
while ((ConfigHandler.serverRunning || ConfigHandler.converterRunning || ConfigHandler.migrationRunning) && (Consumer.isPaused || ConfigHandler.pauseConsumer || ConfigHandler.purgeRunning || Consumer.consumer_id.get(process_id)[1] == 1)) {
if (databaseLifecycle.isWriteLockedByCurrentThread()) {
databaseLifecycle.writeLock().unlock();
}
}
finally {
synchronized (rollbackPurgeGate) {
if (resumePersistence) {
databaseReloadPaused = false;
if (!ConfigHandler.converterRunning) {
setMaintenancePaused(false);
}
}
databaseReloadRunning = false;
}
}
}
public static synchronized void haltPersistence() {
synchronized (rollbackPurgeGate) {
if (persistenceHalted) {
return;
}
persistenceHalted = true;
}
ConfigHandler.databaseReachable = false;
Chat.sendConsoleMessage(Color.RED + "[CoreProtect] " + Phrase.build(Phrase.DATABASE_PERSISTENCE_HALTED));
Chat.sendConsoleMessage(Color.RED + "[CoreProtect] " + Phrase.build(Phrase.DATABASE_QUEUE_LOSS));
}
public static OperationStartResult claimPurge() {
synchronized (rollbackPurgeGate) {
if (persistenceHalted) {
return OperationStartResult.PERSISTENCE_HALTED;
}
if (databaseReloadPaused) {
return OperationStartResult.RELOAD_RUNNING;
}
if (ConfigHandler.purgeRunning || backgroundPurgeRunning) {
return OperationStartResult.PURGE_RUNNING;
}
if (!ConfigHandler.activeRollbacks.isEmpty() || pendingRollbackPublications > 0) {
return OperationStartResult.ROLLBACK_RUNNING;
}
ConfigHandler.purgeRunning = true;
return OperationStartResult.STARTED;
}
}
public static void releasePurge() {
synchronized (rollbackPurgeGate) {
ConfigHandler.purgeRunning = false;
}
}
public static OperationStartResult claimBackgroundPurge() {
boolean pausePersistence;
synchronized (rollbackPurgeGate) {
pausePersistence = ConfigHandler.databaseType.isClickHouse();
OperationStartResult result = beginBackgroundPurge(pausePersistence);
if (result != OperationStartResult.STARTED) {
return result;
}
}
return lockBackgroundPurge(pausePersistence);
}
public static OperationStartResult claimBackgroundPurge(boolean pausePersistence) {
synchronized (rollbackPurgeGate) {
OperationStartResult result = beginBackgroundPurge(pausePersistence);
if (result != OperationStartResult.STARTED) {
return result;
}
}
return lockBackgroundPurge(pausePersistence);
}
private static OperationStartResult beginBackgroundPurge(boolean pausePersistence) {
if (persistenceHalted) {
return OperationStartResult.PERSISTENCE_HALTED;
}
if (databaseReloadPaused) {
return OperationStartResult.RELOAD_RUNNING;
}
if (ConfigHandler.purgeRunning || backgroundPurgeRunning) {
return OperationStartResult.PURGE_RUNNING;
}
if (!ConfigHandler.activeRollbacks.isEmpty() || pendingRollbackPublications > 0) {
return OperationStartResult.ROLLBACK_RUNNING;
}
backgroundPurgeRunning = true;
backgroundPurgePausesPersistence = pausePersistence;
return OperationStartResult.STARTED;
}
private static OperationStartResult lockBackgroundPurge(boolean pausePersistence) {
if (pausePersistence) {
try {
databaseLifecycle.writeLock().lockInterruptibly();
}
catch (InterruptedException exception) {
synchronized (rollbackPurgeGate) {
backgroundPurgePausesPersistence = false;
backgroundPurgeRunning = false;
}
Thread.currentThread().interrupt();
return OperationStartResult.INTERRUPTED;
}
synchronized (rollbackPurgeGate) {
if (persistenceHalted) {
databaseLifecycle.writeLock().unlock();
backgroundPurgePausesPersistence = false;
backgroundPurgeRunning = false;
return OperationStartResult.PERSISTENCE_HALTED;
}
}
}
return OperationStartResult.STARTED;
}
public static void releaseBackgroundPurge() {
synchronized (rollbackPurgeGate) {
if (!backgroundPurgeRunning) {
return;
}
if (backgroundPurgePausesPersistence) {
databaseLifecycle.writeLock().unlock();
}
backgroundPurgePausesPersistence = false;
backgroundPurgeRunning = false;
}
}
public static OperationStartResult claimRollback(String user) {
return claimRollback(user, true);
}
public static OperationStartResult claimRollback(String user, boolean requiresPersistence) {
synchronized (rollbackPurgeGate) {
if (requiresPersistence && persistenceHalted) {
return OperationStartResult.PERSISTENCE_HALTED;
}
if (databaseReloadPaused) {
return OperationStartResult.RELOAD_RUNNING;
}
if (ConfigHandler.purgeRunning || backgroundPurgeRunning) {
return OperationStartResult.PURGE_RUNNING;
}
if (ConfigHandler.activeRollbacks.containsKey(user)) {
return OperationStartResult.ROLLBACK_RUNNING;
}
ConfigHandler.activeRollbacks.put(user, true);
return OperationStartResult.STARTED;
}
}
public static void releaseRollback(String user) {
synchronized (rollbackPurgeGate) {
ConfigHandler.activeRollbacks.remove(user);
}
}
public static void registerRollbackPublications(int count) {
if (count <= 0) {
return;
}
synchronized (rollbackPurgeGate) {
pendingRollbackPublications += count;
}
}
public static void completeRollbackPublications(int count) {
if (count <= 0) {
return;
}
synchronized (rollbackPurgeGate) {
if (pendingRollbackPublications < count) {
throw new IllegalStateException("Invalid rollback publication completion");
}
pendingRollbackPublications -= count;
}
}
private static boolean isShutdownDrainStopped() {
return databaseReloadBlockedForShutdown && !ConfigHandler.shutdownDrainRunning;
}
private static boolean pauseConsumer(int process_id) throws InterruptedException {
try {
while (Consumer.consumer_id.get(process_id)[1] > 0 || Consumer.isPaused
|| ((ConfigHandler.serverRunning || ConfigHandler.converterRunning || ConfigHandler.migrationRunning) && (ConfigHandler.pauseConsumer || ConfigHandler.purgeRunning))) {
if (persistenceHalted || isShutdownDrainStopped()) {
return false;
}
pausedSuccess = true;
Thread.sleep(100);
}
return !persistenceHalted && !isShutdownDrainStopped();
}
catch (Exception e) {
ErrorReporter.report(e);
finally {
pausedSuccess = false;
}
}
private static boolean lockConsumerDatabase(Lock lock) throws InterruptedException {
while (!persistenceHalted && !isShutdownDrainStopped()) {
if (lock.tryLock(100L, TimeUnit.MILLISECONDS)) {
return true;
}
}
return false;
}
static void processConsumerBatch(int processId, boolean lastRun) throws InterruptedException {
Lock databaseLock = databaseLifecycle.readLock();
if (!lockConsumerDatabase(databaseLock)) {
return;
}
try {
boolean exclusive = requiresEntityUuidMaintenance(processId);
if (exclusive) {
databaseLock.unlock();
databaseLock = null;
Lock writeLock = databaseLifecycle.writeLock();
if (!lockConsumerDatabase(writeLock)) {
return;
}
databaseLock = writeLock;
}
if (databaseReloadPaused || isPaused || persistenceHalted || isShutdownDrainStopped()) {
return;
}
if (exclusive && !Database.awaitConnectionDrain(0L)) {
return;
}
Process.processConsumer(processId, lastRun);
}
finally {
if (databaseLock != null) {
databaseLock.unlock();
}
}
pausedSuccess = false;
}
@Override
public void run() {
boolean lastRun = false;
boolean[] drained = { true, true };
while (ConfigHandler.serverRunning || ConfigHandler.converterRunning || !lastRun) {
while (!isShutdownDrainStopped()
&& (ConfigHandler.serverRunning || ConfigHandler.converterRunning || !lastRun || hasPendingConsumerData())) {
if (!ConfigHandler.serverRunning && !ConfigHandler.converterRunning) {
lastRun = true;
}
try {
int process_id = 0;
if (currentConsumer == 0) {
currentConsumer = 1;
if (persistenceHalted) {
if (lastRun) {
break;
}
if (databaseReloadBlockedForShutdown) {
LockSupport.parkNanos(100_000_000L);
}
else {
process_id = 1;
currentConsumer = 0;
errorDelay();
}
Thread.sleep(500);
pauseConsumer(process_id);
Process.processConsumer(process_id, lastRun);
continue;
}
try {
if (DuckDBRecovery.isPending()) {
DuckDBRecovery.recoverIfRequested();
if (DuckDBRecovery.isPending()) {
Thread.sleep(500L);
continue;
}
}
int process_id = 0;
synchronized (Consumer.consumer_id) {
if (currentConsumer == 0) {
currentConsumer = 1;
}
else {
process_id = 1;
currentConsumer = 0;
}
}
Thread.sleep(consumerDelay(lastRun || !drained[0] || !drained[1]));
if (!pauseConsumer(process_id)) {
continue;
}
try {
processConsumerBatch(process_id, lastRun);
}
finally {
drained[process_id] = getConsumerSize(process_id) == 0;
}
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
}
catch (Exception e) {
ErrorReporter.report(e);
errorDelay();
if (ConfigHandler.serverRunning || ConfigHandler.converterRunning) {
errorDelay();
}
}
}
}
+396 -133
View File
@@ -1,8 +1,11 @@
package net.coreprotect.consumer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import org.bukkit.Bukkit;
import org.bukkit.Location;
@@ -16,6 +19,7 @@ import org.bukkit.block.data.Bisected.Half;
import org.bukkit.block.data.Waterlogged;
import org.bukkit.block.data.type.Bed;
import org.bukkit.block.data.type.Bed.Part;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
@@ -27,29 +31,73 @@ import net.coreprotect.config.ConfigHandler;
import net.coreprotect.consumer.process.Process;
import net.coreprotect.listener.block.BlockUtil;
import net.coreprotect.model.BlockGroup;
import net.coreprotect.thread.Scheduler;
import net.coreprotect.model.entity.EntityContainerRollbackUpdate;
import net.coreprotect.model.entity.EntityContainerTransaction;
import net.coreprotect.model.entity.EntityInteraction;
import net.coreprotect.model.entity.EntityInteractionAction;
import net.coreprotect.model.entity.EntityInteractionOrigin;
import net.coreprotect.model.entity.EntitySpawnData;
import net.coreprotect.thread.CacheHandler;
import net.coreprotect.thread.Scheduler;
import net.coreprotect.utility.BlockUtils;
import net.coreprotect.utility.EntitySpawnTracking;
import net.coreprotect.utility.EntityUtils;
import net.coreprotect.utility.WorldUtils;
import net.coreprotect.utility.ErrorReporter;
import net.coreprotect.utility.WorldUtils;
import net.coreprotect.worldedit.WorldEditBlockState;
public class Queue {
protected static synchronized int modifyForceContainer(String id, ItemStack[] container) {
private static final int MAX_ROLLBACK_ROWS_PER_RECORD = 100_000;
public static synchronized void addForceContainer(String id, ItemStack[] container) {
if (container == null) {
return;
}
ConfigHandler.forceContainer.computeIfAbsent(id, k -> new ArrayList<>()).add(container);
}
public static synchronized void setForceContainer(String id, ItemStack[] container) {
if (container == null) {
return;
}
List<ItemStack[]> forceList = new ArrayList<>();
forceList.add(container);
ConfigHandler.forceContainer.put(id, forceList);
}
public static ItemStack[] pollForceContainer(String id) {
return pollForceContainer(id, 0);
}
public static synchronized ItemStack[] pollForceContainer(String id, int index) {
List<ItemStack[]> forceList = ConfigHandler.forceContainer.get(id);
if (forceList == null) {
return 0;
return null;
}
if (container == null) {
forceList.remove(0);
}
else {
forceList.add(container);
ItemStack[] container = index < 0 || index >= forceList.size() ? null : forceList.remove(index);
if (forceList.isEmpty()) {
ConfigHandler.forceContainer.remove(id);
}
return forceList.size();
return container;
}
public static synchronized ItemStack[] peekForceContainer(String id) {
List<ItemStack[]> forceList = ConfigHandler.forceContainer.get(id);
return (forceList == null || forceList.isEmpty()) ? null : forceList.get(0);
}
public static synchronized int getForceContainerSize(String id) {
List<ItemStack[]> forceList = ConfigHandler.forceContainer.get(id);
return forceList == null ? 0 : forceList.size();
}
public static synchronized void removeForceContainer(String id) {
ConfigHandler.forceContainer.remove(id);
}
protected static synchronized int getChestId(String id) {
@@ -58,20 +106,72 @@ public class Queue {
return chestId;
}
protected static synchronized int getItemId(String id) {
int chestId = ConfigHandler.loggingItem.getOrDefault(id, -1) + 1;
ConfigHandler.loggingItem.put(id, chestId);
return chestId;
// The Queue lock keeps ItemTransactionProcess.discard from clearing an item before its generation is registered,
// and compute keeps the append atomic with the consumer's take in ItemLogger
protected static synchronized int addPendingItems(ConcurrentHashMap<String, List<ItemStack>> pendingItems, String id, ItemStack... items) {
pendingItems.compute(id, (key, list) -> {
List<ItemStack> result = list == null ? new ArrayList<>(items.length) : list;
Collections.addAll(result, items);
return result;
});
int itemId = ConfigHandler.loggingItem.getOrDefault(id, -1) + 1;
ConfigHandler.loggingItem.put(id, itemId);
return itemId;
}
private static synchronized void addConsumer(int currentConsumer, Object[] data) {
Consumer.consumer.get(currentConsumer).add(data);
private static boolean queueStandardData(Object[] data, String[] user, Object object, boolean first, long reservation) {
return queueStandardData(data, user, object, first, null, null, reservation);
}
private static synchronized void queueStandardData(int consumerId, int currentConsumer, String[] user, Object object) {
Consumer.consumerUsers.get(currentConsumer).put(consumerId, user);
Consumer.consumerObjects.get(currentConsumer).put(consumerId, object);
Consumer.consumer_id.put(currentConsumer, new Integer[] { Consumer.consumer_id.get(currentConsumer)[0], 0 });
private static synchronized <T> boolean queueStandardData(Object[] data, String[] user, Object object, boolean first, Map<Integer, ? extends Map<Integer, T>> additionalMaps, T additionalData, long reservation) {
boolean rollbackPublication = Process.isRollbackPublication((int) data[1], object);
if (Consumer.isPersistenceHalted()) {
Consumer.completeReservation(reservation, 1);
if (rollbackPublication) {
throw new IllegalStateException("Database persistence halted before rollback state could be queued");
}
return false;
}
int currentConsumer = (int) (reservation >>> 32);
int consumerId = (int) reservation;
boolean published = false;
if (rollbackPublication) {
Consumer.registerRollbackPublications(1);
}
try {
data[0] = consumerId;
if (additionalMaps != null) {
additionalMaps.get(currentConsumer).put(consumerId, additionalData);
}
Consumer.consumerUsers.get(currentConsumer).put(consumerId, user);
Consumer.consumerObjects.get(currentConsumer).put(consumerId, object);
if (first) {
Consumer.consumer.get(currentConsumer).add(0, data);
}
else {
Consumer.consumer.get(currentConsumer).add(data);
}
published = true;
}
finally {
try {
if (!published) {
Consumer.consumer.get(currentConsumer).remove(data);
Consumer.consumerUsers.get(currentConsumer).remove(consumerId);
Consumer.consumerObjects.get(currentConsumer).remove(consumerId);
if (additionalMaps != null) {
additionalMaps.get(currentConsumer).remove(consumerId);
}
if (rollbackPublication) {
Consumer.completeRollbackPublications(1);
}
}
}
finally {
Consumer.completeReservation(reservation, 1);
}
}
return published;
}
private static Location getBlockLocation(Location location) {
@@ -79,17 +179,11 @@ public class Queue {
}
protected static void queueAdvancedBreak(String user, BlockState block, Material type, String blockData, int data, Material breakType, int blockNumber) {
int currentConsumer = Consumer.currentConsumer;
int consumerId = Consumer.newConsumerId(currentConsumer);
addConsumer(currentConsumer, new Object[] { consumerId, Process.BLOCK_BREAK, type, data, breakType, 0, blockNumber, blockData });
queueStandardData(consumerId, currentConsumer, new String[] { user, null }, block);
queueStandardData(new Object[] { null, Process.BLOCK_BREAK, type, data, breakType, 0, blockNumber, blockData }, new String[] { user, null }, block, false, Consumer.reserveConsumer());
}
protected static void queueArtInsert(int id, String name) {
int currentConsumer = Consumer.currentConsumer;
int consumerId = Consumer.newConsumerId(currentConsumer);
addConsumer(currentConsumer, new Object[] { consumerId, Process.ART_INSERT, null, 0, null, 0, id, null });
queueStandardData(consumerId, currentConsumer, new String[] { null, null }, name);
queueStandardData(new Object[] { null, Process.ART_INSERT, null, 0, null, 0, id, null }, new String[] { null, null }, name, false, Consumer.reserveConsumer());
}
protected static void queueBlockBreak(String user, BlockState block, Material type, String blockData, int extraData) {
@@ -114,7 +208,7 @@ public class Queue {
CreatureSpawner mobSpawner = (CreatureSpawner) block;
extraData = EntityUtils.getSpawnerType(mobSpawner.getSpawnedType());
}
else if (type != null && (type == Material.IRON_DOOR || BlockGroup.DOORS.contains(type) || type.equals(Material.SUNFLOWER) || type.equals(Material.LILAC) || type.equals(Material.TALL_GRASS) || type.equals(Material.LARGE_FERN) || type.equals(Material.ROSE_BUSH) || type.equals(Material.PEONY))) { // Double plant
else if (type != null && BlockGroup.LOGGED_BY_LOWER_HALF.contains(type)) { // Double plant
if (block.getBlockData() instanceof Bisected) {
if (((Bisected) block.getBlockData()).getHalf().equals(Half.TOP)) {
if (blockNumber == 5) {
@@ -122,8 +216,8 @@ public class Queue {
}
if (block.getY() > BukkitAdapter.ADAPTER.getMinHeight(block.getWorld())) {
block = block.getWorld().getBlockAt(block.getX(), block.getY() - 1, block.getZ()).getState();
if (type != block.getType()) {
block = block instanceof WorldEditBlockState ? ((WorldEditBlockState) block).getLowerHalf() : block.getWorld().getBlockAt(block.getX(), block.getY() - 1, block.getZ()).getState();
if (block == null || type != block.getType()) {
return;
}
@@ -138,10 +232,7 @@ public class Queue {
}
}
int currentConsumer = Consumer.currentConsumer;
int consumerId = Consumer.newConsumerId(currentConsumer);
addConsumer(currentConsumer, new Object[] { consumerId, Process.BLOCK_BREAK, type, extraData, breakType, 0, blockNumber, blockData });
queueStandardData(consumerId, currentConsumer, new String[] { user, null }, block);
queueStandardData(new Object[] { null, Process.BLOCK_BREAK, type, extraData, breakType, 0, blockNumber, blockData }, new String[] { user, null }, block, false, Consumer.reserveConsumer());
}
protected static void queueBlockPlace(String user, BlockState blockLocation, Material blockType, BlockState blockReplaced, Material forceType, int forceD, int forceData, String blockData) {
@@ -183,10 +274,7 @@ public class Queue {
replacedBlockData = blockReplaced.getBlockData().getAsString();
}
int currentConsumer = Consumer.currentConsumer;
int consumerId = Consumer.newConsumerId(currentConsumer);
addConsumer(currentConsumer, new Object[] { consumerId, Process.BLOCK_PLACE, type, data, replaceType, replaceData, forceData, blockData, replacedBlockData });
queueStandardData(consumerId, currentConsumer, new String[] { user, null }, blockLocation);
queueStandardData(new Object[] { null, Process.BLOCK_PLACE, type, data, replaceType, replaceData, forceData, blockData, replacedBlockData }, new String[] { user, null }, blockLocation, false, Consumer.reserveConsumer());
}
protected static void queueBlockPlaceDelayed(final String user, final Location placed, final Material type, final String blockData, final BlockState replaced, int ticks) {
@@ -254,62 +342,151 @@ public class Queue {
}
protected static void queueContainerBreak(String user, Location location, Material type, ItemStack[] oldInventory) {
int currentConsumer = Consumer.currentConsumer;
int consumerId = Consumer.newConsumerId(currentConsumer);
addConsumer(currentConsumer, new Object[] { consumerId, Process.CONTAINER_BREAK, type, 0, null, 0, 0, null });
Consumer.consumerContainers.get(currentConsumer).put(consumerId, oldInventory);
queueStandardData(consumerId, currentConsumer, new String[] { user, null }, location);
queueStandardData(new Object[] { null, Process.CONTAINER_BREAK, type, 0, null, 0, 0, null }, new String[] { user, null }, location, false, Consumer.consumerContainers, oldInventory, Consumer.reserveConsumer());
}
protected static synchronized void queueContainerTransaction(String user, Location location, Material type, Object inventory, int chestId) {
int currentConsumer = Consumer.currentConsumer;
int consumerId = Consumer.newConsumerId(currentConsumer);
addConsumer(currentConsumer, new Object[] { consumerId, Process.CONTAINER_TRANSACTION, type, 0, null, 0, chestId, null });
Consumer.consumerInventories.get(currentConsumer).put(consumerId, inventory);
queueStandardData(consumerId, currentConsumer, new String[] { user, null }, location);
queueStandardData(new Object[] { null, Process.CONTAINER_TRANSACTION, type, 0, null, 0, chestId, null }, new String[] { user, null }, location, false, Consumer.consumerInventories, inventory, Consumer.reserveConsumer());
}
public static void queueEntityContainerTransaction(String user, java.util.UUID entityUuid, Location currentLocation, ItemStack[] oldContents, ItemStack[] newContents) {
queueEntityContainerTransaction(user, new EntityContainerTransaction(entityUuid, currentLocation, oldContents, newContents));
}
public static void queueEntityContainerTransaction(String user, EntityContainerTransaction transaction) {
if (user == null || user.isEmpty() || transaction == null) {
return;
}
queueStandardData(new Object[] { null, Process.ENTITY_CONTAINER_TRANSACTION, null, 0, null, 0, 0, null }, new String[] { user, null }, transaction, false, Consumer.reserveConsumer());
}
public static void queueEntityInteraction(String user, Entity entity, EntityInteractionAction action) {
queueEntityInteraction(user, entity, action, null);
}
public static void queueEntityInteraction(String user, Entity entity, EntityInteractionAction action, byte[] metadata) {
if (user == null || user.trim().isEmpty() || entity == null || action == null) {
return;
}
Location currentLocation = entity.getLocation();
EntityInteractionOrigin origin = EntitySpawnTracking.getOrCreateInteractionOrigin(entity);
if (origin == null || currentLocation.getWorld() == null) {
return;
}
boolean promotion = EntitySpawnTracking.beginDatabaseIdentityPromotion(entity);
EntityInteraction interaction = new EntityInteraction(entity.getUniqueId(), entity.getType(), origin, currentLocation, action, metadata).withIdentityPromotion(promotion);
queueEntityInteraction(user, interaction);
}
public static void queueEntityInteraction(String user, EntityInteraction interaction) {
if (user == null || user.trim().isEmpty() || interaction == null) {
if (interaction != null && interaction.hasIdentityPromotion()) {
EntitySpawnTracking.cancelDatabaseIdentityPromotion(interaction.getEntityUuid(), interaction.getCurrentLocation());
}
return;
}
queueReservedEntityInteraction(user, interaction, reserveEntityInteractionQueue());
}
protected static long reserveEntityInteractionQueue() {
return Consumer.reserveConsumer();
}
protected static void queueReservedEntityInteraction(String user, EntityInteraction interaction, long reservation) {
boolean queued = false;
try {
if (user == null || user.trim().isEmpty() || interaction == null) {
Consumer.completeReservation(reservation, 1);
return;
}
queued = queueStandardData(new Object[] { null, Process.ENTITY_INTERACTION, null, 0, null, 0, 0, null }, new String[] { user, null }, interaction, false, reservation);
}
finally {
if (!queued && interaction != null && interaction.hasIdentityPromotion()) {
EntitySpawnTracking.cancelDatabaseIdentityPromotion(interaction.getEntityUuid(), interaction.getCurrentLocation());
}
}
}
protected static void queueItemTransaction(String user, Location location, int time, int offset, int itemId) {
int currentConsumer = Consumer.currentConsumer;
int consumerId = Consumer.newConsumerId(currentConsumer);
addConsumer(currentConsumer, new Object[] { consumerId, Process.ITEM_TRANSACTION, null, offset, null, time, itemId, null });
queueStandardData(consumerId, currentConsumer, new String[] { user, null }, location);
queueStandardData(new Object[] { null, Process.ITEM_TRANSACTION, null, offset, null, time, itemId, null }, new String[] { user, null }, location, false, Consumer.reserveConsumer());
}
protected static void queueEntityInsert(int id, String name) {
int currentConsumer = Consumer.currentConsumer;
int consumerId = Consumer.newConsumerId(currentConsumer);
addConsumer(currentConsumer, new Object[] { consumerId, Process.ENTITY_INSERT, null, 0, null, 0, id, null });
queueStandardData(consumerId, currentConsumer, new String[] { null, null }, name);
queueStandardData(new Object[] { null, Process.ENTITY_INSERT, null, 0, null, 0, id, null }, new String[] { null, null }, name, false, Consumer.reserveConsumer());
}
protected static void queueEntityKill(String user, Location location, List<Object> data, EntityType type) {
int currentConsumer = Consumer.currentConsumer;
int consumerId = Consumer.newConsumerId(currentConsumer);
addConsumer(currentConsumer, new Object[] { consumerId, Process.ENTITY_KILL, null, 0, null, 0, 0 });
Consumer.consumerObjectList.get(currentConsumer).put(consumerId, data);
queueStandardData(consumerId, currentConsumer, new String[] { user, null }, new Object[] { getBlockLocation(location), type, null });
queueStandardData(new Object[] { null, Process.ENTITY_KILL, null, 0, null, 0, 0 }, new String[] { user, null }, new Object[] { getBlockLocation(location), type, null }, false, Consumer.consumerObjectList, data, Consumer.reserveConsumer());
}
protected static void queueEntitySpawn(String user, BlockState block, EntityType type, int data) {
int currentConsumer = Consumer.currentConsumer;
int consumerId = Consumer.newConsumerId(currentConsumer);
addConsumer(currentConsumer, new Object[] { consumerId, Process.ENTITY_SPAWN, null, 0, null, 0, data, null });
queueStandardData(consumerId, currentConsumer, new String[] { user, null }, new Object[] { block, type });
queueStandardData(new Object[] { null, Process.ENTITY_SPAWN, null, 0, null, 0, data, null }, new String[] { user, null }, new Object[] { block, type }, false, Consumer.reserveConsumer());
}
public static void queueEntitySpawnLog(String user, java.util.UUID uuid, EntityType type, Location location) {
queueEntitySpawnLog(user, EntitySpawnData.log(uuid, type, location));
}
public static void queueEntitySpawnLog(String user, EntitySpawnData spawnData) {
queueStandardData(new Object[] { null, Process.ENTITY_SPAWN_LOG, null, 0, null, 0, 0, null }, new String[] { user, null }, spawnData, false, Consumer.reserveConsumer());
}
public static void queueEntitySpawnLocation(java.util.UUID uuid, Location location, long verificationEpoch) {
queueEntitySpawnUpdate(EntitySpawnData.location(uuid, location, verificationEpoch));
}
public static void queueEntitySpawnRemoved(java.util.UUID uuid, Location location) {
if (uuid == null || location == null || location.getWorld() == null) {
return;
}
int worldId = WorldUtils.getWorldId(location.getWorld().getName());
if (worldId < 0) {
queueEntitySpawnUpdate(EntitySpawnData.removed(uuid, location));
return;
}
queueEntitySpawnRemoved(uuid, new EntityInteractionOrigin(worldId, location.getX(), location.getY(), location.getZ()), location);
}
public static void queueEntitySpawnRemoved(Entity entity) {
if (entity == null) {
return;
}
Location location = entity.getLocation();
EntityInteractionOrigin origin = EntitySpawnTracking.getOrCreateInteractionOrigin(entity);
if (location.getWorld() == null) {
return;
}
if (origin == null) {
queueEntitySpawnUpdate(EntitySpawnData.removed(entity.getUniqueId(), location));
return;
}
queueEntitySpawnRemoved(entity.getUniqueId(), origin, location);
}
private static void queueEntitySpawnRemoved(java.util.UUID uuid, EntityInteractionOrigin origin, Location location) {
int time = (int) (System.currentTimeMillis() / 1000L);
queueEntitySpawnUpdate(EntitySpawnData.removed(uuid, origin, location, time));
}
public static void queueEntitySpawnRevived(java.util.UUID previousUuid, java.util.UUID uuid, Location location) {
queueEntitySpawnUpdate(EntitySpawnData.revived(previousUuid, uuid, location));
}
public static void queueEntitySpawnUpdate(EntitySpawnData update) {
queueStandardData(new Object[] { null, Process.ENTITY_SPAWN_UPDATE, null, 0, null, 0, 0, null }, new String[] { null, null }, update, false, Consumer.reserveConsumer());
}
public static void queueEntitySpawnUpdateFirst(EntitySpawnData update) {
queueStandardData(new Object[] { null, Process.ENTITY_SPAWN_UPDATE, null, 0, null, 0, 0, null }, new String[] { null, null }, update, true, Consumer.reserveConsumer());
}
protected static void queueMaterialInsert(int id, String name) {
int currentConsumer = Consumer.currentConsumer;
int consumerId = Consumer.newConsumerId(currentConsumer);
addConsumer(currentConsumer, new Object[] { consumerId, Process.MATERIAL_INSERT, null, 0, null, 0, id, null });
queueStandardData(consumerId, currentConsumer, new String[] { null, null }, name);
queueStandardData(new Object[] { null, Process.MATERIAL_INSERT, null, 0, null, 0, id, null }, new String[] { null, null }, name, false, Consumer.reserveConsumer());
}
protected static void queueBlockDataInsert(int id, String data) {
int currentConsumer = Consumer.currentConsumer;
int consumerId = Consumer.newConsumerId(currentConsumer);
addConsumer(currentConsumer, new Object[] { consumerId, Process.BLOCKDATA_INSERT, null, 0, null, 0, id, null });
queueStandardData(consumerId, currentConsumer, new String[] { null, null }, data);
queueStandardData(new Object[] { null, Process.BLOCKDATA_INSERT, null, 0, null, 0, id, null }, new String[] { null, null }, data, false, Consumer.reserveConsumer());
}
protected static void queueNaturalBlockBreak(String user, BlockState block, Block relative, Material type, String blockData, int data) {
@@ -318,57 +495,36 @@ public class Queue {
blockStates.add(relative.getState());
}
int currentConsumer = Consumer.currentConsumer;
int consumerId = Consumer.newConsumerId(currentConsumer);
addConsumer(currentConsumer, new Object[] { consumerId, Process.NATURAL_BLOCK_BREAK, type, data, null, 0, 0, blockData });
Consumer.consumerBlockList.get(currentConsumer).put(consumerId, blockStates);
queueStandardData(consumerId, currentConsumer, new String[] { user, null }, block);
queueStandardData(new Object[] { null, Process.NATURAL_BLOCK_BREAK, type, data, null, 0, 0, blockData }, new String[] { user, null }, block, false, Consumer.consumerBlockList, blockStates, Consumer.reserveConsumer());
}
protected static void queuePlayerChat(Player player, String message, long timestamp) {
int currentConsumer = Consumer.currentConsumer;
int consumerId = Consumer.newConsumerId(currentConsumer);
addConsumer(currentConsumer, new Object[] { consumerId, Process.PLAYER_CHAT, null, 0, null, 0, 0, null });
Consumer.consumerStrings.get(currentConsumer).put(consumerId, message);
queueStandardData(consumerId, currentConsumer, new String[] { player.getName(), null }, new Object[] { timestamp, player.getLocation().clone() });
queueStandardData(new Object[] { null, Process.PLAYER_CHAT, null, 0, null, 0, 0, null }, new String[] { player.getName(), null }, new Object[] { timestamp, player.getLocation().clone() }, false, Consumer.consumerStrings, message, Consumer.reserveConsumer());
}
protected static void queuePlayerCommand(Player player, String message, long timestamp) {
int currentConsumer = Consumer.currentConsumer;
int consumerId = Consumer.newConsumerId(currentConsumer);
addConsumer(currentConsumer, new Object[] { consumerId, Process.PLAYER_COMMAND, null, 0, null, 0, 0, null });
Consumer.consumerStrings.get(currentConsumer).put(consumerId, message);
queueStandardData(consumerId, currentConsumer, new String[] { player.getName(), null }, new Object[] { timestamp, player.getLocation().clone() });
queueStandardData(new Object[] { null, Process.PLAYER_COMMAND, null, 0, null, 0, 0, null }, new String[] { player.getName(), null }, new Object[] { timestamp, player.getLocation().clone() }, false, Consumer.consumerStrings, message, Consumer.reserveConsumer());
}
protected static void queuePlayerInteraction(String user, BlockState block, Material type) {
int currentConsumer = Consumer.currentConsumer;
int consumerId = Consumer.newConsumerId(currentConsumer);
addConsumer(currentConsumer, new Object[] { consumerId, Process.PLAYER_INTERACTION, type, 0, null, 0, 0, null });
queueStandardData(consumerId, currentConsumer, new String[] { user, null }, block);
queuePlayerInteraction(user, block.getLocation(), type, block.getBlockData().getAsString());
}
protected static void queuePlayerInteraction(String user, Location location, Material type, String blockData) {
queueStandardData(new Object[] { null, Process.PLAYER_INTERACTION, type, 0, null, 0, 0, blockData }, new String[] { user, null }, getBlockLocation(location), false, Consumer.reserveConsumer());
}
protected static void queuePlayerKill(String user, Location location, String player) {
int currentConsumer = Consumer.currentConsumer;
int consumerId = Consumer.newConsumerId(currentConsumer);
addConsumer(currentConsumer, new Object[] { consumerId, Process.PLAYER_KILL, null, 0, null, 0, 0, null });
queueStandardData(consumerId, currentConsumer, new String[] { user, null }, new Object[] { getBlockLocation(location), player });
queueStandardData(new Object[] { null, Process.PLAYER_KILL, null, 0, null, 0, 0, null }, new String[] { user, null }, new Object[] { getBlockLocation(location), player }, false, Consumer.reserveConsumer());
}
protected static void queuePlayerLogin(Player player, int time, int configSessions, int configUsernames) {
int currentConsumer = Consumer.currentConsumer;
int consumerId = Consumer.newConsumerId(currentConsumer);
String uuid = player.getUniqueId().toString();
addConsumer(currentConsumer, new Object[] { consumerId, Process.PLAYER_LOGIN, null, configSessions, null, configUsernames, time, null });
Consumer.consumerStrings.get(currentConsumer).put(consumerId, uuid);
queueStandardData(consumerId, currentConsumer, new String[] { player.getName(), uuid }, player.getLocation().clone());
queueStandardData(new Object[] { null, Process.PLAYER_LOGIN, null, configSessions, null, configUsernames, time, null }, new String[] { player.getName(), uuid }, player.getLocation().clone(), false, Consumer.consumerStrings, uuid, Consumer.reserveConsumer());
}
protected static void queuePlayerQuit(Player player, int time) {
int currentConsumer = Consumer.currentConsumer;
int consumerId = Consumer.newConsumerId(currentConsumer);
addConsumer(currentConsumer, new Object[] { consumerId, Process.PLAYER_LOGOUT, null, 0, null, 0, time, null });
queueStandardData(consumerId, currentConsumer, new String[] { player.getName(), null }, player.getLocation().clone());
queueStandardData(new Object[] { null, Process.PLAYER_LOGOUT, null, 0, null, 0, time, null }, new String[] { player.getName(), null }, player.getLocation().clone(), false, Consumer.reserveConsumer());
}
protected static void queueRollbackUpdate(String user, Location location, List<Object[]> list, int table, int action) {
@@ -376,11 +532,130 @@ public class Queue {
location = new Location(Bukkit.getServer().getWorlds().get(0), 0, 0, 0);
}
int currentConsumer = Consumer.currentConsumer;
int consumerId = Consumer.newConsumerId(currentConsumer);
addConsumer(currentConsumer, new Object[] { consumerId, table, null, 0, null, 0, action, null });
Consumer.consumerObjectArrayList.get(currentConsumer).put(consumerId, list);
queueStandardData(consumerId, currentConsumer, new String[] { user, null }, location);
queueRollbackUpdates(user, location, list, table, action, false);
}
public static void queueEntityContainerRollbackUpdate(String user, Location location, List<Object[]> rows, int rollbackType, boolean inventoryRollback) {
if (location == null) {
location = new Location(Bukkit.getServer().getWorlds().get(0), 0, 0, 0);
}
queueRollbackUpdates(user, location, rows, Process.ENTITY_CONTAINER_ROLLBACK_UPDATE, rollbackType, inventoryRollback);
}
public static void queueEntityContainerRollbackUpdate(String user, EntitySpawnData transition, List<Object[]> rows, int rollbackType, boolean inventoryRollback) {
EntityContainerRollbackUpdate update = new EntityContainerRollbackUpdate(user, transition, rows, rollbackType, inventoryRollback);
queueStandardData(new Object[] { null, Process.ENTITY_CONTAINER_TRANSITION_UPDATE, null, 0, null, 0, 0, null }, new String[] { user, null }, update, false, Consumer.reserveConsumer());
}
public static synchronized void queueEntityRetriesFirst(List<EntityContainerRollbackUpdate> containerUpdates, List<EntitySpawnData> spawnUpdates) {
if (Consumer.isPersistenceHalted()) {
return;
}
List<EntityContainerRollbackUpdate> containerRetries = new ArrayList<>(containerUpdates);
List<EntitySpawnData> spawnRetries = new ArrayList<>(spawnUpdates);
int updateCount = containerRetries.size() + spawnRetries.size();
if (updateCount == 0) {
return;
}
List<Object[]> records = new ArrayList<>(updateCount);
long reservation = Consumer.reserveConsumers(updateCount);
int rollbackPublicationCount = containerRetries.size();
for (EntitySpawnData update : spawnRetries) {
if (Process.isRollbackPublication(Process.ENTITY_SPAWN_UPDATE, update)) {
rollbackPublicationCount++;
}
}
Consumer.registerRollbackPublications(rollbackPublicationCount);
boolean published = false;
try {
int currentConsumer = (int) (reservation >>> 32);
int firstConsumerId = (int) reservation;
int updateIndex = 0;
for (EntityContainerRollbackUpdate update : containerRetries) {
int consumerId = firstConsumerId + updateIndex++;
records.add(new Object[] { consumerId, Process.ENTITY_CONTAINER_TRANSITION_UPDATE, null, 0, null, 0, 0, null });
Consumer.consumerUsers.get(currentConsumer).put(consumerId, new String[] { update.getUser(), null });
Consumer.consumerObjects.get(currentConsumer).put(consumerId, update);
}
for (EntitySpawnData update : spawnRetries) {
int consumerId = firstConsumerId + updateIndex++;
records.add(new Object[] { consumerId, Process.ENTITY_SPAWN_UPDATE, null, 0, null, 0, 0, null });
Consumer.consumerUsers.get(currentConsumer).put(consumerId, new String[] { null, null });
Consumer.consumerObjects.get(currentConsumer).put(consumerId, update);
}
Consumer.consumer.get(currentConsumer).addAll(0, records);
published = true;
}
finally {
try {
if (!published) {
int currentConsumer = (int) (reservation >>> 32);
int firstConsumerId = (int) reservation;
Consumer.consumer.get(currentConsumer).removeAll(records);
for (int index = 0; index < updateCount; index++) {
int consumerId = firstConsumerId + index;
Consumer.consumerUsers.get(currentConsumer).remove(consumerId);
Consumer.consumerObjects.get(currentConsumer).remove(consumerId);
}
Consumer.completeRollbackPublications(rollbackPublicationCount);
}
}
finally {
Consumer.completeReservation(reservation, updateCount);
}
}
}
private static synchronized void queueRollbackUpdates(String user, Location location, List<Object[]> rows, int table, int action, boolean inventoryRollback) {
Objects.requireNonNull(rows, "rows");
if (rows.isEmpty()) {
return;
}
int recordCount = ((rows.size() - 1) / MAX_ROLLBACK_ROWS_PER_RECORD) + 1;
long reservation = Consumer.reserveConsumers(recordCount);
int currentConsumer = (int) (reservation >>> 32);
int firstConsumerId = (int) reservation;
List<Object[]> records = new ArrayList<>(recordCount);
boolean registered = false;
boolean published = false;
try {
if (Consumer.isPersistenceHalted()) {
throw new IllegalStateException("Database persistence halted before rollback state could be queued");
}
Consumer.registerRollbackPublications(recordCount);
registered = true;
for (int recordIndex = 0; recordIndex < recordCount; recordIndex++) {
int fromIndex = recordIndex * MAX_ROLLBACK_ROWS_PER_RECORD;
int toIndex = Math.min(fromIndex + MAX_ROLLBACK_ROWS_PER_RECORD, rows.size());
int consumerId = firstConsumerId + recordIndex;
records.add(new Object[] { consumerId, table, null, inventoryRollback ? 1 : 0, null, 0, action, null });
Consumer.consumerUsers.get(currentConsumer).put(consumerId, new String[] { user, null });
Consumer.consumerObjects.get(currentConsumer).put(consumerId, location);
Consumer.consumerObjectArrayList.get(currentConsumer).put(consumerId, new ArrayList<>(rows.subList(fromIndex, toIndex)));
}
Consumer.consumer.get(currentConsumer).addAll(records);
published = true;
}
finally {
try {
if (!published) {
Consumer.consumer.get(currentConsumer).removeAll(records);
for (int recordIndex = 0; recordIndex < recordCount; recordIndex++) {
int consumerId = firstConsumerId + recordIndex;
Consumer.consumerUsers.get(currentConsumer).remove(consumerId);
Consumer.consumerObjects.get(currentConsumer).remove(consumerId);
Consumer.consumerObjectArrayList.get(currentConsumer).remove(consumerId);
}
if (registered) {
Consumer.completeRollbackPublications(recordCount);
}
}
}
finally {
Consumer.completeReservation(reservation, recordCount);
}
}
}
protected static void queueSignText(String user, Location location, int action, int color, int colorSecondary, boolean frontGlowing, boolean backGlowing, boolean isWaxed, boolean isFront, String line1, String line2, String line3, String line4, String line5, String line6, String line7, String line8, int offset) {
@@ -389,39 +664,27 @@ public class Queue {
return;
}
*/
int currentConsumer = Consumer.currentConsumer;
int consumerId = Consumer.newConsumerId(currentConsumer);
addConsumer(currentConsumer, new Object[] { consumerId, Process.SIGN_TEXT, null, color, null, action, offset, null });
Consumer.consumerSigns.get(currentConsumer).put(consumerId, new Object[] { colorSecondary, BlockUtils.getSignData(frontGlowing, backGlowing), isWaxed, isFront, line1, line2, line3, line4, line5, line6, line7, line8 });
queueStandardData(consumerId, currentConsumer, new String[] { user, null }, location);
Object[] signData = new Object[] { colorSecondary, BlockUtils.getSignData(frontGlowing, backGlowing), isWaxed, isFront, line1, line2, line3, line4, line5, line6, line7, line8 };
queueStandardData(new Object[] { null, Process.SIGN_TEXT, null, color, null, action, offset, null }, new String[] { user, null }, location, false, Consumer.consumerSigns, signData, Consumer.reserveConsumer());
}
protected static void queueSignUpdate(String user, BlockState block, int action, int time) {
int currentConsumer = Consumer.currentConsumer;
int consumerId = Consumer.newConsumerId(currentConsumer);
addConsumer(currentConsumer, new Object[] { consumerId, Process.SIGN_UPDATE, null, action, null, 0, time, null });
queueStandardData(consumerId, currentConsumer, new String[] { user, null }, block);
queueStandardData(new Object[] { null, Process.SIGN_UPDATE, null, action, null, 0, time, null }, new String[] { user, null }, block, false, Consumer.reserveConsumer());
}
protected static void queueSkullUpdate(String user, BlockState block, int rowId) {
int currentConsumer = Consumer.currentConsumer;
int consumerId = Consumer.newConsumerId(currentConsumer);
addConsumer(currentConsumer, new Object[] { consumerId, Process.SKULL_UPDATE, null, 0, null, 0, rowId, null });
queueStandardData(consumerId, currentConsumer, new String[] { user, null }, block);
queueStandardData(new Object[] { null, Process.SKULL_UPDATE, null, 0, null, 0, rowId, null }, new String[] { user, null }, block, false, Consumer.reserveConsumer());
}
protected static void queueStructureGrow(String user, BlockState block, List<BlockState> blockList, int replacedListSize) {
int currentConsumer = Consumer.currentConsumer;
int consumerId = Consumer.newConsumerId(currentConsumer);
addConsumer(currentConsumer, new Object[] { consumerId, Process.STRUCTURE_GROWTH, null, 0, null, 0, replacedListSize, null });
Consumer.consumerBlockList.get(currentConsumer).put(consumerId, blockList);
queueStandardData(consumerId, currentConsumer, new String[] { user, null }, block);
queueStandardData(new Object[] { null, Process.STRUCTURE_GROWTH, null, 0, null, 0, replacedListSize, null }, new String[] { user, null }, block, false, Consumer.consumerBlockList, blockList, Consumer.reserveConsumer());
}
protected static void queueWorldInsert(int id, String world) {
int currentConsumer = Consumer.currentConsumer;
int consumerId = Consumer.newConsumerId(currentConsumer);
addConsumer(currentConsumer, new Object[] { consumerId, Process.WORLD_INSERT, null, 0, null, 0, id, null });
queueStandardData(consumerId, currentConsumer, new String[] { null, null }, world);
queueStandardData(new Object[] { null, Process.WORLD_INSERT, null, 0, null, 0, id, null }, new String[] { null, null }, world, false, Consumer.reserveConsumer());
}
protected static boolean tryQueueIdentifierInsert(int action, int id, String value) {
return queueStandardData(new Object[] { null, action, null, 0, null, 0, id, null }, new String[] { null, null }, value, false, Consumer.reserveConsumer());
}
}
@@ -1,9 +1,9 @@
package net.coreprotect.consumer.process;
import java.sql.PreparedStatement;
import java.sql.Statement;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.ConsumerWriteBatch;
import net.coreprotect.database.statement.MaterialStatement;
import net.coreprotect.language.Phrase;
import net.coreprotect.language.Selector;
@@ -11,12 +11,12 @@ import net.coreprotect.utility.Chat;
class ArtInsertProcess {
static void process(PreparedStatement preparedStmt, Statement statement, int batchCount, Object name, int materialId) {
static void process(ConsumerWriteBatch preparedStmt, Statement statement, int batchCount, Object name, int materialId) {
if (name instanceof String) {
String query = "SELECT id FROM " + ConfigHandler.prefix + "art_map WHERE id = '" + materialId + "' LIMIT 0, 1";
boolean hasMaterial = MaterialStatement.hasMaterial(statement, query);
String query = "SELECT id FROM " + ConfigHandler.prefix + "art_map WHERE id = " + materialId + " LIMIT 1 OFFSET 0";
boolean hasMaterial = !ConfigHandler.databaseType.isClickHouse() && MaterialStatement.hasMaterial(statement, query);
if (!hasMaterial) {
MaterialStatement.insert(preparedStmt, batchCount, materialId, (String) name);
MaterialStatement.insert(preparedStmt, ConsumerWriteBatch.ReferenceKind.ART, batchCount, materialId, (String) name);
// validate ID maps to ensure mapping wasn't reloaded from database prior to this insertion completing
ConfigHandler.art.put((String) name, materialId);
@@ -1,6 +1,6 @@
package net.coreprotect.consumer.process;
import java.sql.PreparedStatement;
import net.coreprotect.database.ConsumerWriteBatch;
import java.util.List;
import org.bukkit.Material;
@@ -14,7 +14,7 @@ import net.coreprotect.utility.MaterialUtils;
class BlockBreakProcess {
static void process(PreparedStatement preparedStmt, PreparedStatement preparedStmtSkulls, int batchCount, int processId, int id, Material blockType, int blockDataId, Material replaceType, int forceData, String user, Object object, String blockData) {
static void process(ConsumerWriteBatch preparedStmt, ConsumerWriteBatch preparedStmtSkulls, int batchCount, int processId, int id, Material blockType, int blockDataId, Material replaceType, int forceData, String user, Object object, String blockData) {
if (object instanceof BlockState) {
BlockState block = (BlockState) object;
List<Object> meta = BlockUtils.processMeta(block);
@@ -1,9 +1,9 @@
package net.coreprotect.consumer.process;
import java.sql.PreparedStatement;
import java.sql.Statement;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.ConsumerWriteBatch;
import net.coreprotect.database.statement.MaterialStatement;
import net.coreprotect.language.Phrase;
import net.coreprotect.language.Selector;
@@ -11,12 +11,12 @@ import net.coreprotect.utility.Chat;
class BlockDataInsertProcess {
static void process(PreparedStatement preparedStmt, Statement statement, int batchCount, Object name, int materialId) {
static void process(ConsumerWriteBatch preparedStmt, Statement statement, int batchCount, Object name, int materialId) {
if (name instanceof String) {
String query = "SELECT id FROM " + ConfigHandler.prefix + "blockdata_map WHERE id = '" + materialId + "' LIMIT 0, 1";
boolean hasMaterial = MaterialStatement.hasMaterial(statement, query);
String query = "SELECT id FROM " + ConfigHandler.prefix + "blockdata_map WHERE id = " + materialId + " LIMIT 1 OFFSET 0";
boolean hasMaterial = !ConfigHandler.databaseType.isClickHouse() && MaterialStatement.hasMaterial(statement, query);
if (!hasMaterial) {
MaterialStatement.insert(preparedStmt, batchCount, materialId, (String) name);
MaterialStatement.insert(preparedStmt, ConsumerWriteBatch.ReferenceKind.BLOCK_DATA, batchCount, materialId, (String) name);
// validate ID maps to ensure mapping wasn't reloaded from database prior to this insertion completing
ConfigHandler.blockdata.put((String) name, materialId);
@@ -1,6 +1,6 @@
package net.coreprotect.consumer.process;
import java.sql.PreparedStatement;
import net.coreprotect.database.ConsumerWriteBatch;
import java.util.List;
import org.bukkit.Material;
@@ -13,7 +13,7 @@ import net.coreprotect.utility.MaterialUtils;
class BlockPlaceProcess {
static void process(PreparedStatement preparedStmt, PreparedStatement preparedStmtSkulls, int batchCount, Material blockType, int blockData, Material replaceType, int replaceData, int forceData, String user, Object object, String newBlockData, String replacedBlockData) {
static void process(ConsumerWriteBatch preparedStmt, ConsumerWriteBatch preparedStmtSkulls, int batchCount, Material blockType, int blockData, Material replaceType, int replaceData, int forceData, String user, Object object, String newBlockData, String replacedBlockData) {
if (object instanceof BlockState) {
BlockState block = (BlockState) object;
List<Object> meta = BlockUtils.processMeta(block);
@@ -1,6 +1,6 @@
package net.coreprotect.consumer.process;
import java.sql.PreparedStatement;
import net.coreprotect.database.ConsumerWriteBatch;
import java.util.Map;
import org.bukkit.Location;
@@ -9,17 +9,18 @@ import org.bukkit.inventory.ItemStack;
import net.coreprotect.consumer.Consumer;
import net.coreprotect.database.logger.ContainerBreakLogger;
import net.coreprotect.utility.HopperTransactionUtils;
class ContainerBreakProcess {
static void process(PreparedStatement preparedStmt, int batchCount, int processId, int id, Material type, String user, Object object) {
static void process(ConsumerWriteBatch preparedStmt, int batchCount, int processId, int id, Material type, String user, Object object) {
if (object instanceof Location) {
Location location = (Location) object;
Map<Integer, ItemStack[]> containers = Consumer.consumerContainers.get(processId);
if (containers.get(id) != null) {
ItemStack[] container = containers.get(id);
ContainerBreakLogger.log(preparedStmt, batchCount, user, location, type, container);
containers.remove(id);
String transactionId = HopperTransactionUtils.getTransactionId(location);
HopperTransactionUtils.synchronizeTransaction(transactionId, () -> ContainerBreakLogger.log(preparedStmt, batchCount, user, location, type, container));
}
}
}
@@ -1,13 +1,9 @@
package net.coreprotect.consumer.process;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.bukkit.Location;
import org.bukkit.Material;
@@ -16,81 +12,166 @@ import org.bukkit.inventory.ItemStack;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.consumer.Consumer;
import net.coreprotect.consumer.Queue;
import net.coreprotect.database.ConsumerWriteBatch;
import net.coreprotect.database.logger.ContainerLogger;
import net.coreprotect.model.entity.EntityContainerTransaction;
import net.coreprotect.model.entity.EntitySpawnIdentity;
import net.coreprotect.utility.ErrorReporter;
import net.coreprotect.utility.HopperTransactionUtils;
class ContainerTransactionProcess {
static void process(PreparedStatement preparedStmtContainer, PreparedStatement preparedStmtItems, int batchCount, int processId, int id, Material type, int forceData, String user, Object object) {
if (object instanceof Location) {
Location location = (Location) object;
Map<Integer, Object> inventories = Consumer.consumerInventories.get(processId);
Object inventory = inventories.remove(id);
if (inventory != null) {
String transactingChestId = HopperTransactionUtils.getTransactionId(location);
String loggingChestIdSuffix = HopperTransactionUtils.getLoggingIdSuffix(location);
String loggingChestId = HopperTransactionUtils.getLoggingId(user, loggingChestIdSuffix);
if (ConfigHandler.loggingChest.get(loggingChestId) != null) {
int current_chest = ConfigHandler.loggingChest.get(loggingChestId);
if (ConfigHandler.oldContainer.get(loggingChestId) == null) {
ConfigHandler.removeOldContainerViewer(loggingChestIdSuffix, loggingChestId);
ConfigHandler.loggingChest.remove(loggingChestId);
ConfigHandler.forceContainer.remove(loggingChestId);
HopperTransactionUtils.removeOwner(transactingChestId, loggingChestId);
return;
}
int force_size = 0;
if (ConfigHandler.forceContainer.get(loggingChestId) != null) {
force_size = ConfigHandler.forceContainer.get(loggingChestId).size();
}
if (current_chest == forceData || force_size > 0) { // This prevents client side chest sorting mods from messing things up.
ContainerLogger.log(preparedStmtContainer, preparedStmtItems, batchCount, user, type, inventory, location);
List<ItemStack[]> old = ConfigHandler.oldContainer.get(loggingChestId);
if (old.size() == 0) {
ConfigHandler.oldContainer.remove(loggingChestId);
ConfigHandler.removeOldContainerViewer(loggingChestIdSuffix, loggingChestId);
ConfigHandler.loggingChest.remove(loggingChestId);
ConfigHandler.forceContainer.remove(loggingChestId);
HopperTransactionUtils.removeOwner(transactingChestId, loggingChestId);
}
}
else if (loggingChestId.startsWith("#hopper")) {
if (force_size == 0 && ConfigHandler.oldContainer.getOrDefault(loggingChestId, Collections.synchronizedList(new ArrayList<>())).size() == 1 && HopperTransactionUtils.pendingDeltaCount(transactingChestId) == 0) {
int loopCount = ConfigHandler.loggingChest.getOrDefault(loggingChestId, 0);
int maxInventorySize = (99 * 54);
try {
Inventory checkInventory = (Inventory) inventory;
maxInventorySize = checkInventory.getSize() * checkInventory.getMaxStackSize();
}
catch (Exception e) {
// use default of 5,346
}
static boolean processEntity(ConsumerWriteBatch preparedStmtContainer, int batchCount, String user, Object object, EntitySpawnIdentity identity) throws Exception {
if (!(object instanceof EntityContainerTransaction) || identity == null) {
return false;
}
if (loopCount > maxInventorySize) {
ItemStack[] destinationContents = null;
ItemStack movedItem = null;
if (ConfigHandler.databaseType.isColumnar()) {
preparedStmtContainer.executeAtomically("entity_container_transaction", () -> ContainerLogger.logEntity(preparedStmtContainer, batchCount, user, identity, (EntityContainerTransaction) object));
}
else {
try {
ContainerLogger.logEntity(preparedStmtContainer, batchCount, user, identity, (EntityContainerTransaction) object);
}
catch (Exception e) {
ErrorReporter.report(e);
}
}
return true;
}
String hopperPush = "#hopper-push." + location.getBlockX() + "." + location.getBlockY() + "." + location.getBlockZ();
Object[] hopperPushData = ConfigHandler.hopperSuccess.remove(hopperPush);
if (hopperPushData != null) {
destinationContents = (ItemStack[]) hopperPushData[0];
movedItem = (ItemStack) hopperPushData[1];
}
static void process(ConsumerWriteBatch preparedStmtContainer, ConsumerWriteBatch preparedStmtItems, int batchCount, int processId, int id, Material type, int forceData, String user, Object object) {
if (object instanceof ContainerLogger.PreparedTransaction) {
((ContainerLogger.PreparedTransaction) object).log(preparedStmtContainer, preparedStmtItems, batchCount, user);
return;
}
if (!(object instanceof Location)) {
return;
}
if (destinationContents != null) {
Set<ItemStack> movedItems = new HashSet<>();
Object[] lastAbort = ConfigHandler.hopperAbort.get(hopperPush);
if (lastAbort != null && Arrays.equals(destinationContents, (ItemStack[]) lastAbort[1])) {
((Set<?>) lastAbort[0]).forEach(itemStack -> movedItems.add((ItemStack) itemStack));
}
movedItems.add(movedItem);
ConfigHandler.hopperAbort.put(hopperPush, new Object[] { movedItems, destinationContents });
}
}
}
Location location = (Location) object;
Map<Integer, Object> inventories = Consumer.consumerInventories.get(processId);
Object inventory = inventories.get(id);
if (inventory == null) {
return;
}
String transactingChestId = HopperTransactionUtils.getTransactionId(location);
String loggingChestIdSuffix = HopperTransactionUtils.getLoggingIdSuffix(location);
String loggingChestId = HopperTransactionUtils.getLoggingId(user, loggingChestIdSuffix);
HopperTransactionUtils.synchronizeTransaction(transactingChestId,
() -> processTransaction(preparedStmtContainer, preparedStmtItems, batchCount, processId, id, type, forceData, user, inventory, location, transactingChestId, loggingChestIdSuffix, loggingChestId));
}
private static void processTransaction(ConsumerWriteBatch preparedStmtContainer, ConsumerWriteBatch preparedStmtItems, int batchCount, int processId, int id, Material type, int forceData, String user, Object inventory,
Location location, String transactingChestId, String loggingChestIdSuffix, String loggingChestId) {
if (ConfigHandler.loggingChest.get(loggingChestId) == null) {
return;
}
int current_chest = ConfigHandler.loggingChest.get(loggingChestId);
if (ConfigHandler.oldContainer.get(loggingChestId) == null) {
clearContainerTransaction(transactingChestId, loggingChestIdSuffix, loggingChestId);
return;
}
int force_size = Queue.getForceContainerSize(loggingChestId);
if (current_chest == forceData || force_size > 0) { // This prevents client side chest sorting mods from messing things up.
try {
if (ConfigHandler.databaseType.isColumnar()) {
ContainerLogger.log(preparedStmtContainer, preparedStmtItems, batchCount, user, type, inventory, location,
prepared -> Consumer.consumerObjects.get(processId).put(id, prepared));
}
else {
ContainerLogger.log(preparedStmtContainer, preparedStmtItems, batchCount, user, type, inventory, location);
}
}
finally {
List<ItemStack[]> old = ConfigHandler.oldContainer.get(loggingChestId);
if (old == null || old.isEmpty()) {
clearContainerTransaction(transactingChestId, loggingChestIdSuffix, loggingChestId);
}
}
}
else if (loggingChestId.startsWith("#hopper")) {
if (force_size == 0 && ConfigHandler.oldContainer.getOrDefault(loggingChestId, Collections.synchronizedList(new ArrayList<>())).size() == 1
&& HopperTransactionUtils.pendingDeltaCount(transactingChestId) == 0) {
int loopCount = ConfigHandler.loggingChest.getOrDefault(loggingChestId, 0);
int maxInventorySize = (99 * 54);
try {
Inventory checkInventory = (Inventory) inventory;
maxInventorySize = checkInventory.getSize() * checkInventory.getMaxStackSize();
}
catch (Exception e) {
// use default of 5,346
}
if (loopCount > maxInventorySize) {
ItemStack[] destinationContents = null;
ItemStack movedItem = null;
String hopperPush = HopperTransactionUtils.getHopperPushId(location);
Object[] hopperPushData = ConfigHandler.hopperSuccess.remove(hopperPush);
if (hopperPushData != null) {
destinationContents = (ItemStack[]) hopperPushData[0];
movedItem = (ItemStack) hopperPushData[1];
}
if (destinationContents != null) {
Object[] lastAbort = ConfigHandler.hopperAbort.get(hopperPush);
ConfigHandler.hopperAbort.put(hopperPush, HopperTransactionUtils.createAbortState(lastAbort, destinationContents, movedItem));
}
}
}
}
}
private static void clearContainerTransaction(String transactionId, String locationSuffix, String loggingId) {
ConfigHandler.oldContainer.remove(loggingId);
ConfigHandler.removeOldContainerViewer(locationSuffix, loggingId);
ConfigHandler.loggingChest.remove(loggingId);
Queue.removeForceContainer(loggingId);
HopperTransactionUtils.removeOwner(transactionId, loggingId);
}
static void discard(int processId, int failedIndex, int forceData, String user, Location location) {
if (location.getWorld() == null) {
return;
}
String transactionId = HopperTransactionUtils.getTransactionId(location);
String locationSuffix = HopperTransactionUtils.getLoggingIdSuffix(location);
String loggingId = HopperTransactionUtils.getLoggingId(user, locationSuffix);
HopperTransactionUtils.synchronizeTransaction(transactionId, () -> {
List<ItemStack[]> old = ConfigHandler.oldContainer.get(loggingId);
if (old == null || old.isEmpty()) {
clearContainerTransaction(transactionId, locationSuffix, loggingId);
return;
}
Integer generation = ConfigHandler.loggingChest.get(loggingId);
int forceSize = Queue.getForceContainerSize(loggingId);
int snapshotIndex = 0;
List<Object[]> queued = Consumer.consumer.get(processId);
for (int index = 0; index < failedIndex; index++) {
if (Process.inventoryTransactionGeneration(processId, queued.get(index), Process.CONTAINER_TRANSACTION, loggingId) != null) {
snapshotIndex++;
}
}
if (snapshotIndex >= forceSize) {
if (generation == null || generation != forceData) {
return;
}
snapshotIndex = forceSize;
}
if (snapshotIndex < old.size()) {
old.remove(snapshotIndex);
if (snapshotIndex < forceSize) {
Queue.pollForceContainer(loggingId, snapshotIndex);
}
HopperTransactionUtils.consumeSnapshot(transactionId, loggingId, snapshotIndex);
}
if (old.isEmpty()) {
clearContainerTransaction(transactionId, locationSuffix, loggingId);
}
});
}
}
@@ -1,9 +1,9 @@
package net.coreprotect.consumer.process;
import java.sql.PreparedStatement;
import java.sql.Statement;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.ConsumerWriteBatch;
import net.coreprotect.database.statement.MaterialStatement;
import net.coreprotect.language.Phrase;
import net.coreprotect.language.Selector;
@@ -11,12 +11,12 @@ import net.coreprotect.utility.Chat;
class EntityInsertProcess {
static void process(PreparedStatement preparedStmt, Statement statement, int batchCount, Object name, int materialId) {
static void process(ConsumerWriteBatch preparedStmt, Statement statement, int batchCount, Object name, int materialId) {
if (name instanceof String) {
String query = "SELECT id FROM " + ConfigHandler.prefix + "entity_map WHERE id = '" + materialId + "' LIMIT 0, 1";
boolean hasMaterial = MaterialStatement.hasMaterial(statement, query);
String query = "SELECT id FROM " + ConfigHandler.prefix + "entity_map WHERE id = " + materialId + " LIMIT 1 OFFSET 0";
boolean hasMaterial = !ConfigHandler.databaseType.isClickHouse() && MaterialStatement.hasMaterial(statement, query);
if (!hasMaterial) {
MaterialStatement.insert(preparedStmt, batchCount, materialId, (String) name);
MaterialStatement.insert(preparedStmt, ConsumerWriteBatch.ReferenceKind.ENTITY, batchCount, materialId, (String) name);
// validate ID maps to ensure mapping wasn't reloaded from database prior to this insertion completing
ConfigHandler.entities.put((String) name, materialId);
@@ -1,6 +1,6 @@
package net.coreprotect.consumer.process;
import java.sql.PreparedStatement;
import net.coreprotect.database.ConsumerWriteBatch;
import java.util.List;
import java.util.Map;
@@ -14,7 +14,7 @@ import net.coreprotect.utility.EntityUtils;
class EntityKillProcess {
static void process(PreparedStatement preparedStmt, PreparedStatement preparedStmtEntities, int batchCount, int processId, int id, Object object, String user) {
static void process(ConsumerWriteBatch preparedStmt, ConsumerWriteBatch preparedStmtEntities, ConsumerWriteBatch preparedStmtEntityKillLinks, int batchCount, int processId, int id, Object object, String user) {
if (object instanceof Object[]) {
Object[] values = (Object[]) object;
if (values.length <= 1 || !isLocationData(values[0]) || !(values[1] instanceof EntityType)) {
@@ -27,8 +27,7 @@ class EntityKillProcess {
if (objectLists != null && objectLists.get(id) != null) {
List<Object> objectList = objectLists.get(id);
int entityId = EntityUtils.getEntityId(type);
EntityKillLogger.log(preparedStmt, preparedStmtEntities, batchCount, user, location, objectList, entityId);
objectLists.remove(id);
EntityKillLogger.log(preparedStmt, preparedStmtEntities, preparedStmtEntityKillLinks, batchCount, user, location, objectList, entityId);
}
}
}
@@ -0,0 +1,21 @@
package net.coreprotect.consumer.process;
import net.coreprotect.database.ConsumerWriteBatch;
import net.coreprotect.database.logger.EntitySpawnLogger;
import net.coreprotect.model.entity.EntitySpawnData;
import net.coreprotect.model.entity.EntitySpawnIdentity;
final class EntitySpawnLogProcess {
private EntitySpawnLogProcess() {
}
static EntitySpawnIdentity process(ConsumerWriteBatch batch, Object object, String user, EntitySpawnIdentity identity) throws Exception {
if (object instanceof EntitySpawnData) {
EntitySpawnData data = (EntitySpawnData) object;
return EntitySpawnLogger.logIdentity(batch, user, data, identity);
}
return null;
}
}
@@ -16,7 +16,7 @@ class EntitySpawnProcess {
if (object instanceof Object[]) {
BlockState block = (BlockState) ((Object[]) object)[0];
EntityType type = (EntityType) ((Object[]) object)[1];
String query = "SELECT data FROM " + ConfigHandler.prefix + "entity WHERE rowid='" + rowId + "' LIMIT 0, 1";
String query = "SELECT data FROM " + ConfigHandler.prefix + "entity WHERE rowid=" + rowId + " LIMIT 1 OFFSET 0";
List<Object> data = EntityStatement.getData(statement, block, query);
EntityUtil.spawnEntity(block, type, data);
}
@@ -0,0 +1,50 @@
package net.coreprotect.consumer.process;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.LongSupplier;
import java.util.logging.Logger;
final class EntityUuidConflictReporter {
private static final long INTERVAL_NANOS = TimeUnit.MINUTES.toNanos(5);
private final LongSupplier clock;
private final Consumer<String> warning;
private boolean warned;
private long lastWarning;
private long interactions;
private long removals;
private UUID lastUuid;
EntityUuidConflictReporter() {
this(System::nanoTime, message -> Logger.getLogger("CoreProtect").warning("[CoreProtect] " + message));
}
EntityUuidConflictReporter(LongSupplier clock, Consumer<String> warning) {
this.clock = clock;
this.warning = warning;
}
synchronized void record(boolean removal, UUID uuid) {
if (removal) removals++;
else interactions++;
lastUuid = uuid;
flush();
}
synchronized void flush() {
if (interactions == 0 && removals == 0) return;
long now = clock.getAsLong();
if (warned && now - lastWarning < INTERVAL_NANOS) return;
String skipped = interactions == 0 ? "" : interactions + " entity interaction record(s)";
if (removals > 0) skipped += (skipped.isEmpty() ? "" : " and ") + removals + " entity removal record(s)";
warning.accept("Skipped " + skipped + " due to unresolved DuckDB UUID conflicts. "
+ "These records were not saved; consumer processing continues. Last UUID: " + lastUuid
+ ". Repeated warnings are summarized every 5 minutes.");
warned = true;
lastWarning = now;
interactions = 0;
removals = 0;
lastUuid = null;
}
}
@@ -1,40 +1,42 @@
package net.coreprotect.consumer.process;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.Locale;
import java.util.Map;
import org.bukkit.Location;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.consumer.Consumer;
import net.coreprotect.consumer.Queue;
import net.coreprotect.database.ConsumerWriteBatch;
import net.coreprotect.database.logger.ItemLogger;
class ItemTransactionProcess extends Queue {
static void process(PreparedStatement preparedStmt, int batchCount, int processId, int id, int forceData, int time, int offset, String user, Object object) {
static void process(ConsumerWriteBatch preparedStmt, int batchCount, int processId, int id, int forceData, int time, int offset, String user, Object object) {
if (object instanceof ItemLogger.PreparedTransaction) {
((ItemLogger.PreparedTransaction) object).log(preparedStmt, batchCount, user);
return;
}
if (object instanceof Location) {
Location location = (Location) object;
String loggingItemId = user.toLowerCase(Locale.ROOT) + "." + location.getBlockX() + "." + location.getBlockY() + "." + location.getBlockZ();
String loggingItemId = getLoggingId(user, location);
if (ConfigHandler.loggingItem.get(loggingItemId) != null) {
int current_chest = ConfigHandler.loggingItem.get(loggingItemId);
if (ConfigHandler.itemsPickup.get(loggingItemId) == null && ConfigHandler.itemsDrop.get(loggingItemId) == null && ConfigHandler.itemsThrown.get(loggingItemId) == null && ConfigHandler.itemsShot.get(loggingItemId) == null && ConfigHandler.itemsBreak.get(loggingItemId) == null && ConfigHandler.itemsDestroy.get(loggingItemId) == null && ConfigHandler.itemsCreate.get(loggingItemId) == null && ConfigHandler.itemsSell.get(loggingItemId) == null && ConfigHandler.itemsBuy.get(loggingItemId) == null) {
ConfigHandler.loggingItem.remove(loggingItemId, forceData);
return;
}
if (current_chest == forceData) {
int currentTime = (int) (System.currentTimeMillis() / 1000L);
if (currentTime > time) {
ItemLogger.log(preparedStmt, batchCount, location, offset, user);
ConfigHandler.itemsPickup.remove(loggingItemId);
ConfigHandler.itemsDrop.remove(loggingItemId);
ConfigHandler.itemsThrown.remove(loggingItemId);
ConfigHandler.itemsShot.remove(loggingItemId);
ConfigHandler.itemsBreak.remove(loggingItemId);
ConfigHandler.itemsDestroy.remove(loggingItemId);
ConfigHandler.itemsCreate.remove(loggingItemId);
ConfigHandler.itemsSell.remove(loggingItemId);
ConfigHandler.itemsBuy.remove(loggingItemId);
ConfigHandler.loggingItem.remove(loggingItemId);
// prepare takes the pending lists, so a retried batch has to log from the stored copy
ItemLogger.PreparedTransaction prepared = ItemLogger.prepare(location, offset, user);
Consumer.consumerObjects.get(processId).put(id, prepared);
ConfigHandler.loggingItem.remove(loggingItemId, forceData);
prepared.log(preparedStmt, batchCount, user);
}
else {
Queue.queueItemTransaction(user, location, time, offset, forceData);
@@ -43,4 +45,46 @@ class ItemTransactionProcess extends Queue {
}
}
}
static String getLoggingId(String user, Location location) {
return user.toLowerCase(Locale.ROOT) + "." + location.getBlockX() + "." + location.getBlockY() + "." + location.getBlockZ();
}
static void discard(int forceData, String user, Location location) {
String loggingId = getLoggingId(user, location);
synchronized (Queue.class) {
Integer generation = ConfigHandler.loggingItem.get(loggingId);
if (generation == null || generation != forceData) {
return;
}
Integer retainedGeneration = null;
for (Map.Entry<Integer, ArrayList<Object[]>> queue : Consumer.consumer.entrySet()) {
for (Object[] data : queue.getValue()) {
Integer candidate = Process.inventoryTransactionGeneration(queue.getKey(), data, Process.ITEM_TRANSACTION, loggingId);
if (candidate != null && (retainedGeneration == null || candidate > retainedGeneration)) {
retainedGeneration = candidate;
}
}
}
if (retainedGeneration == null) {
clearItemTransaction(loggingId);
}
else {
ConfigHandler.loggingItem.put(loggingId, retainedGeneration);
}
}
}
private static void clearItemTransaction(String loggingId) {
ConfigHandler.itemsPickup.remove(loggingId);
ConfigHandler.itemsDrop.remove(loggingId);
ConfigHandler.itemsThrown.remove(loggingId);
ConfigHandler.itemsShot.remove(loggingId);
ConfigHandler.itemsBreak.remove(loggingId);
ConfigHandler.itemsDestroy.remove(loggingId);
ConfigHandler.itemsCreate.remove(loggingId);
ConfigHandler.itemsSell.remove(loggingId);
ConfigHandler.itemsBuy.remove(loggingId);
ConfigHandler.loggingItem.remove(loggingId);
}
}
@@ -1,9 +1,9 @@
package net.coreprotect.consumer.process;
import java.sql.PreparedStatement;
import java.sql.Statement;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.ConsumerWriteBatch;
import net.coreprotect.database.statement.MaterialStatement;
import net.coreprotect.language.Phrase;
import net.coreprotect.language.Selector;
@@ -11,12 +11,12 @@ import net.coreprotect.utility.Chat;
class MaterialInsertProcess {
static void process(PreparedStatement preparedStmt, Statement statement, int batchCount, Object name, int materialId) {
static void process(ConsumerWriteBatch preparedStmt, Statement statement, int batchCount, Object name, int materialId) {
if (name instanceof String) {
String query = "SELECT id FROM " + ConfigHandler.prefix + "material_map WHERE id = '" + materialId + "' LIMIT 0, 1";
boolean hasMaterial = MaterialStatement.hasMaterial(statement, query);
String query = "SELECT id FROM " + ConfigHandler.prefix + "material_map WHERE id = " + materialId + " LIMIT 1 OFFSET 0";
boolean hasMaterial = !ConfigHandler.databaseType.isClickHouse() && MaterialStatement.hasMaterial(statement, query);
if (!hasMaterial) {
MaterialStatement.insert(preparedStmt, batchCount, materialId, (String) name);
MaterialStatement.insert(preparedStmt, ConsumerWriteBatch.ReferenceKind.MATERIAL, batchCount, materialId, (String) name);
// validate ID maps to ensure mapping wasn't reloaded from database prior to this insertion completing
ConfigHandler.materials.put((String) name, materialId);
@@ -1,6 +1,6 @@
package net.coreprotect.consumer.process;
import java.sql.PreparedStatement;
import net.coreprotect.database.ConsumerWriteBatch;
import java.sql.Statement;
import java.util.List;
import java.util.Map;
@@ -12,11 +12,10 @@ import net.coreprotect.consumer.Consumer;
import net.coreprotect.database.Lookup;
import net.coreprotect.database.logger.BlockBreakLogger;
import net.coreprotect.utility.MaterialUtils;
import net.coreprotect.utility.Util;
class NaturalBlockBreakProcess {
static void process(Statement statement, PreparedStatement preparedStmt, int batchCount, int processId, int id, String user, Object object, Material blockType, int blockData, String overrideData) {
static void process(Statement statement, ConsumerWriteBatch preparedStmt, int batchCount, int processId, int id, String user, Object object, Material blockType, int blockData, String overrideData) {
if (object instanceof BlockState) {
BlockState block = (BlockState) object;
Map<Integer, List<BlockState>> blockLists = Consumer.consumerBlockList.get(processId);
@@ -28,7 +27,6 @@ class NaturalBlockBreakProcess {
user = removed;
}
}
blockLists.remove(id);
BlockBreakLogger.log(preparedStmt, batchCount, user, block.getLocation(), MaterialUtils.getBlockId(blockType), blockData, null, block.getBlockData().getAsString(), overrideData);
}
}
@@ -1,6 +1,6 @@
package net.coreprotect.consumer.process;
import java.sql.PreparedStatement;
import net.coreprotect.database.ConsumerWriteBatch;
import java.util.Map;
import org.bukkit.Location;
@@ -10,7 +10,7 @@ import net.coreprotect.database.logger.ChatLogger;
class PlayerChatProcess {
static void process(PreparedStatement preparedStmt, int batchCount, int processId, int id, Object object, String user) {
static void process(ConsumerWriteBatch preparedStmt, int batchCount, int processId, int id, Object object, String user) {
if (!(object instanceof Object[])) {
return;
}
@@ -23,7 +23,6 @@ class PlayerChatProcess {
Long timestamp = (Long) data[0];
Location location = (Location) data[1];
ChatLogger.log(preparedStmt, batchCount, timestamp, location, user, message);
strings.remove(id);
}
}
}
@@ -1,6 +1,6 @@
package net.coreprotect.consumer.process;
import java.sql.PreparedStatement;
import net.coreprotect.database.ConsumerWriteBatch;
import java.util.Map;
import org.bukkit.Location;
@@ -10,7 +10,7 @@ import net.coreprotect.database.logger.CommandLogger;
class PlayerCommandProcess {
static void process(PreparedStatement preparedStmt, int batchCount, int processId, int id, Object object, String user) {
static void process(ConsumerWriteBatch preparedStmt, int batchCount, int processId, int id, Object object, String user) {
if (!(object instanceof Object[])) {
return;
}
@@ -23,7 +23,6 @@ class PlayerCommandProcess {
Long timestamp = (Long) data[0];
Location location = (Location) data[1];
CommandLogger.log(preparedStmt, batchCount, timestamp, location, user, message);
strings.remove(id);
}
}
}
@@ -1,18 +1,18 @@
package net.coreprotect.consumer.process;
import java.sql.PreparedStatement;
import net.coreprotect.database.ConsumerWriteBatch;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.BlockState;
import net.coreprotect.database.logger.PlayerInteractLogger;
class PlayerInteractionProcess {
static void process(PreparedStatement preparedStmt, int batchCount, String user, Object object, Material type) {
if (object instanceof BlockState) {
BlockState block = (BlockState) object;
PlayerInteractLogger.log(preparedStmt, batchCount, user, block, type);
static void process(ConsumerWriteBatch preparedStmt, int batchCount, String user, Object object, Material type, String blockData) {
if (object instanceof Location) {
Location location = (Location) object;
PlayerInteractLogger.log(preparedStmt, batchCount, user, location, type, blockData);
}
}
}
@@ -1,6 +1,6 @@
package net.coreprotect.consumer.process;
import java.sql.PreparedStatement;
import net.coreprotect.database.ConsumerWriteBatch;
import org.bukkit.Location;
import org.bukkit.block.BlockState;
@@ -9,7 +9,7 @@ import net.coreprotect.database.logger.PlayerKillLogger;
class PlayerKillProcess {
static void process(PreparedStatement preparedStmt, int batchCount, int id, Object object, String user) {
static void process(ConsumerWriteBatch preparedStmt, int batchCount, int id, Object object, String user) {
if (object instanceof Object[]) {
Object[] values = (Object[]) object;
if (values.length <= 1 || !isLocationData(values[0]) || !(values[1] instanceof String)) {
@@ -1,29 +1,27 @@
package net.coreprotect.consumer.process;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Map;
import org.bukkit.Location;
import net.coreprotect.consumer.Consumer;
import net.coreprotect.database.ConsumerWriteBatch;
import net.coreprotect.database.logger.PlayerSessionLogger;
import net.coreprotect.database.logger.UsernameLogger;
import net.coreprotect.model.action.SessionActions;
class PlayerLoginProcess {
static void process(Connection connection, PreparedStatement preparedStmt, int batchCount, int processId, int id, Object object, int configSessions, int configUsernames, int time, String user) {
static void process(ConsumerWriteBatch batch, int batchCount, int processId, int id, Object object, int configSessions, int configUsernames, int time, String user) {
if (object instanceof Location) {
Map<Integer, String> strings = Consumer.consumerStrings.get(processId);
if (strings.get(id) != null) {
String uuid = strings.get(id);
Location location = (Location) object;
UsernameLogger.log(connection, user, uuid, configUsernames, time);
UsernameLogger.log(batch, user, uuid, configUsernames, time);
if (configSessions == 1) {
PlayerSessionLogger.log(preparedStmt, batchCount, user, location, time, SessionActions.LOGIN);
PlayerSessionLogger.log(batch, batchCount, user, location, time, SessionActions.LOGIN);
}
strings.remove(id);
}
}
}
@@ -1,6 +1,6 @@
package net.coreprotect.consumer.process;
import java.sql.PreparedStatement;
import net.coreprotect.database.ConsumerWriteBatch;
import org.bukkit.Location;
@@ -9,7 +9,7 @@ import net.coreprotect.model.action.SessionActions;
class PlayerLogoutProcess {
static void process(PreparedStatement preparedStmt, int batchCount, Object object, int time, String user) {
static void process(ConsumerWriteBatch preparedStmt, int batchCount, Object object, int time, String user) {
if (object instanceof Location) {
Location location = (Location) object;
PlayerSessionLogger.log(preparedStmt, batchCount, user, location, time, SessionActions.LOGOUT);
File diff suppressed because it is too large. Load diff
@@ -1,6 +1,5 @@
package net.coreprotect.consumer.process;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -8,30 +7,44 @@ import java.util.Map;
import java.util.Map.Entry;
import net.coreprotect.consumer.Consumer;
import net.coreprotect.database.Database;
import net.coreprotect.database.ConsumerWriteBatch;
import net.coreprotect.model.rollback.RollbackUpdateTargets;
import net.coreprotect.utility.MaterialUtils;
class RollbackUpdateProcess {
static void process(Statement statement, int processId, int id, int action, int table) {
static void process(ConsumerWriteBatch batch, int processId, int id, int action, int table) throws Exception {
process(batch, processId, id, action, table, RollbackUpdateTargets.usesInventoryRollbackState(table));
}
static void process(ConsumerWriteBatch batch, int processId, int id, int action, int table, boolean inventoryRollback) throws Exception {
Map<Integer, List<Object[]>> updateLists = Consumer.consumerObjectArrayList.get(processId);
if (updateLists.get(id) != null) {
List<Object[]> list = updateLists.get(id);
boolean inventoryState = RollbackUpdateTargets.usesInventoryRollbackState(table);
Map<Integer, List<Long>> rowIdsByValue = new HashMap<>();
for (Object[] listRow : list) {
long rowid = (Long) listRow[0];
int rolledBack = (Integer) listRow[9];
if (MaterialUtils.rolledBack(rolledBack, inventoryState) == action) { // 1 = restore, 0 = rollback
int toggledValue = MaterialUtils.toggleRolledBack(rolledBack, inventoryState);
rowIdsByValue.computeIfAbsent(toggledValue, key -> new ArrayList<>()).add(rowid);
}
}
for (Entry<Integer, List<Long>> entry : rowIdsByValue.entrySet()) {
Database.performRolledBackUpdate(statement, entry.getKey(), entry.getValue(), table);
}
updateLists.remove(id);
process(batch, list, action, table, inventoryRollback);
}
}
static void process(ConsumerWriteBatch batch, List<Object[]> list, int action, int table, boolean inventoryRollback) throws Exception {
Map<Integer, List<Object[]>> rowsByValue = groupRowsByValue(list, action, inventoryRollback);
for (Entry<Integer, List<Object[]>> entry : rowsByValue.entrySet()) {
batch.updateRolledBackRows(table, entry.getKey(), entry.getValue());
}
}
static void processChecked(ConsumerWriteBatch batch, List<Object[]> list, int action, int table, boolean inventoryRollback) throws Exception {
process(batch, list, action, table, inventoryRollback);
}
private static Map<Integer, List<Object[]>> groupRowsByValue(List<Object[]> list, int action, boolean inventoryRollback) {
Map<Integer, List<Object[]>> rowsByValue = new HashMap<>();
for (Object[] listRow : list) {
int rolledBack = (Integer) listRow[9];
if (MaterialUtils.rolledBack(rolledBack, inventoryRollback) == action) { // 1 = restore, 0 = rollback
int toggledValue = MaterialUtils.toggleRolledBack(rolledBack, inventoryRollback);
rowsByValue.computeIfAbsent(toggledValue, key -> new ArrayList<>()).add(listRow);
}
}
return rowsByValue;
}
}
@@ -1,6 +1,6 @@
package net.coreprotect.consumer.process;
import java.sql.PreparedStatement;
import net.coreprotect.database.ConsumerWriteBatch;
import java.util.Map;
import org.bukkit.Location;
@@ -10,14 +10,13 @@ import net.coreprotect.database.logger.SignTextLogger;
class SignTextProcess {
static void process(PreparedStatement preparedStmt, int batchCount, int processId, int id, int forceData, String user, Object object, int action, int color) {
static void process(ConsumerWriteBatch preparedStmt, int batchCount, int processId, int id, int forceData, String user, Object object, int action, int color) {
if (object instanceof Location) {
Location location = (Location) object;
Map<Integer, Object[]> signs = Consumer.consumerSigns.get(processId);
if (signs.get(id) != null) {
Object[] SIGN_DATA = signs.get(id);
SignTextLogger.log(preparedStmt, batchCount, user, location, action, color, (Integer) SIGN_DATA[0], (Integer) SIGN_DATA[1], (Boolean) SIGN_DATA[2], (Boolean) SIGN_DATA[3], (String) SIGN_DATA[4], (String) SIGN_DATA[5], (String) SIGN_DATA[6], (String) SIGN_DATA[7], (String) SIGN_DATA[8], (String) SIGN_DATA[9], (String) SIGN_DATA[10], (String) SIGN_DATA[11], forceData);
signs.remove(id);
}
}
}
@@ -27,10 +27,10 @@ class SignUpdateProcess {
int userid = ConfigHandler.playerIdCache.get(user.toLowerCase(Locale.ROOT));
String query = "";
if (action == 0) {
query = "SELECT color, color_secondary, data, waxed, face, line_1, line_2, line_3, line_4, line_5, line_6, line_7, line_8 FROM " + ConfigHandler.prefix + "sign WHERE user='" + userid + "' AND wid='" + wid + "' AND x='" + x + "' AND z='" + z + "' AND y='" + y + "' AND time < '" + time + "' ORDER BY rowid DESC LIMIT 0, 1";
query = "SELECT color, color_secondary, data, waxed, face, line_1, line_2, line_3, line_4, line_5, line_6, line_7, line_8 FROM " + ConfigHandler.prefix + "sign WHERE " + ConfigHandler.databaseType.getUserColumn() + "=" + userid + " AND wid=" + wid + " AND x=" + x + " AND z=" + z + " AND y=" + y + " AND time < " + time + " ORDER BY " + ConfigHandler.getDescendingEventOrder() + " LIMIT 1 OFFSET 0";
}
else {
query = "SELECT color, color_secondary, data, waxed, face, line_1, line_2, line_3, line_4, line_5, line_6, line_7, line_8 FROM " + ConfigHandler.prefix + "sign WHERE user='" + userid + "' AND wid='" + wid + "' AND x='" + x + "' AND z='" + z + "' AND y='" + y + "' AND time >= '" + time + "' ORDER BY rowid ASC LIMIT 0, 1";
query = "SELECT color, color_secondary, data, waxed, face, line_1, line_2, line_3, line_4, line_5, line_6, line_7, line_8 FROM " + ConfigHandler.prefix + "sign WHERE " + ConfigHandler.databaseType.getUserColumn() + "=" + userid + " AND wid=" + wid + " AND x=" + x + " AND z=" + z + " AND y=" + y + " AND time >= " + time + " ORDER BY " + ConfigHandler.getAscendingEventOrder() + " LIMIT 1 OFFSET 0";
}
SignStatement.getData(statement, block, query);
BlockUtils.updateBlock(block);
@@ -18,7 +18,7 @@ class SkullUpdateProcess {
*/
if (object instanceof BlockState) {
BlockState block = (BlockState) object;
String query = "SELECT owner, skin FROM " + ConfigHandler.prefix + "skull WHERE rowid='" + rowId + "' LIMIT 0, 1";
String query = "SELECT owner, skin FROM " + ConfigHandler.prefix + "skull WHERE rowid=" + rowId + " LIMIT 1 OFFSET 0";
SkullStatement.getData(statement, block, query);
BlockUtils.updateBlock(block);
}
@@ -1,6 +1,6 @@
package net.coreprotect.consumer.process;
import java.sql.PreparedStatement;
import net.coreprotect.database.ConsumerWriteBatch;
import java.sql.Statement;
import java.util.List;
import java.util.Map;
@@ -12,11 +12,10 @@ import net.coreprotect.database.Lookup;
import net.coreprotect.database.logger.BlockBreakLogger;
import net.coreprotect.database.logger.BlockPlaceLogger;
import net.coreprotect.utility.MaterialUtils;
import net.coreprotect.utility.Util;
class StructureGrowthProcess {
static void process(Statement statement, PreparedStatement preparedStmt, int batchCount, int processId, int id, String user, Object object, int replaceBlockCount) {
static void process(Statement statement, ConsumerWriteBatch preparedStmt, int batchCount, int processId, int id, String user, Object object, int replaceBlockCount) {
if (object instanceof BlockState) {
BlockState block = (BlockState) object;
Map<Integer, List<BlockState>> blockLists = Consumer.consumerBlockList.get(processId);
@@ -39,7 +38,6 @@ class StructureGrowthProcess {
}
count++;
}
blockLists.remove(id);
}
}
}
@@ -1,9 +1,9 @@
package net.coreprotect.consumer.process;
import java.sql.PreparedStatement;
import java.sql.Statement;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.ConsumerWriteBatch;
import net.coreprotect.database.statement.MaterialStatement;
import net.coreprotect.database.statement.WorldStatement;
import net.coreprotect.language.Phrase;
@@ -12,10 +12,10 @@ import net.coreprotect.utility.Chat;
class WorldInsertProcess {
static void process(PreparedStatement preparedStmt, int batchCount, Statement statement, Object world, int worldId) {
static void process(ConsumerWriteBatch preparedStmt, int batchCount, Statement statement, Object world, int worldId) {
if (world instanceof String) {
String query = "SELECT id FROM " + ConfigHandler.prefix + "world WHERE id = '" + worldId + "' LIMIT 0, 1";
boolean hasMaterial = MaterialStatement.hasMaterial(statement, query);
String query = "SELECT id FROM " + ConfigHandler.prefix + "world WHERE id = " + worldId + " LIMIT 1 OFFSET 0";
boolean hasMaterial = !ConfigHandler.databaseType.isClickHouse() && MaterialStatement.hasMaterial(statement, query);
if (!hasMaterial) {
WorldStatement.insert(preparedStmt, batchCount, worldId, (String) world);
@@ -29,18 +29,16 @@ public class BlockLookup {
int z = block.getZ();
int time = (int) (System.currentTimeMillis() / 1000L);
int worldId = WorldUtils.getWorldId(block.getWorld().getName());
String query = "SELECT user,type FROM " + ConfigHandler.prefix + "block " + WorldUtils.getWidIndex("block") + "WHERE wid = '" + worldId + "' AND x = '" + x + "' AND z = '" + z + "' AND y = '" + y + "' AND rolled_back IN(0,2) AND action='1' ORDER BY rowid DESC LIMIT 0, 1";
String table = DuckDBLookupQuery.spatialTable(statement.getConnection(), "block", worldId, x, x, z, z, "spatial_rows");
String index = ConfigHandler.databaseType.isDuckDB() ? "" : WorldUtils.getWidIndex("block");
String query = "SELECT " + ConfigHandler.databaseType.getUserColumn() + ",type FROM " + table + " " + index + "WHERE " + LocationQuery.predicate("wid", " = " + worldId) + " AND " + LocationQuery.predicate("x", " = " + x) + " AND " + LocationQuery.predicate("z", " = " + z) + " AND y = " + y + " AND rolled_back IN(0,2) AND action=1 ORDER BY " + ConfigHandler.getDescendingEventOrder() + " LIMIT 1 OFFSET 0";
ResultSet results = statement.executeQuery(query);
while (results.next()) {
int resultUserId = results.getInt("user");
int resultType = results.getInt("type");
if (ConfigHandler.playerIdCacheReversed.get(resultUserId) == null) {
UserStatement.loadName(statement.getConnection(), resultUserId);
}
result = ConfigHandler.playerIdCacheReversed.get(resultUserId);
result = UserStatement.getName(statement.getConnection(), resultUserId);
if (result.length() > 0) {
Material resultMaterial = MaterialUtils.getType(resultType);
CacheHandler.lookupCache.put("" + x + "." + y + "." + z + "." + worldId + "", new Object[] { time, result, resultMaterial });
@@ -0,0 +1,30 @@
package net.coreprotect.database;
import java.util.List;
import java.util.UUID;
import net.coreprotect.model.entity.EntityContainerRollbackUpdate;
import net.coreprotect.model.entity.EntitySpawnData;
import net.coreprotect.model.entity.EntitySpawnIdentity;
public interface ConsumerEntitySpawnUpdates extends AutoCloseable {
default void prefetch(List<EntitySpawnData> updates) throws Exception {
}
EntitySpawnIdentity apply(EntitySpawnData data);
void applyCombined(EntityContainerRollbackUpdate update, Database.SavepointOperation rowUpdate);
void identityFound(UUID uuid);
void afterCommit(boolean committed);
void afterRetain();
void afterDiscard();
@Override
void close() throws Exception;
}
@@ -0,0 +1,88 @@
package net.coreprotect.database;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public interface ConsumerWriteBatch extends AutoCloseable {
enum ReferenceKind {
ART,
BLOCK_DATA,
ENTITY,
MATERIAL,
WORLD
}
void begin() throws Exception;
boolean commit() throws Exception;
default boolean wasCommitAttempted() {
return true;
}
default boolean shouldCommit() {
return false;
}
void rollback();
void executeAtomically(String name, Database.SavepointOperation operation) throws Exception;
int resolveUserId(String user, String uuid) throws Exception;
void recordUsername(String user, String uuid, int retainHistory, int time) throws Exception;
void updateDatabaseLock(int status, int time) throws Exception;
void addReference(ReferenceKind kind, int batchCount, int id, String value) throws Exception;
void addBlock(int batchCount, int time, int userId, int worldId, int x, int y, int z, int type, int data, byte[] meta, byte[] blockData, int action, int rolledBack) throws Exception;
long addBlockReturningId(int time, int userId, int worldId, int x, int y, int z, int type, int data, byte[] meta, byte[] blockData, int action, int rolledBack) throws Exception;
int addSkull(int time, String owner, String skin) throws Exception;
void addContainer(int batchCount, int time, int userId, int worldId, int x, int y, int z, int type, int data, int amount, byte[] metadata, int action, int rolledBack) throws Exception;
void addEntityContainer(int batchCount, int time, int userId, int entitySpawnRowId, int worldId, int x, int y, int z, int type, int data, int amount, byte[] metadata, int action, int rolledBack) throws Exception;
void addItem(int batchCount, int time, int userId, int worldId, int x, int y, int z, int type, byte[] data, int amount, int action, int rolledBack) throws Exception;
void addChat(int batchCount, long time, int userId, int worldId, int x, int y, int z, String message) throws Exception;
void addCommand(int batchCount, long time, int userId, int worldId, int x, int y, int z, String message) throws Exception;
void addSession(int batchCount, int time, int userId, int worldId, int x, int y, int z, int action) throws Exception;
void addSign(int batchCount, int time, int userId, int worldId, int x, int y, int z, int action, int color, int colorSecondary, int data, int waxed, int face, String[] lines) throws Exception;
int addEntity(int time, byte[] data) throws Exception;
int addEntitySpawn(int time, Long blockRowId, Integer killRowId, UUID uuid, int originWorldId, int currentWorldId, double originX, double originY, double originZ, double currentX, double currentY, double currentZ, float yaw, float pitch, byte[] data, int removed) throws Exception;
void linkEntitySpawnBlock(int trackingRowId, long blockRowId) throws Exception;
void linkEntitySpawnKill(UUID uuid, int killRowId) throws Exception;
boolean checkpointEntitySpawn(int trackingRowId, int worldId, double x, double y, double z, float yaw, float pitch) throws Exception;
void addEntityInteraction(int time, int userId, int entitySpawnRowId, int worldId, int x, int y, int z, int type, int action, byte[] metadata, int rolledBack) throws Exception;
void updateRolledBack(int target, int rolledBack, List<Long> rowIds) throws Exception;
default void updateRolledBackRows(int target, int rolledBack, List<Object[]> rows) throws Exception {
List<Long> rowIds = new ArrayList<>(rows.size());
for (Object[] row : rows) {
rowIds.add((Long) row[0]);
}
updateRolledBack(target, rolledBack, rowIds);
}
ConsumerEntitySpawnUpdates entitySpawnUpdates() throws Exception;
@Override
void close() throws Exception;
}
@@ -15,15 +15,17 @@ import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Entity;
import org.bukkit.entity.ItemFrame;
import org.bukkit.entity.LivingEntity;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import net.coreprotect.CoreProtect;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.consumer.Consumer;
import net.coreprotect.consumer.Queue;
import net.coreprotect.consumer.process.Process;
import net.coreprotect.database.rollback.Rollback;
import net.coreprotect.database.rollback.RollbackComplete;
import net.coreprotect.language.Phrase;
import net.coreprotect.listener.player.InventoryChangeListener;
import net.coreprotect.model.BlockGroup;
import net.coreprotect.thread.Scheduler;
import net.coreprotect.utility.BlockUtils;
@@ -34,14 +36,22 @@ import net.coreprotect.utility.ErrorReporter;
public class ContainerRollback extends Rollback {
public static void performContainerRollbackRestore(Statement statement, CommandSender user, List<String> checkUuids, List<String> checkUsers, String timeString, List<Object> restrictList, Map<Object, Boolean> excludeList, List<String> excludeUserList, List<Integer> actionList, final Location location, Integer[] radius, long startTime, long endTime, boolean restrictWorld, boolean lookup, boolean verbose, final int rollbackType) {
public static boolean performContainerRollbackRestore(Statement statement, CommandSender user, List<String> checkUuids, List<String> checkUsers, String timeString, List<Object> restrictList, Map<Object, Boolean> excludeList, List<String> excludeUserList, List<Integer> actionList, final Location location, Integer[] radius, long startTime, long endTime, boolean restrictWorld, boolean lookup, boolean verbose, final int rollbackType) {
try {
long timeStart = System.currentTimeMillis();
final List<Object[]> lookupList = Lookup.performLookupRaw(statement, user, checkUuids, checkUsers, restrictList, excludeList, excludeUserList, actionList, location, radius, null, startTime, endTime, -1, -1, restrictWorld, lookup);
if (lookupList == null) {
sendAborted(user, "Unable to load container rollback records from the database.");
return false;
}
if (rollbackType == 1) {
Collections.reverse(lookupList);
}
if (Consumer.isPersistenceHalted()) {
sendAborted(user, "Database persistence halted before container rollback updates could be queued.");
return false;
}
String userString = "#server";
if (user != null) {
@@ -49,6 +59,10 @@ public class ContainerRollback extends Rollback {
}
Queue.queueRollbackUpdate(userString, location, lookupList, Process.CONTAINER_ROLLBACK_UPDATE, rollbackType); // Perform update transaction in consumer
if (Consumer.isPersistenceHalted()) {
sendAborted(user, "Database persistence halted while queuing container rollback updates.");
return false;
}
final String finalUserString = userString;
ConfigHandler.rollbackHash.put(userString, new int[] { 0, 0, 0, 0, 0 });
@@ -58,6 +72,11 @@ public class ContainerRollback extends Rollback {
public void run() {
try {
int[] rollbackHashData = ConfigHandler.rollbackHash.get(finalUserString);
if (Consumer.isPersistenceHalted()) {
Chat.console("Container rollback/restore for " + finalUserString + " aborted: database persistence halted before the container could be modified.");
ConfigHandler.rollbackHash.put(finalUserString, new int[] { rollbackHashData[0], rollbackHashData[1], rollbackHashData[2], 2, rollbackHashData[4] });
return;
}
int itemCount = rollbackHashData[0];
// int blockCount = rollbackHashData[1];
int entityCount = rollbackHashData[2];
@@ -91,6 +110,9 @@ public class ContainerRollback extends Rollback {
int modifyCount = 0;
if (container != null) {
if (container instanceof Inventory) {
InventoryChangeListener.flushPendingContainer((Inventory) container, location);
}
for (Object[] lookupRow : lookupList) {
// int unixtimestamp = (int) (System.currentTimeMillis() / 1000L);
// int rowId = lookupRow[0];
@@ -149,6 +171,10 @@ public class ContainerRollback extends Rollback {
}
catch (Exception e) {
ErrorReporter.report(e);
int[] rollbackHashData = ConfigHandler.rollbackHash.get(finalUserString);
if (rollbackHashData != null) {
ConfigHandler.rollbackHash.put(finalUserString, new int[] { rollbackHashData[0], rollbackHashData[1], rollbackHashData[2], 2, rollbackHashData[4] });
}
}
}
}, location, 0);
@@ -163,11 +189,14 @@ public class ContainerRollback extends Rollback {
rollbackHashData = ConfigHandler.rollbackHash.get(finalUserString);
next = rollbackHashData[3];
if (sleepTime > 300000) {
Chat.console(Phrase.build(Phrase.ROLLBACK_ABORTED));
Chat.console("Container rollback/restore for " + finalUserString + " aborted: timed out after 300 seconds waiting for the container task.");
break;
}
}
if (next != 1) {
sendAborted(user);
return false;
}
rollbackHashData = ConfigHandler.rollbackHash.get(finalUserString);
int blockCount = rollbackHashData[1];
long timeFinish = System.currentTimeMillis();
@@ -183,9 +212,12 @@ public class ContainerRollback extends Rollback {
RollbackComplete.output(user, location, checkUsers, restrictList, excludeList, excludeUserList, actionList, timeString, file, totalSeconds, itemCount, blockCount, entityCount, rollbackType, radius, verbose, restrictWorld, 0);
}
return true;
}
catch (Exception e) {
ErrorReporter.report(e);
sendAborted(user);
return false;
}
}
Loaded 100 of 311 files, more files were not shown because too many files have changed in this diff. Show more