Skip to content

Commit

Permalink
add: gamerule 'boatDespawnTimer'
Browse files Browse the repository at this point in the history
  • Loading branch information
SammyForReal committed Jan 18, 2024
1 parent fa0812b commit f8b1614
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 2 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ org.gradle.jvmargs=-Xmx2G
org.gradle.parallel=true

# Mod properties
mod_version=1.0.1
mod_version=1.1.0
maven_group=cc.tweaked_programs
archives_base_name=partnership

Expand Down
2 changes: 2 additions & 0 deletions src/client/resources/assets/partnership/lang/de_de.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@

"itemGroup.partnership": "Partnership Mod",

"gamerule.boatDespawnTimer": "Boote verschwinden timer",

"modmenu.partnership.modfest": "ModFest 1.20",
"modmenu.partnership.kofi": "Ko-fi",
"modmenu.partnership.modrinth": "Modrinth",
Expand Down
2 changes: 2 additions & 0 deletions src/client/resources/assets/partnership/lang/en_gb.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@

"itemGroup.partnership": "Partnership Mod",

"gamerule.boatDespawnTimer": "Boat despawn timer",

"modmenu.partnership.modfest": "ModFest 1.20",
"modmenu.partnership.kofi": "Ko-fi",
"modmenu.partnership.modrinth": "Modrinth"
Expand Down
2 changes: 2 additions & 0 deletions src/client/resources/assets/partnership/lang/en_pt.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@

"itemGroup.partnership": "Partnership Mod",

"gamerule.boatDespawnTimer": "Boat despawn timer",

"modmenu.partnership.modfest": "ModFest 1.20",
"modmenu.partnership.kofi": "Ko-fi",
"modmenu.partnership.modrinth": "Modrinth"
Expand Down
2 changes: 2 additions & 0 deletions src/client/resources/assets/partnership/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@

"itemGroup.partnership": "Partnership Mod",

"gamerule.boatDespawnTimer": "Boat despawn timer",

"modmenu.partnership.modfest": "ModFest 1.20",
"modmenu.partnership.kofi": "Ko-fi",
"modmenu.partnership.modrinth": "Modrinth",
Expand Down
2 changes: 2 additions & 0 deletions src/client/resources/assets/partnership/lang/lol_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@

"itemGroup.partnership": "Partnership Mod",

"gamerule.boatDespawnTimer": "Boaty despawunuy timer",

"modmenu.partnership.modfest": "ModFest 1.20",
"modmenu.partnership.kofi": "Ko-fi",
"modmenu.partnership.modrinth": "Modrinth"
Expand Down
96 changes: 96 additions & 0 deletions src/main/java/cc/tweaked_programs/mixin/BoatDespawnMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package cc.tweaked_programs.mixin;

import cc.tweaked_programs.partnership.main.registries.GameRuleRegistries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializers;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.VariantHolder;
import net.minecraft.world.entity.vehicle.Boat;
import net.minecraft.world.entity.vehicle.VehicleEntity;
import net.minecraft.world.level.Level;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(Boat.class)
abstract public class BoatDespawnMixin extends VehicleEntity implements VariantHolder<Boat.Type> {
public BoatDespawnMixin(EntityType<?> entityType, Level level) {
super(entityType, level);
}

@Unique
@SuppressWarnings("WrongEntityDataParameterClass")
private static final EntityDataAccessor<Integer> DATA_ID_DESPAWN = SynchedEntityData.defineId(Boat.class, EntityDataSerializers.INT);

@Inject(at = @At("RETURN"), method = "<init>(Lnet/minecraft/world/entity/EntityType;Lnet/minecraft/world/level/Level;)V")
public void partnership$defineDespawnTimer(EntityType<? extends Boat> entityType, Level level, CallbackInfo ci) {
int timer = getDefinedDespawnTimer(level);
if (timer < 0)
timer = Integer.MAX_VALUE;

setDespawnTimer(timer);
}

@Inject(at = @At("HEAD"), method = "tick")
public void partnership$shouldDespawn(CallbackInfo ci) {
int despawnTimer = getDespawnTimer();
int definedDespawnTimer = getDefinedDespawnTimer(this.level());

if (definedDespawnTimer >= 0) {
// Boats should despawn
if (this.getPassengers().isEmpty()) {
// Countdown starts as boat is not being used anymore
if (despawnTimer > 0) {
despawnTimer--;

// Make sure to stay in bounds
if (despawnTimer > definedDespawnTimer)
despawnTimer = definedDespawnTimer;

// Update
setDespawnTimer(despawnTimer);
} else if (despawnTimer == 0) {
// Goodbye :)
this.discard();
}
} else if (despawnTimer >= 0) {
// Reset timer
setDespawnTimer(getDefinedDespawnTimer(this.level()));
}
}
}

@Inject(at = @At("TAIL"), method = "defineSynchedData")
protected void partnership$defineDespawnDataId(CallbackInfo ci) {
this.entityData.define(DATA_ID_DESPAWN, -1);
}

@Inject(at = @At("TAIL"), method = "addAdditionalSaveData")
protected void partnership$addDespawnSaveData(CompoundTag compoundTag, CallbackInfo ci) {
compoundTag.putInt("despawnTimer", getDespawnTimer());
}

@Inject(at = @At("TAIL"), method = "readAdditionalSaveData")
protected void readAdditionalSaveData(CompoundTag compoundTag, CallbackInfo ci) {
setDespawnTimer(compoundTag.getInt("despawnTimer"));
}

@Unique
public void setDespawnTimer(int timer) {
this.entityData.set(DATA_ID_DESPAWN, timer);
}

@Unique
public int getDespawnTimer() {
return this.entityData.get(DATA_ID_DESPAWN);
}

@Unique
private int getDefinedDespawnTimer(Level level) {
return level.getGameRules().getInt(GameRuleRegistries.INSTANCE.getBOAT_DESPAWN_TIMER());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ object Partnership : ModInitializer {

override fun onInitialize() {
// WAKE UP
BlockRegistries; BlockEntityRegistries; ItemRegistries; GroupRegistries; MenuRegistries; RecipeRegistries; EntityRegistries; NetworkRegistries
BlockRegistries; BlockEntityRegistries; ItemRegistries; GroupRegistries; MenuRegistries; RecipeRegistries; EntityRegistries; NetworkRegistries; GameRuleRegistries

Compat.check()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package cc.tweaked_programs.partnership.main.registries

import net.fabricmc.fabric.api.gamerule.v1.GameRuleFactory
import net.fabricmc.fabric.api.gamerule.v1.GameRuleRegistry
import net.minecraft.world.level.GameRules

object GameRuleRegistries {
val BOAT_DESPAWN_TIMER = GameRuleRegistry.register("boatDespawnTimer", GameRules.Category.MISC, GameRuleFactory.createIntRule(-1))
}
1 change: 1 addition & 0 deletions src/main/resources/partnership.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"package": "cc.tweaked_programs.mixin",
"compatibilityLevel": "JAVA_17",
"mixins": [
"BoatDespawnMixin",
"BoatMixin",
"EnchantmentHelperMixin",
"PlayerMixin"
Expand Down

0 comments on commit f8b1614

Please sign in to comment.