18 Commits

Author SHA1 Message Date
fbd8d10461 Added metrics 2022-04-26 14:37:24 -05:00
a4d71b2c0b Minor Change
- Version change to Beta 1.0 RC01
- Adjusted the way the listeners are registered
2022-04-25 16:33:54 -05:00
bb9bdcdf03 [Beta] Snapshot v20220425
Changelog:
- Added RandomEffect, which gives a user a random positive potion effect on respawn or teleport.
- Implemented a class loading system with reflections to dynamically load all listeners.
- Adjusted rarity values in the config.yml
- Added a positiveEffects list to ListBox for RandomEffect event
- Adjusted the behavior of the BlockDrops event.
2022-04-25 16:30:38 -05:00
1bf2a818ec [Beta] SNAPSHOT {Bug Fix} Patch 0002
- Fixed command completions which still did not return correctly. They should now return correctly.
2022-04-25 13:40:18 -05:00
0488e1d6b1 [Beta] SNAPSHOT {Bug Fix} - Patch 0001
Changelog:
- Fixed an issue where the integrity checker for the main config would delete the entire data folder if the config was corrupted.
- Fixed an issue where the Rarity check was not working as intended; it would either return false if any Rarity other than NONE was set, and throw a new IllegalArgumentException if the switch clause exited with no return value.
- Fixed an issue where the command /luck reload -m did absolutely nothing.
- Fixed an issue where the proper command arguments were not being Tab Completed.
2022-04-24 19:54:40 -05:00
330fd278b7 Beta 20220422-SNAPSHOT
Changelog:
 - Added a configuration with editable rarity values and rarity types for the different events that can occur, to give the end user more control over how often players receive the effects.
- Modified listeners to use the respective Configuration values.
- Added the ability to reload a specific player configuration with /luck reload -p <player_name>
- Added the ability to reload the main configuration using /luck reload -m
- Added an integrity checker to validate the main configuration on boot.
2022-04-22 19:25:02 -05:00
6119180b0b Add VillagerInventory.java
Added the possibility to find a special rabbits foot on a villager when trading with a BUTCHER profession.
2022-04-21 09:54:36 -05:00
fe620952bc Add CooldownTimer
Added the Cooldown Timer for when a user uses a Rabbit's Foot. This restricts a user from using rabbit's feet consecutively by forcing a 30 second cooldown between uses.
2022-04-17 14:48:05 -05:00
5b1fb352bb Beta 20220417-SNAPSHOT
Changelog:
 - Reorganized the listener registration method to register the listeners in alphabetical order
 - Added the IllOmen effect, which will apply a -25% debuff to a user's luck stat when they have the Bad Omen status effect.
 - Added the ability for Guardian lasers to inflict negative damage to a users luck stat.
 - Added a cache to the Luck class to store user values in the event that they have the Ill Omen debuff applied.
2022-04-17 14:00:22 -05:00
b424a83062 Beta 20220416-SNAPSHOT
Changelog:
 - Changed the way classes interact with the PlayerHandler by using a getter instead of the actual field.
 - Added #isTool(Material) to ItemBuilder to check if an item is, in fact, a type of tool.
 - Created the UnbreakableTool listener which will grant the unbreakable status to a tool if a user is lucky enough.
  NOTE: This is a beta feature, and while it may remain included the way it works will most likely change.
2022-04-16 17:27:54 -05:00
077ed05d74 Remove Compiler Files 2022-04-16 16:25:07 -05:00
8e70e1de73 Beta 20220414-SNAPSHOT
Changelog:
 - Added the ability to fully mature crops when using bone meal
 - Added the ability to cheat death (Grants 1 half heart and between 2.5 and 5 points of absorption)
 - Added #info, #warn, and #err to MiniComponent, and utilized these across the board.
 - Switched the #color method to use org.bukkit.ChatColor instead of TextColor for accessibility.
 - Moved loading configurations and listener registration to separate methods.
2022-04-14 14:58:14 -05:00
899768819e Beta 20220411-SNAPSHOT
Changelog:
 - Added ExpBoost feature
 - Added Special Rabbit Foot, which increases a user's luck multiplier.
 - Added ItemBuilder and MiniComponent library classes
 - Removed Messages#builder in favor of MiniComponent
2022-04-11 10:29:18 -05:00
ea50032a89 Alpha 1.0 RELEASE - Patch 0003 2022-04-10 19:10:24 -05:00
057450c585 Alpha 1.0 RELEASE 2022-04-10 18:48:09 -05:00
d08b240ff9 RC04 - Patch 0002
Changelog:
 - Renamed LuckContainer#baseValue() to getValue()
 - Changed the way files save by using the Map#values() call instead of iterating the kvp's.
 - Made the actual percentage rng value round to the closest whole number before being checked.
 - Changed Luck#defaultValue() to getDefaultValue().
2022-04-09 08:38:42 -05:00
cbc57d5971 Alpha 1.0.0 RC04
Switched PlayerHandler access modifiers in favor of Dependency Injection.
2022-04-02 21:16:08 -05:00
247a2fafc0 Bug Fix 0001
**Changelog**:
 - Fixed a bug where the PlayerListener was not being initialized
 - Included the registration for the listener inside the constructor
 - Dropped static import in favor of class parent access
 - Added to the Luck method "setValue(double)" to automatically update the configuration file when called.
 - Adjusted Luck to utilize all 1024 units when calculating the boolean for rng percentage.
2022-04-02 09:00:40 -05:00
66 changed files with 2377 additions and 768 deletions

6
.gitignore vendored
View File

@ -19,6 +19,12 @@
*.tar.gz
*.rar
# Compiler Files #
build/
.idea/
.gradle/
FeelingLucky.iml
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
replay_pid*

View File

@ -1,2 +0,0 @@
#Wed Feb 09 22:19:17 CST 2022
gradle.version=7.3.3

Binary file not shown.

3
.idea/.gitignore generated vendored
View File

@ -1,3 +0,0 @@
# Default ignored files
/shelf/
/workspace.xml

1
.idea/.name generated
View File

@ -1 +0,0 @@
FeelingLucky

6
.idea/compiler.xml generated
View File

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<bytecodeTargetLevel target="17" />
</component>
</project>

17
.idea/gradle.xml generated
View File

@ -1,17 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>
<option name="distributionType" value="DEFAULT_WRAPPED" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="modules">
<set>
<option value="$PROJECT_DIR$" />
</set>
</option>
</GradleProjectSettings>
</option>
</component>
</project>

View File

@ -1,54 +0,0 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_CryptoCipherInsecureAsymmetricCryptographicAlgorithm" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_CryptoKeyAgreementGuideonApprovedCryptographicAlgorithm" enabled="true" level="SENSEI MARKED INFO" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_CryptoKeyAgreementInsecureCryptographicAlgorithm" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_CryptoKeyPairGenerationApprovedStandardCryptographicAlgorithm" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_CryptoKeyPairGenerationInsecureCryptographicAlgorithm" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_CryptoKeyPairGenerationNonStandardCryptographicAlgorithm" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_CryptoSignatureApprovedHashingAlgorithm" enabled="true" level="SENSEI MARKED INFO" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_CryptoSignatureInsecureHashingAlgorithm" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_CryptoSignatureNonStandardHashingAlgorithm" enabled="true" level="SENSEI WARNING" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_DataInjectionParameterizeLDAPFiltersDirContextsearch" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_DataProtection-CryptographyAvoidbruteforcingUsesufficientlylongkeysizeskeyGenerator" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_DataProtection-CryptographyAvoidcryptographicweaknessUseappropriatekeypairgenerationalgorithminsecure" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_DataProtection-CryptographyAvoidcryptographicweaknessUseappropriatekeypairgenerationalgorithmnotrecommended" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_DataProtection-CryptographyAvoidcryptographicweaknessUseappropriatesecretkeygenerationalgorithmDESfamily" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_DataProtection-CryptographyAvoidcryptographicweaknessUseappropriatesecretkeygenerationalgorithmHmacfamily" enabled="true" level="SENSEI WARNING" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_DataProtection-CryptographyAvoidcryptographicweaknessUseappropriatesecretkeygenerationalgorithmHmacfamily1" enabled="true" level="SENSEI WARNING" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_DataProtection-CryptographyAvoidcryptographicweaknessUseappropriatesecretkeygenerationalgorithmOtheralgorithms" enabled="true" level="SENSEI MARKED INFO" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_DataProtection-CryptographyAvoidcryptographicweaknessUseappropriatesecretkeygenerationalgorithminsecureSecretKeyFactory" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_DataProtection-CryptographyAvoidcryptographicweaknessUseappropriatesecretkeygenerationalgorithmnotrecommendedSecretKeyFactory" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_DataProtection-CryptographyAvoidcryptographicweaknessUseappropriatesecretkeygenerationalgorithmotherSecretKeyFactory" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_DataProtection-CryptographyAvoidcryptographicweaknessUsestrongsymmetriccryptographicalgorithm" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_DataProtection-CryptographyAvoidcryptographicweaknessUsesufficientlylongkeysizeskeyGeneratorbadvalue" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_DataProtection-CryptographyAvoidcryptographicweaknessUsesufficientlylongkeysizeskeyPairGenerator" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_DataProtection-CryptographyAvoidcryptographicweaknessUsesufficientlylongkeysizeskeyPairGeneratorbadvalue" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_DataProtection-SecureDataStorageAvoiddataexposureUseCipherinsteadofNullCipher" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_InjectionAvoidCodeInjectionUseSafeConstructor1stargumentoftypeConstructor" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_InjectionAvoidCodeInjectionUseSafeConstructorargumentsbutnoConstructorargument" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_InjectionAvoidCodeInjectionUseSafeConstructornoarguments" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_InjectionAvoidSQLInjectionUseParameterizedQueriesPreparedStatement" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_InjectionAvoidSQLInjectionUseParameterizedQueriesStatement" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_InjectionAvoidXMLInjectionUsesetFeature" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_InjectionAvoidXMLInjectionUsesetSchema" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_InjectionAvoidXMLInjectionsetFeaturewithbadvalue" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_InputValidationAvoidXXEDonotsetDocumentBuilderFactoryexternal-parameter-entitiestotrue" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_InputValidationAvoidXXEDonotsetDocumentBuilderFactoryload-external-dtdtotrue" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_InputValidationAvoidXXEDonotsetDocumentBuilderFactorysetExpandEntityReferencestotrue" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_InputValidationAvoidXXEDonotsetDocumentBuilderFactorysetXIncludeAwaretotrue" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_InputValidationAvoidXXEDonotsetXMLInputFactoryPropertytotrue" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_PortabilityFlawAvoidlocaledependentcomparisonsequalsaftercaseconversion" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_TLSWeakEncryptionInsecureVersion" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_TLSWeakEncryptionOutdatedVersion" enabled="true" level="SENSEI WARNING" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_XMLExternalEntitiesDocumentBuilderFactorysetExpandEntityReferencestofalse" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_XMLExternalEntitiesDocumentBuilderFactorysetFeaturedissallow-doctype-decl" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_XMLExternalEntitiesDocumentBuilderFactorysetFeaturedissallow-doctype-declwrongboolean" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_XMLExternalEntitiesDocumentBuilderFactorysetFeatureexternal-parameter-entitiesshouldbesetfirst" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_XMLExternalEntitiesDocumentBuilderFactorysetFeatureload-external-dtd" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_XMLExternalEntitiesDocumentBuilderFactorysetXIncludeAware" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_XMLExternalEntitiesXMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
<inspection_tool class="fcd36335-ac86-4bef-8d30-062d8aae0364_XMLExternalEntitiesXMLInputFactory.SUPPORT_DTD" enabled="true" level="SENSEI ERROR" enabled_by_default="true" />
</profile>
</component>

View File

@ -1,45 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
<remote-repository>
<option name="id" value="MavenRepo" />
<option name="name" value="MavenRepo" />
<option name="url" value="https://repo.maven.apache.org/maven2/" />
</remote-repository>
<remote-repository>
<option name="id" value="papermc-repo" />
<option name="name" value="papermc-repo" />
<option name="url" value="https://papermc.io/repo/repository/maven-public/" />
</remote-repository>
<remote-repository>
<option name="id" value="sonatype" />
<option name="name" value="sonatype" />
<option name="url" value="https://oss.sonatype.org/content/groups/public/" />
</remote-repository>
<remote-repository>
<option name="id" value="sonatype" />
<option name="name" value="sonatype" />
<option name="url" value="file:/$PROJECT_DIR$/%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20url%20=%20uri(%22https:/s01.oss.sonatype.org/service/local/staging/deploy/maven2/%22)%0A" />
</remote-repository>
<remote-repository>
<option name="id" value="sonatype" />
<option name="name" value="sonatype" />
<option name="url" value="https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/" />
</remote-repository>
<remote-repository>
<option name="id" value="sonatype" />
<option name="name" value="sonatype" />
<option name="url" value="https://s01.oss.sonatype.org/content/groups/public/" />
</remote-repository>
</component>
</project>

12
.idea/misc.xml generated
View File

@ -1,12 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="EntryPointsManager">
<list size="1">
<item index="0" class="java.lang.String" itemvalue="org.bukkit.event.EventHandler" />
</list>
</component>
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="openjdk-17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@ -1,13 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="FacetManager">
<facet type="minecraft" name="Minecraft">
<configuration>
<autoDetectTypes>
<platformType>PAPER</platformType>
<platformType>ADVENTURE</platformType>
</autoDetectTypes>
</configuration>
</facet>
</component>
</module>

124
.idea/uiDesigner.xml generated
View File

@ -1,124 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>

6
.idea/vcs.xml generated
View File

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@ -1,9 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -3,7 +3,7 @@ plugins {
}
group = 'io.github.simplex'
version = '1.0-RC02'
version = 'Beta-1.0-RC01'
repositories {
mavenCentral()

View File

@ -1,5 +0,0 @@
name: FeelingLucky
version: '1.0-RC02'
author: SimplexDevelopment
main: io.github.simplex.luck.FeelingLucky
api-version: 1.18

View File

@ -1,2 +0,0 @@
Manifest-Version: 1.0

View File

@ -6,6 +6,7 @@ import org.bukkit.entity.Player;
import java.io.Serializable;
public interface LuckContainer extends Serializable {
Attribute asAttribute();
double getNumber();
@ -18,5 +19,5 @@ public interface LuckContainer extends Serializable {
Player associatedPlayer();
double baseValue();
double getValue();
}

View File

@ -0,0 +1,75 @@
package io.github.simplex.lib;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.Contract;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public final class ItemBuilder {
private final ItemStack stack;
private final ItemMeta meta;
public ItemBuilder(Material material) {
this.stack = new ItemStack(material);
this.meta = stack.getItemMeta();
}
@Contract("_ -> new")
public static ItemBuilder of(Material material) {
return new ItemBuilder(material);
}
@Contract(pure = true)
public static boolean isTool(Material m) {
String name = m.getKey().getKey();
return m.equals(Material.SHEARS)
|| name.endsWith("bow")
|| name.endsWith("pickaxe")
|| name.endsWith("axe")
|| name.endsWith("sword")
|| name.endsWith("shovel")
|| name.endsWith("hoe");
}
public ItemBuilder setName(String name) {
meta.displayName(MiniComponent.of(name).send());
return this;
}
public ItemBuilder setAmount(int amount) {
stack.setAmount(amount);
return this;
}
public ItemBuilder setLore(String... lore) {
List<Component> components = new ArrayList<>();
Arrays.stream(lore).forEach(entry -> components.add(MiniComponent.of(entry).send()));
meta.lore(components);
return this;
}
public ItemStack build() {
stack.setItemMeta(meta);
return stack;
}
private Component component(String content, TextColor color, TextDecoration decoration) {
return Component.empty().content(content).decorate(decoration).color(color);
}
private Component component(String content, TextColor color) {
return Component.empty().content(content).color(color);
}
private Component component(String content) {
return Component.empty().content(content);
}
}

View File

@ -1,15 +1,13 @@
package io.github.simplex.lib;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public enum Messages {
NOT_FROM_CONSOLE(builder("This command may only be used in game.", null, null)),
NO_PERMISSION(builder("You do not have permission to use this command.", TextColor.color(255, 0, 0), TextDecoration.ITALIC));
NOT_FROM_CONSOLE(MiniComponent.err("This command may only be used in game.")),
NO_PERMISSION(MiniComponent.err("You do not have permission to use this command.")),
NO_PLAYER(MiniComponent.warn("That player cannot be found.")),
OUT_OF_BOUNDS(MiniComponent.err("Number must be between -1024.0 and 1024.0"));
private final Component message;
@ -20,16 +18,4 @@ public enum Messages {
public Component get() {
return message;
}
private static Component builder(@NotNull String message, @Nullable TextColor color, @Nullable TextDecoration decoration) {
if (color == null) {
if (decoration == null) return Component.empty().content(message);
return Component.empty().content(message).decoration(decoration, TextDecoration.State.TRUE);
}
if (decoration == null) return Component.empty().content(message).color(color);
return Component.empty().content(message).color(color).decoration(decoration, TextDecoration.State.TRUE);
}
}

View File

@ -0,0 +1,59 @@
package io.github.simplex.lib;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.ChatColor;
import org.jetbrains.annotations.Contract;
public class MiniComponent {
private final String content;
private TextDecoration decoration = null;
private TextColor color = null;
public MiniComponent(String content) {
this.content = content;
}
@Contract("_ -> new")
public static MiniComponent of(String content) {
return new MiniComponent(content);
}
@Contract("_ -> new")
public static Component info(String content) {
return new MiniComponent(content).color(ChatColor.GREEN).send();
}
@Contract("_ -> new")
public static Component warn(String content) {
return new MiniComponent(content).color(ChatColor.YELLOW).decorate(TextDecoration.ITALIC).send();
}
@Contract("_ -> new")
public static Component err(String content) {
return new MiniComponent(content).color(ChatColor.RED).decorate(TextDecoration.BOLD).send();
}
public MiniComponent decorate(TextDecoration decoration) {
this.decoration = decoration;
return this;
}
public MiniComponent color(ChatColor color) {
this.color = TextColor.color(color.asBungee().getColor().getRGB());
return this;
}
public Component send() {
if (color == null) {
if (decoration == null) return Component.empty().content(content);
return Component.empty().content(content).decoration(decoration, TextDecoration.State.TRUE);
}
if (decoration == null) return Component.empty().content(content).color(color);
return Component.empty().content(content).decorate(decoration).color(color);
}
}

View File

@ -0,0 +1,89 @@
package io.github.simplex.luck;
import io.github.simplex.luck.listener.AbstractListener;
import io.github.simplex.luck.util.SneakyWorker;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
@SuppressWarnings("ResultOfMethodCallIgnored")
public class Config extends YamlConfiguration {
private final FeelingLucky plugin;
private final List<String> configEntries = new ArrayList<>() {{
add("high_rarity_chance");
add("medium_rarity_chance");
add("low_rarity_chance");
add("block_drops");
add("bonemeal");
add("cheat_death");
add("enchanting");
add("experience");
add("item_drops");
add("random_effect");
add("restore_hunger");
add("take_damage");
add("unbreakable");
}};
private File configFile;
public Config(FeelingLucky plugin) {
this.plugin = plugin;
File dataFolder = plugin.getDataFolder();
if (!dataFolder.exists()) dataFolder.mkdirs();
File configFile = new File(dataFolder, "config.yml");
if (!configFile.exists()) {
SneakyWorker.sneakyTry(configFile::createNewFile);
plugin.saveResource("config.yml", true);
}
this.configFile = configFile;
load();
if (validateIntegrity()) {
File newFile = new File(plugin.getDataFolder(), "config.yml");
SneakyWorker.sneakyTry(() -> {
Files.delete(Path.of(this.configFile.getPath()));
newFile.createNewFile();
plugin.saveResource("config.yml", true);
});
this.configFile = newFile;
load();
}
}
public void save() {
SneakyWorker.sneakyTry(() -> save(configFile));
}
public void load() {
SneakyWorker.sneakyTry(() -> load(configFile));
}
public void reload() {
save();
load();
}
public boolean validateIntegrity() {
for (String key : getKeys(false)) {
if (!configEntries.contains(key)) {
plugin.getLogger().severe("The contents of your configuration file is corrupted! Regenerating a new configuration file...");
return true;
}
}
return false;
}
public AbstractListener.Rarity getRarity(String name) {
return AbstractListener.Rarity.valueOf(getString(name));
}
public double getChance(String path) {
return getDouble(path);
}
}

View File

@ -1,55 +1,95 @@
package io.github.simplex.luck;
import io.github.simplex.luck.listener.*;
import io.github.simplex.luck.player.PlayerConfig;
import io.github.simplex.luck.player.PlayerHandler;
import org.bukkit.Bukkit;
import io.github.simplex.luck.util.LuckCMD;
import io.github.simplex.luck.util.SneakyWorker;
import io.github.simplex.metrics.Metrics;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public final class FeelingLucky extends JavaPlugin {
private static final Map<UUID, PlayerConfig> configMap = new HashMap<>();
public LuckCMD cmd;
public PlayerHandler handler;
private final Map<UUID, PlayerConfig> configMap = new HashMap<>();
public static Map<UUID, PlayerConfig> getConfigMap() {
private PlayerHandler handler;
private Config config;
public Map<UUID, PlayerConfig> getConfigMap() {
return configMap;
}
@Override
public void onEnable() {
Bukkit.getLogger().info("Initializing the PlayerHandler...");
getLogger().info("Initializing metrics...");
new Metrics(this, 15054);
getLogger().info("Metrics loaded. Initializing the PlayerHandler...");
handler = new PlayerHandler(this);
Bukkit.getLogger().info("Initialization complete! Attempting to register the handler...");
this.getServer().getPluginManager().registerEvents(handler, this);
Bukkit.getLogger().info("Registration complete! Attempting to load all player configuration files...");
getLogger().info("Initialization complete! Attempting to register the Listeners...");
registerListeners();
getLogger().info("Registration complete! Attempting to load all player configuration files...");
loadPlayerConfigurations();
getLogger().info("Attempting to load the main configuration...");
config = new Config(this);
getLogger().info("Main Config loaded successfully! Attempting to load the Luck command...");
new LuckCMD(this);
getLogger().info("Successfully loaded the Luck command!");
getLogger().info("Successfully initialized!");
}
@Override
public void onDisable() {
getLogger().info("Saving all player configurations...");
configMap.values().forEach(PlayerConfig::save);
getLogger().info("Complete! Saving the main config...");
config.save();
getLogger().info("Complete! Goodbye! :)");
}
private void loadPlayerConfigurations() {
File[] files = getDataFolder().listFiles();
if (files != null) {
Arrays.stream(files).forEach(file -> {
UUID uuid = UUID.fromString(file.getName().split("\\.")[0]);
configMap.put(uuid, PlayerConfig.loadFrom(file));
configMap.put(uuid, PlayerConfig.loadFrom(this, file));
});
configMap.forEach((u, pc) -> pc.load());
getLogger().info("Successfully loaded all configurations!");
} else {
getLogger().info("There are no player configurations to load.");
}
}
Bukkit.getLogger().info("Attempting to load the Luck command...");
cmd = new LuckCMD(this);
Bukkit.getLogger().info("Successfully loaded the Luck command!");
private void registerListeners() {
try {
Class<?>[] listeners = SneakyWorker.getClasses(AbstractListener.class.getPackage().getName());
Arrays.stream(listeners).forEach(l -> {
if (AbstractListener.class.isAssignableFrom(l)) {
if (l.equals(AbstractListener.class)) return;
Bukkit.getLogger().info("Successfully initialized!");
SneakyWorker.sneakyTry(() -> l.getDeclaredConstructor(FeelingLucky.class).newInstance(this));
}
});
} catch (IOException | ClassNotFoundException ex) {
getLogger().severe(ex.getMessage());
}
}
public PlayerHandler getHandler() {
return handler;
}
@Override
public void onDisable() {
Bukkit.getLogger().info("Saving all player configurations...");
configMap.forEach((u, pc) -> pc.save());
@NotNull
public Config getConfig() {
return config;
}
}

View File

@ -1,158 +0,0 @@
package io.github.simplex.luck;
import io.github.simplex.lib.Messages;
import io.github.simplex.luck.player.Luck;
import io.github.simplex.luck.player.PlayerConfig;
import io.github.simplex.luck.player.PlayerHandler;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.TextColor;
import org.bukkit.Bukkit;
import org.bukkit.command.*;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
public class LuckCMD extends Command implements TabCompleter {
public LuckCMD(FeelingLucky plugin) {
super("luck", "FeelingLucky main command.", "/<command> <info | set | reset | give | take> [player] [amount]", List.of());
setPermission("luck.default");
plugin.getServer().getCommandMap().register("luck", "FeelingLucky", this);
}
@Override
public boolean execute(@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] args) {
if (args.length < 1 || args.length > 3) return false;
if (args.length == 3) {
if ((sender instanceof ConsoleCommandSender) || sender.hasPermission("luck.admin")) {
Player player = Bukkit.getPlayer(args[1]);
double amount = Double.parseDouble(args[2]);
if (player == null) {
sender.sendMessage(Component.empty().content("That player cannot be found."));
return true;
}
Luck luck = PlayerHandler.getLuckContainer(player);
PlayerConfig config = FeelingLucky.getConfigMap().get(player.getUniqueId());
switch (args[0]) {
case "set" -> {
luck.setValue(amount);
PlayerHandler.updatePlayer(player, luck);
config.setLuck(luck.baseValue());
sender.sendMessage(Component.empty().content("Successfully reset " + args[1] + "'s Luck stat."));
return true;
}
case "give" -> {
luck.addTo(amount);
PlayerHandler.updatePlayer(player, luck);
config.setLuck(luck.baseValue());
sender.sendMessage(Component.empty().content("Successfully reset " + args[1] + "'s Luck stat."));
return true;
}
case "take" -> {
luck.takeFrom(amount);
PlayerHandler.updatePlayer(player, luck);
config.setLuck(luck.baseValue());
sender.sendMessage(Component.empty().content("Successfully reset " + args[1] + "'s Luck stat."));
return true;
}
}
} else {
sender.sendMessage(Messages.NO_PERMISSION.get());
}
}
if (args.length == 2) {
if ((sender instanceof ConsoleCommandSender) || sender.hasPermission("luck.admin")) {
if (args[0].equalsIgnoreCase("info")) {
Player player = Bukkit.getPlayer(args[1]);
if (player == null) {
sender.sendMessage("That player cannot be found.");
return true;
}
Luck luck = PlayerHandler.getLuckContainer(player);
sender.sendMessage(Component.empty().content("Luck stat for " + args[1] + ": " + luck.baseValue()));
return true;
}
if (args[0].equalsIgnoreCase("reset")) {
Player player = Bukkit.getPlayer(args[1]);
if (player == null) {
sender.sendMessage(Component.empty().content("That player cannot be found."));
return true;
}
Luck luck = PlayerHandler.getLuckContainer(player);
PlayerConfig config = FeelingLucky.getConfigMap().get(player.getUniqueId());
luck.reset();
PlayerHandler.updatePlayer(player, luck);
config.setLuck(luck.baseValue());
sender.sendMessage(Component.empty().content("Successfully reset " + args[1] + "'s Luck stat."));
return true;
}
} else {
sender.sendMessage(Messages.NO_PERMISSION.get());
return true;
}
}
if (args.length == 1) {
if ((sender instanceof Player player) && player.hasPermission("luck.default")) {
if (args[0].equalsIgnoreCase("info")) {
Luck luck = PlayerHandler.getLuckContainer(player);
TextComponent c = Component.text("Your Luck: " + luck.getPercentage());
player.sendMessage(c.color(TextColor.color(0, 255, 0)));
return true;
}
} else if (sender instanceof ConsoleCommandSender) {
sender.sendMessage(Messages.NOT_FROM_CONSOLE.get());
return true;
}
}
return true;
}
@Override
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
List<String> completions = new ArrayList<>() {{
add("info");
}};
List<String> playerNames = new ArrayList<>() {{
Bukkit.getOnlinePlayers().forEach(p -> add(p.getName()));
}};
List<String> adminCommands = List.of("set", "reset", "give", "take");
if ((sender instanceof ConsoleCommandSender) || sender.hasPermission("luck.admin")) {
completions.addAll(adminCommands);
return completions;
}
if (args[0].equalsIgnoreCase("info") && sender.hasPermission("luck.admin")) {
return playerNames;
}
if (completions.contains(args[1]) && sender.hasPermission("luck.admin")) {
switch (args[0]) {
case "info":
case "reset":
return new ArrayList<>();
case "give":
case "take":
case "set":
return List.of("amount");
}
}
return completions;
}
}

View File

@ -1,38 +0,0 @@
package io.github.simplex.luck;
import io.github.simplex.luck.player.Luck;
import org.bukkit.Bukkit;
import org.bukkit.entity.Item;
import org.bukkit.inventory.ItemStack;
public class SneakyWorker {
public static void sneakyTry(SneakyTry sneakyTry) {
try {
sneakyTry.tryThis();
} catch (Exception ex) {
String sb = "An error of type: "
+ ex.getClass().getSimpleName()
+ " has occurred. A cause will be printed. \n\n"
+ ex.getCause();
Bukkit.getLogger().severe(sb);
}
}
public static void quietTry(SneakyTry sneakyTry) {
try {
sneakyTry.tryThis();
} catch (Exception ignored) {
}
}
public static void move(Item item) {
ItemStack stack = item.getItemStack();
int rng = (Luck.RNG().nextInt(2, 5)) + stack.getAmount();
stack.setAmount(rng);
item.setItemStack(stack);
}
public interface SneakyTry {
void tryThis() throws Exception;
}
}

View File

@ -0,0 +1,37 @@
package io.github.simplex.luck.listener;
import io.github.simplex.luck.Config;
import io.github.simplex.luck.FeelingLucky;
import io.github.simplex.luck.player.PlayerHandler;
import org.bukkit.event.Listener;
public abstract class AbstractListener implements Listener {
protected final FeelingLucky plugin;
protected final Config config;
public AbstractListener(FeelingLucky plugin) {
this.plugin = plugin;
this.config = plugin.getConfig();
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
protected PlayerHandler getHandler() {
return plugin.getHandler();
}
public boolean doesQualify(String name, double luck) {
return switch (config.getRarity(name)) {
case HIGH -> luck > config.getChance("high_rarity_chance");
case MED -> luck > config.getChance("medium_rarity_chance");
case LOW -> luck > config.getChance("low_rarity_chance");
case NONE -> true;
};
}
public enum Rarity {
HIGH,
MED,
LOW,
NONE
}
}

View File

@ -0,0 +1,28 @@
package io.github.simplex.luck.listener;
import io.github.simplex.luck.FeelingLucky;
import io.github.simplex.luck.player.Luck;
import io.github.simplex.luck.util.SneakyWorker;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.block.BlockDropItemEvent;
import java.util.List;
public final class BlockDrops extends AbstractListener {
public BlockDrops(FeelingLucky plugin) {
super(plugin);
}
@EventHandler
public void extraBlockDrops(BlockDropItemEvent event) {
Player player = event.getPlayer();
Luck luck = getHandler().getLuckContainer(player);
List<Item> items = event.getItems();
if (luck.quickRNG(luck.getPercentage()) && doesQualify("block_drops", luck.getPercentage())) {
event.getItems().clear();
event.getItems().addAll(items.stream().map(SneakyWorker::move).toList());
}
}
}

View File

@ -0,0 +1,48 @@
package io.github.simplex.luck.listener;
import io.github.simplex.lib.ItemBuilder;
import io.github.simplex.lib.MiniComponent;
import io.github.simplex.luck.FeelingLucky;
import io.github.simplex.luck.player.Luck;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.data.Ageable;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
public final class BonemealFullCrop extends AbstractListener {
public BonemealFullCrop(FeelingLucky plugin) {
super(plugin);
}
@EventHandler
public void bonemealFullCrop(PlayerInteractEvent event) {
Player player = event.getPlayer();
Action action = event.getAction();
ItemStack bonemeal = ItemBuilder.of(Material.BONE_MEAL).build();
Luck luck = getHandler().getLuckContainer(player);
ItemStack handItem = event.getItem();
if (handItem == null) return;
Block block = event.getClickedBlock();
if (block == null) return;
BlockData data = block.getBlockData();
if (action.isRightClick()
&& handItem.isSimilar(bonemeal)
&& (data instanceof Ageable crop)
&& luck.quickRNG(luck.getPercentage())
&& doesQualify("bonemeal", luck.getPercentage())) {
crop.setAge(crop.getMaximumAge());
data.merge(crop);
block.setBlockData(data);
player.sendMessage(MiniComponent.info("You got lucky and your crops grew to maturity."));
}
}
}

View File

@ -0,0 +1,27 @@
package io.github.simplex.luck.listener;
import io.github.simplex.lib.MiniComponent;
import io.github.simplex.luck.FeelingLucky;
import io.github.simplex.luck.player.Luck;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.entity.PlayerDeathEvent;
public final class CheatDeath extends AbstractListener {
public CheatDeath(FeelingLucky plugin) {
super(plugin);
}
@EventHandler
public void cheatDeath(PlayerDeathEvent event) {
Player player = event.getPlayer();
Luck luck = getHandler().getLuckContainer(player);
double absorption = Math.round(Luck.RNG().nextDouble(5.0, 10.0));
if (luck.quickRNG(luck.getPercentage()) && doesQualify("cheat_death", luck.getPercentage())) {
event.setCancelled(true);
player.setHealth(1.0);
player.setAbsorptionAmount(absorption);
player.sendMessage(MiniComponent.of("You got lucky and cheated death!").send());
}
}
}

View File

@ -0,0 +1,39 @@
package io.github.simplex.luck.listener;
import io.github.simplex.luck.FeelingLucky;
import io.github.simplex.luck.player.Luck;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.enchantment.EnchantItemEvent;
import java.util.List;
import java.util.Map;
public final class EnchantmentBoost extends AbstractListener {
public EnchantmentBoost(FeelingLucky plugin) {
super(plugin);
}
@EventHandler
public void enchantItem(EnchantItemEvent event) {
Map<Enchantment, Integer> enchMap = event.getEnchantsToAdd();
List<Enchantment> enchList = enchMap.keySet().stream().toList();
Player player = event.getEnchanter();
Luck luck = getHandler().getLuckContainer(player);
if (luck.quickRNG(luck.getPercentage()) && doesQualify("enchanting", luck.getPercentage())) {
Enchantment particular = enchList.get(Luck.RNG().nextInt(enchList.size()));
int rng = Luck.RNG().nextInt(1, 5);
if ((enchMap.get(particular) + rng) > particular.getMaxLevel()) {
enchMap.replace(particular, particular.getMaxLevel());
}
enchMap.replace(particular, enchMap.get(particular) + rng);
}
}
public FeelingLucky plugin() {
return plugin;
}
}

View File

@ -0,0 +1,27 @@
package io.github.simplex.luck.listener;
import com.destroystokyo.paper.event.player.PlayerPickupExperienceEvent;
import io.github.simplex.luck.FeelingLucky;
import io.github.simplex.luck.player.Luck;
import org.bukkit.entity.ExperienceOrb;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
public final class ExpBoost extends AbstractListener {
public ExpBoost(FeelingLucky plugin) {
super(plugin);
}
@EventHandler
public void boostExperienceGain(PlayerPickupExperienceEvent event) {
ExperienceOrb orb = event.getExperienceOrb();
int n = orb.getExperience();
int math = (5 * n ^ 2) / (2 * n + 4);
int rounded = Math.round(math);
Player player = event.getPlayer();
Luck luck = plugin.getHandler().getLuckContainer(player);
if (luck.quickRNG(luck.getPercentage()) && doesQualify("experience", luck.getPercentage())) {
orb.setExperience(rounded);
}
}
}

View File

@ -0,0 +1,81 @@
package io.github.simplex.luck.listener;
import io.github.simplex.lib.MiniComponent;
import io.github.simplex.luck.FeelingLucky;
import io.github.simplex.luck.player.Luck;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.entity.EntityPotionEffectEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
public class IllOmen extends AbstractListener {
public IllOmen(FeelingLucky plugin) {
super(plugin);
}
@EventHandler
public void reconnectCheck(PlayerJoinEvent event) {
Player player = event.getPlayer();
PotionEffectType type = PotionEffectType.BAD_OMEN;
Luck luck = getHandler().getLuckContainer(player);
if (player.hasPotionEffect(type)) {
luck.cache();
double maths = luck.getValue() - (luck.getValue() * 0.25);
luck.setValue(maths);
player.sendMessage(MiniComponent.info("A -25% debuff has been applied to your luck from the Bad Omen status effect."));
} else if (luck.cached(player) && !player.hasPotionEffect(type)) {
luck.restore();
player.sendMessage("The -25% debuff to your luck has been removed.");
}
}
@EventHandler
public void effectApplyCheck(EntityPotionEffectEvent event) {
EntityPotionEffectEvent.Cause cause = EntityPotionEffectEvent.Cause.PATROL_CAPTAIN;
EntityPotionEffectEvent.Action added = EntityPotionEffectEvent.Action.ADDED;
EntityPotionEffectEvent.Action changed = EntityPotionEffectEvent.Action.CHANGED;
if (event.getCause().equals(cause) && (event.getAction().equals(added) || event.getAction().equals(changed))) {
if (event.getEntity() instanceof Player player) {
Luck luck = plugin.getHandler().getLuckContainer(player);
luck.cache();
double maths = luck.getValue() - (luck.getValue() * 0.25);
luck.setValue(maths);
player.sendMessage(MiniComponent.warn("A -25% debuff has been applied to your luck from the Bad Omen status effect."));
}
}
}
@EventHandler
public void effectRemoveCheck(EntityPotionEffectEvent event) {
PotionEffect old = event.getOldEffect();
EntityPotionEffectEvent.Action cleared = EntityPotionEffectEvent.Action.CLEARED;
EntityPotionEffectEvent.Action removed = EntityPotionEffectEvent.Action.REMOVED;
if (old == null) return;
if (old.getType().equals(PotionEffectType.BAD_OMEN) && (event.getAction().equals(cleared) || event.getAction().equals(removed))) {
if ((event.getEntity() instanceof Player player)) {
Luck luck = plugin.getHandler().getLuckContainer(player);
if (luck.cached(player)) {
luck.restore();
player.sendMessage("The -25% debuff to your luck has been removed.");
}
}
}
}
@EventHandler
public void disconnectCheck(PlayerQuitEvent event) {
if (event.getPlayer().hasPotionEffect(PotionEffectType.BAD_OMEN)) {
Luck luck = plugin.getHandler().getLuckContainer(event.getPlayer());
if (luck.cached(event.getPlayer())) {
luck.restore();
}
}
}
}

View File

@ -0,0 +1,74 @@
package io.github.simplex.luck.listener;
import io.github.simplex.luck.FeelingLucky;
import io.github.simplex.luck.player.Luck;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.EntityDropItemEvent;
import org.bukkit.inventory.ItemStack;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class ItemDrops extends AbstractListener {
private final Map<UUID, Player> entityPlayerMap = new HashMap<>();
private boolean canAffect = false;
public ItemDrops(FeelingLucky plugin) {
super(plugin);
}
@EventHandler
public void checkForPreItemDrop(EntityDamageByEntityEvent event) {
if (!(event.getEntity() instanceof LivingEntity entity)) {
return;
}
if (!(event.getDamager() instanceof Player player)) {
return;
}
if (!(entity.getHealth() <= 0.0)) {
return;
}
entityPlayerMap.put(entity.getUniqueId(), player);
}
@EventHandler
public void checkForDroppedItems(EntityDeathEvent event) {
if (event.getEntity() instanceof Player) {
canAffect = false;
return;
}
canAffect = event.getDrops().isEmpty();
}
@EventHandler
public void itemDrops(EntityDropItemEvent event) {
Entity entity = event.getEntity();
if (entityPlayerMap.get(entity.getUniqueId()) == null) return;
if (!canAffect) return;
Player player = entityPlayerMap.get(entity.getUniqueId());
Luck luck = getHandler().getLuckContainer(player);
Item item = event.getItemDrop();
ItemStack stack = item.getItemStack();
int amount = stack.getAmount();
if (luck.quickRNG(luck.getPercentage()) && doesQualify("item_drops", luck.getPercentage())) {
int rng = Luck.RNG().nextInt(2, 5);
amount += rng;
stack.setAmount(amount);
event.getItemDrop().setItemStack(stack);
}
}
}

View File

@ -1,208 +1,101 @@
package io.github.simplex.luck.listener;
import io.github.simplex.lib.PotionEffectBuilder;
import io.github.simplex.lib.MiniComponent;
import io.github.simplex.luck.FeelingLucky;
import io.github.simplex.luck.ListBox;
import io.github.simplex.luck.SneakyWorker;
import io.github.simplex.luck.player.Luck;
import io.github.simplex.luck.player.PlayerHandler;
import net.kyori.adventure.text.Component;
import org.bukkit.Location;
import io.github.simplex.luck.util.CooldownTimer;
import io.github.simplex.luck.util.SpecialFootItem;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.*;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Guardian;
import org.bukkit.entity.Player;
import org.bukkit.entity.Witch;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockDropItemEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.event.entity.EntityDropItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerItemConsumeEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.Objects;
public record PlayerListener(FeelingLucky plugin) implements Listener {
public final class PlayerListener extends AbstractListener {
private final CooldownTimer timer;
private static final Map<UUID, Player> entityPlayerMap = new HashMap<>();
@EventHandler
public void takeDamage(EntityDamageEvent event) {
Entity entity = event.getEntity();
if (!(entity instanceof Player)) {
return;
}
Player player = (Player) event.getEntity();
Luck luck = PlayerHandler.getLuckContainer(player);
if (ListBox.acceptedCauses.contains(event.getCause())) {
if (luck.notDefault()) {
double percentage = luck.getPercentage();
/*
* If a player's luck stat is a negative number, or they are "marked",
* this will trigger instead of the regular luck spin.
*/
if (percentage < 0 || PlayerHandler.isMarked(player)) {
percentage = Math.abs(percentage);
if (luck.quickRNG(percentage)) {
event.setCancelled(true);
player.damage(event.getDamage() * 2);
player.sendMessage(Component.empty().content("You were unlucky and took double damage."));
}
return;
}
if (luck.quickRNG(percentage)) {
event.setCancelled(true);
player.damage(event.getDamage() / 2);
player.sendMessage(Component.empty().content("You got lucky and took less damage."));
}
}
}
if (ListBox.sideCauses.contains(event.getCause())) {
if (luck.notDefault()) {
double percentage = luck.getPercentage();
/*
* If a player's luck stat is a negative number, or they are "marked",
* this will trigger instead of the regular luck spin.
*/
if (percentage < 0 || PlayerHandler.isMarked(player)) {
percentage = Math.abs(percentage);
if (luck.quickRNG(percentage)) {
event.setCancelled(true);
player.addPotionEffect(PotionEffectBuilder.newEffect().type(ListBox.potionEffects.get(Luck.RNG().nextInt(ListBox.potionEffects.size() - 1))).amplifier(Luck.RNG().nextInt(1, 5)).duration(Luck.RNG().nextInt(1, 120)).create());
}
return;
}
if (luck.quickRNG(percentage)) {
event.setCancelled(true);
player.getActivePotionEffects().removeIf(p -> ListBox.potionEffects.contains(p.getType()));
player.setFireTicks(0);
player.sendMessage(Component.empty().content("You got lucky and your afflictions were cured."));
}
}
}
}
@EventHandler
public void extraBlockDrops(BlockDropItemEvent event) {
Player player = event.getPlayer();
Luck luck = PlayerHandler.getLuckContainer(player);
List<Item> items = event.getItems();
if (luck.quickRNG(luck.getPercentage())) {
items.forEach(SneakyWorker::move);
}
}
@EventHandler
public void checkForPreItemDrop(EntityDamageByEntityEvent event) {
if (!(event.getEntity() instanceof LivingEntity entity)) {
return;
}
if (!(event.getDamager() instanceof Player player)) {
return;
}
if (!(entity.getHealth() <= 0.0)) {
return;
}
if (entity instanceof Witch witch) {
if (Luck.quickRNG2(33.0)) {
Location location = witch.getLocation();
World world = location.getWorld();
Item item = world.dropItemNaturally(location, new ItemStack(Material.RABBIT_FOOT, 1));
new EntityDropItemEvent(witch, item).callEvent();
}
}
entityPlayerMap.put(entity.getUniqueId(), player);
}
@EventHandler
public void itemDrops(EntityDropItemEvent event) {
Entity entity = event.getEntity();
if (entityPlayerMap.get(entity.getUniqueId()) == null) return;
Player player = entityPlayerMap.get(entity.getUniqueId());
Luck luck = PlayerHandler.getLuckContainer(player);
Item item = event.getItemDrop();
ItemStack stack = item.getItemStack();
int amount = stack.getAmount();
if (luck.quickRNG(luck.getPercentage())) {
int rng = Luck.RNG().nextInt(2, 5);
amount += rng;
stack.setAmount(amount);
event.getItemDrop().setItemStack(stack);
}
}
@EventHandler
public void restoreHunger(PlayerItemConsumeEvent event) {
ItemStack item = event.getItem();
Luck luck = PlayerHandler.getLuckContainer(event.getPlayer());
PotionEffect effect = PotionEffectBuilder.newEffect().type(PotionEffectType.SATURATION).amplifier(2).duration(10).particles(false).create();
if (luck.notDefault()) {
double percentage = luck.getPercentage();
ListBox.foods.forEach(food -> {
if (item.isSimilar(food)) {
if (luck.quickRNG(percentage)) {
event.getPlayer().setExhaustion(event.getPlayer().getExhaustion() + 2);
event.getPlayer().addPotionEffect(effect);
}
}
});
}
public PlayerListener(FeelingLucky plugin) {
super(plugin);
this.timer = new CooldownTimer();
}
@EventHandler
public void rabbitFoot(PlayerInteractEvent event) {
Action action = event.getAction();
ItemStack foot = new ItemStack(Material.RABBIT_FOOT);
SpecialFootItem special = new SpecialFootItem();
Player player = event.getPlayer();
Luck luck = PlayerHandler.getLuckContainer(player);
Luck luck = getHandler().getLuckContainer(player);
if (timer.onCooldown(player)) {
player.sendMessage(MiniComponent.err("That feature can only be used once every 30 seconds."));
player.sendMessage(MiniComponent.info("You have " + timer.remaining(player) + " seconds remaining."));
return;
}
if (action.isRightClick() && player.getInventory().getItemInMainHand().isSimilar(foot)) {
if (foot.getItemMeta().equals(special.meta()) || foot.equals(special.get())) {
luck.setMultiplier(luck.multiplier() + 1);
player.sendMessage(MiniComponent.info("Your luck multiplier has increased by 1!"));
}
double rng = Luck.RNG().nextDouble(2.0, 5.0);
player.getInventory().remove(player.getInventory().getItemInMainHand());
luck.addTo(rng);
player.sendMessage(Component.empty().content("Your luck has been increased by " + rng + " points."));
plugin.getHandler().updatePlayer(player, luck);
timer.setCooldown(player.getUniqueId(), System.currentTimeMillis());
player.sendMessage(MiniComponent.info("Your luck has been increased by " + rng + " points."));
}
}
@EventHandler
public void witchesBrew(EntityDamageByEntityEvent event) {
public void luckDecrease(EntityDamageByEntityEvent event) {
Entity eTEMP = event.getDamager();
Entity pTEMP = event.getEntity();
DamageCause cause = event.getCause();
EntityDamageEvent.DamageCause cause = event.getCause();
if (!(pTEMP instanceof Player player)) {
return;
}
if (!(eTEMP instanceof Witch)) {
if (!(eTEMP instanceof Witch) || !(eTEMP instanceof Guardian)) {
return;
}
Luck luck = PlayerHandler.getLuckContainer(player);
if (cause.equals(DamageCause.MAGIC) || cause.equals(DamageCause.POISON)) {
Luck luck = plugin.getHandler().getLuckContainer(player);
if (cause.equals(EntityDamageEvent.DamageCause.MAGIC) || cause.equals(EntityDamageEvent.DamageCause.POISON)) {
if (luck.quickRNG(33.0)) {
luck.takeFrom(5.0);
PlayerHandler.updatePlayer(player, luck);
plugin.getHandler().updatePlayer(player, luck);
player.sendMessage(MiniComponent.warn("Your luck has been decreased by 5 points!"));
}
}
}
@Override
public boolean equals(Object obj) {
if (obj == this) return true;
if (obj == null || obj.getClass() != this.getClass()) return false;
var that = (PlayerListener) obj;
return Objects.equals(this.plugin, that.plugin);
}
@Override
public int hashCode() {
return Objects.hash(plugin);
}
@Override
public String toString() {
return "PlayerListener[" +
"plugin=" + plugin + ']';
}
}

View File

@ -0,0 +1,49 @@
package io.github.simplex.luck.listener;
import io.github.simplex.lib.MiniComponent;
import io.github.simplex.luck.FeelingLucky;
import io.github.simplex.luck.player.Luck;
import io.github.simplex.luck.util.ListBox;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.potion.PotionEffect;
import java.util.List;
public class RandomEffect extends AbstractListener {
public RandomEffect(FeelingLucky plugin) {
super(plugin);
}
@EventHandler
public void giveRandomEffect(PlayerRespawnEvent respawn) {
Player player = respawn.getPlayer();
Luck luck = plugin.getHandler().getLuckContainer(player);
List<PotionEffect> effectList = ListBox.positiveEffects.stream().map(m -> m.createEffect(120, 5)).toList();
int size = effectList.size();
PotionEffect random = effectList.get(Luck.RNG().nextInt(size - 1));
if (luck.quickRNG(luck.getPercentage()) && doesQualify("random_effect", luck.getValue())) {
player.addPotionEffect(random);
player.sendMessage(MiniComponent.info("Thanks to luck, a random positive potion effect has been applied to you."));
}
}
@EventHandler
public void giveRandomEffect(PlayerTeleportEvent tp) {
Player player = tp.getPlayer();
Luck luck = plugin.getHandler().getLuckContainer(player);
List<PotionEffect> effectList = ListBox.positiveEffects.stream().map(m -> m.createEffect(120, 5)).toList();
int size = effectList.size();
PotionEffect random = effectList.get(Luck.RNG().nextInt(size - 1));
if (luck.quickRNG(luck.getPercentage()) && doesQualify("random_effect", luck.getValue())) {
player.addPotionEffect(random);
player.sendMessage(MiniComponent.info("Thanks to luck, a random positive potion effect has been applied to you."));
}
}
}

View File

@ -0,0 +1,35 @@
package io.github.simplex.luck.listener;
import io.github.simplex.lib.PotionEffectBuilder;
import io.github.simplex.luck.FeelingLucky;
import io.github.simplex.luck.player.Luck;
import io.github.simplex.luck.util.ListBox;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerItemConsumeEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
public class RestoreHunger extends AbstractListener {
public RestoreHunger(FeelingLucky plugin) {
super(plugin);
}
@EventHandler
public void restoreHunger(PlayerItemConsumeEvent event) {
ItemStack item = event.getItem();
Luck luck = getHandler().getLuckContainer(event.getPlayer());
PotionEffect effect = PotionEffectBuilder.newEffect().type(PotionEffectType.SATURATION).amplifier(2).duration(10).particles(false).create();
if (luck.notDefault()) {
double percentage = luck.getPercentage();
ListBox.foods.forEach(food -> {
if (item.isSimilar(food)) {
if (luck.quickRNG(percentage) && doesQualify("restore_hunger", percentage)) {
event.getPlayer().setExhaustion(event.getPlayer().getExhaustion() + 2);
event.getPlayer().addPotionEffect(effect);
}
}
});
}
}
}

View File

@ -0,0 +1,78 @@
package io.github.simplex.luck.listener;
import io.github.simplex.lib.PotionEffectBuilder;
import io.github.simplex.luck.FeelingLucky;
import io.github.simplex.luck.player.Luck;
import io.github.simplex.luck.util.ListBox;
import net.kyori.adventure.text.Component;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.entity.EntityDamageEvent;
public class TakeDamage extends AbstractListener {
public TakeDamage(FeelingLucky plugin) {
super(plugin);
}
@EventHandler
public void takeDamage(EntityDamageEvent event) {
Entity entity = event.getEntity();
if (!(entity instanceof Player)) {
return;
}
Player player = (Player) event.getEntity();
Luck luck = getHandler().getLuckContainer(player);
if (ListBox.acceptedCauses.contains(event.getCause())) {
if (luck.notDefault()) {
double percentage = luck.getPercentage();
/*
* If a player's luck stat is a negative number, or they are "marked",
* this will trigger instead of the regular luck spin.
*/
if (percentage < 0 || luck.isMarked(player)) {
percentage = Math.abs(percentage);
if (luck.quickRNG(percentage)) {
event.setCancelled(true);
player.damage(event.getDamage() * 2);
player.sendMessage(Component.empty().content("You were unlucky and took double damage."));
}
return;
}
if (luck.quickRNG(percentage) && doesQualify("take_damage", percentage)) {
event.setCancelled(true);
player.damage(event.getDamage() / 2);
player.sendMessage(Component.empty().content("You got lucky and took less damage."));
}
}
}
if (ListBox.sideCauses.contains(event.getCause())) {
if (luck.notDefault()) {
double percentage = luck.getPercentage();
/*
* If a player's luck stat is a negative number, or they are "marked",
* this will trigger instead of the regular luck spin.
*/
if (percentage < 0 || luck.isMarked(player)) {
percentage = Math.abs(percentage);
if (luck.quickRNG(percentage)) {
event.setCancelled(true);
player.addPotionEffect(PotionEffectBuilder.newEffect().type(ListBox.potionEffects.get(Luck.RNG().nextInt(ListBox.potionEffects.size() - 1))).amplifier(Luck.RNG().nextInt(1, 5)).duration(Luck.RNG().nextInt(1, 120)).create());
}
return;
}
if (luck.quickRNG(percentage) && doesQualify("take_damage", percentage)) {
event.setCancelled(true);
player.getActivePotionEffects().removeIf(p -> ListBox.potionEffects.contains(p.getType()));
player.setFireTicks(0);
player.sendMessage(Component.empty().content("You got lucky and your afflictions were cured."));
}
}
}
}
}

View File

@ -0,0 +1,39 @@
package io.github.simplex.luck.listener;
import io.github.simplex.lib.ItemBuilder;
import io.github.simplex.lib.MiniComponent;
import io.github.simplex.luck.FeelingLucky;
import io.github.simplex.luck.player.Luck;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.inventory.CraftItemEvent;
import org.bukkit.inventory.CraftingInventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
public class UnbreakableTool extends AbstractListener {
public UnbreakableTool(FeelingLucky plugin) {
super(plugin);
}
@EventHandler
public void unbreakableTool(CraftItemEvent event) {
CraftingInventory inventory = event.getInventory();
ItemStack stack = inventory.getResult();
if (stack == null) return;
ItemMeta meta = stack.getItemMeta();
if (ItemBuilder.isTool(stack.getType())) {
if (event.getWhoClicked() instanceof Player player) {
Luck luck = getHandler().getLuckContainer(player);
if (luck.quickRNG(luck.getPercentage()) && doesQualify("unbreakable", luck.getPercentage())) {
meta.setUnbreakable(true);
stack.setItemMeta(meta);
inventory.setResult(stack);
player.sendMessage(MiniComponent.info("By the grace of Luck you have crafted an unbreakable tool!"));
}
}
}
}
}

View File

@ -0,0 +1,51 @@
package io.github.simplex.luck.listener;
import io.github.simplex.lib.ItemBuilder;
import io.github.simplex.luck.FeelingLucky;
import io.github.simplex.luck.player.Luck;
import io.github.simplex.luck.util.SpecialFootItem;
import org.bukkit.Material;
import org.bukkit.entity.Villager;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerInteractAtEntityEvent;
import org.bukkit.inventory.MerchantRecipe;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class VillagerInventory extends AbstractListener {
private final SpecialFootItem foot = new SpecialFootItem();
private final MerchantRecipe recipe = new MerchantRecipe(foot.get(), 0, 2, true);
public VillagerInventory(FeelingLucky plugin) {
super(plugin);
recipe.setIngredients(Arrays.asList(
ItemBuilder.of(Material.EMERALD).build(),
ItemBuilder.of(Material.RABBIT_HIDE).build()
));
recipe.setDemand(8);
recipe.setPriceMultiplier(1.25F);
recipe.setVillagerExperience(25);
recipe.setSpecialPrice(4);
}
@EventHandler
public void addRabbitFootToVillagerInventory(PlayerInteractAtEntityEvent event) {
if (event.getRightClicked() instanceof Villager vil) {
if (!vil.getProfession().equals(Villager.Profession.BUTCHER)) return;
List<MerchantRecipe> recipeList = new ArrayList<>(vil.getRecipes());
if (recipeList.contains(recipe)) return;
Luck luck = plugin.getHandler().getLuckContainer(event.getPlayer());
if (luck == null) return;
if (luck.quickRNG(luck.getPercentage())) {
recipeList.add(recipe);
vil.setRecipes(recipeList);
}
}
}
}

View File

@ -1,30 +1,38 @@
package io.github.simplex.luck.player;
import io.github.simplex.api.LuckContainer;
import io.github.simplex.luck.FeelingLucky;
import org.bukkit.Bukkit;
import org.bukkit.attribute.Attribute;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import java.util.SplittableRandom;
import java.util.*;
@SuppressWarnings("all")
public class Luck implements LuckContainer {
private final Player player;
private final double multiplier;
private final double BASE_VALUE;
private final PlayerLuckChangeEvent event;
private final FeelingLucky plugin;
private final List<Player> markedPlayers = new ArrayList<>();
private final Map<Player, Double> cache = new HashMap<>();
private double BASE_VALUE;
private double multiplier;
private double tempSave;
public Luck(Player player) {
this(player, 1.0);
public Luck(FeelingLucky plugin, Player player) {
this(plugin, player, 1.0);
}
public Luck(Player player, double multiplier) {
public Luck(FeelingLucky plugin, Player player, double multiplier) {
this.player = player;
this.multiplier = multiplier;
BASE_VALUE = player.getAttribute(Attribute.GENERIC_LUCK).getDefaultValue();
event = new PlayerLuckChangeEvent(player);
this.plugin = plugin;
BASE_VALUE = plugin.getConfigMap().get(player.getUniqueId()).getConfig().getDouble("luck");
tempSave = BASE_VALUE;
event = new PlayerLuckChangeEvent(this);
}
@Contract(pure = true,
@ -33,15 +41,33 @@ public class Luck implements LuckContainer {
return new SplittableRandom();
}
public static boolean quickRNG2(double percentage) {
public static boolean quickRNGnoMultiplier(double percentage) {
double rng;
if (percentage >= 100.0) {
rng = 100.0; // 100% chance to trigger, obviously;
rng = 1024.0; // 100% chance to trigger, obviously;
} else {
rng = RNG().nextDouble(0.0, 99.0);
rng = RNG().nextDouble(0.0, 1024.0);
}
return (percentage >= rng);
double actual = Math.round((rng / 1024.0) * 100);
return (percentage >= actual);
}
public FeelingLucky getPlugin() {
return plugin;
}
public void markPlayer(Player player) {
markedPlayers.add(player);
}
public void unmarkPlayer(Player player) {
markedPlayers.remove(player);
}
public boolean isMarked(Player player) {
return markedPlayers.contains(player);
}
@Override
@ -77,64 +103,102 @@ public class Luck implements LuckContainer {
public boolean quickRNG(double percentage) {
double rng;
if (percentage >= 100.0) {
rng = 100.0; // 100% chance to trigger, obviously;
rng = 1024.0; // 100% chance to trigger, obviously;
} else {
rng = RNG().nextDouble(0.0, 99.0);
rng = RNG().nextDouble(0.0, 1024.0);
}
double actual = Math.round((rng / 1024) * 100);
if (multiplier() > 1.0) {
return ((percentage * multiplier()) >= rng);
return ((percentage * multiplier()) >= actual);
}
return (percentage >= rng);
return (percentage >= actual);
}
public void reset() {
setValue(defaultValue());
setValue(getDefaultValue());
}
@Override
public double baseValue() {
public double getValue() {
return BASE_VALUE;
}
public double defaultValue() {
public void setValue(double value) {
BASE_VALUE = value;
player.getAttribute(Attribute.GENERIC_LUCK).setBaseValue(value);
plugin.getConfigMap().get(associatedPlayer().getUniqueId()).setLuck(value);
Bukkit.getPluginManager().callEvent(event);
}
public void setMultiplier(double multiplier) {
this.multiplier = multiplier;
plugin.getConfigMap().get(associatedPlayer().getUniqueId()).setMultiplier(multiplier);
}
public void cache() {
if (cache.containsKey(player)) {
cache.replace(player, getValue());
return;
}
cache.put(player, getValue());
}
public boolean cached(Player player) {
return cache.containsKey(player);
}
public void restore() {
if (!cache.containsKey(player)) {
plugin.getLogger().info("Nothing to restore!");
return;
}
setValue(cache.get(player));
cache.remove(player);
}
public double getDefaultValue() {
return player.getAttribute(Attribute.GENERIC_LUCK).getDefaultValue();
}
public void setValue(double value) {
player.getAttribute(Attribute.GENERIC_LUCK).setBaseValue(value);
Bukkit.getPluginManager().callEvent(event);
}
public void addTo(double value) {
setValue(baseValue() + value);
Bukkit.getPluginManager().callEvent(event);
if (value >= 1024.0 || (getValue() + value) >= 1024.0) {
setValue(1024.0);
return;
}
setValue(getValue() + value);
}
public void takeFrom(double value) {
setValue(baseValue() - value);
Bukkit.getPluginManager().callEvent(event);
if (value <= -1024.0 || (getValue() - value) <= -1024.0) {
setValue(-1024.0);
return;
}
setValue(getValue() + value);
}
public double getPercentage() {
return baseValue() - defaultValue();
return getValue() - getDefaultValue();
}
public boolean notDefault() {
return baseValue() != defaultValue();
return getValue() != getDefaultValue();
}
public boolean lessThan(double value) {
return baseValue() < value;
return getValue() < value;
}
public boolean greaterThan(double value) {
return baseValue() > value;
return getValue() > value;
}
@Override
public String toString() {
return "" + baseValue();
return "" + getValue();
}
}

View File

@ -1,7 +1,9 @@
package io.github.simplex.luck.player;
import io.github.simplex.luck.FeelingLucky;
import io.github.simplex.luck.SneakyWorker;
import io.github.simplex.luck.util.SneakyWorker;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.attribute.Attribute;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
@ -11,14 +13,18 @@ import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.nio.charset.StandardCharsets;
import java.util.UUID;
public class PlayerConfig {
private final File configFile;
private final OfflinePlayer player;
private volatile YamlConfiguration config;
@SuppressWarnings("ResultOfMethodCallIgnored")
public PlayerConfig(FeelingLucky plugin, Player player) {
File dataFolder = plugin.getDataFolder();
this.player = player;
if (!plugin.getDataFolder().exists()) plugin.getDataFolder().mkdirs();
File dataFolder = new File(plugin.getDataFolder(), "players");
if (!dataFolder.exists()) dataFolder.mkdirs();
File file = new File(dataFolder, player.getUniqueId() + ".yml");
if (!file.exists()) {
@ -43,22 +49,23 @@ public class PlayerConfig {
String tempUsername = config.getString("username");
if (tempUsername != null && tempUsername.equalsIgnoreCase("replace")) {
if (tempUsername == null) {
config.set("username", player.getName());
config.set("luck", PlayerHandler.getLuckContainer(player).defaultValue());
config.set("luck", plugin.getHandler().getLuckContainer(player).getDefaultValue());
config.set("multiplier", "1.0");
save();
}
}
protected PlayerConfig(File file) {
protected PlayerConfig(FeelingLucky plugin, File file) {
this.configFile = file;
this.player = Bukkit.getOfflinePlayer(UUID.fromString(file.getName().split("\\.")[0]));
config = YamlConfiguration.loadConfiguration(configFile);
}
@Contract("_ -> new")
public static PlayerConfig loadFrom(File file) {
return new PlayerConfig(file);
@Contract("_, _ -> new")
public static PlayerConfig loadFrom(FeelingLucky plugin, File file) {
return new PlayerConfig(plugin, file);
}
public void save() {
@ -66,15 +73,33 @@ public class PlayerConfig {
}
public void load() {
SneakyWorker.sneakyTry(() -> config = YamlConfiguration.loadConfiguration(configFile));
}
public void reload() {
save();
load();
}
public OfflinePlayer getPlayer() {
return player;
}
public void setUsername(String name) {
config.set("username", name);
save();
}
public void setLuck(double luck) {
config.set("luck", luck);
save();
}
public void setMultiplier(double multiplier) {
config.set("multiplier", multiplier);
save();
}
public YamlConfiguration getConfig() {
return config;
}

View File

@ -1,48 +1,41 @@
package io.github.simplex.luck.player;
import io.github.simplex.luck.FeelingLucky;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public record PlayerHandler(FeelingLucky plugin) implements Listener {
private static final Map<Player, Luck> playerLuckMap = new HashMap<>();
private static final List<Player> markedPlayers = new ArrayList<>();
public class PlayerHandler implements Listener {
public final FeelingLucky plugin;
private final Map<Player, Luck> playerLuckMap = new HashMap<>();
public static Luck getLuckContainer(Player player) {
public PlayerHandler(FeelingLucky plugin) {
this.plugin = plugin;
Bukkit.getServer().getPluginManager().registerEvents(this, plugin);
}
public Luck getLuckContainer(Player player) {
return playerLuckMap.get(player);
}
public static void markPlayer(Player player) {
markedPlayers.add(player);
}
public static void unmarkPlayer(Player player) {
markedPlayers.remove(player);
}
public static boolean isMarked(Player player) {
return markedPlayers.contains(player);
}
public static void updatePlayer(Player player, Luck luck) {
public void updatePlayer(Player player, Luck luck) {
playerLuckMap.replace(player, luck);
}
@EventHandler
public void initializePlayer(PlayerLoginEvent event) {
Player player = event.getPlayer();
PlayerConfig playerConfig = FeelingLucky.getConfigMap().get(player.getUniqueId());
PlayerConfig playerConfig = plugin.getConfigMap().get(player.getUniqueId());
if (playerConfig == null) {
playerConfig = new PlayerConfig(plugin, player);
FeelingLucky.getConfigMap().put(player.getUniqueId(), playerConfig);
plugin.getConfigMap().put(player.getUniqueId(), playerConfig);
}
String username = playerConfig.getConfig().getString("username");
@ -55,9 +48,14 @@ public record PlayerHandler(FeelingLucky plugin) implements Listener {
playerConfig.load();
}
Luck container = new Luck(player, multiplier);
Luck container = new Luck(plugin, player, multiplier);
container.setValue(luck);
playerLuckMap.put(player, container);
}
@EventHandler
public void clearContainer(PlayerQuitEvent event) {
playerLuckMap.remove(event.getPlayer());
}
}

View File

@ -1,6 +1,5 @@
package io.github.simplex.luck.player;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;
import org.jetbrains.annotations.NotNull;
@ -8,16 +7,16 @@ import org.jetbrains.annotations.NotNull;
public class PlayerLuckChangeEvent extends PlayerEvent {
public final HandlerList handlerList = new HandlerList();
public PlayerLuckChangeEvent(@NotNull Player who) {
super(who);
Luck luck = PlayerHandler.getLuckContainer(who);
if (luck.lessThan(0.0) && !PlayerHandler.isMarked(who)) {
PlayerHandler.markPlayer(who);
public PlayerLuckChangeEvent(@NotNull Luck luck) {
super(luck.associatedPlayer());
if (luck.lessThan(0.0) && !luck.isMarked(luck.associatedPlayer())) {
luck.markPlayer(luck.associatedPlayer());
return;
}
if (luck.greaterThan(0.0) && PlayerHandler.isMarked(who)) {
PlayerHandler.unmarkPlayer(who);
if (luck.greaterThan(0.0) && luck.isMarked(luck.associatedPlayer())) {
luck.unmarkPlayer(luck.associatedPlayer());
}
}

View File

@ -0,0 +1,35 @@
package io.github.simplex.luck.util;
import org.bukkit.entity.Player;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
public class CooldownTimer {
public static final long DEFAULT_COOLDOWN = 30L;
private final Map<UUID, Long> cooldowns = new HashMap<>();
public void setCooldown(UUID playerUUID, long time) {
if (time < 1) {
cooldowns.remove(playerUUID);
} else {
cooldowns.put(playerUUID, time);
}
}
public long getCooldown(UUID uuid) {
return cooldowns.getOrDefault(uuid, 0L);
}
public long remaining(Player player) {
long timeLeft = System.currentTimeMillis() - getCooldown(player.getUniqueId());
return TimeUnit.MILLISECONDS.toSeconds(timeLeft) - DEFAULT_COOLDOWN;
}
public boolean onCooldown(Player player) {
long remaining = System.currentTimeMillis() - getCooldown(player.getUniqueId());
return (!(TimeUnit.MILLISECONDS.toSeconds(remaining) >= DEFAULT_COOLDOWN));
}
}

View File

@ -1,4 +1,4 @@
package io.github.simplex.luck;
package io.github.simplex.luck.util;
import org.bukkit.Material;
import org.bukkit.event.entity.EntityDamageEvent;
@ -6,6 +6,7 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffectType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ListBox {
@ -36,6 +37,25 @@ public class ListBox {
add(PotionEffectType.WEAKNESS);
}};
public static final List<PotionEffectType> positiveEffects = new ArrayList<>() {{
add(PotionEffectType.DAMAGE_RESISTANCE);
add(PotionEffectType.DOLPHINS_GRACE);
add(PotionEffectType.INCREASE_DAMAGE);
add(PotionEffectType.ABSORPTION);
add(PotionEffectType.SATURATION);
add(PotionEffectType.FIRE_RESISTANCE);
add(PotionEffectType.WATER_BREATHING);
add(PotionEffectType.SPEED);
add(PotionEffectType.SLOW_FALLING);
add(PotionEffectType.REGENERATION);
add(PotionEffectType.NIGHT_VISION);
add(PotionEffectType.LUCK);
add(PotionEffectType.JUMP);
add(PotionEffectType.INVISIBILITY);
add(PotionEffectType.HEALTH_BOOST);
add(PotionEffectType.FAST_DIGGING);
}};
public static final List<ItemStack> foods = new ArrayList<>() {{
add(new ItemStack(Material.COOKED_BEEF));
add(new ItemStack(Material.COOKED_CHICKEN));

View File

@ -0,0 +1,203 @@
package io.github.simplex.luck.util;
import io.github.simplex.lib.Messages;
import io.github.simplex.lib.MiniComponent;
import io.github.simplex.luck.FeelingLucky;
import io.github.simplex.luck.player.Luck;
import io.github.simplex.luck.player.PlayerConfig;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class LuckCMD extends Command implements TabCompleter {
private final FeelingLucky plugin;
public LuckCMD(FeelingLucky plugin) {
super("luck", "FeelingLucky main command.", "/<command> <info | set | reset | give | take> [player] [amount]", List.of());
this.plugin = plugin;
setPermission("luck.default");
plugin.getServer().getCommandMap().register("luck", "FeelingLucky", this);
}
@Override
public boolean execute(@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] args) {
if (args.length < 1 || args.length > 3) return false;
if (args.length == 3) {
if ((sender instanceof ConsoleCommandSender) || sender.hasPermission("luck.admin")) {
if (args[0].equalsIgnoreCase("reload") && args[1].equalsIgnoreCase("-p")) {
Player player = Bukkit.getPlayer(args[2]);
if (player == null) {
sender.sendMessage(Messages.NO_PLAYER.get());
return true;
}
UUID uuid = player.getUniqueId();
PlayerConfig config = plugin.getConfigMap().get(uuid);
config.reload();
sender.sendMessage(MiniComponent.info("Successfully reloaded " + player.getName() + "'s configuration file."));
return true;
}
Player player = Bukkit.getPlayer(args[1]);
double amount = Double.parseDouble(args[2]);
if (player == null) {
sender.sendMessage(Messages.NO_PLAYER.get());
return true;
}
if (amount > 1024.0 || amount < -1024.0) {
sender.sendMessage(Messages.OUT_OF_BOUNDS.get());
return true;
}
Luck luck = plugin.getHandler().getLuckContainer(player);
PlayerConfig config = plugin.getConfigMap().get(player.getUniqueId());
switch (args[0]) {
case "set" -> {
luck.setValue(amount);
plugin.getHandler().updatePlayer(player, luck);
config.setLuck(luck.getValue());
sender.sendMessage(MiniComponent.info("Successfully reset " + args[1] + "'s Luck stat."));
return true;
}
case "give" -> {
luck.addTo(amount);
plugin.getHandler().updatePlayer(player, luck);
config.setLuck(luck.getValue());
sender.sendMessage(MiniComponent.info("Successfully reset " + args[1] + "'s Luck stat."));
return true;
}
case "take" -> {
luck.takeFrom(amount);
plugin.getHandler().updatePlayer(player, luck);
config.setLuck(luck.getValue());
sender.sendMessage(MiniComponent.info("Successfully reset " + args[1] + "'s Luck stat."));
return true;
}
}
} else {
sender.sendMessage(Messages.NO_PERMISSION.get());
return true;
}
}
if (args.length == 2) {
if ((sender instanceof ConsoleCommandSender) || sender.hasPermission("luck.admin")) {
if (args[0].equalsIgnoreCase("reload") && args[1].equalsIgnoreCase("-m")) {
plugin.getConfig().reload();
sender.sendMessage(MiniComponent.info("Configuration successfully reloaded."));
return true;
}
if (args[0].equalsIgnoreCase("info")) {
Player player = Bukkit.getPlayer(args[1]);
if (player == null) {
sender.sendMessage(Messages.NO_PLAYER.get());
return true;
}
Luck luck = plugin.getHandler().getLuckContainer(player);
sender.sendMessage(MiniComponent.info("Luck stat for " + args[1] + ": " + luck.getValue()));
return true;
}
if (args[0].equalsIgnoreCase("reset")) {
Player player = Bukkit.getPlayer(args[1]);
if (player == null) {
sender.sendMessage(Messages.NO_PLAYER.get());
return true;
}
Luck luck = plugin.getHandler().getLuckContainer(player);
PlayerConfig config = plugin.getConfigMap().get(player.getUniqueId());
luck.reset();
plugin.getHandler().updatePlayer(player, luck);
config.setLuck(luck.getValue());
sender.sendMessage(MiniComponent.info("Successfully reset " + args[1] + "'s Luck stat."));
return true;
}
} else {
sender.sendMessage(Messages.NO_PERMISSION.get());
return true;
}
}
if (args.length == 1) {
if (args[0].equalsIgnoreCase("reload") && sender.hasPermission("luck.admin")) {
plugin.getConfigMap().values().forEach(PlayerConfig::reload);
sender.sendMessage(MiniComponent.info("Player configurations reloaded."));
return true;
}
if ((sender instanceof Player player) && player.hasPermission("luck.default")) {
if (args[0].equalsIgnoreCase("info")) {
Luck luck = plugin.getHandler().getLuckContainer(player);
player.sendMessage(MiniComponent.info("Your Luck: " + luck.getPercentage()));
return true;
}
} else if (sender instanceof ConsoleCommandSender) {
sender.sendMessage(Messages.NOT_FROM_CONSOLE.get());
return true;
}
}
return false;
}
@Override
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
List<String> completions = new ArrayList<>() {{
add("info");
}};
List<String> playerNames = Bukkit.getOnlinePlayers().stream().map(Player::getName).toList();
List<String> adminCommands = List.of("set", "reset", "give", "take", "reload");
if ((sender instanceof ConsoleCommandSender) || sender.hasPermission("luck.admin")) {
completions.addAll(adminCommands);
return completions.stream().filter(n -> n.startsWith(args[0])).toList();
}
if (adminCommands.contains(args[0])
&& sender.hasPermission("luck.admin")) {
if (args.length == 2) {
switch (args[0]) {
case "info":
case "reset":
return playerNames.stream().filter(n -> n.startsWith(args[1])).toList();
case "reload":
return List.of("-m", "-p");
}
}
if (args.length == 3 && playerNames.contains(args[1])) {
switch (args[0]) {
case "give":
case "take":
case "set":
return List.of("amount");
}
}
}
if (args[0].equalsIgnoreCase("reload")
&& args[1].equalsIgnoreCase("-p")
&& sender.hasPermission("luck.admin") && (args.length == 3)) {
return playerNames.stream().filter(n -> n.startsWith(args[2])).toList();
}
return completions.stream().filter(n -> n.startsWith(args[0])).toList();
}
}

View File

@ -0,0 +1,83 @@
package io.github.simplex.luck.util;
import io.github.simplex.luck.listener.AbstractListener;
import io.github.simplex.luck.player.Luck;
import org.bukkit.Bukkit;
import org.bukkit.entity.Item;
import org.bukkit.inventory.ItemStack;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
public class SneakyWorker {
public static void sneakyTry(SneakyTry sneakyTry) {
try {
sneakyTry.tryThis();
} catch (Exception ex) {
String sb = "An error of type: "
+ ex.getClass().getSimpleName()
+ " has occurred. A cause will be printed. \n\n"
+ ex.getCause();
Bukkit.getLogger().severe(sb);
}
}
public static void quietTry(SneakyTry sneakyTry) {
try {
sneakyTry.tryThis();
} catch (Exception ignored) {
}
}
public static Class[] getClasses(String packageName) throws ClassNotFoundException, IOException {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
assert classLoader != null;
String path = packageName.replace(".", "/");
Enumeration<URL> resources = classLoader.getResources(path);
List<File> dirs = new ArrayList<>();
while (resources.hasMoreElements()) {
URL resource = resources.nextElement();
dirs.add(new File(resource.getFile()));
}
ArrayList<Class> classes = new ArrayList<>();
for (File directory : dirs) {
classes.addAll(findClasses(directory, packageName));
}
return classes.toArray(new Class[classes.size()]);
}
private static List<Class<?>> findClasses(File directory, String packageName) throws ClassNotFoundException {
List<Class<?>> classes = new ArrayList<>();
if (!directory.exists()) {
return classes;
}
File[] files = directory.listFiles();
assert files != null;
for (File file : files) {
if (file.isDirectory()) {
assert !file.getName().contains(".");
classes.addAll(findClasses(file, packageName + "." + file.getName().substring(0, file.getName().length() - 6)));
} else if (file.getName().endsWith(".class")) {
classes.add(Class.forName(packageName + "." + file.getName().substring(0, file.getName().length() - 6)));
}
}
return classes;
}
public static Item move(Item item) {
ItemStack stack = item.getItemStack();
int rng = (Luck.RNG().nextInt(2, 5)) + stack.getAmount();
stack.setAmount(rng);
item.setItemStack(stack);
return item;
}
public interface SneakyTry {
void tryThis() throws Exception;
}
}

View File

@ -0,0 +1,26 @@
package io.github.simplex.luck.util;
import io.github.simplex.lib.ItemBuilder;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
public class SpecialFootItem {
public final ItemStack stack;
public SpecialFootItem() {
stack = ItemBuilder.of(Material.RABBIT_FOOT)
.setName("Enhanced Rabbit Foot")
.setAmount(1).setLore("A strange energy radiates from within.",
"This item will increase your luck multiplier by one.")
.build();
}
public ItemStack get() {
return stack;
}
public ItemMeta meta() {
return stack.getItemMeta();
}
}

View File

@ -0,0 +1,863 @@
/*
* This Metrics class was auto-generated and can be copied into your project if you are
* not using a build tool like Gradle or Maven for dependency management.
*
* IMPORTANT: You are not allowed to modify this class, except changing the package.
*
* Unallowed modifications include but are not limited to:
* - Remove the option for users to opt-out
* - Change the frequency for data submission
* - Obfuscate the code (every obfucator should allow you to make an exception for specific files)
* - Reformat the code (if you use a linter, add an exception)
*
* Violations will result in a ban of your plugin and account from bStats.
*/
package io.github.simplex.metrics;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.stream.Collectors;
import java.util.zip.GZIPOutputStream;
import javax.net.ssl.HttpsURLConnection;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
public class Metrics {
private final Plugin plugin;
private final MetricsBase metricsBase;
/**
* Creates a new Metrics instance.
*
* @param plugin Your plugin instance.
* @param serviceId The id of the service. It can be found at <a
* href="https://bstats.org/what-is-my-plugin-id">What is my plugin id?</a>
*/
public Metrics(JavaPlugin plugin, int serviceId) {
this.plugin = plugin;
// Get the config file
File bStatsFolder = new File(plugin.getDataFolder().getParentFile(), "bStats");
File configFile = new File(bStatsFolder, "config.yml");
YamlConfiguration config = YamlConfiguration.loadConfiguration(configFile);
if (!config.isSet("serverUuid")) {
config.addDefault("enabled", true);
config.addDefault("serverUuid", UUID.randomUUID().toString());
config.addDefault("logFailedRequests", false);
config.addDefault("logSentData", false);
config.addDefault("logResponseStatusText", false);
// Inform the server owners about bStats
config
.options()
.header(
"bStats (https://bStats.org) collects some basic information for plugin authors, like how\n"
+ "many people use their plugin and their total player count. It's recommended to keep bStats\n"
+ "enabled, but if you're not comfortable with this, you can turn this setting off. There is no\n"
+ "performance penalty associated with having metrics enabled, and data sent to bStats is fully\n"
+ "anonymous.")
.copyDefaults(true);
try {
config.save(configFile);
} catch (IOException ignored) {
}
}
// Load the data
boolean enabled = config.getBoolean("enabled", true);
String serverUUID = config.getString("serverUuid");
boolean logErrors = config.getBoolean("logFailedRequests", false);
boolean logSentData = config.getBoolean("logSentData", false);
boolean logResponseStatusText = config.getBoolean("logResponseStatusText", false);
metricsBase =
new MetricsBase(
"bukkit",
serverUUID,
serviceId,
enabled,
this::appendPlatformData,
this::appendServiceData,
submitDataTask -> Bukkit.getScheduler().runTask(plugin, submitDataTask),
plugin::isEnabled,
(message, error) -> this.plugin.getLogger().log(Level.WARNING, message, error),
(message) -> this.plugin.getLogger().log(Level.INFO, message),
logErrors,
logSentData,
logResponseStatusText);
}
/**
* Adds a custom chart.
*
* @param chart The chart to add.
*/
public void addCustomChart(CustomChart chart) {
metricsBase.addCustomChart(chart);
}
private void appendPlatformData(JsonObjectBuilder builder) {
builder.appendField("playerAmount", getPlayerAmount());
builder.appendField("onlineMode", Bukkit.getOnlineMode() ? 1 : 0);
builder.appendField("bukkitVersion", Bukkit.getVersion());
builder.appendField("bukkitName", Bukkit.getName());
builder.appendField("javaVersion", System.getProperty("java.version"));
builder.appendField("osName", System.getProperty("os.name"));
builder.appendField("osArch", System.getProperty("os.arch"));
builder.appendField("osVersion", System.getProperty("os.version"));
builder.appendField("coreCount", Runtime.getRuntime().availableProcessors());
}
private void appendServiceData(JsonObjectBuilder builder) {
builder.appendField("pluginVersion", plugin.getDescription().getVersion());
}
private int getPlayerAmount() {
try {
// Around MC 1.8 the return type was changed from an array to a collection,
// This fixes java.lang.NoSuchMethodError:
// org.bukkit.Bukkit.getOnlinePlayers()Ljava/util/Collection;
Method onlinePlayersMethod = Class.forName("org.bukkit.Server").getMethod("getOnlinePlayers");
return onlinePlayersMethod.getReturnType().equals(Collection.class)
? ((Collection<?>) onlinePlayersMethod.invoke(Bukkit.getServer())).size()
: ((Player[]) onlinePlayersMethod.invoke(Bukkit.getServer())).length;
} catch (Exception e) {
// Just use the new method if the reflection failed
return Bukkit.getOnlinePlayers().size();
}
}
public static class MetricsBase {
/** The version of the Metrics class. */
public static final String METRICS_VERSION = "3.0.0";
private static final ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1, task -> new Thread(task, "bStats-Metrics"));
private static final String REPORT_URL = "https://bStats.org/api/v2/data/%s";
private final String platform;
private final String serverUuid;
private final int serviceId;
private final Consumer<JsonObjectBuilder> appendPlatformDataConsumer;
private final Consumer<JsonObjectBuilder> appendServiceDataConsumer;
private final Consumer<Runnable> submitTaskConsumer;
private final Supplier<Boolean> checkServiceEnabledSupplier;
private final BiConsumer<String, Throwable> errorLogger;
private final Consumer<String> infoLogger;
private final boolean logErrors;
private final boolean logSentData;
private final boolean logResponseStatusText;
private final Set<CustomChart> customCharts = new HashSet<>();
private final boolean enabled;
/**
* Creates a new MetricsBase class instance.
*
* @param platform The platform of the service.
* @param serviceId The id of the service.
* @param serverUuid The server uuid.
* @param enabled Whether or not data sending is enabled.
* @param appendPlatformDataConsumer A consumer that receives a {@code JsonObjectBuilder} and
* appends all platform-specific data.
* @param appendServiceDataConsumer A consumer that receives a {@code JsonObjectBuilder} and
* appends all service-specific data.
* @param submitTaskConsumer A consumer that takes a runnable with the submit task. This can be
* used to delegate the data collection to a another thread to prevent errors caused by
* concurrency. Can be {@code null}.
* @param checkServiceEnabledSupplier A supplier to check if the service is still enabled.
* @param errorLogger A consumer that accepts log message and an error.
* @param infoLogger A consumer that accepts info log messages.
* @param logErrors Whether or not errors should be logged.
* @param logSentData Whether or not the sent data should be logged.
* @param logResponseStatusText Whether or not the response status text should be logged.
*/
public MetricsBase(
String platform,
String serverUuid,
int serviceId,
boolean enabled,
Consumer<JsonObjectBuilder> appendPlatformDataConsumer,
Consumer<JsonObjectBuilder> appendServiceDataConsumer,
Consumer<Runnable> submitTaskConsumer,
Supplier<Boolean> checkServiceEnabledSupplier,
BiConsumer<String, Throwable> errorLogger,
Consumer<String> infoLogger,
boolean logErrors,
boolean logSentData,
boolean logResponseStatusText) {
this.platform = platform;
this.serverUuid = serverUuid;
this.serviceId = serviceId;
this.enabled = enabled;
this.appendPlatformDataConsumer = appendPlatformDataConsumer;
this.appendServiceDataConsumer = appendServiceDataConsumer;
this.submitTaskConsumer = submitTaskConsumer;
this.checkServiceEnabledSupplier = checkServiceEnabledSupplier;
this.errorLogger = errorLogger;
this.infoLogger = infoLogger;
this.logErrors = logErrors;
this.logSentData = logSentData;
this.logResponseStatusText = logResponseStatusText;
checkRelocation();
if (enabled) {
// WARNING: Removing the option to opt-out will get your plugin banned from bStats
startSubmitting();
}
}
public void addCustomChart(CustomChart chart) {
this.customCharts.add(chart);
}
private void startSubmitting() {
final Runnable submitTask =
() -> {
if (!enabled || !checkServiceEnabledSupplier.get()) {
// Submitting data or service is disabled
scheduler.shutdown();
return;
}
if (submitTaskConsumer != null) {
submitTaskConsumer.accept(this::submitData);
} else {
this.submitData();
}
};
// Many servers tend to restart at a fixed time at xx:00 which causes an uneven distribution
// of requests on the
// bStats backend. To circumvent this problem, we introduce some randomness into the initial
// and second delay.
// WARNING: You must not modify and part of this Metrics class, including the submit delay or
// frequency!
// WARNING: Modifying this code will get your plugin banned on bStats. Just don't do it!
long initialDelay = (long) (1000 * 60 * (3 + Math.random() * 3));
long secondDelay = (long) (1000 * 60 * (Math.random() * 30));
scheduler.schedule(submitTask, initialDelay, TimeUnit.MILLISECONDS);
scheduler.scheduleAtFixedRate(
submitTask, initialDelay + secondDelay, 1000 * 60 * 30, TimeUnit.MILLISECONDS);
}
private void submitData() {
final JsonObjectBuilder baseJsonBuilder = new JsonObjectBuilder();
appendPlatformDataConsumer.accept(baseJsonBuilder);
final JsonObjectBuilder serviceJsonBuilder = new JsonObjectBuilder();
appendServiceDataConsumer.accept(serviceJsonBuilder);
JsonObjectBuilder.JsonObject[] chartData =
customCharts.stream()
.map(customChart -> customChart.getRequestJsonObject(errorLogger, logErrors))
.filter(Objects::nonNull)
.toArray(JsonObjectBuilder.JsonObject[]::new);
serviceJsonBuilder.appendField("id", serviceId);
serviceJsonBuilder.appendField("customCharts", chartData);
baseJsonBuilder.appendField("service", serviceJsonBuilder.build());
baseJsonBuilder.appendField("serverUUID", serverUuid);
baseJsonBuilder.appendField("metricsVersion", METRICS_VERSION);
JsonObjectBuilder.JsonObject data = baseJsonBuilder.build();
scheduler.execute(
() -> {
try {
// Send the data
sendData(data);
} catch (Exception e) {
// Something went wrong! :(
if (logErrors) {
errorLogger.accept("Could not submit bStats metrics data", e);
}
}
});
}
private void sendData(JsonObjectBuilder.JsonObject data) throws Exception {
if (logSentData) {
infoLogger.accept("Sent bStats metrics data: " + data.toString());
}
String url = String.format(REPORT_URL, platform);
HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection();
// Compress the data to save bandwidth
byte[] compressedData = compress(data.toString());
connection.setRequestMethod("POST");
connection.addRequestProperty("Accept", "application/json");
connection.addRequestProperty("Connection", "close");
connection.addRequestProperty("Content-Encoding", "gzip");
connection.addRequestProperty("Content-Length", String.valueOf(compressedData.length));
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("User-Agent", "Metrics-Service/1");
connection.setDoOutput(true);
try (DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream())) {
outputStream.write(compressedData);
}
StringBuilder builder = new StringBuilder();
try (BufferedReader bufferedReader =
new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
String line;
while ((line = bufferedReader.readLine()) != null) {
builder.append(line);
}
}
if (logResponseStatusText) {
infoLogger.accept("Sent data to bStats and received response: " + builder);
}
}
/** Checks that the class was properly relocated. */
private void checkRelocation() {
// You can use the property to disable the check in your test environment
if (System.getProperty("bstats.relocatecheck") == null
|| !System.getProperty("bstats.relocatecheck").equals("false")) {
// Maven's Relocate is clever and changes strings, too. So we have to use this little
// "trick" ... :D
final String defaultPackage =
new String(new byte[] {'o', 'r', 'g', '.', 'b', 's', 't', 'a', 't', 's'});
final String examplePackage =
new String(new byte[] {'y', 'o', 'u', 'r', '.', 'p', 'a', 'c', 'k', 'a', 'g', 'e'});
// We want to make sure no one just copy & pastes the example and uses the wrong package
// names
if (MetricsBase.class.getPackage().getName().startsWith(defaultPackage)
|| MetricsBase.class.getPackage().getName().startsWith(examplePackage)) {
throw new IllegalStateException("bStats Metrics class has not been relocated correctly!");
}
}
}
/**
* Gzips the given string.
*
* @param str The string to gzip.
* @return The gzipped string.
*/
private static byte[] compress(final String str) throws IOException {
if (str == null) {
return null;
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try (GZIPOutputStream gzip = new GZIPOutputStream(outputStream)) {
gzip.write(str.getBytes(StandardCharsets.UTF_8));
}
return outputStream.toByteArray();
}
}
public static class DrilldownPie extends CustomChart {
private final Callable<Map<String, Map<String, Integer>>> callable;
/**
* Class constructor.
*
* @param chartId The id of the chart.
* @param callable The callable which is used to request the chart data.
*/
public DrilldownPie(String chartId, Callable<Map<String, Map<String, Integer>>> callable) {
super(chartId);
this.callable = callable;
}
@Override
public JsonObjectBuilder.JsonObject getChartData() throws Exception {
JsonObjectBuilder valuesBuilder = new JsonObjectBuilder();
Map<String, Map<String, Integer>> map = callable.call();
if (map == null || map.isEmpty()) {
// Null = skip the chart
return null;
}
boolean reallyAllSkipped = true;
for (Map.Entry<String, Map<String, Integer>> entryValues : map.entrySet()) {
JsonObjectBuilder valueBuilder = new JsonObjectBuilder();
boolean allSkipped = true;
for (Map.Entry<String, Integer> valueEntry : map.get(entryValues.getKey()).entrySet()) {
valueBuilder.appendField(valueEntry.getKey(), valueEntry.getValue());
allSkipped = false;
}
if (!allSkipped) {
reallyAllSkipped = false;
valuesBuilder.appendField(entryValues.getKey(), valueBuilder.build());
}
}
if (reallyAllSkipped) {
// Null = skip the chart
return null;
}
return new JsonObjectBuilder().appendField("values", valuesBuilder.build()).build();
}
}
public static class AdvancedPie extends CustomChart {
private final Callable<Map<String, Integer>> callable;
/**
* Class constructor.
*
* @param chartId The id of the chart.
* @param callable The callable which is used to request the chart data.
*/
public AdvancedPie(String chartId, Callable<Map<String, Integer>> callable) {
super(chartId);
this.callable = callable;
}
@Override
protected JsonObjectBuilder.JsonObject getChartData() throws Exception {
JsonObjectBuilder valuesBuilder = new JsonObjectBuilder();
Map<String, Integer> map = callable.call();
if (map == null || map.isEmpty()) {
// Null = skip the chart
return null;
}
boolean allSkipped = true;
for (Map.Entry<String, Integer> entry : map.entrySet()) {
if (entry.getValue() == 0) {
// Skip this invalid
continue;
}
allSkipped = false;
valuesBuilder.appendField(entry.getKey(), entry.getValue());
}
if (allSkipped) {
// Null = skip the chart
return null;
}
return new JsonObjectBuilder().appendField("values", valuesBuilder.build()).build();
}
}
public static class MultiLineChart extends CustomChart {
private final Callable<Map<String, Integer>> callable;
/**
* Class constructor.
*
* @param chartId The id of the chart.
* @param callable The callable which is used to request the chart data.
*/
public MultiLineChart(String chartId, Callable<Map<String, Integer>> callable) {
super(chartId);
this.callable = callable;
}
@Override
protected JsonObjectBuilder.JsonObject getChartData() throws Exception {
JsonObjectBuilder valuesBuilder = new JsonObjectBuilder();
Map<String, Integer> map = callable.call();
if (map == null || map.isEmpty()) {
// Null = skip the chart
return null;
}
boolean allSkipped = true;
for (Map.Entry<String, Integer> entry : map.entrySet()) {
if (entry.getValue() == 0) {
// Skip this invalid
continue;
}
allSkipped = false;
valuesBuilder.appendField(entry.getKey(), entry.getValue());
}
if (allSkipped) {
// Null = skip the chart
return null;
}
return new JsonObjectBuilder().appendField("values", valuesBuilder.build()).build();
}
}
public static class SimpleBarChart extends CustomChart {
private final Callable<Map<String, Integer>> callable;
/**
* Class constructor.
*
* @param chartId The id of the chart.
* @param callable The callable which is used to request the chart data.
*/
public SimpleBarChart(String chartId, Callable<Map<String, Integer>> callable) {
super(chartId);
this.callable = callable;
}
@Override
protected JsonObjectBuilder.JsonObject getChartData() throws Exception {
JsonObjectBuilder valuesBuilder = new JsonObjectBuilder();
Map<String, Integer> map = callable.call();
if (map == null || map.isEmpty()) {
// Null = skip the chart
return null;
}
for (Map.Entry<String, Integer> entry : map.entrySet()) {
valuesBuilder.appendField(entry.getKey(), new int[] {entry.getValue()});
}
return new JsonObjectBuilder().appendField("values", valuesBuilder.build()).build();
}
}
public abstract static class CustomChart {
private final String chartId;
protected CustomChart(String chartId) {
if (chartId == null) {
throw new IllegalArgumentException("chartId must not be null");
}
this.chartId = chartId;
}
public JsonObjectBuilder.JsonObject getRequestJsonObject(
BiConsumer<String, Throwable> errorLogger, boolean logErrors) {
JsonObjectBuilder builder = new JsonObjectBuilder();
builder.appendField("chartId", chartId);
try {
JsonObjectBuilder.JsonObject data = getChartData();
if (data == null) {
// If the data is null we don't send the chart.
return null;
}
builder.appendField("data", data);
} catch (Throwable t) {
if (logErrors) {
errorLogger.accept("Failed to get data for custom chart with id " + chartId, t);
}
return null;
}
return builder.build();
}
protected abstract JsonObjectBuilder.JsonObject getChartData() throws Exception;
}
public static class SimplePie extends CustomChart {
private final Callable<String> callable;
/**
* Class constructor.
*
* @param chartId The id of the chart.
* @param callable The callable which is used to request the chart data.
*/
public SimplePie(String chartId, Callable<String> callable) {
super(chartId);
this.callable = callable;
}
@Override
protected JsonObjectBuilder.JsonObject getChartData() throws Exception {
String value = callable.call();
if (value == null || value.isEmpty()) {
// Null = skip the chart
return null;
}
return new JsonObjectBuilder().appendField("value", value).build();
}
}
public static class AdvancedBarChart extends CustomChart {
private final Callable<Map<String, int[]>> callable;
/**
* Class constructor.
*
* @param chartId The id of the chart.
* @param callable The callable which is used to request the chart data.
*/
public AdvancedBarChart(String chartId, Callable<Map<String, int[]>> callable) {
super(chartId);
this.callable = callable;
}
@Override
protected JsonObjectBuilder.JsonObject getChartData() throws Exception {
JsonObjectBuilder valuesBuilder = new JsonObjectBuilder();
Map<String, int[]> map = callable.call();
if (map == null || map.isEmpty()) {
// Null = skip the chart
return null;
}
boolean allSkipped = true;
for (Map.Entry<String, int[]> entry : map.entrySet()) {
if (entry.getValue().length == 0) {
// Skip this invalid
continue;
}
allSkipped = false;
valuesBuilder.appendField(entry.getKey(), entry.getValue());
}
if (allSkipped) {
// Null = skip the chart
return null;
}
return new JsonObjectBuilder().appendField("values", valuesBuilder.build()).build();
}
}
public static class SingleLineChart extends CustomChart {
private final Callable<Integer> callable;
/**
* Class constructor.
*
* @param chartId The id of the chart.
* @param callable The callable which is used to request the chart data.
*/
public SingleLineChart(String chartId, Callable<Integer> callable) {
super(chartId);
this.callable = callable;
}
@Override
protected JsonObjectBuilder.JsonObject getChartData() throws Exception {
int value = callable.call();
if (value == 0) {
// Null = skip the chart
return null;
}
return new JsonObjectBuilder().appendField("value", value).build();
}
}
/**
* An extremely simple JSON builder.
*
* <p>While this class is neither feature-rich nor the most performant one, it's sufficient enough
* for its use-case.
*/
public static class JsonObjectBuilder {
private StringBuilder builder = new StringBuilder();
private boolean hasAtLeastOneField = false;
public JsonObjectBuilder() {
builder.append("{");
}
/**
* Appends a null field to the JSON.
*
* @param key The key of the field.
* @return A reference to this object.
*/
public JsonObjectBuilder appendNull(String key) {
appendFieldUnescaped(key, "null");
return this;
}
/**
* Appends a string field to the JSON.
*
* @param key The key of the field.
* @param value The value of the field.
* @return A reference to this object.
*/
public JsonObjectBuilder appendField(String key, String value) {
if (value == null) {
throw new IllegalArgumentException("JSON value must not be null");
}
appendFieldUnescaped(key, "\"" + escape(value) + "\"");
return this;
}
/**
* Appends an integer field to the JSON.
*
* @param key The key of the field.
* @param value The value of the field.
* @return A reference to this object.
*/
public JsonObjectBuilder appendField(String key, int value) {
appendFieldUnescaped(key, String.valueOf(value));
return this;
}
/**
* Appends an object to the JSON.
*
* @param key The key of the field.
* @param object The object.
* @return A reference to this object.
*/
public JsonObjectBuilder appendField(String key, JsonObject object) {
if (object == null) {
throw new IllegalArgumentException("JSON object must not be null");
}
appendFieldUnescaped(key, object.toString());
return this;
}
/**
* Appends a string array to the JSON.
*
* @param key The key of the field.
* @param values The string array.
* @return A reference to this object.
*/
public JsonObjectBuilder appendField(String key, String[] values) {
if (values == null) {
throw new IllegalArgumentException("JSON values must not be null");
}
String escapedValues =
Arrays.stream(values)
.map(value -> "\"" + escape(value) + "\"")
.collect(Collectors.joining(","));
appendFieldUnescaped(key, "[" + escapedValues + "]");
return this;
}
/**
* Appends an integer array to the JSON.
*
* @param key The key of the field.
* @param values The integer array.
* @return A reference to this object.
*/
public JsonObjectBuilder appendField(String key, int[] values) {
if (values == null) {
throw new IllegalArgumentException("JSON values must not be null");
}
String escapedValues =
Arrays.stream(values).mapToObj(String::valueOf).collect(Collectors.joining(","));
appendFieldUnescaped(key, "[" + escapedValues + "]");
return this;
}
/**
* Appends an object array to the JSON.
*
* @param key The key of the field.
* @param values The integer array.
* @return A reference to this object.
*/
public JsonObjectBuilder appendField(String key, JsonObject[] values) {
if (values == null) {
throw new IllegalArgumentException("JSON values must not be null");
}
String escapedValues =
Arrays.stream(values).map(JsonObject::toString).collect(Collectors.joining(","));
appendFieldUnescaped(key, "[" + escapedValues + "]");
return this;
}
/**
* Appends a field to the object.
*
* @param key The key of the field.
* @param escapedValue The escaped value of the field.
*/
private void appendFieldUnescaped(String key, String escapedValue) {
if (builder == null) {
throw new IllegalStateException("JSON has already been built");
}
if (key == null) {
throw new IllegalArgumentException("JSON key must not be null");
}
if (hasAtLeastOneField) {
builder.append(",");
}
builder.append("\"").append(escape(key)).append("\":").append(escapedValue);
hasAtLeastOneField = true;
}
/**
* Builds the JSON string and invalidates this builder.
*
* @return The built JSON string.
*/
public JsonObject build() {
if (builder == null) {
throw new IllegalStateException("JSON has already been built");
}
JsonObject object = new JsonObject(builder.append("}").toString());
builder = null;
return object;
}
/**
* Escapes the given string like stated in https://www.ietf.org/rfc/rfc4627.txt.
*
* <p>This method escapes only the necessary characters '"', '\'. and '\u0000' - '\u001F'.
* Compact escapes are not used (e.g., '\n' is escaped as "\u000a" and not as "\n").
*
* @param value The value to escape.
* @return The escaped value.
*/
private static String escape(String value) {
final StringBuilder builder = new StringBuilder();
for (int i = 0; i < value.length(); i++) {
char c = value.charAt(i);
if (c == '"') {
builder.append("\\\"");
} else if (c == '\\') {
builder.append("\\\\");
} else if (c <= '\u000F') {
builder.append("\\u000").append(Integer.toHexString(c));
} else if (c <= '\u001F') {
builder.append("\\u00").append(Integer.toHexString(c));
} else {
builder.append(c);
}
}
return builder.toString();
}
/**
* A super simple representation of a JSON object.
*
* <p>This class only exists to make methods of the {@link JsonObjectBuilder} type-safe and not
* allow a raw string inputs for methods like {@link JsonObjectBuilder#appendField(String,
* JsonObject)}.
*/
public static class JsonObject {
private final String value;
private JsonObject(String value) {
this.value = value;
}
@Override
public String toString() {
return value;
}
}
}
}

View File

@ -0,0 +1,26 @@
# These entries are for the minimum amount of luck required for the event to actually trigger.
# These work in addition to the RNG generator,
# meaning both the RNG and this condition must be met for the event to trigger.
# These values must be in the form of doubles, as listed below.
# The maximum amount of luck that can be attributed is 1024.0, and the minimum is -1024.0
high_rarity_chance: 512.0
medium_rarity_chance: 128.0
low_rarity_chance: 64.0
# The following entries are for the rarity level of each event trigger.
# This will determine which rarity chance to use which ensures players
# The following values are accepted: NONE, LOW, MED, HIGH
# - None implies that there is no rarity chance attributed to that feature.
# These entries are case-sensitive.
block_drops: LOW
bonemeal: MED
cheat_death: MED
enchanting: HIGH
experience: HIGH
item_drops: LOW
random_effect: HIGH
restore_hunger: NONE
take_damage: MED
unbreakable: HIGH