优化重量相关代码

This commit is contained in:
17146 2024-10-21 02:09:31 +08:00
parent 28ed1fa12d
commit e436675cea
4 changed files with 43 additions and 65 deletions

View file

@ -9,36 +9,19 @@ public class ItemModelHelper {
public static void handleGunAttachments(GeoBone bone, ItemStack stack, String name) {
CompoundTag tag = stack.getOrCreateTag().getCompound("Attachments");
splitBoneName(bone, name, "Scope", tag);
splitBoneName(bone, name, "Magazine", tag);
splitBoneName(bone, name, "Barrel", tag);
splitBoneName(bone, name, "Stock", tag);
}
private static void splitBoneName(GeoBone bone, String boneName, String tagName, CompoundTag tag) {
try {
if (name.startsWith("Scope")) {
String[] parts = name.split("(?<=\\D)(?=\\d)");
if (boneName.startsWith(tagName)) {
String[] parts = boneName.split("(?<=\\D)(?=\\d)");
if (parts.length == 2) {
int index = Integer.parseInt(parts[1]);
bone.setHidden(tag.getInt("Scope") != index);
}
}
if (name.startsWith("Magazine")) {
String[] parts = name.split("(?<=\\D)(?=\\d)");
if (parts.length == 2) {
int index = Integer.parseInt(parts[1]);
bone.setHidden(tag.getInt("Magazine") != index);
}
}
if (name.startsWith("Barrel")) {
String[] parts = name.split("(?<=\\D)(?=\\d)");
if (parts.length == 2) {
int index = Integer.parseInt(parts[1]);
bone.setHidden(tag.getInt("Barrel") != index);
}
}
if (name.startsWith("Stock")) {
String[] parts = name.split("(?<=\\D)(?=\\d)");
if (parts.length == 2) {
int index = Integer.parseInt(parts[1]);
bone.setHidden(tag.getInt("Stock") != index);
bone.setHidden(tag.getInt(tagName) != index);
}
}
} catch (NumberFormatException ignored) {

View file

@ -192,7 +192,7 @@ public class ClientEventHandler {
}
// 开火部分
double weight = stack.getOrCreateTag().getDouble("weight") + stack.getOrCreateTag().getDouble("custom_weight");
double weight = stack.getOrCreateTag().getDouble("weight") + stack.getOrCreateTag().getDouble("CustomWeight");
double speed = 1 - (0.04 * weight);
@ -483,7 +483,7 @@ public class ClientEventHandler {
ItemStack stack = player.getMainHandItem();
float times = 5 * Minecraft.getInstance().getDeltaFrameTime();
double weight = stack.getOrCreateTag().getDouble("weight") + stack.getOrCreateTag().getDouble("custom_weight");
double weight = stack.getOrCreateTag().getDouble("weight") + stack.getOrCreateTag().getDouble("CustomWeight");
double speed = 1.5 - (0.07 * weight);
if (GLFW.glfwGetMouseButton(Minecraft.getInstance().getWindow().getWindow(), GLFW.GLFW_MOUSE_BUTTON_RIGHT) == GLFW.GLFW_PRESS
@ -850,7 +850,7 @@ public class ClientEventHandler {
private static void handleWeaponDraw(LivingEntity entity) {
float times = Minecraft.getInstance().getDeltaFrameTime();
ItemStack stack = entity.getMainHandItem();
double weight = stack.getOrCreateTag().getDouble("weight") + stack.getOrCreateTag().getDouble("custom_weight");
double weight = stack.getOrCreateTag().getDouble("weight") + stack.getOrCreateTag().getDouble("CustomWeight");
double speed = 3.2 - (0.13 * weight);

View file

@ -131,7 +131,7 @@ public class PlayerEventHandler {
int sprintCost;
if (stack.is(ModTags.Items.GUN)) {
double weight = stack.getOrCreateTag().getDouble("weight") + stack.getOrCreateTag().getDouble("custom_weight");
double weight = stack.getOrCreateTag().getDouble("weight") + stack.getOrCreateTag().getDouble("CustomWeight");
sprintCost = (int) (2 + 0.2 * weight);
} else {
sprintCost = 2;

View file

@ -13,6 +13,7 @@ import net.mcreator.superbwarfare.tools.GunsTool;
import net.mcreator.superbwarfare.tools.ItemNBTTool;
import net.mcreator.superbwarfare.tools.TooltipTool;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvent;
@ -126,7 +127,9 @@ public abstract class GunItem extends Item {
if (slot == EquipmentSlot.MAINHAND) {
map = HashMultimap.create(map);
map.put(Attributes.MOVEMENT_SPEED,
new AttributeModifier(uuid, ModUtils.ATTRIBUTE_MODIFIER, -0.01f - 0.005f * (stack.getOrCreateTag().getDouble("weight") + stack.getOrCreateTag().getDouble("custom_weight")), AttributeModifier.Operation.MULTIPLY_BASE));
new AttributeModifier(uuid, ModUtils.ATTRIBUTE_MODIFIER,
-0.01f - 0.005f * (stack.getOrCreateTag().getDouble("weight") + stack.getOrCreateTag().getDouble("CustomWeight")),
AttributeModifier.Operation.MULTIPLY_BASE));
}
return map;
}
@ -197,45 +200,37 @@ public abstract class GunItem extends Item {
}
private void handleGunAttachment(ItemStack stack) {
CompoundTag tag = stack.getOrCreateTag().getCompound("Attachments");
int scopeType = stack.getOrCreateTag().getInt("scope_type");
int barrelType = stack.getOrCreateTag().getInt("barrel_type");
int magType = stack.getOrCreateTag().getInt("magazine_type");
int stockType = stack.getOrCreateTag().getInt("stock_type");
double scopeWeight = switch (tag.getInt("Scope")) {
case 1 -> 0.5;
case 2 -> 1;
case 3 -> 2;
default -> 0;
};
double ScopeWeight = 0;
double BarrelWeight = 0;
double MagWeight = 0;
double StockWeight = 0;
double barrelWeight = switch (tag.getInt("Barrel")) {
case 1 -> 1;
case 2 -> 2;
default -> 0;
};
if (scopeType == 1) {
ScopeWeight = 0.5;
} else if (scopeType == 2) {
ScopeWeight = 1;
} else if (scopeType == 3) {
ScopeWeight = 2;
}
double magazineWeight = switch (tag.getInt("Magazine")) {
case 1 -> 1.5;
case 2 -> 3;
default -> 0;
};
if (barrelType == 1) {
BarrelWeight = 1;
} else if (magType == 2) {
BarrelWeight = 2;
}
double stockWeight = switch (tag.getInt("Stock")) {
case 1 -> -2;
case 2 -> 2;
default -> 0;
};
if (magType == 1) {
MagWeight = 1.5;
} else if (magType == 2) {
MagWeight = 3;
}
double soundRadius = tag.getInt("Barrel") == 2 ? 0.25 : 1;
if (stockType == 1) {
StockWeight = -2;
} else if (stockType == 2) {
ScopeWeight = 2;
}
stack.getOrCreateTag().putDouble("custom_weight", ScopeWeight + BarrelWeight + MagWeight + StockWeight);
stack.getOrCreateTag().putDouble("CustomSoundRadius", barrelType == 2 ? 0.25 : 1);
stack.getOrCreateTag().putDouble("CustomWeight", scopeWeight + barrelWeight + magazineWeight + stockWeight);
stack.getOrCreateTag().putDouble("CustomSoundRadius", soundRadius);
}
public boolean canApplyPerk(Perk perk) {