397 Commits

Author SHA1 Message Date
9003831622 First tries to get postgres up and running 2024-04-07 12:36:46 +02:00
43c9fd01a8 Compile against WorldEdit 7.3.0; fix compile 2024-03-10 06:16:25 +01:00
54f9855a1f Merge pull request #892 from IAISI/master
Fix NPE, update dependencies
2024-02-12 06:32:15 +01:00
626ba83b18 Update dependencies 2024-02-07 11:44:31 +01:00
7adf7a1cb4 Fix NPE:
[LogBlock] Could not parse BlockState for PLAYER_HEAD
java.lang.NullPointerException: Cannot invoke "java.util.UUID.toString()" because the return value of "org.bukkit.profile.PlayerProfile.getUniqueId()" is null
	at de.diddiz.LogBlock.blockstate.BlockStateCodecSkull.getChangesAsComponent(BlockStateCodecSkull.java:77) ~[LogBlock.jar:?]
	at de.diddiz.LogBlock.blockstate.BlockStateCodecs.getChangesAsComponent(BlockStateCodecs.java:57) ~[LogBlock.jar:?]
	at de.diddiz.LogBlock.BlockChange.getTypeDetails(BlockChange.java:95) ~[LogBlock.jar:?]
	at de.diddiz.LogBlock.BlockChange.getTypeDetails(BlockChange.java:87) ~[LogBlock.jar:?]
	at de.diddiz.LogBlock.BlockChange.getLogMessage(BlockChange.java:135) ~[LogBlock.jar:?]
	at de.diddiz.LogBlock.CommandsHandler.showPage(CommandsHandler.java:446) ~[LogBlock.jar:?]
	at de.diddiz.LogBlock.CommandsHandler.showPage(CommandsHandler.java:428) ~[LogBlock.jar:?]
	at de.diddiz.LogBlock.CommandsHandler$CommandLookup.run(CommandsHandler.java:578) ~[LogBlock.jar:?]
	at org.bukkit.craftbukkit.v1_20_R3.scheduler.CraftTask.run(CraftTask.java:101) ~[paper-1.20.4.jar:git-Paper-"9e171ef"]
	at org.bukkit.craftbukkit.v1_20_R3.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:57) ~[paper-1.20.4.jar:git-Paper-"9e171ef"]
	at com.destroystokyo.paper.ServerSchedulerReportingWrapper.run(ServerSchedulerReportingWrapper.java:22) ~[paper-1.20.4.jar:?]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) ~[?:?]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) ~[?:?]
	at java.lang.Thread.run(Thread.java:840) ~[?:?]
2024-02-07 11:38:13 +01:00
da2264481c Update for 1.20.4 (requires 1.20.4 to run) 2023-12-13 09:33:08 +01:00
e3dda845e2 improve sign change log display 2023-12-09 02:39:59 +01:00
224bc1bae7 only log sign changes whan anything was changed 2023-12-09 02:39:46 +01:00
9c9f923536 Log brushing suspicious sand/gravel 2023-11-13 07:19:54 +01:00
e30e331cce allow mobspawners without spawned type 2023-10-20 06:48:05 +02:00
2671080665 formating... 2023-10-18 04:05:45 +02:00
499573fa66 Fix duplicate set entry & add missing piglin head (#888) 2023-10-17 23:52:13 +02:00
f972bae6ba typo 2023-10-17 08:40:56 +02:00
d7e458ad68 Use Tags for Materials in BukkitUtils (#883) 2023-10-17 08:38:46 +02:00
f2e76a50cb MC Version 1.20.1, use waxed sign api (#884)
* bump api version to 1.20.1
* Sign.isWaxed is now API
2023-10-17 08:33:06 +02:00
fc92ad2307 remove eol setting from .editorconfig 2023-10-09 09:00:31 +02:00
95a8d6043d add .gitattributes 2023-10-09 08:54:12 +02:00
0c21d821d9 Fix line endings 2023-10-09 08:52:12 +02:00
19cc874db8 Add trim_trailing_whitespace to .editorconfig 2023-10-03 03:11:47 +02:00
499cdbca27 Add .editorconfig 2023-10-03 03:11:47 +02:00
f5ddf9f9ec Fix #875
Update HikariCP
slf4j is not needed, removed it
2023-10-02 02:31:45 +02:00
a3069292d0 Bump api-version to 1.20 2023-10-01 05:51:37 +02:00
249ab18091 use mkdirs() instead of mkdir() and fix formatting 2023-09-29 03:24:18 +02:00
337a0502bf Fix #873
Use Bukkit API for plugin Directory
2023-09-29 03:20:59 +02:00
a6b37c03d0 Merge pull request #870 from Joo200/feature/mariadb-connector
MariaDB 11 compatibility fix: configurable protocol
2023-09-22 07:32:41 +02:00
8786dc253e add protocol config option to allow mariadb protocol 2023-08-15 16:29:54 +02:00
1c0c6c2d8b cauldron no longer has levels 2023-07-21 02:57:05 +02:00
547aa81063 improve Reflections.isSignWaxed 2023-06-12 06:26:45 +02:00
f743347e66 sign dyes can only be applied when there is some text on the sign 2023-06-12 05:55:12 +02:00
a6e0e72fb6 cleanup sign logging 2023-06-12 05:54:46 +02:00
5fdecb77cd improve block state display 2023-06-12 05:29:06 +02:00
05269c6978 version is now 1.20 2023-06-12 03:43:20 +02:00
419f3530ad properly log sign edits 2023-06-12 03:41:12 +02:00
31bef400a6 add 1.20 materials 2023-06-11 05:24:15 +02:00
1fce7c7a66 avoid invalid fluid levels 2023-06-02 05:25:12 +02:00
a609c021e6 1.19.4 2023-03-17 05:33:58 +01:00
e4fb0f38d1 No more enums 2023-03-17 05:30:40 +01:00
154de294a4 use a better method 2023-02-07 04:41:58 +01:00
d347a25a02 Updates for 1.19.3: Log hanging signs and add some missing materials 2023-02-07 04:30:24 +01:00
b3e829d1be add bamboo blocks (requires 1.19.3) 2023-01-18 06:09:55 +01:00
c3ae8358dd better log rollback exceptions 2023-01-03 04:59:42 +01:00
5d91d5abb7 "/lb sum entities" should list entities, not blocks 2022-11-03 06:04:11 +01:00
7c690c707a port to exact height when player is in specator mode 2022-11-03 06:01:57 +01:00
0bd211083b do not log golems by default 2022-11-03 05:56:41 +01:00
a395f206bd Log respawn anchor explosions 2022-11-03 05:56:30 +01:00
4faced94d4 separate logging option for wateranimals 2022-11-03 05:41:41 +01:00
0f459440be New entity logging option logAllNamedEntityKills 2022-11-03 05:36:55 +01:00
43864ad002 Remove unused config option 2022-09-30 05:26:14 +02:00
c34ad1ca0f Allow minecraft names instead of bukkit names for entities 2022-09-09 07:47:12 +02:00
0e601aa174 add config option if natural entity spawns should be logged 2022-08-03 05:44:57 +02:00
9bfe5d09cd use long when accessing id columns 2022-08-03 05:05:44 +02:00
c6e8105c0e add newline before log output to improve readability 2022-08-03 04:36:09 +02:00
b658d3799b add missing 1.19 materials 2022-07-14 05:31:24 +02:00
86100c1830 version 1.19.0.0-SNAPSHOT 2022-07-03 07:36:40 +02:00
7791c4e960 fix player names with underscore 2022-06-16 21:32:51 +02:00
62a5be6c6b log sculk 2022-06-11 20:39:23 +02:00
0ed501493e Update for Minecraft 19
* Update build plugins
* Update dependencies
* Require Java 17
2022-06-11 20:20:09 +02:00
81457ced65 improve player name parsing 2022-06-11 20:14:48 +02:00
c3394fa8c5 Save full skull profile instead of just the uuid if available 2022-02-16 08:38:50 +01:00
e61f9b058d Fix world height during safe teleport 2022-02-01 20:42:48 +01:00
1b2948c912 use https for xml schema location 2022-01-08 21:49:07 +01:00
8def317c62 Revert "exclude worldedit dependencies (#850)"
This reverts commit 676f862d2d.
2022-01-07 21:34:19 +01:00
676f862d2d exclude worldedit dependencies (#850)
Co-authored-by: qwertyuioplkjhgfd <qwertyuioplkjhgfd@users.noreply.github.com>
2022-01-07 01:46:51 +01:00
6d0ac7169c update enginehub repository location (#849) 2022-01-05 01:37:45 +01:00
5e9be562cd fix candle logging 2021-12-19 09:25:20 +01:00
4e4b5de15d fix some y column types on startup if required 2021-12-19 07:27:37 +01:00
eac1008525 ItemStack.getTagClone is private so we have to use setAccessible 2021-12-19 06:41:10 +01:00
e87e9e2939 Fix itemstack reflection 2021-12-06 03:58:39 +01:00
31589ceec7 Build against spigot-api 1.18 2021-12-06 03:58:16 +01:00
749d114acd Fix issues with minecraft 1.18 2021-12-01 07:48:03 +01:00
964e60b2e5 update distribution repository 2021-11-08 05:49:33 +01:00
142bcae2ca Add feature to disable logging in WorldGuard regions
resolves #826
2021-08-27 03:22:29 +02:00
5dfee1f906 Load config in onLoad 2021-08-27 02:43:18 +02:00
e430ee073f Move all classes to the logblock package 2021-08-27 02:25:11 +02:00
c940dfc05a Store the acting entity object in the actor class 2021-08-26 21:16:40 +02:00
610f3edcab Add EntityChangePreLogEvent 2021-08-26 20:54:55 +02:00
597b1831f1 Normalize actor for crop trampling mobs 2021-08-26 20:53:42 +02:00
d084372876 Simplify Actor.actorFromString 2021-08-26 08:07:28 +02:00
4b7b5b984d Remove fireCustomEvents config option / fire event when any listener is registered 2021-08-26 07:58:44 +02:00
522def8b2e Add a warning that the table conversion for 1.17 might take a long time 2021-08-23 19:41:23 +02:00
b3f268cd04 Avoid deprecated methods when creating the item hover and some cleanup 2021-08-21 00:01:15 +02:00
f2f988b15b Added more pleasant HoverEvent (#835)
* Added more pleasant hoverevent

* Remove synchronized

* Changed item amount

* Requested changes
2021-08-20 23:49:58 +02:00
3730aa92c4 Log breaking dripstone
Fixes #831
2021-08-17 05:54:13 +02:00
5aac2d712b Fix logging blocks below 0
Fixes #834
2021-08-17 05:47:37 +02:00
701f434fae cancel auto clearlog when plugin is disabled 2021-07-12 05:08:03 +02:00
07924138ea Add cauldron interact logging 2021-07-12 04:28:16 +02:00
9c2caa6af8 Better display sign changes in log messages 2021-07-12 03:57:36 +02:00
1e1243449f Log make signs glowing/unglowing 2021-07-12 03:32:09 +02:00
7f3837f1fe Fix logging bonemealing moss 2021-07-09 06:27:21 +02:00
9399b7062e Update to 1.17 2021-06-29 05:23:17 +02:00
a58206b21d Add End Crystal logging
Fixes #830
2021-06-23 05:50:32 +02:00
aca0127781 Update spigot dependency to 1.16.5 2021-06-23 05:48:06 +02:00
1a9827cc32 Explosion logging: Move WitherSkull up, because it is a Fireball too 2021-06-11 04:03:37 +02:00
cf8da11cca Fix setting actor when a creeper causes an explosion 2021-04-30 04:05:30 +02:00
8e3f5aca56 Improve entity killer logging
Fixes #622
2021-04-29 20:37:38 +02:00
2d9456409c Remove accidentally committed debug logging 2021-04-29 16:27:14 +02:00
f0e3353fc9 Prettier help formating
Fixes #592
2021-04-28 05:21:13 +02:00
721b51383c Merge branch 'master' into inventory-logging
Fixes #817, #774, #770, #676, #565
2021-04-28 04:51:46 +02:00
cb1231eab5 Snapshots 2021-04-15 03:23:55 +02:00
1df380741b Release 1.16.5.1 2021-04-15 03:19:14 +02:00
2049a7a7a4 Use a different way to check for existing tables 2021-04-14 18:52:06 +02:00
8148386e3e Back to snapshots 2021-04-13 21:01:37 +02:00
87074da8a1 Release Logblock 1.16.5 2021-04-13 20:53:56 +02:00
0fd3266c7b Load the new mysql driver class if available 2021-04-13 18:32:16 +02:00
c20b677507 Improve table creation logic 2021-04-13 18:24:54 +02:00
fe7e244898 Do not scroll in a result set when we don't have to 2021-03-18 21:25:10 +01:00
4f7c02b285 Flush other viewers of the same inventory; add some documentation 2021-02-22 07:42:50 +01:00
8a8471c3e6 Initial work for improved inventory logging 2021-02-22 00:18:02 +01:00
fe98370acd Avoid syncronous query in /lb me
and improve changes count display in sumarized changes

Fixes #819
2021-02-20 06:32:53 +01:00
76df1a4913 Use GSON in UUIDFetcher 2021-02-17 07:31:50 +01:00
841ce89f21 UUIDs are stored differently since 1.16 2021-02-17 07:10:51 +01:00
033a53e338 Improve database upgrade logic 2021-01-30 17:34:24 +01:00
f42649adc3 Do not forget to close connection when upgrading a table fails 2021-01-29 20:10:43 +01:00
81e0135046 Remove tool from offhand if it is not found in the main inventory 2021-01-16 07:37:56 +01:00
e181c85647 Fix table update and to not break on update issues 2021-01-16 07:16:01 +01:00
dde8dc8289 Do not log bees leaving beehives 2021-01-16 06:21:53 +01:00
ac462261dc Add missing entityid index for entities tables 2021-01-16 06:20:36 +01:00
42715de265 getExistingEntityTypeId should return Integer instead of int
Fixes #804
2020-08-31 00:28:12 +02:00
d548206c3a Workaround for SPIGOT-6025 (catch the exception and log it) 2020-08-28 05:34:48 +02:00
24b5455f08 Use Spigots new HoverEvent api 2020-08-23 07:10:11 +02:00
35f921a9a0 Fix loading old walls 2020-08-23 05:40:15 +02:00
0d7a8016a1 Do not add unnecessary materials or entity types to the database 2020-08-09 23:25:30 +02:00
6a398a67ab Protect against sql injections when using the api 2020-07-20 07:13:32 +02:00
6dcca54637 Do not use the numeric material/entity ids for the api 2020-07-20 06:47:43 +02:00
650f7e20f1 Make MaterialConverter and EntityTypeConverter thread safe 2020-07-20 06:17:06 +02:00
5c22beb2e5 Make MaterialConverter type safe 2020-07-20 06:02:23 +02:00
a63c97bd70 Back to snapshots 2020-07-11 05:45:35 +02:00
1562bbacea Release 1.16.1.1 2020-07-11 05:43:54 +02:00
aba6e4d9c8 Add some missing special block handlings for 1.16 2020-07-02 04:33:18 +02:00
1ef7c78c0d Snapshots for 1.16.1.1 2020-06-27 22:45:20 +02:00
788d8fd4d5 Release 1.16.1.0 2020-06-27 22:43:33 +02:00
3bfb19cdfa Correctly log 1.16 blocks 2020-06-27 22:40:08 +02:00
04b5d9e7ed Snapshots for 1.16.1 2020-06-27 22:39:29 +02:00
a96f82efae Back to snapshots 2020-02-13 05:09:27 +01:00
39f58a6bd4 Release 1.15.2 2020-02-13 05:05:21 +01:00
e1064dd0b1 Load mysql driver on startup
Fixes #784
2020-02-13 04:17:01 +01:00
fc1cd5ef2c Fix oldConfig check 2020-02-08 17:33:39 +01:00
1dba9f20f1 create tables and register events if logging commands 2020-01-26 06:27:09 +01:00
8eb93411ec Configurable command logging 2020-01-26 03:21:12 +01:00
068ac89819 Add optional logging of shulker box contents
Fixes #781
2020-01-24 05:51:18 +01:00
6b71a3c30d smart log breaking ice 2020-01-24 05:17:33 +01:00
6dec1b6c37 Improve rollback ability by accepting similar blocks to the expected one
For example grass_block instead of dirt
2020-01-24 05:06:48 +01:00
b9513df20e Log bamboo growth (new world logging option)
Fixes #769
2020-01-24 04:52:13 +01:00
8b34e39797 Log getting stung by bees
Fixes #780
2020-01-24 04:23:59 +01:00
1cda6506c7 Merge pull request #782 from TheMolkaPL/tool-event
Call ToolUseEvent when a tool is about to be used
2020-01-24 03:49:44 +01:00
5b0e2d9adb Call ToolUseEvent when a tool is about to be used 2020-01-21 17:44:56 +01:00
cdf6c1df04 Fix conversion to roman numbers
Fixes #779
2019-12-27 18:04:41 +01:00
cdee5b3609 Back to snapshots 2019-12-20 05:37:55 +01:00
8e948e857f Release 1.15.1 2019-12-20 05:27:55 +01:00
793df218e5 Bump version to 1.15.1, update dependencies 2019-12-20 05:27:24 +01:00
31428d60e4 Add some hover texts and clickable coords 2019-12-20 05:15:45 +01:00
169328e159 Use ChatComponent API for log messages 2019-12-18 01:33:40 +01:00
60a771224b improve logging of entitiy damaged by projectiles or tnt 2019-12-01 05:14:10 +01:00
3135fe8696 Merge branch 'master' into pretty-chat 2019-11-24 06:19:05 +01:00
59d0794c3d Improve scaffolding logging with fallable blocks
- Log fallable blocks above scaffoldings
- Log scaffolding breaking because blocks below are falling down
2019-11-24 06:16:07 +01:00
8214e7d177 Log falling scaffoldings 2019-11-24 05:46:51 +01:00
e77e95cae0 Merge branch 'master' into pretty-chat 2019-11-17 04:00:59 +01:00
241a7adc48 Wildcard block names in querys
For example "*_slab" to match any slab
2019-11-17 04:00:34 +01:00
27cc59f922 Merge branch 'master' into pretty-chat 2019-11-17 03:19:33 +01:00
8192aa4fb8 Allow block tags in querys
"block #signs" and similar: https://minecraft.gamepedia.com/Tag#List_of_tags
2019-11-17 03:17:32 +01:00
078fe7f423 missing diff for log formatting 2019-10-16 02:37:07 +02:00
76a81c124a Merge branch 'master' into pretty-chat 2019-10-16 02:25:43 +02:00
d98d46d0c9 Fix logging when waterlogging waterlogged blocks 2019-10-16 00:52:29 +02:00
1e1dce99c0 Merge remote-tracking branch 'remotes/origin/master' into pretty-chat 2019-09-30 06:15:29 +02:00
921df872d1 Expose useSSL flag to config 2019-09-30 06:11:26 +02:00
cd38ac9866 Remove unused methods 2019-08-27 22:30:37 +02:00
a7967e9b1e Do not require WorldEdit for CuboidRegion 2019-08-27 20:10:12 +02:00
af895aa21d Merge branch 'master' into pretty-chat 2019-08-11 06:09:24 +02:00
2f92fd3426 Fix beacon logging, they are not a container 2019-08-11 04:56:13 +02:00
f298a5f70f Merge branch 'master' into pretty-chat 2019-08-10 04:34:22 +02:00
091bdca142 Merge pull request #763 from paul-maxime/public-chestaccess
Add public to the ChestAccess variables
2019-08-10 04:25:48 +02:00
07bf9421dd Switch to openjdk8 in travis builds 2019-08-10 04:24:03 +02:00
06f24bf632 Add public to the ChestAccess variables 2019-08-10 00:24:27 +02:00
3f7ace7f70 Merge branch 'master' into pretty-chat 2019-07-09 03:46:09 +02:00
6f4ce7e6d0 Fix lectern logging
Fixes #761
2019-07-09 03:42:33 +02:00
d03bbe68ba Add missing newline 2019-06-24 04:14:52 +02:00
e9d78bffb1 Change action type for some entries 2019-06-23 21:59:07 +02:00
d829005c7e Apply new message formatting to entity changes 2019-06-23 21:57:21 +02:00
a6e4d79e0c Merge remote-tracking branch 'remotes/DarkArk/feature/pretty-chat' into pretty-chat 2019-06-23 21:47:14 +02:00
e8aaadf37b Code style 2019-06-23 17:14:17 +02:00
1525d7682f Reformat 2019-06-23 16:45:14 +02:00
76f7f8701d Improve logging of 1.14 blocks 2019-06-23 05:23:31 +02:00
9b5e0c9025 WorldEdit logging should only log modified blocks
Fixes #757
2019-06-23 05:22:18 +02:00
e6b0108bc5 Chunk.load is just as World.loadChunk 2019-06-20 06:08:59 +02:00
3efd92d9df loadChunk will keep chunks loaded forever, so do not use that 2019-06-20 05:37:55 +02:00
05d7652bcc Improve log messages when the block/entity type is unknown 2019-05-31 18:00:55 +02:00
72fc78b3c0 Add missing break 2019-05-28 07:30:56 +02:00
9eef03aa90 Fix lectern logging 2019-05-28 06:07:57 +02:00
da692ed01a Log lectern book change 2019-05-28 05:25:25 +02:00
424ef3b02b Only log sign base color changes when we log SIGNTEXT for this world 2019-05-27 23:53:50 +02:00
4fda020dfc Log sign base color change 2019-05-27 22:46:32 +02:00
8f429afbeb Made LogBlock's messages far more visual appealing 2019-05-20 23:29:29 -04:00
f6522b73f4 create chat table without fulltext index if the database does not support it. 2019-05-16 01:35:46 +02:00
ac233a3920 Spigot 1.14.1 2019-05-14 17:59:30 +02:00
7a946fc23d Merge branch 'master' into 1.14pre 2019-05-14 17:56:39 +02:00
1602fdb03a Configuration option to require SSL on the database connection 2019-05-14 17:54:33 +02:00
33c18a9e62 Improve compatibility when running 1.13 and 1.14 servers on the same logblock database
Old materials are no longer renamed, instead new materials are added and the old entries are modified
2019-05-05 05:55:47 +02:00
e66b5a8f9d Merge branch 'master' into 1.14pre 2019-05-04 17:50:48 +02:00
bd23c93071 Allow logblock to load when it encounters unknown material/entity types 2019-05-04 17:49:29 +02:00
503541ad4e Fix version parsing 2019-05-02 21:51:16 +02:00
82d61d5ee7 Spigot 1.14 instead of 1.14-pre5 2019-04-27 04:53:34 +02:00
254c856b2f Update to 1.14pre5 2019-04-24 17:49:52 +02:00
d4b127244e Disable tool on drop: Only cancel dropping if there is space for that item 2019-03-21 01:44:33 +01:00
87c09766d3 Allow tool use when the interact event is cancelled 2019-03-06 04:23:19 +01:00
8b1ee254b4 Fix NPE when entities die without a damager
Fixes #745
2019-02-23 18:17:14 +01:00
34eeb52c8d Use EnumSet instead of boolean array 2019-02-22 04:13:36 +01:00
399cbc901f Null check 2019-02-18 21:50:01 +01:00
7dce1776e7 Log pumpkin carving
Fixes #743
2019-02-18 21:15:42 +01:00
92de737e4e Allow null weapons for kills
Fixes #742
2019-02-14 21:24:09 +01:00
025950a8c4 Fix armor stand logging on kill in survival mode 2019-01-14 03:29:07 +01:00
dc62524d0d Fix logging fluids flowing down 2019-01-09 03:58:12 +01:00
947163477b Do not go over the maximum level for fluids 2019-01-08 16:38:26 +01:00
2e81e2be9d Catch failed ItemStack serialization
This will still not serialize the problematic ItemStacks, at that is not in the scope of logblock, but it will avoid problems with logging is this problem occurs.
Fixes #735
2019-01-05 17:16:18 +01:00
58223b7612 Fix startup exception when running without WorldEdit
Fixes #736
2019-01-04 18:10:11 +01:00
51f84251dc Adapt to latest WorldEdit changes 2019-01-04 18:09:12 +01:00
31ef2d942b Return idle connection from Consumer to avoid timeout
Fixes #732
2018-12-11 05:11:13 +01:00
9f8fd3e1ca Use constants for magic values 2018-12-08 18:04:36 +01:00
71527530f2 Reduce converter max batch size
Some popular hosters have quite low memory limits.. Fixes #730
2018-11-27 06:15:36 +01:00
d5ee15ebba Improve stability for material table updates 2018-11-27 06:05:00 +01:00
ab464e1dd5 Use long instead of int to avoid overflow in converter 2018-11-26 06:20:02 +01:00
d847908d3c Skip duplicates for chest/sign conversion too 2018-11-24 03:39:05 +01:00
7e47a0c375 Skip duplicate rows instead of canceling the conversion 2018-11-24 03:34:09 +01:00
ab7c8ffbb2 Fix key limit issues with some mysql versions
Fixes #728
2018-11-13 23:51:35 +01:00
0e8bb2599c WorldEdit changed the bstats artifact group again 2018-11-12 04:30:50 +01:00
7f3bb300ef Version 1.13.2 2018-11-10 04:26:27 +01:00
83157ad64f clearlog for entities 2018-11-10 04:22:52 +01:00
cd798e86de Summarization for entities 2018-11-10 04:00:10 +01:00
c3b0fda017 Improve worldeditor row ordering 2018-11-09 21:05:08 +01:00
27c72b43c1 Error message if entity logging is enabled, but no compatible WorldEdit is found 2018-11-09 17:31:39 +01:00
8a1897e102 Add entities to params command 2018-11-09 06:29:26 +01:00
9ea7a47b4e Animals should not contain ambient entities 2018-11-09 06:27:21 +01:00
76aeb5f602 Filter for entity types 2018-11-09 06:24:36 +01:00
1eace44cce Better messages 2018-11-09 05:04:37 +01:00
a5ffa3b709 Log & rollback armor stand/item frame modifications 2018-11-09 05:00:32 +01:00
4e1a79ca0f Configurable entity logging 2018-11-09 04:04:31 +01:00
ea16656fcb Log armor stands, item frames and paintings 2018-11-09 02:37:48 +01:00
9d1baee2e2 Improve entity logging/rollbacks
- Allow UUID changes on respawn
- Log Spawn
- Implement rollback and redo for entities
2018-11-09 00:41:05 +01:00
3a2c1d8d6f Rollback of killed entities works! 2018-11-08 19:13:13 +01:00
868c56ef6a Allow other edits than block edits 2018-11-08 15:54:59 +01:00
f9d246dd63 Implement basic entity change lookup 2018-11-08 05:44:21 +01:00
707e0a1eed Incomplete entity logging 2018-11-08 05:19:59 +01:00
5ad0f06d16 Don't use MyISAM tables 2018-11-08 02:44:04 +01:00
4ce7ad0f7d Tool lookups no longer require WorldEdit for double chests 2018-11-07 20:56:58 +01:00
4af5b3f1ea There can only one of selection/location be set 2018-11-07 20:55:44 +01:00
8c6ee4cf0c Improve CuboidRegion not to use WorldEdit vectors when not needed 2018-11-07 20:45:57 +01:00
737afcd1fa Simplify we adapter 2018-11-07 02:55:29 +01:00
f2dc3daad0 Adapt to latest WorldEdit changes + Compile against Spigot 1.13.2 2018-11-06 01:56:25 +01:00
35e62e03e9 order results by id only (date is not needed there) 2018-11-02 23:57:12 +01:00
40531988b0 optimize log lookup 2018-11-02 03:33:38 +01:00
fde6927aeb Improve recovering from broken connections 2018-10-21 04:21:59 +02:00
3e836c2f50 Remove LogBlockQuestioner soft dependency, as it is no longer a separate plugin 2018-10-19 16:40:17 +02:00
396b79ab68 Remove unused import 2018-10-19 15:50:04 +02:00
be5ee9f792 Fix NPE in FluidFlowLogging
Fixes #724
2018-10-17 14:59:35 +02:00
497e844486 Split long chat lines (for example from command blocks) 2018-10-15 03:05:58 +02:00
90dbc8d8df Improve water flow logging 2018-10-15 02:49:46 +02:00
91a08e7ea0 Fix worldedit dependency (one more exclusion..) 2018-10-12 22:35:23 +02:00
eec8c91a42 Fix worldedit dependency (again..) 2018-10-12 22:31:24 +02:00
a4368ea77f Fix sign logging 2018-10-12 22:30:34 +02:00
7a8551d94f Get the session by entity, not by name 2018-09-22 14:21:25 +02:00
96c9b694b8 Fix: nocache parameter showed entries from the previous lookup 2018-09-22 14:20:13 +02:00
b1e0f91bd7 exclude bStats from WorldEdit dependency as it breaks the build 2018-09-21 22:36:34 +02:00
91315b10c8 New param "nocache" to prevent replacing the lookup cache (useful for tools) 2018-09-21 22:22:52 +02:00
210d6cec37 Log turtle egg trampling 2018-09-21 00:42:28 +02:00
eb99b6d278 Add players: Try a less odd query if the first one failed 2018-09-15 04:05:17 +02:00
a55cbabbdd Add some debug output when adding players fails 2018-09-15 03:36:52 +02:00
421f16784f Don't try to process row if actors could not be inserted 2018-09-13 02:29:21 +02:00
faaab7c4f6 Limit UUIDs to 36 chars + prevent null UUIDs 2018-09-13 02:25:34 +02:00
0bdeb7dc86 Escape UUID in database querys 2018-09-13 02:17:19 +02:00
2bb9e09959 Cleanup Actor.equals() and .hashCode() + some formatting 2018-09-13 02:04:33 +02:00
d214b646db cleanup: getBlockSet/Replaced in class BlockChange 2018-09-12 21:18:43 +02:00
3ef6e48ca4 remove mcstats metrics
its dead for a long time now
2018-09-05 16:21:34 +02:00
1b316dc11f add alternative mvn repo for worldedit 2018-09-05 13:28:33 +02:00
3c64376dd1 improve log importer
- try importing line by line if the batch import did not work
- print more informative error messages
- fix writing failed imports to failed.txt
2018-09-05 12:59:06 +02:00
c390504b70 Sort files before importing them 2018-09-05 03:05:41 +02:00
df8c8bcdda Fix clearing containers on rollback 2018-09-02 04:58:18 +02:00
544385f2ad Log replaced fluids at the correct position 2018-08-29 22:54:35 +02:00
9c2a93dafb Param for force replace not matching blocks 2018-08-29 22:26:37 +02:00
404c9b91c0 Fix bucket empty with waterlogged blocks 2018-08-29 22:05:39 +02:00
1cbc192b31 Log BlockFormEvent by fluids 2018-08-29 19:38:19 +02:00
76fce15305 Log watering waterlogged 2018-08-29 18:33:31 +02:00
8045ab1ecd Ignore deprecation in updater + remove unused method 2018-08-29 03:56:34 +02:00
2faa9cbd6d There is no older WorldEdit for Minecraft 1.13
Resolves #714
2018-08-29 02:43:07 +02:00
f65509408e Log fish buckets as water place, not lava place
Fixes #715
2018-08-29 02:41:10 +02:00
2afa3c88cd Update to Spigot 1.13.1 2018-08-27 17:13:44 +02:00
282090459f Optionally log fluid flow as the player who created the fluid 2018-08-21 06:50:09 +02:00
681c4a2033 Reduce amount of string allocations 2018-08-21 06:29:44 +02:00
56404533db Improved fire logging and rollback 2018-08-21 06:17:24 +02:00
710de5b35a Fix time restriction checking 2018-08-20 18:25:40 +02:00
eb0b969477 add jenkins ci to the readme 2018-08-16 04:18:13 +02:00
c997fb9db9 improve clearlog output when no rows are found 2018-08-13 16:12:03 +02:00
572df51d28 Change the api from using byte[] to YamlConfiguration
byte[] is only used for database rows
2018-08-09 05:00:11 +02:00
ce1a1c3bd2 Fix fire spread logging 2018-08-05 05:45:05 +02:00
51b72bafa6 Update slf4j-jdk14 dependency 2018-08-04 15:42:55 +02:00
dc4d7e0319 We might need the player in clearlog 2018-08-04 15:19:30 +02:00
466775631b Improve clearlog
- Allow clearing of chat & kills table, Resolves #662
- Improve performance by using muti table delete, Fixes #179
- Do not use INTO OUTFILE for dumping logs
2018-08-04 15:09:57 +02:00
83506e6cd9 Cleanup query generation 2018-08-04 05:36:06 +02:00
89d9da02e3 Do not enforce row limit for auto clear logs 2018-08-03 23:04:35 +02:00
c623525847 More secure writelogfile command 2018-08-03 18:29:22 +02:00
cfea33e1fd Fix clearlog table 2018-08-03 18:28:33 +02:00
8b71a8a62b Tool config option dropToDisable
If set to true, the tool will be disabled (and if removeOnDisable is true removed)
when you try to drop it.

Adds #548
2018-08-03 16:59:44 +02:00
e6310e6174 Config option if a tool should be removed on disable 2018-08-03 16:04:07 +02:00
8d11ea3f53 Only remove tools on disable if the player could get them 2018-08-03 15:59:33 +02:00
d04501baf4 Load the config after worlds are loaded
Fixes #267
2018-08-03 15:36:39 +02:00
47f7ddec01 Limit query results
lookup.limesLimit is now correctly used as default limit for lookups
The new lookup.hardLimesLimit is the max limit that is enforced in all
lookups + rollbacks (default 100000)

Fixes #636, Fixes #128
2018-08-03 14:57:08 +02:00
cbf0011cd3 Print a warning and skip invalid data in serialized blockstates 2018-08-03 14:52:39 +02:00
66070e2b38 Do not spam exceptions when the database connection pool is closed
When some database access is still running asynchronously, it will
create an SQLException when the pool is closed (on shutdown). Just
silently ignore them in this case.
2018-08-03 14:20:55 +02:00
6168ef1713 Have an extra map for uncommited playerIds
They will only added to the main list after a successful
commit and cleared otherwise
2018-08-03 14:09:09 +02:00
d0f64070ae Update the javadocs for the Consumer 2018-08-03 03:14:16 +02:00
2c06c90c6d Set a name for the consumer thread 2018-08-03 03:08:23 +02:00
60a049ed49 Rewrite the Consumer to be more stable and much faster
- Use prepared statements and batching for all inserts
- There is now one continuously running Consumer thread
- Use proper synchronization
Fixes #712, Fixes #710

Its a large rewrite so there could still be bugs..
2018-08-03 01:53:28 +02:00
a37dd9cff1 Material mappings don't use the consumer any more 2018-08-03 01:42:15 +02:00
1878b94781 Use batch import for dumped files, its much faster 2018-08-03 01:35:33 +02:00
a853230c8d I don't like tags 2018-08-03 01:04:38 +02:00
3d1f57cc79 ServerPrepStmts ftw 2018-08-03 01:04:02 +02:00
f85dbdbbdf Logging both halfes of a door on interact was a bad idea 2018-08-03 01:03:36 +02:00
e98910615f Not all bisected blocks are double blocks 2018-08-03 01:03:07 +02:00
39a0fbeafa Set default time for tools in the config 2018-08-03 01:02:18 +02:00
ecae2abaf0 Tags are still evil 2018-08-03 01:01:54 +02:00
cc939ab413 Spaces instead of tabs in the pom.xml 2018-08-02 05:28:09 +02:00
680db124a9 Build on java 8 2018-08-02 05:14:26 +02:00
d8e53173e0 Set default time limit also for lookups
Fixes #309
2018-08-02 03:43:18 +02:00
33d97ed971 Improved + rollbackable interact logging
Rollback door openings, comparator, repeater and noteblock modifications and similar things
2018-08-02 02:47:20 +02:00
3dbb7a6c43 Improve chest rollbacks
Fixes #533
2018-08-02 01:18:43 +02:00
0eb2b0dbc8 Force CAVE_AIR and VOID_AIR to AIR + Cleanup rollbacks 2018-08-02 00:50:03 +02:00
37ce2303dc Improve sign logging
Fixes #407
2018-08-01 20:28:21 +02:00
95b7be57fc Dragon Egg teleport logging 2018-08-01 20:02:15 +02:00
92b1a2f394 Improve logging of fallable blocks
Also in this commit: Bukkit's Tags are just broken
2018-08-01 20:01:51 +02:00
5a7ba77095 Make the materials mappings safe to be used by multiple servers at the same time 2018-08-01 16:53:34 +02:00
7a5e46b65f Increase Updater batch size 2018-08-01 03:38:19 +02:00
8084b3e4c0 Respect LAVAFLOW config setting in all cases
Fixes #648
2018-08-01 02:53:49 +02:00
dd58019be1 Clone QueryParams before using them async
Fixes #696, Fixes #684
2018-08-01 02:47:27 +02:00
c30aba4f90 Validate config tool params on execution
Fixes #642 #686
2018-08-01 02:36:06 +02:00
6d13c6436c BEDEXPLOSION logging
Fixes #693
2018-08-01 01:46:50 +02:00
4baa989e60 Player name auto-completion does more harm than good, so don't do that
Fixes #388
2018-08-01 00:38:49 +02:00
1b0a575945 Remove worldedit-core from the pom
It is implicitly added by worldedit-bukkit
2018-08-01 00:32:33 +02:00
cd2dbc0813 Integrate Questioner into Logblock
You do not need the extra plugin any more. If you want no questions asked, just disable it in the config.
2018-08-01 00:28:13 +02:00
fd450aee80 Check if world is logged in LogBlock.getCount 2018-07-31 23:17:26 +02:00
e5097a1577 Compare all aspects if ItemStacks, not only Material 2018-07-31 22:00:17 +02:00
c541847225 Lets keywords have a min amount of arguments
So querys like */lb world world* (Find logs in the world named "world") or */lb player destroyed* (Find logs for a player named "destroyed") are possible
2018-07-31 21:00:50 +02:00
2fe886205b Calculate online time locally
Fixes #645
2018-07-31 18:58:48 +02:00
28aabd09c7 Rename RegionContainer to CuboidRegion 2018-07-31 18:00:28 +02:00
b80777a9a0 Improve WorldEdit encapsulation 2018-07-31 17:58:31 +02:00
c73b29e43b Fix cloning of QueryParams and using them async 2018-07-31 17:40:14 +02:00
354c9078b1 Case insensitive check for ignored chat commands
Fixes #651
2018-07-31 16:16:02 +02:00
759cd230a1 sanity checks for some command params
rollback/redo of chat or kills is not possible
2018-07-31 15:54:21 +02:00
19762691d0 Fix chest access logging (incorrect field name) 2018-07-31 15:35:11 +02:00
1e8779cb9a Add null check for inventory logging
Maybe fix #681
2018-07-31 15:18:06 +02:00
a1d622ebbd Use the plugin logger instead of the global logger 2018-07-31 14:16:34 +02:00
76500f2e51 Always check collation when upgrading tables 2018-07-31 14:08:50 +02:00
d403af42d9 Cancel early when trying to lookup chat while chat is not logged
Fixes #683
2018-07-31 13:33:33 +02:00
f83fc7cee2 Typo 2018-07-31 13:23:45 +02:00
6680e9e3eb Allow querying for item types in chestaccess 2018-07-31 03:01:26 +02:00
58e5cdf890 Serialize BlockStates
Banner, spawner and player heads can now be rolled back
2018-07-31 02:19:40 +02:00
fca9c90032 Improve version parsing for update check 2018-07-30 16:48:09 +02:00
f6e440c1df cleanup (de)serializing of ItemStacks 2018-07-30 16:41:55 +02:00
afb81f850d The uncolored skulker box is also a block 2018-07-30 16:41:12 +02:00
166548da18 Upgrade config, chestaccess and kills too 2018-07-29 15:37:28 +02:00
dc5c8a82c3 Log stack traces for some more exceptions 2018-07-29 15:37:28 +02:00
9f1fc3fe7b Do not check for invalid materials in the config before upgrading them 2018-07-29 15:37:28 +02:00
0bdbce59b8 Legacy materials are not iterated, so don't have to check for that 2018-07-29 15:37:28 +02:00
88faff8230 Formating 2018-07-29 15:37:28 +02:00
71a950a48b Add the new airs to the config 2018-07-29 15:37:28 +02:00
f41a2f0b45 Update repositorys/Remove dead repos 2018-07-29 15:37:28 +02:00
0a7ea8e747 Treat CAVE_AIR and VOID_AIR the same as AIR 2018-07-29 15:37:28 +02:00
905c25a943 Set the maximum size for the connection pool 2018-07-29 15:37:28 +02:00
6e2325d346 The weapon might be null 2018-07-29 15:37:28 +02:00
5cb9dfda50 Update dependencies & maven plugins, add relocations 2018-07-29 15:37:28 +02:00
bdf18fac1e Fix MaterialConverter query 2018-07-29 15:37:28 +02:00
8f18af2252 Convert old blockchange logs 2018-07-29 15:37:28 +02:00
d1d16a6246 Update from 1.13-pre7 to 1.13 2018-07-29 15:37:28 +02:00
84270c59c6 Update default config 2018-07-29 15:37:28 +02:00
ea87835595 Update to WorldEdit for 1.13 2018-07-29 15:37:28 +02:00
6d35da12e3 Access MaterialConverter only from main thread, avoid sync 2018-07-29 15:37:28 +02:00
0792fbd32a Not every material has a BlockData 2018-07-29 15:37:28 +02:00
51505cf34a Better rollback of doors 2018-07-29 15:37:28 +02:00
ce30d33824 Update to 1.13 / Logging/Rollbacks work but largely untested
Still missing: Import data from 1.12 and older
2018-07-29 15:37:28 +02:00
1ce92e1d3f Updated HikariCP to latest version (#707) 2017-12-12 10:01:32 +11:00
0ed8728d01 #703: Chest rollback issues on 1.12.1 2017-08-18 17:57:09 +10:00
40c1cc1c22 #702: MySQL error in upgrader 2017-08-16 20:52:28 +10:00
a51651c306 repo.md-5.net -> https 2017-08-13 19:41:34 +10:00
72851ca52f #700: Change chat columns to 256 2017-08-11 18:31:55 +10:00
defcfeeb07 #698: Misc explosion logging 2017-07-08 18:04:45 +10:00
ba8c1281e3 Change materials.yml location to relative (#697) 2017-06-30 09:01:30 +10:00
80de6eb383 Update for 1.12 (#694) 2017-06-10 08:27:28 +10:00
207c66809f Add separate logging entry for ender crystal explosions and default all posible explosions to true (#669) 2017-02-10 08:19:26 +11:00
ba0e83e437 Update for 1.11 and added shulker boxes (#687) 2016-12-01 09:37:50 +11:00
4195995ca9 Begin 1.10.1 development cycle 2016-05-06 17:12:39 +10:00
5a1c7ec422 1.10.0 release 2016-05-06 17:11:51 +10:00
0e3a04766a We no longer build on java 6 2016-03-01 19:52:45 +00:00
9f319dfb7f Merge pull request #658 from parryjacob/master
Update to use Spigot 1.9
2016-03-02 06:44:50 +11:00
1f1483efab Update to use Spigot 1.9 2016-03-01 03:33:27 -05:00
6faa37928a Update HikariCP, shift over to Java7 2015-09-22 02:33:46 -04:00
080450a278 Include lb-players table in UTF8(mb4) checks
Mojang enforce a very restricted set of characters for names, however offline servers can be sent a much wider set of characters.
Also, fix not using UTF8mb4 on table creation if possible.

Fixes #630
2015-08-03 20:28:44 +01:00
a2c162489a Move to proper three-part versioning (ie 1.9.4 instead of 1.94) and set the dev version to 1.10.0
If it detects a version number of the form 1.xy in the config, it will change that to 1.x.y, so we should make sure we always use e.g. 1.10.0 for version 1.10

Fixes #629
2015-07-30 22:27:06 +01:00
232ddc6323 Added changelog file
If this is reasonably up to date we should be able to copy and paste it directly into DBO and github release pages
2015-07-28 15:59:37 +01:00
f4f4369861 Regularised MySQL String escaping and made name inserts use it 2015-07-27 20:01:15 +01:00
eb31555aa6 Update version for next dev series 2015-07-25 20:25:44 +01:00
108 changed files with 17622 additions and 8438 deletions

14
.editorconfig Normal file
View File

@ -0,0 +1,14 @@
root = true
[*]
charset = utf-8
insert_final_newline = true
trim_trailing_whitespace = true
[*.java]
indent_style = space
indent_size = 4
[*.yml]
indent_style = space
indent_size = 2

11
.gitattributes vendored Normal file
View File

@ -0,0 +1,11 @@
# Set the default behavior, in case people don't have core.autocrlf set.
* text=auto
# Explicitly declare text files you want to always be normalized and converted
# to native line endings on checkout.
*.java text
*.txt text
*.yml text
*.xml text
*.md text
LICENSE text

View File

@ -1,6 +1,5 @@
language: java language: java
jdk: jdk:
- oraclejdk7 - openjdk8
- openjdk6
notifications: notifications:
email: false email: false

103
CHANGELOG.md Normal file
View File

@ -0,0 +1,103 @@
# 1.94
This supports all versions of Bukkit from 1.7.2-R0.3 onwards, including 1.8
## Main changes
* Supports UUIDs - see [this wiki page](https://github.com/LogBlock/LogBlock/wiki/Converting-a-pre-existing-database-to-use-UUIDs) for a short description of the limitations of the migration system
* Supports WorldEdit version 6 and up - unfortunately this means dropping support for previous versions of WorldEdit
* Database insertion optimisation which in tests logged actions six times faster using a local database, and over **250** times faster when the database was on a different server than LogBlock
## Fixes and improvements
* Logs itemdata with values over 256 (logs potion types and item durability)
* Better support for inventory types such as trapped chests
* Fix error when trying to query a double chest without WorldEdit installed
* Correctly log fireballs
* Log players extinguishing fire
* Support utf8mb4 text logging (fixes error when using some obscure characters)
* Overhauled parameter parsing
* Added "/lb hide on" and "/lb hide off" to complement the existing "/lb hide" toggle
* Don't log chat from hidden players
* Fixed error when trying to query illegal triple chests
* Fixed error when a projectile that kills an entity had no source
# 1.80
* Wooden button logging
* Better fireball logging
* Better location querying
* Smarter smart logging
* Add TNT Carts
* Ability to disable sanity ID
* Allow more log rows
* Use UTF-8 for logging
* Comparator, tripwire, pressure plate, crop trample logging added
* Door log fix
* Better messages
* Block spreading
* Fix timezone support
* Death logging fix
* Chest decay
* Various SQL improvements (see source)
# v1.70
* More prepared statements
* Increase to accuracy (Thanks DarkArc)
* WorldEdit logging
* Case insensitive player ignoring
* Consumer warnings + more aggressive defaults (Thanks DarkArc)
# v1.61
* Use more prepared statements
* Fix timestamps on some entries
* Report progress for large rollbacks
# v1.60
* Falling blocks (sand/gravel) are now logged properly in the location they land
* The format of the date displayed in lookups is now customizable under "lookup.dateFormat" in config.yml, please refer to http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html for the display options
* Parameters can now be escaped with double quotes, allowing for spaces in parameters e.g (world "world with spaces")
* Blocks placed now have their data logged properly. Blocks with extra data such as stairs will now be resetted properly during redos
* Fixed bug whereby lava and water logging options in world config weren't considered
* Fixed duplicate "[LogBlock]" tags in message logging
* Fixed y axis not being considered in the radius specified by area
* Fixed possible crash on /lb tp
# v1.59
* Fix * permission
# v1.58
* Require WorldEdit once again.
* 1.57 table updates are optional
# v1.57
* PermissionDefault to OP
* Increase default table sizes
# v1.56
* Invert chest logging
* Remove worldedit auto download
# v1.55
* Transition to maven
* Use async chat event
* Add metrics
* Small performance increases
# v1.54
* Fix derpy fake inventory plugins throwing npes.
# v1.53
* Make use of the new chest logging system
* Remove the use of old permissions systems
# v1.52
* Properly log block replacement.
* Added option to disallow tool dropping to prevent item duplication.
* Removed double logblock.tp permission.
* Fixed config updater error.
* Check if EntityChangeBlockEvent is triggered by Enderman.
* Fixed onlinetime logging for players.
* Fixed npe in /lb me.

Binary file not shown.

View File

@ -4,4 +4,4 @@ LogBlock
This plugin logs block changes such as breaking, placing, modifying, or burning to a MySQL Database. It can be used as an anti-griefing tool to find out who made a particular edit, or even roll back changes by certain players. This plugin logs block changes such as breaking, placing, modifying, or burning to a MySQL Database. It can be used as an anti-griefing tool to find out who made a particular edit, or even roll back changes by certain players.
Originally written by bootswithdefer, for hMod, ported to Bukkit by me, because of the inability to identfy griefers. BigBrother also did't work, so I was forced to do it myself. The honor belongs to bootswithdefer for the sourcecode, I only spent about 8 hours to transcribe. All functions except sign text logging shold work as in hMod. The use of permissions plugin is possible, but not necessary. Originally written by bootswithdefer, for hMod, ported to Bukkit by me, because of the inability to identfy griefers. BigBrother also did't work, so I was forced to do it myself. The honor belongs to bootswithdefer for the sourcecode, I only spent about 8 hours to transcribe. All functions except sign text logging shold work as in hMod. The use of permissions plugin is possible, but not necessary.
Questioner: http://git.io/u2MxKQ You can download development builds [from our Jenkins server](https://www.iani.de/jenkins/job/LogBlock/).

76
pom.xml
View File

@ -1,10 +1,10 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>de.diddiz</groupId> <groupId>de.diddiz</groupId>
<artifactId>logblock</artifactId> <artifactId>logblock</artifactId>
<version>1.94</version> <version>1.20.0.0-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>LogBlock</name> <name>LogBlock</name>
@ -29,66 +29,61 @@
<distributionManagement> <distributionManagement>
<repository> <repository>
<id>md_5-releases</id> <id>nexus</id>
<url>http://repo.md-5.net/content/repositories/releases/</url> <name>Releases</name>
<url>https://www.iani.de/nexus/content/repositories/releases/</url>
</repository> </repository>
<snapshotRepository> <snapshotRepository>
<id>md_5-snapshots</id> <id>nexus</id>
<url>http://repo.md-5.net/content/repositories/snapshots/</url> <name>Snapshot</name>
<url>https://www.iani.de/nexus/content/repositories/snapshots/</url>
</snapshotRepository> </snapshotRepository>
</distributionManagement> </distributionManagement>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.bukkit</groupId> <groupId>org.spigotmc</groupId>
<artifactId>bukkit</artifactId> <artifactId>spigot-api</artifactId>
<version>1.7.2-R0.3</version> <version>1.20.4-R0.1-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>${project.groupId}</groupId> <groupId>com.sk89q.worldedit</groupId>
<artifactId>questioner</artifactId> <artifactId>worldedit-bukkit</artifactId>
<version>${project.version}</version> <version>7.3.0</version>
<scope>system</scope> <scope>provided</scope>
<systemPath>${project.basedir}/LogBlockQuestioner.jar</systemPath>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.sk89q</groupId> <groupId>com.sk89q.worldguard</groupId>
<artifactId>worldedit</artifactId> <artifactId>worldguard-bukkit</artifactId>
<version>6.0.0-SNAPSHOT</version> <version>7.1.0-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<version>4.11</version> <version>4.13.2</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.zaxxer</groupId> <groupId>com.zaxxer</groupId>
<artifactId>HikariCP-java6</artifactId> <artifactId>HikariCP</artifactId>
<version>2.3.8</version> <version>5.1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.10</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<repositories> <repositories>
<repository> <repository>
<id>repobo-snap</id> <id>spigot-repo</id>
<url>http://repo.bukkit.org/content/groups/public</url> <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository> </repository>
<repository> <repository>
<id>sk89q-repo</id> <id>sk89q-repo</id>
<url>http://maven.sk89q.com/repo/</url> <url>https://maven.enginehub.org/repo/</url>
</repository> </repository>
<repository> <repository>
<id>kitteh-repo</id> <id>brokkonaut-repo</id>
<url>http://repo.kitteh.org/content/groups/public</url> <url>https://www.iani.de/nexus/content/groups/public/</url>
</repository> </repository>
</repositories> </repositories>
<profiles> <profiles>
@ -123,23 +118,22 @@
<resources> <resources>
<resource> <resource>
<filtering>true</filtering> <filtering>true</filtering>
<directory>${project.basedir}/src/main/resources</directory> <directory>src/main/resources</directory>
</resource> </resource>
</resources> </resources>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version> <version>3.12.1</version>
<configuration> <configuration>
<source>1.6</source> <release>17</release>
<target>1.6</target>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>
<groupId>org.codehaus.mojo</groupId> <groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId> <artifactId>build-helper-maven-plugin</artifactId>
<version>1.9.1</version> <version>3.5.0</version>
<executions> <executions>
<execution> <execution>
<id>regex-property</id> <id>regex-property</id>
@ -159,7 +153,7 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId> <artifactId>maven-shade-plugin</artifactId>
<version>2.3</version> <version>3.5.1</version>
<configuration> <configuration>
</configuration> </configuration>
<executions> <executions>
@ -168,6 +162,14 @@
<goals> <goals>
<goal>shade</goal> <goal>shade</goal>
</goals> </goals>
<configuration>
<relocations>
<relocation>
<pattern>com.zaxxer.hikari</pattern>
<shadedPattern>de.diddiz.lib.com.zaxxer.hikari</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution> </execution>
</executions> </executions>
</plugin> </plugin>

View File

@ -1,129 +1,182 @@
package de.diddiz.LogBlock; package de.diddiz.LogBlock;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.projectiles.BlockProjectileSource; import org.bukkit.entity.Projectile;
import org.bukkit.projectiles.ProjectileSource; import org.bukkit.projectiles.BlockProjectileSource;
import org.bukkit.projectiles.ProjectileSource;
import java.sql.ResultSet;
import java.sql.SQLException; import static de.diddiz.LogBlock.util.BukkitUtils.entityName;
import static de.diddiz.util.BukkitUtils.entityName; import java.sql.ResultSet;
import org.bukkit.Bukkit; import java.sql.SQLException;
import org.bukkit.Bukkit;
public class Actor { import org.bukkit.Location;
import org.bukkit.block.Block;
@Override
public int hashCode() { public class Actor {
int hash = 5;
hash = 79 * hash + (this.UUID != null ? this.UUID.hashCode() : 0); @Override
return hash; public int hashCode() {
} return this.UUID != null ? this.UUID.hashCode() : 0;
}
@Override
public boolean equals(Object obj) { @Override
if (obj == null) { public boolean equals(Object obj) {
return false; if (obj == null || getClass() != obj.getClass()) {
} return false;
if (getClass() != obj.getClass()) { }
return false; final Actor other = (Actor) obj;
} return (this.UUID == null) ? (other.UUID == null) : this.UUID.equals(other.UUID);
final Actor other = (Actor) obj; }
return ((this.UUID == null && other.UUID == null) || this.UUID.equals(other.UUID));
} final String name;
final String UUID;
final String name; final Location blockLocation;
final String UUID; final Entity entity;
public Actor(String name, String UUID) { public Actor(String name, String UUID) {
this.name = name; this.name = name;
this.UUID = UUID; this.UUID = UUID == null ? "unknown" : (UUID.length() > 36 ? UUID.substring(0, 36) : UUID);
this.blockLocation = null;
} this.entity = null;
}
public Actor(String name, java.util.UUID UUID) {
this.name = name; public Actor(String name, String UUID, Block block) {
this.UUID = UUID.toString(); this.name = name;
this.UUID = UUID == null ? "unknown" : (UUID.length() > 36 ? UUID.substring(0, 36) : UUID);
} this.blockLocation = block == null ? null : block.getLocation();
this.entity = null;
public Actor(String name) { }
this(name, generateUUID(name));
} public Actor(String name, java.util.UUID UUID) {
this.name = name;
public Actor(ResultSet rs) throws SQLException { this.UUID = UUID.toString();
this(rs.getString("playername"), rs.getString("UUID")); this.blockLocation = null;
} this.entity = null;
}
public String getName() {
return name; public Actor(String name, java.util.UUID UUID, Block block) {
} this.name = name;
this.UUID = UUID.toString();
public String getUUID() { this.blockLocation = block == null ? null : block.getLocation();
return UUID; this.entity = null;
} }
public static Actor actorFromEntity(Entity entity) { public Actor(String name, java.util.UUID UUID, Entity entity) {
if (entity instanceof Player) { this.name = name;
return new Actor(entityName(entity), entity.getUniqueId()); this.UUID = UUID.toString();
} else { this.blockLocation = null;
return new Actor(entityName(entity)); this.entity = entity;
} }
}
public Actor(String name) {
public static Actor actorFromEntity(EntityType entity) { this(name, generateUUID(name));
return new Actor(entity.getName()); }
}
public Actor(String name, Block block) {
public static Actor actorFromProjectileSource(ProjectileSource psource) { this(name, generateUUID(name), block);
if (psource instanceof Entity) { }
return actorFromEntity((Entity) psource);
} public Actor(String name, Entity entity) {
if (psource instanceof BlockProjectileSource) { this.name = name;
return new Actor(((BlockProjectileSource) psource).getBlock().getType().toString()); this.UUID = generateUUID(name);
} else { this.blockLocation = null;
return new Actor(psource.toString()); this.entity = entity;
} }
} public Actor(ResultSet rs) throws SQLException {
/** this(rs.getString("playername"), rs.getString("UUID"));
* Generate an Actor object from a String name, trying to guess if it's an online player }
* and if so, setting the UUID accordingly. This only checks against currently online
* players and is a "best effort" attempt for use with the pre-UUID API public String getName() {
* <p> return name;
* If you know something is an entity (player or otherwise) use the {@link #actorFromEntity(org.bukkit.entity.Entity) } }
* or {@link #actorFromEntity(org.bukkit.entity.EntityType) } methods
* <p> public String getUUID() {
* If you know something is a server effect (like gravity) use {@link #Actor(java.lang.String)} return UUID;
* @deprecated Only use this if you have a String of unknown origin }
*
* @param actorName String of unknown origin public Location getBlockLocation() {
* @return return blockLocation;
*/ }
public static Actor actorFromString(String actorName) {
Player[] players = Bukkit.getServer().getOnlinePlayers(); /**
for (Player p : players) { * The acting entity object (if known)
if (p.getName().equalsIgnoreCase(actorName)) { */
return actorFromEntity(p); public Entity getEntity() {
} return entity;
} }
// No player found online with that name, assuming non-player entity/effect
return new Actor(actorName); public static Actor actorFromEntity(Entity entity) {
} if (entity instanceof Player) {
return new Actor(entityName(entity), entity.getUniqueId(), entity);
public static boolean isValidUUID(String uuid) { }
try { if (entity instanceof Projectile) {
java.util.UUID.fromString(uuid); ProjectileSource shooter = ((Projectile) entity).getShooter();
return true; if (shooter != null) {
} catch (IllegalArgumentException e) { return actorFromProjectileSource(shooter);
return false; }
} }
} return new Actor(entityName(entity), entity);
}
public static String generateUUID(String name) {
return "log_" + name; @Deprecated
public static Actor actorFromEntity(EntityType entity) {
} return new Actor(entity.name());
}
}
public static Actor actorFromProjectileSource(ProjectileSource psource) {
if (psource instanceof Entity) {
return actorFromEntity((Entity) psource);
}
if (psource instanceof BlockProjectileSource) {
return new Actor(((BlockProjectileSource) psource).getBlock().getType().toString());
} else {
return new Actor(psource.toString());
}
}
/**
* Generate an Actor object from a String name, trying to guess if it's an online player
* and if so, setting the UUID accordingly. This only checks against currently online
* players and is a "best effort" attempt for use with the pre-UUID API
* <p>
* If you know something is an entity (player or otherwise) use the {@link #actorFromEntity(org.bukkit.entity.Entity) }
* or {@link #actorFromEntity(org.bukkit.entity.EntityType) } methods
* <p>
* If you know something is a server effect (like gravity) use {@link #Actor(java.lang.String)}
*
* @deprecated Only use this if you have a String of unknown origin
*
* @param actorName
* String of unknown origin
* @return
*/
@Deprecated
public static Actor actorFromString(String actorName) {
Player p = Bukkit.getServer().getPlayerExact(actorName);
if (p != null) {
return actorFromEntity(p);
}
// No player found online with that name, assuming non-player entity/effect
return new Actor(actorName);
}
public static boolean isValidUUID(String uuid) {
try {
java.util.UUID.fromString(uuid);
return true;
} catch (IllegalArgumentException e) {
return false;
}
}
public static String generateUUID(String name) {
return "log_" + name;
}
}

View File

@ -1,28 +1,32 @@
package de.diddiz.LogBlock; package de.diddiz.LogBlock;
import java.util.Arrays; import java.util.Arrays;
import java.util.logging.Level; import java.util.logging.Level;
import static de.diddiz.LogBlock.config.Config.autoClearLog; import static de.diddiz.LogBlock.config.Config.autoClearLog;
import static org.bukkit.Bukkit.*; import static org.bukkit.Bukkit.*;
public class AutoClearLog implements Runnable { public class AutoClearLog implements Runnable {
private final LogBlock logblock; private final LogBlock logblock;
AutoClearLog(LogBlock logblock) { AutoClearLog(LogBlock logblock) {
this.logblock = logblock; this.logblock = logblock;
} }
@Override @Override
public void run() { public void run() {
final CommandsHandler handler = logblock.getCommandsHandler(); final CommandsHandler handler = logblock.getCommandsHandler();
for (final String paramStr : autoClearLog) { for (final String paramStr : autoClearLog) {
try { if (!logblock.isCompletelyEnabled()) {
final QueryParams params = new QueryParams(logblock, getConsoleSender(), Arrays.asList(paramStr.split(" "))); return; // do not try when plugin is disabled
handler.new CommandClearLog(getServer().getConsoleSender(), params, false); }
} catch (final Exception ex) { try {
getLogger().log(Level.SEVERE, "Failed to schedule auto ClearLog: ", ex); final QueryParams params = new QueryParams(logblock, getConsoleSender(), Arrays.asList(paramStr.split(" ")));
} params.noForcedLimit = true;
} handler.new CommandClearLog(getServer().getConsoleSender(), params, false);
} } catch (final Exception ex) {
} getLogger().log(Level.SEVERE, "Failed to schedule auto ClearLog: ", ex);
}
}
}
}

View File

@ -1,128 +1,272 @@
package de.diddiz.LogBlock; package de.diddiz.LogBlock;
import de.diddiz.LogBlock.config.Config; import static de.diddiz.LogBlock.util.ActionColor.CREATE;
import de.diddiz.util.BukkitUtils; import static de.diddiz.LogBlock.util.ActionColor.DESTROY;
import org.bukkit.Location; import static de.diddiz.LogBlock.util.ActionColor.INTERACT;
import org.bukkit.Material; import static de.diddiz.LogBlock.util.MessagingUtil.createTextComponentWithColor;
import static de.diddiz.LogBlock.util.MessagingUtil.prettyDate;
import java.sql.ResultSet; import static de.diddiz.LogBlock.util.MessagingUtil.prettyLocation;
import java.sql.SQLException; import static de.diddiz.LogBlock.util.MessagingUtil.prettyMaterial;
import static de.diddiz.LogBlock.util.MessagingUtil.prettyState;
import static de.diddiz.util.LoggingUtil.checkText; import static de.diddiz.LogBlock.util.TypeColor.DEFAULT;
import static de.diddiz.util.MaterialName.materialName;
import de.diddiz.LogBlock.blockstate.BlockStateCodecs;
public class BlockChange implements LookupCacheElement { import de.diddiz.LogBlock.util.BukkitUtils;
public final long id, date; import de.diddiz.LogBlock.util.Utils;
public final Location loc; import java.sql.ResultSet;
public final Actor actor; import java.sql.SQLException;
public final String playerName; import java.util.logging.Level;
public final int replaced, type; import net.md_5.bungee.api.chat.BaseComponent;
public final byte data; import net.md_5.bungee.api.chat.TextComponent;
public final String signtext; import org.bukkit.Location;
public final ChestAccess ca; import org.bukkit.Material;
import org.bukkit.Note;
public BlockChange(long date, Location loc, Actor actor, int replaced, int type, byte data, String signtext, ChestAccess ca) { import org.bukkit.block.data.BlockData;
id = 0; import org.bukkit.block.data.Lightable;
this.date = date; import org.bukkit.block.data.Openable;
this.loc = loc; import org.bukkit.block.data.Powerable;
this.actor = actor; import org.bukkit.block.data.Waterlogged;
this.replaced = replaced; import org.bukkit.block.data.type.Candle;
this.type = type; import org.bukkit.block.data.type.Comparator;
this.data = data; import org.bukkit.block.data.type.DaylightDetector;
this.signtext = checkText(signtext); import org.bukkit.block.data.type.Lectern;
this.ca = ca; import org.bukkit.block.data.type.NoteBlock;
this.playerName = actor == null ? null : actor.getName(); import org.bukkit.block.data.type.Repeater;
} import org.bukkit.block.data.type.Sign;
import org.bukkit.block.data.type.Switch;
public BlockChange(ResultSet rs, QueryParams p) throws SQLException { import org.bukkit.block.data.type.WallSign;
id = p.needId ? rs.getInt("id") : 0; import org.bukkit.inventory.ItemStack;
date = p.needDate ? rs.getTimestamp("date").getTime() : 0;
loc = p.needCoords ? new Location(p.world, rs.getInt("x"), rs.getInt("y"), rs.getInt("z")) : null; public class BlockChange implements LookupCacheElement {
actor = p.needPlayer ? new Actor(rs) : null; public final long id, date;
playerName = p.needPlayer ? rs.getString("playername") : null; public final Location loc;
replaced = p.needType ? rs.getInt("replaced") : 0; public final Actor actor;
type = p.needType ? rs.getInt("type") : 0; public final String playerName;
data = p.needData ? rs.getByte("data") : (byte) 0; public final int replacedMaterial, replacedData, typeMaterial, typeData;
signtext = p.needSignText ? rs.getString("signtext") : null; public final byte[] replacedState, typeState;
ca = p.needChestAccess && rs.getShort("itemtype") != 0 && rs.getShort("itemamount") != 0 ? new ChestAccess(rs.getShort("itemtype"), rs.getShort("itemamount"), rs.getShort("itemdata")) : null; public final ChestAccess ca;
}
public BlockChange(long date, Location loc, Actor actor, int replaced, int replacedData, byte[] replacedState, int type, int typeData, byte[] typeState, ChestAccess ca) {
@Override id = 0;
public String toString() { this.date = date;
final StringBuilder msg = new StringBuilder(); this.loc = loc;
if (date > 0) { this.actor = actor;
msg.append(Config.formatter.format(date)).append(" "); this.replacedMaterial = replaced;
} this.replacedData = replacedData;
if (actor != null) { this.replacedState = replacedState;
msg.append(actor.getName()).append(" "); this.typeMaterial = type;
} this.typeData = typeData;
if (signtext != null) { this.typeState = typeState;
final String action = type == 0 ? "destroyed " : "created "; this.ca = ca;
if (!signtext.contains("\0")) { this.playerName = actor == null ? null : actor.getName();
msg.append(action).append(signtext); }
} else {
msg.append(action).append(materialName(type != 0 ? type : replaced)).append(" [").append(signtext.replace("\0", "] [")).append("]"); public BlockChange(ResultSet rs, QueryParams p) throws SQLException {
} id = p.needId ? rs.getLong("id") : 0;
} else if (type == replaced) { date = p.needDate ? rs.getTimestamp("date").getTime() : 0;
if (type == 0) { loc = p.needCoords ? new Location(p.world, rs.getInt("x"), rs.getInt("y"), rs.getInt("z")) : null;
msg.append("did an unspecified action"); actor = p.needPlayer ? new Actor(rs) : null;
} else if (ca != null) { playerName = p.needPlayer ? rs.getString("playername") : null;
if (ca.itemType == 0 || ca.itemAmount == 0) { replacedMaterial = p.needType ? rs.getInt("replaced") : 0;
msg.append("looked inside ").append(materialName(type)); replacedData = p.needType ? rs.getInt("replacedData") : -1;
} else if (ca.itemAmount < 0) { typeMaterial = p.needType ? rs.getInt("type") : 0;
msg.append("took ").append(-ca.itemAmount).append("x ").append(materialName(ca.itemType, ca.itemData)).append(" from ").append(materialName(type)); typeData = p.needType ? rs.getInt("typeData") : -1;
} else { replacedState = p.needType ? rs.getBytes("replacedState") : null;
msg.append("put ").append(ca.itemAmount).append("x ").append(materialName(ca.itemType, ca.itemData)).append(" into ").append(materialName(type)); typeState = p.needType ? rs.getBytes("typeState") : null;
} ChestAccess catemp = null;
} else if (BukkitUtils.getContainerBlocks().contains(Material.getMaterial(type))) { if (p.needChestAccess) {
msg.append("opened ").append(materialName(type)); ItemStack stack = Utils.loadItemStack(rs.getBytes("item"));
} else if (type == 64 || type == 71) if (stack != null) {
// This is a problem that will have to be addressed in LB 2, catemp = new ChestAccess(stack, rs.getBoolean("itemremove"), rs.getInt("itemtype"));
// there is no way to tell from the top half of the block if }
// the door is opened or closed. }
{ ca = catemp;
msg.append("moved ").append(materialName(type)); }
}
// Trapdoor private BaseComponent getTypeDetails(BlockData type, byte[] typeState) {
else if (type == 96) { return getTypeDetails(type, typeState, null, null);
msg.append((data < 8 || data > 11) ? "opened" : "closed").append(" ").append(materialName(type)); }
}
// Fence gate private BaseComponent getTypeDetails(BlockData type, byte[] typeState, BlockData oldType, byte[] oldTypeState) {
else if (type == 107) { BaseComponent typeDetails = null;
msg.append(data > 3 ? "opened" : "closed").append(" ").append(materialName(type));
} else if (type == 69) { if (BlockStateCodecs.hasCodec(type.getMaterial())) {
msg.append("switched ").append(materialName(type)); try {
} else if (type == 77 || type == 143) { typeDetails = BlockStateCodecs.getChangesAsComponent(type.getMaterial(), Utils.deserializeYamlConfiguration(typeState), type.equals(oldType) ? Utils.deserializeYamlConfiguration(oldTypeState) : null);
msg.append("pressed ").append(materialName(type)); } catch (Exception e) {
} else if (type == 92) { LogBlock.getInstance().getLogger().log(Level.SEVERE, "Could not parse BlockState for " + type.getMaterial(), e);
msg.append("ate a piece of ").append(materialName(type)); }
} else if (type == 25 || type == 93 || type == 94 || type == 149 || type == 150) { }
msg.append("changed ").append(materialName(type));
} else if (type == 70 || type == 72 || type == 147 || type == 148) { if (typeDetails == null) {
msg.append("stepped on ").append(materialName(type)); return new TextComponent("");
} else if (type == 132) { } else {
msg.append("ran into ").append(materialName(type)); TextComponent component = new TextComponent(" ");
} component.addExtra(typeDetails);
} else if (type == 0) { return component;
msg.append("destroyed ").append(materialName(replaced, data)); }
} else if (replaced == 0) { }
msg.append("created ").append(materialName(type, data));
} else { @Override
msg.append("replaced ").append(materialName(replaced, (byte) 0)).append(" with ").append(materialName(type, data)); public String toString() {
} return BaseComponent.toPlainText(getLogMessage(-1));
if (loc != null) { }
msg.append(" at ").append(loc.getBlockX()).append(":").append(loc.getBlockY()).append(":").append(loc.getBlockZ());
} @Override
return msg.toString(); public BaseComponent[] getLogMessage(int entry) {
} TextComponent msg = new TextComponent();
if (date > 0) {
@Override msg.addExtra(prettyDate(date));
public Location getLocation() { msg.addExtra(" ");
return loc; }
} if (actor != null) {
msg.addExtra(actor.getName());
@Override msg.addExtra(" ");
public String getMessage() { }
return toString(); BlockData type = getBlockSet();
} BlockData replaced = getBlockReplaced();
} if (type == null || replaced == null) {
msg.addExtra("did an unknown block modification");
return new BaseComponent[] { msg };
}
// Process type details once for later use.
BaseComponent typeDetails = getTypeDetails(type, typeState, replaced, replacedState);
BaseComponent replacedDetails = getTypeDetails(replaced, replacedState);
if (type.getMaterial().equals(replaced.getMaterial()) || (type.getMaterial() == Material.CAKE && BukkitUtils.isCandleCake(replaced.getMaterial()))) {
if (BukkitUtils.isEmpty(type.getMaterial())) {
msg.addExtra(createTextComponentWithColor("did an unspecified action", INTERACT.getColor()));
} else if (ca != null) {
if (ca.itemStack == null) {
msg.addExtra(createTextComponentWithColor("looked inside ", INTERACT.getColor()));
msg.addExtra(prettyMaterial(type));
} else if (ca.remove) {
msg.addExtra(createTextComponentWithColor("took ", DESTROY.getColor()));
msg.addExtra(BukkitUtils.toString(ca.itemStack));
msg.addExtra(createTextComponentWithColor(" from ", DESTROY.getColor()));
msg.addExtra(prettyMaterial(type));
} else {
msg.addExtra(createTextComponentWithColor("put ", CREATE.getColor()));
msg.addExtra(BukkitUtils.toString(ca.itemStack));
msg.addExtra(createTextComponentWithColor(" into ", CREATE.getColor()));
msg.addExtra(prettyMaterial(type));
}
} else if (type instanceof Waterlogged && ((Waterlogged) type).isWaterlogged() != ((Waterlogged) replaced).isWaterlogged()) {
if (((Waterlogged) type).isWaterlogged()) {
msg.addExtra(createTextComponentWithColor("waterlogged ", CREATE.getColor()));
msg.addExtra(prettyMaterial(type));
} else {
msg.addExtra(createTextComponentWithColor("dried ", DESTROY.getColor()));
msg.addExtra(prettyMaterial(type));
}
} else if (BukkitUtils.isContainerBlock(type.getMaterial())) {
msg.addExtra(createTextComponentWithColor("opened ", INTERACT.getColor()));
msg.addExtra(prettyMaterial(type));
} else if (type instanceof Openable && ((Openable) type).isOpen() != ((Openable) replaced).isOpen()) {
// Door, Trapdoor, Fence gate
msg.addExtra(createTextComponentWithColor(((Openable) type).isOpen() ? "opened " : "closed ", INTERACT.getColor()));
msg.addExtra(prettyMaterial(type));
} else if (type.getMaterial() == Material.LEVER && ((Switch) type).isPowered() != ((Switch) replaced).isPowered()) {
msg.addExtra(createTextComponentWithColor("switched ", INTERACT.getColor()));
msg.addExtra(prettyMaterial(type));
msg.addExtra(prettyState(((Switch) type).isPowered() ? " on" : " off"));
} else if (type instanceof Switch && ((Switch) type).isPowered() != ((Switch) replaced).isPowered()) {
msg.addExtra(createTextComponentWithColor("pressed ", INTERACT.getColor()));
msg.addExtra(prettyMaterial(type));
} else if (type.getMaterial() == Material.CAKE) {
msg.addExtra(createTextComponentWithColor("ate a piece of ", DESTROY.getColor()));
msg.addExtra(prettyMaterial(type));
} else if (type.getMaterial() == Material.NOTE_BLOCK) {
Note note = ((NoteBlock) type).getNote();
msg.addExtra(createTextComponentWithColor("set ", INTERACT.getColor()));
msg.addExtra(prettyMaterial(type));
msg.addExtra(" to ");
msg.addExtra(prettyState(note.getTone().name() + (note.isSharped() ? "#" : "")));
} else if (type.getMaterial() == Material.REPEATER) {
msg.addExtra(createTextComponentWithColor("set ", INTERACT.getColor()));
msg.addExtra(prettyMaterial(type));
msg.addExtra(" to ");
msg.addExtra(prettyState(((Repeater) type).getDelay()));
msg.addExtra(createTextComponentWithColor(" ticks delay", DEFAULT.getColor()));
} else if (type.getMaterial() == Material.COMPARATOR) {
msg.addExtra(createTextComponentWithColor("set ", INTERACT.getColor()));
msg.addExtra(prettyMaterial(type));
msg.addExtra(" to ");
msg.addExtra(prettyState(((Comparator) type).getMode()));
} else if (type.getMaterial() == Material.DAYLIGHT_DETECTOR) {
msg.addExtra(createTextComponentWithColor("set ", INTERACT.getColor()));
msg.addExtra(prettyMaterial(type));
msg.addExtra(" to ");
msg.addExtra(prettyState(((DaylightDetector) type).isInverted() ? "inverted" : "normal"));
} else if (type instanceof Lectern) {
msg.addExtra(createTextComponentWithColor("changed the book on a ", INTERACT.getColor()));
msg.addExtra(prettyMaterial(type));
msg.addExtra(" to");
msg.addExtra(prettyState(typeDetails));
} else if (type instanceof Powerable) {
msg.addExtra(createTextComponentWithColor("stepped on ", INTERACT.getColor()));
msg.addExtra(prettyMaterial(type));
} else if (type.getMaterial() == Material.TRIPWIRE) {
msg.addExtra(createTextComponentWithColor("ran into ", INTERACT.getColor()));
msg.addExtra(prettyMaterial(type));
} else if (type instanceof Sign || type instanceof WallSign) {
msg.addExtra(createTextComponentWithColor("edited a ", CREATE.getColor()));
msg.addExtra(prettyMaterial(type));
msg.addExtra(createTextComponentWithColor(" to", CREATE.getColor()));
msg.addExtra(prettyState(typeDetails));
} else if (type instanceof Candle && ((Candle) type).getCandles() != ((Candle) replaced).getCandles()) {
msg.addExtra(createTextComponentWithColor("added a candle to ", CREATE.getColor()));
msg.addExtra(prettyMaterial(type));
} else if ((type instanceof Candle || BukkitUtils.isCandleCake(type.getMaterial())) && ((Lightable) type).isLit() != ((Lightable) replaced).isLit()) {
if (((Lightable) type).isLit()) {
msg.addExtra(createTextComponentWithColor("lit a ", CREATE.getColor()));
msg.addExtra(prettyMaterial(type));
} else {
msg.addExtra(createTextComponentWithColor("extinguished a ", CREATE.getColor()));
msg.addExtra(prettyMaterial(type));
}
} else {
msg.addExtra(createTextComponentWithColor("replaced ", CREATE.getColor()));
msg.addExtra(prettyMaterial(replaced));
msg.addExtra(prettyState(replacedDetails));
msg.addExtra(createTextComponentWithColor(" with ", CREATE.getColor()));
msg.addExtra(prettyMaterial(type));
msg.addExtra(prettyState(typeDetails));
}
} else if (BukkitUtils.isEmpty(type.getMaterial())) {
msg.addExtra(createTextComponentWithColor("destroyed ", DESTROY.getColor()));
msg.addExtra(prettyMaterial(replaced));
msg.addExtra(prettyState(replacedDetails));
} else if (BukkitUtils.isEmpty(replaced.getMaterial())) {
msg.addExtra(createTextComponentWithColor("created ", CREATE.getColor()));
msg.addExtra(prettyMaterial(type));
msg.addExtra(prettyState(typeDetails));
} else {
msg.addExtra(createTextComponentWithColor("replaced ", CREATE.getColor()));
msg.addExtra(prettyMaterial(replaced));
msg.addExtra(prettyState(replacedDetails));
msg.addExtra(createTextComponentWithColor(" with ", CREATE.getColor()));
msg.addExtra(prettyMaterial(type));
msg.addExtra(prettyState(typeDetails));
}
if (loc != null) {
msg.addExtra(" at ");
msg.addExtra(prettyLocation(loc, entry));
}
return new BaseComponent[] { msg };
}
public BlockData getBlockReplaced() {
return MaterialConverter.getBlockData(replacedMaterial, replacedData);
}
public BlockData getBlockSet() {
return MaterialConverter.getBlockData(typeMaterial, typeData);
}
@Override
public Location getLocation() {
return loc;
}
}

View File

@ -1,40 +1,59 @@
package de.diddiz.LogBlock; package de.diddiz.LogBlock;
import org.bukkit.Location; import static de.diddiz.LogBlock.util.LoggingUtil.checkText;
import static de.diddiz.LogBlock.util.MessagingUtil.brackets;
import java.sql.ResultSet; import static de.diddiz.LogBlock.util.MessagingUtil.prettyDate;
import java.sql.SQLException;
import de.diddiz.LogBlock.util.MessagingUtil;
import static de.diddiz.util.LoggingUtil.checkText; import de.diddiz.LogBlock.util.MessagingUtil.BracketType;
import java.sql.ResultSet;
public class ChatMessage implements LookupCacheElement { import java.sql.SQLException;
final long id, date; import net.md_5.bungee.api.chat.BaseComponent;
final String playerName, message; import net.md_5.bungee.api.chat.TextComponent;
final Actor player; import org.bukkit.Location;
public ChatMessage(Actor player, String message) { public class ChatMessage implements LookupCacheElement {
id = 0; final long id, date;
date = System.currentTimeMillis() / 1000; final String playerName, message;
this.player = player; final Actor player;
this.message = checkText(message);
this.playerName = player == null ? null : player.getName(); public ChatMessage(Actor player, String message) {
} id = 0;
date = System.currentTimeMillis() / 1000;
public ChatMessage(ResultSet rs, QueryParams p) throws SQLException { this.player = player;
id = p.needId ? rs.getInt("id") : 0; this.message = checkText(message);
date = p.needDate ? rs.getTimestamp("date").getTime() : 0; this.playerName = player == null ? null : player.getName();
player = p.needPlayer ? new Actor(rs) : null; }
playerName = p.needPlayer ? rs.getString("playername") : null;
message = p.needMessage ? rs.getString("message") : null; public ChatMessage(ResultSet rs, QueryParams p) throws SQLException {
} id = p.needId ? rs.getLong("id") : 0;
date = p.needDate ? rs.getTimestamp("date").getTime() : 0;
@Override player = p.needPlayer ? new Actor(rs) : null;
public Location getLocation() { playerName = p.needPlayer ? rs.getString("playername") : null;
return null; message = p.needMessage ? rs.getString("message") : null;
} }
@Override @Override
public String getMessage() { public Location getLocation() {
return (player != null ? "<" + player.getName() + "> " : "") + (message != null ? message : ""); return null;
} }
}
@Override
public BaseComponent[] getLogMessage(int entry) {
TextComponent msg = new TextComponent();
if (date > 0) {
msg.addExtra(prettyDate(date));
msg.addExtra(" ");
}
if (playerName != null) {
msg.addExtra(brackets(BracketType.ANGLE, MessagingUtil.createTextComponentWithColor(playerName, net.md_5.bungee.api.ChatColor.WHITE)));
msg.addExtra(" ");
}
if (message != null) {
for (BaseComponent messageComponent : TextComponent.fromLegacyText(message)) {
msg.addExtra(messageComponent);
}
}
return new BaseComponent[] { msg };
}
}

View File

@ -1,11 +1,15 @@
package de.diddiz.LogBlock; package de.diddiz.LogBlock;
public class ChestAccess { import org.bukkit.inventory.ItemStack;
final short itemType, itemAmount, itemData;
public class ChestAccess {
public ChestAccess(short itemType, short itemAmount, short itemData) { public final ItemStack itemStack;
this.itemType = itemType; public final boolean remove;
this.itemAmount = itemAmount; public final int itemType;
this.itemData = itemData >= 0 ? itemData : 0;
} public ChestAccess(ItemStack itemStack, boolean remove, int itemType) {
} this.itemStack = itemStack;
this.remove = remove;
this.itemType = itemType;
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,70 +1,147 @@
package de.diddiz.LogBlock; package de.diddiz.LogBlock;
import de.diddiz.util.Utils.ExtensionFilenameFilter; import static de.diddiz.LogBlock.util.Utils.newline;
import java.io.*; import de.diddiz.LogBlock.util.Utils.ExtensionFilenameFilter;
import java.sql.Connection; import java.io.*;
import java.sql.SQLException; import java.sql.Connection;
import java.sql.Statement; import java.sql.SQLException;
import java.util.logging.Level; import java.sql.Statement;
import java.util.Arrays;
import static de.diddiz.util.Utils.newline; import java.util.Comparator;
import static org.bukkit.Bukkit.getLogger; import java.util.logging.Level;
import java.util.regex.Pattern;
public class DumpedLogImporter implements Runnable {
private final LogBlock logblock; public class DumpedLogImporter implements Runnable {
private final LogBlock logblock;
DumpedLogImporter(LogBlock logblock) {
this.logblock = logblock; DumpedLogImporter(LogBlock logblock) {
} this.logblock = logblock;
}
@Override
public void run() { @Override
final File[] imports = new File("plugins/LogBlock/import/").listFiles(new ExtensionFilenameFilter("sql")); public void run() {
if (imports != null && imports.length > 0) { final File[] imports = new File(logblock.getDataFolder(), "import").listFiles(new ExtensionFilenameFilter("sql"));
getLogger().info("Found " + imports.length + " imports."); if (imports != null && imports.length > 0) {
Connection conn = null; logblock.getLogger().info("Found " + imports.length + " imports.");
try { Arrays.sort(imports, new ImportsComparator());
conn = logblock.getConnection(); Connection conn = null;
if (conn == null) { try {
return; conn = logblock.getConnection();
} if (conn == null) {
conn.setAutoCommit(false); return;
final Statement st = conn.createStatement(); }
final BufferedWriter writer = new BufferedWriter(new FileWriter(new File(logblock.getDataFolder(), "import/failed.txt"))); conn.setAutoCommit(false);
int successes = 0, errors = 0; final Statement st = conn.createStatement();
for (final File sqlFile : imports) { final BufferedWriter writer = new BufferedWriter(new FileWriter(new File(logblock.getDataFolder(), "import/failed.txt")));
getLogger().info("Trying to import " + sqlFile.getName() + " ..."); int successes = 0, errors = 0;
final BufferedReader reader = new BufferedReader(new FileReader(sqlFile)); try {
String line; for (final File sqlFile : imports) {
while ((line = reader.readLine()) != null) { String line = null;
try { try {
st.execute(line); logblock.getLogger().info("Trying to import " + sqlFile.getName() + " ...");
successes++; // first try batch import the whole file
} catch (final Exception ex) { final BufferedReader reader = new BufferedReader(new FileReader(sqlFile));
getLogger().warning("Error while importing: '" + line + "': " + ex.getMessage()); int statements = 0;
writer.write(line + newline); while ((line = reader.readLine()) != null) {
errors++; if (line.endsWith(";")) {
} line = line.substring(0, line.length() - 1);
} }
conn.commit(); if (!line.isEmpty()) {
reader.close(); statements++;
sqlFile.delete(); st.addBatch(line);
getLogger().info("Successfully imported " + sqlFile.getName() + "."); }
} }
writer.close(); st.executeBatch();
st.close(); conn.commit();
getLogger().info("Successfully imported stored queue. (" + successes + " rows imported, " + errors + " errors)"); reader.close();
} catch (final Exception ex) { sqlFile.delete();
getLogger().log(Level.WARNING, "Error while importing: ", ex); successes += statements;
} finally { logblock.getLogger().info("Successfully imported " + sqlFile.getName() + ".");
if (conn != null) { } catch (final Exception ignored) {
try { // if the batch import did not work, retry line by line
conn.close(); try {
} catch (final SQLException ex) { final BufferedReader reader = new BufferedReader(new FileReader(sqlFile));
} while ((line = reader.readLine()) != null) {
} if (line.endsWith(";")) {
} line = line.substring(0, line.length() - 1);
} }
} if (!line.isEmpty()) {
} try {
st.execute(line);
successes++;
} catch (final SQLException ex) {
logblock.getLogger().severe("Error while importing: '" + line + "': " + ex.getMessage());
writer.write(line + newline);
errors++;
}
}
}
conn.commit();
reader.close();
sqlFile.delete();
logblock.getLogger().info("Successfully imported " + sqlFile.getName() + ".");
} catch (final Exception ex) {
logblock.getLogger().severe("Error while importing " + sqlFile.getName() + ": " + ex.getMessage());
errors++;
}
}
}
} finally {
writer.close();
}
st.close();
logblock.getLogger().info("Successfully imported stored queue. (" + successes + " rows imported, " + errors + " errors)");
} catch (final Exception ex) {
logblock.getLogger().log(Level.WARNING, "Error while importing: ", ex);
} finally {
if (conn != null) {
try {
conn.close();
} catch (final SQLException ex) {
}
}
}
}
}
private static class ImportsComparator implements Comparator<File> {
private final Pattern splitPattern = Pattern.compile("[\\-\\.]");
@Override
public int compare(File o1, File o2) {
String[] name1 = splitPattern.split(o1.getName());
String[] name2 = splitPattern.split(o2.getName());
if (name1.length > name2.length) {
return 1;
} else if (name1.length < name2.length) {
return -1;
}
for (int i = 0; i < name1.length; i++) {
String part1 = name1[i];
String part2 = name2[i];
if (part1.length() > 0 && part2.length() > 0) {
char first1 = part1.charAt(0);
char first2 = part2.charAt(0);
if (first1 >= '0' && first1 <= '9' && first2 >= '0' && first2 <= '9') {
try {
long long1 = Long.parseLong(part1);
long long2 = Long.parseLong(part2);
if (long1 == long2) {
continue;
}
return long1 > long2 ? 1 : -1;
} catch (NumberFormatException e) {
// fallthrough to string compare
}
}
}
int compareString = part1.compareTo(part2);
if (compareString != 0) {
return compareString;
}
}
return 0;
}
}
}

View File

@ -0,0 +1,138 @@
package de.diddiz.LogBlock;
import static de.diddiz.LogBlock.util.ActionColor.CREATE;
import static de.diddiz.LogBlock.util.ActionColor.DESTROY;
import static de.diddiz.LogBlock.util.ActionColor.INTERACT;
import static de.diddiz.LogBlock.util.MessagingUtil.createTextComponentWithColor;
import static de.diddiz.LogBlock.util.MessagingUtil.prettyDate;
import static de.diddiz.LogBlock.util.MessagingUtil.prettyEntityType;
import static de.diddiz.LogBlock.util.MessagingUtil.prettyLocation;
import static de.diddiz.LogBlock.util.MessagingUtil.prettyMaterial;
import de.diddiz.LogBlock.util.Utils;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Location;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.inventory.ItemStack;
public class EntityChange implements LookupCacheElement {
public static enum EntityChangeType {
CREATE,
KILL,
MODIFY,
ADDEQUIP,
REMOVEEQUIP,
GET_STUNG;
private static EntityChangeType[] values = values();
public static EntityChangeType valueOf(int ordinal) {
return values[ordinal];
}
}
public final long id, date;
public final Location loc;
public final Actor actor;
public final EntityType type;
public final int entityId;
public final UUID entityUUID;
public final EntityChangeType changeType;
public final byte[] data;
public EntityChange(long date, Location loc, Actor actor, EntityType type, UUID entityid, EntityChangeType changeType, byte[] data) {
id = 0;
this.date = date;
this.loc = loc;
this.actor = actor;
this.type = type;
this.entityId = -1;
this.entityUUID = entityid;
this.changeType = changeType;
this.data = data;
}
public EntityChange(ResultSet rs, QueryParams p) throws SQLException {
id = p.needId ? rs.getLong("id") : 0;
date = p.needDate ? rs.getTimestamp("date").getTime() : 0;
loc = p.needCoords ? new Location(p.world, rs.getInt("x"), rs.getInt("y"), rs.getInt("z")) : null;
actor = p.needPlayer ? new Actor(rs) : null;
type = p.needType ? EntityTypeConverter.getEntityType(rs.getInt("entitytypeid")) : null;
entityId = p.needData ? rs.getInt("entityid") : 0;
entityUUID = p.needData ? UUID.fromString(rs.getString("entityuuid")) : null;
changeType = p.needType ? EntityChangeType.valueOf(rs.getInt("action")) : null;
data = p.needData ? rs.getBytes("data") : null;
}
@Override
public String toString() {
return BaseComponent.toPlainText(getLogMessage());
}
@Override
public BaseComponent[] getLogMessage(int entry) {
TextComponent msg = new TextComponent();
if (date > 0) {
msg.addExtra(prettyDate(date));
msg.addExtra(" ");
}
if (actor != null) {
msg.addExtra(actor.getName());
msg.addExtra(" ");
}
if (changeType == EntityChangeType.CREATE) {
msg.addExtra(createTextComponentWithColor("created ", CREATE.getColor()));
} else if (changeType == EntityChangeType.KILL) {
boolean living = type != null && LivingEntity.class.isAssignableFrom(type.getEntityClass()) && !ArmorStand.class.isAssignableFrom(type.getDeclaringClass());
msg.addExtra(createTextComponentWithColor(living ? "killed " : "destroyed ", DESTROY.getColor()));
} else if (changeType == EntityChangeType.ADDEQUIP) {
YamlConfiguration conf = Utils.deserializeYamlConfiguration(data);
ItemStack stack = conf == null ? null : conf.getItemStack("item");
if (stack == null) {
msg.addExtra(createTextComponentWithColor("added an item to ", CREATE.getColor()));
} else {
msg.addExtra(createTextComponentWithColor("added ", CREATE.getColor()));
msg.addExtra(prettyMaterial(stack.getType()));
msg.addExtra(" to ");
}
} else if (changeType == EntityChangeType.REMOVEEQUIP) {
YamlConfiguration conf = Utils.deserializeYamlConfiguration(data);
ItemStack stack = conf == null ? null : conf.getItemStack("item");
if (stack == null) {
msg.addExtra(createTextComponentWithColor("removed an item from ", DESTROY.getColor()));
} else {
msg.addExtra(createTextComponentWithColor("removed ", DESTROY.getColor()));
msg.addExtra(prettyMaterial(stack.getType()));
msg.addExtra(" from ");
}
} else if (changeType == EntityChangeType.MODIFY) {
msg.addExtra(createTextComponentWithColor("modified ", INTERACT.getColor()));
} else if (changeType == EntityChangeType.GET_STUNG) {
msg.addExtra(createTextComponentWithColor("got stung by ", DESTROY.getColor()));
} else {
msg.addExtra(createTextComponentWithColor("did an unknown action to ", INTERACT.getColor()));
}
if (type != null) {
msg.addExtra(prettyEntityType(type));
} else {
msg.addExtra(prettyMaterial("an unknown entity"));
}
if (loc != null) {
msg.addExtra(" at ");
msg.addExtra(prettyLocation(loc, entry));
}
return new BaseComponent[] { msg };
}
@Override
public Location getLocation() {
return loc;
}
}

View File

@ -0,0 +1,114 @@
package de.diddiz.LogBlock;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
import java.util.HashMap;
import java.util.logging.Level;
import org.bukkit.entity.EntityType;
public class EntityTypeConverter {
private static EntityType[] idToEntityType = new EntityType[10];
private static HashMap<EntityType, Integer> entityTypeToId = new HashMap<>();
private static int nextEntityTypeId;
public synchronized static Integer getExistingEntityTypeId(EntityType entityType) {
return entityType == null ? null : entityTypeToId.get(entityType);
}
public synchronized static int getOrAddEntityTypeId(EntityType entityType) {
Integer key = entityTypeToId.get(entityType);
int tries = 0;
while (key == null && tries < 10) {
tries++;
key = nextEntityTypeId;
Connection conn = LogBlock.getInstance().getConnection();
try {
conn.setAutoCommit(false);
PreparedStatement smt = conn.prepareStatement("INSERT IGNORE INTO `lb-entitytypes` (id, name) VALUES (?, ?)");
smt.setInt(1, key);
smt.setString(2, entityType.name());
boolean couldAdd = smt.executeUpdate() > 0;
conn.commit();
smt.close();
if (couldAdd) {
internalAddEntityType(key, entityType);
} else {
initializeEntityTypes(conn);
}
} catch (Exception e) {
LogBlock.getInstance().getLogger().log(Level.SEVERE, "Could not update lb-entitytypes", e);
reinitializeEntityTypesCatchException();
if (tries == 10) {
throw new RuntimeException(e);
}
} finally {
try {
conn.close();
} catch (SQLException e) {
// ignored
}
}
key = entityTypeToId.get(entityType);
}
return key.intValue();
}
public synchronized static EntityType getEntityType(int entityTypeId) {
return entityTypeId >= 0 && entityTypeId < idToEntityType.length ? idToEntityType[entityTypeId] : null;
}
private static void reinitializeEntityTypesCatchException() {
Connection conn = LogBlock.getInstance().getConnection();
try {
initializeEntityTypes(conn);
} catch (Exception e) {
LogBlock.getInstance().getLogger().log(Level.SEVERE, "Could not reinitialize lb-entitytypes", e);
} finally {
try {
conn.close();
} catch (Exception e) {
// ignored
}
}
}
protected synchronized static void initializeEntityTypes(Connection connection) throws SQLException {
Statement smt = connection.createStatement();
ResultSet rs = smt.executeQuery("SELECT id, name FROM `lb-entitytypes`");
while (rs.next()) {
int key = rs.getInt(1);
try {
EntityType entityType = EntityType.valueOf(rs.getString(2));
internalAddEntityType(key, entityType);
} catch (IllegalArgumentException ignored) {
// the key is used, but not available in this version
if (nextEntityTypeId <= key) {
nextEntityTypeId = key + 1;
}
}
}
rs.close();
smt.close();
connection.close();
}
private static void internalAddEntityType(int key, EntityType entityType) {
entityTypeToId.put(entityType, key);
int length = idToEntityType.length;
while (length <= key) {
length = (length * 3 / 2) + 5;
}
if (length > idToEntityType.length) {
idToEntityType = Arrays.copyOf(idToEntityType, length);
}
idToEntityType[key] = entityType;
if (nextEntityTypeId <= key) {
nextEntityTypeId = key + 1;
}
}
}

View File

@ -1,11 +1,18 @@
package de.diddiz.LogBlock; package de.diddiz.LogBlock;
import de.diddiz.LogBlock.config.Config; import static de.diddiz.LogBlock.util.ActionColor.DESTROY;
import org.bukkit.Location; import static de.diddiz.LogBlock.util.MessagingUtil.prettyDate;
import org.bukkit.inventory.ItemStack; import static de.diddiz.LogBlock.util.MessagingUtil.prettyLocation;
import static de.diddiz.LogBlock.util.MessagingUtil.prettyMaterial;
import de.diddiz.LogBlock.util.BukkitUtils;
import de.diddiz.LogBlock.util.MessagingUtil;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Location;
import org.bukkit.Material;
public class Kill implements LookupCacheElement { public class Kill implements LookupCacheElement {
final long id, date; final long id, date;
@ -23,7 +30,7 @@ public class Kill implements LookupCacheElement {
} }
public Kill(ResultSet rs, QueryParams p) throws SQLException { public Kill(ResultSet rs, QueryParams p) throws SQLException {
id = p.needId ? rs.getInt("id") : 0; id = p.needId ? rs.getLong("id") : 0;
date = p.needDate ? rs.getTimestamp("date").getTime() : 0; date = p.needDate ? rs.getTimestamp("date").getTime() : 0;
loc = p.needCoords ? new Location(p.world, rs.getInt("x"), rs.getInt("y"), rs.getInt("z")) : null; loc = p.needCoords ? new Location(p.world, rs.getInt("x"), rs.getInt("y"), rs.getInt("z")) : null;
killerName = p.needKiller ? rs.getString("killer") : null; killerName = p.needKiller ? rs.getString("killer") : null;
@ -33,17 +40,7 @@ public class Kill implements LookupCacheElement {
@Override @Override
public String toString() { public String toString() {
final StringBuilder msg = new StringBuilder(); return BaseComponent.toPlainText(getLogMessage());
if (date > 0) {
msg.append(Config.formatter.format(date)).append(" ");
}
msg.append(killerName).append(" killed ").append(victimName);
if (loc != null) {
msg.append(" at ").append(loc.getBlockX()).append(":").append(loc.getBlockY()).append(":").append(loc.getBlockZ());
}
String weaponName = prettyItemName(new ItemStack(weapon));
msg.append(" with " + weaponName); // + ("aeiou".contains(weaponName.substring(0, 1)) ? "an " : "a " )
return msg.toString();
} }
@Override @Override
@ -52,15 +49,29 @@ public class Kill implements LookupCacheElement {
} }
@Override @Override
public String getMessage() { public BaseComponent[] getLogMessage(int entry) {
return toString(); TextComponent msg = new TextComponent();
if (date > 0) {
msg.addExtra(prettyDate(date));
msg.addExtra(" ");
}
msg.addExtra(MessagingUtil.createTextComponentWithColor(killerName + " killed ", DESTROY.getColor()));
msg.addExtra(new TextComponent(victimName));
if (loc != null) {
msg.addExtra(" at ");
msg.addExtra(prettyLocation(loc, entry));
}
if (weapon != 0) {
msg.addExtra(" with ");
msg.addExtra(prettyItemName(MaterialConverter.getMaterial(weapon)));
}
return new BaseComponent[] { msg };
} }
public String prettyItemName(ItemStack i) { public TextComponent prettyItemName(Material t) {
String item = i.getType().toString().replace('_', ' ').toLowerCase(); if (t == null || BukkitUtils.isEmpty(t)) {
if (item.equals("air")) { return prettyMaterial("fist");
item = "fist";
} }
return item; return prettyMaterial(t.toString().replace('_', ' '));
} }
} }

View File

@ -1,336 +1,386 @@
package de.diddiz.LogBlock; package de.diddiz.LogBlock;
import de.diddiz.LogBlock.config.Config; import de.diddiz.LogBlock.addons.worldguard.WorldGuardLoggingFlagsAddon;
import de.diddiz.LogBlock.listeners.*; import de.diddiz.LogBlock.config.Config;
import de.diddiz.util.MySQLConnectionPool; import de.diddiz.LogBlock.listeners.*;
import de.diddiz.worldedit.WorldEditLoggingHook; import de.diddiz.LogBlock.questioner.Questioner;
import org.bukkit.ChatColor; import de.diddiz.LogBlock.util.BukkitUtils;
import org.bukkit.command.Command; import de.diddiz.LogBlock.util.MySQLConnectionPool;
import org.bukkit.command.CommandSender; import de.diddiz.LogBlock.worldedit.WorldEditHelper;
import org.bukkit.entity.Player; import de.diddiz.LogBlock.worldedit.WorldEditLoggingHook;
import org.bukkit.permissions.Permission; import org.bukkit.ChatColor;
import org.bukkit.plugin.PluginManager; import org.bukkit.Material;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import java.io.FileNotFoundException; import org.bukkit.entity.Player;
import java.io.IOException; import org.bukkit.permissions.Permission;
import java.sql.Connection; import org.bukkit.plugin.PluginManager;
import java.sql.ResultSet; import org.bukkit.plugin.java.JavaPlugin;
import java.sql.SQLException;
import java.sql.Statement; import java.io.File;
import java.util.ArrayList; import java.io.FileNotFoundException;
import java.util.List; import java.sql.Connection;
import java.util.Timer; import java.sql.ResultSet;
import java.util.logging.Level; import java.sql.SQLException;
import java.sql.Statement;
import static de.diddiz.LogBlock.config.Config.*; import java.util.ArrayList;
import static de.diddiz.util.MaterialName.materialName; import java.util.List;
import static org.bukkit.Bukkit.getPluginManager; import java.util.logging.Level;
public class LogBlock extends JavaPlugin { import static de.diddiz.LogBlock.config.Config.*;
private static LogBlock logblock = null; import static org.bukkit.Bukkit.getPluginManager;
private MySQLConnectionPool pool;
private Consumer consumer = null; public class LogBlock extends JavaPlugin {
private CommandsHandler commandsHandler; private static LogBlock logblock = null;
private Updater updater = null; private MySQLConnectionPool pool;
private Timer timer = null; private Consumer consumer = null;
private boolean errorAtLoading = false, noDb = false, connected = true; private CommandsHandler commandsHandler;
private boolean noDb = false, connected = true;
public static LogBlock getInstance() { private PlayerInfoLogging playerInfoLogging;
return logblock; private ScaffoldingLogging scaffoldingLogging;
} private Questioner questioner;
private WorldGuardLoggingFlagsAddon worldGuardLoggingFlagsAddon;
public Consumer getConsumer() { private boolean isConfigLoaded;
return consumer; private volatile boolean isCompletelyEnabled;
}
public static LogBlock getInstance() {
public CommandsHandler getCommandsHandler() { return logblock;
return commandsHandler; }
}
public boolean isCompletelyEnabled() {
Updater getUpdater() { return isCompletelyEnabled;
return updater; }
}
public Consumer getConsumer() {
@Override return consumer;
public void onLoad() { }
logblock = this;
try { public CommandsHandler getCommandsHandler() {
updater = new Updater(this); return commandsHandler;
Config.load(this); }
getLogger().info("Connecting to " + user + "@" + url + "...");
pool = new MySQLConnectionPool(url, user, password); @Override
final Connection conn = getConnection(); public void onLoad() {
if (conn == null) { logblock = this;
noDb = true; BukkitUtils.isDoublePlant(Material.AIR); // Force static code to run
return; try {
} Config.load(this);
final Statement st = conn.createStatement(); isConfigLoaded = true;
final ResultSet rs = st.executeQuery("SHOW CHARACTER SET where charset='utf8mb4';"); } catch (final Exception ex) {
if (rs.next()) { getLogger().log(Level.SEVERE, "Could not load LogBlock config! " + ex.getMessage(), ex);
Config.mb4 = true; }
// Allegedly JDBC driver since 2010 hasn't needed this. I did. if (Config.worldGuardLoggingFlags) {
st.executeQuery("SET NAMES utf8mb4;"); if (getServer().getPluginManager().getPlugin("WorldGuard") == null) {
} getLogger().log(Level.SEVERE, "Invalid config! addons.worldguardLoggingFlags is set to true, but WorldGuard is not loaded.");
conn.close(); } else {
if (updater.update()) { worldGuardLoggingFlagsAddon = new WorldGuardLoggingFlagsAddon(this);
load(this); worldGuardLoggingFlagsAddon.onPluginLoad();
} }
updater.checkTables(); }
} catch (final NullPointerException ex) { }
getLogger().log(Level.SEVERE, "Error while loading: ", ex);
} catch (final Exception ex) { @Override
getLogger().severe("Error while loading: " + ex.getMessage()); public void onEnable() {
errorAtLoading = true; final PluginManager pm = getPluginManager();
return; if (!isConfigLoaded) {
} pm.disablePlugin(this);
consumer = new Consumer(this); return;
} }
consumer = new Consumer(this);
@Override try {
public void onEnable() { getLogger().info("Connecting to " + user + "@" + url + "...");
materialName(0); // Force static code to run try {
final PluginManager pm = getPluginManager(); Class.forName("com.mysql.cj.jdbc.Driver");
if (errorAtLoading) { } catch (ClassNotFoundException ignored) {
pm.disablePlugin(this); Class.forName("com.mysql.jdbc.Driver");
return; }
} pool = new MySQLConnectionPool(url, user, password, mysqlUseSSL, mysqlRequireSSL);
if (noDb) { final Connection conn = getConnection(true);
return; if (conn == null) {
} noDb = true;
if (pm.getPlugin("WorldEdit") != null) { return;
if (Integer.parseInt(pm.getPlugin("WorldEdit").getDescription().getVersion().substring(0, 1)) > 5) { }
new WorldEditLoggingHook(this).hook(); final Statement st = conn.createStatement();
} else { try {
getLogger().warning("Failed to hook into WorldEdit. Your WorldEdit version seems to be outdated, please make sure WorldEdit is at least version 6."); final ResultSet rs = st.executeQuery("SHOW CHARACTER SET where charset='utf8mb4';");
} if (rs.next()) {
} Config.mb4 = true;
commandsHandler = new CommandsHandler(this); // Allegedly JDBC driver since 2010 hasn't needed this. I did.
getCommand("lb").setExecutor(commandsHandler); try {
if (enableAutoClearLog && autoClearLogDelay > 0) { st.executeUpdate("SET NAMES utf8mb4;");
getServer().getScheduler().runTaskTimerAsynchronously(this, new AutoClearLog(this), 6000, autoClearLogDelay * 60 * 20); } catch (Exception ex) {
} getLogger().severe("could not set names to utf8mb4: " + ex.getMessage());
getServer().getScheduler().runTaskAsynchronously(this, new DumpedLogImporter(this)); }
registerEvents(); }
if (useBukkitScheduler) { } catch (Exception ex) {
if (getServer().getScheduler().runTaskTimerAsynchronously(this, consumer, delayBetweenRuns < 20 ? 20 : delayBetweenRuns, delayBetweenRuns).getTaskId() > 0) { getLogger().severe("could not verify character set: " + ex.getMessage());
getLogger().info("Scheduled consumer with bukkit scheduler."); }
} else { conn.close();
getLogger().warning("Failed to schedule consumer with bukkit scheduler. Now trying schedule with timer."); Updater updater = new Updater(this);
timer = new Timer(); updater.checkTables();
timer.schedule(consumer, delayBetweenRuns < 20 ? 1000 : delayBetweenRuns * 50, delayBetweenRuns * 50); MaterialConverter.initializeMaterials(getConnection());
} MaterialConverter.getOrAddMaterialId(Material.AIR); // AIR must be the first entry
} else { EntityTypeConverter.initializeEntityTypes(getConnection());
timer = new Timer(); if (updater.update()) {
timer.schedule(consumer, delayBetweenRuns < 20 ? 1000 : delayBetweenRuns * 50, delayBetweenRuns * 50); load(this);
getLogger().info("Scheduled consumer with timer."); }
} } catch (final NullPointerException ex) {
getServer().getScheduler().runTaskAsynchronously(this, new Updater.PlayerCountChecker(this)); getLogger().log(Level.SEVERE, "Error while loading: ", ex);
for (final Tool tool : toolsByType.values()) { } catch (final Exception ex) {
if (pm.getPermission("logblock.tools." + tool.name) == null) { getLogger().log(Level.SEVERE, "Error while loading: " + ex.getMessage(), ex);
final Permission perm = new Permission("logblock.tools." + tool.name, tool.permissionDefault); pm.disablePlugin(this);
pm.addPermission(perm); return;
} }
}
try { if (WorldEditHelper.hasWorldEdit()) {
Metrics metrics = new Metrics(this); new WorldEditLoggingHook(this).hook();
metrics.start(); }
} catch (IOException ex) { commandsHandler = new CommandsHandler(this);
getLogger().info("Could not start metrics: " + ex.getMessage()); getCommand("lb").setExecutor(commandsHandler);
} if (enableAutoClearLog && autoClearLogDelay > 0) {
} getServer().getScheduler().runTaskTimerAsynchronously(this, new AutoClearLog(this), 6000, autoClearLogDelay * 60 * 20);
}
private void registerEvents() { new DumpedLogImporter(this).run();
final PluginManager pm = getPluginManager(); registerEvents();
pm.registerEvents(new ToolListener(this), this); consumer.start();
pm.registerEvents(new PlayerInfoLogging(this), this); for (final Tool tool : toolsByType.values()) {
if (askRollbackAfterBan) { if (pm.getPermission("logblock.tools." + tool.name) == null) {
pm.registerEvents(new BanListener(this), this); final Permission perm = new Permission("logblock.tools." + tool.name, tool.permissionDefault);
} pm.addPermission(perm);
if (isLogging(Logging.BLOCKPLACE)) { }
pm.registerEvents(new BlockPlaceLogging(this), this); }
} questioner = new Questioner(this);
if (isLogging(Logging.BLOCKPLACE) || isLogging(Logging.LAVAFLOW) || isLogging(Logging.WATERFLOW)) { if (worldGuardLoggingFlagsAddon != null) {
pm.registerEvents(new FluidFlowLogging(this), this); worldGuardLoggingFlagsAddon.onPluginEnable();
} }
if (isLogging(Logging.BLOCKBREAK)) { isCompletelyEnabled = true;
pm.registerEvents(new BlockBreakLogging(this), this); getServer().getScheduler().runTaskAsynchronously(this, new Updater.PlayerCountChecker(this));
} }
if (isLogging(Logging.SIGNTEXT)) {
pm.registerEvents(new SignChangeLogging(this), this); private void registerEvents() {
} final PluginManager pm = getPluginManager();
if (isLogging(Logging.FIRE)) { pm.registerEvents(new ToolListener(this), this);
pm.registerEvents(new BlockBurnLogging(this), this); pm.registerEvents(playerInfoLogging = new PlayerInfoLogging(this), this);
} if (askRollbackAfterBan) {
if (isLogging(Logging.SNOWFORM)) { pm.registerEvents(new BanListener(this), this);
pm.registerEvents(new SnowFormLogging(this), this); }
} if (isLogging(Logging.BLOCKPLACE)) {
if (isLogging(Logging.SNOWFADE)) { pm.registerEvents(new BlockPlaceLogging(this), this);
pm.registerEvents(new SnowFadeLogging(this), this); }
} if (isLogging(Logging.LAVAFLOW) || isLogging(Logging.WATERFLOW)) {
if (isLogging(Logging.CREEPEREXPLOSION) || isLogging(Logging.TNTEXPLOSION) || isLogging(Logging.GHASTFIREBALLEXPLOSION) || isLogging(Logging.ENDERDRAGON) || isLogging(Logging.MISCEXPLOSION)) { pm.registerEvents(new FluidFlowLogging(this), this);
pm.registerEvents(new ExplosionLogging(this), this); }
} if (isLogging(Logging.BLOCKBREAK)) {
if (isLogging(Logging.LEAVESDECAY)) { pm.registerEvents(new BlockBreakLogging(this), this);
pm.registerEvents(new LeavesDecayLogging(this), this); }
} if (isLogging(Logging.SIGNTEXT)) {
if (isLogging(Logging.CHESTACCESS)) { pm.registerEvents(new SignChangeLogging(this), this);
pm.registerEvents(new ChestAccessLogging(this), this); }
} if (isLogging(Logging.FIRE)) {
if (isLogging(Logging.SWITCHINTERACT) || isLogging(Logging.DOORINTERACT) || isLogging(Logging.CAKEEAT) || isLogging(Logging.DIODEINTERACT) || isLogging(Logging.COMPARATORINTERACT) || isLogging(Logging.NOTEBLOCKINTERACT) || isLogging(Logging.PRESUREPLATEINTERACT) || isLogging(Logging.TRIPWIREINTERACT) || isLogging(Logging.CROPTRAMPLE)) { pm.registerEvents(new BlockBurnLogging(this), this);
pm.registerEvents(new InteractLogging(this), this); }
} if (isLogging(Logging.SNOWFORM)) {
if (isLogging(Logging.CREATURECROPTRAMPLE)) { pm.registerEvents(new SnowFormLogging(this), this);
pm.registerEvents(new CreatureInteractLogging(this), this); }
} if (isLogging(Logging.SNOWFADE)) {
if (isLogging(Logging.KILL)) { pm.registerEvents(new SnowFadeLogging(this), this);
pm.registerEvents(new KillLogging(this), this); }
} if (isLogging(Logging.SCAFFOLDING)) {
if (isLogging(Logging.CHAT)) { pm.registerEvents(scaffoldingLogging = new ScaffoldingLogging(this), this);
pm.registerEvents(new ChatLogging(this), this); }
} if (isLogging(Logging.CAULDRONINTERACT)) {
if (isLogging(Logging.ENDERMEN)) { pm.registerEvents(new CauldronLogging(this), this);
pm.registerEvents(new EndermenLogging(this), this); }
} if (isLogging(Logging.CREEPEREXPLOSION) || isLogging(Logging.TNTEXPLOSION) || isLogging(Logging.GHASTFIREBALLEXPLOSION) || isLogging(Logging.ENDERDRAGON) || isLogging(Logging.MISCEXPLOSION)) {
if (isLogging(Logging.WITHER)) { pm.registerEvents(new ExplosionLogging(this), this);
pm.registerEvents(new WitherLogging(this), this); }
} if (isLogging(Logging.LEAVESDECAY)) {
if (isLogging(Logging.NATURALSTRUCTUREGROW) || isLogging(Logging.BONEMEALSTRUCTUREGROW)) { pm.registerEvents(new LeavesDecayLogging(this), this);
pm.registerEvents(new StructureGrowLogging(this), this); }
} if (isLogging(Logging.CHESTACCESS)) {
if (isLogging(Logging.GRASSGROWTH) || isLogging(Logging.MYCELIUMSPREAD) || isLogging(Logging.VINEGROWTH) || isLogging(Logging.MUSHROOMSPREAD)) { pm.registerEvents(new ChestAccessLogging(this), this);
pm.registerEvents(new BlockSpreadLogging(this), this); }
} if (isLogging(Logging.BLOCKBREAK) || isLogging(Logging.BLOCKPLACE) || isLogging(Logging.SWITCHINTERACT) || isLogging(Logging.DOORINTERACT) || isLogging(Logging.CAKEEAT) || isLogging(Logging.DIODEINTERACT) || isLogging(Logging.COMPARATORINTERACT) || isLogging(Logging.NOTEBLOCKINTERACT)
if (isLogging(Logging.LOCKEDCHESTDECAY)) { || isLogging(Logging.PRESUREPLATEINTERACT) || isLogging(Logging.TRIPWIREINTERACT) || isLogging(Logging.CROPTRAMPLE)) {
pm.registerEvents(new LockedChestDecayLogging(this), this); pm.registerEvents(new InteractLogging(this), this);
} }
} if (isLogging(Logging.CREATURECROPTRAMPLE)) {
pm.registerEvents(new CreatureInteractLogging(this), this);
@Override }
public void onDisable() { if (isLogging(Logging.KILL)) {
if (timer != null) { pm.registerEvents(new KillLogging(this), this);
timer.cancel(); }
} if (isLogging(Logging.CHAT) || isLogging(Logging.PLAYER_COMMANDS) || isLogging(Logging.CONSOLE_COMMANDS) || isLogging(Logging.COMMANDBLOCK_COMMANDS)) {
getServer().getScheduler().cancelTasks(this); pm.registerEvents(new ChatLogging(this), this);
if (consumer != null) { }
if (logPlayerInfo && getServer().getOnlinePlayers() != null) { if (isLogging(Logging.ENDERMEN)) {
for (final Player player : getServer().getOnlinePlayers()) { pm.registerEvents(new EndermenLogging(this), this);
consumer.queueLeave(player); }
} if (isLogging(Logging.WITHER)) {
} pm.registerEvents(new WitherLogging(this), this);
getLogger().info("Waiting for consumer ..."); }
consumer.run(); if (isLogging(Logging.NATURALSTRUCTUREGROW)) {
if (consumer.getQueueSize() > 0) { pm.registerEvents(new StructureGrowLogging(this), this);
int tries = 9; }
while (consumer.getQueueSize() > 0) { if (isLogging(Logging.BONEMEALSTRUCTUREGROW)) {
getLogger().info("Remaining queue size: " + consumer.getQueueSize()); pm.registerEvents(new BlockFertilizeLogging(this), this);
if (tries > 0) { }
getLogger().info("Remaining tries: " + tries); if (isLogging(Logging.GRASSGROWTH) || isLogging(Logging.MYCELIUMSPREAD) || isLogging(Logging.VINEGROWTH) || isLogging(Logging.MUSHROOMSPREAD) || isLogging(Logging.BAMBOOGROWTH) || isLogging(Logging.DRIPSTONEGROWTH) || isLogging(Logging.SCULKSPREAD)) {
} else { pm.registerEvents(new BlockSpreadLogging(this), this);
getLogger().info("Unable to save queue to database. Trying to write to a local file."); }
try { if (isLogging(Logging.DRAGONEGGTELEPORT)) {
consumer.writeToFile(); pm.registerEvents(new DragonEggLogging(this), this);
getLogger().info("Successfully dumped queue."); }
} catch (final FileNotFoundException ex) { if (isLogging(Logging.LECTERNBOOKCHANGE)) {
getLogger().info("Failed to write. Given up."); pm.registerEvents(new LecternLogging(this), this);
break; }
} if (isLogging(Logging.OXIDIZATION)) {
} pm.registerEvents(new OxidizationLogging(this), this);
consumer.run(); }
tries--; if (Config.isLoggingAnyEntities()) {
} if (!WorldEditHelper.hasFullWorldEdit()) {
} getLogger().severe("No compatible WorldEdit found, entity logging will not work!");
} } else {
if (pool != null) { pm.registerEvents(new AdvancedEntityLogging(this), this);
pool.close(); getLogger().info("Entity logging enabled!");
} }
} }
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { @Override
if (noDb) { public void onDisable() {
sender.sendMessage(ChatColor.RED + "No database connected. Check your MySQL user/pw and database for typos. Start/restart your MySQL server."); isCompletelyEnabled = false;
} getServer().getScheduler().cancelTasks(this);
return true; if (consumer != null) {
} if (logPlayerInfo && playerInfoLogging != null) {
for (final Player player : getServer().getOnlinePlayers()) {
public boolean hasPermission(CommandSender sender, String permission) { playerInfoLogging.onPlayerQuit(player);
return sender.hasPermission(permission); }
} }
getLogger().info("Waiting for consumer ...");
public Connection getConnection() { consumer.shutdown();
try { if (consumer.getQueueSize() > 0) {
final Connection conn = pool.getConnection(); getLogger().info("Remaining queue size: " + consumer.getQueueSize() + ". Trying to write to a local file.");
if (!connected) { try {
getLogger().info("MySQL connection rebuild"); consumer.writeToFile();
connected = true; getLogger().info("Successfully dumped queue.");
} } catch (final FileNotFoundException ex) {
return conn; getLogger().info("Failed to write. Given up.");
} catch (final Exception ex) { }
if (connected) { }
getLogger().log(Level.SEVERE, "Error while fetching connection: ", ex); }
connected = false; if (pool != null) {
} else { pool.close();
getLogger().severe("MySQL connection lost"); }
} }
return null;
} @Override
} public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
if (noDb) {
/** sender.sendMessage(ChatColor.RED + "No database connected. Check your MySQL user/pw and database for typos. Start/restart your MySQL server.");
* Returns a list of block changes based on the given query parameters, the query parameters }
* are essentially programmatic versions of the parameters a player would pass return true;
* to the logblock lookup command i.e /lb lookup <i>query-parameters</i> }
*
* Note: this method directly calls a SQL query and is hence a slow blocking function, avoid running public boolean hasPermission(CommandSender sender, String permission) {
* it on the main game thread return sender.hasPermission(permission);
* }
* @param params QueryParams that contains the needed columns (all other will be filled with default values) and the params. World is required.
* @return Returns a list of block changes based on the given query parameters public Connection getConnection() {
* @throws SQLException if a sql exception occurs while looking up the block changes return getConnection(false);
*/ }
public List<BlockChange> getBlockChanges(QueryParams params) throws SQLException {
final Connection conn = getConnection(); public Connection getConnection(boolean testConnection) {
Statement state = null; try {
if (conn == null) { final Connection conn = pool.getConnection();
throw new SQLException("No connection"); if (!connected) {
} getLogger().info("MySQL connection rebuild");
try { connected = true;
state = conn.createStatement(); }
final ResultSet rs = state.executeQuery(params.getQuery()); return conn;
final List<BlockChange> blockchanges = new ArrayList<BlockChange>(); } catch (final Exception ex) {
while (rs.next()) { if (testConnection) {
blockchanges.add(new BlockChange(rs, params)); getLogger().log(Level.SEVERE, "Could not connect to the Database! Please check your config! " + ex.getMessage());
} } else if (connected) {
return blockchanges; getLogger().log(Level.SEVERE, "Error while fetching connection: ", ex);
} finally { connected = false;
if (state != null) { } else {
state.close(); getLogger().log(Level.SEVERE, "MySQL connection lost", ex);
} }
conn.close(); return null;
} }
} }
public int getCount(QueryParams params) throws SQLException { /**
final Connection conn = getConnection(); * Returns a list of block changes based on the given query parameters, the query parameters
Statement state = null; * are essentially programmatic versions of the parameters a player would pass
if (conn == null) { * to the logblock lookup command i.e /lb lookup <i>query-parameters</i>
throw new SQLException("No connection"); *
} * Note: this method directly calls a SQL query and is hence a slow blocking function, avoid running
try { * it on the main game thread
state = conn.createStatement(); *
final QueryParams p = params.clone(); * @param params QueryParams that contains the needed columns (all other will be filled with default values) and the params. World is required.
p.needCount = true; * @return Returns a list of block changes based on the given query parameters
final ResultSet rs = state.executeQuery(p.getQuery()); * @throws SQLException if a sql exception occurs while looking up the block changes
if (!rs.next()) { */
return 0; public List<BlockChange> getBlockChanges(QueryParams params) throws SQLException {
} final Connection conn = getConnection();
return rs.getInt(1); Statement state = null;
} finally { if (conn == null) {
if (state != null) { throw new SQLException("No connection");
state.close(); }
} try {
conn.close(); state = conn.createStatement();
} final ResultSet rs = state.executeQuery(params.getQuery());
} final List<BlockChange> blockchanges = new ArrayList<>();
} while (rs.next()) {
blockchanges.add(new BlockChange(rs, params));
}
return blockchanges;
} finally {
if (state != null) {
state.close();
}
conn.close();
}
}
public int getCount(QueryParams params) throws SQLException {
if (params == null || params.world == null || !Config.isLogged(params.world)) {
throw new IllegalArgumentException("World is not logged: " + ((params == null || params.world == null) ? "null" : params.world.getName()));
}
final Connection conn = getConnection();
Statement state = null;
if (conn == null) {
throw new SQLException("No connection");
}
try {
state = conn.createStatement();
final QueryParams p = params.clone();
p.needCount = true;
final ResultSet rs = state.executeQuery(p.getQuery());
if (!rs.next()) {
return 0;
}
return rs.getInt(1);
} finally {
if (state != null) {
state.close();
}
conn.close();
}
}
@Override
public File getFile() {
return super.getFile();
}
public Questioner getQuestioner() {
return questioner;
}
public ScaffoldingLogging getScaffoldingLogging() {
return scaffoldingLogging;
}
}

View File

@ -1,27 +1,73 @@
package de.diddiz.LogBlock; package de.diddiz.LogBlock;
public enum Logging { public enum Logging {
BLOCKPLACE(true), BLOCKBREAK(true), SIGNTEXT, TNTEXPLOSION(true), CREEPEREXPLOSION(true), BLOCKPLACE(true),
GHASTFIREBALLEXPLOSION(true), ENDERDRAGON(true), MISCEXPLOSION, FIRE(true), LEAVESDECAY, BLOCKBREAK(true),
LAVAFLOW, WATERFLOW, CHESTACCESS, KILL, CHAT, SNOWFORM, SNOWFADE, DOORINTERACT, SIGNTEXT(true),
SWITCHINTERACT, CAKEEAT, ENDERMEN, NOTEBLOCKINTERACT, DIODEINTERACT, COMPARATORINTERACT, TNTEXPLOSION(true),
PRESUREPLATEINTERACT, TRIPWIREINTERACT, CREATURECROPTRAMPLE, CROPTRAMPLE, CREEPEREXPLOSION(true),
NATURALSTRUCTUREGROW, GRASSGROWTH, MYCELIUMSPREAD, VINEGROWTH, MUSHROOMSPREAD, GHASTFIREBALLEXPLOSION(true),
WITHER(true), WITHER_SKULL(true), BONEMEALSTRUCTUREGROW, ENDERDRAGON(true),
WORLDEDIT, TNTMINECARTEXPLOSION(true), LOCKEDCHESTDECAY; MISCEXPLOSION(true),
FIRE(true),
public static final int length = Logging.values().length; LEAVESDECAY,
private final boolean defaultEnabled; LAVAFLOW,
WATERFLOW,
private Logging() { CHESTACCESS,
this(false); KILL,
} CHAT,
SNOWFORM,
private Logging(boolean defaultEnabled) { SNOWFADE,
this.defaultEnabled = defaultEnabled; DOORINTERACT,
} SWITCHINTERACT,
CAKEEAT,
public boolean isDefaultEnabled() { ENDERMEN,
return defaultEnabled; NOTEBLOCKINTERACT,
} DIODEINTERACT,
} COMPARATORINTERACT,
PRESUREPLATEINTERACT,
TRIPWIREINTERACT,
CAULDRONINTERACT(true),
CREATURECROPTRAMPLE,
CROPTRAMPLE,
NATURALSTRUCTUREGROW,
GRASSGROWTH,
MYCELIUMSPREAD,
VINEGROWTH,
DRIPSTONEGROWTH,
MUSHROOMSPREAD,
BAMBOOGROWTH,
WITHER(true),
WITHER_SKULL(true),
BONEMEALSTRUCTUREGROW,
WORLDEDIT,
TNTMINECARTEXPLOSION(true),
ENDERCRYSTALEXPLOSION(true),
BEDEXPLOSION(true),
DRAGONEGGTELEPORT(true),
DAYLIGHTDETECTORINTERACT,
LECTERNBOOKCHANGE(true),
SCAFFOLDING(true),
OXIDIZATION,
SCULKSPREAD(true),
SHULKER_BOX_CONTENT,
RESPAWNANCHOREXPLOSION(true),
PLAYER_COMMANDS,
COMMANDBLOCK_COMMANDS,
CONSOLE_COMMANDS;
public static final int length = Logging.values().length;
private final boolean defaultEnabled;
private Logging() {
this(false);
}
private Logging(boolean defaultEnabled) {
this.defaultEnabled = defaultEnabled;
}
public boolean isDefaultEnabled() {
return defaultEnabled;
}
}

View File

@ -1,9 +1,18 @@
package de.diddiz.LogBlock; package de.diddiz.LogBlock;
import org.bukkit.Location; import net.md_5.bungee.api.chat.BaseComponent;
import org.bukkit.Location;
public interface LookupCacheElement {
public Location getLocation(); public interface LookupCacheElement {
public Location getLocation();
public String getMessage();
} public default BaseComponent[] getLogMessage() {
return getLogMessage(-1);
}
public BaseComponent[] getLogMessage(int entry);
public default int getNumChanges() {
return 1;
}
}

View File

@ -1,34 +1,40 @@
package de.diddiz.LogBlock; package de.diddiz.LogBlock;
import de.diddiz.LogBlock.QueryParams.BlockChangeType; import de.diddiz.LogBlock.QueryParams.BlockChangeType;
import de.diddiz.LogBlock.QueryParams.SummarizationMode; import de.diddiz.LogBlock.QueryParams.SummarizationMode;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
public class LookupCacheElementFactory { public class LookupCacheElementFactory {
private final QueryParams params; private final QueryParams params;
private final float spaceFactor; private final float spaceFactor;
public LookupCacheElementFactory(QueryParams params, float spaceFactor) { public LookupCacheElementFactory(QueryParams params, float spaceFactor) {
this.params = params; this.params = params;
this.spaceFactor = spaceFactor; this.spaceFactor = spaceFactor;
} }
public LookupCacheElement getLookupCacheElement(ResultSet rs) throws SQLException { public LookupCacheElement getLookupCacheElement(ResultSet rs) throws SQLException {
if (params.bct == BlockChangeType.CHAT) { if (params.bct == BlockChangeType.CHAT) {
return new ChatMessage(rs, params); return new ChatMessage(rs, params);
} }
if (params.bct == BlockChangeType.KILLS) { if (params.bct == BlockChangeType.KILLS) {
if (params.sum == SummarizationMode.NONE) { if (params.sum == SummarizationMode.NONE) {
return new Kill(rs, params); return new Kill(rs, params);
} else if (params.sum == SummarizationMode.PLAYERS) { } else if (params.sum == SummarizationMode.PLAYERS) {
return new SummedKills(rs, params, spaceFactor); return new SummedKills(rs, params, spaceFactor);
} }
} }
if (params.sum == SummarizationMode.NONE) { if (params.bct == BlockChangeType.ENTITIES || params.bct == BlockChangeType.ENTITIES_CREATED || params.bct == BlockChangeType.ENTITIES_KILLED) {
return new BlockChange(rs, params); if (params.sum == SummarizationMode.NONE) {
} return new EntityChange(rs, params);
return new SummedBlockChanges(rs, params, spaceFactor); }
} return new SummedEntityChanges(rs, params, spaceFactor);
} }
if (params.sum == SummarizationMode.NONE) {
return new BlockChange(rs, params);
}
return new SummedBlockChanges(rs, params, spaceFactor);
}
}

View File

@ -0,0 +1,256 @@
package de.diddiz.LogBlock;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
import java.util.HashMap;
import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.data.BlockData;
public class MaterialConverter {
private static String[] idToMaterial = new String[10];
private static HashMap<String, Integer> materialToID = new HashMap<>();
private static int nextMaterialId;
private static String[] idToBlockState = new String[10];
private static HashMap<String, Integer> blockStateToID = new HashMap<>();
private static int nextBlockStateId;
private static HashMap<String, Material> materialKeyToMaterial = new HashMap<>();
static {
for (Material m : Material.values()) {
materialKeyToMaterial.put(m.getKey().toString(), m);
}
}
public synchronized static Integer getExistingMaterialId(BlockData blockData) {
return blockData == null ? null : getExistingMaterialId(blockData.getMaterial());
}
public synchronized static Integer getExistingMaterialId(Material material) {
if (material == null) {
return null;
}
String materialString = material.getKey().toString();
return materialToID.get(materialString);
}
public synchronized static int getOrAddMaterialId(BlockData blockData) {
return getOrAddMaterialId(blockData == null ? Material.AIR : blockData.getMaterial());
}
public synchronized static int getOrAddMaterialId(Material material) {
if (material == null) {
material = Material.AIR;
}
String materialString = material.getKey().toString();
Integer key = materialToID.get(materialString);
int tries = 0;
while (key == null && tries < 10) {
tries++;
key = nextMaterialId;
Connection conn = LogBlock.getInstance().getConnection();
try {
conn.setAutoCommit(false);
PreparedStatement smt = conn.prepareStatement("INSERT IGNORE INTO `lb-materials` (id, name) VALUES (?, ?)");
smt.setInt(1, key);
smt.setString(2, materialString);
boolean couldAdd = smt.executeUpdate() > 0;
conn.commit();
smt.close();
if (couldAdd) {
internalAddMaterial(key, materialString);
} else {
initializeMaterials(conn);
}
} catch (Exception e) {
LogBlock.getInstance().getLogger().log(Level.SEVERE, "Could not update lb-materials", e);
reinitializeMaterialsCatchException();
if (tries == 10) {
throw new RuntimeException(e);
}
} finally {
try {
conn.close();
} catch (SQLException e) {
// ignored
}
}
key = materialToID.get(materialString);
}
return key.intValue();
}
public synchronized static Integer getExistingBlockStateId(BlockData blockData) {
if (blockData == null) {
return -1;
}
String blockDataString = blockData.getAsString();
int dataPart = blockDataString.indexOf("[");
if (dataPart < 0) {
return -1;
}
String materialString = blockDataString.substring(dataPart);
return blockStateToID.get(materialString);
}
public synchronized static int getOrAddBlockStateId(BlockData blockData) {
if (blockData == null) {
return -1;
}
String blockDataString = blockData.getAsString();
int dataPart = blockDataString.indexOf("[");
if (dataPart < 0) {
return -1;
}
String materialString = blockDataString.substring(dataPart);
Integer key = blockStateToID.get(materialString);
int tries = 0;
while (key == null && tries < 10) {
tries++;
key = nextBlockStateId;
Connection conn = LogBlock.getInstance().getConnection();
try {
conn.setAutoCommit(false);
PreparedStatement smt = conn.prepareStatement("INSERT IGNORE INTO `lb-blockstates` (id, name) VALUES (?, ?)");
smt.setInt(1, key);
smt.setString(2, materialString);
boolean couldAdd = smt.executeUpdate() > 0;
conn.commit();
smt.close();
if (couldAdd) {
internalAddBlockState(key, materialString);
} else {
initializeMaterials(conn);
}
} catch (Exception e) {
LogBlock.getInstance().getLogger().log(Level.SEVERE, "Could not update lb-blockstates", e);
reinitializeMaterialsCatchException();
if (tries == 10) {
throw new RuntimeException(e);
}
} finally {
try {
conn.close();
} catch (SQLException e) {
// ignored
}
}
key = blockStateToID.get(materialString);
}
return key.intValue();
}
public synchronized static BlockData getBlockData(int materialId, int blockStateId) {
String material = materialId >= 0 && materialId < idToMaterial.length ? idToMaterial[materialId] : null;
if (material == null) {
return null;
}
if (blockStateId >= 0 && blockStateId < idToBlockState.length && idToBlockState[blockStateId] != null) {
material = material + updateBlockState(material, idToBlockState[blockStateId]);
}
try {
return Bukkit.createBlockData(material);
} catch (IllegalArgumentException ignored) {
// fall back to create the default block data for the material
try {
return Bukkit.createBlockData(idToMaterial[materialId]);
} catch (IllegalArgumentException ignored2) {
return null;
}
}
}
public synchronized static Material getMaterial(int materialId) {
return materialId >= 0 && materialId < idToMaterial.length ? materialKeyToMaterial.get(idToMaterial[materialId]) : null;
}
private static void reinitializeMaterialsCatchException() {
Connection conn = LogBlock.getInstance().getConnection();
try {
initializeMaterials(conn);
} catch (Exception e) {
LogBlock.getInstance().getLogger().log(Level.SEVERE, "Could not reinitialize lb-materials", e);
} finally {
try {
conn.close();
} catch (Exception e) {
// ignored
}
}
}
protected synchronized static void initializeMaterials(Connection connection) throws SQLException {
Statement smt = connection.createStatement();
ResultSet rs = smt.executeQuery("SELECT id, name FROM `lb-materials`");
while (rs.next()) {
int key = rs.getInt(1);
String materialString = rs.getString(2);
internalAddMaterial(key, materialString);
}
rs.close();
rs = smt.executeQuery("SELECT id, name FROM `lb-blockstates`");
while (rs.next()) {
int key = rs.getInt(1);
String materialString = rs.getString(2);
internalAddBlockState(key, materialString);
}
rs.close();
smt.close();
connection.close();
}
private static void internalAddMaterial(int key, String materialString) {
materialToID.put(materialString, key);
int length = idToMaterial.length;
while (length <= key) {
length = (length * 3 / 2) + 5;
}
if (length > idToMaterial.length) {
idToMaterial = Arrays.copyOf(idToMaterial, length);
}
idToMaterial[key] = materialString;
if (nextMaterialId <= key) {
nextMaterialId = key + 1;
}
}
private static void internalAddBlockState(int key, String materialString) {
blockStateToID.put(materialString, key);
int length = idToBlockState.length;
while (length <= key) {
length = (length * 3 / 2) + 5;
}
if (length > idToBlockState.length) {
idToBlockState = Arrays.copyOf(idToBlockState, length);
}
idToBlockState[key] = materialString;
if (nextBlockStateId <= key) {
nextBlockStateId = key + 1;
}
}
private static String updateBlockState(String material, String blockState) {
// since 1.16
if (material.endsWith("_wall")) {
if (blockState.contains("east=false") || blockState.contains("east=true")) {
blockState = blockState.replace("east=false", "east=none");
blockState = blockState.replace("west=false", "west=none");
blockState = blockState.replace("north=false", "north=none");
blockState = blockState.replace("south=false", "south=none");
blockState = blockState.replace("east=true", "east=low");
blockState = blockState.replace("west=true", "west=low");
blockState = blockState.replace("north=true", "north=low");
blockState = blockState.replace("south=true", "south=low");
}
}
return blockState;
}
}

View File

@ -1,750 +0,0 @@
/*
* Copyright 2011-2013 Tyler Blair. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and contributors and should not be interpreted as representing official policies,
* either expressed or implied, of anybody else.
*/
package de.diddiz.LogBlock;
import org.bukkit.Bukkit;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.scheduler.BukkitTask;
import java.io.*;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.*;
import java.util.logging.Level;
import java.util.zip.GZIPOutputStream;
public class Metrics {
/**
* The current revision number
*/
private final static int REVISION = 7;
/**
* The base url of the metrics domain
*/
private static final String BASE_URL = "http://report.mcstats.org";
/**
* The url used to report a server's status
*/
private static final String REPORT_URL = "/plugin/%s";
/**
* Interval of time to ping (in minutes)
*/
private static final int PING_INTERVAL = 15;
/**
* The plugin this metrics submits for
*/
private final Plugin plugin;
/**
* All of the custom graphs to submit to metrics
*/
private final Set<Graph> graphs = Collections.synchronizedSet(new HashSet<Graph>());
/**
* The plugin configuration file
*/
private final YamlConfiguration configuration;
/**
* The plugin configuration file
*/
private final File configurationFile;
/**
* Unique server id
*/
private final String guid;
/**
* Debug mode
*/
private final boolean debug;
/**
* Lock for synchronization
*/
private final Object optOutLock = new Object();
/**
* The scheduled task
*/
private volatile BukkitTask task = null;
public Metrics(final Plugin plugin) throws IOException {
if (plugin == null) {
throw new IllegalArgumentException("Plugin cannot be null");
}
this.plugin = plugin;
// load the config
configurationFile = getConfigFile();
configuration = YamlConfiguration.loadConfiguration(configurationFile);
// add some defaults
configuration.addDefault("opt-out", false);
configuration.addDefault("guid", UUID.randomUUID().toString());
configuration.addDefault("debug", false);
// Do we need to create the file?
if (configuration.get("guid", null) == null) {
configuration.options().header("http://mcstats.org").copyDefaults(true);
configuration.save(configurationFile);
}
// Load the guid then
guid = configuration.getString("guid");
debug = configuration.getBoolean("debug", false);
}
/**
* Construct and create a Graph that can be used to separate specific plotters to their own graphs on the metrics
* website. Plotters can be added to the graph object returned.
*
* @param name The name of the graph
* @return Graph object created. Will never return NULL under normal circumstances unless bad parameters are given
*/
public Graph createGraph(final String name) {
if (name == null) {
throw new IllegalArgumentException("Graph name cannot be null");
}
// Construct the graph object
final Graph graph = new Graph(name);
// Now we can add our graph
graphs.add(graph);
// and return back
return graph;
}
/**
* Add a Graph object to BukkitMetrics that represents data for the plugin that should be sent to the backend
*
* @param graph The name of the graph
*/
public void addGraph(final Graph graph) {
if (graph == null) {
throw new IllegalArgumentException("Graph cannot be null");
}
graphs.add(graph);
}
/**
* Start measuring statistics. This will immediately create an async repeating task as the plugin and send the
* initial data to the metrics backend, and then after that it will post in increments of PING_INTERVAL * 1200
* ticks.
*
* @return True if statistics measuring is running, otherwise false.
*/
public boolean start() {
synchronized (optOutLock) {
// Did we opt out?
if (isOptOut()) {
return false;
}
// Is metrics already running?
if (task != null) {
return true;
}
// Begin hitting the server with glorious data
task = plugin.getServer().getScheduler().runTaskTimerAsynchronously(plugin, new Runnable() {
private boolean firstPost = true;
public void run() {
try {
// This has to be synchronized or it can collide with the disable method.
synchronized (optOutLock) {
// Disable Task, if it is running and the server owner decided to opt-out
if (isOptOut() && task != null) {
task.cancel();
task = null;
// Tell all plotters to stop gathering information.
for (Graph graph : graphs) {
graph.onOptOut();
}
}
}
// We use the inverse of firstPost because if it is the first time we are posting,
// it is not a interval ping, so it evaluates to FALSE
// Each time thereafter it will evaluate to TRUE, i.e PING!
postPlugin(!firstPost);
// After the first post we set firstPost to false
// Each post thereafter will be a ping
firstPost = false;
} catch (IOException e) {
if (debug) {
Bukkit.getLogger().log(Level.INFO, "[Metrics] " + e.getMessage());
}
}
}
}, 0, PING_INTERVAL * 1200);
return true;
}
}
/**
* Has the server owner denied plugin metrics?
*
* @return true if metrics should be opted out of it
*/
public boolean isOptOut() {
synchronized (optOutLock) {
try {
// Reload the metrics file
configuration.load(getConfigFile());
} catch (IOException ex) {
if (debug) {
Bukkit.getLogger().log(Level.INFO, "[Metrics] " + ex.getMessage());
}
return true;
} catch (InvalidConfigurationException ex) {
if (debug) {
Bukkit.getLogger().log(Level.INFO, "[Metrics] " + ex.getMessage());
}
return true;
}
return configuration.getBoolean("opt-out", false);
}
}
/**
* Enables metrics for the server by setting "opt-out" to false in the config file and starting the metrics task.
*
* @throws java.io.IOException
*/
public void enable() throws IOException {
// This has to be synchronized or it can collide with the check in the task.
synchronized (optOutLock) {
// Check if the server owner has already set opt-out, if not, set it.
if (isOptOut()) {
configuration.set("opt-out", false);
configuration.save(configurationFile);
}
// Enable Task, if it is not running
if (task == null) {
start();
}
}
}
/**
* Disables metrics for the server by setting "opt-out" to true in the config file and canceling the metrics task.
*
* @throws java.io.IOException
*/
public void disable() throws IOException {
// This has to be synchronized or it can collide with the check in the task.
synchronized (optOutLock) {
// Check if the server owner has already set opt-out, if not, set it.
if (!isOptOut()) {
configuration.set("opt-out", true);
configuration.save(configurationFile);
}
// Disable Task, if it is running
if (task != null) {
task.cancel();
task = null;
}
}
}
/**
* Gets the File object of the config file that should be used to store data such as the GUID and opt-out status
*
* @return the File object for the config file
*/
public File getConfigFile() {
// I believe the easiest way to get the base folder (e.g craftbukkit set via -P) for plugins to use
// is to abuse the plugin object we already have
// plugin.getDataFolder() => base/plugins/PluginA/
// pluginsFolder => base/plugins/
// The base is not necessarily relative to the startup directory.
File pluginsFolder = plugin.getDataFolder().getParentFile();
// return => base/plugins/PluginMetrics/config.yml
return new File(new File(pluginsFolder, "PluginMetrics"), "config.yml");
}
/**
* Generic method that posts a plugin to the metrics website
*/
private void postPlugin(final boolean isPing) throws IOException {
// Server software specific section
PluginDescriptionFile description = plugin.getDescription();
String pluginName = description.getName();
boolean onlineMode = Bukkit.getServer().getOnlineMode(); // TRUE if online mode is enabled
String pluginVersion = description.getVersion();
String serverVersion = Bukkit.getVersion();
int playersOnline = Bukkit.getServer().getOnlinePlayers().length;
// END server software specific section -- all code below does not use any code outside of this class / Java
// Construct the post data
StringBuilder json = new StringBuilder(1024);
json.append('{');
// The plugin's description file containg all of the plugin data such as name, version, author, etc
appendJSONPair(json, "guid", guid);
appendJSONPair(json, "plugin_version", pluginVersion);
appendJSONPair(json, "server_version", serverVersion);
appendJSONPair(json, "players_online", Integer.toString(playersOnline));
// New data as of R6
String osname = System.getProperty("os.name");
String osarch = System.getProperty("os.arch");
String osversion = System.getProperty("os.version");
String java_version = System.getProperty("java.version");
int coreCount = Runtime.getRuntime().availableProcessors();
// normalize os arch .. amd64 -> x86_64
if (osarch.equals("amd64")) {
osarch = "x86_64";
}
appendJSONPair(json, "osname", osname);
appendJSONPair(json, "osarch", osarch);
appendJSONPair(json, "osversion", osversion);
appendJSONPair(json, "cores", Integer.toString(coreCount));
appendJSONPair(json, "auth_mode", onlineMode ? "1" : "0");
appendJSONPair(json, "java_version", java_version);
// If we're pinging, append it
if (isPing) {
appendJSONPair(json, "ping", "1");
}
if (graphs.size() > 0) {
synchronized (graphs) {
json.append(',');
json.append('"');
json.append("graphs");
json.append('"');
json.append(':');
json.append('{');
boolean firstGraph = true;
final Iterator<Graph> iter = graphs.iterator();
while (iter.hasNext()) {
Graph graph = iter.next();
StringBuilder graphJson = new StringBuilder();
graphJson.append('{');
for (Plotter plotter : graph.getPlotters()) {
appendJSONPair(graphJson, plotter.getColumnName(), Integer.toString(plotter.getValue()));
}
graphJson.append('}');
if (!firstGraph) {
json.append(',');
}
json.append(escapeJSON(graph.getName()));
json.append(':');
json.append(graphJson);
firstGraph = false;
}
json.append('}');
}
}
// close json
json.append('}');
// Create the url
URL url = new URL(BASE_URL + String.format(REPORT_URL, urlEncode(pluginName)));
// Connect to the website
URLConnection connection;
// Mineshafter creates a socks proxy, so we can safely bypass it
// It does not reroute POST requests so we need to go around it
if (isMineshafterPresent()) {
connection = url.openConnection(Proxy.NO_PROXY);
} else {
connection = url.openConnection();
}
byte[] uncompressed = json.toString().getBytes();
byte[] compressed = gzip(json.toString());
// Headers
connection.addRequestProperty("User-Agent", "MCStats/" + REVISION);
connection.addRequestProperty("Content-Type", "application/json");
connection.addRequestProperty("Content-Encoding", "gzip");
connection.addRequestProperty("Content-Length", Integer.toString(compressed.length));
connection.addRequestProperty("Accept", "application/json");
connection.addRequestProperty("Connection", "close");
connection.setDoOutput(true);
if (debug) {
System.out.println("[Metrics] Prepared request for " + pluginName + " uncompressed=" + uncompressed.length + " compressed=" + compressed.length);
}
// Write the data
OutputStream os = connection.getOutputStream();
os.write(compressed);
os.flush();
// Now read the response
final BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String response = reader.readLine();
// close resources
os.close();
reader.close();
if (response == null || response.startsWith("ERR") || response.startsWith("7")) {
if (response == null) {
response = "null";
} else if (response.startsWith("7")) {
response = response.substring(response.startsWith("7,") ? 2 : 1);
}
throw new IOException(response);
} else {
// Is this the first update this hour?
if (response.equals("1") || response.contains("This is your first update this hour")) {
synchronized (graphs) {
final Iterator<Graph> iter = graphs.iterator();
while (iter.hasNext()) {
final Graph graph = iter.next();
for (Plotter plotter : graph.getPlotters()) {
plotter.reset();
}
}
}
}
}
}
/**
* GZip compress a string of bytes
*
* @param input
* @return
*/
public static byte[] gzip(String input) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gzos = null;
try {
gzos = new GZIPOutputStream(baos);
gzos.write(input.getBytes("UTF-8"));
} catch (IOException e) {
e.printStackTrace();
} finally {
if (gzos != null) {
try {
gzos.close();
} catch (IOException ignore) {
}
}
}
return baos.toByteArray();
}
/**
* Check if mineshafter is present. If it is, we need to bypass it to send POST requests
*
* @return true if mineshafter is installed on the server
*/
private boolean isMineshafterPresent() {
try {
Class.forName("mineshafter.MineServer");
return true;
} catch (Exception e) {
return false;
}
}
/**
* Appends a json encoded key/value pair to the given string builder.
*
* @param json
* @param key
* @param value
* @throws UnsupportedEncodingException
*/
private static void appendJSONPair(StringBuilder json, String key, String value) throws UnsupportedEncodingException {
boolean isValueNumeric = false;
try {
if (value.equals("0") || !value.endsWith("0")) {
Double.parseDouble(value);
isValueNumeric = true;
}
} catch (NumberFormatException e) {
isValueNumeric = false;
}
if (json.charAt(json.length() - 1) != '{') {
json.append(',');
}
json.append(escapeJSON(key));
json.append(':');
if (isValueNumeric) {
json.append(value);
} else {
json.append(escapeJSON(value));
}
}
/**
* Escape a string to create a valid JSON string
*
* @param text
* @return
*/
private static String escapeJSON(String text) {
StringBuilder builder = new StringBuilder();
builder.append('"');
for (int index = 0; index < text.length(); index++) {
char chr = text.charAt(index);
switch (chr) {
case '"':
case '\\':
builder.append('\\');
builder.append(chr);
break;
case '\b':
builder.append("\\b");
break;
case '\t':
builder.append("\\t");
break;
case '\n':
builder.append("\\n");
break;
case '\r':
builder.append("\\r");
break;
default:
if (chr < ' ') {
String t = "000" + Integer.toHexString(chr);
builder.append("\\u" + t.substring(t.length() - 4));
} else {
builder.append(chr);
}
break;
}
}
builder.append('"');
return builder.toString();
}
/**
* Encode text as UTF-8
*
* @param text the text to encode
* @return the encoded text, as UTF-8
*/
private static String urlEncode(final String text) throws UnsupportedEncodingException {
return URLEncoder.encode(text, "UTF-8");
}
/**
* Represents a custom graph on the website
*/
public static class Graph {
/**
* The graph's name, alphanumeric and spaces only :) If it does not comply to the above when submitted, it is
* rejected
*/
private final String name;
/**
* The set of plotters that are contained within this graph
*/
private final Set<Plotter> plotters = new LinkedHashSet<Plotter>();
private Graph(final String name) {
this.name = name;
}
/**
* Gets the graph's name
*
* @return the Graph's name
*/
public String getName() {
return name;
}
/**
* Add a plotter to the graph, which will be used to plot entries
*
* @param plotter the plotter to add to the graph
*/
public void addPlotter(final Plotter plotter) {
plotters.add(plotter);
}
/**
* Remove a plotter from the graph
*
* @param plotter the plotter to remove from the graph
*/
public void removePlotter(final Plotter plotter) {
plotters.remove(plotter);
}
/**
* Gets an <b>unmodifiable</b> set of the plotter objects in the graph
*
* @return an unmodifiable {@link java.util.Set} of the plotter objects
*/
public Set<Plotter> getPlotters() {
return Collections.unmodifiableSet(plotters);
}
@Override
public int hashCode() {
return name.hashCode();
}
@Override
public boolean equals(final Object object) {
if (!(object instanceof Graph)) {
return false;
}
final Graph graph = (Graph) object;
return graph.name.equals(name);
}
/**
* Called when the server owner decides to opt-out of BukkitMetrics while the server is running.
*/
protected void onOptOut() {
}
}
/**
* Interface used to collect custom data for a plugin
*/
public static abstract class Plotter {
/**
* The plot's name
*/
private final String name;
/**
* Construct a plotter with the default plot name
*/
public Plotter() {
this("Default");
}
/**
* Construct a plotter with a specific plot name
*
* @param name the name of the plotter to use, which will show up on the website
*/
public Plotter(final String name) {
this.name = name;
}
/**
* Get the current value for the plotted point. Since this function defers to an external function it may or may
* not return immediately thus cannot be guaranteed to be thread friendly or safe. This function can be called
* from any thread so care should be taken when accessing resources that need to be synchronized.
*
* @return the current value for the point to be plotted.
*/
public abstract int getValue();
/**
* Get the column name for the plotted point
*
* @return the plotted point's column name
*/
public String getColumnName() {
return name;
}
/**
* Called after the website graphs have been updated
*/
public void reset() {
}
@Override
public int hashCode() {
return getColumnName().hashCode();
}
@Override
public boolean equals(final Object object) {
if (!(object instanceof Plotter)) {
return false;
}
final Plotter plotter = (Plotter) object;
return plotter.name.equals(name) && plotter.getValue() == getValue();
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,49 +1,49 @@
package de.diddiz.LogBlock; package de.diddiz.LogBlock;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import static de.diddiz.LogBlock.config.Config.toolsByType; import static de.diddiz.LogBlock.config.Config.toolsByType;
import static org.bukkit.Bukkit.getServer; import static org.bukkit.Bukkit.getServer;
public class Session { public class Session {
private static final Map<String, Session> sessions = new HashMap<String, Session>(); private static final Map<String, Session> sessions = new HashMap<>();
public QueryParams lastQuery = null; public QueryParams lastQuery = null;
public LookupCacheElement[] lookupCache = null; public LookupCacheElement[] lookupCache = null;
public int page = 1; public int page = 1;
public Map<Tool, ToolData> toolData; public Map<Tool, ToolData> toolData;
private Session(Player player) { private Session(Player player) {
toolData = new HashMap<Tool, ToolData>(); toolData = new HashMap<>();
final LogBlock logblock = LogBlock.getInstance(); final LogBlock logblock = LogBlock.getInstance();
if (player != null) { if (player != null) {
for (final Tool tool : toolsByType.values()) { for (final Tool tool : toolsByType.values()) {
toolData.put(tool, new ToolData(tool, logblock, player)); toolData.put(tool, new ToolData(tool, logblock, player));
} }
} }
} }
public static boolean hasSession(CommandSender sender) { public static boolean hasSession(CommandSender sender) {
return sessions.containsKey(sender.getName().toLowerCase()); return sessions.containsKey(sender.getName().toLowerCase());
} }
public static boolean hasSession(String playerName) { public static boolean hasSession(String playerName) {
return sessions.containsKey(playerName.toLowerCase()); return sessions.containsKey(playerName.toLowerCase());
} }
public static Session getSession(CommandSender sender) { public static Session getSession(CommandSender sender) {
return getSession(sender.getName()); return getSession(sender.getName());
} }
public static Session getSession(String playerName) { public static Session getSession(String playerName) {
Session session = sessions.get(playerName.toLowerCase()); Session session = sessions.get(playerName.toLowerCase());
if (session == null) { if (session == null) {
session = new Session(getServer().getPlayer(playerName)); session = new Session(getServer().getPlayer(playerName));
sessions.put(playerName.toLowerCase(), session); sessions.put(playerName.toLowerCase(), session);
} }
return session; return session;
} }
} }

View File

@ -1,36 +1,43 @@
package de.diddiz.LogBlock; package de.diddiz.LogBlock;
import de.diddiz.LogBlock.QueryParams.SummarizationMode; import static de.diddiz.LogBlock.util.MessagingUtil.prettyMaterial;
import org.bukkit.Location;
import de.diddiz.LogBlock.QueryParams.SummarizationMode;
import java.sql.ResultSet; import de.diddiz.LogBlock.util.MessagingUtil;
import java.sql.SQLException; import java.sql.ResultSet;
import java.sql.SQLException;
import static de.diddiz.util.MaterialName.materialName; import java.util.Objects;
import static de.diddiz.util.Utils.spaces; import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
public class SummedBlockChanges implements LookupCacheElement { import org.bukkit.Location;
private final String group;
private final int created, destroyed; public class SummedBlockChanges implements LookupCacheElement {
private final float spaceFactor; private final int type;
private final Actor actor; private final int created, destroyed;
private final float spaceFactor;
public SummedBlockChanges(ResultSet rs, QueryParams p, float spaceFactor) throws SQLException { private final Actor actor;
// Actor currently useless here as we don't yet output UUID in results anywhere
actor = p.sum == SummarizationMode.PLAYERS ? new Actor(rs) : null; public SummedBlockChanges(ResultSet rs, QueryParams p, float spaceFactor) throws SQLException {
group = actor == null ? materialName(rs.getInt("type")) : actor.getName(); // Actor currently useless here as we don't yet output UUID in results anywhere
created = rs.getInt("created"); actor = p.sum == SummarizationMode.PLAYERS ? new Actor(rs) : null;
destroyed = rs.getInt("destroyed"); type = p.sum == SummarizationMode.TYPES ? rs.getInt("type") : 0;
this.spaceFactor = spaceFactor; created = rs.getInt("created");
} destroyed = rs.getInt("destroyed");
this.spaceFactor = spaceFactor;
@Override }
public Location getLocation() {
return null; @Override
} public Location getLocation() {
return null;
@Override }
public String getMessage() {
return created + spaces((int) ((10 - String.valueOf(created).length()) / spaceFactor)) + destroyed + spaces((int) ((10 - String.valueOf(destroyed).length()) / spaceFactor)) + group; @Override
} public BaseComponent[] getLogMessage(int entry) {
} return MessagingUtil.formatSummarizedChanges(created, destroyed, actor != null ? new TextComponent(actor.getName()) : prettyMaterial(Objects.toString(MaterialConverter.getMaterial(type))), 10, 10, spaceFactor);
}
@Override
public int getNumChanges() {
return created + destroyed;
}
}

View File

@ -0,0 +1,44 @@
package de.diddiz.LogBlock;
import static de.diddiz.LogBlock.util.MessagingUtil.prettyMaterial;
import de.diddiz.LogBlock.QueryParams.SummarizationMode;
import de.diddiz.LogBlock.util.MessagingUtil;
import org.bukkit.Location;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Objects;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
public class SummedEntityChanges implements LookupCacheElement {
private final int type;
private final int created, destroyed;
private final float spaceFactor;
private final Actor actor;
public SummedEntityChanges(ResultSet rs, QueryParams p, float spaceFactor) throws SQLException {
// Actor currently useless here as we don't yet output UUID in results anywhere
actor = p.sum == SummarizationMode.PLAYERS ? new Actor(rs) : null;
type = p.sum == SummarizationMode.TYPES ? rs.getInt("entitytypeid") : 0;
created = rs.getInt("created");
destroyed = rs.getInt("destroyed");
this.spaceFactor = spaceFactor;
}
@Override
public Location getLocation() {
return null;
}
@Override
public BaseComponent[] getLogMessage(int entry) {
return MessagingUtil.formatSummarizedChanges(created, destroyed, actor != null ? new TextComponent(actor.getName()) : prettyMaterial(Objects.toString(EntityTypeConverter.getEntityType(type))), 10, 10, spaceFactor);
}
@Override
public int getNumChanges() {
return created + destroyed;
}
}

View File

@ -1,11 +1,11 @@
package de.diddiz.LogBlock; package de.diddiz.LogBlock;
import org.bukkit.Location; import de.diddiz.LogBlock.util.MessagingUtil;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import net.md_5.bungee.api.chat.BaseComponent;
import static de.diddiz.util.Utils.spaces; import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Location;
public class SummedKills implements LookupCacheElement { public class SummedKills implements LookupCacheElement {
private final Actor player; private final Actor player;
@ -25,7 +25,12 @@ public class SummedKills implements LookupCacheElement {
} }
@Override @Override
public String getMessage() { public BaseComponent[] getLogMessage(int entry) {
return kills + spaces((int) ((6 - String.valueOf(kills).length()) / spaceFactor)) + killed + spaces((int) ((7 - String.valueOf(killed).length()) / spaceFactor)) + player.getName(); return MessagingUtil.formatSummarizedChanges(kills, killed, new TextComponent(player.getName()), 6, 7, spaceFactor);
}
@Override
public int getNumChanges() {
return kills + killed;
} }
} }

View File

@ -1,30 +1,35 @@
package de.diddiz.LogBlock; package de.diddiz.LogBlock;
import org.bukkit.permissions.PermissionDefault; import org.bukkit.Material;
import org.bukkit.permissions.PermissionDefault;
import java.util.List;
import java.util.List;
public class Tool {
public final String name; public class Tool {
public final List<String> aliases; public final String name;
public final ToolBehavior leftClickBehavior, rightClickBehavior; public final List<String> aliases;
public final boolean defaultEnabled; public final ToolBehavior leftClickBehavior, rightClickBehavior;
public final int item; public final boolean defaultEnabled;
public final boolean canDrop; public final Material item;
public final QueryParams params; public final boolean canDrop;
public final ToolMode mode; public final QueryParams params;
public final PermissionDefault permissionDefault; public final ToolMode mode;
public final PermissionDefault permissionDefault;
public Tool(String name, List<String> aliases, ToolBehavior leftClickBehavior, ToolBehavior rightClickBehavior, boolean defaultEnabled, int item, boolean canDrop, QueryParams params, ToolMode mode, PermissionDefault permissionDefault) { public final boolean removeOnDisable;
this.name = name; public final boolean dropToDisable;
this.aliases = aliases;
this.leftClickBehavior = leftClickBehavior; public Tool(String name, List<String> aliases, ToolBehavior leftClickBehavior, ToolBehavior rightClickBehavior, boolean defaultEnabled, Material item, boolean canDrop, QueryParams params, ToolMode mode, PermissionDefault permissionDefault, boolean removeOnDisable, boolean dropToDisable) {
this.rightClickBehavior = rightClickBehavior; this.name = name;
this.defaultEnabled = defaultEnabled; this.aliases = aliases;
this.item = item; this.leftClickBehavior = leftClickBehavior;
this.canDrop = canDrop; this.rightClickBehavior = rightClickBehavior;
this.params = params; this.defaultEnabled = defaultEnabled;
this.mode = mode; this.item = item;
this.permissionDefault = permissionDefault; this.canDrop = canDrop;
} this.params = params;
} this.mode = mode;
this.permissionDefault = permissionDefault;
this.removeOnDisable = removeOnDisable;
this.dropToDisable = dropToDisable;
}
}

View File

@ -1,5 +1,7 @@
package de.diddiz.LogBlock; package de.diddiz.LogBlock;
public enum ToolBehavior { public enum ToolBehavior {
TOOL, BLOCK, NONE TOOL,
} BLOCK,
NONE
}

View File

@ -1,15 +1,15 @@
package de.diddiz.LogBlock; package de.diddiz.LogBlock;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
public class ToolData { public class ToolData {
public boolean enabled; public boolean enabled;
public QueryParams params; public QueryParams params;
public ToolMode mode; public ToolMode mode;
public ToolData(Tool tool, LogBlock logblock, Player player) { public ToolData(Tool tool, LogBlock logblock, Player player) {
enabled = tool.defaultEnabled && logblock.hasPermission(player, "logblock.tools." + tool.name); enabled = tool.defaultEnabled && logblock.hasPermission(player, "logblock.tools." + tool.name);
params = tool.params.clone(); params = tool.params.clone();
mode = tool.mode; mode = tool.mode;
} }
} }

View File

@ -1,14 +1,18 @@
package de.diddiz.LogBlock; package de.diddiz.LogBlock;
public enum ToolMode { public enum ToolMode {
CLEARLOG("logblock.clearlog"), LOOKUP("logblock.lookup"), REDO("logblock.rollback"), ROLLBACK("logblock.rollback"), WRITELOGFILE("logblock.rollback"); CLEARLOG("logblock.clearlog"),
private final String permission; LOOKUP("logblock.lookup"),
REDO("logblock.rollback"),
private ToolMode(String permission) { ROLLBACK("logblock.rollback"),
this.permission = permission; WRITELOGFILE("logblock.rollback");
} private final String permission;
public String getPermission() { private ToolMode(String permission) {
return permission; this.permission = permission;
} }
}
public String getPermission() {
return permission;
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,269 +1,493 @@
package de.diddiz.LogBlock; package de.diddiz.LogBlock;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState; import org.bukkit.block.BlockState;
import org.bukkit.block.Sign; import org.bukkit.block.Container;
import org.bukkit.command.CommandSender; import org.bukkit.block.data.Bisected.Half;
import org.bukkit.inventory.InventoryHolder; import org.bukkit.block.data.Bisected;
import org.bukkit.inventory.ItemStack; import org.bukkit.block.data.BlockData;
import org.bukkit.material.Bed; import org.bukkit.block.data.type.Bed;
import org.bukkit.material.PistonBaseMaterial; import org.bukkit.block.data.type.Chest;
import org.bukkit.material.PistonExtensionMaterial; import org.bukkit.block.data.type.Bed.Part;
import org.bukkit.block.data.type.Piston;
import java.io.File; import org.bukkit.block.data.type.PistonHead;
import java.io.PrintWriter; import org.bukkit.block.data.type.TechnicalPiston.Type;
import java.text.SimpleDateFormat; import org.bukkit.command.CommandSender;
import java.util.ArrayList; import org.bukkit.configuration.file.YamlConfiguration;
import java.util.List; import org.bukkit.entity.ArmorStand;
import java.util.Queue; import org.bukkit.entity.Bee;
import java.util.concurrent.LinkedBlockingQueue; import org.bukkit.entity.Entity;
import java.util.logging.Level; import org.bukkit.entity.ItemFrame;
import org.bukkit.inventory.EquipmentSlot;
import static de.diddiz.LogBlock.config.Config.dontRollback; import org.bukkit.inventory.ItemStack;
import static de.diddiz.LogBlock.config.Config.replaceAnyway;
import static de.diddiz.util.BukkitUtils.*; import de.diddiz.LogBlock.QueryParams.Order;
import static de.diddiz.util.MaterialName.materialName; import de.diddiz.LogBlock.blockstate.BlockStateCodecs;
import static org.bukkit.Bukkit.getLogger; import de.diddiz.LogBlock.util.BukkitUtils;
import de.diddiz.LogBlock.util.Utils;
public class WorldEditor implements Runnable { import de.diddiz.LogBlock.worldedit.WorldEditHelper;
private final LogBlock logblock; import java.io.File;
private final Queue<Edit> edits = new LinkedBlockingQueue<Edit>(); import java.io.PrintWriter;
private final World world; import java.sql.ResultSet;
import java.sql.SQLException;
/** import java.text.SimpleDateFormat;
* The player responsible for editing the world, used to report progress import java.util.ArrayList;
*/ import java.util.Collections;
private CommandSender sender; import java.util.Comparator;
private int taskID; import java.util.HashMap;
private int successes = 0, blacklistCollisions = 0; import java.util.List;
private long elapsedTime = 0; import java.util.UUID;
public LookupCacheElement[] errors; import java.util.logging.Level;
import net.md_5.bungee.api.chat.BaseComponent;
public WorldEditor(LogBlock logblock, World world) { import net.md_5.bungee.api.chat.TextComponent;
this.logblock = logblock;
this.world = world; import static de.diddiz.LogBlock.config.Config.dontRollback;
} import static de.diddiz.LogBlock.config.Config.replaceAnyway;
import static de.diddiz.LogBlock.util.BukkitUtils.*;
public int getSize() {
return edits.size(); public class WorldEditor implements Runnable {
} private final LogBlock logblock;
private final ArrayList<Edit> edits = new ArrayList<>();
public int getSuccesses() { private final World world;
return successes;
} /**
* The player responsible for editing the world, used to report progress
public int getErrors() { */
return errors.length; private CommandSender sender;
} private int taskID;
private int successes = 0, blacklistCollisions = 0;
public int getBlacklistCollisions() { private long elapsedTime = 0;
return blacklistCollisions; public LookupCacheElement[] errors;
} private boolean forceReplace;
private HashMap<Integer, UUID> uuidReplacements = new HashMap<>();
private boolean started = false;
public void setSender(CommandSender sender) {
this.sender = sender; public WorldEditor(LogBlock logblock, World world) {
} this(logblock, world, false);
}
public void queueEdit(int x, int y, int z, int replaced, int type, byte data, String signtext, short itemType, short itemAmount, short itemData) {
edits.add(new Edit(0, new Location(world, x, y, z), null, replaced, type, data, signtext, new ChestAccess(itemType, itemAmount, itemData))); public WorldEditor(LogBlock logblock, World world, boolean forceReplace) {
} this.logblock = logblock;
this.world = world;
public long getElapsedTime() { this.forceReplace = forceReplace;
return elapsedTime; }
}
public int getSize() {
synchronized public void start() throws Exception { return edits.size();
final long start = System.currentTimeMillis(); }
taskID = logblock.getServer().getScheduler().scheduleSyncRepeatingTask(logblock, this, 0, 1);
if (taskID == -1) { public int getSuccesses() {
throw new Exception("Failed to schedule task"); return successes;
} }
try {
this.wait(); public int getErrors() {
} catch (final InterruptedException ex) { return errors.length;
throw new Exception("Interrupted"); }
}
elapsedTime = System.currentTimeMillis() - start; public int getBlacklistCollisions() {
} return blacklistCollisions;
}
@Override
public synchronized void run() { public void setSender(CommandSender sender) {
final List<WorldEditorException> errorList = new ArrayList<WorldEditorException>(); this.sender = sender;
int counter = 0; }
float size = edits.size();
while (!edits.isEmpty() && counter < 100) { public void queueBlockEdit(long time, int x, int y, int z, int replaced, int replaceData, byte[] replacedState, int type, int typeData, byte[] typeState, ChestAccess item) {
try { if (started) {
switch (edits.poll().perform()) { throw new IllegalStateException("Already started");
case SUCCESS: }
successes++; edits.add(new BlockEdit(time, new Location(world, x, y, z), null, replaced, replaceData, replacedState, type, typeData, typeState, item));
break; }
case BLACKLISTED:
blacklistCollisions++; public void queueEntityEdit(ResultSet rs, QueryParams p, boolean rollback) throws SQLException {
break; if (started) {
} throw new IllegalStateException("Already started");
} catch (final WorldEditorException ex) { }
errorList.add(ex); edits.add(new EntityEdit(rs, p, rollback));
} catch (final Exception ex) { }
getLogger().log(Level.WARNING, "[WorldEditor] Exeption: ", ex);
} public void reverseRowOrder() {
counter++; if (started) {
if (sender != null) { throw new IllegalStateException("Already started");
float percentage = ((size - edits.size()) / size) * 100.0F; }
if (percentage % 20 == 0) { Collections.reverse(edits);
sender.sendMessage(ChatColor.GOLD + "[LogBlock]" + ChatColor.YELLOW + " Rollback progress: " + percentage + "%" + }
" Blocks edited: " + counter);
} public void sortRows(QueryParams.Order order) {
} if (started) {
} throw new IllegalStateException("Already started");
if (edits.isEmpty()) { }
logblock.getServer().getScheduler().cancelTask(taskID); edits.sort(new EditComparator(order));
if (errorList.size() > 0) { }
try {
final File file = new File("plugins/LogBlock/error/WorldEditor-" + new SimpleDateFormat("yy-MM-dd-HH-mm-ss").format(System.currentTimeMillis()) + ".log"); public long getElapsedTime() {
file.getParentFile().mkdirs(); return elapsedTime;
final PrintWriter writer = new PrintWriter(file); }
for (final LookupCacheElement err : errorList) {
writer.println(err.getMessage()); synchronized public void start() throws Exception {
} if (started) {
writer.close(); throw new IllegalStateException("Already started");
} catch (final Exception ex) { }
} started = true;
} final long start = System.currentTimeMillis();
errors = errorList.toArray(new WorldEditorException[errorList.size()]); taskID = logblock.getServer().getScheduler().scheduleSyncRepeatingTask(logblock, this, 0, 1);
notify(); if (taskID == -1) {
} throw new Exception("Failed to schedule task");
} }
try {
private static enum PerformResult { this.wait();
SUCCESS, BLACKLISTED, NO_ACTION } catch (final InterruptedException ex) {
} throw new Exception("Interrupted");
}
private class Edit extends BlockChange { elapsedTime = System.currentTimeMillis() - start;
public Edit(long time, Location loc, Actor actor, int replaced, int type, byte data, String signtext, ChestAccess ca) { }
super(time, loc, actor, replaced, type, data, signtext, ca);
} @Override
public synchronized void run() {
PerformResult perform() throws WorldEditorException { final List<WorldEditorException> errorList = new ArrayList<>();
if (dontRollback.contains(replaced)) { int counter = 0;
return PerformResult.BLACKLISTED; float size = edits.size();
} while (!edits.isEmpty() && counter < 100) {
final Block block = loc.getBlock(); try {
if (replaced == 0 && block.getTypeId() == 0) { switch (edits.remove(edits.size() - 1).perform()) {
return PerformResult.NO_ACTION; case SUCCESS:
} successes++;
final BlockState state = block.getState(); break;
if (!world.isChunkLoaded(block.getChunk())) { case BLACKLISTED:
world.loadChunk(block.getChunk()); blacklistCollisions++;
} break;
if (type == replaced) { case NO_ACTION:
if (type == 0) { break;
if (!block.setTypeId(0)) { }
throw new WorldEditorException(block.getTypeId(), 0, block.getLocation()); } catch (final WorldEditorException ex) {
} errorList.add(ex);
} else if (ca != null) { } catch (final Exception ex) {
if (getContainerBlocks().contains(Material.getMaterial(type))) { logblock.getLogger().log(Level.WARNING, "[WorldEditor] Exeption: ", ex);
int leftover; }
try { counter++;
leftover = modifyContainer(state, new ItemStack(ca.itemType, -ca.itemAmount, ca.itemData)); if (sender != null) {
// Special-case blocks which might be double chests float percentage = ((size - edits.size()) / size) * 100.0F;
if (leftover > 0 && (type == 54 || type == 146)) { if (percentage % 20 == 0) {
for (final BlockFace face : new BlockFace[]{BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST}) { sender.sendMessage(ChatColor.GOLD + "[LogBlock]" + ChatColor.YELLOW + " Rollback progress: " + percentage + "%" +
if (block.getRelative(face).getTypeId() == type) { " Blocks edited: " + counter);
leftover = modifyContainer(block.getRelative(face).getState(), new ItemStack(ca.itemType, ca.itemAmount < 0 ? leftover : -leftover, ca.itemData)); }
} }
} }
} if (edits.isEmpty()) {
} catch (final Exception ex) { logblock.getServer().getScheduler().cancelTask(taskID);
throw new WorldEditorException(ex.getMessage(), block.getLocation()); if (errorList.size() > 0) {
} try {
if (!state.update()) { final File errorDir = new File(logblock.getDataFolder(), "error");
throw new WorldEditorException("Failed to update inventory of " + materialName(block.getTypeId()), block.getLocation()); errorDir.mkdirs();
} final File file = new File(errorDir, "WorldEditor-" + new SimpleDateFormat("yy-MM-dd-HH-mm-ss").format(System.currentTimeMillis()) + ".log");
if (leftover > 0 && ca.itemAmount < 0) { final PrintWriter writer = new PrintWriter(file);
throw new WorldEditorException("Not enough space left in " + materialName(block.getTypeId()), block.getLocation()); for (final WorldEditorException err : errorList) {
} writer.println(BaseComponent.toPlainText(err.getLogMessage()));
} err.printStackTrace(writer);
} else { writer.println();
return PerformResult.NO_ACTION; writer.println();
} }
return PerformResult.SUCCESS; writer.close();
} } catch (final Exception ex) {
if (!(equalTypes(block.getTypeId(), type) || replaceAnyway.contains(block.getTypeId()))) { }
return PerformResult.NO_ACTION; }
} errors = errorList.toArray(new WorldEditorException[errorList.size()]);
if (state instanceof InventoryHolder) { notify();
((InventoryHolder) state).getInventory().clear(); }
state.update(); }
}
if (block.getTypeId() == replaced) { protected UUID getReplacedUUID(int entityid, UUID unreplaced) {
if (block.getData() != (type == 0 ? data : (byte) 0)) { UUID replaced = uuidReplacements.get(entityid);
block.setData(type == 0 ? data : (byte) 0, true); return replaced != null ? replaced : unreplaced;
} else { }
return PerformResult.NO_ACTION;
} public static enum PerformResult {
} else if (!block.setTypeIdAndData(replaced, type == 0 ? data : (byte) 0, true)) { SUCCESS,
throw new WorldEditorException(block.getTypeId(), replaced, block.getLocation()); BLACKLISTED,
} NO_ACTION
final int curtype = block.getTypeId(); }
if (signtext != null && (curtype == 63 || curtype == 68)) {
final Sign sign = (Sign) block.getState(); public interface Edit {
final String[] lines = signtext.split("\0", 4); PerformResult perform() throws WorldEditorException;
if (lines.length < 4) {
return PerformResult.NO_ACTION; public long getTime();
} }
for (int i = 0; i < 4; i++) {
sign.setLine(i, lines[i]); public class EntityEdit extends EntityChange implements Edit {
} private boolean rollback;
if (!sign.update()) {
throw new WorldEditorException("Failed to update signtext of " + materialName(block.getTypeId()), block.getLocation()); public EntityEdit(ResultSet rs, QueryParams p, boolean rollback) throws SQLException {
} super(rs, p);
} else if (curtype == 26) { this.rollback = rollback;
final Bed bed = (Bed) block.getState().getData(); }
final Block secBlock = bed.isHeadOfBed() ? block.getRelative(bed.getFacing().getOppositeFace()) : block.getRelative(bed.getFacing());
if (secBlock.getTypeId() == 0 && !secBlock.setTypeIdAndData(26, (byte) (bed.getData() | 8), true)) { @Override
throw new WorldEditorException(secBlock.getTypeId(), 26, secBlock.getLocation()); public long getTime() {
} return date;
} else if ((curtype == 29 || curtype == 33) && (block.getData() & 8) > 0) { }
final PistonBaseMaterial piston = (PistonBaseMaterial) block.getState().getData();
final Block secBlock = block.getRelative(piston.getFacing()); @Override
if (secBlock.getTypeId() == 0 && !secBlock.setTypeIdAndData(34, curtype == 29 ? (byte) (block.getData() | 8) : (byte) (block.getData() & ~8), true)) { public PerformResult perform() throws WorldEditorException {
throw new WorldEditorException(secBlock.getTypeId(), 34, secBlock.getLocation()); if (type == null) {
} throw new WorldEditorException("Unkown entity type for entity " + entityUUID, loc);
} else if (curtype == 34) { }
final PistonExtensionMaterial piston = (PistonExtensionMaterial) block.getState().getData(); if (changeType == (rollback ? EntityChangeType.KILL : EntityChangeType.CREATE)) {
final Block secBlock = block.getRelative(piston.getFacing().getOppositeFace()); // spawn entity
if (secBlock.getTypeId() == 0 && !secBlock.setTypeIdAndData(piston.isSticky() ? 29 : 33, (byte) (block.getData() | 8), true)) { UUID uuid = getReplacedUUID(entityId, entityUUID);
throw new WorldEditorException(secBlock.getTypeId(), piston.isSticky() ? 29 : 33, secBlock.getLocation()); Entity result = null;
} YamlConfiguration deserialized = Utils.deserializeYamlConfiguration(data);
} else if (curtype == 18 && (block.getData() & 8) > 0) { double x = deserialized.getDouble("x");
block.setData((byte) (block.getData() & 0xF7)); double y = deserialized.getDouble("y");
} double z = deserialized.getDouble("z");
return PerformResult.SUCCESS; float yaw = (float) deserialized.getDouble("yaw");
} float pitch = (float) deserialized.getDouble("pitch");
} Location location = new Location(world, x, y, z, yaw, pitch);
Entity existing = BukkitUtils.loadEntityAround(location.getChunk(), uuid);
@SuppressWarnings("serial") if (existing != null) {
public static class WorldEditorException extends Exception implements LookupCacheElement { return PerformResult.NO_ACTION;
private final Location loc; }
byte[] serializedWorldEditEntity = (byte[]) deserialized.get("worldedit");
public WorldEditorException(int typeBefore, int typeAfter, Location loc) { if (serializedWorldEditEntity != null) {
this("Failed to replace " + materialName(typeBefore) + " with " + materialName(typeAfter), loc); result = WorldEditHelper.restoreEntity(location, type, serializedWorldEditEntity);
} }
if (result == null) {
public WorldEditorException(String msg, Location loc) { throw new WorldEditorException("Could not restore " + type, location);
super(msg + " at " + loc.getWorld().getName() + ":" + loc.getBlockX() + ":" + loc.getBlockY() + ":" + loc.getBlockZ()); } else {
this.loc = loc; if (!result.getUniqueId().equals(uuid)) {
} logblock.getConsumer().queueEntityUUIDChange(world, entityId, result.getUniqueId());
uuidReplacements.put(entityId, result.getUniqueId());
@Override }
public Location getLocation() { }
return loc; return PerformResult.SUCCESS;
} } else if (changeType == (rollback ? EntityChangeType.CREATE : EntityChangeType.KILL)) {
} // kill entity
} UUID uuid = getReplacedUUID(entityId, entityUUID);
YamlConfiguration deserialized = Utils.deserializeYamlConfiguration(data);
double x = deserialized.getDouble("x");
double y = deserialized.getDouble("y");
double z = deserialized.getDouble("z");
float yaw = (float) deserialized.getDouble("yaw");
float pitch = (float) deserialized.getDouble("pitch");
Location location = new Location(world, x, y, z, yaw, pitch);
Entity existing = BukkitUtils.loadEntityAround(location.getChunk(), uuid);
if (existing != null) {
existing.remove();
return PerformResult.SUCCESS;
}
return PerformResult.NO_ACTION; // the entity is not there, so we cannot do anything
} else if (changeType == (rollback ? EntityChangeType.REMOVEEQUIP : EntityChangeType.ADDEQUIP)) {
// set equip
UUID uuid = getReplacedUUID(entityId, entityUUID);
Entity existing = BukkitUtils.loadEntityAround(loc.getChunk(), uuid);
if (existing != null) {
YamlConfiguration deserialized = Utils.deserializeYamlConfiguration(data);
ItemStack item = deserialized.getItemStack("item");
if (item != null && existing instanceof ItemFrame) {
ItemStack old = ((ItemFrame) existing).getItem();
if (old == null || old.getType() == Material.AIR) {
((ItemFrame) existing).setItem(item);
return PerformResult.SUCCESS;
}
} else if (item != null && existing instanceof ArmorStand) {
EquipmentSlot slot = EquipmentSlot.valueOf(deserialized.getString("slot"));
ArmorStand stand = (ArmorStand) existing;
ItemStack old = BukkitUtils.getItemInSlot(stand, slot);
if (old == null || old.getType() == Material.AIR) {
BukkitUtils.setItemInSlot(stand, slot, item);
return PerformResult.SUCCESS;
}
}
}
return PerformResult.NO_ACTION; // the entity is not there, or equip does not match
} else if (changeType == (rollback ? EntityChangeType.ADDEQUIP : EntityChangeType.REMOVEEQUIP)) {
// remove equip
UUID uuid = getReplacedUUID(entityId, entityUUID);
Entity existing = BukkitUtils.loadEntityAround(loc.getChunk(), uuid);
if (existing != null) {
YamlConfiguration deserialized = Utils.deserializeYamlConfiguration(data);
ItemStack item = deserialized.getItemStack("item");
if (item != null && existing instanceof ItemFrame) {
ItemStack old = ((ItemFrame) existing).getItem();
if (old != null && old.isSimilar(item)) {
((ItemFrame) existing).setItem(null);
return PerformResult.SUCCESS;
}
} else if (item != null && existing instanceof ArmorStand) {
EquipmentSlot slot = EquipmentSlot.valueOf(deserialized.getString("slot"));
ArmorStand stand = (ArmorStand) existing;
ItemStack old = BukkitUtils.getItemInSlot(stand, slot);
if (old != null && old.isSimilar(item)) {
BukkitUtils.setItemInSlot(stand, slot, null);
return PerformResult.SUCCESS;
}
}
}
return PerformResult.NO_ACTION; // the entity is not there, or equip does not match
} else if (changeType == EntityChangeType.GET_STUNG) {
UUID uuid = getReplacedUUID(entityId, entityUUID);
Entity existing = BukkitUtils.loadEntityAround(loc.getChunk(), uuid);
if (existing != null && existing instanceof Bee) {
((Bee) existing).setHasStung(!rollback);
}
}
return PerformResult.NO_ACTION;
}
}
public class BlockEdit extends BlockChange implements Edit {
public BlockEdit(long time, Location loc, Actor actor, int replaced, int replaceData, byte[] replacedState, int type, int typeData, byte[] typeState, ChestAccess ca) {
super(time, loc, actor, replaced, replaceData, replacedState, type, typeData, typeState, ca);
}
@Override
public long getTime() {
return date;
}
@Override
public PerformResult perform() throws WorldEditorException {
BlockData replacedBlock = getBlockReplaced();
BlockData setBlock = getBlockSet();
if (replacedBlock == null || setBlock == null) {
throw new WorldEditorException("Could not parse the material", loc.clone());
}
// action: set to replaced
if (dontRollback.contains(replacedBlock.getMaterial())) {
return PerformResult.BLACKLISTED;
}
final Block block = loc.getBlock();
if (BukkitUtils.isEmpty(replacedBlock.getMaterial()) && BukkitUtils.isEmpty(block.getType())) {
return PerformResult.NO_ACTION;
}
BlockState state = block.getState();
if (setBlock.equals(replacedBlock)) {
if (ca != null) {
if (state instanceof Container && state.getType() == replacedBlock.getMaterial()) {
int leftover;
try {
leftover = modifyContainer(state, new ItemStack(ca.itemStack), !ca.remove);
} catch (final Exception ex) {
throw new WorldEditorException(ex.getMessage(), block.getLocation());
}
if (leftover > 0 && ca.remove) {
throw new WorldEditorException("Not enough space left in " + block.getType(), block.getLocation());
}
return PerformResult.SUCCESS;
}
return PerformResult.NO_ACTION;
}
}
if (!forceReplace && !BukkitUtils.isSimilarForRollback(setBlock.getMaterial(), block.getType()) && !block.isEmpty() && !replaceAnyway.contains(block.getType())) {
return PerformResult.NO_ACTION;
}
if (state instanceof Container && replacedBlock.getMaterial() != block.getType()) {
((Container) state).getSnapshotInventory().clear();
state.update();
}
block.setBlockData(replacedBlock);
BlockData newData = block.getBlockData();
if (BlockStateCodecs.hasCodec(replacedBlock.getMaterial())) {
state = block.getState();
try {
BlockStateCodecs.deserialize(state, Utils.deserializeYamlConfiguration(replacedState));
state.update();
} catch (Exception e) {
throw new WorldEditorException("Failed to restore blockstate of " + block.getType() + ": " + e, block.getLocation());
}
}
final Material curtype = block.getType();
if (newData instanceof Bed) {
final Bed bed = (Bed) newData;
final Block secBlock = bed.getPart() == Part.HEAD ? block.getRelative(bed.getFacing().getOppositeFace()) : block.getRelative(bed.getFacing());
if (secBlock.isEmpty()) {
Bed bed2 = (Bed) bed.clone();
bed2.setPart(bed.getPart() == Part.HEAD ? Part.FOOT : Part.HEAD);
secBlock.setBlockData(bed2);
}
} else if (curtype == Material.IRON_DOOR || BukkitUtils.isWoodenDoor(curtype) || BukkitUtils.isDoublePlant(curtype)) {
final Bisected firstPart = (Bisected) newData;
final Block secBlock = block.getRelative(firstPart.getHalf() == Half.TOP ? BlockFace.DOWN : BlockFace.UP);
if (secBlock.isEmpty()) {
Bisected secondPart = (Bisected) firstPart.clone();
secondPart.setHalf(firstPart.getHalf() == Half.TOP ? Half.BOTTOM : Half.TOP);
secBlock.setBlockData(secondPart);
}
} else if ((curtype == Material.PISTON || curtype == Material.STICKY_PISTON)) {
Piston piston = (Piston) newData;
if (piston.isExtended()) {
final Block secBlock = block.getRelative(piston.getFacing());
if (secBlock.isEmpty()) {
PistonHead head = (PistonHead) Material.PISTON_HEAD.createBlockData();
head.setFacing(piston.getFacing());
head.setType(curtype == Material.PISTON ? Type.NORMAL : Type.STICKY);
secBlock.setBlockData(head);
}
}
} else if (curtype == Material.PISTON_HEAD) {
PistonHead head = (PistonHead) newData;
final Block secBlock = block.getRelative(head.getFacing().getOppositeFace());
if (secBlock.isEmpty()) {
Piston piston = (Piston) (head.getType() == Type.NORMAL ? Material.PISTON : Material.STICKY_PISTON).createBlockData();
piston.setFacing(head.getFacing());
piston.setExtended(true);
secBlock.setBlockData(piston);
}
} else if (newData instanceof Chest) {
Chest chest = (Chest) newData;
if (chest.getType() != org.bukkit.block.data.type.Chest.Type.SINGLE) {
if (getConnectedChest(block) == null) {
chest.setType(org.bukkit.block.data.type.Chest.Type.SINGLE);
block.setBlockData(chest);
}
}
}
return PerformResult.SUCCESS;
}
}
public static class EditComparator implements Comparator<Edit> {
private final int mult;
public EditComparator(QueryParams.Order order) {
mult = order == Order.DESC ? 1 : -1;
}
@Override
public int compare(Edit edit1, Edit edit2) {
long time1 = edit1.getTime();
long time2 = edit2.getTime();
return time1 > time2 ? mult : time1 < time2 ? -mult : 0;
}
}
@SuppressWarnings("serial")
public static class WorldEditorException extends Exception implements LookupCacheElement {
private final Location loc;
public WorldEditorException(Material typeBefore, Material typeAfter, Location loc) {
this("Failed to replace " + typeBefore.name() + " with " + typeAfter.name(), loc);
}
public WorldEditorException(String msg, Location loc) {
super(msg + " at " + loc.getWorld().getName() + ":" + loc.getBlockX() + ":" + loc.getBlockY() + ":" + loc.getBlockZ());
this.loc = loc;
}
@Override
public Location getLocation() {
return loc;
}
@Override
public BaseComponent[] getLogMessage(int entry) {
return TextComponent.fromLegacyText(getMessage());
}
}
}

View File

@ -0,0 +1,38 @@
package de.diddiz.LogBlock;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.bukkit.inventory.ItemStack;
import de.diddiz.LogBlock.QueryParams.BlockChangeType;
import de.diddiz.LogBlock.util.Utils;
public class WorldEditorEditFactory {
private final WorldEditor editor;
private final boolean rollback;
private final QueryParams params;
public WorldEditorEditFactory(WorldEditor editor, QueryParams params, boolean rollback) {
this.editor = editor;
this.params = params;
this.rollback = rollback;
}
public void processRow(ResultSet rs) throws SQLException {
if (params.bct == BlockChangeType.ENTITIES) {
editor.queueEntityEdit(rs, params, rollback);
return;
}
ChestAccess chestaccess = null;
ItemStack stack = Utils.loadItemStack(rs.getBytes("item"));
if (stack != null) {
chestaccess = new ChestAccess(stack, rs.getBoolean("itemremove") == rollback, rs.getInt("itemtype"));
}
if (rollback) {
editor.queueBlockEdit(rs.getTimestamp("date").getTime(), rs.getInt("x"), rs.getInt("y"), rs.getInt("z"), rs.getInt("replaced"), rs.getInt("replacedData"), rs.getBytes("replacedState"), rs.getInt("type"), rs.getInt("typeData"), rs.getBytes("typeState"), chestaccess);
} else {
editor.queueBlockEdit(rs.getTimestamp("date").getTime(), rs.getInt("x"), rs.getInt("y"), rs.getInt("z"), rs.getInt("type"), rs.getInt("typeData"), rs.getBytes("typeState"), rs.getInt("replaced"), rs.getInt("replacedData"), rs.getBytes("replacedState"), chestaccess);
}
}
}

View File

@ -0,0 +1,80 @@
package de.diddiz.LogBlock.addons.worldguard;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.association.RegionAssociable;
import com.sk89q.worldguard.protection.flags.StateFlag;
import com.sk89q.worldguard.protection.flags.registry.FlagConflictException;
import com.sk89q.worldguard.protection.flags.registry.FlagRegistry;
import de.diddiz.LogBlock.LogBlock;
import de.diddiz.LogBlock.events.BlockChangePreLogEvent;
import de.diddiz.LogBlock.events.EntityChangePreLogEvent;
import java.util.logging.Level;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
public class WorldGuardLoggingFlagsAddon {
private final StateFlag LOGBLOCK_LOG_BLOCKS = new StateFlag("logblock-log-blocks", true);
private final StateFlag LOGBLOCK_LOG_ENTITIES = new StateFlag("logblock-log-entities", true);
private LogBlock plugin;
private WorldGuardPlugin worldGuard;
public WorldGuardLoggingFlagsAddon(LogBlock plugin) {
this.plugin = plugin;
}
public void onPluginLoad() {
registerFlags();
}
public void onPluginEnable() {
worldGuard = (WorldGuardPlugin) plugin.getServer().getPluginManager().getPlugin("WorldGuard");
plugin.getServer().getPluginManager().registerEvents(new LoggingListener(), plugin);
}
private void registerFlags() {
try {
FlagRegistry registry = WorldGuard.getInstance().getFlagRegistry();
registry.register(LOGBLOCK_LOG_BLOCKS);
registry.register(LOGBLOCK_LOG_ENTITIES);
} catch (FlagConflictException e) {
plugin.getLogger().log(Level.SEVERE, "Could not initialize Flags", e);
}
}
private class LoggingListener implements Listener {
@EventHandler(ignoreCancelled = true)
public void onBlockChangePreLog(BlockChangePreLogEvent event) {
RegionAssociable regionAssociable = null;
Location location = event.getLocation();
Entity actorEntity = event.getOwnerActor().getEntity();
if (actorEntity instanceof Player) {
regionAssociable = worldGuard.wrapPlayer((Player) actorEntity);
}
ApplicableRegionSet set = WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery().getApplicableRegions(BukkitAdapter.adapt(location));
if (!set.testState(regionAssociable, LOGBLOCK_LOG_BLOCKS)) {
event.setCancelled(true);
}
}
@EventHandler(ignoreCancelled = true)
public void onEntityChangePreLog(EntityChangePreLogEvent event) {
RegionAssociable regionAssociable = null;
Location location = event.getLocation();
Entity actorEntity = event.getOwnerActor().getEntity();
if (actorEntity instanceof Player) {
regionAssociable = worldGuard.wrapPlayer((Player) actorEntity);
}
ApplicableRegionSet set = WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery().getApplicableRegions(BukkitAdapter.adapt(location));
if (!set.testState(regionAssociable, LOGBLOCK_LOG_ENTITIES)) {
event.setCancelled(true);
}
}
}
}

View File

@ -0,0 +1,16 @@
package de.diddiz.LogBlock.blockstate;
import net.md_5.bungee.api.chat.BaseComponent;
import org.bukkit.Material;
import org.bukkit.block.BlockState;
import org.bukkit.configuration.file.YamlConfiguration;
public interface BlockStateCodec {
Material[] getApplicableMaterials();
YamlConfiguration serialize(BlockState state);
void deserialize(BlockState state, YamlConfiguration conf);
BaseComponent getChangesAsComponent(YamlConfiguration conf, YamlConfiguration oldState);
}

View File

@ -0,0 +1,70 @@
package de.diddiz.LogBlock.blockstate;
import java.util.List;
import net.md_5.bungee.api.chat.BaseComponent;
import org.bukkit.DyeColor;
import org.bukkit.Material;
import org.bukkit.block.Banner;
import org.bukkit.block.BlockState;
import org.bukkit.block.banner.Pattern;
import org.bukkit.block.banner.PatternType;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
public class BlockStateCodecBanner implements BlockStateCodec {
@Override
public Material[] getApplicableMaterials() {
return new Material[] { Material.WHITE_BANNER, Material.ORANGE_BANNER, Material.MAGENTA_BANNER, Material.LIGHT_BLUE_BANNER, Material.YELLOW_BANNER, Material.LIME_BANNER, Material.PINK_BANNER, Material.GRAY_BANNER, Material.LIGHT_GRAY_BANNER, Material.CYAN_BANNER, Material.PURPLE_BANNER,
Material.BLUE_BANNER, Material.BROWN_BANNER, Material.GREEN_BANNER, Material.RED_BANNER, Material.BLACK_BANNER, Material.WHITE_WALL_BANNER, Material.ORANGE_WALL_BANNER, Material.MAGENTA_WALL_BANNER, Material.LIGHT_BLUE_WALL_BANNER, Material.YELLOW_WALL_BANNER,
Material.LIME_WALL_BANNER, Material.PINK_WALL_BANNER, Material.GRAY_WALL_BANNER, Material.LIGHT_GRAY_WALL_BANNER, Material.CYAN_WALL_BANNER, Material.PURPLE_WALL_BANNER, Material.BLUE_WALL_BANNER, Material.BROWN_WALL_BANNER, Material.GREEN_WALL_BANNER, Material.RED_WALL_BANNER,
Material.BLACK_WALL_BANNER };
}
@Override
public YamlConfiguration serialize(BlockState state) {
if (state instanceof Banner) {
Banner banner = (Banner) state;
int nr = 0;
List<Pattern> patterns = banner.getPatterns();
if (!patterns.isEmpty()) {
YamlConfiguration conf = new YamlConfiguration();
ConfigurationSection patternsSection = conf.createSection("patterns");
for (Pattern pattern : patterns) {
ConfigurationSection section = patternsSection.createSection(Integer.toString(nr));
section.set("color", pattern.getColor().name());
section.set("pattern", pattern.getPattern().name());
nr++;
}
return conf;
}
}
return null;
}
@Override
public void deserialize(BlockState state, YamlConfiguration conf) {
if (state instanceof Banner) {
Banner banner = (Banner) state;
int oldPatterns = banner.getPatterns().size();
for (int i = 0; i < oldPatterns; i++) {
banner.removePattern(0);
}
ConfigurationSection patternsSection = conf == null ? null : conf.getConfigurationSection("patterns");
if (patternsSection != null) {
for (String key : patternsSection.getKeys(false)) {
ConfigurationSection section = patternsSection.getConfigurationSection(key);
if (section != null) {
DyeColor color = DyeColor.valueOf(section.getString("color"));
PatternType type = PatternType.valueOf(section.getString("pattern"));
banner.addPattern(new Pattern(color, type));
}
}
}
}
}
@Override
public BaseComponent getChangesAsComponent(YamlConfiguration conf, YamlConfiguration oldState) {
return null;
}
}

View File

@ -0,0 +1,54 @@
package de.diddiz.LogBlock.blockstate;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Material;
import org.bukkit.block.BlockState;
import org.bukkit.block.Lectern;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;
public class BlockStateCodecLectern implements BlockStateCodec {
@Override
public Material[] getApplicableMaterials() {
return new Material[] { Material.LECTERN };
}
@Override
public YamlConfiguration serialize(BlockState state) {
if (state instanceof Lectern) {
Lectern lectern = (Lectern) state;
ItemStack book = lectern.getSnapshotInventory().getItem(0);
if (book != null && book.getType() != Material.AIR) {
YamlConfiguration conf = new YamlConfiguration();
conf.set("book", book);
return conf;
}
}
return null;
}
@Override
public void deserialize(BlockState state, YamlConfiguration conf) {
if (state instanceof Lectern) {
Lectern lectern = (Lectern) state;
ItemStack book = null;
if (conf != null) {
book = conf.getItemStack("book");
}
try {
lectern.getSnapshotInventory().setItem(0, book);
} catch (NullPointerException e) {
//ignored
}
}
}
@Override
public BaseComponent getChangesAsComponent(YamlConfiguration conf, YamlConfiguration oldState) {
if (conf != null) {
return new TextComponent("[book]");
}
return new TextComponent("empty");
}
}

View File

@ -0,0 +1,87 @@
package de.diddiz.LogBlock.blockstate;
import static de.diddiz.LogBlock.config.Config.getWorldConfig;
import de.diddiz.LogBlock.Logging;
import de.diddiz.LogBlock.config.WorldConfig;
import de.diddiz.LogBlock.util.BukkitUtils;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Material;
import org.bukkit.block.BlockState;
import org.bukkit.block.ShulkerBox;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;
public class BlockStateCodecShulkerBox implements BlockStateCodec {
@Override
public Material[] getApplicableMaterials() {
return BukkitUtils.getShulkerBoxBlocks().toArray(Material[]::new);
}
@Override
public YamlConfiguration serialize(BlockState state) {
WorldConfig wcfg = getWorldConfig(state.getWorld());
if (wcfg == null || !wcfg.isLogging(Logging.SHULKER_BOX_CONTENT)) {
return null;
}
if (state instanceof ShulkerBox) {
ShulkerBox shulkerBox = (ShulkerBox) state;
ItemStack[] content = shulkerBox.getSnapshotInventory().getStorageContents();
YamlConfiguration conf = new YamlConfiguration();
boolean anySlot = false;
for (int i = 0; i < content.length; i++) {
ItemStack stack = content[i];
if (stack != null && stack.getType() != Material.AIR) {
conf.set("slot" + i, stack);
anySlot = true;
}
}
if (anySlot) {
return conf;
}
}
return null;
}
@Override
public void deserialize(BlockState state, YamlConfiguration conf) {
if (state instanceof ShulkerBox) {
ShulkerBox shulkerBox = (ShulkerBox) state;
if (conf != null) {
ItemStack[] content = shulkerBox.getSnapshotInventory().getStorageContents();
for (int i = 0; i < content.length; i++) {
ItemStack stack = conf.getItemStack("slot" + i);
if (stack != null && stack.getType() != Material.AIR) {
content[i] = stack;
}
}
shulkerBox.getSnapshotInventory().setContents(content);
}
}
}
@Override
public BaseComponent getChangesAsComponent(YamlConfiguration conf, YamlConfiguration oldState) {
if (conf != null) {
StringBuilder sb = new StringBuilder();
sb.append("[");
boolean anySlot = false;
for (String key : conf.getKeys(false)) {
if (key.startsWith("slot")) {
ItemStack stack = conf.getItemStack(key);
if (stack != null && stack.getType() != Material.AIR) {
if (anySlot) {
sb.append(",");
}
anySlot = true;
sb.append(stack.getAmount()).append("x").append(stack.getType());
}
}
}
sb.append("]");
return anySlot ? new TextComponent(sb.toString()) : null;
}
return null;
}
}

View File

@ -0,0 +1,227 @@
package de.diddiz.LogBlock.blockstate;
import de.diddiz.LogBlock.util.BukkitUtils;
import java.awt.Color;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.DyeColor;
import org.bukkit.Material;
import org.bukkit.block.BlockState;
import org.bukkit.block.Sign;
import org.bukkit.block.sign.Side;
import org.bukkit.block.sign.SignSide;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
public class BlockStateCodecSign implements BlockStateCodec {
@Override
public Material[] getApplicableMaterials() {
return BukkitUtils.getAllSignMaterials().toArray(Material[]::new);
}
@Override
public YamlConfiguration serialize(BlockState state) {
YamlConfiguration conf = null;
if (state instanceof Sign sign) {
boolean waxed = sign.isWaxed();
if (waxed) {
conf = new YamlConfiguration();
conf.set("waxed", waxed);
}
for (Side side : Side.values()) {
SignSide signSide = sign.getSide(side);
String[] lines = signSide.getLines();
boolean hasText = false;
for (int i = 0; i < lines.length; i++) {
if (lines[i] != null && lines[i].length() > 0) {
hasText = true;
break;
}
}
DyeColor signColor = signSide.getColor();
if (signColor == null) {
signColor = DyeColor.BLACK;
}
boolean glowing = signSide.isGlowingText();
if (hasText || signColor != DyeColor.BLACK || glowing) {
if (conf == null) {
conf = new YamlConfiguration();
}
ConfigurationSection sideSection = side == Side.FRONT ? conf : conf.createSection(side.name().toLowerCase());
if (hasText) {
sideSection.set("lines", Arrays.asList(lines));
}
if (signColor != DyeColor.BLACK) {
sideSection.set("color", signColor.name());
}
if (glowing) {
sideSection.set("glowing", true);
}
}
}
}
return conf;
}
/**
* This is required for the SignChangeEvent, because we have no updated BlockState there.
* @param state
*/
public YamlConfiguration serialize(BlockState state, Side side, String[] lines) {
YamlConfiguration conf = state == null ? null : serialize(state);
if (lines != null) {
if (conf == null) {
conf = new YamlConfiguration();
}
ConfigurationSection sideSection = side == Side.FRONT ? conf : conf.getConfigurationSection(side.name().toLowerCase());
if (sideSection == null) {
sideSection = conf.createSection(side.name().toLowerCase());
}
sideSection.set("lines", Arrays.asList(lines));
}
return conf;
}
@Override
public void deserialize(BlockState state, YamlConfiguration conf) {
if (state instanceof Sign) {
Sign sign = (Sign) state;
if (conf != null) {
sign.setWaxed(conf.getBoolean("waxed"));
for (Side side : Side.values()) {
ConfigurationSection sideSection = side == Side.FRONT ? conf : conf.getConfigurationSection(side.name().toLowerCase());
DyeColor signColor = DyeColor.BLACK;
boolean glowing = false;
List<String> lines = Collections.emptyList();
if (sideSection != null) {
if (sideSection.contains("lines")) {
lines = sideSection.getStringList("lines");
}
if (sideSection.contains("color")) {
try {
signColor = DyeColor.valueOf(sideSection.getString("color"));
} catch (IllegalArgumentException | NullPointerException e) {
// ignored
}
}
glowing = sideSection.getBoolean("glowing", false);
}
SignSide signSide = sign.getSide(side);
for (int i = 0; i < 4; i++) {
String line = lines.size() > i && lines.get(i) != null ? lines.get(i) : "";
signSide.setLine(i, line);
}
signSide.setColor(signColor);
signSide.setGlowingText(glowing);
}
} else {
sign.setWaxed(false);
for (Side side : Side.values()) {
SignSide signSide = sign.getSide(side);
for (int i = 0; i < 4; i++) {
signSide.setLine(i, "");
}
signSide.setColor(DyeColor.BLACK);
signSide.setGlowingText(false);
}
}
}
}
@Override
public BaseComponent getChangesAsComponent(YamlConfiguration state, YamlConfiguration oldState) {
if (state != null) {
TextComponent tc = new TextComponent();
// StringBuilder sb = new StringBuilder();
boolean isWaxed = state.getBoolean("waxed");
boolean oldWaxed = oldState != null && oldState.getBoolean("waxed");
if (isWaxed != oldWaxed) {
tc.addExtra(isWaxed ? "(waxed)" : "(not waxed)");
}
for (Side side : Side.values()) {
boolean sideHeaderAdded = false;
ConfigurationSection sideSection = side == Side.FRONT ? state : state.getConfigurationSection(side.name().toLowerCase());
List<String> lines = sideSection == null ? Collections.emptyList() : sideSection.getStringList("lines");
List<String> oldLines = Collections.emptyList();
DyeColor signColor = DyeColor.BLACK;
if (sideSection != null && sideSection.contains("color")) {
try {
signColor = DyeColor.valueOf(sideSection.getString("color"));
} catch (IllegalArgumentException | NullPointerException e) {
// ignored
}
}
DyeColor oldSignColor = DyeColor.BLACK;
boolean glowing = sideSection != null && sideSection.getBoolean("glowing", false);
boolean oldGlowing = false;
if (oldState != null) {
ConfigurationSection oldSideSection = side == Side.FRONT ? oldState : oldState.getConfigurationSection(side.name().toLowerCase());
if (oldSideSection != null) {
oldLines = oldSideSection.getStringList("lines");
if (oldSideSection.contains("color")) {
try {
oldSignColor = DyeColor.valueOf(oldSideSection.getString("color"));
} catch (IllegalArgumentException | NullPointerException e) {
// ignored
}
}
oldGlowing = oldSideSection.getBoolean("glowing", false);
}
}
if (!lines.equals(oldLines)) {
sideHeaderAdded = addSideHeaderText(tc, side, sideHeaderAdded);
for (String line : lines) {
if (tc.getExtra() != null && !tc.getExtra().isEmpty()) {
tc.addExtra(" ");
}
tc.addExtra("[");
if (line != null && !line.isEmpty()) {
tc.addExtra(new TextComponent(TextComponent.fromLegacyText(line)));
}
tc.addExtra("]");
}
}
if (signColor != oldSignColor) {
sideHeaderAdded = addSideHeaderText(tc, side, sideHeaderAdded);
if (tc.getExtra() != null && !tc.getExtra().isEmpty()) {
tc.addExtra(" ");
}
tc.addExtra("(color: ");
TextComponent colorText = new TextComponent(signColor.name().toLowerCase());
colorText.setColor(ChatColor.of(new Color(signColor.getColor().asARGB())));
tc.addExtra(colorText);
tc.addExtra(")");
}
if (glowing != oldGlowing) {
sideHeaderAdded = addSideHeaderText(tc, side, sideHeaderAdded);
if (tc.getExtra() != null && !tc.getExtra().isEmpty()) {
tc.addExtra(" ");
}
if (glowing) {
tc.addExtra("(glowing)");
} else {
tc.addExtra("(not glowing)");
}
}
}
return tc;
}
return null;
}
private static boolean addSideHeaderText(TextComponent tc, Side side, boolean wasAdded) {
if (!wasAdded) {
if (tc.getExtra() != null && !tc.getExtra().isEmpty()) {
tc.addExtra(" ");
}
tc.addExtra(side.name() + ":");
}
return true;
}
}

View File

@ -0,0 +1,90 @@
package de.diddiz.LogBlock.blockstate;
import java.util.UUID;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.chat.hover.content.Text;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.block.BlockState;
import org.bukkit.block.Skull;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.profile.PlayerProfile;
public class BlockStateCodecSkull implements BlockStateCodec {
private static final boolean HAS_PROFILE_API;
static {
boolean hasProfileApi = false;
try {
Skull.class.getMethod("getOwnerProfile");
hasProfileApi = true;
} catch (NoSuchMethodException ignored) {
}
HAS_PROFILE_API = hasProfileApi;
}
@Override
public Material[] getApplicableMaterials() {
return new Material[] { Material.PLAYER_WALL_HEAD, Material.PLAYER_HEAD };
}
@Override
public YamlConfiguration serialize(BlockState state) {
if (state instanceof Skull) {
Skull skull = (Skull) state;
OfflinePlayer owner = skull.hasOwner() ? skull.getOwningPlayer() : null;
PlayerProfile profile = HAS_PROFILE_API ? skull.getOwnerProfile() : null;
if (owner != null || profile != null) {
YamlConfiguration conf = new YamlConfiguration();
if (profile != null) {
conf.set("profile", profile);
} else if (owner != null) {
conf.set("owner", owner.getUniqueId().toString());
}
return conf;
}
}
return null;
}
@Override
public void deserialize(BlockState state, YamlConfiguration conf) {
if (state instanceof Skull) {
Skull skull = (Skull) state;
PlayerProfile profile = conf == null || !HAS_PROFILE_API ? null : (PlayerProfile) conf.get("profile");
if (profile != null) {
skull.setOwnerProfile(profile);
} else {
UUID ownerId = conf == null ? null : UUID.fromString(conf.getString("owner"));
if (ownerId == null) {
skull.setOwningPlayer(null);
} else {
skull.setOwningPlayer(Bukkit.getOfflinePlayer(ownerId));
}
}
}
}
@Override
public BaseComponent getChangesAsComponent(YamlConfiguration conf, YamlConfiguration oldState) {
if (HAS_PROFILE_API && conf != null) {
PlayerProfile profile = (PlayerProfile) conf.get("profile");
if (profile != null) {
TextComponent tc = new TextComponent("[" + (profile.getName() != null ? profile.getName() : (profile.getUniqueId() != null ? profile.getUniqueId().toString() : "~unknown~")) + "]");
if (profile.getName() != null && profile.getUniqueId() != null) {
tc.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text("UUID: " + profile.getUniqueId().toString())));
}
return tc;
}
}
String ownerIdString = conf == null ? null : conf.getString("owner");
UUID ownerId = ownerIdString == null ? null : UUID.fromString(ownerIdString);
if (ownerId != null) {
OfflinePlayer owner = Bukkit.getOfflinePlayer(ownerId);
return new TextComponent("[" + (owner.getName() != null ? owner.getName() : owner.getUniqueId().toString()) + "]");
}
return null;
}
}

View File

@ -0,0 +1,74 @@
package de.diddiz.LogBlock.blockstate;
import de.diddiz.LogBlock.LogBlock;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Material;
import org.bukkit.block.BlockState;
import org.bukkit.block.CreatureSpawner;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.EntityType;
public class BlockStateCodecSpawner implements BlockStateCodec {
@Override
public Material[] getApplicableMaterials() {
return new Material[] { Material.SPAWNER };
}
@Override
public YamlConfiguration serialize(BlockState state) {
if (state instanceof CreatureSpawner) {
CreatureSpawner spawner = (CreatureSpawner) state;
YamlConfiguration conf = new YamlConfiguration();
conf.set("delay", spawner.getDelay());
conf.set("maxNearbyEntities", spawner.getMaxNearbyEntities());
conf.set("maxSpawnDelay", spawner.getMaxSpawnDelay());
conf.set("minSpawnDelay", spawner.getMinSpawnDelay());
conf.set("requiredPlayerRange", spawner.getRequiredPlayerRange());
conf.set("spawnCount", spawner.getSpawnCount());
if (spawner.getSpawnedType() != null) {
conf.set("spawnedType", spawner.getSpawnedType().name());
}
conf.set("spawnRange", spawner.getSpawnRange());
return conf;
}
return null;
}
@Override
public void deserialize(BlockState state, YamlConfiguration conf) {
if (state instanceof CreatureSpawner) {
CreatureSpawner spawner = (CreatureSpawner) state;
if (conf != null) {
spawner.setDelay(conf.getInt("delay"));
spawner.setMaxNearbyEntities(conf.getInt("maxNearbyEntities"));
spawner.setMaxSpawnDelay(conf.getInt("maxSpawnDelay"));
spawner.setMinSpawnDelay(conf.getInt("minSpawnDelay"));
spawner.setRequiredPlayerRange(conf.getInt("requiredPlayerRange"));
spawner.setSpawnCount(conf.getInt("spawnCount"));
EntityType spawnedType = null;
String spawnedTypeString = conf.getString("spawnedType");
if (spawnedTypeString != null) {
try {
spawnedType = EntityType.valueOf(spawnedTypeString);
} catch (IllegalArgumentException ignored) {
LogBlock.getInstance().getLogger().warning("Could not find spawner spawned type: " + spawnedTypeString);
}
}
spawner.setSpawnedType(spawnedType);
spawner.setSpawnRange(conf.getInt("spawnRange"));
}
}
}
@Override
public BaseComponent getChangesAsComponent(YamlConfiguration conf, YamlConfiguration oldState) {
if (conf != null) {
EntityType entity = EntityType.valueOf(conf.getString("spawnedType"));
if (entity != null) {
return new TextComponent("[" + entity.getKey().getKey() + "]");
}
}
return null;
}
}

View File

@ -0,0 +1,61 @@
package de.diddiz.LogBlock.blockstate;
import java.util.HashMap;
import java.util.Map;
import net.md_5.bungee.api.chat.BaseComponent;
import org.bukkit.Material;
import org.bukkit.block.BlockState;
import org.bukkit.configuration.file.YamlConfiguration;
public class BlockStateCodecs {
private static Map<Material, BlockStateCodec> codecs = new HashMap<>();
public static void registerCodec(BlockStateCodec codec) {
Material[] materials = codec.getApplicableMaterials();
for (Material material : materials) {
if (codecs.containsKey(material)) {
throw new IllegalArgumentException("BlockStateCodec for " + material + " already registered!");
}
codecs.put(material, codec);
}
}
static {
registerCodec(new BlockStateCodecSign());
registerCodec(new BlockStateCodecSkull());
registerCodec(new BlockStateCodecBanner());
registerCodec(new BlockStateCodecSpawner());
registerCodec(new BlockStateCodecLectern());
registerCodec(new BlockStateCodecShulkerBox());
}
public static boolean hasCodec(Material material) {
return codecs.containsKey(material);
}
public static YamlConfiguration serialize(BlockState state) {
BlockStateCodec codec = codecs.get(state.getType());
if (codec != null) {
YamlConfiguration serialized = codec.serialize(state);
if (serialized != null && !serialized.getKeys(false).isEmpty()) {
return serialized;
}
}
return null;
}
public static void deserialize(BlockState block, YamlConfiguration state) {
BlockStateCodec codec = codecs.get(block.getType());
if (codec != null) {
codec.deserialize(block, state);
}
}
public static BaseComponent getChangesAsComponent(Material material, YamlConfiguration state, YamlConfiguration oldState) {
BlockStateCodec codec = codecs.get(material);
if (codec != null) {
return codec.getChangesAsComponent(state, oldState);
}
return null;
}
}

View File

@ -1,293 +1,383 @@
package de.diddiz.LogBlock.config; package de.diddiz.LogBlock.config;
import de.diddiz.LogBlock.*; import de.diddiz.LogBlock.*;
import org.bukkit.Material; import de.diddiz.LogBlock.util.ComparableVersion;
import org.bukkit.World; import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.World;
import org.bukkit.permissions.PermissionDefault; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Entity;
import java.io.File; import org.bukkit.permissions.PermissionDefault;
import java.io.IOException;
import java.text.SimpleDateFormat; import java.io.File;
import java.util.*; import java.io.IOException;
import java.util.Map.Entry; import java.text.SimpleDateFormat;
import java.util.logging.Level; import java.util.*;
import java.util.zip.DataFormatException; import java.util.Map.Entry;
import java.util.logging.Level;
import static de.diddiz.util.BukkitUtils.friendlyWorldname; import java.util.zip.DataFormatException;
import static de.diddiz.util.Utils.parseTimeSpec;
import static org.bukkit.Bukkit.*; import static de.diddiz.LogBlock.util.BukkitUtils.friendlyWorldname;
import static de.diddiz.LogBlock.util.Utils.parseTimeSpec;
public class Config { import static org.bukkit.Bukkit.*;
private static LoggingEnabledMapping superWorldConfig;
private static Map<String, WorldConfig> worldConfigs; public class Config {
public static String url, user, password; private static LoggingEnabledMapping superWorldConfig;
public static int delayBetweenRuns, forceToProcessAtLeast, timePerRun; private static Map<String, WorldConfig> worldConfigs;
public static boolean fireCustomEvents; public static String url, user, password;
public static boolean useBukkitScheduler; public static String mysqlDatabase;
public static int queueWarningSize; public static boolean mysqlUseSSL;
public static boolean enableAutoClearLog; public static boolean mysqlRequireSSL;
public static List<String> autoClearLog; public static int delayBetweenRuns, forceToProcessAtLeast, timePerRun;
public static int autoClearLogDelay; public static boolean useBukkitScheduler;
public static boolean dumpDeletedLog; public static int queueWarningSize;
public static boolean logCreeperExplosionsAsPlayerWhoTriggeredThese, logPlayerInfo; public static boolean enableAutoClearLog;
public static LogKillsLevel logKillsLevel; public static List<String> autoClearLog;
public static Set<Integer> dontRollback, replaceAnyway; public static int autoClearLogDelay;
public static int rollbackMaxTime, rollbackMaxArea; public static boolean dumpDeletedLog;
public static Map<String, Tool> toolsByName; public static boolean logBedExplosionsAsPlayerWhoTriggeredThese;
public static Map<Integer, Tool> toolsByType; public static boolean logCreeperExplosionsAsPlayerWhoTriggeredThese, logPlayerInfo;
public static int defaultDist, defaultTime; public static boolean logFireSpreadAsPlayerWhoCreatedIt;
public static int linesPerPage, linesLimit; public static boolean logFluidFlowAsPlayerWhoTriggeredIt;
public static boolean askRollbacks, askRedos, askClearLogs, askClearLogAfterRollback, askRollbackAfterBan; public static LogKillsLevel logKillsLevel;
public static String banPermission; public static Set<Material> dontRollback, replaceAnyway;
public static Set<Integer> hiddenBlocks; public static int rollbackMaxTime, rollbackMaxArea;
public static Set<String> hiddenPlayers; public static Map<String, Tool> toolsByName;
public static Set<String> ignoredChat; public static Map<Material, Tool> toolsByType;
public static SimpleDateFormat formatter; public static int defaultDist, defaultTime;
public static boolean safetyIdCheck; public static int linesPerPage, linesLimit, hardLinesLimit;
public static boolean debug; public static boolean askRollbacks, askRedos, askClearLogs, askClearLogAfterRollback, askRollbackAfterBan;
public static boolean logEnvironmentalKills; public static String banPermission;
// Not loaded from config - checked at runtime public static Set<Material> hiddenBlocks;
public static boolean mb4 = false; public static Set<String> hiddenPlayers;
public static List<String> ignoredChat;
public static enum LogKillsLevel { public static SimpleDateFormat formatter;
PLAYERS, MONSTERS, ANIMALS; public static SimpleDateFormat formatterShort;
} public static boolean debug;
public static boolean logEnvironmentalKills;
public static void load(LogBlock logblock) throws DataFormatException, IOException { // addons
final ConfigurationSection config = logblock.getConfig(); public static boolean worldGuardLoggingFlags;
final Map<String, Object> def = new HashMap<String, Object>(); // Not loaded from config - checked at runtime
def.put("version", logblock.getDescription().getVersion()); public static boolean mb4 = false;
final List<String> worldNames = new ArrayList<String>();
for (final World world : getWorlds()) { public static final String CURRENT_CONFIG_VERSION = "1.19.0";
worldNames.add(world.getName());
} public static enum LogKillsLevel {
if (worldNames.isEmpty()) { PLAYERS,
worldNames.add("world"); MONSTERS,
worldNames.add("world_nether"); ANIMALS;
worldNames.add("world_the_end"); }
}
def.put("loggedWorlds", worldNames); public static void load(LogBlock logblock) throws DataFormatException, IOException {
def.put("mysql.host", "localhost"); final ConfigurationSection config = logblock.getConfig();
def.put("mysql.port", 3306); final Map<String, Object> def = new HashMap<>();
def.put("mysql.database", "minecraft"); def.put("version", CURRENT_CONFIG_VERSION);
def.put("mysql.user", "username"); final List<String> worldNames = new ArrayList<>();
def.put("mysql.password", "pass"); for (final World world : getWorlds()) {
def.put("consumer.delayBetweenRuns", 2); worldNames.add(world.getName());
def.put("consumer.forceToProcessAtLeast", 200); }
def.put("consumer.timePerRun", 1000); if (worldNames.isEmpty()) {
def.put("consumer.fireCustomEvents", false); worldNames.add("world");
def.put("consumer.useBukkitScheduler", true); worldNames.add("world_nether");
def.put("consumer.queueWarningSize", 1000); worldNames.add("world_the_end");
def.put("clearlog.dumpDeletedLog", false); }
def.put("clearlog.enableAutoClearLog", false); def.put("loggedWorlds", worldNames);
def.put("clearlog.auto", Arrays.asList("world \"world\" before 365 days all", "world \"world\" player lavaflow waterflow leavesdecay before 7 days all", "world world_nether before 365 days all", "world world_nether player lavaflow before 7 days all")); def.put("mysql.protocol", "mysql");
def.put("clearlog.autoClearLogDelay", "6h"); def.put("mysql.host", "localhost");
def.put("logging.logCreeperExplosionsAsPlayerWhoTriggeredThese", false); def.put("mysql.port", 3306);
def.put("logging.logKillsLevel", "PLAYERS"); def.put("mysql.database", "minecraft");
def.put("logging.logEnvironmentalKills", false); def.put("mysql.user", "username");
def.put("logging.logPlayerInfo", false); def.put("mysql.password", "pass");
def.put("logging.hiddenPlayers", new ArrayList<String>()); def.put("mysql.useSSL", true);
def.put("logging.hiddenBlocks", Arrays.asList(0)); def.put("mysql.requireSSL", false);
def.put("logging.ignoredChat", Arrays.asList("/register", "/login")); def.put("consumer.delayBetweenRuns", 2);
def.put("rollback.dontRollback", Arrays.asList(10, 11, 46, 51)); def.put("consumer.forceToProcessAtLeast", 200);
def.put("rollback.replaceAnyway", Arrays.asList(8, 9, 10, 11, 51)); def.put("consumer.timePerRun", 1000);
def.put("rollback.maxTime", "2 days"); def.put("consumer.useBukkitScheduler", true);
def.put("rollback.maxArea", 50); def.put("consumer.queueWarningSize", 1000);
def.put("lookup.defaultDist", 20); def.put("clearlog.dumpDeletedLog", false);
def.put("lookup.defaultTime", "30 minutes"); def.put("clearlog.enableAutoClearLog", false);
def.put("lookup.linesPerPage", 15); final List<String> autoClearlog = new ArrayList<>();
def.put("lookup.linesLimit", 1500); for (final String world : worldNames) {
try { autoClearlog.add("world \"" + world + "\" before 365 days all");
formatter = new SimpleDateFormat(config.getString("lookup.dateFormat", "MM-dd HH:mm:ss")); autoClearlog.add("world \"" + world + "\" player lavaflow waterflow leavesdecay before 7 days all");
} catch (IllegalArgumentException e) { autoClearlog.add("world \"" + world + "\" entities before 365 days");
throw new DataFormatException("Invalid specification for date format, please see http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html : " + e.getMessage()); }
} def.put("clearlog.auto", autoClearlog);
def.put("lookup.dateFormat", "MM-dd HH:mm:ss"); def.put("clearlog.autoClearLogDelay", "6h");
def.put("questioner.askRollbacks", true); def.put("logging.logBedExplosionsAsPlayerWhoTriggeredThese", true);
def.put("questioner.askRedos", true); def.put("logging.logCreeperExplosionsAsPlayerWhoTriggeredThese", false);
def.put("questioner.askClearLogs", true); def.put("logging.logFireSpreadAsPlayerWhoCreatedIt", true);
def.put("questioner.askClearLogAfterRollback", true); def.put("logging.logFluidFlowAsPlayerWhoTriggeredIt", false);
def.put("questioner.askRollbackAfterBan", false); def.put("logging.logKillsLevel", "PLAYERS");
def.put("questioner.banPermission", "mcbans.ban.local"); def.put("logging.logEnvironmentalKills", false);
def.put("tools.tool.aliases", Arrays.asList("t")); def.put("logging.logPlayerInfo", false);
def.put("tools.tool.leftClickBehavior", "NONE"); def.put("logging.hiddenPlayers", new ArrayList<String>());
def.put("tools.tool.rightClickBehavior", "TOOL"); def.put("logging.hiddenBlocks", Arrays.asList(Material.AIR.name(), Material.CAVE_AIR.name(), Material.VOID_AIR.name()));
def.put("tools.tool.defaultEnabled", true); def.put("logging.ignoredChat", Arrays.asList("/register", "/login"));
def.put("tools.tool.item", 270); def.put("rollback.dontRollback", Arrays.asList(Material.LAVA.name(), Material.TNT.name(), Material.FIRE.name()));
def.put("tools.tool.canDrop", true); def.put("rollback.replaceAnyway", Arrays.asList(Material.LAVA.name(), Material.WATER.name(), Material.FIRE.name(), Material.GRASS_BLOCK.name()));
def.put("tools.tool.params", "area 0 all sum none limit 15 desc silent"); def.put("rollback.maxTime", "2 days");
def.put("tools.tool.mode", "LOOKUP"); def.put("rollback.maxArea", 50);
def.put("tools.tool.permissionDefault", "OP"); def.put("lookup.defaultDist", 20);
def.put("tools.toolblock.aliases", Arrays.asList("tb")); def.put("lookup.defaultTime", "30 minutes");
def.put("tools.toolblock.leftClickBehavior", "TOOL"); def.put("lookup.linesPerPage", 15);
def.put("tools.toolblock.rightClickBehavior", "BLOCK"); def.put("lookup.linesLimit", 1500);
def.put("tools.toolblock.defaultEnabled", true); def.put("lookup.hardLinesLimit", 100000);
def.put("tools.toolblock.item", 7); try {
def.put("tools.toolblock.canDrop", false); formatter = new SimpleDateFormat(config.getString("lookup.dateFormat", "yyyy-MM-dd HH:mm:ss"));
def.put("tools.toolblock.params", "area 0 all sum none limit 15 desc silent"); } catch (IllegalArgumentException e) {
def.put("tools.toolblock.mode", "LOOKUP"); throw new DataFormatException("Invalid specification for date format, please see http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html : " + e.getMessage());
def.put("tools.toolblock.permissionDefault", "OP"); }
def.put("safety.id.check", true); def.put("lookup.dateFormat", "yyyy-MM-dd HH:mm:ss");
def.put("debug", false); try {
for (final Entry<String, Object> e : def.entrySet()) { formatterShort = new SimpleDateFormat(config.getString("lookup.dateFormatShort", "MM-dd HH:mm"));
if (!config.contains(e.getKey())) { } catch (IllegalArgumentException e) {
config.set(e.getKey(), e.getValue()); throw new DataFormatException("Invalid specification for date format, please see http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html : " + e.getMessage());
} }
} def.put("lookup.dateFormatShort", "MM-dd HH:mm");
logblock.saveConfig(); def.put("questioner.askRollbacks", true);
url = "jdbc:mysql://" + config.getString("mysql.host") + ":" + config.getInt("mysql.port") + "/" + getStringIncludingInts(config, "mysql.database"); def.put("questioner.askRedos", true);
user = getStringIncludingInts(config, "mysql.user"); def.put("questioner.askClearLogs", true);
password = getStringIncludingInts(config, "mysql.password"); def.put("questioner.askClearLogAfterRollback", true);
delayBetweenRuns = config.getInt("consumer.delayBetweenRuns", 2); def.put("questioner.askRollbackAfterBan", false);
forceToProcessAtLeast = config.getInt("consumer.forceToProcessAtLeast", 0); def.put("questioner.banPermission", "mcbans.ban.local");
timePerRun = config.getInt("consumer.timePerRun", 1000); def.put("tools.tool.aliases", Arrays.asList("t"));
fireCustomEvents = config.getBoolean("consumer.fireCustomEvents", false); def.put("tools.tool.leftClickBehavior", "NONE");
useBukkitScheduler = config.getBoolean("consumer.useBukkitScheduler", true); def.put("tools.tool.rightClickBehavior", "TOOL");
queueWarningSize = config.getInt("consumer.queueWarningSize", 1000); def.put("tools.tool.defaultEnabled", true);
enableAutoClearLog = config.getBoolean("clearlog.enableAutoClearLog"); def.put("tools.tool.item", Material.WOODEN_PICKAXE.name());
autoClearLog = config.getStringList("clearlog.auto"); def.put("tools.tool.canDrop", true);
dumpDeletedLog = config.getBoolean("clearlog.dumpDeletedLog", false); def.put("tools.tool.removeOnDisable", true);
autoClearLogDelay = parseTimeSpec(config.getString("clearlog.autoClearLogDelay").split(" ")); def.put("tools.tool.dropToDisable", false);
logCreeperExplosionsAsPlayerWhoTriggeredThese = config.getBoolean("logging.logCreeperExplosionsAsPlayerWhoTriggeredThese", false); def.put("tools.tool.params", "area 0 all sum none limit 15 desc since 60d silent");
logPlayerInfo = config.getBoolean("logging.logPlayerInfo", true); def.put("tools.tool.mode", "LOOKUP");
try { def.put("tools.tool.permissionDefault", "OP");
logKillsLevel = LogKillsLevel.valueOf(config.getString("logging.logKillsLevel").toUpperCase()); def.put("tools.toolblock.aliases", Arrays.asList("tb"));
} catch (final IllegalArgumentException ex) { def.put("tools.toolblock.leftClickBehavior", "TOOL");
throw new DataFormatException("logging.logKillsLevel doesn't appear to be a valid log level. Allowed are 'PLAYERS', 'MONSTERS' and 'ANIMALS'"); def.put("tools.toolblock.rightClickBehavior", "BLOCK");
} def.put("tools.toolblock.defaultEnabled", true);
logEnvironmentalKills = config.getBoolean("logging.logEnvironmentalKills", false); def.put("tools.toolblock.item", Material.BEDROCK.name());
hiddenPlayers = new HashSet<String>(); def.put("tools.toolblock.canDrop", false);
for (final String playerName : config.getStringList("logging.hiddenPlayers")) { def.put("tools.toolblock.removeOnDisable", true);
hiddenPlayers.add(playerName.toLowerCase().trim()); def.put("tools.toolblock.dropToDisable", false);
} def.put("tools.toolblock.params", "area 0 all sum none limit 15 desc since 60d silent");
hiddenBlocks = new HashSet<Integer>(); def.put("tools.toolblock.mode", "LOOKUP");
for (final Object blocktype : config.getList("logging.hiddenBlocks")) { def.put("tools.toolblock.permissionDefault", "OP");
final Material mat = Material.matchMaterial(String.valueOf(blocktype)); def.put("safety.id.check", true);
if (mat != null) { def.put("addons.worldguardLoggingFlags", false);
hiddenBlocks.add(mat.getId()); def.put("debug", false);
} else { for (final Entry<String, Object> e : def.entrySet()) {
throw new DataFormatException("Not a valid material: '" + blocktype + "'"); if (!config.contains(e.getKey())) {
} config.set(e.getKey(), e.getValue());
} }
ignoredChat = new HashSet<String>(); }
for (String chatCommand : config.getStringList("logging.ignoredChat")) { if (config.contains("consumer.fireCustomEvents")) {
ignoredChat.add(chatCommand); config.set("consumer.fireCustomEvents", null);
} }
dontRollback = new HashSet<Integer>(config.getIntegerList("rollback.dontRollback")); logblock.saveConfig();
replaceAnyway = new HashSet<Integer>(config.getIntegerList("rollback.replaceAnyway"));
rollbackMaxTime = parseTimeSpec(config.getString("rollback.maxTime").split(" ")); ComparableVersion configVersion = new ComparableVersion(config.getString("version"));
rollbackMaxArea = config.getInt("rollback.maxArea", 50); boolean oldConfig = configVersion.compareTo(new ComparableVersion(CURRENT_CONFIG_VERSION)) < 0;
defaultDist = config.getInt("lookup.defaultDist", 20);
defaultTime = parseTimeSpec(config.getString("lookup.defaultTime").split(" ")); mysqlDatabase = getStringIncludingInts(config, "mysql.database");
linesPerPage = config.getInt("lookup.linesPerPage", 15); url = "jdbc:" + config.getString("mysql.protocol") + "://" + config.getString("mysql.host") + ":" + config.getInt("mysql.port") + "/" + mysqlDatabase;
linesLimit = config.getInt("lookup.linesLimit", 1500); user = getStringIncludingInts(config, "mysql.user");
askRollbacks = config.getBoolean("questioner.askRollbacks", true); password = getStringIncludingInts(config, "mysql.password");
askRedos = config.getBoolean("questioner.askRedos", true); mysqlUseSSL = config.getBoolean("mysql.useSSL", true);
askClearLogs = config.getBoolean("questioner.askClearLogs", true); mysqlRequireSSL = config.getBoolean("mysql.requireSSL", false);
askClearLogAfterRollback = config.getBoolean("questioner.askClearLogAfterRollback", true); delayBetweenRuns = config.getInt("consumer.delayBetweenRuns", 2);
askRollbackAfterBan = config.getBoolean("questioner.askRollbackAfterBan", false); forceToProcessAtLeast = config.getInt("consumer.forceToProcessAtLeast", 0);
safetyIdCheck = config.getBoolean("safety.id.check", true); timePerRun = config.getInt("consumer.timePerRun", 1000);
debug = config.getBoolean("debug", false); useBukkitScheduler = config.getBoolean("consumer.useBukkitScheduler", true);
banPermission = config.getString("questioner.banPermission"); queueWarningSize = config.getInt("consumer.queueWarningSize", 1000);
final List<Tool> tools = new ArrayList<Tool>(); enableAutoClearLog = config.getBoolean("clearlog.enableAutoClearLog");
final ConfigurationSection toolsSec = config.getConfigurationSection("tools"); autoClearLog = config.getStringList("clearlog.auto");
for (final String toolName : toolsSec.getKeys(false)) { dumpDeletedLog = config.getBoolean("clearlog.dumpDeletedLog", false);
try { autoClearLogDelay = parseTimeSpec(config.getString("clearlog.autoClearLogDelay").split(" "));
final ConfigurationSection tSec = toolsSec.getConfigurationSection(toolName); logBedExplosionsAsPlayerWhoTriggeredThese = config.getBoolean("logging.logBedExplosionsAsPlayerWhoTriggeredThese", true);
final List<String> aliases = tSec.getStringList("aliases"); logCreeperExplosionsAsPlayerWhoTriggeredThese = config.getBoolean("logging.logCreeperExplosionsAsPlayerWhoTriggeredThese", false);
final ToolBehavior leftClickBehavior = ToolBehavior.valueOf(tSec.getString("leftClickBehavior").toUpperCase()); logFireSpreadAsPlayerWhoCreatedIt = config.getBoolean("logging.logFireSpreadAsPlayerWhoCreatedIt", true);
final ToolBehavior rightClickBehavior = ToolBehavior.valueOf(tSec.getString("rightClickBehavior").toUpperCase()); logFluidFlowAsPlayerWhoTriggeredIt = config.getBoolean("logging.logFluidFlowAsPlayerWhoTriggeredIt", false);
final boolean defaultEnabled = tSec.getBoolean("defaultEnabled", false); logPlayerInfo = config.getBoolean("logging.logPlayerInfo", true);
final int item = tSec.getInt("item", 0); try {
final boolean canDrop = tSec.getBoolean("canDrop", false); logKillsLevel = LogKillsLevel.valueOf(config.getString("logging.logKillsLevel").toUpperCase());
final QueryParams params = new QueryParams(logblock); } catch (final IllegalArgumentException ex) {
params.prepareToolQuery = true; throw new DataFormatException("logging.logKillsLevel doesn't appear to be a valid log level. Allowed are 'PLAYERS', 'MONSTERS' and 'ANIMALS'");
params.parseArgs(getConsoleSender(), Arrays.asList(tSec.getString("params").split(" "))); }
final ToolMode mode = ToolMode.valueOf(tSec.getString("mode").toUpperCase()); logEnvironmentalKills = config.getBoolean("logging.logEnvironmentalKills", false);
final PermissionDefault pdef = PermissionDefault.valueOf(tSec.getString("permissionDefault").toUpperCase()); hiddenPlayers = new HashSet<>();
tools.add(new Tool(toolName, aliases, leftClickBehavior, rightClickBehavior, defaultEnabled, item, canDrop, params, mode, pdef)); for (final String playerName : config.getStringList("logging.hiddenPlayers")) {
} catch (final Exception ex) { hiddenPlayers.add(playerName.toLowerCase().trim());
getLogger().log(Level.WARNING, "Error at parsing tool '" + toolName + "': ", ex); }
} hiddenBlocks = new HashSet<>();
} for (final String blocktype : config.getStringList("logging.hiddenBlocks")) {
toolsByName = new HashMap<String, Tool>(); final Material mat = Material.matchMaterial(blocktype);
toolsByType = new HashMap<Integer, Tool>(); if (mat != null) {
for (final Tool tool : tools) { hiddenBlocks.add(mat);
toolsByType.put(tool.item, tool); } else if (!oldConfig) {
toolsByName.put(tool.name.toLowerCase(), tool); throw new DataFormatException("Not a valid material in hiddenBlocks: '" + blocktype + "'");
for (final String alias : tool.aliases) { }
toolsByName.put(alias, tool); }
} ignoredChat = new ArrayList<>();
} for (String chatCommand : config.getStringList("logging.ignoredChat")) {
final List<String> loggedWorlds = config.getStringList("loggedWorlds"); ignoredChat.add(chatCommand.toLowerCase());
worldConfigs = new HashMap<String, WorldConfig>(); }
if (loggedWorlds.isEmpty()) { dontRollback = new HashSet<>();
throw new DataFormatException("No worlds configured"); for (String e : config.getStringList("rollback.dontRollback")) {
} Material mat = Material.matchMaterial(e);
for (final String world : loggedWorlds) { if (mat != null) {
worldConfigs.put(world, new WorldConfig(new File(logblock.getDataFolder(), friendlyWorldname(world) + ".yml"))); dontRollback.add(mat);
} } else if (!oldConfig) {
superWorldConfig = new LoggingEnabledMapping(); throw new DataFormatException("Not a valid material in dontRollback: '" + e + "'");
for (final WorldConfig wcfg : worldConfigs.values()) { }
for (final Logging l : Logging.values()) { }
if (wcfg.isLogging(l)) { replaceAnyway = new HashSet<>();
superWorldConfig.setLogging(l, true); for (String e : config.getStringList("rollback.replaceAnyway")) {
} Material mat = Material.matchMaterial(e);
} if (mat != null) {
} replaceAnyway.add(mat);
} } else if (!oldConfig) {
throw new DataFormatException("Not a valid material in replaceAnyway: '" + e + "'");
private static String getStringIncludingInts(ConfigurationSection cfg, String key) { }
String str = cfg.getString(key); }
if (str == null) { rollbackMaxTime = parseTimeSpec(config.getString("rollback.maxTime").split(" "));
str = String.valueOf(cfg.getInt(key)); rollbackMaxArea = config.getInt("rollback.maxArea", 50);
} defaultDist = config.getInt("lookup.defaultDist", 20);
if (str == null) { defaultTime = parseTimeSpec(config.getString("lookup.defaultTime").split(" "));
str = "No value set for '" + key + "'"; linesPerPage = config.getInt("lookup.linesPerPage", 15);
} linesLimit = config.getInt("lookup.linesLimit", 1500);
return str; hardLinesLimit = config.getInt("lookup.hardLinesLimit", 100000);
} askRollbacks = config.getBoolean("questioner.askRollbacks", true);
askRedos = config.getBoolean("questioner.askRedos", true);
public static boolean isLogging(World world, Logging l) { askClearLogs = config.getBoolean("questioner.askClearLogs", true);
final WorldConfig wcfg = worldConfigs.get(world.getName()); askClearLogAfterRollback = config.getBoolean("questioner.askClearLogAfterRollback", true);
return wcfg != null && wcfg.isLogging(l); askRollbackAfterBan = config.getBoolean("questioner.askRollbackAfterBan", false);
} debug = config.getBoolean("debug", false);
banPermission = config.getString("questioner.banPermission");
public static boolean isLogging(String worldName, Logging l) { final List<Tool> tools = new ArrayList<>();
final WorldConfig wcfg = worldConfigs.get(worldName); final ConfigurationSection toolsSec = config.getConfigurationSection("tools");
return wcfg != null && wcfg.isLogging(l); for (final String toolName : toolsSec.getKeys(false)) {
} try {
final ConfigurationSection tSec = toolsSec.getConfigurationSection(toolName);
public static boolean isLogged(World world) { final List<String> aliases = tSec.getStringList("aliases");
return worldConfigs.containsKey(world.getName()); final ToolBehavior leftClickBehavior = ToolBehavior.valueOf(tSec.getString("leftClickBehavior").toUpperCase());
} final ToolBehavior rightClickBehavior = ToolBehavior.valueOf(tSec.getString("rightClickBehavior").toUpperCase());
final boolean defaultEnabled = tSec.getBoolean("defaultEnabled", false);
public static WorldConfig getWorldConfig(World world) { final Material item = Material.matchMaterial(tSec.getString("item", "OAK_LOG"));
return worldConfigs.get(world.getName()); final boolean canDrop = tSec.getBoolean("canDrop", false);
} final boolean removeOnDisable = tSec.getBoolean("removeOnDisable", true);
final boolean dropToDisable = tSec.getBoolean("dropToDisable", false);
public static WorldConfig getWorldConfig(String world) { final QueryParams params = new QueryParams(logblock);
return worldConfigs.get(world); params.prepareToolQuery = true;
} params.parseArgs(getConsoleSender(), Arrays.asList(tSec.getString("params").split(" ")), false);
final ToolMode mode = ToolMode.valueOf(tSec.getString("mode").toUpperCase());
public static boolean isLogging(Logging l) { final PermissionDefault pdef = PermissionDefault.valueOf(tSec.getString("permissionDefault").toUpperCase());
return superWorldConfig.isLogging(l); tools.add(new Tool(toolName, aliases, leftClickBehavior, rightClickBehavior, defaultEnabled, item, canDrop, params, mode, pdef, removeOnDisable, dropToDisable));
} } catch (final Exception ex) {
getLogger().log(Level.WARNING, "Error at parsing tool '" + toolName + "': ", ex);
public static Collection<WorldConfig> getLoggedWorlds() { }
return worldConfigs.values(); }
} toolsByName = new HashMap<>();
} toolsByType = new HashMap<>();
for (final Tool tool : tools) {
class LoggingEnabledMapping { toolsByType.put(tool.item, tool);
private final boolean[] logging = new boolean[Logging.length]; toolsByName.put(tool.name.toLowerCase(), tool);
for (final String alias : tool.aliases) {
public void setLogging(Logging l, boolean enabled) { toolsByName.put(alias, tool);
logging[l.ordinal()] = enabled; }
} }
worldGuardLoggingFlags = config.getBoolean("addons.worldguardLoggingFlags");
public boolean isLogging(Logging l) { final List<String> loggedWorlds = config.getStringList("loggedWorlds");
return logging[l.ordinal()]; worldConfigs = new HashMap<>();
} if (loggedWorlds.isEmpty()) {
} throw new DataFormatException("No worlds configured");
}
for (final String world : loggedWorlds) {
worldConfigs.put(world, new WorldConfig(world, new File(logblock.getDataFolder(), friendlyWorldname(world) + ".yml")));
}
superWorldConfig = new LoggingEnabledMapping();
for (final WorldConfig wcfg : worldConfigs.values()) {
for (final Logging l : Logging.values()) {
if (wcfg.isLogging(l)) {
superWorldConfig.setLogging(l, true);
}
}
}
}
private static String getStringIncludingInts(ConfigurationSection cfg, String key) {
String str = cfg.getString(key);
if (str == null) {
str = String.valueOf(cfg.getInt(key));
}
if (str == null) {
str = "No value set for '" + key + "'";
}
return str;
}
public static boolean isLogging(World world, Logging l) {
final WorldConfig wcfg = worldConfigs.get(world.getName());
return wcfg != null && wcfg.isLogging(l);
}
public static boolean isLogging(String worldName, Logging l) {
final WorldConfig wcfg = worldConfigs.get(worldName);
return wcfg != null && wcfg.isLogging(l);
}
public static boolean isLogged(World world) {
return worldConfigs.containsKey(world.getName());
}
public static WorldConfig getWorldConfig(World world) {
return worldConfigs.get(world.getName());
}
public static WorldConfig getWorldConfig(String world) {
return worldConfigs.get(world);
}
public static boolean isLogging(Logging l) {
return superWorldConfig.isLogging(l);
}
public static Collection<WorldConfig> getLoggedWorlds() {
return worldConfigs.values();
}
public static boolean isLogging(World world, EntityLogging logging, Entity entity) {
final WorldConfig wcfg = worldConfigs.get(world.getName());
return wcfg != null && wcfg.isLogging(logging, entity);
}
public static boolean isLoggingAnyEntities() {
for (WorldConfig worldConfig : worldConfigs.values()) {
if (worldConfig.isLoggingAnyEntities()) {
return true;
}
}
return false;
}
public static boolean isLoggingNatualSpawns(World world) {
final WorldConfig wcfg = worldConfigs.get(world.getName());
return wcfg != null && wcfg.logNaturalEntitySpawns;
}
}
class LoggingEnabledMapping {
private final EnumSet<Logging> logging = EnumSet.noneOf(Logging.class);
public void setLogging(Logging l, boolean enabled) {
if (enabled) {
logging.add(l);
} else {
logging.remove(l);
}
}
public boolean isLogging(Logging l) {
return logging.contains(l);
}
}

View File

@ -0,0 +1,28 @@
package de.diddiz.LogBlock.config;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.bukkit.entity.EntityType;
public enum EntityLogging {
SPAWN(new String[] { EntityType.ARMOR_STAND.name(), EntityType.ITEM_FRAME.name(), EntityType.SNOWMAN.name() }),
DESTROY(new String[] { EntityType.ARMOR_STAND.name(), EntityType.ITEM_FRAME.name(), EntityType.VILLAGER.name(), EntityType.SNOWMAN.name(), "ANIMAL" }),
MODIFY(new String[] { "ALL" });
public static final int length = EntityLogging.values().length;
private final List<String> defaultEnabled;
private EntityLogging() {
this(null);
}
private EntityLogging(String[] defaultEnabled) {
this.defaultEnabled = defaultEnabled == null ? Collections.emptyList() : Collections.unmodifiableList(Arrays.asList(defaultEnabled));
}
public List<String> getDefaultEnabled() {
return defaultEnabled;
}
}

View File

@ -1,35 +1,171 @@
package de.diddiz.LogBlock.config; package de.diddiz.LogBlock.config;
import de.diddiz.LogBlock.Logging; import de.diddiz.LogBlock.LogBlock;
import org.bukkit.configuration.file.YamlConfiguration; import de.diddiz.LogBlock.Logging;
import de.diddiz.LogBlock.util.BukkitUtils;
import java.io.File; import org.bukkit.configuration.file.YamlConfiguration;
import java.io.IOException; import org.bukkit.entity.Animals;
import java.util.HashMap; import org.bukkit.entity.ArmorStand;
import java.util.Map; import org.bukkit.entity.Entity;
import java.util.Map.Entry; import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
public class WorldConfig extends LoggingEnabledMapping { import org.bukkit.entity.Monster;
public final String table; import org.bukkit.entity.Player;
import org.bukkit.entity.WaterMob;
public WorldConfig(File file) throws IOException {
final Map<String, Object> def = new HashMap<String, Object>(); import java.io.File;
// "Before MySQL 5.1.6, database and table names cannot contain "/", "\", ".", or characters that are not permitted in file names" - MySQL manual import java.io.IOException;
// They _can_ contain spaces, but replace them as well import java.util.EnumMap;
def.put("table", "lb-" + file.getName().substring(0, file.getName().length() - 4).replaceAll("[ ./\\\\]", "_")); import java.util.HashMap;
for (final Logging l : Logging.values()) { import java.util.HashSet;
def.put("logging." + l.toString(), l.isDefaultEnabled()); import java.util.List;
} import java.util.Map;
final YamlConfiguration config = YamlConfiguration.loadConfiguration(file); import java.util.Map.Entry;
for (final Entry<String, Object> e : def.entrySet()) { import java.util.logging.Level;
if (config.get(e.getKey()) == null) {
config.set(e.getKey(), e.getValue()); public class WorldConfig extends LoggingEnabledMapping {
} public final String world;
} public final String table;
config.save(file); public final String insertBlockStatementString;
table = config.getString("table"); public final String selectBlockActorIdStatementString;
for (final Logging l : Logging.values()) { public final String insertBlockStateStatementString;
setLogging(l, config.getBoolean("logging." + l.toString())); public final String insertBlockChestDataStatementString;
} public final String insertEntityStatementString;
} public final String updateEntityUUIDString;
}
private final EnumMap<EntityLogging, EntityLoggingList> entityLogging = new EnumMap<>(EntityLogging.class);
public final boolean logNaturalEntitySpawns;
public final boolean logAllNamedEntityKills;
public WorldConfig(String world, File file) throws IOException {
this.world = world;
final Map<String, Object> def = new HashMap<>();
// "Before MySQL 5.1.6, database and table names cannot contain "/", "\", ".", or characters that are not permitted in file names" - MySQL manual
// They _can_ contain spaces, but replace them as well
def.put("table", "lb-" + file.getName().substring(0, file.getName().length() - 4).replaceAll("[ ./\\\\]", "_"));
for (final Logging l : Logging.values()) {
def.put("logging." + l.toString(), l.isDefaultEnabled());
}
final YamlConfiguration config = YamlConfiguration.loadConfiguration(file);
for (final Entry<String, Object> e : def.entrySet()) {
if (config.get(e.getKey()) == null) {
config.set(e.getKey(), e.getValue());
}
}
for (EntityLogging el : EntityLogging.values()) {
if (!(config.get("entity." + el.name().toLowerCase()) instanceof List)) {
config.set("entity." + el.name().toLowerCase(), el.getDefaultEnabled());
}
entityLogging.put(el, new EntityLoggingList(el, config.getStringList("entity." + el.name().toLowerCase())));
}
if (!config.isBoolean("entity.logNaturalSpawns")) {
config.set("entity.logNaturalSpawns", false);
}
logNaturalEntitySpawns = config.getBoolean("entity.logNaturalSpawns");
if (!config.isBoolean("entity.logAllNamedEntityKills")) {
config.set("entity.logAllNamedEntityKills", true);
}
logAllNamedEntityKills = config.getBoolean("entity.logAllNamedEntityKills");
config.save(file);
table = config.getString("table");
for (final Logging l : Logging.values()) {
setLogging(l, config.getBoolean("logging." + l.toString()));
}
insertBlockStatementString = "INSERT INTO `" + table + "-blocks` (date, playerid, replaced, replaceddata, type, typedata, x, y, z) VALUES (FROM_UNIXTIME(?), ?, ?, ?, ?, ?, ?, ?, ?)";
selectBlockActorIdStatementString = "SELECT playerid FROM `" + table + "-blocks` WHERE x = ? AND y = ? AND z = ? ORDER BY date DESC LIMIT 1";
insertBlockStateStatementString = "INSERT INTO `" + table + "-state` (replacedState, typeState, id) VALUES(?, ?, ?)";
insertBlockChestDataStatementString = "INSERT INTO `" + table + "-chestdata` (item, itemremove, id, itemtype) values (?, ?, ?, ?)";
insertEntityStatementString = "INSERT INTO `" + table + "-entities` (date, playerid, entityid, entitytypeid, x, y, z, action, data) VALUES (FROM_UNIXTIME(?), ?, ?, ?, ?, ?, ?, ?, ?)";
updateEntityUUIDString = "UPDATE `" + table + "-entityids` SET entityuuid = ? WHERE entityid = ?";
}
public boolean isLogging(EntityLogging logging, Entity entity) {
return entityLogging.get(logging).isLogging(entity);
}
public boolean isLoggingAnyEntities() {
for (EntityLoggingList list : entityLogging.values()) {
if (list.isLoggingAnyEntities()) {
return true;
}
}
return false;
}
private class EntityLoggingList {
private final EntityLogging entityAction;
private final HashSet<EntityType> logged = new HashSet<>();
private final boolean logAll;
private final boolean logAnimals;
private final boolean logWateranimals;
private final boolean logMonsters;
private final boolean logLiving;
public EntityLoggingList(EntityLogging entityAction, List<String> types) {
this.entityAction = entityAction;
boolean all = false;
boolean animals = false;
boolean wateranimals = false;
boolean monsters = false;
boolean living = false;
for (String type : types) {
EntityType et = BukkitUtils.matchEntityType(type);
if (et != null) {
logged.add(et);
} else {
if (type.equalsIgnoreCase("all")) {
all = true;
} else if (type.equalsIgnoreCase("animal") || type.equalsIgnoreCase("animals")) {
animals = true;
} else if (type.equalsIgnoreCase("wateranimal") || type.equalsIgnoreCase("wateranimals")) {
wateranimals = true;
} else if (type.equalsIgnoreCase("monster") || type.equalsIgnoreCase("monsters")) {
monsters = true;
} else if (type.equalsIgnoreCase("living")) {
living = true;
} else {
LogBlock.getInstance().getLogger().log(Level.WARNING, "Unkown entity type in config for " + world + ": " + type);
}
}
}
logAll = all;
logAnimals = animals;
logWateranimals = wateranimals;
logMonsters = monsters;
logLiving = living;
}
public boolean isLogging(Entity entity) {
if (entity == null || (entity instanceof Player)) {
return false;
}
EntityType type = entity.getType();
if (logAll || logged.contains(type)) {
return true;
}
if (logLiving && LivingEntity.class.isAssignableFrom(entity.getClass()) && !(entity instanceof ArmorStand)) {
return true;
}
if (logAnimals && Animals.class.isAssignableFrom(entity.getClass())) {
return true;
}
if (logWateranimals && WaterMob.class.isAssignableFrom(entity.getClass())) {
return true;
}
if (logMonsters && (Monster.class.isAssignableFrom(entity.getClass()) || entity.getType() == EntityType.SLIME || entity.getType() == EntityType.WITHER || entity.getType() == EntityType.ENDER_DRAGON || entity.getType() == EntityType.SHULKER || entity.getType() == EntityType.GHAST)) {
return true;
}
if (entityAction == EntityLogging.DESTROY && logAllNamedEntityKills && entity.getCustomName() != null) {
return true;
}
return false;
}
public boolean isLoggingAnyEntities() {
return logAll || logAnimals || logLiving || logMonsters || !logged.isEmpty() || (entityAction == EntityLogging.DESTROY && logAllNamedEntityKills);
}
}
}

View File

@ -2,123 +2,93 @@ package de.diddiz.LogBlock.events;
import de.diddiz.LogBlock.Actor; import de.diddiz.LogBlock.Actor;
import de.diddiz.LogBlock.ChestAccess; import de.diddiz.LogBlock.ChestAccess;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.data.BlockData;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
public class BlockChangePreLogEvent extends PreLogEvent { public class BlockChangePreLogEvent extends PreLogEvent {
private static final HandlerList handlers = new HandlerList(); private static final HandlerList handlers = new HandlerList();
private Location location; private Location location;
private int typeBefore, typeAfter; private BlockData typeBefore, typeAfter;
private byte data;
private String signText;
private ChestAccess chestAccess; private ChestAccess chestAccess;
private YamlConfiguration stateBefore;
private YamlConfiguration stateAfter;
public BlockChangePreLogEvent(Actor owner, Location location, int typeBefore, int typeAfter, byte data, public BlockChangePreLogEvent(Actor owner, Location location, BlockData typeBefore, BlockData typeAfter, YamlConfiguration stateBefore, YamlConfiguration stateAfter, ChestAccess chestAccess) {
String signText, ChestAccess chestAccess) {
super(owner); super(owner);
this.location = location; this.location = location;
this.typeBefore = typeBefore; this.typeBefore = typeBefore;
this.typeAfter = typeAfter; this.typeAfter = typeAfter;
this.data = data; this.stateBefore = stateBefore;
this.signText = signText; this.stateAfter = stateAfter;
this.chestAccess = chestAccess; this.chestAccess = chestAccess;
} }
public Location getLocation() { public Location getLocation() {
return location; return location;
} }
public void setLocation(Location location) { public void setLocation(Location location) {
this.location = location; this.location = location;
} }
public int getTypeBefore() { public BlockData getTypeBefore() {
return typeBefore; return typeBefore;
} }
public void setTypeBefore(int typeBefore) { public void setTypeBefore(BlockData typeBefore) {
if (typeBefore == null) {
typeBefore = Bukkit.createBlockData(Material.AIR);
}
this.typeBefore = typeBefore; this.typeBefore = typeBefore;
} }
public int getTypeAfter() { public BlockData getTypeAfter() {
return typeAfter; return typeAfter;
} }
public void setTypeAfter(int typeAfter) { public void setTypeAfter(BlockData typeAfter) {
if (typeAfter == null) {
typeAfter = Bukkit.createBlockData(Material.AIR);
}
this.typeAfter = typeAfter; this.typeAfter = typeAfter;
} }
public byte getData() { public YamlConfiguration getStateBefore() {
return stateBefore;
return data;
} }
public void setData(byte data) { public YamlConfiguration getStateAfter() {
return stateAfter;
this.data = data;
} }
public String getSignText() { public void setStateBefore(YamlConfiguration stateBefore) {
this.stateBefore = stateBefore;
return signText;
} }
public void setSignText(String[] signText) { public void setStateAfter(YamlConfiguration stateAfter) {
this.stateAfter = stateAfter;
if (signText != null) {
// Check for block
Validate.isTrue(isValidSign(), "Must be valid sign block");
// Check for problems
Validate.noNullElements(signText, "No null lines");
Validate.isTrue(signText.length == 4, "Sign text must be 4 strings");
this.signText = signText[0] + "\0" + signText[1] + "\0" + signText[2] + "\0" + signText[3];
} else {
this.signText = null;
}
}
private boolean isValidSign() {
if ((typeAfter == 63 || typeAfter == 68) && typeBefore == 0) {
return true;
}
if ((typeBefore == 63 || typeBefore == 68) && typeAfter == 0) {
return true;
}
if ((typeAfter == 63 || typeAfter == 68) && typeBefore == typeAfter) {
return true;
}
return false;
} }
public ChestAccess getChestAccess() { public ChestAccess getChestAccess() {
return chestAccess; return chestAccess;
} }
public void setChestAccess(ChestAccess chestAccess) { public void setChestAccess(ChestAccess chestAccess) {
this.chestAccess = chestAccess; this.chestAccess = chestAccess;
} }
@Override
public HandlerList getHandlers() { public HandlerList getHandlers() {
return handlers; return handlers;
} }
public static HandlerList getHandlerList() { public static HandlerList getHandlerList() {
return handlers; return handlers;
} }
} }

View File

@ -0,0 +1,54 @@
package de.diddiz.LogBlock.events;
import de.diddiz.LogBlock.Actor;
import de.diddiz.LogBlock.EntityChange.EntityChangeType;
import org.bukkit.Location;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Entity;
import org.bukkit.event.HandlerList;
public class EntityChangePreLogEvent extends PreLogEvent {
private static final HandlerList handlers = new HandlerList();
private Location location;
private Entity entity;
private EntityChangeType changeType;
private YamlConfiguration changeData;
public EntityChangePreLogEvent(Actor owner, Location location, Entity entity, EntityChangeType changeType, YamlConfiguration changeData) {
super(owner);
this.location = location;
this.entity = entity;
this.changeType = changeType;
this.changeData = changeData;
}
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
public Entity getEntity() {
return entity;
}
public EntityChangeType getChangeType() {
return changeType;
}
public YamlConfiguration getChangeData() {
return changeData;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}

View File

@ -10,7 +10,6 @@ public abstract class PreLogEvent extends Event implements Cancellable {
protected Actor owner; protected Actor owner;
public PreLogEvent(Actor owner) { public PreLogEvent(Actor owner) {
this.owner = owner; this.owner = owner;
} }
@ -21,8 +20,8 @@ public abstract class PreLogEvent extends Event implements Cancellable {
* @deprecated {@link #getOwnerActor() } returns an object encapsulating * @deprecated {@link #getOwnerActor() } returns an object encapsulating
* name and uuid. Names are not guaranteed to be unique. * name and uuid. Names are not guaranteed to be unique.
*/ */
@Deprecated
public String getOwner() { public String getOwner() {
return owner.getName(); return owner.getName();
} }
@ -41,17 +40,16 @@ public abstract class PreLogEvent extends Event implements Cancellable {
* @param owner The player/monster/cause who is involved in this event * @param owner The player/monster/cause who is involved in this event
*/ */
public void setOwner(Actor owner) { public void setOwner(Actor owner) {
this.owner = owner; this.owner = owner;
} }
@Override
public boolean isCancelled() { public boolean isCancelled() {
return cancelled; return cancelled;
} }
@Override
public void setCancelled(boolean cancelled) { public void setCancelled(boolean cancelled) {
this.cancelled = cancelled; this.cancelled = cancelled;
} }
} }

View File

@ -0,0 +1,59 @@
package de.diddiz.LogBlock.events;
import de.diddiz.LogBlock.QueryParams;
import de.diddiz.LogBlock.Tool;
import de.diddiz.LogBlock.ToolBehavior;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;
/**
* Fired whether a tool is about to be used by a player.
*/
public class ToolUseEvent extends PlayerEvent implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private boolean cancel;
private final Tool tool;
private final ToolBehavior behavior;
private final QueryParams params;
public ToolUseEvent(Player who, Tool tool, ToolBehavior behavior, QueryParams params) {
super(who);
this.tool = tool;
this.behavior = behavior;
this.params = params;
}
@Override
public boolean isCancelled() {
return cancel;
}
@Override
public void setCancelled(boolean cancel) {
this.cancel = cancel;
}
public Tool getTool() {
return tool;
}
public ToolBehavior getBehavior() {
return behavior;
}
public QueryParams getParams() {
return params;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}

View File

@ -0,0 +1,327 @@
package de.diddiz.LogBlock.listeners;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.BlockFace;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Bee;
import org.bukkit.entity.EnderCrystal;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Hanging;
import org.bukkit.entity.IronGolem;
import org.bukkit.entity.ItemFrame;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Snowman;
import org.bukkit.entity.Wither;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.EntityPlaceEvent;
import org.bukkit.event.hanging.HangingBreakByEntityEvent;
import org.bukkit.event.hanging.HangingBreakEvent;
import org.bukkit.event.hanging.HangingPlaceEvent;
import org.bukkit.event.player.PlayerArmorStandManipulateEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;
import de.diddiz.LogBlock.Actor;
import de.diddiz.LogBlock.EntityChange;
import de.diddiz.LogBlock.EntityChange.EntityChangeType;
import de.diddiz.LogBlock.LogBlock;
import de.diddiz.LogBlock.config.Config;
import de.diddiz.LogBlock.config.EntityLogging;
import de.diddiz.LogBlock.util.LoggingUtil;
import de.diddiz.LogBlock.worldedit.WorldEditHelper;
import java.util.UUID;
public class AdvancedEntityLogging extends LoggingListener {
private Player lastSpawner;
private Class<? extends Entity> lastSpawning;
private boolean lastSpawnerEgg;
// serialize them before the death event
private UUID lastEntityDamagedForDeathUUID;
private byte[] lastEntityDamagedForDeathSerialized;
private Entity lastEntityDamagedForDeathDamager;
public AdvancedEntityLogging(LogBlock lb) {
super(lb);
new BukkitRunnable() {
@Override
public void run() {
resetOnTick();
}
}.runTaskTimer(lb, 1, 1);
}
private void resetOnTick() {
lastSpawner = null;
lastSpawning = null;
lastSpawnerEgg = false;
lastEntityDamagedForDeathUUID = null;
lastEntityDamagedForDeathSerialized = null;
lastEntityDamagedForDeathDamager = null;
}
private void setLastSpawner(Player player, Class<? extends Entity> spawning, boolean spawnEgg) {
lastSpawner = player;
lastSpawning = spawning;
lastSpawnerEgg = spawnEgg;
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerBlockPlace(BlockPlaceEvent event) {
Material placed = event.getBlock().getType();
if (placed == Material.WITHER_SKELETON_SKULL) {
setLastSpawner(event.getPlayer(), Wither.class, false);
} else if (placed == Material.CARVED_PUMPKIN) {
Material below = event.getBlock().getRelative(BlockFace.DOWN).getType();
if (below == Material.SNOW_BLOCK) {
setLastSpawner(event.getPlayer(), Snowman.class, false);
} else if (below == Material.IRON_BLOCK) {
setLastSpawner(event.getPlayer(), IronGolem.class, false);
}
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerInteract(PlayerInteractEvent event) {
if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
ItemStack inHand = event.getItem();
if (inHand != null) {
Material mat = inHand.getType();
if (mat == Material.ARMOR_STAND) {
setLastSpawner(event.getPlayer(), ArmorStand.class, false);
} else if (mat.name().endsWith("_SPAWN_EGG")) {
setLastSpawner(event.getPlayer(), null, true);
}
}
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
ItemStack inHand = event.getHand() == EquipmentSlot.HAND ? event.getPlayer().getInventory().getItemInMainHand() : event.getPlayer().getInventory().getItemInOffHand();
if (inHand != null && inHand.getType() != Material.AIR) {
Material mat = inHand.getType();
if (mat.name().endsWith("_SPAWN_EGG")) {
setLastSpawner(event.getPlayer(), null, true);
}
Entity entity = event.getRightClicked();
if (entity instanceof ItemFrame) {
ItemStack oldItem = ((ItemFrame) entity).getItem();
if (oldItem == null || oldItem.getType() == Material.AIR) {
if (Config.isLogging(entity.getWorld(), EntityLogging.MODIFY, entity)) {
Actor actor = Actor.actorFromEntity(event.getPlayer());
YamlConfiguration data = new YamlConfiguration();
inHand = inHand.clone();
inHand.setAmount(1);
data.set("item", inHand);
consumer.queueEntityModification(actor, entity, EntityChange.EntityChangeType.ADDEQUIP, data);
}
}
}
}
}
@EventHandler(priority = EventPriority.MONITOR)
public void onEntitySpawn(CreatureSpawnEvent event) {
if (!event.isCancelled()) {
if (event.getSpawnReason() == SpawnReason.CUSTOM || event.getSpawnReason() == SpawnReason.BEEHIVE) {
return;
}
if (event.getEntityType() == EntityType.ARMOR_STAND) {
resetOnTick();
return; // logged in the method below
}
LivingEntity entity = event.getEntity();
if (Config.isLogging(entity.getWorld(), EntityLogging.SPAWN, entity)) {
Actor actor = null;
if (lastSpawner != null && lastSpawner.getWorld() == entity.getWorld() && lastSpawner.getLocation().distance(entity.getLocation()) < 10) {
if (lastSpawnerEgg && event.getSpawnReason() == SpawnReason.SPAWNER_EGG) {
actor = Actor.actorFromEntity(lastSpawner);
} else if (lastSpawning != null && lastSpawning.isAssignableFrom(entity.getClass())) {
actor = Actor.actorFromEntity(lastSpawner);
}
}
if (actor == null) {
if (event.getSpawnReason() == SpawnReason.NATURAL && !Config.isLoggingNatualSpawns(entity.getWorld())) {
return;
}
actor = new Actor(event.getSpawnReason().toString());
}
queueEntitySpawnOrKill(entity, actor, EntityChange.EntityChangeType.CREATE);
}
}
resetOnTick();
}
@EventHandler(priority = EventPriority.MONITOR)
public void onEntityPlace(EntityPlaceEvent event) {
if (!event.isCancelled()) {
Entity entity = event.getEntity();
if (Config.isLogging(entity.getWorld(), EntityLogging.SPAWN, entity)) {
Actor actor = null;
if (event.getPlayer() != null) {
actor = Actor.actorFromEntity(event.getPlayer());
}
if (actor == null) {
actor = new Actor("UNKNOWN");
}
queueEntitySpawnOrKill(entity, actor, EntityChange.EntityChangeType.CREATE);
}
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onEntityDeath(EntityDeathEvent event) {
LivingEntity entity = event.getEntity();
if (Config.isLogging(entity.getWorld(), EntityLogging.DESTROY, entity)) {
Actor actor = null;
EntityDamageEvent lastDamage = entity.getLastDamageCause();
if (lastDamage instanceof EntityDamageByEntityEvent) {
Entity damager = LoggingUtil.getRealDamager(((EntityDamageByEntityEvent) lastDamage).getDamager());
if (damager != null) {
actor = Actor.actorFromEntity(damager);
}
}
if (actor == null && entity.getKiller() != null) {
actor = Actor.actorFromEntity(entity.getKiller());
}
if (actor == null) {
actor = new Actor(lastDamage == null ? "UNKNOWN" : lastDamage.getCause().toString());
}
queueEntitySpawnOrKill(entity, actor, EntityChange.EntityChangeType.KILL);
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onHangingPlace(HangingPlaceEvent event) {
Hanging entity = event.getEntity();
if (Config.isLogging(entity.getWorld(), EntityLogging.SPAWN, entity)) {
Actor actor = Actor.actorFromEntity(event.getPlayer());
queueEntitySpawnOrKill(entity, actor, EntityChange.EntityChangeType.CREATE);
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onHangingBreak(HangingBreakEvent event) {
Entity entity = event.getEntity();
if (Config.isLogging(entity.getWorld(), EntityLogging.DESTROY, entity)) {
Actor actor;
if (event instanceof HangingBreakByEntityEvent) {
Entity damager = LoggingUtil.getRealDamager(((HangingBreakByEntityEvent) event).getRemover());
actor = Actor.actorFromEntity(damager);
} else {
actor = new Actor(event.getCause().toString());
}
queueEntitySpawnOrKill(entity, actor, EntityChange.EntityChangeType.KILL);
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onEntityDamage(EntityDamageEvent event) {
Entity entity = event.getEntity();
if (entity instanceof ItemFrame) {
ItemStack oldItem = ((ItemFrame) entity).getItem();
if (oldItem != null && oldItem.getType() != Material.AIR) {
if (Config.isLogging(entity.getWorld(), EntityLogging.MODIFY, entity)) {
Actor actor;
if (event instanceof EntityDamageByEntityEvent) {
Entity damager = LoggingUtil.getRealDamager(((EntityDamageByEntityEvent) event).getDamager());
actor = Actor.actorFromEntity(damager);
} else {
actor = new Actor(event.getCause().toString());
}
YamlConfiguration data = new YamlConfiguration();
data.set("item", oldItem);
consumer.queueEntityModification(actor, entity, EntityChange.EntityChangeType.REMOVEEQUIP, data);
}
}
}
if (Config.isLogging(entity.getWorld(), EntityLogging.DESTROY, entity)) {
lastEntityDamagedForDeathUUID = entity.getUniqueId();
lastEntityDamagedForDeathSerialized = WorldEditHelper.serializeEntity(entity);
}
if (entity instanceof EnderCrystal) {
if (Config.isLogging(entity.getWorld(), EntityLogging.DESTROY, entity)) {
if (event instanceof EntityDamageByEntityEvent) {
Entity damager = LoggingUtil.getRealDamager(((EntityDamageByEntityEvent) event).getDamager());
if (lastEntityDamagedForDeathDamager == null || !(damager instanceof EnderCrystal)) {
lastEntityDamagedForDeathDamager = damager;
}
}
Actor actor = lastEntityDamagedForDeathDamager != null ? Actor.actorFromEntity(lastEntityDamagedForDeathDamager) : new Actor(event.getCause().toString());
queueEntitySpawnOrKill(entity, actor, EntityChange.EntityChangeType.KILL);
}
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
Entity damager = event.getDamager();
if (damager instanceof Bee && !((Bee) damager).hasStung()) {
if (Config.isLogging(damager.getWorld(), EntityLogging.MODIFY, damager)) {
Actor actor = Actor.actorFromEntity(event.getEntity());
consumer.queueEntityModification(actor, damager, EntityChange.EntityChangeType.GET_STUNG, null);
}
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerArmorStandManipulate(PlayerArmorStandManipulateEvent event) {
ArmorStand entity = event.getRightClicked();
ItemStack oldItem = event.getArmorStandItem();
ItemStack newItem = event.getPlayerItem();
boolean oldEmpty = oldItem == null || oldItem.getType() == Material.AIR;
boolean newEmpty = newItem == null || newItem.getType() == Material.AIR;
if ((!oldEmpty || !newEmpty) && Config.isLogging(entity.getWorld(), EntityLogging.MODIFY, entity)) {
Actor actor = Actor.actorFromEntity(event.getPlayer());
if (!oldEmpty && !newEmpty && newItem.getAmount() > 1) {
return;
}
if (!oldEmpty) {
YamlConfiguration data = new YamlConfiguration();
data.set("item", oldItem);
data.set("slot", event.getSlot().name());
consumer.queueEntityModification(actor, entity, EntityChange.EntityChangeType.REMOVEEQUIP, data);
}
if (!newEmpty) {
YamlConfiguration data = new YamlConfiguration();
data.set("item", newItem);
data.set("slot", event.getSlot().name());
consumer.queueEntityModification(actor, entity, EntityChange.EntityChangeType.ADDEQUIP, data);
}
}
}
protected void queueEntitySpawnOrKill(Entity entity, Actor actor, EntityChange.EntityChangeType changeType) {
Location location = entity.getLocation();
YamlConfiguration data = new YamlConfiguration();
data.set("x", location.getX());
data.set("y", location.getY());
data.set("z", location.getZ());
data.set("yaw", location.getYaw());
data.set("pitch", location.getPitch());
if (changeType == EntityChangeType.KILL && entity.getUniqueId().equals(lastEntityDamagedForDeathUUID)) {
data.set("worldedit", lastEntityDamagedForDeathSerialized);
} else {
data.set("worldedit", WorldEditHelper.serializeEntity(entity));
}
consumer.queueEntityModification(actor, entity, changeType, data);
}
}

View File

@ -1,48 +1,48 @@
package de.diddiz.LogBlock.listeners; package de.diddiz.LogBlock.listeners;
import de.diddiz.LogBlock.CommandsHandler; import de.diddiz.LogBlock.CommandsHandler;
import de.diddiz.LogBlock.LogBlock; import de.diddiz.LogBlock.LogBlock;
import de.diddiz.LogBlock.QueryParams; import de.diddiz.LogBlock.QueryParams;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import static de.diddiz.LogBlock.config.Config.banPermission; import static de.diddiz.LogBlock.config.Config.banPermission;
import static de.diddiz.LogBlock.config.Config.isLogged; import static de.diddiz.LogBlock.config.Config.isLogged;
import static org.bukkit.Bukkit.getScheduler; import static org.bukkit.Bukkit.getScheduler;
public class BanListener implements Listener { public class BanListener implements Listener {
private final CommandsHandler handler; private final CommandsHandler handler;
private final LogBlock logblock; private final LogBlock logblock;
public BanListener(LogBlock logblock) { public BanListener(LogBlock logblock) {
this.logblock = logblock; this.logblock = logblock;
handler = logblock.getCommandsHandler(); handler = logblock.getCommandsHandler();
} }
@EventHandler @EventHandler
public void onPlayerCommandPreprocess(final PlayerCommandPreprocessEvent event) { public void onPlayerCommandPreprocess(final PlayerCommandPreprocessEvent event) {
final String[] split = event.getMessage().split(" "); final String[] split = event.getMessage().split(" ");
if (split.length > 1 && split[0].equalsIgnoreCase("/ban") && logblock.hasPermission(event.getPlayer(), banPermission)) { if (split.length > 1 && split[0].equalsIgnoreCase("/ban") && logblock.hasPermission(event.getPlayer(), banPermission)) {
final QueryParams p = new QueryParams(logblock); final QueryParams p = new QueryParams(logblock);
p.setPlayer(split[1].equalsIgnoreCase("g") ? split[2] : split[1]); p.setPlayer(split[1].equalsIgnoreCase("g") ? split[2] : split[1]);
p.since = 0; p.since = 0;
p.silent = false; p.silent = false;
getScheduler().scheduleAsyncDelayedTask(logblock, new Runnable() { getScheduler().runTaskAsynchronously(logblock, new Runnable() {
@Override @Override
public void run() { public void run() {
for (final World world : logblock.getServer().getWorlds()) { for (final World world : logblock.getServer().getWorlds()) {
if (isLogged(world)) { if (isLogged(world)) {
p.world = world; p.world = world;
try { try {
handler.new CommandRollback(event.getPlayer(), p, false); handler.new CommandRollback(event.getPlayer(), p, false);
} catch (final Exception ex) { } catch (final Exception ex) {
} }
} }
} }
} }
}); });
} }
} }
} }

View File

@ -1,64 +1,88 @@
package de.diddiz.LogBlock.listeners; package de.diddiz.LogBlock.listeners;
import de.diddiz.LogBlock.Actor; import de.diddiz.LogBlock.Actor;
import de.diddiz.LogBlock.LogBlock; import de.diddiz.LogBlock.LogBlock;
import de.diddiz.LogBlock.Logging; import de.diddiz.LogBlock.Logging;
import de.diddiz.LogBlock.config.WorldConfig; import de.diddiz.LogBlock.config.WorldConfig;
import de.diddiz.util.BukkitUtils; import de.diddiz.LogBlock.util.BukkitUtils;
import org.bukkit.GameMode; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.GameMode;
import org.bukkit.block.Block; import org.bukkit.Material;
import org.bukkit.block.Sign; import org.bukkit.block.Block;
import org.bukkit.event.EventHandler; import org.bukkit.block.data.BlockData;
import org.bukkit.event.EventPriority; import org.bukkit.block.data.Waterlogged;
import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerBucketFillEvent; import org.bukkit.event.EventPriority;
import org.bukkit.event.block.BlockBreakEvent;
import static de.diddiz.LogBlock.config.Config.getWorldConfig; import org.bukkit.event.block.BlockDropItemEvent;
import static de.diddiz.LogBlock.config.Config.isLogging; import org.bukkit.event.player.PlayerBucketFillEvent;
import static de.diddiz.util.LoggingUtil.smartLogBlockBreak;
import static de.diddiz.util.LoggingUtil.smartLogFallables; import static de.diddiz.LogBlock.config.Config.getWorldConfig;
import static de.diddiz.LogBlock.config.Config.isLogging;
public class BlockBreakLogging extends LoggingListener { import static de.diddiz.LogBlock.util.LoggingUtil.smartLogBlockBreak;
public BlockBreakLogging(LogBlock lb) { import static de.diddiz.LogBlock.util.LoggingUtil.smartLogBlockReplace;
super(lb); import static de.diddiz.LogBlock.util.LoggingUtil.smartLogFallables;
}
public class BlockBreakLogging extends LoggingListener {
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public BlockBreakLogging(LogBlock lb) {
public void onBlockBreak(BlockBreakEvent event) { super(lb);
if (isLogging(event.getBlock().getWorld(), Logging.BLOCKBREAK)) { }
WorldConfig wcfg = getWorldConfig(event.getBlock().getWorld());
if (wcfg == null) { @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
return; public void onBlockBreak(BlockBreakEvent event) {
} if (isLogging(event.getBlock().getWorld(), Logging.BLOCKBREAK)) {
WorldConfig wcfg = getWorldConfig(event.getBlock().getWorld());
final Actor actor = Actor.actorFromEntity(event.getPlayer()); if (wcfg == null) {
final Block origin = event.getBlock(); return;
final int typeId = origin.getTypeId(); }
final Material type = origin.getType();
final Actor actor = Actor.actorFromEntity(event.getPlayer());
if (wcfg.isLogging(Logging.SIGNTEXT) && (typeId == 63 || typeId == 68)) { final Block origin = event.getBlock();
consumer.queueSignBreak(actor, (Sign) origin.getState()); final Material type = origin.getType();
} else if (wcfg.isLogging(Logging.CHESTACCESS) && BukkitUtils.getContainerBlocks().contains(type)) {
consumer.queueContainerBreak(actor, origin.getState()); if (wcfg.isLogging(Logging.CHESTACCESS) && BukkitUtils.isContainerBlock(type) && !BukkitUtils.isShulkerBoxBlock(type)) {
} else if (type == Material.ICE) { consumer.queueContainerBreak(actor, origin.getState());
// When in creative mode ice doesn't form water } else if (type == Material.ICE) {
if (event.getPlayer().getGameMode().equals(GameMode.CREATIVE)) { // When in creative mode ice doesn't form water
consumer.queueBlockBreak(actor, origin.getState()); if (event.getPlayer().getGameMode().equals(GameMode.CREATIVE)) {
} else { smartLogBlockBreak(consumer, actor, origin);
consumer.queueBlockReplace(actor, origin.getState(), 9, (byte) 0); } else {
} smartLogBlockReplace(consumer, actor, origin, Bukkit.createBlockData(Material.WATER));
} else { }
smartLogBlockBreak(consumer, actor, origin); } else {
} smartLogBlockBreak(consumer, actor, origin);
smartLogFallables(consumer, actor, origin); }
} smartLogFallables(consumer, actor, origin);
} }
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerBucketFill(PlayerBucketFillEvent event) { @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
if (isLogging(event.getBlockClicked().getWorld(), Logging.BLOCKBREAK)) { public void onPlayerBucketFill(PlayerBucketFillEvent event) {
consumer.queueBlockBreak(Actor.actorFromEntity(event.getPlayer()), event.getBlockClicked().getState()); if (isLogging(event.getBlockClicked().getWorld(), Logging.BLOCKBREAK)) {
} BlockData clickedBlockData = event.getBlockClicked().getBlockData();
} if (clickedBlockData instanceof Waterlogged) {
} Waterlogged clickedWaterlogged = (Waterlogged) clickedBlockData;
if (clickedWaterlogged.isWaterlogged()) {
Waterlogged clickedWaterloggedWithoutWater = (Waterlogged) clickedWaterlogged.clone();
clickedWaterloggedWithoutWater.setWaterlogged(false);
consumer.queueBlockReplace(Actor.actorFromEntity(event.getPlayer()), event.getBlockClicked().getLocation(), clickedWaterlogged, clickedWaterloggedWithoutWater);
}
} else {
consumer.queueBlockBreak(Actor.actorFromEntity(event.getPlayer()), event.getBlockClicked().getState());
}
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockDropItem(BlockDropItemEvent event) {
if (isLogging(event.getBlock().getWorld(), Logging.BLOCKBREAK)) {
Material type = event.getBlock().getType();
if (type == Material.SUSPICIOUS_GRAVEL || type == Material.SUSPICIOUS_SAND) {
Material simplyBroken = type == Material.SUSPICIOUS_SAND ? Material.SAND : Material.GRAVEL;
if (event.getItems().size() != 1 || event.getItems().get(0).getItemStack().getType() != simplyBroken) {
consumer.queueBlockReplace(Actor.actorFromEntity(event.getPlayer()), event.getBlockState(), simplyBroken.createBlockData());
}
}
}
}
}

View File

@ -1,44 +1,72 @@
package de.diddiz.LogBlock.listeners; package de.diddiz.LogBlock.listeners;
import de.diddiz.LogBlock.Actor; import de.diddiz.LogBlock.Actor;
import de.diddiz.LogBlock.LogBlock; import de.diddiz.LogBlock.LogBlock;
import de.diddiz.LogBlock.Logging; import de.diddiz.LogBlock.Logging;
import org.bukkit.Material; import de.diddiz.LogBlock.config.Config;
import org.bukkit.block.Block;
import org.bukkit.entity.Player; import org.bukkit.Material;
import org.bukkit.event.EventHandler; import org.bukkit.block.Block;
import org.bukkit.event.EventPriority; import org.bukkit.entity.Player;
import org.bukkit.event.block.Action; import org.bukkit.event.EventHandler;
import org.bukkit.event.block.BlockBurnEvent; import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockBurnEvent;
import static de.diddiz.LogBlock.config.Config.isLogging; import org.bukkit.event.block.BlockIgniteEvent;
import static de.diddiz.util.LoggingUtil.smartLogBlockBreak; import org.bukkit.event.block.BlockIgniteEvent.IgniteCause;
import static de.diddiz.util.LoggingUtil.smartLogFallables; import org.bukkit.event.player.PlayerInteractEvent;
public class BlockBurnLogging extends LoggingListener { import static de.diddiz.LogBlock.config.Config.isLogging;
public BlockBurnLogging(LogBlock lb) { import static de.diddiz.LogBlock.util.LoggingUtil.smartLogBlockBreak;
super(lb); import static de.diddiz.LogBlock.util.LoggingUtil.smartLogBlockReplace;
} import static de.diddiz.LogBlock.util.LoggingUtil.smartLogFallables;
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public class BlockBurnLogging extends LoggingListener {
public void onBlockBurn(BlockBurnEvent event) { public BlockBurnLogging(LogBlock lb) {
if (isLogging(event.getBlock().getWorld(), Logging.FIRE)) { super(lb);
smartLogBlockBreak(consumer, new Actor("Fire"), event.getBlock()); }
smartLogFallables(consumer, new Actor("Fire"), event.getBlock());
} @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
} public void onBlockBurn(BlockBurnEvent event) {
if (isLogging(event.getBlock().getWorld(), Logging.FIRE)) {
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) smartLogBlockReplace(consumer, new Actor("Fire", Config.logFireSpreadAsPlayerWhoCreatedIt ? event.getIgnitingBlock() : null), event.getBlock(), Material.FIRE.createBlockData());
public void onExtinguish(PlayerInteractEvent event) { smartLogFallables(consumer, new Actor("Fire", Config.logFireSpreadAsPlayerWhoCreatedIt ? event.getIgnitingBlock() : null), event.getBlock());
if (event.getAction().equals(Action.LEFT_CLICK_BLOCK)) { }
Player player = event.getPlayer(); }
Block block = event.getClickedBlock().getRelative(event.getBlockFace());
if (block.getType().equals(Material.FIRE) && isLogging(player.getWorld(), Logging.FIRE)) { @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
Actor actor = Actor.actorFromEntity(player); public void onBlockIgnite(BlockIgniteEvent event) {
smartLogBlockBreak(consumer, actor, block); Actor actor = new Actor("Fire", Config.logFireSpreadAsPlayerWhoCreatedIt ? event.getIgnitingBlock() : null);
smartLogFallables(consumer, actor, block); if (event.getCause() == IgniteCause.FLINT_AND_STEEL) {
} if (event.getIgnitingEntity() != null) {
} return; // handled in block place
} } else {
} actor = new Actor("Dispenser");
}
} else if (event.getCause() == IgniteCause.LIGHTNING) {
actor = new Actor("Lightning");
} else if (event.getCause() == IgniteCause.EXPLOSION) {
actor = new Actor("Explosion");
} else if (event.getCause() == IgniteCause.LAVA) {
actor = new Actor("Lava");
} else if (event.getCause() == IgniteCause.ENDER_CRYSTAL) {
actor = new Actor("EnderCrystal");
}
if (isLogging(event.getBlock().getWorld(), Logging.FIRE)) {
consumer.queueBlockPlace(actor, event.getBlock().getLocation(), Material.FIRE.createBlockData());
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onExtinguish(PlayerInteractEvent event) {
if (event.getAction().equals(Action.LEFT_CLICK_BLOCK)) {
Player player = event.getPlayer();
Block block = event.getClickedBlock().getRelative(event.getBlockFace());
if (block.getType().equals(Material.FIRE) && isLogging(player.getWorld(), Logging.FIRE)) {
Actor actor = Actor.actorFromEntity(player);
smartLogBlockBreak(consumer, actor, block);
smartLogFallables(consumer, actor, block);
}
}
}
}

View File

@ -0,0 +1,36 @@
package de.diddiz.LogBlock.listeners;
import de.diddiz.LogBlock.Actor;
import de.diddiz.LogBlock.LogBlock;
import de.diddiz.LogBlock.Logging;
import de.diddiz.LogBlock.config.WorldConfig;
import org.bukkit.block.BlockState;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.BlockFertilizeEvent;
import static de.diddiz.LogBlock.config.Config.getWorldConfig;
public class BlockFertilizeLogging extends LoggingListener {
public BlockFertilizeLogging(LogBlock lb) {
super(lb);
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockFertilize(BlockFertilizeEvent event) {
final WorldConfig wcfg = getWorldConfig(event.getBlock().getLocation().getWorld());
if (wcfg != null) {
if (!wcfg.isLogging(Logging.BONEMEALSTRUCTUREGROW)) {
return;
}
final Actor actor;
if (event.getPlayer() != null) {
actor = Actor.actorFromEntity(event.getPlayer());
} else {
actor = new Actor("Dispenser");
}
for (final BlockState state : event.getBlocks()) {
consumer.queueBlockReplace(actor, state.getBlock().getState(), state);
}
}
}
}

View File

@ -1,92 +1,67 @@
package de.diddiz.LogBlock.listeners; package de.diddiz.LogBlock.listeners;
import de.diddiz.LogBlock.Actor; import de.diddiz.LogBlock.Actor;
import de.diddiz.LogBlock.LogBlock; import de.diddiz.LogBlock.LogBlock;
import de.diddiz.LogBlock.Logging; import de.diddiz.LogBlock.Logging;
import de.diddiz.LogBlock.config.WorldConfig; import de.diddiz.LogBlock.config.Config;
import de.diddiz.util.BukkitUtils; import de.diddiz.LogBlock.util.LoggingUtil;
import org.bukkit.Location; import org.bukkit.Material;
import org.bukkit.Material; import org.bukkit.block.Block;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockState;
import org.bukkit.block.BlockState; import org.bukkit.block.data.BlockData;
import org.bukkit.event.EventHandler; import org.bukkit.block.data.Waterlogged;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventHandler;
import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerBucketEmptyEvent; import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.player.PlayerBucketEmptyEvent;
import static de.diddiz.LogBlock.config.Config.getWorldConfig;
import static de.diddiz.LogBlock.config.Config.isLogging; import static de.diddiz.LogBlock.config.Config.isLogging;
public class BlockPlaceLogging extends LoggingListener { public class BlockPlaceLogging extends LoggingListener {
public BlockPlaceLogging(LogBlock lb) { public BlockPlaceLogging(LogBlock lb) {
super(lb); super(lb);
} }
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockPlace(BlockPlaceEvent event) { public void onBlockPlace(BlockPlaceEvent event) {
final WorldConfig wcfg = getWorldConfig(event.getBlock().getWorld()); if (Config.isLogging(event.getBlock().getWorld(), Logging.BLOCKPLACE)) {
if (wcfg != null && wcfg.isLogging(Logging.BLOCKPLACE)) { final BlockState before = event.getBlockReplacedState();
final Material type = event.getBlock().getType(); final BlockState after = event.getBlockPlaced().getState();
final BlockState before = event.getBlockReplacedState(); final Actor actor = Actor.actorFromEntity(event.getPlayer());
final BlockState after = event.getBlockPlaced().getState(); if (before.getType() == Material.LECTERN && after.getType() == Material.LECTERN) {
final Actor actor = Actor.actorFromEntity(event.getPlayer()); return;
}
//Handle falling blocks LoggingUtil.smartLogBlockPlace(consumer, actor, before, after);
if (BukkitUtils.getRelativeTopFallables().contains(type)) { }
}
// Catch placed blocks overwriting something
if (before.getType() != Material.AIR) { @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
consumer.queueBlockBreak(actor, before); public void onPlayerBucketEmpty(PlayerBucketEmptyEvent event) {
} if (isLogging(event.getPlayer().getWorld(), Logging.BLOCKPLACE)) {
Material placedMaterial = event.getBucket() == Material.LAVA_BUCKET ? Material.LAVA : Material.WATER;
Location loc = event.getBlock().getLocation(); BlockData clickedBlockData = event.getBlockClicked().getBlockData();
int x = loc.getBlockX(); if (placedMaterial == Material.WATER && clickedBlockData instanceof Waterlogged) {
int y = loc.getBlockY(); Waterlogged clickedWaterlogged = (Waterlogged) clickedBlockData;
int z = loc.getBlockZ(); if (!clickedWaterlogged.isWaterlogged()) {
// Blocks only fall if they have a chance to start a velocity Waterlogged clickedWaterloggedWithWater = (Waterlogged) clickedWaterlogged.clone();
if (event.getBlock().getRelative(BlockFace.DOWN).getType() == Material.AIR) { clickedWaterloggedWithWater.setWaterlogged(true);
while (y > 0 && BukkitUtils.canFall(loc.getWorld(), x, (y - 1), z)) { consumer.queueBlockReplace(Actor.actorFromEntity(event.getPlayer()), event.getBlockClicked().getLocation(), clickedWaterlogged, clickedWaterloggedWithWater);
y--; return;
} }
} }
// If y is 0 then the sand block fell out of the world :( Block placedAt = event.getBlockClicked().getRelative(event.getBlockFace());
if (y != 0) { if (placedAt.isEmpty()) {
Location finalLoc = new Location(loc.getWorld(), x, y, z); consumer.queueBlockPlace(Actor.actorFromEntity(event.getPlayer()), placedAt.getLocation(), placedMaterial.createBlockData());
// Run this check to avoid false positives } else {
if (!BukkitUtils.getFallingEntityKillers().contains(finalLoc.getBlock().getType())) { BlockData placedAtBlock = placedAt.getBlockData();
if (finalLoc.getBlock().getType() == Material.AIR || finalLoc.equals(event.getBlock().getLocation())) { if (placedAtBlock instanceof Waterlogged && !(((Waterlogged) placedAtBlock).isWaterlogged())) {
consumer.queueBlockPlace(actor, finalLoc, type.getId(), event.getBlock().getData()); Waterlogged clickedWaterloggedWithWater = (Waterlogged) placedAtBlock.clone();
} else { clickedWaterloggedWithWater.setWaterlogged(true);
consumer.queueBlockReplace(actor, finalLoc, finalLoc.getBlock().getTypeId(), finalLoc.getBlock().getData(), type.getId(), event.getBlock().getData()); consumer.queueBlockReplace(Actor.actorFromEntity(event.getPlayer()), placedAt.getLocation(), placedAtBlock, clickedWaterloggedWithWater);
} } else {
} consumer.queueBlockReplace(Actor.actorFromEntity(event.getPlayer()), placedAt.getLocation(), placedAtBlock, placedMaterial.createBlockData());
} }
return; }
} }
}
//Sign logging is handled elsewhere }
if (wcfg.isLogging(Logging.SIGNTEXT) && (type.getId() == 63 || type.getId() == 68)) {
return;
}
//Delay queuing by one tick to allow data to be updated
LogBlock.getInstance().getServer().getScheduler().scheduleSyncDelayedTask(LogBlock.getInstance(), new Runnable() {
@Override
public void run() {
if (before.getTypeId() == 0) {
consumer.queueBlockPlace(actor, after);
} else {
consumer.queueBlockReplace(actor, before, after);
}
}
}, 1L);
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerBucketEmpty(PlayerBucketEmptyEvent event) {
if (isLogging(event.getPlayer().getWorld(), Logging.BLOCKPLACE)) {
consumer.queueBlockPlace(Actor.actorFromEntity(event.getPlayer()), event.getBlockClicked().getRelative(event.getBlockFace()).getLocation(), event.getBucket() == Material.WATER_BUCKET ? 9 : 11, (byte) 0);
}
}
}

View File

@ -5,6 +5,10 @@ import de.diddiz.LogBlock.LogBlock;
import de.diddiz.LogBlock.Logging; import de.diddiz.LogBlock.Logging;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.type.PointedDripstone;
import org.bukkit.block.data.type.PointedDripstone.Thickness;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.block.BlockSpreadEvent; import org.bukkit.event.block.BlockSpreadEvent;
@ -22,37 +26,74 @@ public class BlockSpreadLogging extends LoggingListener {
String name; String name;
World world = event.getBlock().getWorld(); World world = event.getNewState().getWorld();
Material type = event.getSource().getType(); Material type = event.getNewState().getType();
switch (type) { if (type == Material.SHORT_GRASS) {
case GRASS: if (!isLogging(world, Logging.GRASSGROWTH)) {
if (!isLogging(world, Logging.GRASSGROWTH)) {
return;
}
name = "GrassGrowth";
break;
case MYCEL:
if (!isLogging(world, Logging.MYCELIUMSPREAD)) {
return;
}
name = "MyceliumSpread";
break;
case VINE:
if (!isLogging(world, Logging.VINEGROWTH)) {
return;
}
name = "VineGrowth";
break;
case RED_MUSHROOM:
case BROWN_MUSHROOM:
if (!isLogging(world, Logging.MUSHROOMSPREAD)) {
return;
}
name = "MushroomSpread";
break;
default:
return; return;
}
name = "GrassGrowth";
} else if (type == Material.MYCELIUM) {
if (!isLogging(world, Logging.MYCELIUMSPREAD)) {
return;
}
name = "MyceliumSpread";
} else if (type == Material.VINE || type == Material.CAVE_VINES || type == Material.CAVE_VINES_PLANT || type == Material.WEEPING_VINES || type == Material.WEEPING_VINES_PLANT || type == Material.TWISTING_VINES || type == Material.TWISTING_VINES_PLANT) {
if (!isLogging(world, Logging.VINEGROWTH)) {
return;
}
name = "VineGrowth";
} else if (type == Material.RED_MUSHROOM || type == Material.BROWN_MUSHROOM) {
if (!isLogging(world, Logging.MUSHROOMSPREAD)) {
return;
}
name = "MushroomSpread";
} else if (type == Material.BAMBOO || type == Material.BAMBOO_SAPLING) {
if (!isLogging(world, Logging.BAMBOOGROWTH)) {
return;
}
name = "BambooGrowth";
if (type == Material.BAMBOO_SAPLING) {
// bamboo sapling gets replaced by bamboo
consumer.queueBlockReplace(new Actor(name), event.getSource().getState(), Material.BAMBOO.createBlockData());
}
} else if (type == Material.POINTED_DRIPSTONE) {
if (!isLogging(world, Logging.DRIPSTONEGROWTH)) {
return;
}
name = "DripstoneGrowth";
PointedDripstone pointed = (PointedDripstone) event.getNewState().getBlockData();
if (pointed.getThickness() != Thickness.TIP_MERGE) {
BlockFace direction = pointed.getVerticalDirection();
Block previousPart = event.getBlock().getRelative(direction.getOppositeFace());
if (previousPart.getType() == Material.POINTED_DRIPSTONE) {
PointedDripstone newBelow = (PointedDripstone) previousPart.getBlockData();
newBelow.setThickness(Thickness.FRUSTUM);
consumer.queueBlockReplace(new Actor(name), previousPart.getState(), newBelow);
previousPart = previousPart.getRelative(direction.getOppositeFace());
if (previousPart.getType() == Material.POINTED_DRIPSTONE) {
Block evenMorePrevious = previousPart.getRelative(direction.getOppositeFace());
newBelow = (PointedDripstone) previousPart.getBlockData();
newBelow.setThickness(evenMorePrevious.getType() == Material.POINTED_DRIPSTONE ? Thickness.MIDDLE : Thickness.BASE);
consumer.queueBlockReplace(new Actor(name), previousPart.getState(), newBelow);
}
}
} else {
// special case because the old state is already changed (for one half)
PointedDripstone oldState = (PointedDripstone) event.getNewState().getBlockData();
oldState.setThickness(Thickness.TIP);
consumer.queueBlockReplace(new Actor(name), oldState, event.getNewState());
return;
}
} else if (type == Material.SCULK || type == Material.SCULK_VEIN || type == Material.SCULK_CATALYST || type == Material.SCULK_SENSOR || type == Material.SCULK_SHRIEKER) {
if (!isLogging(world, Logging.SCULKSPREAD)) {
return;
}
name = "SculkSpread";
} else {
return;
} }
consumer.queueBlockReplace(new Actor(name), event.getBlock().getState(), event.getNewState()); consumer.queueBlockReplace(new Actor(name), event.getBlock().getState(), event.getNewState());

View File

@ -0,0 +1,27 @@
package de.diddiz.LogBlock.listeners;
import de.diddiz.LogBlock.Actor;
import de.diddiz.LogBlock.LogBlock;
import de.diddiz.LogBlock.Logging;
import de.diddiz.LogBlock.config.Config;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.CauldronLevelChangeEvent;
public class CauldronLogging extends LoggingListener {
public CauldronLogging(LogBlock lb) {
super(lb);
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onCauldronLevelChange(CauldronLevelChangeEvent event) {
if (Config.isLogging(event.getBlock().getWorld(), Logging.CAULDRONINTERACT)) {
Entity causingEntity = event.getEntity();
if (causingEntity instanceof Player) {
consumer.queueBlockReplace(Actor.actorFromEntity(causingEntity), event.getBlock().getBlockData(), event.getNewState());
}
}
}
}

View File

@ -1,37 +1,58 @@
package de.diddiz.LogBlock.listeners; package de.diddiz.LogBlock.listeners;
import de.diddiz.LogBlock.Actor; import de.diddiz.LogBlock.Actor;
import de.diddiz.LogBlock.LogBlock; import de.diddiz.LogBlock.LogBlock;
import de.diddiz.LogBlock.Logging; import de.diddiz.LogBlock.Logging;
import org.bukkit.event.EventHandler; import org.bukkit.command.BlockCommandSender;
import org.bukkit.event.EventPriority; import org.bukkit.command.CommandSender;
import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.entity.minecart.CommandMinecart;
import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.EventHandler;
import org.bukkit.event.server.ServerCommandEvent; import org.bukkit.event.EventPriority;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import static de.diddiz.LogBlock.config.Config.isLogging; import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.server.ServerCommandEvent;
public class ChatLogging extends LoggingListener {
public ChatLogging(LogBlock lb) { import static de.diddiz.LogBlock.config.Config.isLogging;
super(lb);
} public class ChatLogging extends LoggingListener {
public ChatLogging(LogBlock lb) {
@EventHandler(priority = EventPriority.MONITOR) super(lb);
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) { }
if (isLogging(event.getPlayer().getWorld(), Logging.CHAT)) {
consumer.queueChat(Actor.actorFromEntity(event.getPlayer()), event.getMessage()); @EventHandler(priority = EventPriority.MONITOR)
} public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
} if (isLogging(event.getPlayer().getWorld(), Logging.PLAYER_COMMANDS)) {
consumer.queueChat(Actor.actorFromEntity(event.getPlayer()), event.getMessage());
@EventHandler(priority = EventPriority.MONITOR) }
public void onPlayerChat(AsyncPlayerChatEvent event) { }
if (isLogging(event.getPlayer().getWorld(), Logging.CHAT)) {
consumer.queueChat(Actor.actorFromEntity(event.getPlayer()), event.getMessage()); @EventHandler(priority = EventPriority.MONITOR)
} public void onPlayerChat(AsyncPlayerChatEvent event) {
} if (isLogging(event.getPlayer().getWorld(), Logging.CHAT)) {
consumer.queueChat(Actor.actorFromEntity(event.getPlayer()), event.getMessage());
@EventHandler(priority = EventPriority.MONITOR) }
public void onServerCommand(ServerCommandEvent event) { }
consumer.queueChat(new Actor("Console"), "/" + event.getCommand());
} @EventHandler(priority = EventPriority.MONITOR)
} public void onServerCommand(ServerCommandEvent event) {
CommandSender sender = event.getSender();
Actor actor;
if (sender instanceof BlockCommandSender) {
if (!isLogging(((BlockCommandSender) sender).getBlock().getWorld(), Logging.COMMANDBLOCK_COMMANDS)) {
return;
}
actor = new Actor("CommandBlock");
} else if (sender instanceof CommandMinecart) {
if (!isLogging(((CommandMinecart) sender).getWorld(), Logging.COMMANDBLOCK_COMMANDS)) {
return;
}
actor = new Actor("CommandMinecart");
} else {
if (!isLogging(Logging.CONSOLE_COMMANDS)) {
return;
}
actor = new Actor("Console");
}
consumer.queueChat(actor, "/" + event.getCommand());
}
}

View File

@ -1,67 +1,303 @@
package de.diddiz.LogBlock.listeners; package de.diddiz.LogBlock.listeners;
import de.diddiz.LogBlock.Actor; import de.diddiz.LogBlock.Actor;
import de.diddiz.LogBlock.LogBlock; import de.diddiz.LogBlock.LogBlock;
import de.diddiz.LogBlock.Logging; import de.diddiz.LogBlock.Logging;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.block.BlockState; import org.bukkit.Material;
import org.bukkit.block.DoubleChest; import org.bukkit.block.BlockState;
import org.bukkit.entity.HumanEntity; import org.bukkit.block.DoubleChest;
import org.bukkit.event.EventHandler; import org.bukkit.entity.HumanEntity;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventHandler;
import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.event.EventPriority;
import org.bukkit.event.inventory.InventoryOpenEvent; import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.InventoryHolder; import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import java.util.HashMap; import org.bukkit.event.inventory.InventoryOpenEvent;
import java.util.Map; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import static de.diddiz.LogBlock.config.Config.isLogging; import org.bukkit.inventory.ItemStack;
import static de.diddiz.util.BukkitUtils.*; import java.util.ArrayList;
import java.util.HashMap;
public class ChestAccessLogging extends LoggingListener { import java.util.Map;
private final Map<HumanEntity, ItemStack[]> containers = new HashMap<HumanEntity, ItemStack[]>(); import java.util.Map.Entry;
public ChestAccessLogging(LogBlock lb) { import static de.diddiz.LogBlock.config.Config.isLogging;
super(lb); import static de.diddiz.LogBlock.util.BukkitUtils.*;
}
public class ChestAccessLogging extends LoggingListener {
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) private class PlayerActiveInventoryModifications {
public void onInventoryClose(InventoryCloseEvent event) { private final HumanEntity actor;
private final Location location;
if (!isLogging(event.getPlayer().getWorld(), Logging.CHESTACCESS)) { private final HashMap<ItemStack, Integer> modifications;
return;
} public PlayerActiveInventoryModifications(HumanEntity actor, Location location) {
InventoryHolder holder = event.getInventory().getHolder(); this.actor = actor;
if (holder instanceof BlockState || holder instanceof DoubleChest) { this.location = location;
final HumanEntity player = event.getPlayer(); this.modifications = new HashMap<>();
final ItemStack[] before = containers.get(player); }
if (before != null) {
final ItemStack[] after = compressInventory(event.getInventory().getContents()); public void addModification(ItemStack stack, int amount) {
final ItemStack[] diff = compareInventories(before, after); if (amount == 0) {
final Location loc = getInventoryHolderLocation(holder); return;
for (final ItemStack item : diff) { }
consumer.queueChestAccess(Actor.actorFromEntity(player), loc, loc.getWorld().getBlockTypeIdAt(loc), (short) item.getTypeId(), (short) item.getAmount(), rawData(item)); // if we have other viewers, we have to flush their changes
} ArrayList<PlayerActiveInventoryModifications> allViewers = containersByLocation.get(location);
containers.remove(player); if (allViewers.size() > 1) {
} for (PlayerActiveInventoryModifications other : allViewers) {
} if (other != this) {
} other.flush();
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) }
public void onInventoryOpen(InventoryOpenEvent event) { }
if (!isLogging(event.getPlayer().getWorld(), Logging.CHESTACCESS)) { // consumer.getLogblock().getLogger().info("Modify container: " + stack + " change: " + amount);
return; stack = new ItemStack(stack);
} stack.setAmount(1);
if (event.getInventory() != null) { Integer existing = modifications.get(stack);
InventoryHolder holder = event.getInventory().getHolder(); int newTotal = amount + (existing == null ? 0 : existing);
if (holder instanceof BlockState || holder instanceof DoubleChest) { if (newTotal == 0) {
if (getInventoryHolderType(holder) != 58) { modifications.remove(stack);
containers.put(event.getPlayer(), compressInventory(event.getInventory().getContents())); } else {
} modifications.put(stack, newTotal);
} }
} }
}
} public void flush() {
if (!modifications.isEmpty()) {
for (Entry<ItemStack, Integer> e : modifications.entrySet()) {
ItemStack stack = e.getKey();
int amount = e.getValue();
stack.setAmount(Math.abs(amount));
// consumer.getLogblock().getLogger().info("Store container: " + stack + " take: " + (amount < 0));
consumer.queueChestAccess(Actor.actorFromEntity(actor), location, location.getWorld().getBlockAt(location).getBlockData(), stack, amount < 0);
}
modifications.clear();
}
}
public HumanEntity getActor() {
return actor;
}
public Location getLocation() {
return location;
}
}
private final Map<HumanEntity, PlayerActiveInventoryModifications> containersByOwner = new HashMap<>();
private final Map<Location, ArrayList<PlayerActiveInventoryModifications>> containersByLocation = new HashMap<>();
public ChestAccessLogging(LogBlock lb) {
super(lb);
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onInventoryClose(InventoryCloseEvent event) {
final HumanEntity player = event.getPlayer();
if (!isLogging(player.getWorld(), Logging.CHESTACCESS)) {
return;
}
InventoryHolder holder = event.getInventory().getHolder();
if (holder instanceof BlockState || holder instanceof DoubleChest) {
final PlayerActiveInventoryModifications modifications = containersByOwner.remove(player);
if (modifications != null) {
final Location loc = modifications.getLocation();
ArrayList<PlayerActiveInventoryModifications> atLocation = containersByLocation.get(loc);
atLocation.remove(modifications);
if (atLocation.isEmpty()) {
containersByLocation.remove(loc);
}
modifications.flush();
}
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onInventoryOpen(InventoryOpenEvent event) {
final HumanEntity player = event.getPlayer();
if (!isLogging(player.getWorld(), Logging.CHESTACCESS)) {
return;
}
if (event.getInventory() != null) {
InventoryHolder holder = event.getInventory().getHolder();
if (holder instanceof BlockState || holder instanceof DoubleChest) {
if (getInventoryHolderType(holder) != Material.CRAFTING_TABLE) {
PlayerActiveInventoryModifications modifications = new PlayerActiveInventoryModifications(event.getPlayer(), getInventoryHolderLocation(holder));
containersByOwner.put(modifications.getActor(), modifications);
containersByLocation.compute(modifications.getLocation(), (k, v) -> {
if (v == null) {
v = new ArrayList<>();
}
v.add(modifications);
return v;
});
}
}
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onInventoryClick(InventoryClickEvent event) {
final HumanEntity player = event.getWhoClicked();
if (!isLogging(player.getWorld(), Logging.CHESTACCESS)) {
return;
}
InventoryHolder holder = event.getInventory().getHolder();
if (holder instanceof BlockState || holder instanceof DoubleChest) {
final PlayerActiveInventoryModifications modifications = containersByOwner.get(player);
if (modifications != null) {
switch (event.getAction()) {
case PICKUP_ONE:
case DROP_ONE_SLOT:
if (event.getRawSlot() < event.getView().getTopInventory().getSize()) {
modifications.addModification(event.getCurrentItem(), -1);
}
break;
case PICKUP_HALF:
// server behaviour: round up
if (event.getRawSlot() < event.getView().getTopInventory().getSize()) {
modifications.addModification(event.getCurrentItem(), -(event.getCurrentItem().getAmount() + 1) / 2);
}
break;
case PICKUP_SOME: // oversized stack - can not take all when clicking
// server behaviour: leave a full stack in the slot, take everything else
if (event.getRawSlot() < event.getView().getTopInventory().getSize()) {
int taken = event.getCurrentItem().getAmount() - event.getCurrentItem().getMaxStackSize();
modifications.addModification(event.getCursor(), -taken);
}
break;
case PICKUP_ALL:
case DROP_ALL_SLOT:
if (event.getRawSlot() < event.getView().getTopInventory().getSize()) {
modifications.addModification(event.getCurrentItem(), -event.getCurrentItem().getAmount());
}
break;
case PLACE_ONE:
if (event.getRawSlot() < event.getView().getTopInventory().getSize()) {
modifications.addModification(event.getCursor(), 1);
}
break;
case PLACE_SOME: // not enough free place in target slot
// server behaviour: place as much as possible
if (event.getRawSlot() < event.getView().getTopInventory().getSize()) {
int placeable = event.getCurrentItem().getMaxStackSize() - event.getCurrentItem().getAmount();
modifications.addModification(event.getCursor(), placeable);
}
break;
case PLACE_ALL:
if (event.getRawSlot() < event.getView().getTopInventory().getSize()) {
modifications.addModification(event.getCursor(), event.getCursor().getAmount());
}
break;
case SWAP_WITH_CURSOR:
if (event.getRawSlot() < event.getView().getTopInventory().getSize()) {
modifications.addModification(event.getCursor(), event.getCursor().getAmount());
modifications.addModification(event.getCurrentItem(), -event.getCurrentItem().getAmount());
}
break;
case MOVE_TO_OTHER_INVENTORY: // shift + click
boolean removed = event.getRawSlot() < event.getView().getTopInventory().getSize();
modifications.addModification(event.getCurrentItem(), event.getCurrentItem().getAmount() * (removed ? -1 : 1));
break;
case COLLECT_TO_CURSOR: // double click
// server behaviour: first collect all with an amount != maxstacksize, then others, starting from slot 0 (container)
ItemStack cursor = event.getCursor();
if (cursor == null) {
return;
}
int toPickUp = cursor.getMaxStackSize() - cursor.getAmount();
int takenFromContainer = 0;
boolean takeFromFullStacks = false;
Inventory top = event.getView().getTopInventory();
Inventory bottom = event.getView().getBottomInventory();
while (toPickUp > 0) {
for (ItemStack stack : top.getStorageContents()) {
if (cursor.isSimilar(stack)) {
if (takeFromFullStacks == (stack.getAmount() == stack.getMaxStackSize())) {
int take = Math.min(toPickUp, stack.getAmount());
toPickUp -= take;
takenFromContainer += take;
if (toPickUp <= 0) {
break;
}
}
}
}
if (toPickUp <= 0) {
break;
}
for (ItemStack stack : bottom.getStorageContents()) {
if (cursor.isSimilar(stack)) {
if (takeFromFullStacks == (stack.getAmount() == stack.getMaxStackSize())) {
int take = Math.min(toPickUp, stack.getAmount());
toPickUp -= take;
if (toPickUp <= 0) {
break;
}
}
}
}
if (takeFromFullStacks) {
break;
} else {
takeFromFullStacks = true;
}
}
if (takenFromContainer > 0) {
modifications.addModification(event.getCursor(), -takenFromContainer);
}
break;
case HOTBAR_SWAP: // number key or offhand key
case HOTBAR_MOVE_AND_READD: // something was in the other slot
if (event.getRawSlot() < event.getView().getTopInventory().getSize()) {
ItemStack otherSlot = (event.getClick() == ClickType.SWAP_OFFHAND) ? event.getWhoClicked().getInventory().getItemInOffHand() : event.getWhoClicked().getInventory().getItem(event.getHotbarButton());
if (event.getCurrentItem() != null && event.getCurrentItem().getType() != Material.AIR) {
modifications.addModification(event.getCurrentItem(), -event.getCurrentItem().getAmount());
}
if (otherSlot != null && otherSlot.getType() != Material.AIR) {
modifications.addModification(otherSlot, otherSlot.getAmount());
}
}
break;
case DROP_ALL_CURSOR:
case DROP_ONE_CURSOR:
case CLONE_STACK:
case NOTHING:
// only the cursor or nothing (but not the inventory) was modified
break;
case UNKNOWN:
default:
// unable to log something we don't know
consumer.getLogblock().getLogger().warning("Unknown inventory action by " + event.getWhoClicked().getName() + ": " + event.getAction() + " Slot: " + event.getSlot() + " Slot type: " + event.getSlotType());
break;
}
}
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onInventoryDrag(InventoryDragEvent event) {
final HumanEntity player = event.getWhoClicked();
if (!isLogging(player.getWorld(), Logging.CHESTACCESS)) {
return;
}
InventoryHolder holder = event.getInventory().getHolder();
if (holder instanceof BlockState || holder instanceof DoubleChest) {
final PlayerActiveInventoryModifications modifications = containersByOwner.get(player);
if (modifications != null) {
Inventory container = event.getView().getTopInventory();
int containerSize = container.getSize();
for (Entry<Integer, ItemStack> e : event.getNewItems().entrySet()) {
int slot = e.getKey();
if (slot < containerSize) {
ItemStack old = container.getItem(slot);
int oldAmount = (old == null || old.getType() == Material.AIR) ? 0 : old.getAmount();
modifications.addModification(e.getValue(), e.getValue().getAmount() - oldAmount);
}
}
}
}
}
}

View File

@ -4,12 +4,12 @@ import de.diddiz.LogBlock.Actor;
import de.diddiz.LogBlock.LogBlock; import de.diddiz.LogBlock.LogBlock;
import de.diddiz.LogBlock.Logging; import de.diddiz.LogBlock.Logging;
import de.diddiz.LogBlock.config.WorldConfig; import de.diddiz.LogBlock.config.WorldConfig;
import de.diddiz.util.BukkitUtils; import de.diddiz.LogBlock.util.BukkitUtils;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.entity.EntityType; import org.bukkit.block.data.type.TurtleEgg;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
@ -26,34 +26,39 @@ public class CreatureInteractLogging extends LoggingListener {
public void onEntityInteract(EntityInteractEvent event) { public void onEntityInteract(EntityInteractEvent event) {
final WorldConfig wcfg = getWorldConfig(event.getEntity().getWorld()); final WorldConfig wcfg = getWorldConfig(event.getEntity().getWorld());
final EntityType entityType = event.getEntityType();
// Mobs only // Mobs only
if (event.getEntity() instanceof Player || entityType == null) { if (event.getEntity() instanceof Player) {
return; return;
} }
if (wcfg != null) { if (wcfg != null) {
final Block clicked = event.getBlock(); final Block clicked = event.getBlock();
final Material type = clicked.getType(); final Material type = clicked.getType();
final int typeId = type.getId();
final byte blockData = clicked.getData();
final Location loc = clicked.getLocation(); final Location loc = clicked.getLocation();
switch (type) { if (type == Material.FARMLAND) {
case SOIL: if (wcfg.isLogging(Logging.CREATURECROPTRAMPLE)) {
if (wcfg.isLogging(Logging.CREATURECROPTRAMPLE)) { // 3 = Dirt ID
// 3 = Dirt ID consumer.queueBlock(new Actor("CreatureTrample"), loc, type.createBlockData(), Material.DIRT.createBlockData());
consumer.queueBlock(Actor.actorFromEntity(entityType), loc, typeId, 3, blockData); // Log the crop on top as being broken
// Log the crop on top as being broken Block trampledCrop = clicked.getRelative(BlockFace.UP);
Block trampledCrop = clicked.getRelative(BlockFace.UP); if (BukkitUtils.isCropBlock(trampledCrop.getType())) {
if (BukkitUtils.getCropBlocks().contains(trampledCrop.getType())) { consumer.queueBlockBreak(new Actor("CreatureTrample"), trampledCrop.getState());
consumer.queueBlockBreak(new Actor("CreatureTrample"), trampledCrop.getState());
}
} }
break; }
} else if (type == Material.TURTLE_EGG) {
if (wcfg.isLogging(Logging.CREATURECROPTRAMPLE)) {
TurtleEgg turtleEggData = (TurtleEgg) clicked.getBlockData();
int eggs = turtleEggData.getEggs();
if (eggs > 1) {
TurtleEgg turtleEggData2 = (TurtleEgg) turtleEggData.clone();
turtleEggData2.setEggs(eggs - 1);
consumer.queueBlock(new Actor("CreatureTrample"), loc, turtleEggData, turtleEggData2);
} else {
consumer.queueBlock(new Actor("CreatureTrample"), loc, turtleEggData, Material.AIR.createBlockData());
}
}
} }
} }
} }
} }

View File

@ -0,0 +1,73 @@
package de.diddiz.LogBlock.listeners;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockFromToEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.scheduler.BukkitRunnable;
import de.diddiz.LogBlock.Actor;
import de.diddiz.LogBlock.LogBlock;
import de.diddiz.LogBlock.Logging;
import de.diddiz.LogBlock.config.Config;
import de.diddiz.LogBlock.util.LoggingUtil;
public class DragonEggLogging extends LoggingListener {
private UUID lastDragonEggInteractionPlayer;
private Location lastDragonEggInteractionLocation;
public DragonEggLogging(LogBlock lb) {
super(lb);
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerInteract(PlayerInteractEvent event) {
if (event.getAction() == Action.RIGHT_CLICK_BLOCK && event.hasBlock() && event.getClickedBlock().getType() == Material.DRAGON_EGG) {
Block block = event.getClickedBlock();
if (!Config.isLogging(block.getWorld(), Logging.DRAGONEGGTELEPORT)) {
return;
}
lastDragonEggInteractionPlayer = event.getPlayer().getUniqueId();
lastDragonEggInteractionLocation = block.getLocation();
new BukkitRunnable() {
@Override
public void run() {
lastDragonEggInteractionPlayer = null;
lastDragonEggInteractionLocation = null;
}
}.runTask(LogBlock.getInstance());
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onDragonEggTeleport(BlockFromToEvent event) {
Block block = event.getBlock();
Player teleportCause = null;
if (lastDragonEggInteractionPlayer != null && lastDragonEggInteractionLocation != null && lastDragonEggInteractionLocation.equals(block.getLocation())) {
teleportCause = Bukkit.getPlayer(lastDragonEggInteractionPlayer);
}
if (block.getType() == Material.DRAGON_EGG && Config.isLogging(block.getWorld(), Logging.DRAGONEGGTELEPORT)) {
Actor actor = new Actor("DragonEgg");
if (teleportCause != null) {
actor = Actor.actorFromEntity(teleportCause);
}
BlockData data = block.getBlockData();
consumer.queueBlockBreak(actor, block.getLocation(), data);
BlockState finalState = event.getToBlock().getState();
finalState.setBlockData(data);
LoggingUtil.smartLogBlockPlace(consumer, actor, event.getToBlock().getState(), finalState);
}
}
}

View File

@ -1,24 +1,24 @@
package de.diddiz.LogBlock.listeners; package de.diddiz.LogBlock.listeners;
import de.diddiz.LogBlock.Actor; import de.diddiz.LogBlock.Actor;
import de.diddiz.LogBlock.LogBlock; import de.diddiz.LogBlock.LogBlock;
import de.diddiz.LogBlock.Logging; import de.diddiz.LogBlock.Logging;
import org.bukkit.entity.Enderman; import org.bukkit.entity.Enderman;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.entity.EntityChangeBlockEvent; import org.bukkit.event.entity.EntityChangeBlockEvent;
import static de.diddiz.LogBlock.config.Config.isLogging; import static de.diddiz.LogBlock.config.Config.isLogging;
public class EndermenLogging extends LoggingListener { public class EndermenLogging extends LoggingListener {
public EndermenLogging(LogBlock lb) { public EndermenLogging(LogBlock lb) {
super(lb); super(lb);
} }
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onEntityChangeBlock(EntityChangeBlockEvent event) { public void onEntityChangeBlock(EntityChangeBlockEvent event) {
if (event.getEntity() instanceof Enderman && isLogging(event.getBlock().getWorld(), Logging.ENDERMEN)) { if (event.getEntity() instanceof Enderman && isLogging(event.getBlock().getWorld(), Logging.ENDERMEN)) {
consumer.queueBlockReplace(new Actor("Enderman"), event.getBlock().getState(), event.getTo().getId(), (byte) 0); // Figure out how to get the data of the placed block; consumer.queueBlockReplace(new Actor("Enderman"), event.getBlock().getState(), event.getBlockData()); // Figure out how to get the data of the placed block;
} }
} }
} }

View File

@ -1,105 +1,228 @@
package de.diddiz.LogBlock.listeners; package de.diddiz.LogBlock.listeners;
import de.diddiz.LogBlock.Actor; import de.diddiz.LogBlock.Actor;
import de.diddiz.LogBlock.LogBlock; import de.diddiz.LogBlock.LogBlock;
import de.diddiz.LogBlock.Logging; import de.diddiz.LogBlock.Logging;
import de.diddiz.LogBlock.config.WorldConfig; import de.diddiz.LogBlock.config.Config;
import org.bukkit.Material; import de.diddiz.LogBlock.config.WorldConfig;
import org.bukkit.block.Block; import de.diddiz.LogBlock.util.BukkitUtils;
import org.bukkit.block.Sign; import org.bukkit.Bukkit;
import org.bukkit.entity.*; import org.bukkit.Location;
import org.bukkit.entity.minecart.ExplosiveMinecart; import org.bukkit.Material;
import org.bukkit.event.EventHandler; import org.bukkit.block.Block;
import org.bukkit.event.EventPriority; import org.bukkit.block.data.type.RespawnAnchor;
import org.bukkit.event.entity.EntityExplodeEvent; import org.bukkit.entity.*;
import org.bukkit.projectiles.ProjectileSource; import org.bukkit.entity.minecart.ExplosiveMinecart;
import org.bukkit.event.EventHandler;
import static de.diddiz.LogBlock.config.Config.getWorldConfig; import org.bukkit.event.EventPriority;
import static de.diddiz.LogBlock.config.Config.logCreeperExplosionsAsPlayerWhoTriggeredThese; import org.bukkit.event.block.Action;
import static de.diddiz.util.BukkitUtils.getContainerBlocks; import org.bukkit.event.block.BlockExplodeEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
public class ExplosionLogging extends LoggingListener { import org.bukkit.event.player.PlayerInteractEvent;
public ExplosionLogging(LogBlock lb) { import org.bukkit.inventory.ItemStack;
super(lb); import org.bukkit.projectiles.ProjectileSource;
} import org.bukkit.scheduler.BukkitRunnable;
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) import static de.diddiz.LogBlock.config.Config.getWorldConfig;
public void onEntityExplode(EntityExplodeEvent event) { import static de.diddiz.LogBlock.config.Config.logCreeperExplosionsAsPlayerWhoTriggeredThese;
final WorldConfig wcfg = getWorldConfig(event.getLocation().getWorld());
if (wcfg != null) { import java.util.UUID;
Actor actor = new Actor("Explosion");
Entity source = event.getEntity(); public class ExplosionLogging extends LoggingListener {
if (source == null) {
if (!wcfg.isLogging(Logging.MISCEXPLOSION)) { private UUID lastBedInteractionPlayer;
return; private Location lastBedInteractionLocation;
} private UUID lastRespawnAnchorInteractionPlayer;
} else if (source instanceof TNTPrimed) { private Location lastRespawnAnchorInteractionLocation;
if (!wcfg.isLogging(Logging.TNTEXPLOSION)) {
return; public ExplosionLogging(LogBlock lb) {
} super(lb);
actor = new Actor("TNT"); }
} else if (source instanceof ExplosiveMinecart) {
if (!wcfg.isLogging(Logging.TNTMINECARTEXPLOSION)) { @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
return; public void onEntityExplode(EntityExplodeEvent event) {
} final WorldConfig wcfg = getWorldConfig(event.getLocation().getWorld());
actor = new Actor("TNTMinecart"); if (wcfg != null) {
} else if (source instanceof Creeper) { Actor actor = new Actor("Explosion");
if (!wcfg.isLogging(Logging.CREEPEREXPLOSION)) { Entity source = event.getEntity();
return; if (source == null) {
} if (!wcfg.isLogging(Logging.MISCEXPLOSION)) {
if (logCreeperExplosionsAsPlayerWhoTriggeredThese) { return;
final Entity target = ((Creeper) source).getTarget(); }
actor = target instanceof Player ? Actor.actorFromEntity(target) : new Actor("Creeper"); } else if (source instanceof TNTPrimed) {
} else { if (!wcfg.isLogging(Logging.TNTEXPLOSION)) {
new Actor("Creeper"); return;
} }
} else if (source instanceof Fireball) { actor = new Actor("TNT");
Fireball fireball = (Fireball) source; } else if (source instanceof ExplosiveMinecart) {
ProjectileSource shooter = fireball.getShooter(); if (!wcfg.isLogging(Logging.TNTMINECARTEXPLOSION)) {
if (shooter == null) { return;
return; }
} actor = new Actor("TNTMinecart");
if (shooter instanceof Ghast) { } else if (source instanceof Creeper) {
if (!wcfg.isLogging(Logging.GHASTFIREBALLEXPLOSION)) { if (!wcfg.isLogging(Logging.CREEPEREXPLOSION)) {
return; return;
} }
actor = Actor.actorFromProjectileSource(shooter); if (logCreeperExplosionsAsPlayerWhoTriggeredThese) {
} else if (shooter instanceof Wither) { final Entity target = ((Creeper) source).getTarget();
if (!wcfg.isLogging(Logging.WITHER)) { actor = target instanceof Player ? Actor.actorFromEntity(target) : new Actor("Creeper");
return; } else {
} actor = new Actor("Creeper");
actor = Actor.actorFromProjectileSource(shooter); }
} } else if (source instanceof Wither) {
} else if (source instanceof EnderDragon) { if (!wcfg.isLogging(Logging.WITHER)) {
if (!wcfg.isLogging(Logging.ENDERDRAGON)) { return;
return; }
} actor = Actor.actorFromEntity(source);
actor = Actor.actorFromEntity(source); } else if (source instanceof WitherSkull) {
} else if (source instanceof Wither) { if (!wcfg.isLogging(Logging.WITHER_SKULL)) {
if (!wcfg.isLogging(Logging.WITHER)) { return;
return; }
} actor = Actor.actorFromEntity(source);
actor = Actor.actorFromEntity(source); } else if (source instanceof Fireball) {
} else if (source instanceof WitherSkull) { Fireball fireball = (Fireball) source;
if (!wcfg.isLogging(Logging.WITHER_SKULL)) { ProjectileSource shooter = fireball.getShooter();
return; if (shooter == null) {
} if (!wcfg.isLogging(Logging.MISCEXPLOSION)) {
actor = Actor.actorFromEntity(source); return;
} else { }
if (!wcfg.isLogging(Logging.MISCEXPLOSION)) { actor = Actor.actorFromEntity(source);
return; } else if (shooter instanceof Ghast) {
} if (!wcfg.isLogging(Logging.GHASTFIREBALLEXPLOSION)) {
} return;
for (final Block block : event.blockList()) { }
final int type = block.getTypeId(); actor = Actor.actorFromProjectileSource(shooter);
if (wcfg.isLogging(Logging.SIGNTEXT) & (type == 63 || type == 68)) { } else if (shooter instanceof Wither) {
consumer.queueSignBreak(actor, (Sign) block.getState()); if (!wcfg.isLogging(Logging.WITHER)) {
} else if (wcfg.isLogging(Logging.CHESTACCESS) && (getContainerBlocks().contains(Material.getMaterial(type)))) { return;
consumer.queueContainerBreak(actor, block.getState()); }
} else { actor = Actor.actorFromProjectileSource(shooter);
consumer.queueBlockBreak(actor, block.getState()); }
} } else if (source instanceof EnderDragon) {
} if (!wcfg.isLogging(Logging.ENDERDRAGON)) {
} return;
} }
} actor = Actor.actorFromEntity(source);
} else if (source instanceof EnderCrystal) {
if (!wcfg.isLogging(Logging.ENDERCRYSTALEXPLOSION)) {
return;
}
actor = Actor.actorFromEntity(source);
} else {
if (!wcfg.isLogging(Logging.MISCEXPLOSION)) {
return;
}
}
for (final Block block : event.blockList()) {
final Material type = block.getType();
if (wcfg.isLogging(Logging.CHESTACCESS) && BukkitUtils.isContainerBlock(type) && !BukkitUtils.isShulkerBoxBlock(type)) {
consumer.queueContainerBreak(actor, block.getState());
} else {
consumer.queueBlockBreak(actor, block.getState());
}
}
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerInteract(PlayerInteractEvent event) {
if (event.getAction() == Action.RIGHT_CLICK_BLOCK && event.hasBlock()) {
Block block = event.getClickedBlock();
if (BukkitUtils.isBed(block.getType()) && !block.getWorld().isBedWorks()) {
if (!Config.isLogging(block.getWorld(), Logging.BEDEXPLOSION)) {
return;
}
lastBedInteractionPlayer = event.getPlayer().getUniqueId();
lastBedInteractionLocation = block.getLocation();
new BukkitRunnable() {
@Override
public void run() {
lastBedInteractionPlayer = null;
lastBedInteractionLocation = null;
}
}.runTask(LogBlock.getInstance());
} else if (block.getType() == Material.RESPAWN_ANCHOR && block.getBlockData() instanceof RespawnAnchor data) {
if (!Config.isLogging(block.getWorld(), Logging.RESPAWNANCHOREXPLOSION)) {
return;
}
ItemStack inHand = event.getItem();
int charges = data.getCharges();
if (charges < data.getMaximumCharges() && inHand != null && inHand.getType() == Material.GLOWSTONE) {
// charge
Actor actor = Actor.actorFromEntity(event.getPlayer());
RespawnAnchor blockNew = (RespawnAnchor) data.clone();
blockNew.setCharges(charges + 1);
consumer.queueBlockReplace(actor, block.getState(), blockNew);
} else if (charges > 0 && !block.getWorld().isRespawnAnchorWorks()) {
// explode
Actor actor = Actor.actorFromEntity(event.getPlayer());
consumer.queueBlockBreak(actor, block.getState());
lastRespawnAnchorInteractionPlayer = event.getPlayer().getUniqueId();
lastRespawnAnchorInteractionLocation = block.getLocation();
new BukkitRunnable() {
@Override
public void run() {
lastRespawnAnchorInteractionPlayer = null;
lastRespawnAnchorInteractionLocation = null;
}
}.runTask(LogBlock.getInstance());
}
}
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockExplode(BlockExplodeEvent event) {
Player bedCause = null;
if (lastBedInteractionPlayer != null && lastBedInteractionLocation != null) {
Location block = event.getBlock().getLocation();
if (lastBedInteractionLocation.getWorld() == block.getWorld() && block.distanceSquared(lastBedInteractionLocation) <= 1) {
bedCause = Bukkit.getPlayer(lastBedInteractionPlayer);
}
}
Player respawnAnchorCause = null;
if (lastRespawnAnchorInteractionPlayer != null && lastRespawnAnchorInteractionLocation != null) {
Location block = event.getBlock().getLocation();
if (lastRespawnAnchorInteractionLocation.equals(block)) {
respawnAnchorCause = Bukkit.getPlayer(lastRespawnAnchorInteractionPlayer);
}
}
for (final Block block : event.blockList()) {
final WorldConfig wcfg = getWorldConfig(block.getLocation().getWorld());
if (wcfg != null) {
Actor actor = new Actor("Explosion");
if (bedCause != null) {
if (!wcfg.isLogging(Logging.BEDEXPLOSION)) {
return;
}
if (Config.logBedExplosionsAsPlayerWhoTriggeredThese) {
actor = Actor.actorFromEntity(bedCause);
} else {
actor = new Actor("BedExplosion");
}
} else if (respawnAnchorCause != null) {
if (!wcfg.isLogging(Logging.RESPAWNANCHOREXPLOSION)) {
return;
}
if (Config.logBedExplosionsAsPlayerWhoTriggeredThese) {
actor = Actor.actorFromEntity(respawnAnchorCause);
} else {
actor = new Actor("RespawnAnchorExplosion");
}
} else if (!wcfg.isLogging(Logging.MISCEXPLOSION)) {
return;
}
final Material type = block.getType();
if (wcfg.isLogging(Logging.CHESTACCESS) && BukkitUtils.isContainerBlock(type) && !BukkitUtils.isShulkerBoxBlock(type)) {
consumer.queueContainerBreak(actor, block.getState());
} else {
consumer.queueBlockBreak(actor, block.getState());
}
}
}
}
}

View File

@ -1,83 +1,127 @@
package de.diddiz.LogBlock.listeners; package de.diddiz.LogBlock.listeners;
import de.diddiz.LogBlock.Actor; import de.diddiz.LogBlock.Actor;
import de.diddiz.LogBlock.LogBlock; import de.diddiz.LogBlock.LogBlock;
import de.diddiz.LogBlock.Logging; import de.diddiz.LogBlock.Logging;
import de.diddiz.LogBlock.config.WorldConfig; import de.diddiz.LogBlock.config.Config;
import org.bukkit.block.Block; import de.diddiz.LogBlock.config.WorldConfig;
import org.bukkit.block.BlockFace; import de.diddiz.LogBlock.util.BukkitUtils;
import org.bukkit.event.EventHandler; import org.bukkit.Material;
import org.bukkit.event.EventPriority; import org.bukkit.block.Block;
import org.bukkit.event.block.BlockFromToEvent; import org.bukkit.block.BlockFace;
import org.bukkit.block.data.BlockData;
import java.util.Arrays; import org.bukkit.block.data.Levelled;
import java.util.HashSet; import org.bukkit.block.data.Waterlogged;
import java.util.Set; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import static de.diddiz.LogBlock.config.Config.getWorldConfig; import org.bukkit.event.block.BlockFormEvent;
import org.bukkit.event.block.BlockFromToEvent;
public class FluidFlowLogging extends LoggingListener {
private static final Set<Integer> nonFluidProofBlocks = new HashSet<Integer>(Arrays.asList(27, 28, 31, 32, 37, 38, 39, 40, 50, 51, 55, 59, 66, 69, 70, 75, 76, 78, 93, 94, 104, 105, 106)); import static de.diddiz.LogBlock.config.Config.getWorldConfig;
public FluidFlowLogging(LogBlock lb) { public class FluidFlowLogging extends LoggingListener {
super(lb);
} public FluidFlowLogging(LogBlock lb) {
super(lb);
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) }
public void onBlockFromTo(BlockFromToEvent event) {
final WorldConfig wcfg = getWorldConfig(event.getBlock().getWorld()); @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
if (wcfg != null) { public void onBlockFromTo(BlockFromToEvent event) {
final Block to = event.getToBlock(); final WorldConfig wcfg = getWorldConfig(event.getBlock().getWorld());
final int typeFrom = event.getBlock().getTypeId(); if (wcfg != null && (wcfg.isLogging(Logging.WATERFLOW) || wcfg.isLogging(Logging.LAVAFLOW))) {
final int typeTo = to.getTypeId(); final BlockData blockDataFrom = event.getBlock().getBlockData();
final boolean canFlow = typeTo == 0 || nonFluidProofBlocks.contains(typeTo); Material typeFrom = blockDataFrom.getMaterial();
if (typeFrom == 10 || typeFrom == 11) { boolean fromWaterlogged = false;
if (canFlow && wcfg.isLogging(Logging.LAVAFLOW)) { if (blockDataFrom instanceof Waterlogged) {
if (isSurroundedByWater(to) && event.getBlock().getData() <= 2) { typeFrom = Material.WATER;
consumer.queueBlockReplace(new Actor("LavaFlow"), to.getState(), 4, (byte) 0); fromWaterlogged = true;
} else if (typeTo == 0) { }
consumer.queueBlockPlace(new Actor("LavaFlow"), to.getLocation(), 10, (byte) (event.getBlock().getData() + 1)); if (typeFrom == Material.SEAGRASS || typeFrom == Material.KELP_PLANT || typeFrom == Material.KELP) {
} else { typeFrom = Material.WATER;
consumer.queueBlockReplace(new Actor("LavaFlow"), to.getState(), 10, (byte) (event.getBlock().getData() + 1)); fromWaterlogged = true;
} }
} else if (typeTo == 8 || typeTo == 9) {
if (event.getFace() == BlockFace.DOWN) { Block source = Config.logFluidFlowAsPlayerWhoTriggeredIt ? event.getBlock() : null;
consumer.queueBlockReplace(new Actor("LavaFlow"), to.getState(), 1, (byte) 0); final Block to = event.getToBlock();
} else { final Material typeTo = to.getType();
consumer.queueBlockReplace(new Actor("LavaFlow"), to.getState(), 4, (byte) 0); boolean down = event.getFace() == BlockFace.DOWN;
} final boolean canFlow = BukkitUtils.isEmpty(typeTo) || BukkitUtils.isNonFluidProofBlock(typeTo);
} if (typeFrom == Material.LAVA && wcfg.isLogging(Logging.LAVAFLOW)) {
} else if ((typeFrom == 8 || typeFrom == 9) && wcfg.isLogging(Logging.WATERFLOW)) { Levelled levelledFrom = (Levelled) blockDataFrom;
if (typeTo == 0) { if (canFlow) {
consumer.queueBlockPlace(new Actor("WaterFlow"), to.getLocation(), 8, (byte) (event.getBlock().getData() + 1)); if (isSurroundedByWater(to) && levelledFrom.getLevel() <= 2) {
} else if (nonFluidProofBlocks.contains(typeTo)) { consumer.queueBlockReplace(new Actor("LavaFlow", source), to.getState(), Material.COBBLESTONE.createBlockData());
consumer.queueBlockReplace(new Actor("WaterFlow"), to.getState(), 8, (byte) (event.getBlock().getData() + 1)); } else {
} else if (typeTo == 10 || typeTo == 11) { Levelled newBlock = (Levelled) blockDataFrom.clone();
if (to.getData() == 0) { newBlock.setLevel(down ? 1 : Math.min(levelledFrom.getLevel() + 1, levelledFrom.getMaximumLevel()));
consumer.queueBlockReplace(new Actor("WaterFlow"), to.getState(), 49, (byte) 0); if (BukkitUtils.isEmpty(typeTo)) {
} else if (event.getFace() == BlockFace.DOWN) { consumer.queueBlockPlace(new Actor("LavaFlow", source), to.getLocation(), newBlock);
consumer.queueBlockReplace(new Actor("LavaFlow"), to.getState(), 1, (byte) 0); } else {
} consumer.queueBlockReplace(new Actor("LavaFlow", source), to.getState(), newBlock);
} }
if (typeTo == 0 || nonFluidProofBlocks.contains(typeTo)) { }
for (final BlockFace face : new BlockFace[]{BlockFace.DOWN, BlockFace.NORTH, BlockFace.WEST, BlockFace.EAST, BlockFace.SOUTH}) { } else if (typeTo == Material.WATER) {
final Block lower = to.getRelative(face); if (down) {
if (lower.getTypeId() == 10 || lower.getTypeId() == 11) { consumer.queueBlockReplace(new Actor("LavaFlow", source), to.getState(), Material.STONE.createBlockData());
consumer.queueBlockReplace(new Actor("WaterFlow"), lower.getState(), lower.getData() == 0 ? 49 : 4, (byte) 0); } else {
} consumer.queueBlockReplace(new Actor("LavaFlow", source), to.getState(), Material.COBBLESTONE.createBlockData());
} }
} }
} } else if ((typeFrom == Material.WATER) && wcfg.isLogging(Logging.WATERFLOW)) {
} Levelled levelledFrom = fromWaterlogged ? null : (Levelled) blockDataFrom;
} Levelled newBlock = (Levelled) Material.WATER.createBlockData();
newBlock.setLevel(fromWaterlogged || down ? 1 : Math.min(levelledFrom.getLevel() + 1, levelledFrom.getMaximumLevel()));
private static boolean isSurroundedByWater(Block block) { if (BukkitUtils.isEmpty(typeTo)) {
for (final BlockFace face : new BlockFace[]{BlockFace.NORTH, BlockFace.WEST, BlockFace.EAST, BlockFace.SOUTH}) { consumer.queueBlockPlace(new Actor("WaterFlow", source), to.getLocation(), newBlock);
final int type = block.getRelative(face).getTypeId(); } else if (BukkitUtils.isNonFluidProofBlock(typeTo)) {
if (type == 8 || type == 9) { consumer.queueBlockReplace(new Actor("WaterFlow", source), to.getState(), newBlock);
return true; } else if (typeTo == Material.LAVA) {
} int toLevel = ((Levelled) to.getBlockData()).getLevel();
} if (toLevel == 0) {
return false; consumer.queueBlockReplace(new Actor("WaterFlow", source), to.getState(), Material.OBSIDIAN.createBlockData());
} } else if (event.getFace() == BlockFace.DOWN) {
} consumer.queueBlockReplace(new Actor("WaterFlow", source), to.getState(), Material.STONE.createBlockData());
}
}
if (BukkitUtils.isEmpty(typeTo) || BukkitUtils.isNonFluidProofBlock(typeTo)) {
for (final BlockFace face : new BlockFace[] { BlockFace.DOWN, BlockFace.NORTH, BlockFace.WEST, BlockFace.EAST, BlockFace.SOUTH }) {
final Block lower = to.getRelative(face);
if (lower.getType() == Material.LAVA) {
int toLevel = ((Levelled) lower.getBlockData()).getLevel();
if (toLevel == 0) {
consumer.queueBlockReplace(new Actor("WaterFlow", source), lower.getState(), Material.OBSIDIAN.createBlockData());
} else if (event.getFace() == BlockFace.DOWN) {
consumer.queueBlockReplace(new Actor("WaterFlow", source), lower.getState(), Material.STONE.createBlockData());
}
}
}
}
}
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockForm(BlockFormEvent event) {
final WorldConfig wcfg = getWorldConfig(event.getBlock().getWorld());
if (wcfg != null && (wcfg.isLogging(Logging.WATERFLOW) || wcfg.isLogging(Logging.LAVAFLOW))) {
if (wcfg.isLogging(Logging.LAVAFLOW) && event.getBlock().getType() == Material.WATER && event.getNewState().getType() == Material.COBBLESTONE) {
consumer.queueBlockReplace(new Actor("LavaFlow"), event.getBlock().getBlockData(), event.getNewState());
}
if (wcfg.isLogging(Logging.WATERFLOW) && event.getBlock().getType() == Material.LAVA) {
consumer.queueBlockReplace(new Actor("WaterFlow"), event.getBlock().getBlockData(), event.getNewState());
}
if (wcfg.isLogging(Logging.WATERFLOW) && BukkitUtils.isConcreteBlock(event.getNewState().getType())) {
consumer.queueBlockReplace(new Actor("WaterFlow"), event.getBlock().getBlockData(), event.getNewState());
}
}
}
private static boolean isSurroundedByWater(Block block) {
for (final BlockFace face : new BlockFace[] { BlockFace.NORTH, BlockFace.WEST, BlockFace.EAST, BlockFace.SOUTH }) {
if (block.getRelative(face).getType() == Material.WATER) {
return true;
}
}
return false;
}
}

View File

@ -1,103 +1,274 @@
package de.diddiz.LogBlock.listeners; package de.diddiz.LogBlock.listeners;
import de.diddiz.LogBlock.Actor; import de.diddiz.LogBlock.Actor;
import de.diddiz.LogBlock.LogBlock; import de.diddiz.LogBlock.LogBlock;
import de.diddiz.LogBlock.Logging; import de.diddiz.LogBlock.Logging;
import de.diddiz.LogBlock.config.WorldConfig; import de.diddiz.LogBlock.config.WorldConfig;
import de.diddiz.util.BukkitUtils; import de.diddiz.LogBlock.util.BukkitUtils;
import org.bukkit.Location; import java.util.UUID;
import org.bukkit.Material; import org.bukkit.DyeColor;
import org.bukkit.block.Block; import org.bukkit.GameEvent;
import org.bukkit.block.BlockFace; import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.Material;
import org.bukkit.event.EventHandler; import org.bukkit.Note;
import org.bukkit.event.EventPriority; import org.bukkit.Note.Tone;
import org.bukkit.event.block.Action; import org.bukkit.block.Block;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import static de.diddiz.LogBlock.config.Config.getWorldConfig; import org.bukkit.block.Sign;
import org.bukkit.block.data.BlockData;
public class InteractLogging extends LoggingListener { import org.bukkit.block.data.Directional;
public InteractLogging(LogBlock lb) { import org.bukkit.block.data.Lightable;
super(lb); import org.bukkit.block.data.Openable;
} import org.bukkit.block.data.type.Cake;
import org.bukkit.block.data.type.Candle;
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) import org.bukkit.block.data.type.Comparator;
public void onPlayerInteract(PlayerInteractEvent event) { import org.bukkit.block.data.type.Comparator.Mode;
final WorldConfig wcfg = getWorldConfig(event.getPlayer().getWorld()); import org.bukkit.block.data.type.DaylightDetector;
if (wcfg != null) { import org.bukkit.block.data.type.Door;
final Block clicked = event.getClickedBlock(); import org.bukkit.block.data.type.NoteBlock;
if (clicked == null) { import org.bukkit.block.data.type.Repeater;
return; import org.bukkit.block.data.type.Switch;
} import org.bukkit.block.data.type.TurtleEgg;
final Material type = clicked.getType(); import org.bukkit.block.sign.Side;
final int typeId = type.getId(); import org.bukkit.block.sign.SignSide;
final byte blockData = clicked.getData(); import org.bukkit.entity.Player;
final Player player = event.getPlayer(); import org.bukkit.event.Event.Result;
final Location loc = clicked.getLocation(); import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
switch (type) { import org.bukkit.event.block.Action;
case LEVER: import org.bukkit.event.player.PlayerInteractEvent;
case WOOD_BUTTON: import org.bukkit.event.world.GenericGameEvent;
case STONE_BUTTON: import org.bukkit.inventory.ItemStack;
if (wcfg.isLogging(Logging.SWITCHINTERACT) && event.getAction() == Action.RIGHT_CLICK_BLOCK) {
consumer.queueBlock(Actor.actorFromEntity(player), loc, typeId, typeId, blockData); import static de.diddiz.LogBlock.config.Config.getWorldConfig;
}
break; public class InteractLogging extends LoggingListener {
case FENCE_GATE: public InteractLogging(LogBlock lb) {
case WOODEN_DOOR: super(lb);
case TRAP_DOOR: }
if (wcfg.isLogging(Logging.DOORINTERACT) && event.getAction() == Action.RIGHT_CLICK_BLOCK) {
consumer.queueBlock(Actor.actorFromEntity(player), loc, typeId, typeId, blockData); private UUID lastInteractionPlayer;
} private BlockData lastInteractionBlockData;
break; private Location lastInteractionLocation;
case CAKE_BLOCK:
if (wcfg.isLogging(Logging.CAKEEAT) && event.getAction() == Action.RIGHT_CLICK_BLOCK && player.getFoodLevel() < 20) { @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
consumer.queueBlock(Actor.actorFromEntity(player), loc, typeId, typeId, blockData); public void onPlayerInteract(PlayerInteractEvent event) {
} final WorldConfig wcfg = getWorldConfig(event.getPlayer().getWorld());
break; if (wcfg != null) {
case NOTE_BLOCK: final Block clicked = event.getClickedBlock();
if (wcfg.isLogging(Logging.NOTEBLOCKINTERACT) && event.getAction() == Action.RIGHT_CLICK_BLOCK) { if (clicked == null) {
consumer.queueBlock(Actor.actorFromEntity(player), loc, typeId, typeId, blockData); return;
} }
break; final BlockData blockData = clicked.getBlockData();
case DIODE_BLOCK_OFF: final Material type = blockData.getMaterial();
case DIODE_BLOCK_ON: final Player player = event.getPlayer();
if (wcfg.isLogging(Logging.DIODEINTERACT) && event.getAction() == Action.RIGHT_CLICK_BLOCK) { final Location loc = clicked.getLocation();
consumer.queueBlock(Actor.actorFromEntity(player), loc, typeId, typeId, blockData); lastInteractionPlayer = player.getUniqueId();
} lastInteractionBlockData = blockData;
break; lastInteractionLocation = loc;
case REDSTONE_COMPARATOR_OFF:
case REDSTONE_COMPARATOR_ON: if (BukkitUtils.isFenceGate(type) || BukkitUtils.isWoodenTrapdoor(type)) {
if (wcfg.isLogging(Logging.COMPARATORINTERACT) && event.getAction() == Action.RIGHT_CLICK_BLOCK) { if (wcfg.isLogging(Logging.DOORINTERACT) && event.getAction() == Action.RIGHT_CLICK_BLOCK) {
consumer.queueBlock(Actor.actorFromEntity(player), loc, typeId, typeId, blockData); Openable newBlockData = (Openable) blockData.clone();
} newBlockData.setOpen(!newBlockData.isOpen());
break; consumer.queueBlock(Actor.actorFromEntity(player), loc, blockData, newBlockData);
case WOOD_PLATE: }
case STONE_PLATE: } else if (BukkitUtils.isPressurePlate(type)) {
case IRON_PLATE: if (wcfg.isLogging(Logging.PRESUREPLATEINTERACT) && event.getAction() == Action.PHYSICAL) {
case GOLD_PLATE: consumer.queueBlock(Actor.actorFromEntity(player), loc, blockData, blockData);
if (wcfg.isLogging(Logging.PRESUREPLATEINTERACT) && event.getAction() == Action.PHYSICAL) { }
consumer.queueBlock(Actor.actorFromEntity(player), loc, typeId, typeId, blockData); } else if (BukkitUtils.isWoodenDoor(type)) {
} if (wcfg.isLogging(Logging.DOORINTERACT) && event.getAction() == Action.RIGHT_CLICK_BLOCK) {
break; Door newBlockData = (Door) blockData.clone();
case TRIPWIRE: newBlockData.setOpen(!newBlockData.isOpen());
if (wcfg.isLogging(Logging.TRIPWIREINTERACT) && event.getAction() == Action.PHYSICAL) { consumer.queueBlock(Actor.actorFromEntity(player), loc, blockData, newBlockData);
consumer.queueBlock(Actor.actorFromEntity(player), loc, typeId, typeId, blockData); }
} } else if (BukkitUtils.isButton(type) || type == Material.LEVER) {
break; if (wcfg.isLogging(Logging.SWITCHINTERACT) && event.getAction() == Action.RIGHT_CLICK_BLOCK) {
case SOIL: Switch newBlockData = (Switch) blockData.clone();
if (wcfg.isLogging(Logging.CROPTRAMPLE) && event.getAction() == Action.PHYSICAL) { if (!newBlockData.isPowered() || type == Material.LEVER) {
// 3 = Dirt ID newBlockData.setPowered(!newBlockData.isPowered());
consumer.queueBlock(Actor.actorFromEntity(player), loc, typeId, 3, blockData); }
// Log the crop on top as being broken consumer.queueBlock(Actor.actorFromEntity(player), loc, blockData, newBlockData);
Block trampledCrop = clicked.getRelative(BlockFace.UP); }
if (BukkitUtils.getCropBlocks().contains(trampledCrop.getType())) { } else if (BukkitUtils.isSign(type)) {
consumer.queueBlockBreak(Actor.actorFromEntity(player), trampledCrop.getState()); if (wcfg.isLogging(Logging.SIGNTEXT) && event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getItem() != null) {
} Material itemType = event.getItem().getType();
} if (BukkitUtils.isDye(itemType) || itemType == Material.GLOW_INK_SAC || itemType == Material.INK_SAC || itemType == Material.HONEYCOMB) {
break; final BlockState before = event.getClickedBlock().getState();
} if (before instanceof Sign signBefore) {
} if (!signBefore.isWaxed()) {
} final Sign signAfter = (Sign) event.getClickedBlock().getState();
} Side side = BukkitUtils.getFacingSignSide(player, clicked);
SignSide signSideBefore = signBefore.getSide(side);
SignSide signSideAfter = signAfter.getSide(side);
if (itemType == Material.GLOW_INK_SAC) {
if (!signSideBefore.isGlowingText() && hasText(signSideBefore)) {
signSideAfter.setGlowingText(true);
consumer.queueBlockReplace(Actor.actorFromEntity(player), signBefore, signAfter);
}
} else if (itemType == Material.INK_SAC) {
if (signSideBefore.isGlowingText() && hasText(signSideBefore)) {
signSideAfter.setGlowingText(false);
consumer.queueBlockReplace(Actor.actorFromEntity(player), signBefore, signAfter);
}
} else if (itemType == Material.HONEYCOMB) {
signAfter.setWaxed(true);
consumer.queueBlockReplace(Actor.actorFromEntity(player), signBefore, signAfter);
} else if (BukkitUtils.isDye(itemType) && hasText(signSideBefore)) {
DyeColor newColor = BukkitUtils.dyeToDyeColor(itemType);
if (newColor != null && signSideBefore.getColor() != newColor) {
signSideAfter.setColor(newColor);
consumer.queueBlockReplace(Actor.actorFromEntity(player), signBefore, signAfter);
}
}
}
}
}
}
} else if (type == Material.CAKE) {
if (event.hasItem() && BukkitUtils.isCandle(event.getItem().getType()) && event.useItemInHand() != Result.DENY) {
BlockData newBlockData = Material.valueOf(event.getItem().getType().name() + "_CAKE").createBlockData();
consumer.queueBlock(Actor.actorFromEntity(player), loc, blockData, newBlockData);
} else if (wcfg.isLogging(Logging.CAKEEAT) && event.getAction() == Action.RIGHT_CLICK_BLOCK && player.getFoodLevel() < 20) {
Cake newBlockData = (Cake) blockData.clone();
if (newBlockData.getBites() < 6) {
newBlockData.setBites(newBlockData.getBites() + 1);
consumer.queueBlock(Actor.actorFromEntity(player), loc, blockData, newBlockData);
} else {
consumer.queueBlock(Actor.actorFromEntity(player), loc, blockData, Material.AIR.createBlockData());
}
}
} else if (type == Material.NOTE_BLOCK) {
if (wcfg.isLogging(Logging.NOTEBLOCKINTERACT) && event.getAction() == Action.RIGHT_CLICK_BLOCK) {
NoteBlock newBlockData = (NoteBlock) blockData.clone();
if (newBlockData.getNote().getOctave() == 2) {
newBlockData.setNote(new Note(0, Tone.F, true));
} else {
newBlockData.setNote(newBlockData.getNote().sharped());
}
consumer.queueBlock(Actor.actorFromEntity(player), loc, blockData, newBlockData);
}
} else if (type == Material.REPEATER) {
if (wcfg.isLogging(Logging.DIODEINTERACT) && event.getAction() == Action.RIGHT_CLICK_BLOCK) {
Repeater newBlockData = (Repeater) blockData.clone();
newBlockData.setDelay((newBlockData.getDelay() % 4) + 1);
consumer.queueBlock(Actor.actorFromEntity(player), loc, blockData, newBlockData);
}
} else if (type == Material.COMPARATOR) {
if (wcfg.isLogging(Logging.COMPARATORINTERACT) && event.getAction() == Action.RIGHT_CLICK_BLOCK) {
Comparator newBlockData = (Comparator) blockData.clone();
newBlockData.setMode(newBlockData.getMode() == Mode.COMPARE ? Mode.SUBTRACT : Mode.COMPARE);
consumer.queueBlock(Actor.actorFromEntity(player), loc, blockData, newBlockData);
}
} else if (type == Material.DAYLIGHT_DETECTOR) {
if (wcfg.isLogging(Logging.DAYLIGHTDETECTORINTERACT) && event.getAction() == Action.RIGHT_CLICK_BLOCK) {
DaylightDetector newBlockData = (DaylightDetector) blockData.clone();
newBlockData.setInverted(!newBlockData.isInverted());
consumer.queueBlock(Actor.actorFromEntity(player), loc, blockData, newBlockData);
}
} else if (type == Material.TRIPWIRE) {
if (wcfg.isLogging(Logging.TRIPWIREINTERACT) && event.getAction() == Action.PHYSICAL) {
consumer.queueBlock(Actor.actorFromEntity(player), loc, blockData, blockData);
}
} else if (type == Material.FARMLAND) {
if (wcfg.isLogging(Logging.CROPTRAMPLE) && event.getAction() == Action.PHYSICAL) {
// 3 = Dirt ID
consumer.queueBlock(Actor.actorFromEntity(player), loc, blockData, Material.DIRT.createBlockData());
// Log the crop on top as being broken
Block trampledCrop = clicked.getRelative(BlockFace.UP);
if (BukkitUtils.isCropBlock(trampledCrop.getType())) {
consumer.queueBlockBreak(Actor.actorFromEntity(player), trampledCrop.getState());
}
}
} else if (type == Material.TURTLE_EGG) {
if (wcfg.isLogging(Logging.BLOCKBREAK) && event.getAction() == Action.PHYSICAL) {
TurtleEgg turtleEggData = (TurtleEgg) blockData;
int eggs = turtleEggData.getEggs();
if (eggs > 1) {
TurtleEgg turtleEggData2 = (TurtleEgg) turtleEggData.clone();
turtleEggData2.setEggs(eggs - 1);
consumer.queueBlock(Actor.actorFromEntity(player), loc, turtleEggData, turtleEggData2);
} else {
consumer.queueBlock(Actor.actorFromEntity(player), loc, turtleEggData, Material.AIR.createBlockData());
}
}
} else if (type == Material.PUMPKIN) {
if ((wcfg.isLogging(Logging.BLOCKBREAK) || wcfg.isLogging(Logging.BLOCKPLACE)) && event.getAction() == Action.RIGHT_CLICK_BLOCK) {
ItemStack inHand = event.getItem();
if (inHand != null && inHand.getType() == Material.SHEARS) {
BlockFace clickedFace = event.getBlockFace();
Directional newBlockData = (Directional) Material.CARVED_PUMPKIN.createBlockData();
if (clickedFace == BlockFace.NORTH || clickedFace == BlockFace.SOUTH || clickedFace == BlockFace.EAST || clickedFace == BlockFace.WEST) {
newBlockData.setFacing(clickedFace);
} else {
// use player distance to calculate the facing
Location playerLoc = player.getLocation();
playerLoc.subtract(0.5, 0, 0.5);
double dx = playerLoc.getX() - loc.getX();
double dz = playerLoc.getZ() - loc.getZ();
if (Math.abs(dx) > Math.abs(dz)) {
newBlockData.setFacing(dx > 0 ? BlockFace.EAST : BlockFace.WEST);
} else {
newBlockData.setFacing(dz > 0 ? BlockFace.SOUTH : BlockFace.NORTH);
}
}
consumer.queueBlock(Actor.actorFromEntity(player), loc, blockData, newBlockData);
}
}
}
}
}
private boolean hasText(SignSide signSide) {
for (int i = 0; i < 4; i++) {
if (!signSide.getLine(i).isEmpty()) {
return true;
}
}
return false;
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onGenericGameEvent(GenericGameEvent event) {
if (lastInteractionPlayer != null && event.getEntity() != null && event.getEntity().getUniqueId().equals(lastInteractionPlayer) && lastInteractionLocation != null && event.getLocation().equals(lastInteractionLocation)) {
if (lastInteractionBlockData instanceof Candle) {
Candle previousCandle = (Candle) lastInteractionBlockData;
if (previousCandle.isLit()) {
BlockData newData = lastInteractionLocation.getBlock().getBlockData();
if (newData instanceof Candle) {
Candle newCandle = (Candle) newData;
if (!newCandle.isLit() && !newCandle.isWaterlogged()) {
// log candle extinguish
consumer.queueBlockReplace(Actor.actorFromEntity(event.getEntity()), lastInteractionLocation, lastInteractionBlockData, newData);
}
}
}
} else if (lastInteractionBlockData instanceof Lightable && BukkitUtils.isCandleCake(lastInteractionBlockData.getMaterial())) {
Lightable previousLightable = (Lightable) lastInteractionBlockData;
BlockData newData = lastInteractionLocation.getBlock().getBlockData();
if (event.getEvent().equals(GameEvent.EAT)) {
final WorldConfig wcfg = getWorldConfig(event.getLocation().getWorld());
if (wcfg.isLogging(Logging.CAKEEAT)) {
// nom nom (don't know why newData is incorrect here)
newData = Material.CAKE.createBlockData();
((Cake) newData).setBites(1);
consumer.queueBlockReplace(Actor.actorFromEntity(event.getEntity()), lastInteractionLocation, lastInteractionBlockData, newData);
}
} else if (previousLightable.isLit()) {
if (newData instanceof Lightable) {
Lightable newLightable = (Lightable) newData;
if (!newLightable.isLit()) {
// log cake extinguish
consumer.queueBlockReplace(Actor.actorFromEntity(event.getEntity()), lastInteractionLocation, lastInteractionBlockData, newData);
}
}
}
}
}
lastInteractionPlayer = null;
lastInteractionBlockData = null;
lastInteractionLocation = null;
}
}

View File

@ -1,50 +1,51 @@
package de.diddiz.LogBlock.listeners; package de.diddiz.LogBlock.listeners;
import de.diddiz.LogBlock.Actor; import de.diddiz.LogBlock.Actor;
import de.diddiz.LogBlock.LogBlock; import de.diddiz.LogBlock.LogBlock;
import de.diddiz.LogBlock.Logging; import de.diddiz.LogBlock.Logging;
import de.diddiz.LogBlock.config.Config.*; import de.diddiz.LogBlock.config.Config.*;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Monster; import org.bukkit.entity.Monster;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDeathEvent; import org.bukkit.event.entity.EntityDeathEvent;
import static de.diddiz.LogBlock.config.Config.*; import static de.diddiz.LogBlock.config.Config.*;
public class KillLogging extends LoggingListener {
public class KillLogging extends LoggingListener {
public KillLogging(LogBlock lb) {
public KillLogging(LogBlock lb) { super(lb);
super(lb); }
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onEntityDeath(EntityDeathEvent deathEvent) {
public void onEntityDeath(EntityDeathEvent deathEvent) { EntityDamageEvent event = deathEvent.getEntity().getLastDamageCause();
EntityDamageEvent event = deathEvent.getEntity().getLastDamageCause(); // For a death event, there should always be a damage event and it should not be cancelled. Check anyway.
// For a death event, there should always be a damage event and it should not be cancelled. Check anyway. if (event != null && event.isCancelled() == false && isLogging(event.getEntity().getWorld(), Logging.KILL) && event.getEntity() instanceof LivingEntity) {
if (event != null && event.isCancelled() == false && isLogging(event.getEntity().getWorld(), Logging.KILL) && event.getEntity() instanceof LivingEntity) { final LivingEntity victim = (LivingEntity) event.getEntity();
final LivingEntity victim = (LivingEntity) event.getEntity(); if (event instanceof EntityDamageByEntityEvent) {
if (event instanceof EntityDamageByEntityEvent) { final Entity killer = ((EntityDamageByEntityEvent) event).getDamager();
final Entity killer = ((EntityDamageByEntityEvent) event).getDamager(); if (logKillsLevel == LogKillsLevel.PLAYERS && !(victim instanceof Player && killer instanceof Player)) {
if (logKillsLevel == LogKillsLevel.PLAYERS && !(victim instanceof Player && killer instanceof Player)) { return;
return; } else if (logKillsLevel == LogKillsLevel.MONSTERS && !((victim instanceof Player || victim instanceof Monster) && killer instanceof Player || killer instanceof Monster)) {
} else if (logKillsLevel == LogKillsLevel.MONSTERS && !((victim instanceof Player || victim instanceof Monster) && killer instanceof Player || killer instanceof Monster)) { return;
return; }
} consumer.queueKill(killer, victim);
consumer.queueKill(killer, victim); } else if (deathEvent.getEntity().getKiller() != null) {
} else if (logEnvironmentalKills) { consumer.queueKill(deathEvent.getEntity().getKiller(), victim);
if (logKillsLevel == LogKillsLevel.PLAYERS && !(victim instanceof Player)) { } else if (logEnvironmentalKills) {
return; if (logKillsLevel == LogKillsLevel.PLAYERS && !(victim instanceof Player)) {
} else if (logKillsLevel == LogKillsLevel.MONSTERS && !((victim instanceof Player || victim instanceof Monster))) { return;
return; } else if (logKillsLevel == LogKillsLevel.MONSTERS && !((victim instanceof Player || victim instanceof Monster))) {
} return;
consumer.queueKill(new Actor(event.getCause().toString()), victim); }
} consumer.queueKill(new Actor(event.getCause().toString()), victim);
} }
} }
} }
}

View File

@ -1,26 +1,26 @@
package de.diddiz.LogBlock.listeners; package de.diddiz.LogBlock.listeners;
import de.diddiz.LogBlock.Actor; import de.diddiz.LogBlock.Actor;
import de.diddiz.LogBlock.LogBlock; import de.diddiz.LogBlock.LogBlock;
import de.diddiz.LogBlock.Logging; import de.diddiz.LogBlock.Logging;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.block.LeavesDecayEvent; import org.bukkit.event.block.LeavesDecayEvent;
import static de.diddiz.LogBlock.config.Config.isLogging; import static de.diddiz.LogBlock.config.Config.isLogging;
import static de.diddiz.util.LoggingUtil.smartLogBlockBreak; import static de.diddiz.LogBlock.util.LoggingUtil.smartLogBlockBreak;
import static de.diddiz.util.LoggingUtil.smartLogFallables; import static de.diddiz.LogBlock.util.LoggingUtil.smartLogFallables;
public class LeavesDecayLogging extends LoggingListener { public class LeavesDecayLogging extends LoggingListener {
public LeavesDecayLogging(LogBlock lb) { public LeavesDecayLogging(LogBlock lb) {
super(lb); super(lb);
} }
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onLeavesDecay(LeavesDecayEvent event) { public void onLeavesDecay(LeavesDecayEvent event) {
if (isLogging(event.getBlock().getWorld(), Logging.LEAVESDECAY)) { if (isLogging(event.getBlock().getWorld(), Logging.LEAVESDECAY)) {
smartLogBlockBreak(consumer, new Actor("LeavesDecay"), event.getBlock()); smartLogBlockBreak(consumer, new Actor("LeavesDecay"), event.getBlock());
smartLogFallables(consumer, new Actor("LeavesDecay"), event.getBlock()); smartLogFallables(consumer, new Actor("LeavesDecay"), event.getBlock());
} }
} }
} }

View File

@ -0,0 +1,67 @@
package de.diddiz.LogBlock.listeners;
import static de.diddiz.LogBlock.config.Config.getWorldConfig;
import de.diddiz.LogBlock.Actor;
import de.diddiz.LogBlock.LogBlock;
import de.diddiz.LogBlock.Logging;
import de.diddiz.LogBlock.config.WorldConfig;
import org.bukkit.Material;
import org.bukkit.block.BlockState;
import org.bukkit.block.Lectern;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.player.PlayerTakeLecternBookEvent;
import org.bukkit.inventory.ItemStack;
public class LecternLogging extends LoggingListener {
public LecternLogging(LogBlock lb) {
super(lb);
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockPlace(BlockPlaceEvent event) {
final WorldConfig wcfg = getWorldConfig(event.getPlayer().getWorld());
if (wcfg != null && wcfg.isLogging(Logging.LECTERNBOOKCHANGE)) {
final BlockState before = event.getBlockReplacedState();
final BlockState after = event.getBlockPlaced().getState();
if (before.getType() == Material.LECTERN && after.getType() == Material.LECTERN) {
Lectern lecternBefore = (Lectern) before.getBlock().getState();
ItemStack book = lecternBefore.getSnapshotInventory().getItem(0);
try {
lecternBefore.getSnapshotInventory().setItem(0, null);
} catch (NullPointerException e) {
//ignored
}
lecternBefore.setBlockData(before.getBlockData());
consumer.queueBlockReplace(Actor.actorFromEntity(event.getPlayer()), lecternBefore, after);
try {
lecternBefore.getSnapshotInventory().setItem(0, book);
} catch (NullPointerException e) {
//ignored
}
}
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerTakeLecternBook(PlayerTakeLecternBookEvent event) {
final WorldConfig wcfg = getWorldConfig(event.getPlayer().getWorld());
if (wcfg != null && wcfg.isLogging(Logging.LECTERNBOOKCHANGE)) {
Lectern oldState = event.getLectern();
Lectern newState = (Lectern) oldState.getBlock().getState();
try {
newState.getSnapshotInventory().setItem(0, null);
} catch (NullPointerException e) {
//ignored
}
org.bukkit.block.data.type.Lectern oldBlockData = (org.bukkit.block.data.type.Lectern) oldState.getBlockData();
org.bukkit.block.data.type.Lectern blockData = (org.bukkit.block.data.type.Lectern) Material.LECTERN.createBlockData();
blockData.setFacing(oldBlockData.getFacing());
blockData.setPowered(oldBlockData.isPowered());
newState.setBlockData(blockData);
consumer.queueBlockReplace(Actor.actorFromEntity(event.getPlayer()), oldState, newState);
}
}
}

View File

@ -1,26 +0,0 @@
package de.diddiz.LogBlock.listeners;
import de.diddiz.LogBlock.Actor;
import de.diddiz.LogBlock.LogBlock;
import de.diddiz.LogBlock.Logging;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.BlockFadeEvent;
import static de.diddiz.LogBlock.config.Config.isLogging;
public class LockedChestDecayLogging extends LoggingListener {
public LockedChestDecayLogging(LogBlock lb) {
super(lb);
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockFade(BlockFadeEvent event) {
if (isLogging(event.getBlock().getWorld(), Logging.LOCKEDCHESTDECAY)) {
final int type = event.getBlock().getTypeId();
if (type == 95) {
consumer.queueBlockReplace(new Actor("LockedChestDecay"), event.getBlock().getState(), event.getNewState());
}
}
}
}

View File

@ -1,13 +1,13 @@
package de.diddiz.LogBlock.listeners; package de.diddiz.LogBlock.listeners;
import de.diddiz.LogBlock.Consumer; import de.diddiz.LogBlock.Consumer;
import de.diddiz.LogBlock.LogBlock; import de.diddiz.LogBlock.LogBlock;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
public class LoggingListener implements Listener { public class LoggingListener implements Listener {
protected final Consumer consumer; protected final Consumer consumer;
public LoggingListener(LogBlock lb) { public LoggingListener(LogBlock lb) {
consumer = lb.getConsumer(); consumer = lb.getConsumer();
} }
} }

View File

@ -0,0 +1,28 @@
package de.diddiz.LogBlock.listeners;
import static de.diddiz.LogBlock.config.Config.isLogging;
import de.diddiz.LogBlock.Actor;
import de.diddiz.LogBlock.LogBlock;
import de.diddiz.LogBlock.Logging;
import org.bukkit.Material;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.BlockFormEvent;
public class OxidizationLogging extends LoggingListener {
public OxidizationLogging(LogBlock lb) {
super(lb);
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockPhysics(BlockFormEvent event) {
if (isLogging(event.getBlock().getWorld(), Logging.OXIDIZATION)) {
final Material type = event.getNewState().getType();
if (type.name().contains("COPPER")) {
consumer.queueBlockReplace(new Actor("NaturalOxidization"), event.getBlock().getState(), event.getNewState());
}
}
}
}

View File

@ -1,23 +1,43 @@
package de.diddiz.LogBlock.listeners; package de.diddiz.LogBlock.listeners;
import de.diddiz.LogBlock.LogBlock; import de.diddiz.LogBlock.LogBlock;
import org.bukkit.event.EventHandler; import de.diddiz.LogBlock.config.Config;
import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerJoinEvent; import java.util.HashMap;
import org.bukkit.event.player.PlayerQuitEvent; import java.util.UUID;
public class PlayerInfoLogging extends LoggingListener { import org.bukkit.entity.Player;
public PlayerInfoLogging(LogBlock lb) { import org.bukkit.event.EventHandler;
super(lb); import org.bukkit.event.EventPriority;
} import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerJoin(PlayerJoinEvent event) { public class PlayerInfoLogging extends LoggingListener {
consumer.queueJoin(event.getPlayer());
} private final HashMap<UUID, Long> playerLogins = new HashMap<>();
@EventHandler(priority = EventPriority.MONITOR) public PlayerInfoLogging(LogBlock lb) {
public void onPlayerQuit(PlayerQuitEvent event) { super(lb);
consumer.queueLeave(event.getPlayer()); }
}
} @EventHandler(priority = EventPriority.MONITOR)
public void onPlayerJoin(PlayerJoinEvent event) {
playerLogins.put(event.getPlayer().getUniqueId(), System.currentTimeMillis());
consumer.queueJoin(event.getPlayer());
}
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerQuit(PlayerQuitEvent event) {
onPlayerQuit(event.getPlayer());
}
public void onPlayerQuit(Player player) {
Long joinTime = playerLogins.remove(player.getUniqueId());
if (Config.logPlayerInfo && joinTime != null) {
long onlineTime = (System.currentTimeMillis() - joinTime) / 1000;
if (onlineTime > 0) {
consumer.queueLeave(player, onlineTime);
}
}
}
}

View File

@ -0,0 +1,171 @@
package de.diddiz.LogBlock.listeners;
import de.diddiz.LogBlock.Actor;
import de.diddiz.LogBlock.LogBlock;
import de.diddiz.LogBlock.Logging;
import java.util.ArrayDeque;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockFadeEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import static de.diddiz.LogBlock.config.Config.isLogging;
import static de.diddiz.LogBlock.util.LoggingUtil.smartLogFallables;
public class ScaffoldingLogging extends LoggingListener {
private final static long MAX_SCAFFOLDING_LOG_TIME_MS = 2000;
private final static EnumSet<BlockFace> NEIGHBOURS_SIDES_AND_UP = EnumSet.of(BlockFace.UP, BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST);
private final static EnumSet<BlockFace> NEIGHBOURS_SIDES_AND_BELOW = EnumSet.of(BlockFace.DOWN, BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST);
private final ArrayDeque<ScaffoldingBreaker> scaffoldingBreakersList = new ArrayDeque<>();
private final HashMap<Location, ScaffoldingBreaker> scaffoldingBreakersByLocation = new HashMap<>();
private final HashMap<Location, Actor> scaffoldingPlacersByLocation = new HashMap<>();
public ScaffoldingLogging(LogBlock lb) {
super(lb);
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockFade(BlockFadeEvent event) {
Block block = event.getBlock();
if (isLogging(block.getWorld(), Logging.SCAFFOLDING)) {
final Material type = block.getType();
if (type == Material.SCAFFOLDING) {
Actor actor = scaffoldingPlacersByLocation.get(block.getLocation()); // get placer before cleanupScaffoldingBreakers
cleanupScaffoldingBreakers();
if (actor == null) {
actor = getScaffoldingBreaker(block);
if (actor != null) {
for (BlockFace dir : NEIGHBOURS_SIDES_AND_UP) {
Block otherBlock = block.getRelative(dir);
if (otherBlock.getType() == Material.SCAFFOLDING) {
addScaffoldingBreaker(actor, otherBlock);
}
}
} else {
actor = new Actor("ScaffoldingFall");
}
}
consumer.queueBlockReplace(actor, block.getState(), event.getNewState());
smartLogFallables(consumer, actor, block);
}
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockBreak(BlockBreakEvent event) {
Block block = event.getBlock();
if (isLogging(block.getWorld(), Logging.SCAFFOLDING)) {
cleanupScaffoldingBreakers();
Block otherBlock;
if (block.getType() == Material.SCAFFOLDING) {
for (BlockFace dir : NEIGHBOURS_SIDES_AND_UP) {
otherBlock = block.getRelative(dir);
if (otherBlock.getType() == Material.SCAFFOLDING) {
addScaffoldingBreaker(Actor.actorFromEntity(event.getPlayer()), otherBlock);
}
}
} else if ((otherBlock = block.getRelative(BlockFace.UP)).getType() == Material.SCAFFOLDING) {
addScaffoldingBreaker(Actor.actorFromEntity(event.getPlayer()), otherBlock);
}
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockPlace(BlockPlaceEvent event) {
Block block = event.getBlock();
if (isLogging(block.getWorld(), Logging.SCAFFOLDING)) {
cleanupScaffoldingBreakers();
if (block.getType() == Material.SCAFFOLDING) {
scaffoldingPlacersByLocation.put(block.getLocation(), Actor.actorFromEntity(event.getPlayer()));
}
}
}
public void addScaffoldingBreaker(Actor actor, Block block) {
ScaffoldingBreaker breaker = new ScaffoldingBreaker(actor, block.getLocation());
scaffoldingBreakersList.addLast(breaker);
scaffoldingBreakersByLocation.put(breaker.getLocation(), breaker);
}
private void cleanupScaffoldingBreakers() {
if (!scaffoldingPlacersByLocation.isEmpty()) {
scaffoldingPlacersByLocation.clear();
}
if (!scaffoldingBreakersList.isEmpty()) {
long time = System.currentTimeMillis() - MAX_SCAFFOLDING_LOG_TIME_MS;
while (!scaffoldingBreakersList.isEmpty() && scaffoldingBreakersList.getFirst().getTime() < time) {
ScaffoldingBreaker breaker = scaffoldingBreakersList.removeFirst();
scaffoldingBreakersByLocation.remove(breaker.getLocation(), breaker);
}
}
}
private Actor getScaffoldingBreaker(Block block) {
if (scaffoldingBreakersList.isEmpty()) {
return null;
}
ScaffoldingBreaker breaker = scaffoldingBreakersByLocation.get(block.getLocation());
if (breaker != null) {
return breaker.getActor();
}
// Search all connected scaffoldings
ArrayDeque<Block> front = new ArrayDeque<>();
HashSet<Block> frontAndDone = new HashSet<>();
front.addLast(block);
frontAndDone.add(block);
while (!front.isEmpty()) {
Block current = front.removeFirst();
Location loc = current.getLocation();
breaker = scaffoldingBreakersByLocation.get(loc);
if (breaker != null) {
return breaker.getActor();
}
for (BlockFace dir : NEIGHBOURS_SIDES_AND_BELOW) {
Block otherBlock = current.getRelative(dir);
if (!frontAndDone.contains(otherBlock) && otherBlock.getType() == Material.SCAFFOLDING) {
front.addLast(otherBlock);
frontAndDone.add(otherBlock);
}
}
}
return null;
}
class ScaffoldingBreaker {
protected final Actor actor;
protected final long time;
protected final Location location;
public ScaffoldingBreaker(Actor actor, Location location) {
this.actor = actor;
this.location = location;
this.time = System.currentTimeMillis();
}
public Actor getActor() {
return actor;
}
public Location getLocation() {
return location;
}
public long getTime() {
return time;
}
}
}

View File

@ -1,23 +1,40 @@
package de.diddiz.LogBlock.listeners; package de.diddiz.LogBlock.listeners;
import de.diddiz.LogBlock.Actor; import de.diddiz.LogBlock.Actor;
import de.diddiz.LogBlock.LogBlock; import de.diddiz.LogBlock.LogBlock;
import de.diddiz.LogBlock.Logging; import de.diddiz.LogBlock.Logging;
import org.bukkit.event.EventHandler; import java.util.Objects;
import org.bukkit.event.EventPriority; import org.bukkit.block.BlockState;
import org.bukkit.event.block.SignChangeEvent; import org.bukkit.block.Sign;
import org.bukkit.block.sign.SignSide;
import static de.diddiz.LogBlock.config.Config.isLogging; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
public class SignChangeLogging extends LoggingListener { import org.bukkit.event.block.SignChangeEvent;
public SignChangeLogging(LogBlock lb) {
super(lb); import static de.diddiz.LogBlock.config.Config.isLogging;
}
public class SignChangeLogging extends LoggingListener {
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public SignChangeLogging(LogBlock lb) {
public void onSignChange(SignChangeEvent event) { super(lb);
if (isLogging(event.getBlock().getWorld(), Logging.SIGNTEXT)) { }
consumer.queueSignPlace(Actor.actorFromEntity(event.getPlayer()), event.getBlock().getLocation(), event.getBlock().getTypeId(), event.getBlock().getData(), event.getLines());
} @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
} public void onSignChange(SignChangeEvent event) {
} if (isLogging(event.getBlock().getWorld(), Logging.SIGNTEXT)) {
BlockState newState = event.getBlock().getState();
if (newState instanceof Sign sign) {
SignSide signSide = sign.getSide(event.getSide());
boolean changed = false;
for (int i = 0; i < 4; i++) {
if (!Objects.equals(signSide.getLine(i), event.getLine(i))) {
signSide.setLine(i, event.getLine(i));
changed = true;
}
}
if (changed) {
consumer.queueBlockReplace(Actor.actorFromEntity(event.getPlayer()), event.getBlock().getState(), newState);
}
}
}
}
}

View File

@ -1,26 +1,28 @@
package de.diddiz.LogBlock.listeners; package de.diddiz.LogBlock.listeners;
import de.diddiz.LogBlock.Actor; import de.diddiz.LogBlock.Actor;
import de.diddiz.LogBlock.LogBlock; import de.diddiz.LogBlock.LogBlock;
import de.diddiz.LogBlock.Logging; import de.diddiz.LogBlock.Logging;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.Material;
import org.bukkit.event.block.BlockFadeEvent; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import static de.diddiz.LogBlock.config.Config.isLogging; import org.bukkit.event.block.BlockFadeEvent;
public class SnowFadeLogging extends LoggingListener { import static de.diddiz.LogBlock.config.Config.isLogging;
public SnowFadeLogging(LogBlock lb) {
super(lb); public class SnowFadeLogging extends LoggingListener {
} public SnowFadeLogging(LogBlock lb) {
super(lb);
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) }
public void onBlockFade(BlockFadeEvent event) {
if (isLogging(event.getBlock().getWorld(), Logging.SNOWFADE)) { @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
final int type = event.getBlock().getTypeId(); public void onBlockFade(BlockFadeEvent event) {
if (type == 78 || type == 79) { if (isLogging(event.getBlock().getWorld(), Logging.SNOWFADE)) {
consumer.queueBlockReplace(new Actor("SnowFade"), event.getBlock().getState(), event.getNewState()); final Material type = event.getBlock().getType();
} if (type == Material.SNOW || type == Material.ICE) {
} consumer.queueBlockReplace(new Actor("SnowFade"), event.getBlock().getState(), event.getNewState());
} }
} }
}
}

View File

@ -1,32 +1,28 @@
package de.diddiz.LogBlock.listeners; package de.diddiz.LogBlock.listeners;
import de.diddiz.LogBlock.Actor; import de.diddiz.LogBlock.Actor;
import de.diddiz.LogBlock.LogBlock; import de.diddiz.LogBlock.LogBlock;
import de.diddiz.LogBlock.Logging; import de.diddiz.LogBlock.Logging;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.Material;
import org.bukkit.event.block.BlockFormEvent; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import static de.diddiz.LogBlock.config.Config.isLogging; import org.bukkit.event.block.BlockFormEvent;
public class SnowFormLogging extends LoggingListener { import static de.diddiz.LogBlock.config.Config.isLogging;
public SnowFormLogging(LogBlock lb) {
super(lb); public class SnowFormLogging extends LoggingListener {
} public SnowFormLogging(LogBlock lb) {
super(lb);
// @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) }
// public void onLeavesDecay(LeavesDecayEvent event) {
// if (isLogging(event.getBlock().getWorld(), Logging.SNOWFORM)) @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
// consumer.queueBlockBreak("LeavesDecay", event.getBlock().getState()); public void onBlockForm(BlockFormEvent event) {
// } if (isLogging(event.getBlock().getWorld(), Logging.SNOWFORM)) {
final Material type = event.getNewState().getType();
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) if (type == Material.SNOW || type == Material.ICE) {
public void onBlockForm(BlockFormEvent event) { consumer.queueBlockReplace(new Actor("SnowForm"), event.getBlock().getState(), event.getNewState());
if (isLogging(event.getBlock().getWorld(), Logging.SNOWFORM)) { }
final int type = event.getNewState().getTypeId(); }
if (type == 78 || type == 79) { }
consumer.queueBlockReplace(new Actor("SnowForm"), event.getBlock().getState(), event.getNewState()); }
}
}
}
}

View File

@ -1,40 +1,34 @@
package de.diddiz.LogBlock.listeners; package de.diddiz.LogBlock.listeners;
import de.diddiz.LogBlock.Actor; import de.diddiz.LogBlock.Actor;
import de.diddiz.LogBlock.LogBlock; import de.diddiz.LogBlock.LogBlock;
import de.diddiz.LogBlock.Logging; import de.diddiz.LogBlock.Logging;
import de.diddiz.LogBlock.config.WorldConfig; import de.diddiz.LogBlock.config.WorldConfig;
import org.bukkit.block.BlockState; import org.bukkit.block.BlockState;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.world.StructureGrowEvent; import org.bukkit.event.world.StructureGrowEvent;
import static de.diddiz.LogBlock.config.Config.getWorldConfig; import static de.diddiz.LogBlock.config.Config.getWorldConfig;
public class StructureGrowLogging extends LoggingListener { public class StructureGrowLogging extends LoggingListener {
public StructureGrowLogging(LogBlock lb) { public StructureGrowLogging(LogBlock lb) {
super(lb); super(lb);
} }
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onStructureGrow(StructureGrowEvent event) { public void onStructureGrow(StructureGrowEvent event) {
final WorldConfig wcfg = getWorldConfig(event.getWorld()); final WorldConfig wcfg = getWorldConfig(event.getWorld());
if (wcfg != null) { if (wcfg != null) {
final Actor actor; if (!wcfg.isLogging(Logging.NATURALSTRUCTUREGROW)) {
if (event.getPlayer() != null) { return;
if (!wcfg.isLogging(Logging.BONEMEALSTRUCTUREGROW)) { }
return; if (!event.isFromBonemeal()) {
} final Actor actor = new Actor("NaturalGrow");
actor = Actor.actorFromEntity(event.getPlayer()); for (final BlockState state : event.getBlocks()) {
} else { consumer.queueBlockReplace(actor, state.getBlock().getState(), state);
if (!wcfg.isLogging(Logging.NATURALSTRUCTUREGROW)) { }
return; }
} }
actor = new Actor("NaturalGrow"); }
} }
for (final BlockState state : event.getBlocks()) {
consumer.queueBlockReplace(actor, state.getBlock().getState(), state);
}
}
}
}

View File

@ -1,124 +1,151 @@
package de.diddiz.LogBlock.listeners; package de.diddiz.LogBlock.listeners;
import de.diddiz.LogBlock.*; import de.diddiz.LogBlock.*;
import de.diddiz.worldedit.RegionContainer; import de.diddiz.LogBlock.events.ToolUseEvent;
import org.bukkit.ChatColor; import de.diddiz.LogBlock.util.BukkitUtils;
import org.bukkit.block.Block; import de.diddiz.LogBlock.util.CuboidRegion;
import org.bukkit.block.BlockFace; import org.bukkit.ChatColor;
import org.bukkit.entity.Player; import org.bukkit.Material;
import org.bukkit.event.EventHandler; import org.bukkit.block.Block;
import org.bukkit.event.Listener; import org.bukkit.entity.Player;
import org.bukkit.event.block.Action; import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerChangedWorldEvent; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerDropItemEvent; import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerChangedWorldEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import java.util.Map.Entry; import org.bukkit.inventory.ItemStack;
import static de.diddiz.LogBlock.Session.getSession; import java.util.Map.Entry;
import static de.diddiz.LogBlock.Session.hasSession;
import static de.diddiz.LogBlock.config.Config.isLogged; import static de.diddiz.LogBlock.Session.getSession;
import static de.diddiz.LogBlock.config.Config.toolsByType; import static de.diddiz.LogBlock.Session.hasSession;
import static de.diddiz.LogBlock.config.Config.isLogged;
public class ToolListener implements Listener { import static de.diddiz.LogBlock.config.Config.toolsByType;
private final CommandsHandler handler;
private final LogBlock logblock; public class ToolListener implements Listener {
private final CommandsHandler handler;
public ToolListener(LogBlock logblock) { private final LogBlock logblock;
this.logblock = logblock;
handler = logblock.getCommandsHandler(); public ToolListener(LogBlock logblock) {
} this.logblock = logblock;
handler = logblock.getCommandsHandler();
@EventHandler(ignoreCancelled = true) }
public void onPlayerInteract(PlayerInteractEvent event) {
if (event.getMaterial() != null) { @EventHandler
final Action action = event.getAction(); public void onPlayerInteract(PlayerInteractEvent event) {
final int type = event.getMaterial().getId(); if (event.getMaterial() != null) {
final Tool tool = toolsByType.get(type); final Action action = event.getAction();
final Player player = event.getPlayer(); final Material type = event.getMaterial();
if (tool != null && (action == Action.RIGHT_CLICK_BLOCK || action == Action.LEFT_CLICK_BLOCK) && logblock.hasPermission(player, "logblock.tools." + tool.name)) { final Tool tool = toolsByType.get(type);
final ToolBehavior behavior = action == Action.RIGHT_CLICK_BLOCK ? tool.rightClickBehavior : tool.leftClickBehavior; final Player player = event.getPlayer();
final ToolData toolData = getSession(player).toolData.get(tool); if (tool != null && (action == Action.RIGHT_CLICK_BLOCK || action == Action.LEFT_CLICK_BLOCK) && logblock.hasPermission(player, "logblock.tools." + tool.name)) {
if (behavior != ToolBehavior.NONE && toolData.enabled) { final ToolBehavior behavior = action == Action.RIGHT_CLICK_BLOCK ? tool.rightClickBehavior : tool.leftClickBehavior;
if (!isLogged(player.getWorld())) { final ToolData toolData = getSession(player).toolData.get(tool);
player.sendMessage(ChatColor.RED + "This world is not currently logged."); if (behavior != ToolBehavior.NONE && toolData.enabled) {
event.setCancelled(true); if (!isLogged(player.getWorld())) {
return; player.sendMessage(ChatColor.RED + "This world is not currently logged.");
} event.setCancelled(true);
final Block block = event.getClickedBlock(); return;
final QueryParams params = toolData.params; }
params.loc = null; final Block block = event.getClickedBlock();
params.sel = null; final QueryParams params = toolData.params.clone();
if (behavior == ToolBehavior.BLOCK) { params.loc = null;
params.setLocation(block.getRelative(event.getBlockFace()).getLocation()); params.sel = null;
} else if ((block.getTypeId() != 54 && block.getTypeId() != 146) || tool.params.radius != 0) { if (behavior == ToolBehavior.BLOCK) {
params.setLocation(block.getLocation()); params.setLocation(block.getRelative(event.getBlockFace()).getLocation());
} else { } else if (tool.params.radius != 0) {
if (logblock.getServer().getPluginManager().isPluginEnabled("WorldEdit")) { params.setLocation(block.getLocation());
for (final BlockFace face : new BlockFace[]{BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST}) { } else {
if (block.getRelative(face).getTypeId() == block.getTypeId()) { Block otherHalfChest = BukkitUtils.getConnectedChest(block);
params.setSelection(RegionContainer.fromCorners(event.getPlayer().getWorld(), if (otherHalfChest == null) {
block.getLocation(), block.getRelative(face).getLocation())); params.setLocation(block.getLocation());
} } else {
} params.setSelection(CuboidRegion.fromCorners(block.getLocation().getWorld(), block.getLocation(), otherHalfChest.getLocation()));
} }
if (params.sel == null) { }
params.setLocation(block.getLocation()); try {
} params.validate();
} if (this.callToolUseEvent(new ToolUseEvent(player, tool, behavior, params))) {
try { return;
if (toolData.mode == ToolMode.ROLLBACK) { }
handler.new CommandRollback(player, params, true); if (toolData.mode == ToolMode.ROLLBACK) {
} else if (toolData.mode == ToolMode.REDO) { handler.new CommandRollback(player, params, true);
handler.new CommandRedo(player, params, true); } else if (toolData.mode == ToolMode.REDO) {
} else if (toolData.mode == ToolMode.CLEARLOG) { handler.new CommandRedo(player, params, true);
handler.new CommandClearLog(player, params, true); } else if (toolData.mode == ToolMode.CLEARLOG) {
} else if (toolData.mode == ToolMode.WRITELOGFILE) { handler.new CommandClearLog(player, params, true);
handler.new CommandWriteLogFile(player, params, true); } else if (toolData.mode == ToolMode.WRITELOGFILE) {
} else { handler.new CommandWriteLogFile(player, params, true);
handler.new CommandLookup(player, params, true); } else {
} handler.new CommandLookup(player, params, true);
} catch (final Exception ex) { }
player.sendMessage(ChatColor.RED + ex.getMessage()); } catch (final Exception ex) {
} player.sendMessage(ChatColor.RED + ex.getMessage());
event.setCancelled(true); }
} event.setCancelled(true);
} }
} }
} }
}
@EventHandler
public void onPlayerChangedWorld(PlayerChangedWorldEvent event) { private boolean callToolUseEvent(ToolUseEvent event) {
final Player player = event.getPlayer(); this.logblock.getServer().getPluginManager().callEvent(event);
if (hasSession(player)) { return event.isCancelled();
final Session session = getSession(player); }
for (final Entry<Tool, ToolData> entry : session.toolData.entrySet()) {
final Tool tool = entry.getKey(); @EventHandler
final ToolData toolData = entry.getValue(); public void onPlayerChangedWorld(PlayerChangedWorldEvent event) {
if (toolData.enabled && !logblock.hasPermission(player, "logblock.tools." + tool.name)) { final Player player = event.getPlayer();
toolData.enabled = false; if (hasSession(player)) {
player.getInventory().removeItem(new ItemStack(tool.item, 1)); final Session session = getSession(player);
player.sendMessage(ChatColor.GREEN + "Tool disabled."); for (final Entry<Tool, ToolData> entry : session.toolData.entrySet()) {
} final Tool tool = entry.getKey();
} final ToolData toolData = entry.getValue();
} if (toolData.enabled && !logblock.hasPermission(player, "logblock.tools." + tool.name)) {
} toolData.enabled = false;
if (tool.removeOnDisable && logblock.hasPermission(player, "logblock.spawnTools")) {
@EventHandler player.getInventory().removeItem(new ItemStack(tool.item, 1));
public void onPlayerDropItem(PlayerDropItemEvent event) { }
final Player player = event.getPlayer(); player.sendMessage(ChatColor.GREEN + "Tool disabled.");
if (hasSession(player)) { }
final Session session = getSession(player); }
for (final Entry<Tool, ToolData> entry : session.toolData.entrySet()) { }
final Tool tool = entry.getKey(); }
final ToolData toolData = entry.getValue();
final int item = event.getItemDrop().getItemStack().getTypeId(); @EventHandler
if (item == tool.item && toolData.enabled && !tool.canDrop) { public void onPlayerDropItem(PlayerDropItemEvent event) {
player.sendMessage(ChatColor.RED + "You cannot drop this tool."); final Player player = event.getPlayer();
event.setCancelled(true); if (hasSession(player)) {
} final Session session = getSession(player);
} for (final Entry<Tool, ToolData> entry : session.toolData.entrySet()) {
} final Tool tool = entry.getKey();
} final ToolData toolData = entry.getValue();
} final Material item = event.getItemDrop().getItemStack().getType();
if (item == tool.item && toolData.enabled) {
if (tool.dropToDisable) {
toolData.enabled = false;
ItemStack stack = event.getItemDrop().getItemStack();
if (tool.removeOnDisable && logblock.hasPermission(player, "logblock.spawnTools")) {
if (stack.isSimilar(new ItemStack(item))) {
if (stack.getAmount() > 1) {
stack.setAmount(stack.getAmount() - 1);
event.getItemDrop().setItemStack(stack);
} else {
event.getItemDrop().remove();
}
}
}
if (BukkitUtils.hasInventoryStorageSpaceFor(player.getInventory(), stack)) {
event.setCancelled(true);
}
player.sendMessage(ChatColor.GREEN + "Tool disabled.");
} else if (!tool.canDrop) {
player.sendMessage(ChatColor.RED + "You cannot drop this tool.");
event.setCancelled(true);
}
}
}
}
}
}

View File

@ -1,24 +1,24 @@
package de.diddiz.LogBlock.listeners; package de.diddiz.LogBlock.listeners;
import de.diddiz.LogBlock.Actor; import de.diddiz.LogBlock.Actor;
import de.diddiz.LogBlock.LogBlock; import de.diddiz.LogBlock.LogBlock;
import de.diddiz.LogBlock.Logging; import de.diddiz.LogBlock.Logging;
import org.bukkit.entity.Wither; import org.bukkit.entity.Wither;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.entity.EntityChangeBlockEvent; import org.bukkit.event.entity.EntityChangeBlockEvent;
import static de.diddiz.LogBlock.config.Config.isLogging; import static de.diddiz.LogBlock.config.Config.isLogging;
public class WitherLogging extends LoggingListener { public class WitherLogging extends LoggingListener {
public WitherLogging(LogBlock lb) { public WitherLogging(LogBlock lb) {
super(lb); super(lb);
} }
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onEntityChangeBlock(EntityChangeBlockEvent event) { public void onEntityChangeBlock(EntityChangeBlockEvent event) {
if (event.getEntity() instanceof Wither && isLogging(event.getBlock().getWorld(), Logging.WITHER)) { if (event.getEntity() instanceof Wither && isLogging(event.getBlock().getWorld(), Logging.WITHER)) {
consumer.queueBlockReplace(Actor.actorFromEntity(event.getEntity()), event.getBlock().getState(), event.getTo().getId(), event.getData()); // Wither walked through a block. consumer.queueBlockReplace(Actor.actorFromEntity(event.getEntity()), event.getBlock().getState(), event.getBlockData()); // Wither walked through a block.
} }
} }
} }

View File

@ -0,0 +1,73 @@
package de.diddiz.LogBlock.questioner;
import org.bukkit.entity.Player;
public class Question {
private String answer;
private final String[] answers;
private final String questionMessage;
private final Player respondent;
private final long start;
public Question(Player respondent, String questionMessage, String[] answers) {
this.start = System.currentTimeMillis();
this.respondent = respondent;
this.questionMessage = questionMessage;
this.answers = answers;
}
public synchronized String ask() {
StringBuilder options = new StringBuilder();
for (String ans : this.answers) {
options.append("/" + ans + ", ");
}
options.delete(options.length() - 2, options.length());
this.respondent.sendMessage(this.questionMessage);
this.respondent.sendMessage("- " + options + "?");
while (answer == null) {
try {
wait();
} catch (InterruptedException ex) {
if (answer == null) {
answer = "interrupted";
}
}
}
return this.answer;
}
public synchronized boolean isExpired(boolean forceExpire) {
if (forceExpire || System.currentTimeMillis() - this.start > 120000L || this.answer != null) {
if (answer == null) {
answer = "timed out";
}
notifyAll();
return true;
}
return false;
}
public boolean returnAnswer(String answer) {
return returnAnswer(answer, false);
}
public synchronized boolean returnAnswer(String answer, boolean forceReturn) {
if (forceReturn) {
if (this.answer == null) {
this.answer = answer;
}
notifyAll();
return true;
}
for (String s : answers) {
if (s.equalsIgnoreCase(answer)) {
if (this.answer == null) {
this.answer = s;
}
notifyAll();
return true;
}
}
return false;
}
}

View File

@ -0,0 +1,76 @@
package de.diddiz.LogBlock.questioner;
import java.util.Iterator;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.Map.Entry;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import de.diddiz.LogBlock.LogBlock;
public class Questioner {
private final LogBlock logBlock;
private final ConcurrentHashMap<UUID, Question> questions = new ConcurrentHashMap<>();
public Questioner(LogBlock logBlock) {
this.logBlock = logBlock;
logBlock.getServer().getPluginManager().registerEvents(new QuestionerListener(), logBlock);
logBlock.getServer().getScheduler().scheduleSyncRepeatingTask(logBlock, new QuestionsReaper(), 600, 600);
}
public String ask(Player respondent, String questionMessage, String... answers) {
if (Bukkit.isPrimaryThread()) {
throw new IllegalStateException("This method may not be called from the primary thread");
}
Question question = new Question(respondent, questionMessage, answers);
Question oldQuestion = this.questions.put(respondent.getUniqueId(), question);
if (oldQuestion != null) {
oldQuestion.returnAnswer("no", true);
// wait a little time to let the other thread continue
try {
Thread.sleep(10);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
return question.ask();
}
private class QuestionsReaper implements Runnable {
@Override
public void run() {
if (questions.isEmpty()) {
return;
}
Iterator<Entry<UUID, Question>> it = questions.entrySet().iterator();
while (it.hasNext()) {
Entry<UUID, Question> e = it.next();
Question question = e.getValue();
if (question.isExpired(logBlock.getServer().getPlayer(e.getKey()) == null)) {
it.remove();
}
}
}
}
private class QuestionerListener implements Listener {
@EventHandler(ignoreCancelled = true)
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
UUID player = event.getPlayer().getUniqueId();
Question question;
question = questions.get(player);
if (question != null) {
String answer = event.getMessage().substring(1).toLowerCase();
if (question.returnAnswer(answer)) {
questions.remove(player, question);
event.setCancelled(true);
}
}
}
}
}

View File

@ -0,0 +1,24 @@
package de.diddiz.LogBlock.util;
import net.md_5.bungee.api.ChatColor;
public enum ActionColor {
DESTROY(ChatColor.RED),
CREATE(ChatColor.DARK_GREEN),
INTERACT(ChatColor.GRAY);
private final ChatColor color;
ActionColor(ChatColor color) {
this.color = color;
}
public ChatColor getColor() {
return color;
}
@Override
public String toString() {
return color.toString();
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,424 @@
package de.diddiz.LogBlock.util;
// Taken from maven-artifact at
// http://grepcode.com/file_/repo1.maven.org/maven2/org.apache.maven/maven-artifact/3.2.3/org/apache/maven/artifact/versioning/ComparableVersion.java/?v=source
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Locale;
import java.util.Properties;
import java.util.Stack;
/**
* Generic implementation of version comparison.
*
* <p>Features:
* <ul>
* <li>mixing of '<code>-</code>' (dash) and '<code>.</code>' (dot) separators,</li>
* <li>transition between characters and digits also constitutes a separator:
* <code>1.0alpha1 =&gt; [1, 0, alpha, 1]</code></li>
* <li>unlimited number of version components,</li>
* <li>version components in the text can be digits or strings,</li>
* <li>strings are checked for well-known qualifiers and the qualifier ordering is used for version ordering.
* Well-known qualifiers (case insensitive) are:<ul>
* <li><code>alpha</code> or <code>a</code></li>
* <li><code>beta</code> or <code>b</code></li>
* <li><code>milestone</code> or <code>m</code></li>
* <li><code>rc</code> or <code>cr</code></li>
* <li><code>snapshot</code></li>
* <li><code>(the empty string)</code> or <code>ga</code> or <code>final</code></li>
* <li><code>sp</code></li>
* </ul>
* Unknown qualifiers are considered after known qualifiers, with lexical order (always case insensitive),
* </li>
* <li>a dash usually precedes a qualifier, and is always less important than something preceded with a dot.</li>
* </ul></p>
*
* @see <a href="https://cwiki.apache.org/confluence/display/MAVENOLD/Versioning">"Versioning" on Maven Wiki</a>
* @author <a href="mailto:kenney@apache.org">Kenney Westerhof</a>
* @author <a href="mailto:hboutemy@apache.org">Hervé Boutemy</a>
*/
public class ComparableVersion implements Comparable<ComparableVersion> {
private String value;
private String canonical;
private ListItem items;
private interface Item {
int INTEGER_ITEM = 0;
int STRING_ITEM = 1;
int LIST_ITEM = 2;
int compareTo(Item item);
int getType();
boolean isNull();
}
/**
* Represents a numeric item in the version item list.
*/
private static class IntegerItem implements Item {
private static final BigInteger BIG_INTEGER_ZERO = new BigInteger("0");
private final BigInteger value;
public static final IntegerItem ZERO = new IntegerItem();
private IntegerItem() {
this.value = BIG_INTEGER_ZERO;
}
public IntegerItem(String str) {
this.value = new BigInteger(str);
}
@Override
public int getType() {
return INTEGER_ITEM;
}
@Override
public boolean isNull() {
return BIG_INTEGER_ZERO.equals(value);
}
@Override
public int compareTo(Item item) {
if (item == null) {
return BIG_INTEGER_ZERO.equals(value) ? 0 : 1; // 1.0 == 1, 1.1 > 1
}
switch (item.getType()) {
case INTEGER_ITEM:
return value.compareTo(((IntegerItem) item).value);
case STRING_ITEM:
return 1; // 1.1 > 1-sp
case LIST_ITEM:
return 1; // 1.1 > 1-1
default:
throw new RuntimeException("invalid item: " + item.getClass());
}
}
@Override
public String toString() {
return value.toString();
}
}
/**
* Represents a string in the version item list, usually a qualifier.
*/
private static class StringItem implements Item {
private static final String[] QUALIFIERS = { "alpha", "beta", "milestone", "rc", "snapshot", "", "sp" };
private static final List<String> _QUALIFIERS = Arrays.asList(QUALIFIERS);
private static final Properties ALIASES = new Properties();
static {
ALIASES.put("ga", "");
ALIASES.put("final", "");
ALIASES.put("cr", "rc");
}
/**
* A comparable value for the empty-string qualifier. This one is used to determine if a given qualifier makes
* the version older than one without a qualifier, or more recent.
*/
private static final String RELEASE_VERSION_INDEX = String.valueOf(_QUALIFIERS.indexOf(""));
private String value;
public StringItem(String value, boolean followedByDigit) {
if (followedByDigit && value.length() == 1) {
// a1 = alpha-1, b1 = beta-1, m1 = milestone-1
switch (value.charAt(0)) {
case 'a':
value = "alpha";
break;
case 'b':
value = "beta";
break;
case 'm':
value = "milestone";
break;
}
}
this.value = ALIASES.getProperty(value, value);
}
@Override
public int getType() {
return STRING_ITEM;
}
@Override
public boolean isNull() {
return (comparableQualifier(value).compareTo(RELEASE_VERSION_INDEX) == 0);
}
/**
* Returns a comparable value for a qualifier.
*
* This method takes into account the ordering of known qualifiers then unknown qualifiers with lexical ordering.
*
* just returning an Integer with the index here is faster, but requires a lot of if/then/else to check for -1
* or QUALIFIERS.size and then resort to lexical ordering. Most comparisons are decided by the first character,
* so this is still fast. If more characters are needed then it requires a lexical sort anyway.
*
* @param qualifier
* @return an equivalent value that can be used with lexical comparison
*/
public static String comparableQualifier(String qualifier) {
int i = _QUALIFIERS.indexOf(qualifier);
return i == -1 ? (_QUALIFIERS.size() + "-" + qualifier) : String.valueOf(i);
}
@Override
public int compareTo(Item item) {
if (item == null) {
// 1-rc < 1, 1-ga > 1
return comparableQualifier(value).compareTo(RELEASE_VERSION_INDEX);
}
switch (item.getType()) {
case INTEGER_ITEM:
return -1; // 1.any < 1.1 ?
case STRING_ITEM:
return comparableQualifier(value).compareTo(comparableQualifier(((StringItem) item).value));
case LIST_ITEM:
return -1; // 1.any < 1-1
default:
throw new RuntimeException("invalid item: " + item.getClass());
}
}
@Override
public String toString() {
return value;
}
}
/**
* Represents a version list item. This class is used both for the global item list and for sub-lists (which start
* with '-(number)' in the version specification).
*/
private static class ListItem extends ArrayList<Item> implements Item {
private static final long serialVersionUID = 5914575811857700009L;
@Override
public int getType() {
return LIST_ITEM;
}
@Override
public boolean isNull() {
return (size() == 0);
}
void normalize() {
for (ListIterator<Item> iterator = listIterator(size()); iterator.hasPrevious();) {
Item item = iterator.previous();
if (item.isNull()) {
iterator.remove(); // remove null trailing items: 0, "", empty list
} else {
break;
}
}
}
@Override
public int compareTo(Item item) {
if (item == null) {
if (size() == 0) {
return 0; // 1-0 = 1- (normalize) = 1
}
Item first = get(0);
return first.compareTo(null);
}
switch (item.getType()) {
case INTEGER_ITEM:
return -1; // 1-1 < 1.0.x
case STRING_ITEM:
return 1; // 1-1 > 1-sp
case LIST_ITEM:
Iterator<Item> left = iterator();
Iterator<Item> right = ((ListItem) item).iterator();
while (left.hasNext() || right.hasNext()) {
Item l = left.hasNext() ? left.next() : null;
Item r = right.hasNext() ? right.next() : null;
// if this is shorter, then invert the compare and mul with -1
int result = l == null ? (r == null ? 0 : -1 * r.compareTo(l)) : l.compareTo(r);
if (result != 0) {
return result;
}
}
return 0;
default:
throw new RuntimeException("invalid item: " + item.getClass());
}
}
@Override
public String toString() {
StringBuilder buffer = new StringBuilder("(");
for (Iterator<Item> iter = iterator(); iter.hasNext();) {
buffer.append(iter.next());
if (iter.hasNext()) {
buffer.append(',');
}
}
buffer.append(')');
return buffer.toString();
}
}
public ComparableVersion(String version) {
parseVersion(version);
}
public final void parseVersion(String version) {
this.value = version;
items = new ListItem();
version = version.toLowerCase(Locale.ENGLISH);
ListItem list = items;
Stack<Item> stack = new Stack<>();
stack.push(list);
boolean isDigit = false;
int startIndex = 0;
for (int i = 0; i < version.length(); i++) {
char c = version.charAt(i);
if (c == '.') {
if (i == startIndex) {
list.add(IntegerItem.ZERO);
} else {
list.add(parseItem(isDigit, version.substring(startIndex, i)));
}
startIndex = i + 1;
} else if (c == '-') {
if (i == startIndex) {
list.add(IntegerItem.ZERO);
} else {
list.add(parseItem(isDigit, version.substring(startIndex, i)));
}
startIndex = i + 1;
if (isDigit) {
list.normalize(); // 1.0-* = 1-*
if ((i + 1 < version.length()) && Character.isDigit(version.charAt(i + 1))) {
// new ListItem only if previous were digits and new char is a digit,
// ie need to differentiate only 1.1 from 1-1
list.add(list = new ListItem());
stack.push(list);
}
}
} else if (Character.isDigit(c)) {
if (!isDigit && i > startIndex) {
list.add(new StringItem(version.substring(startIndex, i), true));
startIndex = i;
}
isDigit = true;
} else {
if (isDigit && i > startIndex) {
list.add(parseItem(true, version.substring(startIndex, i)));
startIndex = i;
}
isDigit = false;
}
}
if (version.length() > startIndex) {
list.add(parseItem(isDigit, version.substring(startIndex)));
}
while (!stack.isEmpty()) {
list = (ListItem) stack.pop();
list.normalize();
}
canonical = items.toString();
}
private static Item parseItem(boolean isDigit, String buf) {
return isDigit ? new IntegerItem(buf) : new StringItem(buf, false);
}
@Override
public int compareTo(ComparableVersion o) {
return items.compareTo(o.items);
}
public int compareTo(String version) {
return compareTo(new ComparableVersion(version));
}
@Override
public String toString() {
return value;
}
public String toCanonicalString() {
return canonical;
}
@Override
public boolean equals(Object o) {
return (o instanceof ComparableVersion) && canonical.equals(((ComparableVersion) o).canonical);
}
@Override
public int hashCode() {
return canonical.hashCode();
}
}

View File

@ -0,0 +1,58 @@
package de.diddiz.LogBlock.util;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.util.BlockVector;
public class CuboidRegion implements Cloneable {
private World world;
private BlockVector min = new BlockVector();
private BlockVector max = new BlockVector();
public CuboidRegion(World world, BlockVector first, BlockVector second) {
this.world = world;
this.min.setX(Math.min(first.getBlockX(), second.getBlockX()));
this.min.setY(Math.min(first.getBlockY(), second.getBlockY()));
this.min.setZ(Math.min(first.getBlockZ(), second.getBlockZ()));
this.max.setX(Math.max(first.getBlockX(), second.getBlockX()));
this.max.setY(Math.max(first.getBlockY(), second.getBlockY()));
this.max.setZ(Math.max(first.getBlockZ(), second.getBlockZ()));
}
public static CuboidRegion fromCorners(World world, Location first, Location second) {
return new CuboidRegion(world, new BlockVector(first.getBlockX(), first.getBlockY(), first.getBlockZ()), new BlockVector(second.getBlockX(), second.getBlockY(), second.getBlockZ()));
}
public World getWorld() {
return world;
}
public BlockVector getMinimumPoint() {
return min;
}
public BlockVector getMaximumPoint() {
return max;
}
public int getSizeX() {
return max.getBlockX() - min.getBlockX() + 1;
}
public int getSizeZ() {
return max.getBlockZ() - min.getBlockZ() + 1;
}
@Override
public CuboidRegion clone() {
try {
CuboidRegion clone = (CuboidRegion) super.clone();
clone.min = min.clone();
clone.max = max.clone();
return clone;
} catch (final CloneNotSupportedException ex) {
throw new Error("CuboidRegion should be cloneable", ex);
}
}
}

View File

@ -0,0 +1,361 @@
package de.diddiz.LogBlock.util;
import de.diddiz.LogBlock.Actor;
import de.diddiz.LogBlock.Consumer;
import de.diddiz.LogBlock.Logging;
import de.diddiz.LogBlock.config.WorldConfig;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.Directional;
import org.bukkit.block.data.type.Bell;
import org.bukkit.block.data.type.Bell.Attachment;
import org.bukkit.block.data.type.Lantern;
import org.bukkit.block.data.type.PointedDripstone;
import org.bukkit.block.data.type.PointedDripstone.Thickness;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Projectile;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.projectiles.ProjectileSource;
import java.util.List;
import static de.diddiz.LogBlock.config.Config.getWorldConfig;
import static de.diddiz.LogBlock.config.Config.mb4;
public class LoggingUtil {
public static void smartLogBlockPlace(Consumer consumer, Actor actor, BlockState replaced, BlockState placed) {
Location loc = replaced.getLocation();
Material placedType = placed.getType();
if (!placedType.hasGravity() || !BukkitUtils.canDirectlyFallIn(replaced.getBlock().getRelative(BlockFace.DOWN).getType())) {
if (placedType == Material.TWISTING_VINES) {
Block below = placed.getBlock().getRelative(BlockFace.DOWN);
if (below.getType() == Material.TWISTING_VINES) {
consumer.queueBlockReplace(actor, below.getState(), Material.TWISTING_VINES_PLANT.createBlockData());
}
}
if (placedType == Material.WEEPING_VINES) {
Block above = placed.getBlock().getRelative(BlockFace.UP);
if (above.getType() == Material.WEEPING_VINES) {
consumer.queueBlockReplace(actor, above.getState(), Material.WEEPING_VINES_PLANT.createBlockData());
}
}
if (BukkitUtils.isEmpty(replaced.getType())) {
consumer.queueBlockPlace(actor, placed);
} else {
consumer.queueBlockReplace(actor, replaced, placed);
}
return;
}
int x = loc.getBlockX();
int initialy = loc.getBlockY();
int y = initialy;
int z = loc.getBlockZ();
while (y > loc.getWorld().getMinHeight() && BukkitUtils.canFallIn(loc.getWorld(), x, (y - 1), z)) {
y--;
}
if (initialy != y && !BukkitUtils.isEmpty(replaced.getType())) {
// this is not the final location but the block got removed (vines etc)
consumer.queueBlockBreak(actor, replaced);
}
// If y is minHeight then the block fell out of the world :(
if (y > loc.getWorld().getMinHeight()) {
// Run this check to avoid false positives
Location finalLoc = new Location(loc.getWorld(), x, y, z);
if (y == initialy || !BukkitUtils.isFallingEntityKiller(finalLoc.getBlock().getType())) {
if (BukkitUtils.isEmpty(finalLoc.getBlock().getType())) {
consumer.queueBlockPlace(actor, finalLoc, placed.getBlockData());
} else {
consumer.queueBlockReplace(actor, finalLoc.getBlock().getState(), placed.getBlockData());
}
}
}
}
public static void smartLogFallables(Consumer consumer, Actor actor, Block origin) {
WorldConfig wcfg = getWorldConfig(origin.getWorld());
if (wcfg == null) {
return;
}
//Handle falling blocks
Block checkBlock = origin.getRelative(BlockFace.UP);
int up = 0;
final int highestBlock = checkBlock.getWorld().getHighestBlockYAt(checkBlock.getLocation());
while (checkBlock.getType().hasGravity()) {
// Record this block as falling
consumer.queueBlockBreak(actor, checkBlock.getState());
// Guess where the block is going (This could be thrown of by explosions, but it is better than nothing)
Location loc = origin.getLocation();
int x = loc.getBlockX();
int y = loc.getBlockY();
int z = loc.getBlockZ();
while (y > loc.getWorld().getMinHeight() && BukkitUtils.canFallIn(loc.getWorld(), x, (y - 1), z)) {
y--;
}
// If y is minHeight then the sand block fell out of the world :(
if (y > loc.getWorld().getMinHeight()) {
Location finalLoc = new Location(loc.getWorld(), x, y, z);
// Run this check to avoid false positives
if (!BukkitUtils.isFallingEntityKiller(finalLoc.getBlock().getType())) {
finalLoc.add(0, up, 0); // Add this here after checking for block breakers
if (BukkitUtils.isEmpty(finalLoc.getBlock().getType())) {
consumer.queueBlockPlace(actor, finalLoc, checkBlock.getBlockData());
} else {
consumer.queueBlockReplace(actor, finalLoc, finalLoc.getBlock().getBlockData(), checkBlock.getBlockData());
}
up++;
}
}
if (checkBlock.getY() >= highestBlock) {
break;
}
checkBlock = checkBlock.getRelative(BlockFace.UP);
}
if (wcfg.isLogging(Logging.SCAFFOLDING) && checkBlock.getType() == Material.SCAFFOLDING && consumer.getLogblock().getScaffoldingLogging() != null) {
consumer.getLogblock().getScaffoldingLogging().addScaffoldingBreaker(actor, checkBlock);
}
}
public static void smartLogBlockBreak(Consumer consumer, Actor actor, Block origin) {
smartLogBlockReplace(consumer, actor, origin, null);
}
public static void smartLogBlockReplace(Consumer consumer, Actor actor, Block origin, BlockData replacedWith) {
WorldConfig wcfg = getWorldConfig(origin.getWorld());
if (wcfg == null) {
return;
}
Material replacedType = origin.getType();
if (replacedType == Material.TWISTING_VINES || replacedType == Material.TWISTING_VINES_PLANT) {
Block below = origin.getRelative(BlockFace.DOWN);
if (below.getType() == Material.TWISTING_VINES_PLANT) {
consumer.queueBlockReplace(actor, below.getState(), Material.TWISTING_VINES.createBlockData());
}
}
if (replacedType == Material.WEEPING_VINES || replacedType == Material.WEEPING_VINES_PLANT) {
Block above = origin.getRelative(BlockFace.UP);
if (above.getType() == Material.WEEPING_VINES_PLANT) {
consumer.queueBlockReplace(actor, above.getState(), Material.WEEPING_VINES.createBlockData());
}
}
if (replacedType == Material.CAVE_VINES || replacedType == Material.CAVE_VINES_PLANT) {
Block above = origin.getRelative(BlockFace.UP);
if (above.getType() == Material.CAVE_VINES_PLANT) {
consumer.queueBlockReplace(actor, above.getState(), Material.CAVE_VINES.createBlockData());
}
}
Block checkBlock = origin.getRelative(BlockFace.UP);
Material typeAbove = checkBlock.getType();
if (BukkitUtils.isRelativeTopBreakable(typeAbove)) {
if (typeAbove == Material.IRON_DOOR || BukkitUtils.isWoodenDoor(typeAbove)) {
Block doorBlock = checkBlock;
// If the doorBlock is the top half a door the player simply punched a door
// this will be handled later.
if (!BukkitUtils.isTop(doorBlock.getBlockData())) {
doorBlock = doorBlock.getRelative(BlockFace.UP);
// Fall back check just in case the top half wasn't a door
if (doorBlock.getType() == Material.IRON_DOOR || BukkitUtils.isWoodenDoor(doorBlock.getType())) {
consumer.queueBlockBreak(actor, doorBlock.getState());
}
consumer.queueBlockBreak(actor, checkBlock.getState());
}
} else if (BukkitUtils.isDoublePlant(typeAbove)) {
Block plantBlock = checkBlock;
// If the plantBlock is the top half of a double plant the player simply
// punched the plant this will be handled later.
if (!BukkitUtils.isTop(plantBlock.getBlockData())) {
plantBlock = plantBlock.getRelative(BlockFace.UP);
// Fall back check just in case the top half wasn't a plant
if (BukkitUtils.isDoublePlant(plantBlock.getType())) {
consumer.queueBlockBreak(actor, plantBlock.getState());
}
consumer.queueBlockBreak(actor, checkBlock.getState());
}
} else {
consumer.queueBlockBreak(actor, checkBlock.getState());
// check next blocks above
checkBlock = checkBlock.getRelative(BlockFace.UP);
typeAbove = checkBlock.getType();
while (BukkitUtils.isRelativeTopBreakable(typeAbove)) {
consumer.queueBlockBreak(actor, checkBlock.getState());
checkBlock = checkBlock.getRelative(BlockFace.UP);
typeAbove = checkBlock.getType();
}
}
} else if (typeAbove == Material.LANTERN) {
Lantern lantern = (Lantern) checkBlock.getBlockData();
if (!lantern.isHanging()) {
consumer.queueBlockBreak(actor, checkBlock.getState());
}
} else if (typeAbove == Material.BELL) {
Bell bell = (Bell) checkBlock.getBlockData();
if (bell.getAttachment() == Attachment.FLOOR) {
consumer.queueBlockBreak(actor, checkBlock.getState());
}
} else if (typeAbove == Material.POINTED_DRIPSTONE) {
Block dripStoneBlock = checkBlock;
while (true) {
if (dripStoneBlock.getType() != Material.POINTED_DRIPSTONE) {
break;
}
PointedDripstone dripstone = (PointedDripstone) dripStoneBlock.getBlockData();
if (dripstone.getVerticalDirection() != BlockFace.UP) {
if (dripstone.getThickness() == Thickness.TIP_MERGE) {
PointedDripstone newDripstone = (PointedDripstone) dripstone.clone();
newDripstone.setThickness(Thickness.TIP);
consumer.queueBlockReplace(actor, dripStoneBlock.getState(), newDripstone);
}
break;
}
consumer.queueBlockBreak(actor, dripStoneBlock.getState());
dripStoneBlock = dripStoneBlock.getRelative(BlockFace.UP);
}
}
checkBlock = origin.getRelative(BlockFace.DOWN);
Material typeBelow = checkBlock.getType();
if (typeBelow == Material.LANTERN) {
Lantern lantern = (Lantern) checkBlock.getBlockData();
if (lantern.isHanging()) {
consumer.queueBlockBreak(actor, checkBlock.getState());
}
} else if (BukkitUtils.isHangingSign(typeBelow)) {
consumer.queueBlockBreak(actor, checkBlock.getState());
} else if (typeBelow == Material.BELL) {
Bell bell = (Bell) checkBlock.getBlockData();
if (bell.getAttachment() == Attachment.CEILING) {
consumer.queueBlockBreak(actor, checkBlock.getState());
}
} else if (typeBelow == Material.WEEPING_VINES || typeBelow == Material.WEEPING_VINES_PLANT || typeBelow == Material.CAVE_VINES || typeBelow == Material.CAVE_VINES_PLANT) {
consumer.queueBlockBreak(actor, checkBlock.getState());
// check next blocks below
checkBlock = checkBlock.getRelative(BlockFace.DOWN);
typeBelow = checkBlock.getType();
while (typeBelow == Material.WEEPING_VINES || typeBelow == Material.WEEPING_VINES_PLANT || typeBelow == Material.CAVE_VINES || typeBelow == Material.CAVE_VINES_PLANT) {
consumer.queueBlockBreak(actor, checkBlock.getState());
checkBlock = checkBlock.getRelative(BlockFace.DOWN);
typeBelow = checkBlock.getType();
}
} else if ((replacedType == Material.BIG_DRIPLEAF || replacedType == Material.BIG_DRIPLEAF_STEM) && (typeBelow == Material.BIG_DRIPLEAF || typeBelow == Material.BIG_DRIPLEAF_STEM)) {
consumer.queueBlockBreak(actor, checkBlock.getState());
// check next blocks below
checkBlock = checkBlock.getRelative(BlockFace.DOWN);
typeBelow = checkBlock.getType();
while (typeBelow == Material.BIG_DRIPLEAF || typeBelow == Material.BIG_DRIPLEAF_STEM) {
consumer.queueBlockBreak(actor, checkBlock.getState());
checkBlock = checkBlock.getRelative(BlockFace.DOWN);
typeBelow = checkBlock.getType();
}
} else if (typeBelow == Material.POINTED_DRIPSTONE) {
Block dripStoneBlock = checkBlock;
while (true) {
if (dripStoneBlock.getType() != Material.POINTED_DRIPSTONE) {
break;
}
PointedDripstone dripstone = (PointedDripstone) dripStoneBlock.getBlockData();
if (dripstone.getVerticalDirection() != BlockFace.DOWN) {
if (dripstone.getThickness() == Thickness.TIP_MERGE) {
PointedDripstone newDripstone = (PointedDripstone) dripstone.clone();
newDripstone.setThickness(Thickness.TIP);
consumer.queueBlockReplace(actor, dripStoneBlock.getState(), newDripstone);
}
break;
}
consumer.queueBlockBreak(actor, dripStoneBlock.getState());
dripStoneBlock = dripStoneBlock.getRelative(BlockFace.DOWN);
}
}
List<Location> relativeBreakables = BukkitUtils.getBlocksNearby(origin, BukkitUtils.getRelativeBreakables());
if (!relativeBreakables.isEmpty()) {
for (Location location : relativeBreakables) {
Block block = location.getBlock();
BlockData blockData = block.getBlockData();
if (blockData instanceof Directional) {
if (blockData.getMaterial() == Material.BELL) {
if (((Bell) blockData).getAttachment() == Attachment.SINGLE_WALL) {
if (block.getRelative(((Bell) blockData).getFacing()).equals(origin)) {
consumer.queueBlockBreak(actor, block.getState());
}
}
} else {
if (block.getRelative(((Directional) blockData).getFacing().getOppositeFace()).equals(origin)) {
consumer.queueBlockBreak(actor, block.getState());
}
}
}
}
}
// Special door check
if (replacedType == Material.IRON_DOOR || BukkitUtils.isWoodenDoor(replacedType)) {
Block doorBlock = origin;
// Up or down?
if (!BukkitUtils.isTop(doorBlock.getBlockData())) {
doorBlock = doorBlock.getRelative(BlockFace.UP);
} else {
doorBlock = doorBlock.getRelative(BlockFace.DOWN);
}
if (doorBlock.getType() == Material.IRON_DOOR || BukkitUtils.isWoodenDoor(doorBlock.getType())) {
consumer.queueBlockBreak(actor, doorBlock.getState());
}
} else if (BukkitUtils.isDoublePlant(replacedType)) { // Special double plant check
Block plantBlock = origin;
// Up or down?
if (!BukkitUtils.isTop(origin.getBlockData())) {
plantBlock = plantBlock.getRelative(BlockFace.UP);
} else {
plantBlock = plantBlock.getRelative(BlockFace.DOWN);
}
if (BukkitUtils.isDoublePlant(plantBlock.getType())) {
consumer.queueBlockBreak(actor, plantBlock.getState());
}
}
// Do this down here so that the block is added after blocks sitting on it
if (replacedWith == null) {
consumer.queueBlockBreak(actor, origin.getState());
} else {
consumer.queueBlockReplace(actor, origin.getState(), replacedWith);
}
}
public static String checkText(String text) {
if (text == null) {
return text;
}
if (mb4) {
return text;
}
return text.replaceAll("[^\\u0000-\\uFFFF]", "?");
}
public static Entity getRealDamager(Entity damager) {
if (damager instanceof Projectile) {
ProjectileSource realDamager = ((Projectile) damager).getShooter();
if (realDamager instanceof Entity) {
damager = (Entity) realDamager;
}
}
if (damager instanceof TNTPrimed) {
Entity realRemover = ((TNTPrimed) damager).getSource();
if (realRemover != null) {
damager = realRemover;
}
}
return damager;
}
}

View File

@ -0,0 +1,132 @@
package de.diddiz.LogBlock.util;
import static de.diddiz.LogBlock.util.ActionColor.CREATE;
import static de.diddiz.LogBlock.util.ActionColor.DESTROY;
import static de.diddiz.LogBlock.util.TypeColor.DEFAULT;
import static de.diddiz.LogBlock.util.Utils.spaces;
import de.diddiz.LogBlock.config.Config;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.chat.hover.content.Text;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.EntityType;
public class MessagingUtil {
public static BaseComponent[] formatSummarizedChanges(int created, int destroyed, BaseComponent actor, int createdWidth, int destroyedWidth, float spaceFactor) {
TextComponent textCreated = createTextComponentWithColor(created + spaces((int) ((10 - String.valueOf(created).length()) / spaceFactor)), CREATE.getColor());
TextComponent textDestroyed = createTextComponentWithColor(destroyed + spaces((int) ((10 - String.valueOf(destroyed).length()) / spaceFactor)), DESTROY.getColor());
return new BaseComponent[] { textCreated, textDestroyed, actor };
}
public static TextComponent createTextComponentWithColor(String text, ChatColor color) {
TextComponent tc = new TextComponent(text);
tc.setColor(color);
return tc;
}
public static TextComponent brackets(BracketType type, BaseComponent... content) {
TextComponent tc = createTextComponentWithColor(type.getStarting(), TypeColor.BRACKETS.getColor());
for (BaseComponent c : content) {
tc.addExtra(c);
}
tc.addExtra(new TextComponent(type.getEnding()));
return tc;
}
public static TextComponent prettyDate(long date) {
TextComponent tc = brackets(BracketType.STANDARD, createTextComponentWithColor(Config.formatterShort.format(date), TypeColor.DATE.getColor()));
tc.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(Config.formatter.format(date))));
return tc;
}
public static TextComponent prettyState(String stateName) {
return createTextComponentWithColor(stateName, TypeColor.STATE.getColor());
}
public static TextComponent prettyState(BaseComponent stateName) {
TextComponent tc = new TextComponent();
tc.setColor(TypeColor.STATE.getColor());
if (stateName != null) {
tc.addExtra(stateName);
}
return tc;
}
public static TextComponent prettyState(int stateValue) {
return prettyState(Integer.toString(stateValue));
}
public static <E extends Enum<E>> TextComponent prettyState(E enumerator) {
return prettyState(enumerator.toString());
}
public static TextComponent prettyMaterial(String materialName) {
return createTextComponentWithColor(materialName.toUpperCase(), TypeColor.MATERIAL.getColor());
}
public static TextComponent prettyMaterial(Material material) {
return prettyMaterial(material.name());
}
public static TextComponent prettyMaterial(BlockData material) {
TextComponent tc = prettyMaterial(material.getMaterial());
String bdString = material.getAsString();
int bracket = bdString.indexOf("[");
if (bracket >= 0) {
int bracket2 = bdString.indexOf("]", bracket);
if (bracket2 >= 0) {
String state = bdString.substring(bracket + 1, bracket2).replace(',', '\n');
tc.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(state)));
}
}
return tc;
}
public static TextComponent prettyEntityType(EntityType type) {
return prettyMaterial(type.name());
}
public static TextComponent prettyLocation(Location loc, int entryId) {
return prettyLocation(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ(), entryId);
}
public static TextComponent prettyLocation(int x, int y, int z, int entryId) {
TextComponent tc = createTextComponentWithColor("", DEFAULT.getColor());
tc.addExtra(createTextComponentWithColor(Integer.toString(x), TypeColor.COORDINATE.getColor()));
tc.addExtra(createTextComponentWithColor(", ", DEFAULT.getColor()));
tc.addExtra(createTextComponentWithColor(Integer.toString(y), TypeColor.COORDINATE.getColor()));
tc.addExtra(createTextComponentWithColor(", ", DEFAULT.getColor()));
tc.addExtra(createTextComponentWithColor(Integer.toString(z), TypeColor.COORDINATE.getColor()));
if (entryId > 0) {
tc.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/lb tp " + entryId));
tc.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text("Teleport here")));
}
return tc;
}
public enum BracketType {
STANDARD("[", "]"),
ANGLE("<", ">");
private String starting, ending;
BracketType(String starting, String ending) {
this.starting = starting;
this.ending = ending;
}
public String getStarting() {
return starting;
}
public String getEnding() {
return ending;
}
}
}

View File

@ -1,45 +1,51 @@
package de.diddiz.util; package de.diddiz.LogBlock.util;
import com.zaxxer.hikari.HikariDataSource; import com.zaxxer.hikari.HikariDataSource;
import de.diddiz.LogBlock.config.Config; import de.diddiz.LogBlock.config.Config;
import java.io.Closeable; import java.io.Closeable;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
public class MySQLConnectionPool implements Closeable { public class MySQLConnectionPool implements Closeable {
private final HikariDataSource ds; private final HikariDataSource ds;
public MySQLConnectionPool(String url, String user, String password) throws ClassNotFoundException { public MySQLConnectionPool(String url, String user, String password, boolean useSSL, boolean requireSSL) {
this.ds = new HikariDataSource(); this.ds = new HikariDataSource();
ds.setJdbcUrl(url); ds.setJdbcUrl(url);
ds.setUsername(user); ds.setUsername(user);
ds.setPassword(password); ds.setPassword(password);
ds.setMinimumIdle(2); ds.setMinimumIdle(2);
ds.setPoolName("LogBlock-Connection-Pool"); ds.setMaximumPoolSize(15);
ds.setPoolName("LogBlock-Connection-Pool");
ds.addDataSourceProperty("useUnicode", "true");
ds.addDataSourceProperty("characterEncoding", "utf-8"); ds.addDataSourceProperty("useUnicode", "true");
ds.addDataSourceProperty("rewriteBatchedStatements", "true"); ds.addDataSourceProperty("characterEncoding", "utf-8");
ds.addDataSourceProperty("rewriteBatchedStatements", "true");
ds.addDataSourceProperty("cachePrepStmts", "true");
ds.addDataSourceProperty("prepStmtCacheSize", "250"); ds.addDataSourceProperty("cachePrepStmts", "true");
ds.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); ds.addDataSourceProperty("prepStmtCacheSize", "250");
} ds.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
ds.addDataSourceProperty("useServerPrepStmts", "true");
@Override
public void close() { ds.addDataSourceProperty("useSSL", Boolean.toString(useSSL));
ds.close(); ds.addDataSourceProperty("requireSSL", Boolean.toString(requireSSL));
} ds.addDataSourceProperty("verifyServerCertificate", "false");
}
public Connection getConnection() throws SQLException {
Connection connection = ds.getConnection(); @Override
if (Config.mb4) { public void close() {
connection.createStatement().executeQuery("SET NAMES utf8mb4"); ds.close();
} }
return connection;
} public Connection getConnection() throws SQLException {
Connection connection = ds.getConnection();
} if (Config.mb4) {
connection.createStatement().executeUpdate("SET NAMES utf8mb4");
}
return connection;
}
}

View File

@ -0,0 +1,27 @@
package de.diddiz.LogBlock.util;
import org.bukkit.Bukkit;
public class ReflectionUtil {
private static String versionString;
public static String getVersion() {
if (versionString == null) {
String name = Bukkit.getServer().getClass().getPackage().getName();
versionString = name.substring(name.lastIndexOf('.') + 1);
}
return versionString;
}
public static Class<?> getMinecraftClass(String minecraftClassName) throws ClassNotFoundException {
String clazzName = "net.minecraft." + minecraftClassName;
return Class.forName(clazzName);
}
public static Class<?> getCraftBukkitClass(String craftBukkitClassName) throws ClassNotFoundException {
String clazzName = "org.bukkit.craftbukkit." + getVersion() + "." + craftBukkitClassName;
return Class.forName(clazzName);
}
}

View File

@ -0,0 +1,24 @@
package de.diddiz.LogBlock.util;
public class SqlUtil {
public static String escapeString(String s) {
return escapeString(s, false);
}
public static String escapeString(String s, boolean escapeMatcher) {
s = s.replace("\u0000", "\\0");
s = s.replace("\u0026", "\\Z");
s = s.replace("\\", "\\\\");
s = s.replace("'", "\\'");
s = s.replace("\"", "\\\"");
s = s.replace("\b", "\\b");
s = s.replace("\n", "\\n");
s = s.replace("\r", "\\r");
s = s.replace("\t", "\\t");
if (escapeMatcher) {
s = s.replace("%", "\\%");
s = s.replace("_", "\\_");
}
return s;
}
}

View File

@ -0,0 +1,29 @@
package de.diddiz.LogBlock.util;
import net.md_5.bungee.api.ChatColor;
public enum TypeColor {
DEFAULT(ChatColor.YELLOW),
MATERIAL(ChatColor.BLUE),
STATE(ChatColor.BLUE),
DATE(ChatColor.DARK_AQUA),
BRACKETS(ChatColor.DARK_GRAY),
COORDINATE(ChatColor.WHITE),
HEADER(ChatColor.GOLD),
ERROR(ChatColor.RED);
private final ChatColor color;
TypeColor(ChatColor color) {
this.color = color;
}
public ChatColor getColor() {
return color;
}
@Override
public String toString() {
return color.toString();
}
}

View File

@ -1,79 +1,61 @@
package de.diddiz.util; package de.diddiz.LogBlock.util;
import org.json.simple.JSONArray; import com.google.gson.Gson;
import org.json.simple.JSONObject; import com.google.gson.GsonBuilder;
import org.json.simple.parser.JSONParser; import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import java.io.InputStreamReader; import com.google.gson.JsonObject;
import java.io.OutputStream; import java.io.InputStreamReader;
import java.net.HttpURLConnection; import java.io.OutputStream;
import java.net.URL; import java.net.HttpURLConnection;
import java.nio.ByteBuffer; import java.net.URL;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
// Adapted from https://gist.github.com/evilmidget38/26d70114b834f71fb3b4 // Adapted from https://gist.github.com/evilmidget38/26d70114b834f71fb3b4
public class UUIDFetcher { public class UUIDFetcher {
private static final String PROFILE_URL = "https://api.mojang.com/profiles/minecraft"; private static final String PROFILE_URL = "https://api.mojang.com/profiles/minecraft";
private static final JSONParser jsonParser = new JSONParser(); private static final Gson gson = new GsonBuilder().setLenient().create();
public static Map<String, UUID> getUUIDs(List<String> names) throws Exception { public static Map<String, UUID> getUUIDs(List<String> names) throws Exception {
Map<String, UUID> uuidMap = new HashMap<String, UUID>(); Map<String, UUID> uuidMap = new HashMap<>();
HttpURLConnection connection = createConnection(); HttpURLConnection connection = createConnection();
String body = JSONArray.toJSONString(names); String body = gson.toJson(names);
writeBody(connection, body); writeBody(connection, body);
JSONArray array = (JSONArray) jsonParser.parse(new InputStreamReader(connection.getInputStream())); JsonArray array = gson.fromJson(new InputStreamReader(connection.getInputStream()), JsonArray.class);
for (Object profile : array) { for (JsonElement profile : array) {
JSONObject jsonProfile = (JSONObject) profile; JsonObject jsonProfile = (JsonObject) profile;
String id = (String) jsonProfile.get("id"); String id = jsonProfile.get("id").getAsString();
String name = (String) jsonProfile.get("name"); String name = jsonProfile.get("name").getAsString();
UUID uuid = UUIDFetcher.getUUID(id); UUID uuid = getUUID(id);
uuidMap.put(name, uuid); uuidMap.put(name, uuid);
} }
return uuidMap; return uuidMap;
} }
private static void writeBody(HttpURLConnection connection, String body) throws Exception { private static void writeBody(HttpURLConnection connection, String body) throws Exception {
OutputStream stream = connection.getOutputStream(); OutputStream stream = connection.getOutputStream();
stream.write(body.getBytes()); stream.write(body.getBytes());
stream.flush(); stream.flush();
stream.close(); stream.close();
} }
private static HttpURLConnection createConnection() throws Exception { private static HttpURLConnection createConnection() throws Exception {
URL url = new URL(PROFILE_URL); URL url = new URL(PROFILE_URL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST"); connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestProperty("Content-Type", "application/json");
connection.setUseCaches(false); connection.setUseCaches(false);
connection.setDoInput(true); connection.setDoInput(true);
connection.setDoOutput(true); connection.setDoOutput(true);
return connection; return connection;
} }
private static UUID getUUID(String id) { private static UUID getUUID(String id) {
return UUID.fromString(id.substring(0, 8) + "-" + id.substring(8, 12) + "-" + id.substring(12, 16) + "-" + id.substring(16, 20) + "-" + id.substring(20, 32)); return UUID.fromString(id.substring(0, 8) + "-" + id.substring(8, 12) + "-" + id.substring(12, 16) + "-" + id.substring(16, 20) + "-" + id.substring(20, 32));
} }
}
public static byte[] toBytes(UUID uuid) {
ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]);
byteBuffer.putLong(uuid.getMostSignificantBits());
byteBuffer.putLong(uuid.getLeastSignificantBits());
return byteBuffer.array();
}
public static UUID fromBytes(byte[] array) {
if (array.length != 16) {
throw new IllegalArgumentException("Illegal byte array length: " + array.length);
}
ByteBuffer byteBuffer = ByteBuffer.wrap(array);
long mostSignificant = byteBuffer.getLong();
long leastSignificant = byteBuffer.getLong();
return new UUID(mostSignificant, leastSignificant);
}
}

View File

@ -1,194 +1,298 @@
package de.diddiz.util; package de.diddiz.LogBlock.util;
import java.io.File; import java.io.ByteArrayInputStream;
import java.io.FilenameFilter; import java.io.ByteArrayOutputStream;
import java.text.ParseException; import java.io.File;
import java.text.SimpleDateFormat; import java.io.FilenameFilter;
import java.util.ArrayList; import java.io.IOException;
import java.util.List; import java.io.InputStreamReader;
import java.util.regex.Matcher; import java.io.OutputStreamWriter;
import java.util.regex.Pattern; import java.text.ParseException;
import java.text.SimpleDateFormat;
public class Utils { import java.util.ArrayList;
public static String newline = System.getProperty("line.separator"); import java.util.List;
import java.util.regex.Matcher;
public static boolean isInt(String str) { import java.util.regex.Pattern;
try { import java.util.zip.GZIPInputStream;
Integer.parseInt(str); import java.util.zip.GZIPOutputStream;
return true; import java.util.zip.ZipException;
} catch (final NumberFormatException ex) {
} import org.bukkit.configuration.InvalidConfigurationException;
return false; import org.bukkit.configuration.file.YamlConfiguration;
} import org.bukkit.inventory.ItemStack;
public static boolean isShort(String str) { import de.diddiz.LogBlock.LogBlock;
try {
Short.parseShort(str); public class Utils {
return true; public static String newline = System.getProperty("line.separator");
} catch (final NumberFormatException ex) {
} public static boolean isInt(String str) {
return false; try {
} Integer.parseInt(str);
return true;
public static boolean isByte(String str) { } catch (final NumberFormatException ex) {
try { }
Byte.parseByte(str); return false;
return true; }
} catch (final NumberFormatException ex) {
} public static boolean isShort(String str) {
return false; try {
} Short.parseShort(str);
return true;
public static String listing(String[] entries, String delimiter, String finalDelimiter) { } catch (final NumberFormatException ex) {
final int len = entries.length; }
if (len == 0) { return false;
return ""; }
}
if (len == 1) { public static boolean isByte(String str) {
return entries[0]; try {
} Byte.parseByte(str);
final StringBuilder builder = new StringBuilder(entries[0]); return true;
for (int i = 1; i < len - 1; i++) { } catch (final NumberFormatException ex) {
builder.append(delimiter).append(entries[i]); }
} return false;
builder.append(finalDelimiter).append(entries[len - 1]); }
return builder.toString();
} public static String listing(String[] entries, String delimiter, String finalDelimiter) {
final int len = entries.length;
public static String listing(List<?> entries, String delimiter, String finalDelimiter) { if (len == 0) {
final int len = entries.size(); return "";
if (len == 0) { }
return ""; if (len == 1) {
} return entries[0];
if (len == 1) { }
return entries.get(0).toString(); final StringBuilder builder = new StringBuilder(entries[0]);
} for (int i = 1; i < len - 1; i++) {
final StringBuilder builder = new StringBuilder(entries.get(0).toString()); builder.append(delimiter).append(entries[i]);
for (int i = 1; i < len - 1; i++) { }
builder.append(delimiter).append(entries.get(i).toString()); builder.append(finalDelimiter).append(entries[len - 1]);
} return builder.toString();
builder.append(finalDelimiter).append(entries.get(len - 1).toString()); }
return builder.toString();
} public static String listing(List<?> entries, String delimiter, String finalDelimiter) {
final int len = entries.size();
public static int parseTimeSpec(String[] spec) { if (len == 0) {
if (spec == null || spec.length < 1 || spec.length > 2) { return "";
return -1; }
} if (len == 1) {
if (spec.length == 1 && isInt(spec[0])) { return entries.get(0).toString();
return Integer.valueOf(spec[0]); }
} final StringBuilder builder = new StringBuilder(entries.get(0).toString());
if (!spec[0].contains(":") && !spec[0].contains(".")) { for (int i = 1; i < len - 1; i++) {
if (spec.length == 2) { builder.append(delimiter).append(entries.get(i).toString());
if (!isInt(spec[0])) { }
return -1; builder.append(finalDelimiter).append(entries.get(len - 1).toString());
} return builder.toString();
int min = Integer.parseInt(spec[0]); }
if (spec[1].startsWith("h")) {
min *= 60; public static int parseTimeSpec(String[] spec) {
} else if (spec[1].startsWith("d")) { if (spec == null || spec.length < 1 || spec.length > 2) {
min *= 1440; return -1;
} }
return min; if (spec.length == 1 && isInt(spec[0])) {
} else if (spec.length == 1) { return Integer.valueOf(spec[0]);
int days = 0, hours = 0, minutes = 0; }
int lastIndex = 0, currIndex = 1; if (!spec[0].contains(":") && !spec[0].contains(".")) {
while (currIndex <= spec[0].length()) { if (spec.length == 2) {
while (currIndex <= spec[0].length() && isInt(spec[0].substring(lastIndex, currIndex))) { if (!isInt(spec[0])) {
currIndex++; return -1;
} }
if (currIndex - 1 != lastIndex) { int min = Integer.parseInt(spec[0]);
if (currIndex > spec[0].length()) { if (spec[1].startsWith("h")) {
return -1; min *= 60;
} } else if (spec[1].startsWith("d")) {
final String param = spec[0].substring(currIndex - 1, currIndex).toLowerCase(); min *= 1440;
if (param.equals("d")) { }
days = Integer.parseInt(spec[0].substring(lastIndex, currIndex - 1)); return min;
} else if (param.equals("h")) { } else if (spec.length == 1) {
hours = Integer.parseInt(spec[0].substring(lastIndex, currIndex - 1)); int days = 0, hours = 0, minutes = 0;
} else if (param.equals("m")) { int lastIndex = 0, currIndex = 1;
minutes = Integer.parseInt(spec[0].substring(lastIndex, currIndex - 1)); while (currIndex <= spec[0].length()) {
} while (currIndex <= spec[0].length() && isInt(spec[0].substring(lastIndex, currIndex))) {
} currIndex++;
lastIndex = currIndex; }
currIndex++; if (currIndex - 1 != lastIndex) {
} if (currIndex > spec[0].length()) {
if (days == 0 && hours == 0 && minutes == 0) { return -1;
return -1; }
} final String param = spec[0].substring(currIndex - 1, currIndex).toLowerCase();
return minutes + hours * 60 + days * 1440; if (param.equals("d")) {
} else { days = Integer.parseInt(spec[0].substring(lastIndex, currIndex - 1));
return -1; } else if (param.equals("h")) {
} hours = Integer.parseInt(spec[0].substring(lastIndex, currIndex - 1));
} } else if (param.equals("m")) {
final String timestamp; minutes = Integer.parseInt(spec[0].substring(lastIndex, currIndex - 1));
if (spec.length == 1) { }
if (spec[0].contains(":")) { }
timestamp = new SimpleDateFormat("dd.MM.yyyy").format(System.currentTimeMillis()) + " " + spec[0]; lastIndex = currIndex;
} else { currIndex++;
timestamp = spec[0] + " 00:00:00"; }
} if (days == 0 && hours == 0 && minutes == 0) {
} else { return -1;
timestamp = spec[0] + " " + spec[1]; }
} return minutes + hours * 60 + days * 1440;
try { } else {
return (int) ((System.currentTimeMillis() - new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").parse(timestamp).getTime()) / 60000); return -1;
} catch (final ParseException ex) { }
return -1; }
} final String timestamp;
} if (spec.length == 1) {
if (spec[0].contains(":")) {
public static String spaces(int count) { timestamp = new SimpleDateFormat("dd.MM.yyyy").format(System.currentTimeMillis()) + " " + spec[0];
final StringBuilder filled = new StringBuilder(count); } else {
for (int i = 0; i < count; i++) { timestamp = spec[0] + " 00:00:00";
filled.append(' '); }
} } else {
return filled.toString(); timestamp = spec[0] + " " + spec[1];
} }
try {
public static String join(String[] s, String delimiter) { return (int) ((System.currentTimeMillis() - new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").parse(timestamp).getTime()) / 60000);
if (s == null || s.length == 0) { } catch (final ParseException ex) {
return ""; return -1;
} }
final int len = s.length; }
final StringBuilder builder = new StringBuilder(s[0]);
for (int i = 1; i < len; i++) { public static String spaces(int count) {
builder.append(delimiter).append(s[i]); final StringBuilder filled = new StringBuilder(count);
} for (int i = 0; i < count; i++) {
return builder.toString(); filled.append(' ');
} }
return filled.toString();
/** }
* Converts a list of arguments e.g ['lb', 'clearlog', 'world', '"my', 'world', 'of', 'swag"']
* into a list of arguments with any text encapsulated by quotes treated as one word public static String join(String[] s, String delimiter) {
* For this particular example: ['lb', 'clearlog', 'world', '"my world of swag"'] if (s == null || s.length == 0) {
* return "";
* @param args The list of arguments }
* @return A new list with the quoted arguments parsed to single values final int len = s.length;
*/ final StringBuilder builder = new StringBuilder(s[0]);
public static List<String> parseQuotes(List<String> args) { for (int i = 1; i < len; i++) {
List<String> newArguments = new ArrayList<String>(); builder.append(delimiter).append(s[i]);
String subjectString = join(args.toArray(new String[args.size()]), " "); }
return builder.toString();
Pattern regex = Pattern.compile("[^\\s\"']+|\"[^\"]*\"|'[^']*'"); }
Matcher regexMatcher = regex.matcher(subjectString);
while (regexMatcher.find()) { /**
newArguments.add(regexMatcher.group()); * Converts a list of arguments e.g ['lb', 'clearlog', 'world', '"my', 'world', 'of', 'swag"']
} * into a list of arguments with any text encapsulated by quotes treated as one word
* For this particular example: ['lb', 'clearlog', 'world', '"my world of swag"']
return newArguments; *
} * @param args The list of arguments
* @return A new list with the quoted arguments parsed to single values
public static class ExtensionFilenameFilter implements FilenameFilter { */
private final String ext; public static List<String> parseQuotes(List<String> args) {
List<String> newArguments = new ArrayList<>();
public ExtensionFilenameFilter(String ext) { String subjectString = join(args.toArray(new String[args.size()]), " ");
this.ext = ext;
} Pattern regex = Pattern.compile("[^\\s\"']+|\"[^\"]*\"|'[^']*'");
Matcher regexMatcher = regex.matcher(subjectString);
@Override while (regexMatcher.find()) {
public boolean accept(File dir, String name) { newArguments.add(regexMatcher.group());
return name.toLowerCase().endsWith(ext); }
}
} return newArguments;
} }
public static class ExtensionFilenameFilter implements FilenameFilter {
private final String ext;
public ExtensionFilenameFilter(String ext) {
this.ext = "." + ext;
}
@Override
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(ext);
}
}
private final static char[] hexArray = "0123456789ABCDEF".toCharArray();
public static String mysqlEscapeBytes(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2 + 2];
hexChars[0] = '0';
hexChars[1] = 'x';
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2 + 2] = hexArray[v >>> 4];
hexChars[j * 2 + 3] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
public static String mysqlPrepareBytesForInsertAllowNull(byte[] bytes) {
if (bytes == null) {
return "null";
}
return "'" + mysqlEscapeBytes(bytes) + "'";
}
public static String mysqlTextEscape(String untrusted) {
return untrusted.replace("\\", "\\\\").replace("'", "\\'");
}
public static ItemStack loadItemStack(byte[] data) {
if (data == null || data.length == 0) {
return null;
}
YamlConfiguration conf = deserializeYamlConfiguration(data);
return conf == null ? null : conf.getItemStack("stack");
}
public static byte[] saveItemStack(ItemStack stack) {
if (stack == null || BukkitUtils.isEmpty(stack.getType())) {
return null;
}
YamlConfiguration conf = new YamlConfiguration();
conf.set("stack", stack);
return serializeYamlConfiguration(conf);
}
public static YamlConfiguration deserializeYamlConfiguration(byte[] data) {
if (data == null || data.length == 0) {
return null;
}
YamlConfiguration conf = new YamlConfiguration();
try {
InputStreamReader reader = new InputStreamReader(new GZIPInputStream(new ByteArrayInputStream(data)), "UTF-8");
conf.load(reader);
reader.close();
return conf;
} catch (ZipException | InvalidConfigurationException e) {
LogBlock.getInstance().getLogger().warning("Could not deserialize YamlConfiguration: " + e.getMessage());
return conf;
} catch (IOException e) {
throw new RuntimeException("IOException should be impossible for ByteArrayInputStream", e);
}
}
public static byte[] serializeYamlConfiguration(YamlConfiguration conf) {
if (conf == null || conf.getKeys(false).isEmpty()) {
return null;
}
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(new GZIPOutputStream(baos), "UTF-8");
writer.write(conf.saveToString());
writer.close();
return baos.toByteArray();
} catch (IOException e) {
throw new RuntimeException("IOException should be impossible for ByteArrayOutputStream", e);
}
}
public static String serializeForSQL(YamlConfiguration conf) {
return mysqlPrepareBytesForInsertAllowNull(serializeYamlConfiguration(conf));
}
public static double warpDegrees(double degrees) {
double d = degrees % 360.0;
if (d >= 180.0) {
d -= 360.0;
}
if (d < -180.0) {
d += 360.0;
}
return d;
}
}

View File

@ -0,0 +1,181 @@
package de.diddiz.LogBlock.worldedit;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.UUID;
import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.util.BlockVector;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.jnbt.DoubleTag;
import com.sk89q.jnbt.FloatTag;
import com.sk89q.jnbt.ListTag;
import com.sk89q.jnbt.NBTInputStream;
import com.sk89q.jnbt.NBTOutputStream;
import com.sk89q.jnbt.NamedTag;
import com.sk89q.jnbt.ShortTag;
import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.IncompleteRegionException;
import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.bukkit.WorldEditPlugin;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.regions.Region;
import de.diddiz.LogBlock.LogBlock;
import de.diddiz.LogBlock.util.CuboidRegion;
public class WorldEditHelper {
private static boolean checkedForWorldEdit;
private static boolean hasWorldEdit;
public static boolean hasWorldEdit() {
if (!checkedForWorldEdit) {
checkedForWorldEdit = true;
Plugin worldEdit = Bukkit.getPluginManager().getPlugin("WorldEdit");
hasWorldEdit = worldEdit != null;
if (worldEdit != null) {
Internal.setWorldEdit(worldEdit);
}
}
return hasWorldEdit;
}
public static boolean hasFullWorldEdit() {
return hasWorldEdit && Internal.hasBukkitImplAdapter();
}
public static byte[] serializeEntity(Entity entity) {
if (!hasWorldEdit()) {
return null;
}
return Internal.serializeEntity(entity);
}
public static Entity restoreEntity(Location location, EntityType type, byte[] serialized) {
if (!hasWorldEdit()) {
return null;
}
return Internal.restoreEntity(location, type, serialized);
}
public static CuboidRegion getSelectedRegion(Player player) throws IllegalArgumentException {
if (!hasWorldEdit()) {
throw new IllegalArgumentException("WorldEdit not found!");
}
return Internal.getSelectedRegion(player);
}
private static class Internal {
private static WorldEditPlugin worldEdit;
private static Method getBukkitImplAdapter;
public static void setWorldEdit(Plugin worldEdit) {
Internal.worldEdit = (WorldEditPlugin) worldEdit;
}
public static boolean hasBukkitImplAdapter() {
if (getBukkitImplAdapter == null) {
try {
getBukkitImplAdapter = WorldEditPlugin.class.getDeclaredMethod("getBukkitImplAdapter");
getBukkitImplAdapter.setAccessible(true);
} catch (Exception e) {
LogBlock.getInstance().getLogger().log(Level.SEVERE, "Exception while checking for BukkitImplAdapter", e);
return false;
}
}
try {
return getBukkitImplAdapter.invoke(worldEdit) != null;
} catch (Exception e) {
LogBlock.getInstance().getLogger().log(Level.SEVERE, "Exception while checking for BukkitImplAdapter", e);
return false;
}
}
public static Entity restoreEntity(Location location, EntityType type, byte[] serialized) {
com.sk89q.worldedit.world.entity.EntityType weType = BukkitAdapter.adapt(type);
com.sk89q.worldedit.util.Location weLocation = BukkitAdapter.adapt(location);
try {
NBTInputStream nbtis = new NBTInputStream(new ByteArrayInputStream(serialized));
NamedTag namedTag = nbtis.readNamedTag();
nbtis.close();
UUID newUUID = null;
if (namedTag.getName().equals("entity") && namedTag.getTag() instanceof CompoundTag) {
CompoundTag serializedState = (CompoundTag) namedTag.getTag();
BaseEntity state = new BaseEntity(weType, serializedState);
com.sk89q.worldedit.entity.Entity weEntity = weLocation.getExtent().createEntity(weLocation, state);
if (weEntity != null) {
CompoundTag newNbt = weEntity.getState().getNbtData();
int[] uuidInts = newNbt.getIntArray("UUID");
if (uuidInts != null && uuidInts.length >= 4) {
newUUID = new UUID(((long) uuidInts[0] << 32) | (uuidInts[1] & 0xFFFFFFFFL), ((long) uuidInts[2] << 32) | (uuidInts[3] & 0xFFFFFFFFL));
} else {
newUUID = new UUID(newNbt.getLong("UUIDMost"), newNbt.getLong("UUIDLeast")); // pre 1.16
}
}
}
return newUUID == null ? null : Bukkit.getEntity(newUUID);
} catch (IOException e) {
throw new RuntimeException("This IOException should be impossible", e);
}
}
public static byte[] serializeEntity(Entity entity) {
com.sk89q.worldedit.entity.Entity weEntity = BukkitAdapter.adapt(entity);
BaseEntity state = weEntity.getState();
if (state != null) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
NBTOutputStream nbtos = new NBTOutputStream(baos);
CompoundTag nbt = state.getNbtData();
LinkedHashMap<String, Tag<?, ?>> value = new LinkedHashMap<>(nbt.getValue());
value.put("Health", new FloatTag(20.0f));
value.put("Motion", new ListTag(DoubleTag.class, Arrays.asList(new DoubleTag[] { new DoubleTag(0), new DoubleTag(0), new DoubleTag(0) })));
value.put("Fire", new ShortTag((short) -20));
value.put("HurtTime", new ShortTag((short) 0));
nbtos.writeNamedTag("entity", new CompoundTag(value));
nbtos.close();
return baos.toByteArray();
} catch (IOException e) {
throw new RuntimeException("This IOException should be impossible", e);
}
}
return null;
}
public static CuboidRegion getSelectedRegion(Player player) throws IllegalArgumentException {
LocalSession session = worldEdit.getSession(player);
World world = player.getWorld();
com.sk89q.worldedit.world.World weWorld = BukkitAdapter.adapt(world);
if (!weWorld.equals(session.getSelectionWorld())) {
throw new IllegalArgumentException("No selection defined");
}
Region selection;
try {
selection = session.getSelection(weWorld);
} catch (IncompleteRegionException e) {
throw new IllegalArgumentException("No selection defined");
}
if (selection == null) {
throw new IllegalArgumentException("No selection defined");
}
if (!(selection instanceof com.sk89q.worldedit.regions.CuboidRegion)) {
throw new IllegalArgumentException("You have to define a cuboid selection");
}
BlockVector3 min = selection.getMinimumPoint();
BlockVector3 max = selection.getMaximumPoint();
return new CuboidRegion(world, new BlockVector(min.getBlockX(), min.getBlockY(), min.getBlockZ()), new BlockVector(max.getBlockX(), max.getBlockY(), max.getBlockZ()));
}
}
}

View File

@ -0,0 +1,106 @@
package de.diddiz.LogBlock.worldedit;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.event.extent.EditSessionEvent;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.extent.AbstractDelegateExtent;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.util.eventbus.Subscribe;
import com.sk89q.worldedit.world.block.BlockStateHolder;
import de.diddiz.LogBlock.LogBlock;
import de.diddiz.LogBlock.Logging;
import de.diddiz.LogBlock.blockstate.BlockStateCodecs;
import de.diddiz.LogBlock.config.Config;
import de.diddiz.LogBlock.util.BukkitUtils;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData;
import java.util.logging.Level;
//...so they ALSO have a class called Actor... need to fully-qualify when we use ours
public class WorldEditLoggingHook {
private LogBlock plugin;
public WorldEditLoggingHook(LogBlock plugin) {
this.plugin = plugin;
}
// Convert WE Actor to LB Actor
private de.diddiz.LogBlock.Actor AtoA(Actor weActor) {
if (weActor.isPlayer()) {
return new de.diddiz.LogBlock.Actor(weActor.getName(), weActor.getUniqueId());
}
return new de.diddiz.LogBlock.Actor(weActor.getName());
}
public void hook() {
WorldEdit.getInstance().getEventBus().register(new Object() {
@Subscribe
public void wrapForLogging(final EditSessionEvent event) {
final Actor actor = event.getActor();
if (actor == null) {
return;
}
final de.diddiz.LogBlock.Actor lbActor = AtoA(actor);
// Check to ensure the world should be logged
final World world;
try {
world = BukkitAdapter.adapt(event.getWorld());
} catch (RuntimeException ex) {
plugin.getLogger().warning("Failed to register logging for WorldEdit!");
plugin.getLogger().log(Level.WARNING, ex.getMessage(), ex);
return;
}
// If config becomes reloadable, this check should be moved
if (!(Config.isLogging(world, Logging.WORLDEDIT))) {
return;
}
event.setExtent(new AbstractDelegateExtent(event.getExtent()) {
@Override
public final <B extends BlockStateHolder<B>> boolean setBlock(BlockVector3 position, B block) throws WorldEditException {
onBlockChange(position, block);
return super.setBlock(position, block);
}
protected <B extends BlockStateHolder<B>> void onBlockChange(BlockVector3 pt, B block) {
if (event.getStage() != EditSession.Stage.BEFORE_CHANGE) {
return;
}
Location location = BukkitAdapter.adapt(world, pt);
Block blockBefore = location.getBlock();
BlockData blockDataBefore = blockBefore.getBlockData();
Material typeBefore = blockDataBefore.getMaterial();
BlockData blockDataNew = BukkitAdapter.adapt(block);
if (!blockDataBefore.equals(blockDataNew)) {
// Check to see if we've broken a sign
if (BlockStateCodecs.hasCodec(typeBefore)) {
plugin.getConsumer().queueBlockBreak(lbActor, blockBefore.getState());
} else if (!BukkitUtils.isEmpty(typeBefore)) {
plugin.getConsumer().queueBlockBreak(lbActor, location, blockDataBefore);
}
if (!BukkitUtils.isEmpty(blockDataNew.getMaterial())) {
plugin.getConsumer().queueBlockPlace(lbActor, location, blockDataNew);
}
}
}
});
}
});
}
}

View File

@ -1,34 +0,0 @@
package de.diddiz.util;
import java.util.List;
public class Block {
private int block;
private int data;
/**
* @param block The id of the block
* @param data The data for the block, -1 for any data
*/
public Block(int block, int data) {
this.block = block;
this.data = data;
}
public int getBlock() {
return this.block;
}
public int getData() {
return this.data;
}
public static boolean inList(List<Block> types, int blockID) {
for (Block block : types) {
if (block.getBlock() == blockID) {
return true;
}
}
return false;
}
}

View File

@ -1,435 +0,0 @@
package de.diddiz.util;
import org.bukkit.*;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import org.bukkit.block.DoubleChest;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import java.io.File;
import java.util.*;
import static de.diddiz.util.MaterialName.materialName;
public class BukkitUtils {
private static final Set<Set<Integer>> blockEquivalents;
private static final Set<Material> relativeBreakable;
private static final Set<Material> relativeTopBreakable;
private static final Set<Material> relativeTopFallables;
private static final Set<Material> fallingEntityKillers;
private static final Set<Material> cropBlocks;
private static final Set<Material> containerBlocks;
private static final Map<EntityType, Integer> projectileItems;
static {
blockEquivalents = new HashSet<Set<Integer>>(7);
blockEquivalents.add(new HashSet<Integer>(Arrays.asList(2, 3, 60)));
blockEquivalents.add(new HashSet<Integer>(Arrays.asList(8, 9, 79)));
blockEquivalents.add(new HashSet<Integer>(Arrays.asList(10, 11)));
blockEquivalents.add(new HashSet<Integer>(Arrays.asList(61, 62)));
blockEquivalents.add(new HashSet<Integer>(Arrays.asList(73, 74)));
blockEquivalents.add(new HashSet<Integer>(Arrays.asList(75, 76)));
blockEquivalents.add(new HashSet<Integer>(Arrays.asList(93, 94)));
// Blocks that break when they are attached to a block
relativeBreakable = new HashSet<Material>(11);
relativeBreakable.add(Material.WALL_SIGN);
relativeBreakable.add(Material.LADDER);
relativeBreakable.add(Material.STONE_BUTTON);
relativeBreakable.add(Material.WOOD_BUTTON);
relativeBreakable.add(Material.REDSTONE_TORCH_ON);
relativeBreakable.add(Material.REDSTONE_TORCH_OFF);
relativeBreakable.add(Material.LEVER);
relativeBreakable.add(Material.TORCH);
relativeBreakable.add(Material.TRAP_DOOR);
relativeBreakable.add(Material.TRIPWIRE_HOOK);
relativeBreakable.add(Material.COCOA);
// Blocks that break when they are on top of a block
relativeTopBreakable = new HashSet<Material>(33);
relativeTopBreakable.add(Material.SAPLING);
relativeTopBreakable.add(Material.LONG_GRASS);
relativeTopBreakable.add(Material.DEAD_BUSH);
relativeTopBreakable.add(Material.YELLOW_FLOWER);
relativeTopBreakable.add(Material.RED_ROSE);
relativeTopBreakable.add(Material.BROWN_MUSHROOM);
relativeTopBreakable.add(Material.RED_MUSHROOM);
relativeTopBreakable.add(Material.CROPS);
relativeTopBreakable.add(Material.POTATO);
relativeTopBreakable.add(Material.CARROT);
relativeTopBreakable.add(Material.WATER_LILY);
relativeTopBreakable.add(Material.CACTUS);
relativeTopBreakable.add(Material.SUGAR_CANE_BLOCK);
relativeTopBreakable.add(Material.FLOWER_POT);
relativeTopBreakable.add(Material.POWERED_RAIL);
relativeTopBreakable.add(Material.DETECTOR_RAIL);
relativeTopBreakable.add(Material.ACTIVATOR_RAIL);
relativeTopBreakable.add(Material.RAILS);
relativeTopBreakable.add(Material.REDSTONE_WIRE);
relativeTopBreakable.add(Material.SIGN_POST);
relativeTopBreakable.add(Material.STONE_PLATE);
relativeTopBreakable.add(Material.WOOD_PLATE);
relativeTopBreakable.add(Material.IRON_PLATE);
relativeTopBreakable.add(Material.GOLD_PLATE);
relativeTopBreakable.add(Material.SNOW);
relativeTopBreakable.add(Material.DIODE_BLOCK_ON);
relativeTopBreakable.add(Material.DIODE_BLOCK_OFF);
relativeTopBreakable.add(Material.REDSTONE_COMPARATOR_ON);
relativeTopBreakable.add(Material.REDSTONE_COMPARATOR_OFF);
relativeTopBreakable.add(Material.WOODEN_DOOR);
relativeTopBreakable.add(Material.IRON_DOOR_BLOCK);
relativeTopBreakable.add(Material.CARPET);
relativeTopBreakable.add(Material.DOUBLE_PLANT);
// Blocks that fall
relativeTopFallables = new HashSet<Material>(4);
relativeTopFallables.add(Material.SAND);
relativeTopFallables.add(Material.GRAVEL);
relativeTopFallables.add(Material.DRAGON_EGG);
relativeTopFallables.add(Material.ANVIL);
// Blocks that break falling entities
fallingEntityKillers = new HashSet<Material>(32);
fallingEntityKillers.add(Material.SIGN_POST);
fallingEntityKillers.add(Material.WALL_SIGN);
fallingEntityKillers.add(Material.STONE_PLATE);
fallingEntityKillers.add(Material.WOOD_PLATE);
fallingEntityKillers.add(Material.IRON_PLATE);
fallingEntityKillers.add(Material.GOLD_PLATE);
fallingEntityKillers.add(Material.SAPLING);
fallingEntityKillers.add(Material.YELLOW_FLOWER);
fallingEntityKillers.add(Material.RED_ROSE);
fallingEntityKillers.add(Material.CROPS);
fallingEntityKillers.add(Material.CARROT);
fallingEntityKillers.add(Material.POTATO);
fallingEntityKillers.add(Material.RED_MUSHROOM);
fallingEntityKillers.add(Material.BROWN_MUSHROOM);
fallingEntityKillers.add(Material.STEP);
fallingEntityKillers.add(Material.WOOD_STEP);
fallingEntityKillers.add(Material.TORCH);
fallingEntityKillers.add(Material.FLOWER_POT);
fallingEntityKillers.add(Material.POWERED_RAIL);
fallingEntityKillers.add(Material.DETECTOR_RAIL);
fallingEntityKillers.add(Material.ACTIVATOR_RAIL);
fallingEntityKillers.add(Material.RAILS);
fallingEntityKillers.add(Material.LEVER);
fallingEntityKillers.add(Material.REDSTONE_WIRE);
fallingEntityKillers.add(Material.REDSTONE_TORCH_ON);
fallingEntityKillers.add(Material.REDSTONE_TORCH_OFF);
fallingEntityKillers.add(Material.DIODE_BLOCK_ON);
fallingEntityKillers.add(Material.DIODE_BLOCK_OFF);
fallingEntityKillers.add(Material.REDSTONE_COMPARATOR_ON);
fallingEntityKillers.add(Material.REDSTONE_COMPARATOR_OFF);
fallingEntityKillers.add(Material.DAYLIGHT_DETECTOR);
fallingEntityKillers.add(Material.CARPET);
// Crop Blocks
cropBlocks = new HashSet<Material>(5);
cropBlocks.add(Material.CROPS);
cropBlocks.add(Material.MELON_STEM);
cropBlocks.add(Material.PUMPKIN_STEM);
cropBlocks.add(Material.CARROT);
cropBlocks.add(Material.POTATO);
// Container Blocks
containerBlocks = new HashSet<Material>(6);
containerBlocks.add(Material.CHEST);
containerBlocks.add(Material.TRAPPED_CHEST);
containerBlocks.add(Material.DISPENSER);
containerBlocks.add(Material.DROPPER);
containerBlocks.add(Material.HOPPER);
containerBlocks.add(Material.BREWING_STAND);
containerBlocks.add(Material.FURNACE);
containerBlocks.add(Material.BURNING_FURNACE);
containerBlocks.add(Material.BEACON);
// Doesn't actually have a block inventory
// containerBlocks.add(Material.ENDER_CHEST);
// It doesn't seem like you could injure people with some of these, but they exist, so....
projectileItems = new EnumMap<EntityType, Integer>(EntityType.class);
projectileItems.put(EntityType.ARROW, 262);
projectileItems.put(EntityType.EGG, 344);
projectileItems.put(EntityType.ENDER_PEARL, 368);
projectileItems.put(EntityType.SMALL_FIREBALL, 385); // Fire charge
projectileItems.put(EntityType.FIREBALL, 385); // Fire charge
projectileItems.put(EntityType.FISHING_HOOK, 346);
projectileItems.put(EntityType.SNOWBALL, 332);
projectileItems.put(EntityType.SPLASH_POTION, 373);
projectileItems.put(EntityType.THROWN_EXP_BOTTLE, 384);
projectileItems.put(EntityType.WITHER_SKULL, 397);
}
private static final BlockFace[] relativeBlockFaces = new BlockFace[]{
BlockFace.EAST, BlockFace.WEST, BlockFace.NORTH, BlockFace.SOUTH, BlockFace.UP, BlockFace.DOWN
};
/**
* Returns a list of block locations around the block that are of the type specified by the integer list parameter
*
* @param block The central block to get the blocks around
* @param type The type of blocks around the center block to return
* @return List of block locations around the block that are of the type specified by the integer list parameter
*/
public static List<Location> getBlocksNearby(org.bukkit.block.Block block, Set<Material> type) {
ArrayList<Location> blocks = new ArrayList<Location>();
for (BlockFace blockFace : relativeBlockFaces) {
if (type.contains(block.getRelative(blockFace).getType())) {
blocks.add(block.getRelative(blockFace).getLocation());
}
}
return blocks;
}
public static boolean isTop(Material mat, byte data) {
switch (mat) {
case DOUBLE_PLANT:
return data > 5;
case IRON_DOOR_BLOCK:
case WOODEN_DOOR:
return data == 8 || data == 9;
default:
return false;
}
}
public static int getInventoryHolderType(InventoryHolder holder) {
if (holder instanceof DoubleChest) {
return getInventoryHolderType(((DoubleChest) holder).getLeftSide());
} else if (holder instanceof BlockState) {
return ((BlockState) holder).getTypeId();
} else {
return -1;
}
}
public static Location getInventoryHolderLocation(InventoryHolder holder) {
if (holder instanceof DoubleChest) {
return getInventoryHolderLocation(((DoubleChest) holder).getLeftSide());
} else if (holder instanceof BlockState) {
return ((BlockState) holder).getLocation();
} else {
return null;
}
}
public static ItemStack[] compareInventories(ItemStack[] items1, ItemStack[] items2) {
final ItemStackComparator comperator = new ItemStackComparator();
final ArrayList<ItemStack> diff = new ArrayList<ItemStack>();
final int l1 = items1.length, l2 = items2.length;
int c1 = 0, c2 = 0;
while (c1 < l1 || c2 < l2) {
if (c1 >= l1) {
diff.add(items2[c2]);
c2++;
continue;
}
if (c2 >= l2) {
items1[c1].setAmount(items1[c1].getAmount() * -1);
diff.add(items1[c1]);
c1++;
continue;
}
final int comp = comperator.compare(items1[c1], items2[c2]);
if (comp < 0) {
items1[c1].setAmount(items1[c1].getAmount() * -1);
diff.add(items1[c1]);
c1++;
} else if (comp > 0) {
diff.add(items2[c2]);
c2++;
} else {
final int amount = items2[c2].getAmount() - items1[c1].getAmount();
if (amount != 0) {
items1[c1].setAmount(amount);
diff.add(items1[c1]);
}
c1++;
c2++;
}
}
return diff.toArray(new ItemStack[diff.size()]);
}
public static ItemStack[] compressInventory(ItemStack[] items) {
final ArrayList<ItemStack> compressed = new ArrayList<ItemStack>();
for (final ItemStack item : items) {
if (item != null) {
final int type = item.getTypeId();
final short data = rawData(item);
boolean found = false;
for (final ItemStack item2 : compressed) {
if (type == item2.getTypeId() && data == rawData(item2)) {
item2.setAmount(item2.getAmount() + item.getAmount());
found = true;
break;
}
}
if (!found) {
compressed.add(new ItemStack(type, item.getAmount(), data));
}
}
}
Collections.sort(compressed, new ItemStackComparator());
return compressed.toArray(new ItemStack[compressed.size()]);
}
public static boolean equalTypes(int type1, int type2) {
if (type1 == type2) {
return true;
}
for (final Set<Integer> equivalent : blockEquivalents) {
if (equivalent.contains(type1) && equivalent.contains(type2)) {
return true;
}
}
return false;
}
public static String friendlyWorldname(String worldName) {
return new File(worldName).getName();
}
public static Set<Set<Integer>> getBlockEquivalents() {
return blockEquivalents;
}
public static Set<Material> getRelativeBreakables() {
return relativeBreakable;
}
public static Set<Material> getRelativeTopBreakabls() {
return relativeTopBreakable;
}
public static Set<Material> getRelativeTopFallables() {
return relativeTopFallables;
}
public static Set<Material> getFallingEntityKillers() {
return fallingEntityKillers;
}
public static Set<Material> getCropBlocks() {
return cropBlocks;
}
public static Set<Material> getContainerBlocks() {
return containerBlocks;
}
public static String entityName(Entity entity) {
if (entity instanceof Player) {
return ((Player) entity).getName();
}
if (entity instanceof TNTPrimed) {
return "TNT";
}
return entity.getClass().getSimpleName().substring(5);
}
public static void giveTool(Player player, int type) {
final Inventory inv = player.getInventory();
if (inv.contains(type)) {
player.sendMessage(ChatColor.RED + "You have already a " + materialName(type));
} else {
final int free = inv.firstEmpty();
if (free >= 0) {
if (player.getItemInHand() != null && player.getItemInHand().getTypeId() != 0) {
inv.setItem(free, player.getItemInHand());
}
player.setItemInHand(new ItemStack(type, 1));
player.sendMessage(ChatColor.GREEN + "Here's your " + materialName(type));
} else {
player.sendMessage(ChatColor.RED + "You have no empty slot in your inventory");
}
}
}
public static short rawData(ItemStack item) {
return item.getType() != null ? item.getData() != null ? item.getDurability() : 0 : 0;
}
public static int saveSpawnHeight(Location loc) {
final World world = loc.getWorld();
final Chunk chunk = world.getChunkAt(loc);
if (!world.isChunkLoaded(chunk)) {
world.loadChunk(chunk);
}
final int x = loc.getBlockX(), z = loc.getBlockZ();
int y = loc.getBlockY();
boolean lower = world.getBlockTypeIdAt(x, y, z) == 0, upper = world.getBlockTypeIdAt(x, y + 1, z) == 0;
while ((!lower || !upper) && y != 127) {
lower = upper;
upper = world.getBlockTypeIdAt(x, ++y, z) == 0;
}
while (world.getBlockTypeIdAt(x, y - 1, z) == 0 && y != 0) {
y--;
}
return y;
}
public static int modifyContainer(BlockState b, ItemStack item) {
if (b instanceof InventoryHolder) {
final Inventory inv = ((InventoryHolder) b).getInventory();
if (item.getAmount() < 0) {
item.setAmount(-item.getAmount());
final ItemStack tmp = inv.removeItem(item).get(0);
return tmp != null ? tmp.getAmount() : 0;
} else if (item.getAmount() > 0) {
final ItemStack tmp = inv.addItem(item).get(0);
return tmp != null ? tmp.getAmount() : 0;
}
}
return 0;
}
public static boolean canFall(World world, int x, int y, int z) {
Material mat = world.getBlockAt(x, y, z).getType();
// Air
if (mat == Material.AIR) {
return true;
} else if (mat == Material.WATER || mat == Material.STATIONARY_WATER || mat == Material.LAVA || mat == Material.STATIONARY_LAVA) { // Fluids
return true;
} else if (getFallingEntityKillers().contains(mat) || mat == Material.FIRE || mat == Material.VINE || mat == Material.LONG_GRASS || mat == Material.DEAD_BUSH) { // Misc.
return true;
}
return false;
}
public static class ItemStackComparator implements Comparator<ItemStack> {
@Override
public int compare(ItemStack a, ItemStack b) {
final int aType = a.getTypeId(), bType = b.getTypeId();
if (aType < bType) {
return -1;
}
if (aType > bType) {
return 1;
}
final short aData = rawData(a), bData = rawData(b);
if (aData < bData) {
return -1;
}
if (aData > bData) {
return 1;
}
return 0;
}
}
public static int itemIDfromProjectileEntity(Entity e) {
Integer i = projectileItems.get(e.getType());
return (i == null) ? 0 : i;
}
}

Some files were not shown because too many files have changed in this diff Show More