More old patch work, cleaned up static methods added to ItemStack, and various networking functions.

This commit is contained in:
LexManos committed 2019-01-28 16:25:49 -08:00
1 parent 0067d3edce
commit b40e2cc59e
44 files changed
+507 -1170

No files matched your search

@@ -19,31 +19,21 @@
package net.minecraftforge.common;
import java.io.BufferedReader;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import com.google.common.base.Throwables;
import com.google.common.collect.Lists;
import com.google.common.collect.Queues;
@@ -54,10 +44,7 @@ import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import net.minecraft.advancements.Advancement;
import net.minecraft.advancements.AdvancementManager;
import net.minecraft.block.Block;
import net.minecraft.block.BlockFarmland;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.BlockWorldState;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
@@ -72,17 +59,13 @@ import net.minecraft.entity.item.EntityMinecartContainer;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.entity.projectile.EntityThrowable;
import net.minecraft.fluid.IFluidState;
import net.minecraft.init.Blocks;
import net.minecraft.init.Fluids;
import net.minecraft.init.Items;
import net.minecraft.inventory.ContainerRepair;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.Item;
import net.minecraft.item.ItemAxe;
import net.minecraft.item.ItemBucket;
import net.minecraft.item.ItemEnchantedBook;
import net.minecraft.item.ItemPickaxe;
import net.minecraft.item.ItemPotion;
@@ -90,21 +73,13 @@ import net.minecraft.item.ItemSpade;
import net.minecraft.item.ItemSpawnEgg;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemTippedArrow;
import net.minecraft.item.ItemUseContext;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.NetHandlerPlayServer;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.SPacketBlockChange;
import net.minecraft.network.play.server.SPacketRecipeBook;
import net.minecraft.network.play.server.SPacketRecipeBook.State;
import net.minecraft.potion.PotionType;
import net.minecraft.potion.PotionUtils;
import net.minecraft.resources.IResource;
import net.minecraft.resources.IResourceManager;
import net.minecraft.stats.StatList;
import net.minecraft.tags.ItemTags;
import net.minecraft.tags.Tag;
import net.minecraft.tileentity.MobSpawnerBaseLogic;
import net.minecraft.tileentity.TileEntity;
@@ -113,7 +88,6 @@ import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.JsonUtils;
import net.minecraft.util.NonNullList;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.WeightedRandom;
import net.minecraft.util.math.AxisAlignedBB;
@@ -135,8 +109,6 @@ import net.minecraft.world.storage.loot.LootEntry;
import net.minecraft.world.storage.loot.LootTable;
import net.minecraft.world.storage.loot.LootTableManager;
import net.minecraft.world.storage.loot.conditions.LootCondition;
import net.minecraftforge.common.crafting.CraftingHelper;
import net.minecraftforge.common.util.BlockSnapshot;
import net.minecraftforge.event.AnvilUpdateEvent;
import net.minecraftforge.event.DifficultyChangeEvent;
import net.minecraftforge.event.ForgeEventFactory;
@@ -163,22 +135,10 @@ import net.minecraftforge.event.entity.player.AdvancementEvent;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.event.world.NoteBlockEvent;
import net.minecraftforge.eventbus.api.Event.Result;
import net.minecraftforge.fluids.IFluidBlock;
import net.minecraftforge.fml.ModContainer;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.fml.network.ConnectionType;
import net.minecraftforge.fml.loading.moddiscovery.ModFile;
import net.minecraftforge.fml.loading.moddiscovery.ModFileInfo;
import net.minecraftforge.fml.network.NetworkHooks;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.IForgeRegistry;
import net.minecraftforge.registries.RegistryManager;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Marker;
@@ -1026,23 +986,6 @@ public class ForgeHooks
return null;
}
/* TODO this should be unnecessary now?
public static void sendRecipeBook(NetHandlerPlayServer connection, State state, List<IRecipe> recipes, List<IRecipe> display, boolean isGuiOpen, boolean isFilteringCraftable)
{
NetworkDispatcher disp = NetworkDispatcher.get(connection.getNetworkManager());
//Not sure how it could ever be null, but screw it lets protect against it. Could Error the client but we dont care if they are asking for this stuff in the wrong state!
ConnectionType type = disp == null || disp.getConnectionType() == null ? ConnectionType.MODDED : disp.getConnectionType();
if (type == ConnectionType.VANILLA)
{
IForgeRegistry<IRecipe> vanilla = RegistryManager.VANILLA.getRegistry(IRecipe.class);
recipes = recipes.stream().filter(vanilla::containsValue).collect(Collectors.toList());
display = display.stream().filter(vanilla::containsValue).collect(Collectors.toList());
}
if (!recipes.isEmpty() || !display.isEmpty())
connection.sendPacket(new SPacketRecipeBook(state, recipes, display, isGuiOpen, isFilteringCraftable));
}
*/
public static void onAdvancement(EntityPlayerMP player, Advancement advancement)
{
MinecraftForge.EVENT_BUS.post(new AdvancementEvent(player, advancement));
@@ -130,7 +130,7 @@ public class ForgeMod implements WorldPersistenceHooks.WorldPersistenceHook
{
universalBucket = new UniversalBucket();
universalBucket.setUnlocalizedName("forge.bucketFilled");
event.getRegistry().register(universalBucket.setRegistryName(ForgeVersion.MOD_ID, "bucketFilled"));
event.getRegistry().register(universalBucket.setRegistryName(ForgeVersion.MOD_ID, "bucket_filled"));
MinecraftForge.EVENT_BUS.register(universalBucket);
}
}
@@ -44,7 +44,7 @@ public class IngredientNBT extends Ingredient
if (input == null)
return false;
//Can't use areItemStacksEqualUsingNBTShareTag because it compares stack size as well
return this.stack.getItem() == input.getItem() && this.stack.getDamage() == input.getDamage() && ItemStack.areItemStackShareTagsEqual(this.stack, input);
return this.stack.getItem() == input.getItem() && this.stack.getDamage() == input.getDamage() && this.stack.areShareTagsEqual(input);
}
@Override
@@ -121,15 +121,18 @@ public interface IForgeItem
* behavior client side!
*
* Note that this will sometimes be applied multiple times, the following MUST
* be supported: Item item = stack.getItem(); NBTTagCompound nbtShare1 =
* item.getNBTShareTag(stack); stack.setTagCompound(nbtShare1); NBTTagCompound
* nbtShare2 = item.getNBTShareTag(stack); assert nbtShare1.equals(nbtShare2);
* be supported:
* Item item = stack.getItem();
* NBTTagCompound nbtShare1 = item.getNBTShareTag(stack);
* stack.setTagCompound(nbtShare1);
* NBTTagCompound nbtShare2 = item.getNBTShareTag(stack);
* assert nbtShare1.equals(nbtShare2);
*
* @param stack The stack to send the NBT tag for
* @return The NBT tag
*/
@Nullable
default NBTTagCompound getNBTShareTag(ItemStack stack)
default NBTTagCompound getShareTag(ItemStack stack)
{
return stack.getTag();
}
@@ -141,7 +144,7 @@ public interface IForgeItem
* @param stack The stack that received NBT
* @param nbt Received NBT, can be null
*/
default void readNBTShareTag(ItemStack stack, @Nullable NBTTagCompound nbt)
default void readShareTag(ItemStack stack, @Nullable NBTTagCompound nbt)
{
stack.setTag(nbt);
}
@@ -355,4 +355,80 @@ public interface IForgeItemStack extends ICapabilitySerializable<NBTTagCompound>
{
return getStack().getItem().onDroppedByPlayer(getStack(), player);
}
/**
* Get the NBT data to be sent to the client. The Item can control what data is kept in the tag.
*
* Note that this will sometimes be applied multiple times, the following MUST
* be supported:
* Item item = stack.getItem();
* NBTTagCompound nbtShare1 = item.getNBTShareTag(stack);
* stack.setTagCompound(nbtShare1);
* NBTTagCompound nbtShare2 = item.getNBTShareTag(stack);
* assert nbtShare1.equals(nbtShare2);
*
* @return The NBT tag
*/
@Nullable
default NBTTagCompound getShareTag()
{
return getStack().getItem().getShareTag(getStack());
}
/**
* Override this method to decide what to do with the NBT data received from
* getNBTShareTag().
*
* @param stack The stack that received NBT
* @param nbt Received NBT, can be null
*/
default void readShareTag(@Nullable NBTTagCompound nbt)
{
getStack().getItem().readShareTag(getStack(), nbt);
}
/**
*
* Should this item, when held, allow sneak-clicks to pass through to the underlying block?
*
* @param world The world
* @param pos Block position in world
* @param player The Player that is wielding the item
* @return
*/
default boolean doesSneakBypassUse(net.minecraft.world.IWorldReader world, BlockPos pos, EntityPlayer player)
{
return getStack().isEmpty() || getStack().getItem().doesSneakBypassUse(getStack(), world, pos, player);
}
/**
* Modeled after ItemStack.areItemStackTagsEqual
* Uses Item.getNBTShareTag for comparison instead of NBT and capabilities.
* Only used for comparing itemStacks that were transferred from server to client using Item.getNBTShareTag.
*/
default boolean areShareTagsEqual(ItemStack other)
{
NBTTagCompound shareTagA = getStack().getShareTag();
NBTTagCompound shareTagB = other.getShareTag();
if (shareTagA == null)
return shareTagB == null;
else
return shareTagB != null && shareTagA.equals(shareTagB);
}
/**
* Determines if the ItemStack is equal to the other item stack, including Item, Count, and NBT.
*
* @param other The other stack
* @param limitTags True to use shareTag False to use full NBT tag
* @return true if equals
*/
default boolean equals(ItemStack other, boolean limitTags)
{
if (getStack().isEmpty())
return other.isEmpty();
else
return !other.isEmpty() && getStack().getCount() == other.getCount() && getStack().getItem() == other.getItem() &&
(limitTags ? getStack().areShareTagsEqual(other) : ItemStack.areItemStackTagsEqual(getStack(), other));
}
}
@@ -1,58 +0,0 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2018.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.common.util;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.PacketBuffer;
public class PacketUtil
{
private PacketUtil() {}
/**
* Most ItemStack serialization is Server to Client, and must go through PacketBuffer.writeItemStack which uses Item.getNBTShareTag.
* One exception is items from the creative menu, which must be sent from Client to Server with their full NBT.
* <br/>
* This method matches PacketBuffer.writeItemStack but without the Item.getNBTShareTag patch.
* It is compatible with PacketBuffer.readItemStack.
*/
public static void writeItemStackFromClientToServer(PacketBuffer buffer, ItemStack stack)
{
if (stack.isEmpty())
{
buffer.writeShort(-1);
}
else
{
buffer.writeShort(Item.getIdFromItem(stack.getItem()));
buffer.writeByte(stack.getCount());
NBTTagCompound nbttagcompound = null;
if (stack.getItem().isDamageable() || stack.getItem().getShareTag())
{
nbttagcompound = stack.getTag();
}
buffer.writeCompoundTag(nbttagcompound);
}
}
}