[Feature] Support for Custom Boats (quiqueck/BetterNether#4)
This commit is contained in:
parent
696e0d5def
commit
a18aa400ed
10 changed files with 483 additions and 1 deletions
|
@ -0,0 +1,42 @@
|
|||
package org.betterx.bclib.mixin.client.boat;
|
||||
|
||||
|
||||
import org.betterx.bclib.items.boat.BoatTypeOverride;
|
||||
|
||||
import com.mojang.blaze3d.vertex.PoseStack;
|
||||
import net.minecraft.client.renderer.MultiBufferSource;
|
||||
import net.minecraft.client.renderer.entity.BoatRenderer;
|
||||
import net.minecraft.client.renderer.entity.EntityRenderer;
|
||||
import net.minecraft.client.renderer.entity.EntityRendererProvider;
|
||||
import net.minecraft.world.entity.vehicle.Boat;
|
||||
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
|
||||
@Mixin(BoatRenderer.class)
|
||||
public abstract class BoatRendererMixin extends EntityRenderer<Boat> {
|
||||
protected BoatRendererMixin(EntityRendererProvider.Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
@Inject(method = "<init>", at = @At("TAIL"))
|
||||
private void bcl_init(EntityRendererProvider.Context context, boolean bl, CallbackInfo ci) {
|
||||
BoatTypeOverride.values().forEach(type -> type.createBoatModels(context));
|
||||
}
|
||||
|
||||
@Inject(method = "render(Lnet/minecraft/world/entity/vehicle/Boat;FFLcom/mojang/blaze3d/vertex/PoseStack;Lnet/minecraft/client/renderer/MultiBufferSource;I)V", at = @At("HEAD"), cancellable = true)
|
||||
void bcl_render(
|
||||
Boat boat,
|
||||
float f, float g,
|
||||
PoseStack poseStack, MultiBufferSource multiBufferSource,
|
||||
int i,
|
||||
CallbackInfo ci
|
||||
) {
|
||||
if (org.betterx.bclib.client.render.BoatRenderer.render(boat, f, g, poseStack, multiBufferSource, i)) {
|
||||
super.render(boat, f, g, poseStack, multiBufferSource, i);
|
||||
ci.cancel();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package org.betterx.bclib.mixin.common.boat;
|
||||
|
||||
import org.betterx.bclib.items.boat.CustomBoatTypeOverride;
|
||||
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.item.BoatItem;
|
||||
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.ModifyArg;
|
||||
|
||||
@Mixin(BoatItem.class)
|
||||
public class BoatItemMixin {
|
||||
@ModifyArg(method = "use", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/Level;noCollision(Lnet/minecraft/world/entity/Entity;Lnet/minecraft/world/phys/AABB;)Z"))
|
||||
Entity bcl_suse(Entity boat) {
|
||||
if (this instanceof CustomBoatTypeOverride self) {
|
||||
if (boat instanceof CustomBoatTypeOverride newBoat) {
|
||||
newBoat.setCustomType(self.bcl_getCustomType());
|
||||
}
|
||||
}
|
||||
return boat;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,99 @@
|
|||
package org.betterx.bclib.mixin.common.boat;
|
||||
|
||||
import org.betterx.bclib.items.boat.BoatTypeOverride;
|
||||
import org.betterx.bclib.items.boat.CustomBoatTypeOverride;
|
||||
|
||||
import net.minecraft.core.BlockPos;
|
||||
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.Entity;
|
||||
import net.minecraft.world.entity.EntityType;
|
||||
import net.minecraft.world.entity.vehicle.Boat;
|
||||
import net.minecraft.world.item.BoatItem;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.item.Items;
|
||||
import net.minecraft.world.level.GameRules;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
||||
|
||||
@Mixin(Boat.class)
|
||||
public abstract class BoatMixin extends Entity implements CustomBoatTypeOverride {
|
||||
private static final EntityDataAccessor<Integer> DATA_CUSTOM_ID_TYPE = SynchedEntityData.defineId(
|
||||
Boat.class,
|
||||
EntityDataSerializers.INT
|
||||
);
|
||||
|
||||
public BoatMixin(EntityType<?> entityType, Level level) {
|
||||
super(entityType, level);
|
||||
}
|
||||
|
||||
public void setCustomType(BoatTypeOverride type) {
|
||||
this.entityData.set(DATA_CUSTOM_ID_TYPE, type != null ? type.ordinal() : -1);
|
||||
}
|
||||
|
||||
public BoatTypeOverride bcl_getCustomType() {
|
||||
return BoatTypeOverride.byId(this.entityData.get(DATA_CUSTOM_ID_TYPE));
|
||||
}
|
||||
|
||||
@Inject(method = "defineSynchedData", at = @At("TAIL"))
|
||||
void bcl_adddefineSynchedData(CallbackInfo ci) {
|
||||
this.entityData.define(DATA_CUSTOM_ID_TYPE, -1);
|
||||
}
|
||||
|
||||
@Inject(method = "addAdditionalSaveData", at = @At("HEAD"))
|
||||
void bcl_addAdditionalSaveData(CompoundTag compoundTag, CallbackInfo ci) {
|
||||
BoatTypeOverride type = this.bcl_getCustomType();
|
||||
if (type != null) {
|
||||
compoundTag.putString("cType", type.name());
|
||||
}
|
||||
}
|
||||
|
||||
@Inject(method = "readAdditionalSaveData", at = @At("HEAD"))
|
||||
void bcl_readAdditionalSaveData(CompoundTag compoundTag, CallbackInfo ci) {
|
||||
if (compoundTag.contains("cType")) {
|
||||
this.setCustomType(BoatTypeOverride.byName(compoundTag.getString("cType")));
|
||||
} else {
|
||||
this.setCustomType(null);
|
||||
}
|
||||
}
|
||||
|
||||
@Inject(method = "getDropItem", at = @At("HEAD"), cancellable = true)
|
||||
void bcl_getDropItem(CallbackInfoReturnable<Item> cir) {
|
||||
BoatTypeOverride type = this.bcl_getCustomType();
|
||||
if (type != null) {
|
||||
BoatItem boat = type.getBoatItem();
|
||||
if (boat != null) {
|
||||
cir.setReturnValue(boat);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Inject(method = "checkFallDamage", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/vehicle/Boat;kill()V", shift = At.Shift.AFTER), cancellable = true)
|
||||
void bcl_checkFallDamage(double d, boolean bl, BlockState blockState, BlockPos blockPos, CallbackInfo ci) {
|
||||
BoatTypeOverride type = this.bcl_getCustomType();
|
||||
if (type != null) {
|
||||
if (this.level.getGameRules().getBoolean(GameRules.RULE_DOENTITYDROPS)) {
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
this.spawnAtLocation(type.getPlanks());
|
||||
}
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
this.spawnAtLocation(Items.STICK);
|
||||
}
|
||||
|
||||
this.resetFallDistance();
|
||||
ci.cancel();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package org.betterx.bclib.mixin.common.boat;
|
||||
|
||||
import org.betterx.bclib.items.boat.BoatTypeOverride;
|
||||
import org.betterx.bclib.items.boat.CustomBoatTypeOverride;
|
||||
|
||||
import net.minecraft.world.InteractionHand;
|
||||
import net.minecraft.world.InteractionResult;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.entity.vehicle.ChestBoat;
|
||||
import net.minecraft.world.item.BoatItem;
|
||||
import net.minecraft.world.item.Item;
|
||||
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.Shadow;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
||||
|
||||
@Mixin(ChestBoat.class)
|
||||
public abstract class ChestBoatMixin {
|
||||
@Shadow
|
||||
public abstract InteractionResult interact(Player player, InteractionHand interactionHand);
|
||||
|
||||
@Inject(method = "getDropItem", at = @At("HEAD"), cancellable = true)
|
||||
void bcl_getDropItem(CallbackInfoReturnable<Item> cir) {
|
||||
if (this instanceof CustomBoatTypeOverride cbto) {
|
||||
BoatTypeOverride type = cbto.bcl_getCustomType();
|
||||
if (type != null) {
|
||||
BoatItem boat = type.getChestBoatItem();
|
||||
if (boat != null) {
|
||||
cir.setReturnValue(boat);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue