Skip to content

Commit

Permalink
Deduplicate compat code by building patches on top of each other; bun…
Browse files Browse the repository at this point in the history
…dle into single release JAR
  • Loading branch information
NebelNidas committed Jun 24, 2024
1 parent c935ec3 commit 2d9f024
Show file tree
Hide file tree
Showing 43 changed files with 534 additions and 367 deletions.
11 changes: 6 additions & 5 deletions bettersleeping-1.17/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
version += '+1.17'
base {
archivesName = archivesName.get() + '-compat-1.17'
}

dependencies {
implementation project(path: ':bettersleeping-core', configuration: 'namedElements')
include project(path: ':bettersleeping-core')
api project(path: ':bettersleeping-core', configuration: 'namedElements')

minecraft "com.mojang:minecraft:${project.minecraft_version_1_17}"
mappings "net.fabricmc:yarn:${project.yarn_mappings_1_17}:v2"
Expand All @@ -12,8 +13,8 @@ dependencies {
modImplementation(fabricApi.module('fabric-lifecycle-events-v1', project.fabric_api_version_1_17))

// Other mods
modImplementation "com.terraformersmc:modmenu:${project.modmenu_version_1_17}"
modImplementation("me.shedaniel.cloth:cloth-config-fabric:${project.cloth_config_version_1_17}") {
modRuntimeOnly "com.terraformersmc:modmenu:${project.modmenu_version_1_17}"
modRuntimeOnly("me.shedaniel.cloth:cloth-config-fabric:${project.cloth_config_version_1_17}") {
exclude(group: 'net.fabricmc.fabric-api')
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
package com.github.reviversmc.bettersleeping;

import net.fabricmc.api.ModInitializer;
import java.util.function.Supplier;

import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;

import com.github.reviversmc.bettersleeping.compat.minecraft.McVersionCompatInitializer;
import com.github.reviversmc.bettersleeping.compat.minecraft.McVersionHelper;
import com.github.reviversmc.bettersleeping.events.EventHandler117;

public class BetterSleeping117 implements ModInitializer {
public static EventHandler117 eventHandler;
public class BetterSleeping117 extends McVersionCompatInitializer {
static final Supplier<Boolean> IS_COMPATIBLE = () -> McVersionHelper.isWithin("1.17", "1.18.2");

@Override
public boolean isCompatible() {
return IS_COMPATIBLE.get();
}

@Override
public void onInitialize() {
eventHandler = new EventHandler117();
public void initialize() {
BetterSleeping.eventHandler = new EventHandler117();

ServerTickEvents.END_SERVER_TICK.register(eventHandler::onTick);
ServerTickEvents.END_SERVER_TICK.register(BetterSleeping.eventHandler::onTick);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.github.reviversmc.bettersleeping;

import java.util.Collections;
import java.util.List;

import com.github.reviversmc.bettersleeping.compat.minecraft.McVersionMixinProvider;

public class MixinProvider117 extends McVersionMixinProvider {
@Override
public List<String> getMixins() {
if (BetterSleeping117.IS_COMPATIBLE.get()) {
return Collections.singletonList("ServerPlayerEntityMixin117");
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

import com.github.reviversmc.bettersleeping.BetterSleeping117;
import com.github.reviversmc.bettersleeping.BetterSleeping;

@Mixin(ServerPlayerEntity.class)
public abstract class ServerPlayerEntityMixin117 extends PlayerEntity {
Expand All @@ -21,12 +21,12 @@ public ServerPlayerEntityMixin117(World world, BlockPos pos, float yaw, GameProf

@Inject(method = "sleep", at = @At("TAIL"))
public void onSleep(BlockPos position, CallbackInfo callbackInfo) {
BetterSleeping117.eventHandler.onSleep(cast(this));
BetterSleeping.eventHandler.onSleep(cast(this));
}

@Inject(method = "wakeUp", at = @At("RETURN"))
@Inject(method = "wakeUp", at = @At("TAIL"))
private void onWakeUp(boolean bl, boolean updateSleepingPlayers, CallbackInfo callbackInfo) {
BetterSleeping117.eventHandler.onWakeUp(cast(this));
BetterSleeping.eventHandler.onWakeUp(cast(this));
}

private ServerPlayerEntity cast(PlayerEntity player) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
"required": true,
"package": "com.github.reviversmc.bettersleeping.mixin",
"compatibilityLevel": "JAVA_16",
"mixins": [
"ServerPlayerEntityMixin117"
],
"plugin": "com.github.reviversmc.bettersleeping.MixinProvider117",
"injectors": {
"defaultRequire": 1
}
Expand Down
36 changes: 17 additions & 19 deletions bettersleeping-1.17/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
{
"schemaVersion": 1,
"id": "bettersleeping",
"id": "bettersleeping-compat-1-17",
"version": "${version}",
"name": "BetterSleeping",
"description": "This mod adds sleep notifications, gives sleeping players buffs and debuffs to the ones who haven't slept in a long time!",
"authors": [
"NebelNidas",
"TacoMonkey",
"ExtraCrafTX"
],
"contact": {
"homepage": "https://github.com/ReviversMC/bettersleeping",
"issues": "https://github.com/ReviversMC/bettersleeping/issues"
},
"icon": "assets/bettersleeping/icon.png",
"name": "BetterSleeping 1.17 Compat",
"license": "CC-BY-NC-SA-4.0",
"environment": "*",
"mixins": [
"bettersleeping.mixins.json"
],
"entrypoints": {
"main": [
"bettersleeping": [
"com.github.reviversmc.bettersleeping.BetterSleeping117"
],
"modmenu": [
"com.github.reviversmc.bettersleeping.client.ModMenuIntegration"
"com.github.reviversmc.bettersleeping.compat.mods.ModMenuIntegration"
]
},
"mixins": [
"bettersleeping117.mixins.json"
],
"depends": {
"minecraft": ">=1.17- <1.19",
"fabricloader": ">=0.12.9",
"minecraft": ">=1.17",
"bettersleeping-core": "*"
},
"custom": {
"modmenu": {
"badges": ["library"],
"parent": {
"id": "bettersleeping"
}
}
}
}
11 changes: 6 additions & 5 deletions bettersleeping-1.19.3/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
version += '+1.19.3'
base {
archivesName = archivesName.get() + '-compat-1.19.3'
}

dependencies {
implementation project(path: ':bettersleeping-core', configuration: 'namedElements')
include project(path: ':bettersleeping-core')
api project(path: ':bettersleeping-1.19', configuration: 'namedElements')

minecraft "com.mojang:minecraft:${project.minecraft_version_1_19_3}"
mappings "net.fabricmc:yarn:${project.yarn_mappings_1_19_3}:v2"
Expand All @@ -12,8 +13,8 @@ dependencies {
modImplementation(fabricApi.module('fabric-lifecycle-events-v1', project.fabric_api_version_1_19_3))

// Other mods
modImplementation "com.terraformersmc:modmenu:${project.modmenu_version_1_19_3}"
modImplementation("me.shedaniel.cloth:cloth-config-fabric:${project.cloth_config_version_1_19_3}") {
modRuntimeOnly "com.terraformersmc:modmenu:${project.modmenu_version_1_19_3}"
modRuntimeOnly("me.shedaniel.cloth:cloth-config-fabric:${project.cloth_config_version_1_19_3}") {
exclude(group: 'net.fabricmc.fabric-api')
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
package com.github.reviversmc.bettersleeping;

import net.fabricmc.api.ModInitializer;
import java.util.function.Supplier;

import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;

import com.github.reviversmc.bettersleeping.compat.minecraft.McVersionCompatInitializer;
import com.github.reviversmc.bettersleeping.compat.minecraft.McVersionHelper;
import com.github.reviversmc.bettersleeping.events.EventHandler1193;

public class BetterSleeping1193 implements ModInitializer {
public static EventHandler1193 eventHandler;
public class BetterSleeping1193 extends McVersionCompatInitializer {
static final Supplier<Boolean> IS_COMPATIBLE = () -> McVersionHelper.isWithin("1.19.3", "1.20.4");

@Override
public boolean isCompatible() {
return IS_COMPATIBLE.get();
}

@Override
public void onInitialize() {
eventHandler = new EventHandler1193();
public void initialize() {
BetterSleeping.eventHandler = new EventHandler1193();

ServerTickEvents.END_SERVER_TICK.register(eventHandler::onTick);
ServerTickEvents.END_SERVER_TICK.register(BetterSleeping.eventHandler::onTick);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.github.reviversmc.bettersleeping;

import java.util.List;

import com.github.reviversmc.bettersleeping.compat.minecraft.McVersionMixinProvider;

public class MixinProvider1193 extends McVersionMixinProvider {
@Override
public List<String> getMixins() {
if (BetterSleeping1193.IS_COMPATIBLE.get()) {
return List.of("ServerPlayerEntityMixin119");
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,16 @@
import net.minecraft.registry.Registries;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;
import net.minecraft.world.dimension.DimensionType;

import com.github.reviversmc.bettersleeping.BetterSleeping;

public class EventHandler1193 extends EventHandlerBase {
@Override
protected void sendPlayerMessage(ServerPlayerEntity player, String message) {
MutableText formattedMessage = Text.literal(message);

for (String format : BetterSleeping.config.messages.messageFormatting) {
formattedMessage.formatted(Formatting.byName(format));
}

player.sendMessage(formattedMessage);
}

public class EventHandler1193 extends EventHandler119 {
@Override
protected ServerWorld getServerWorld(ServerPlayerEntity player) {
// ServerPlayerEntity#getWorld() is method_14220, which doesn't exist in 1.20+.
// Entity#getWorld() is method_37908, which still exists in 1.20+.
return (ServerWorld) ((Entity) player).getWorld();
}

@Override
protected boolean isBedWorking(DimensionType dimension) {
return dimension.bedWorks();
}

@Override
protected List<StatusEffect> getHarmfulEffects() {
return Registries.STATUS_EFFECT.stream()
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
"required": true,
"package": "com.github.reviversmc.bettersleeping.mixin",
"compatibilityLevel": "JAVA_17",
"mixins": [
"ServerPlayerEntityMixin119"
],
"plugin": "com.github.reviversmc.bettersleeping.MixinProvider1193",
"injectors": {
"defaultRequire": 1
}
Expand Down
38 changes: 18 additions & 20 deletions bettersleeping-1.19.3/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
{
"schemaVersion": 1,
"id": "bettersleeping",
"id": "bettersleeping-compat-1-19-3",
"version": "${version}",
"name": "BetterSleeping",
"description": "This mod adds sleep notifications, gives sleeping players buffs and debuffs to the ones who haven't slept in a long time!",
"authors": [
"NebelNidas",
"TacoMonkey",
"ExtraCrafTX"
],
"contact": {
"homepage": "https://github.com/ReviversMC/bettersleeping",
"issues": "https://github.com/ReviversMC/bettersleeping/issues"
},
"icon": "assets/bettersleeping/icon.png",
"name": "BetterSleeping 1.19.3 Compat",
"license": "CC-BY-NC-SA-4.0",
"environment": "*",
"mixins": [
"bettersleeping.mixins.json"
],
"entrypoints": {
"main": [
"bettersleeping": [
"com.github.reviversmc.bettersleeping.BetterSleeping1193"
],
"modmenu": [
"com.github.reviversmc.bettersleeping.client.ModMenuIntegration"
"com.github.reviversmc.bettersleeping.compat.mods.ModMenuIntegration"
]
},
"mixins": [
"bettersleeping1193.mixins.json"
],
"depends": {
"minecraft": ">=1.19.3- <1.20.5",
"bettersleeping-core": "*"
"fabricloader": ">=0.12.9",
"minecraft": ">=1.19.3",
"bettersleeping-compat-1-19": "*"
},
"custom": {
"modmenu": {
"badges": ["library"],
"parent": {
"id": "bettersleeping"
}
}
}
}
11 changes: 6 additions & 5 deletions bettersleeping-1.19/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
version += '+1.19'
base {
archivesName = archivesName.get() + '-compat-1.19'
}

dependencies {
implementation project(path: ':bettersleeping-core', configuration: 'namedElements')
include project(path: ':bettersleeping-core')
api project(path: ':bettersleeping-1.17', configuration: 'namedElements')

minecraft "com.mojang:minecraft:${project.minecraft_version_1_19}"
mappings "net.fabricmc:yarn:${project.yarn_mappings_1_19}:v2"
Expand All @@ -12,8 +13,8 @@ dependencies {
modImplementation(fabricApi.module('fabric-lifecycle-events-v1', project.fabric_api_version_1_19))

// Other mods
modImplementation "com.terraformersmc:modmenu:${project.modmenu_version_1_19}"
modImplementation("me.shedaniel.cloth:cloth-config-fabric:${project.cloth_config_version_1_19}") {
modRuntimeOnly "com.terraformersmc:modmenu:${project.modmenu_version_1_19}"
modRuntimeOnly("me.shedaniel.cloth:cloth-config-fabric:${project.cloth_config_version_1_19}") {
exclude(group: 'net.fabricmc.fabric-api')
}

Expand Down
Loading

0 comments on commit 2d9f024

Please sign in to comment.