superb-warfare/src/main/java/net/mcreator/superbwarfare/tools/GunsTool.java

192 lines
7.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package net.mcreator.superbwarfare.tools;
import com.google.gson.stream.JsonReader;
import net.mcreator.superbwarfare.ModUtils;
import net.mcreator.superbwarfare.network.ModVariables;
import net.mcreator.superbwarfare.network.message.GunsDataMessage;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.event.server.ServerStartedEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.network.PacketDistributor;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.UUID;
@Mod.EventBusSubscriber(modid = ModUtils.MODID)
public class GunsTool {
public static HashMap<String, HashMap<String, Double>> gunsData = new HashMap<>();
/**
* 初始化数据从data中读取数据json文件
*/
public static void initJsonData(ResourceManager manager) {
for (var entry : manager.listResources("guns", file -> file.getPath().endsWith(".json")).entrySet()) {
var id = entry.getKey();
var attribute = entry.getValue();
try {
JsonReader reader = new JsonReader(new InputStreamReader(attribute.open()));
reader.beginObject();
var map = new HashMap<String, Double>();
while (reader.hasNext()) {
map.put(reader.nextName(), reader.nextDouble());
}
var path = id.getPath();
gunsData.put(path.substring(5, path.length() - 5), map);
reader.endObject();
reader.close();
} catch (Exception e) {
ModUtils.LOGGER.error(e.getMessage());
}
}
}
public static void initGun(Level level, ItemStack stack, String location) {
if (level.getServer() == null) return;
gunsData.get(location).forEach((k, v) -> stack.getOrCreateTag().putDouble(k, v));
}
public static void initCreativeGun(ItemStack stack, String location) {
if (gunsData != null && gunsData.get(location) != null) {
gunsData.get(location).forEach((k, v) -> stack.getOrCreateTag().putDouble(k, v));
stack.getOrCreateTag().putInt("ammo", stack.getOrCreateTag().getInt("mag") + stack.getOrCreateTag().getInt("customMag"));
}
}
public static void genUUID(ItemStack stack) {
UUID uuid = UUID.randomUUID();
stack.getOrCreateTag().putUUID("gun_uuid", uuid);
}
@SubscribeEvent
public static void onPlayerLogin(PlayerEvent.PlayerLoggedInEvent event) {
if (event.getEntity() instanceof ServerPlayer player) {
ModUtils.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> player), new GunsDataMessage(GunsTool.gunsData));
}
}
@SubscribeEvent
public static void serverStarted(ServerStartedEvent event) {
initJsonData(event.getServer().getResourceManager());
}
public static void reload(Player player, ItemStack stack, GunInfo.Type type) {
reload(player, stack, type, false);
}
public static void reload(Player player, ItemStack stack, GunInfo.Type type, boolean extraOne) {
CompoundTag tag = stack.getOrCreateTag();
int mag = tag.getInt("mag") + tag.getInt("customMag");
int ammo = tag.getInt("ammo");
int ammoToAdd = mag - ammo + (extraOne ? 1 : 0);
// 空仓换弹的栓动武器应该在换单后取消待上膛标记
if (ammo == 0 && tag.getDouble("bolt_action_time") > 0) {
tag.putDouble("need_bolt_action", 0);
}
int playerAmmo = player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).map(c -> switch (type) {
case RIFLE -> c.rifleAmmo;
case HANDGUN -> c.handgunAmmo;
case SHOTGUN -> c.shotgunAmmo;
case SNIPER -> c.sniperAmmo;
}).orElse(0);
player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
var newAmmoCount = Math.max(0, playerAmmo - ammoToAdd);
switch (type) {
case RIFLE -> capability.rifleAmmo = newAmmoCount;
case HANDGUN -> capability.handgunAmmo = newAmmoCount;
case SHOTGUN -> capability.shotgunAmmo = newAmmoCount;
case SNIPER -> capability.sniperAmmo = newAmmoCount;
}
capability.syncPlayerVariables(player);
});
int needToAdd = ammo + Math.min(ammoToAdd, playerAmmo);
tag.putInt("ammo", needToAdd);
tag.putBoolean("is_normal_reloading", false);
tag.putBoolean("is_empty_reloading", false);
}
public static void setPerkIntTag(ItemStack stack, String name, int num) {
CompoundTag tag = stack.getOrCreateTag().getCompound("PerkData");
tag.putInt(name, num);
stack.addTagElement("PerkData", tag);
}
public static int getPerkIntTag(ItemStack stack, String name) {
CompoundTag tag = stack.getOrCreateTag().getCompound("PerkData");
return tag.getInt(name);
}
public static void setPerkDoubleTag(ItemStack stack, String name, double num) {
CompoundTag tag = stack.getOrCreateTag().getCompound("PerkData");
tag.putDouble(name, num);
stack.addTagElement("PerkData", tag);
}
public static double getPerkDoubleTag(ItemStack stack, String name) {
CompoundTag tag = stack.getOrCreateTag().getCompound("PerkData");
return tag.getDouble(name);
}
public static void setPerkBooleanTag(ItemStack stack, String name, boolean flag) {
CompoundTag tag = stack.getOrCreateTag().getCompound("PerkData");
tag.putBoolean(name, flag);
stack.addTagElement("PerkData", tag);
}
public static boolean getPerkBooleanTag(ItemStack stack, String name) {
CompoundTag tag = stack.getOrCreateTag().getCompound("PerkData");
return tag.getBoolean(name);
}
public static int getAttachmentType(ItemStack stack, AttachmentType type) {
CompoundTag tag = stack.getOrCreateTag().getCompound("Attachments");
return tag.getInt(type.getName());
}
public enum AttachmentType {
SCOPE("Scope"),
MAGAZINE("Magazine"),
BARREL("Barrel"),
STOCK("Stock"),
GRIP("Grip");
private final String name;
AttachmentType(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
public static void setGunIntTag(ItemStack stack, String name, int num) {
CompoundTag tag = stack.getOrCreateTag().getCompound("GunData");
tag.putInt(name, num);
stack.addTagElement("GunData", tag);
}
public static int getGunIntTag(ItemStack stack, String name) {
CompoundTag tag = stack.getOrCreateTag().getCompound("GunData");
return tag.getInt(name);
}
}