Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using MiNET;
- using MiNET.Net;
- using MiNET.Items;
- using MiNET.Utils;
- using fNbt;
- using log4net;
- namespace xCore
- {
- public class HashedItemSigner : ItemSigner
- {
- public static string SessionTagKey { get; set; } = string.Empty;
- //public static int SessionKey = 0;
- private static readonly ILog Log = LogManager.GetLogger(typeof(HashedItemSigner));
- private static bool _signItems = true;
- public override Item SignItem(Item item)
- {
- //if (SessionKey == 0) xCoreGames.rnd.Next(0, 100000);
- if(string.IsNullOrEmpty(SessionTagKey))
- {
- var m = xCoreGames.rnd.Next(10, 20);
- for (var i = 0; i < m; i++)
- SessionTagKey += (char)xCoreGames.rnd.Next(0, 40);
- }
- //if (!_signItems) return item;
- if (item.ExtraData == null) item.ExtraData = new NbtCompound();
- //item.ExtraData["Hash"] = new NbtInt("Hash", 911);
- {
- if (!item.ExtraData.Contains(SessionTagKey)) {
- //lock(item.ExtraData)
- //{
- var vTag = new NbtCompound(SessionTagKey);
- foreach (var t in item.ExtraData)
- vTag.Add((NbtTag)t.Clone());
- var itemDat = new NbtCompound("ItemData");
- itemDat.Add(new NbtInt("Id", item.Id));
- itemDat.Add(new NbtShort("Metadata", item.Metadata));
- //itemDat.Add(new NbtByte("Count", item.Count));
- vTag.Add(itemDat);
- item.ExtraData.Add(vTag);
- //}
- //item.ExtraData["Hash"] = new NbtInt(911);
- }
- }
- return item;
- }
- public override bool VerifyItemStack(Player player, Item itemStack)
- {
- //if (!_signItems) return true;
- if (itemStack.Id == 0 && itemStack.Count == 0 && itemStack.Metadata == 0) return true;
- if (itemStack.ExtraData == null)
- {
- Log.Error($"{player.Username} Missing ExtraData on item with ID: {itemStack.Id}, Meta: {itemStack.Metadata}, Count: {itemStack.Count}");
- return false;
- }
- //if (!itemStack.ExtraData.Contains(SessionTagKey))
- //{
- // Log.Error($"{player.Username} Missing hash for ExtraData on item with ID: {itemStack.Id}, Meta: {itemStack.Metadata}, Count: {itemStack.Count}");
- // return false;
- //}
- //if (tag.IntValue != SessionKey)
- //{
- // Log.Error($"{player.Username} Invalid hash for ExtraData on item with ID: {itemStack.Id}, Meta: {itemStack.Metadata}, Count: {itemStack.Count} NBT: {itemStack.ExtraData}");
- // return false;
- //}
- NbtCompound signTag = null;
- if (!itemStack.ExtraData.TryGet(SessionTagKey, out signTag))
- {
- Log.Error($"{player.Username} Missing hash for ExtraData on item with ID: {itemStack.Id}, Meta: {itemStack.Metadata}, Count: {itemStack.Count}");
- return false;
- }
- NbtCompound itemDatTag = null;
- if (signTag.TryGet("ItemData", out itemDatTag))
- {
- NbtInt idTag = null;
- NbtShort metadataTag = null;
- NbtByte countTag = null;
- if(itemDatTag.TryGet("Id", out idTag) && itemDatTag.TryGet("Metadata", out metadataTag) /*&& itemDatTag.TryGet("Count", out countTag)*/)
- {
- if(idTag.IntValue != itemStack.Id || metadataTag.ShortValue != itemStack.Metadata /*|| countTag.ByteValue != itemStack.Count*/)
- {
- return false;//id, metadata или count не совпадают с подписью
- }
- }
- else
- {
- return false;//отсутствует информация об id, metadata и count
- }
- }
- else
- {
- return false;//отсутствует информация об id, metadata и count
- }
- foreach(var extradataTag in itemStack.ExtraData)
- {
- if (extradataTag.Name == SessionTagKey) continue;
- NbtTag outTag = null;
- if (signTag.TryGet(extradataTag.Name, out outTag))
- {
- if (extradataTag != outTag)
- {
- return false;//неверный тег extradataTag.Name
- }
- }
- else
- {
- return false;//в SignData отсутствует тег extradataTag.Name
- }
- }
- foreach (var sigTag in itemStack.ExtraData)
- {
- if (sigTag.Name == "ItemData") continue;
- NbtTag outTag = null;
- if (!itemStack.ExtraData.TryGet(sigTag.Name, out outTag))
- {
- return false;//в ExtraData отсутствует тег extradataTag.Name
- }
- }
- return true;
- }
- }
- public static class ItemSignerConfig
- {
- public static void AddTag(this Item item, NbtTag tag)
- {
- if (item.ExtraData == null) return;//Hacks...
- NbtCompound signTag = null;
- if (!item.ExtraData.TryGet(HashedItemSigner.SessionTagKey, out signTag)) return;//hacks...
- signTag.Add((NbtTag)tag.Clone());
- item.ExtraData.Add((NbtTag)tag.Clone());
- }
- public static void RemoveTag(this Item item, string tagName)
- {
- if (item.ExtraData == null) return;//Hacks...
- NbtCompound signTag = null;
- if (!item.ExtraData.TryGet(HashedItemSigner.SessionTagKey, out signTag)) return;//hacks...
- NbtTag tag = null;
- if (signTag.TryGet(tagName, out tag))
- signTag.Remove(tag);
- if (item.ExtraData.TryGet(tagName, out tag))
- item.ExtraData.Remove(tag);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement