Skip to content

Commit

Permalink
fix: rpgstats compat update
Browse files Browse the repository at this point in the history
closes: #35
  • Loading branch information
Jamalam360 committed Jun 3, 2023
1 parent 82da602 commit 4beb766
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 24 deletions.
8 changes: 4 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
## Changelog

### Features

### Fixes

Closed Issues: None
- RPGStats compatibility is now compatible with both RPGStats 5.0.0+ and 4.0.0+.

Closed Issues: #35

[Full Changelog](https://github.com/JamCoreModding/right-click-harvest/compare/...)
[Full Changelog](https://github.com/JamCoreModding/right-click-harvest/compare/3.2.0+1.19.x...3.2.1+1.19.x-fabric)
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
org.gradle.jvmargs=-Xmx4G
org.gradle.parallel=true

mod_version=3.2.0+1.19.x
release_name=V3.2.0 [1.19.x]
mod_version=3.2.1+1.19.x-fabric
release_name=V3.2.1 [1.19.x Fabric]
archive_base_name=right-click-harvest
supported_versions=1.19,1.19.1,1.19.2,1.19.3,1.19.4

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,36 +33,32 @@
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier;

public class RpgStats implements ModInitializer {
public class RpgStatsGte5 implements ModInitializer {
private static final Identifier FARMING = new Identifier("rpgstats:farming");
private Object levelUtils = null;
private Method addXpAndLevelUp = null;

@Override
public void onInitialize() {
try {
String identifierMapping = FabricLoader.getInstance().getMappingResolver()
.mapClassName("intermediary", "net.minecraft.class_2960");
Class<?> identifier = Class.forName(identifierMapping);

Class<?> clazz = Class.forName("io.github.silverandro.rpgstats.LevelUtils");
levelUtils = clazz.getField("INSTANCE").get(null);
addXpAndLevelUp = clazz.getMethod("addXpAndLevelUp", identifier, ServerPlayerEntity.class,
addXpAndLevelUp = clazz.getMethod("addXpAndLevelUp", Identifier.class, ServerPlayerEntity.class,
int.class);

RightClickHarvestCallbacks.AFTER_HARVEST.register((player, block) -> {
if (player instanceof ServerPlayerEntity serverPlayerEntity) {
try {
addXpAndLevelUp.invoke(levelUtils, FARMING, serverPlayerEntity, 1);
} catch (Exception e) {
RightClickHarvestModInit.LOGGER.error("Failed to call RPGStats methods");
e.printStackTrace();
}
}
});
} catch (Exception e) {
RightClickHarvestModInit.LOGGER.error("Failed to enable RPGStats compatibility");
e.printStackTrace();
}

RightClickHarvestCallbacks.AFTER_HARVEST.register((player, block) -> {
if (player instanceof ServerPlayerEntity serverPlayerEntity) {
try {
addXpAndLevelUp.invoke(levelUtils, FARMING, serverPlayerEntity, 1);
} catch (Exception e) {
RightClickHarvestModInit.LOGGER.error("Failed to call RPGStats methods");
e.printStackTrace();
}
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2023 Jamalam360
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package io.github.jamalam360.rightclickharvest.compat;

import java.lang.reflect.Method;

import io.github.jamalam360.rightclickharvest.RightClickHarvestCallbacks;
import io.github.jamalam360.rightclickharvest.RightClickHarvestModInit;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier;

public class RpgStatsLt5 implements ModInitializer {
private static final Identifier FARMING = new Identifier("rpgstats:farming");
private Method addXpAndLevelUp = null;

@Override
public void onInitialize() {
try {
Class<?> clazz = Class.forName("mc.rpgstats.main.RPGStats");
addXpAndLevelUp = clazz.getMethod("addXpAndLevelUp", Identifier.class, ServerPlayerEntity.class,
int.class);

RightClickHarvestCallbacks.AFTER_HARVEST.register((player, block) -> {
if (player instanceof ServerPlayerEntity serverPlayerEntity) {
try {
addXpAndLevelUp.invoke(null, FARMING, serverPlayerEntity, 1);
} catch (Exception e) {
RightClickHarvestModInit.LOGGER.error("Failed to call RPGStats methods");
e.printStackTrace();
}
}
});
} catch (Exception e) {
RightClickHarvestModInit.LOGGER.error("Failed to enable RPGStats compatibility");
e.printStackTrace();
}
}
}

3 changes: 2 additions & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
},
"custom": {
"jamlib:compatibility_modules": {
"rpgstats": "io.github.jamalam360.rightclickharvest.compat.RpgStats"
"rpgstats[>=5.0.0]": "io.github.jamalam360.rightclickharvest.compat.RpgStatsGte5",
"rpgstats[<5.0.0]": "io.github.jamalam360.rightclickharvest.compat.RpgStatsLt5"
}
}
}

0 comments on commit 4beb766

Please sign in to comment.