重写枪械数据读取
This commit is contained in:
parent
b398fa1b23
commit
0d5a4d7685
4 changed files with 122 additions and 79 deletions
|
@ -0,0 +1,67 @@
|
||||||
|
package com.atsuishio.superbwarfare.item.gun.data;
|
||||||
|
|
||||||
|
import com.google.gson.annotations.SerializedName;
|
||||||
|
|
||||||
|
public class DefaultGunData {
|
||||||
|
|
||||||
|
@SerializedName("RecoilX")
|
||||||
|
public double recoilX;
|
||||||
|
@SerializedName("RecoilY")
|
||||||
|
public double recoilY;
|
||||||
|
|
||||||
|
@SerializedName("DefaultZoom")
|
||||||
|
public double defaultZoom = 1.25;
|
||||||
|
@SerializedName("MinZoom")
|
||||||
|
public double minZoom = defaultZoom;
|
||||||
|
@SerializedName("MaxZoom")
|
||||||
|
public double maxZoom = defaultZoom;
|
||||||
|
|
||||||
|
@SerializedName("Spread")
|
||||||
|
public double spread;
|
||||||
|
@SerializedName("Damage")
|
||||||
|
public double damage;
|
||||||
|
@SerializedName("Headshot")
|
||||||
|
public double headshot = 1.5;
|
||||||
|
@SerializedName("Velocity")
|
||||||
|
public double velocity;
|
||||||
|
@SerializedName("Magazine")
|
||||||
|
public int magazine;
|
||||||
|
@SerializedName("ProjectileAmount")
|
||||||
|
public int projectileAmount = 1;
|
||||||
|
@SerializedName("Weight")
|
||||||
|
public double weight;
|
||||||
|
@SerializedName("FireMode")
|
||||||
|
public int fireMode;
|
||||||
|
@SerializedName("BurstAmount")
|
||||||
|
public int burstAmount;
|
||||||
|
@SerializedName("BypassArmor")
|
||||||
|
public double bypassArmor;
|
||||||
|
|
||||||
|
@SerializedName("NormalReloadTime")
|
||||||
|
public int normalReloadTime;
|
||||||
|
@SerializedName("EmptyReloadTime")
|
||||||
|
public int emptyReloadTime;
|
||||||
|
@SerializedName("BoltActionTime")
|
||||||
|
public int boltActionTime;
|
||||||
|
@SerializedName("PrepareTime")
|
||||||
|
public int prepareTime;
|
||||||
|
@SerializedName("PrepareLoadTime")
|
||||||
|
public int prepareLoadTime;
|
||||||
|
@SerializedName("PrepareEmptyTime")
|
||||||
|
public int prepareEmptyTime;
|
||||||
|
@SerializedName("IterativeTime")
|
||||||
|
public int iterativeTime;
|
||||||
|
@SerializedName("FinishTime")
|
||||||
|
public int finishTime;
|
||||||
|
|
||||||
|
@SerializedName("SoundRadius")
|
||||||
|
public double soundRadius;
|
||||||
|
@SerializedName("RPM")
|
||||||
|
public int rpm = 600;
|
||||||
|
|
||||||
|
@SerializedName("ExplosionDamage")
|
||||||
|
public double explosionDamage;
|
||||||
|
@SerializedName("ExplosionRadius")
|
||||||
|
public double explosionRadius;
|
||||||
|
|
||||||
|
}
|
|
@ -12,7 +12,6 @@ import net.minecraft.util.Mth;
|
||||||
import net.minecraft.world.item.ItemStack;
|
import net.minecraft.world.item.ItemStack;
|
||||||
import net.minecraft.world.item.component.CustomData;
|
import net.minecraft.world.item.component.CustomData;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.WeakHashMap;
|
import java.util.WeakHashMap;
|
||||||
|
|
||||||
|
@ -51,7 +50,7 @@ public class GunData {
|
||||||
perk = new Perks(this);
|
perk = new Perks(this);
|
||||||
|
|
||||||
ammo = new IntValue(data, "Ammo");
|
ammo = new IntValue(data, "Ammo");
|
||||||
fireMode = new IntValue(data, "FireMode", (int) getGunData("FireMode"));
|
fireMode = new IntValue(data, "FireMode", defaultGunData().fireMode);
|
||||||
level = new IntValue(data, "Level");
|
level = new IntValue(data, "Level");
|
||||||
exp = new DoubleValue(data, "Exp");
|
exp = new DoubleValue(data, "Exp");
|
||||||
upgradePoint = new DoubleValue(data, "UpgradePoint");
|
upgradePoint = new DoubleValue(data, "UpgradePoint");
|
||||||
|
@ -124,19 +123,15 @@ public class GunData {
|
||||||
return attachmentTag;
|
return attachmentTag;
|
||||||
}
|
}
|
||||||
|
|
||||||
double getGunData(String key) {
|
DefaultGunData defaultGunData() {
|
||||||
return getGunData(key, 0);
|
return GunsTool.gunsData.getOrDefault(id, new DefaultGunData());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// 枪械本体属性开始
|
// 枪械本体属性开始
|
||||||
|
|
||||||
private double getGunData(String key, double defaultValue) {
|
|
||||||
return GunsTool.gunsData.getOrDefault(id, new HashMap<>()).getOrDefault(key, defaultValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
public double rawDamage() {
|
public double rawDamage() {
|
||||||
return getGunData("Damage");
|
return defaultGunData().damage;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double perkDamageRate() {
|
public double perkDamageRate() {
|
||||||
|
@ -152,83 +147,83 @@ public class GunData {
|
||||||
}
|
}
|
||||||
|
|
||||||
public double explosionDamage() {
|
public double explosionDamage() {
|
||||||
return getGunData("ExplosionDamage");
|
return defaultGunData().explosionDamage;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double explosionRadius() {
|
public double explosionRadius() {
|
||||||
return getGunData("ExplosionRadius");
|
return defaultGunData().explosionRadius;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double velocity() {
|
public double velocity() {
|
||||||
return getGunData("Velocity") + item.getCustomVelocity(stack);
|
return defaultGunData().velocity + item.getCustomVelocity(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
public double spread() {
|
public double spread() {
|
||||||
return getGunData("Spread");
|
return defaultGunData().spread;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int magazine() {
|
public int magazine() {
|
||||||
return (int) (getGunData("Magazine") + item.getCustomMagazine(stack));
|
return defaultGunData().magazine + item.getCustomMagazine(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int projectileAmount() {
|
public int projectileAmount() {
|
||||||
return (int) getGunData("ProjectileAmount", 1);
|
return defaultGunData().projectileAmount;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double headshot() {
|
public double headshot() {
|
||||||
return getGunData("Headshot", 1.5) + item.getCustomHeadshot(stack);
|
return defaultGunData().headshot + item.getCustomHeadshot(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int defaultNormalReloadTime() {
|
public int defaultNormalReloadTime() {
|
||||||
return (int) getGunData("NormalReloadTime");
|
return defaultGunData().normalReloadTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int defaultEmptyReloadTime() {
|
public int defaultEmptyReloadTime() {
|
||||||
return (int) getGunData("EmptyReloadTime");
|
return defaultGunData().emptyReloadTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int defaultIterativeTime() {
|
public int defaultIterativeTime() {
|
||||||
return (int) getGunData("IterativeTime");
|
return defaultGunData().iterativeTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int defaultPrepareTime() {
|
public int defaultPrepareTime() {
|
||||||
return (int) getGunData("PrepareTime");
|
return defaultGunData().prepareTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int defaultPrepareLoadTime() {
|
public int defaultPrepareLoadTime() {
|
||||||
return (int) getGunData("PrepareLoadTime");
|
return defaultGunData().prepareLoadTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int defaultPrepareEmptyTime() {
|
public int defaultPrepareEmptyTime() {
|
||||||
return (int) getGunData("PrepareEmptyTime");
|
return defaultGunData().prepareEmptyTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int defaultFinishTime() {
|
public int defaultFinishTime() {
|
||||||
return (int) getGunData("FinishTime");
|
return defaultGunData().finishTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int defaultActionTime() {
|
public int defaultActionTime() {
|
||||||
return (int) getGunData("BoltActionTime") + item.getCustomBoltActionTime(stack());
|
return defaultGunData().boltActionTime + item.getCustomBoltActionTime(stack());
|
||||||
}
|
}
|
||||||
|
|
||||||
public double soundRadius() {
|
public double soundRadius() {
|
||||||
return getGunData("SoundRadius", 15) + item.getCustomSoundRadius(stack);
|
return defaultGunData().soundRadius + item.getCustomSoundRadius(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
public double bypassArmor() {
|
public double bypassArmor() {
|
||||||
return getGunData("BypassesArmor") + item.getCustomBypassArmor(stack);
|
return defaultGunData().bypassArmor + item.getCustomBypassArmor(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
public double recoilX() {
|
public double recoilX() {
|
||||||
return getGunData("RecoilX");
|
return defaultGunData().recoilX;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double recoilY() {
|
public double recoilY() {
|
||||||
return getGunData("RecoilY");
|
return defaultGunData().recoilY;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double weight() {
|
public double weight() {
|
||||||
return getGunData("Weight") + customWeight();
|
return defaultGunData().weight + customWeight();
|
||||||
}
|
}
|
||||||
|
|
||||||
public double customWeight() {
|
public double customWeight() {
|
||||||
|
@ -237,17 +232,17 @@ public class GunData {
|
||||||
|
|
||||||
|
|
||||||
public double defaultZoom() {
|
public double defaultZoom() {
|
||||||
return getGunData("DefaultZoom", 1.25);
|
return defaultGunData().defaultZoom;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double minZoom() {
|
public double minZoom() {
|
||||||
int scopeType = this.attachment.get(AttachmentType.SCOPE);
|
int scopeType = this.attachment.get(AttachmentType.SCOPE);
|
||||||
return scopeType == 3 ? getGunData("MinZoom", 1.25) : 1.25;
|
return scopeType == 3 ? defaultGunData().minZoom : 1.25;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double maxZoom() {
|
public double maxZoom() {
|
||||||
int scopeType = this.attachment.get(AttachmentType.SCOPE);
|
int scopeType = this.attachment.get(AttachmentType.SCOPE);
|
||||||
return scopeType == 3 ? getGunData("MaxZoom", 1) : 114514;
|
return scopeType == 3 ? defaultGunData().maxZoom : 114514;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double zoom() {
|
public double zoom() {
|
||||||
|
@ -257,11 +252,11 @@ public class GunData {
|
||||||
}
|
}
|
||||||
|
|
||||||
public int rpm() {
|
public int rpm() {
|
||||||
return (int) (getGunData("RPM") + item.getCustomRPM(stack));
|
return (defaultGunData().rpm + item.getCustomRPM(stack));
|
||||||
}
|
}
|
||||||
|
|
||||||
public int burstAmount() {
|
public int burstAmount() {
|
||||||
return (int) getGunData("BurstAmount");
|
return defaultGunData().burstAmount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
package com.atsuishio.superbwarfare.network.message.receive;
|
package com.atsuishio.superbwarfare.network.message.receive;
|
||||||
|
|
||||||
import com.atsuishio.superbwarfare.Mod;
|
import com.atsuishio.superbwarfare.Mod;
|
||||||
|
import com.atsuishio.superbwarfare.item.gun.data.DefaultGunData;
|
||||||
import com.atsuishio.superbwarfare.tools.GunsTool;
|
import com.atsuishio.superbwarfare.tools.GunsTool;
|
||||||
|
import com.google.gson.Gson;
|
||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
import net.minecraft.network.codec.ByteBufCodecs;
|
import net.minecraft.network.codec.ByteBufCodecs;
|
||||||
import net.minecraft.network.codec.StreamCodec;
|
import net.minecraft.network.codec.StreamCodec;
|
||||||
|
@ -10,8 +12,9 @@ import net.neoforged.neoforge.network.handling.IPayloadContext;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public record GunsDataMessage(HashMap<String, HashMap<String, Double>> gunsData) implements CustomPacketPayload {
|
public record GunsDataMessage(Map<String, String> gunsData) implements CustomPacketPayload {
|
||||||
public static final Type<GunsDataMessage> TYPE = new Type<>(Mod.loc("set_guns_data"));
|
public static final Type<GunsDataMessage> TYPE = new Type<>(Mod.loc("set_guns_data"));
|
||||||
|
|
||||||
|
|
||||||
|
@ -19,19 +22,27 @@ public record GunsDataMessage(HashMap<String, HashMap<String, Double>> gunsData)
|
||||||
ByteBufCodecs.map(
|
ByteBufCodecs.map(
|
||||||
HashMap::new,
|
HashMap::new,
|
||||||
ByteBufCodecs.STRING_UTF8,
|
ByteBufCodecs.STRING_UTF8,
|
||||||
ByteBufCodecs.map(
|
ByteBufCodecs.STRING_UTF8
|
||||||
HashMap::new,
|
|
||||||
ByteBufCodecs.STRING_UTF8,
|
|
||||||
ByteBufCodecs.DOUBLE
|
|
||||||
)
|
|
||||||
),
|
),
|
||||||
GunsDataMessage::gunsData,
|
GunsDataMessage::gunsData,
|
||||||
GunsDataMessage::new
|
GunsDataMessage::new
|
||||||
);
|
);
|
||||||
|
|
||||||
|
private static final Gson GSON = new Gson();
|
||||||
|
|
||||||
|
public static GunsDataMessage create() {
|
||||||
|
var map = new HashMap<String, String>();
|
||||||
|
for (var entry : GunsTool.gunsData.entrySet()) {
|
||||||
|
map.put(entry.getKey(), GSON.toJson(entry.getValue()));
|
||||||
|
}
|
||||||
|
return new GunsDataMessage(map);
|
||||||
|
}
|
||||||
|
|
||||||
public static void handler(final GunsDataMessage message, final IPayloadContext context) {
|
public static void handler(final GunsDataMessage message, final IPayloadContext context) {
|
||||||
GunsTool.gunsData = message.gunsData;
|
GunsTool.gunsData.clear();
|
||||||
|
for (var entry : message.gunsData.entrySet()) {
|
||||||
|
GunsTool.gunsData.put(entry.getKey(), GSON.fromJson(entry.getValue(), DefaultGunData.class));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -3,10 +3,11 @@ package com.atsuishio.superbwarfare.tools;
|
||||||
import com.atsuishio.superbwarfare.Mod;
|
import com.atsuishio.superbwarfare.Mod;
|
||||||
import com.atsuishio.superbwarfare.init.ModAttachments;
|
import com.atsuishio.superbwarfare.init.ModAttachments;
|
||||||
import com.atsuishio.superbwarfare.init.ModTags;
|
import com.atsuishio.superbwarfare.init.ModTags;
|
||||||
|
import com.atsuishio.superbwarfare.item.gun.data.DefaultGunData;
|
||||||
import com.atsuishio.superbwarfare.item.gun.data.GunData;
|
import com.atsuishio.superbwarfare.item.gun.data.GunData;
|
||||||
import com.atsuishio.superbwarfare.item.gun.data.value.ReloadState;
|
import com.atsuishio.superbwarfare.item.gun.data.value.ReloadState;
|
||||||
import com.atsuishio.superbwarfare.network.message.receive.GunsDataMessage;
|
import com.atsuishio.superbwarfare.network.message.receive.GunsDataMessage;
|
||||||
import com.google.gson.stream.JsonReader;
|
import com.google.gson.Gson;
|
||||||
import net.minecraft.nbt.CompoundTag;
|
import net.minecraft.nbt.CompoundTag;
|
||||||
import net.minecraft.server.level.ServerPlayer;
|
import net.minecraft.server.level.ServerPlayer;
|
||||||
import net.minecraft.server.packs.resources.ResourceManager;
|
import net.minecraft.server.packs.resources.ResourceManager;
|
||||||
|
@ -27,7 +28,7 @@ import java.util.UUID;
|
||||||
@EventBusSubscriber(modid = Mod.MODID)
|
@EventBusSubscriber(modid = Mod.MODID)
|
||||||
public class GunsTool {
|
public class GunsTool {
|
||||||
|
|
||||||
public static HashMap<String, HashMap<String, Double>> gunsData = new HashMap<>();
|
public static HashMap<String, DefaultGunData> gunsData = new HashMap<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 初始化数据,从data中读取数据json文件
|
* 初始化数据,从data中读取数据json文件
|
||||||
|
@ -37,18 +38,11 @@ public class GunsTool {
|
||||||
var id = entry.getKey();
|
var id = entry.getKey();
|
||||||
var attribute = entry.getValue();
|
var attribute = entry.getValue();
|
||||||
try {
|
try {
|
||||||
JsonReader reader = new JsonReader(new InputStreamReader(attribute.open()));
|
Gson gson = new Gson();
|
||||||
|
var data = gson.fromJson(new InputStreamReader(attribute.open()), DefaultGunData.class);
|
||||||
reader.beginObject();
|
|
||||||
var map = new HashMap<String, Double>();
|
|
||||||
while (reader.hasNext()) {
|
|
||||||
map.put(reader.nextName(), reader.nextDouble());
|
|
||||||
}
|
|
||||||
var path = id.getPath();
|
var path = id.getPath();
|
||||||
gunsData.put(path.substring(5, path.length() - 5), map);
|
|
||||||
|
|
||||||
reader.endObject();
|
gunsData.put(path.substring(5, path.length() - 5), data);
|
||||||
reader.close();
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Mod.LOGGER.error(e.getMessage());
|
Mod.LOGGER.error(e.getMessage());
|
||||||
}
|
}
|
||||||
|
@ -58,7 +52,7 @@ public class GunsTool {
|
||||||
@SubscribeEvent
|
@SubscribeEvent
|
||||||
public static void onPlayerLogin(PlayerEvent.PlayerLoggedInEvent event) {
|
public static void onPlayerLogin(PlayerEvent.PlayerLoggedInEvent event) {
|
||||||
if (event.getEntity() instanceof ServerPlayer player) {
|
if (event.getEntity() instanceof ServerPlayer player) {
|
||||||
PacketDistributor.sendToPlayer(player, new GunsDataMessage(GunsTool.gunsData));
|
PacketDistributor.sendToPlayer(player, GunsDataMessage.create());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,7 +65,7 @@ public class GunsTool {
|
||||||
public static void datapackSync(OnDatapackSyncEvent event) {
|
public static void datapackSync(OnDatapackSyncEvent event) {
|
||||||
initJsonData(event.getPlayerList().getServer().getResourceManager());
|
initJsonData(event.getPlayerList().getServer().getResourceManager());
|
||||||
|
|
||||||
event.getRelevantPlayers().forEach(player -> PacketDistributor.sendToPlayer(player, new GunsDataMessage(GunsTool.gunsData)));
|
event.getRelevantPlayers().forEach(player -> PacketDistributor.sendToPlayer(player, GunsDataMessage.create()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void reload(Player player, ItemStack stack, GunData gunData, AmmoType type) {
|
public static void reload(Player player, ItemStack stack, GunData gunData, AmmoType type) {
|
||||||
|
@ -129,11 +123,6 @@ public class GunsTool {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* GunData */
|
|
||||||
public static CompoundTag getGunData(final CompoundTag tag) {
|
|
||||||
return tag.getCompound("GunData");
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void setGunIntTag(final CompoundTag tag, String name, int num) {
|
public static void setGunIntTag(final CompoundTag tag, String name, int num) {
|
||||||
var data = tag.getCompound("GunData");
|
var data = tag.getCompound("GunData");
|
||||||
data.putInt(name, num);
|
data.putInt(name, num);
|
||||||
|
@ -150,12 +139,6 @@ public class GunsTool {
|
||||||
return data.getInt(name);
|
return data.getInt(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setGunDoubleTag(final CompoundTag tag, String name, double num) {
|
|
||||||
var data = tag.getCompound("GunData");
|
|
||||||
data.putDouble(name, num);
|
|
||||||
tag.put("GunData", data);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static double getGunDoubleTag(final CompoundTag tag, String name) {
|
public static double getGunDoubleTag(final CompoundTag tag, String name) {
|
||||||
return getGunDoubleTag(tag, name, 0);
|
return getGunDoubleTag(tag, name, 0);
|
||||||
}
|
}
|
||||||
|
@ -165,19 +148,6 @@ public class GunsTool {
|
||||||
if (!data.contains(name)) return defaultValue;
|
if (!data.contains(name)) return defaultValue;
|
||||||
return data.getDouble(name);
|
return data.getDouble(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setGunBooleanTag(final CompoundTag tag, String name, boolean flag) {
|
|
||||||
var data = tag.getCompound("GunData");
|
|
||||||
data.putBoolean(name, flag);
|
|
||||||
tag.put("GunData", data);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean getGunBooleanTag(final CompoundTag tag, String name) {
|
|
||||||
var data = tag.getCompound("GunData");
|
|
||||||
if (!data.contains(name)) return false;
|
|
||||||
return data.getBoolean(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public static UUID getGunUUID(final CompoundTag tag) {
|
public static UUID getGunUUID(final CompoundTag tag) {
|
||||||
if (!tag.contains("GunData")) return null;
|
if (!tag.contains("GunData")) return null;
|
||||||
|
|
Loading…
Add table
Reference in a new issue