[1.12] Allow the server to handle item-block interaction if onItemUse doesn not return PASS (#3967)

This commit is contained in:
malte0811 2017-07-21 00:11:50 +02:00 committed by LexManos
parent ad15209f8d
commit a6b9fbfa9f
3 changed files with 118 additions and 7 deletions

View file

@ -104,7 +104,7 @@
}
return p_178893_1_.equals(this.field_178895_c) && flag;
@@ -382,13 +393,29 @@
@@ -382,13 +393,34 @@
}
else
{
@ -121,7 +121,12 @@
if (this.field_78779_k != GameType.SPECTATOR)
{
+ EnumActionResult ret = itemstack.onItemUseFirst(p_187099_1_, p_187099_2_, p_187099_3_, p_187099_6_, p_187099_4_, f, f1, f2);
+ if (ret != EnumActionResult.PASS) return ret;
+ if (ret != EnumActionResult.PASS)
+ {
+ // The server needs to process the item use as well. Otherwise onItemUseFirst won't ever be called on the server without causing weird bugs
+ this.field_78774_b.func_147297_a(new CPacketPlayerTryUseItemOnBlock(p_187099_3_, p_187099_4_, p_187099_6_, f, f1, f2));
+ return ret;
+ }
+
IBlockState iblockstate = p_187099_2_.func_180495_p(p_187099_3_);
+ boolean bypass = itemstack.func_190926_b() || itemstack.func_77973_b().doesSneakBypassUse(itemstack, p_187099_2_, p_187099_3_, p_187099_1_);
@ -136,7 +141,7 @@
}
if (!flag && itemstack.func_77973_b() instanceof ItemBlock)
@@ -404,7 +431,7 @@
@@ -404,7 +436,7 @@
this.field_78774_b.func_147297_a(new CPacketPlayerTryUseItemOnBlock(p_187099_3_, p_187099_4_, p_187099_6_, f, f1, f2));
@ -145,7 +150,7 @@
{
if (itemstack.func_190926_b())
{
@@ -430,14 +457,20 @@
@@ -430,14 +462,20 @@
{
int i = itemstack.func_77960_j();
int j = itemstack.func_190916_E();
@ -167,7 +172,7 @@
}
}
}
@@ -466,6 +499,8 @@
@@ -466,6 +504,8 @@
}
else
{
@ -176,7 +181,7 @@
int i = itemstack.func_190916_E();
ActionResult<ItemStack> actionresult = itemstack.func_77957_a(p_187101_2_, p_187101_1_, p_187101_3_);
ItemStack itemstack1 = actionresult.func_188398_b();
@@ -473,6 +508,10 @@
@@ -473,6 +513,10 @@
if (itemstack1 != itemstack || itemstack1.func_190916_E() != i)
{
p_187101_1_.func_184611_a(p_187101_3_, itemstack1);
@ -187,7 +192,7 @@
}
return actionresult.func_188397_a();
@@ -509,6 +548,9 @@
@@ -509,6 +553,9 @@
this.func_78750_j();
Vec3d vec3d = new Vec3d(p_187102_3_.field_72307_f.field_72450_a - p_187102_2_.field_70165_t, p_187102_3_.field_72307_f.field_72448_b - p_187102_2_.field_70163_u, p_187102_3_.field_72307_f.field_72449_c - p_187102_2_.field_70161_v);
this.field_78774_b.func_147297_a(new CPacketUseEntity(p_187102_2_, p_187102_4_, vec3d));

View file

@ -0,0 +1,100 @@
package net.minecraftforge.debug;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.*;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.world.World;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.SidedProxy;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import javax.annotation.Nonnull;
@Mod(modid = OnItemUseFirstTest.MODID, name = "OnItemUseFirstTest", version = "0.0.0", acceptableRemoteVersions = "*")
@Mod.EventBusSubscriber
public class OnItemUseFirstTest
{
public static final boolean ENABLE = true;
public static final String MODID = "onitemusefirsttest";
@SidedProxy
public static CommonProxy proxy = null;
public static abstract class CommonProxy
{
public void registerItem(RegistryEvent.Register<Item> event)
{
ItemTest.instance = new ItemTest().setCreativeTab(CreativeTabs.MISC);
event.getRegistry().register(ItemTest.instance);
}
}
public static final class ServerProxy extends CommonProxy
{
}
public static final class ClientProxy extends CommonProxy
{
@Override
public void registerItem(RegistryEvent.Register<Item> event)
{
super.registerItem(event);
for (int i = 0; i < EnumActionResult.values().length; i++)
{
ModelLoader.setCustomModelResourceLocation(ItemTest.instance, i, new ModelResourceLocation(new ResourceLocation(MODID, "test_item"), "inventory"));
}
}
}
@SubscribeEvent
public static void registerItem(RegistryEvent.Register<Item> event)
{
if (ENABLE)
{
proxy.registerItem(event);
}
}
public static class ItemTest extends Item
{
static Item instance;
public ItemTest()
{
setRegistryName(MODID, "test_item");
setHasSubtypes(true);
}
@Nonnull
@Override
public EnumActionResult onItemUseFirst(EntityPlayer player, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ, EnumHand hand)
{
ItemStack stack = player.getHeldItem(hand);
EnumActionResult ret = EnumActionResult.values()[stack.getMetadata()];
player.sendMessage(new TextComponentString("Called onItemUseFirst in thread " + Thread.currentThread().getName() + ", returns " + ret + " in hand " + hand.name()));
return ret;
}
@Override
public void getSubItems(CreativeTabs tab, NonNullList<ItemStack> subItems)
{
if (isInCreativeTab(tab))
{
for (int i = 0; i < EnumActionResult.values().length; i++)
{
subItems.add(new ItemStack(this, 1, i));
}
}
}
@Override
public String getItemStackDisplayName(ItemStack stack)
{
return "OnItemUseFirst Returns: " + EnumActionResult.values()[stack.getMetadata()];
}
}
}

View file

@ -0,0 +1,6 @@
{
"parent": "item/generated",
"textures": {
"layer0": "minecraft:items/shulker_shell"
}
}