允许自定义载具修理材料和恢复量

This commit is contained in:
Light_Quanta 2025-05-25 23:11:24 +08:00
parent 146d11a28d
commit 7a73933c27
No known key found for this signature in database
GPG key ID: 11A39A1B8C890959
3 changed files with 53 additions and 3 deletions

View file

@ -22,6 +22,14 @@ public class DefaultVehicleData {
@SerializedName("RepairAmount") @SerializedName("RepairAmount")
public float repairAmount = VehicleConfig.REPAIR_AMOUNT.get().floatValue(); public float repairAmount = VehicleConfig.REPAIR_AMOUNT.get().floatValue();
@ServerOnly
@SerializedName("RepairMaterial")
public String repairMaterial = "minecraft:iron_ingot";
@ServerOnly
@SerializedName("RepairMaterialHealAmount")
public float repairMaterialHealAmount = 50;
@SerializedName("MaxEnergy") @SerializedName("MaxEnergy")
public int maxEnergy = 100000; public int maxEnergy = 100000;

View file

@ -6,8 +6,12 @@ import com.atsuishio.superbwarfare.init.ModDamageTypes;
import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader; import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache; import com.google.common.cache.LoadingCache;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.ItemTags;
import net.minecraft.world.damagesource.DamageTypes; import net.minecraft.world.damagesource.DamageTypes;
import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.EntityType;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
public class VehicleData { public class VehicleData {
@ -46,6 +50,41 @@ public class VehicleData {
return data.repairAmount; return data.repairAmount;
} }
public String repairMaterial() {
return data.repairMaterial;
}
public float repairMaterialHealAmount() {
return data.repairMaterialHealAmount;
}
public boolean canRepairManually() {
var material = repairMaterial();
if (material == null) return false;
if (material.startsWith("#")) {
material = material.substring(1);
}
return ResourceLocation.tryParse(material) != null;
}
public boolean isRepairMaterial(ItemStack stack) {
var material = repairMaterial();
var useTag = false;
if (material.startsWith("#")) {
material = material.substring(1);
useTag = true;
}
var location = ResourceLocation.parse(material);
if (!useTag) {
return stack.getItem() == BuiltInRegistries.ITEM.get(location);
} else {
return stack.is(ItemTags.create(location));
}
}
public int maxEnergy() { public int maxEnergy() {
return data.maxEnergy; return data.maxEnergy;
} }

View file

@ -50,7 +50,6 @@ import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.projectile.AbstractArrow; import net.minecraft.world.entity.projectile.AbstractArrow;
import net.minecraft.world.entity.vehicle.DismountHelper; import net.minecraft.world.entity.vehicle.DismountHelper;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.minecraft.world.level.gameevent.GameEvent; import net.minecraft.world.level.gameevent.GameEvent;
import net.minecraft.world.level.material.Fluid; import net.minecraft.world.level.material.Fluid;
@ -334,6 +333,7 @@ public abstract class VehicleEntity extends Entity {
@Override @Override
public @NotNull InteractionResult interact(Player player, @NotNull InteractionHand hand) { public @NotNull InteractionResult interact(Player player, @NotNull InteractionHand hand) {
if (player.getVehicle() == this) return InteractionResult.PASS; if (player.getVehicle() == this) return InteractionResult.PASS;
var data = data();
ItemStack stack = player.getMainHandItem(); ItemStack stack = player.getMainHandItem();
if (player.isShiftKeyDown() && stack.is(ModItems.CROWBAR.get()) && this.getPassengers().isEmpty()) { if (player.isShiftKeyDown() && stack.is(ModItems.CROWBAR.get()) && this.getPassengers().isEmpty()) {
@ -344,8 +344,11 @@ public abstract class VehicleEntity extends Entity {
this.remove(RemovalReason.DISCARDED); this.remove(RemovalReason.DISCARDED);
this.discard(); this.discard();
return InteractionResult.SUCCESS; return InteractionResult.SUCCESS;
} else if (this.getHealth() < this.getMaxHealth() && stack.is(Items.IRON_INGOT)) { } else if (this.getHealth() < this.getMaxHealth()
this.heal(Math.min(50, this.getMaxHealth())); && data.canRepairManually()
&& data.isRepairMaterial(stack)
) {
this.heal(Math.min(data.repairMaterialHealAmount(), this.getMaxHealth()));
stack.shrink(1); stack.shrink(1);
if (!this.level().isClientSide) { if (!this.level().isClientSide) {
this.level().playSound(null, this, SoundEvents.IRON_GOLEM_REPAIR, this.getSoundSource(), 0.5f, 1); this.level().playSound(null, this, SoundEvents.IRON_GOLEM_REPAIR, this.getSoundSource(), 0.5f, 1);