- 118
- 0
Я сделал перезарядку, создаю новый мир, стреляю -> нажимаю перезарядку -> предмет перезаряжен. Стреляю -> нажимаю перезарядку -> ничего не происходит
Оружие:
MessageActionWeapons:
Регистрация MessageActionWeapons и EventActionPrimaryKey:
ActionWeapons:
IActionPrimaryKey:
Оружие:
public class ItemGun extends Item implements IActionPrimaryKey
{
public ItemGun(String name)
{
super();
super.maxStackSize = 1;
this.setMaxDamage(6);
this.setUnlocalizedName(name);
this.setTextureName(ModInfo.TEXTURE_PREFIX + "guns/" + name);
}
public void onUpdate(ItemStack itemStack, World world, Entity entity, EntityPlayer player, int par4, boolean par5)
{
if(MyKey.key[0].getIsKeyPressed())
{
MyMod.network.sendToServer(new MessageActionWeapons(3));
}
}
public void onPlayerStoppedUsing(ItemStack is, World world, EntityPlayer player, int i)
{
int j = this.getMaxItemUseDuration(is) - i;
if (player.inventory.hasItem(MyItems.bullet) && is.getItem() == MyItems.gun)
{
EntityBullet entityB = new EntityBullet(world, player);
if(is.getItemDamage() < 6)
{
is.damageItem(1, player);
world.playSoundAtEntity(player, "mymod:shoot", 0.5F, 1.0F / (itemRand.nextFloat() * 0.4F + 1.2F));
if(!player.capabilities.isCreativeMode)
{
player.inventory.consumeInventoryItem(MyItems.bullet);
}
}
if (!world.isRemote)
{
if(is.getItemDamage() < 6)
{
world.spawnEntityInWorld(entityB);
}
}
}
}
public int getMaxItemUseDuration(ItemStack p_77626_1_)
{
return 72000;
}
public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer player)
{
if ( player.inventory.hasItem(MyItems.bullet))
{
player.setItemInUse(itemStack, this.getMaxItemUseDuration(itemStack));
}
return itemStack;
}
@SideOnly(Side.CLIENT)
public void onReloadClient(ItemStack is, World world, EntityPlayer player, boolean isPress)
{
if (isPress)
{MyMod.network.sendToServer(new MessageActionWeapons(3));}
}
public void reload(ItemStack is, World world, EntityPlayer player)
{
if(MyKey.key[0].getIsKeyPressed() && player.inventory.hasItem(MyItems.bullet) && is.getItemDamage() < getMaxDamage())
{
player.addChatMessage(new ChatComponentText(GCCoreUtil.translate("RELOAD!")));
if(is.getItemDamage() == 6)
{
setDamage(is, 0);
}
is.setItemDamage(0);
player.inventory.consumeInventoryItem(MyItems.bullet);
}
MessageActionWeapons:
public class MessageActionWeapons implements IMessage {
public int button;
public MessageActionWeapons() {}
public MessageActionWeapons(int button) {
this.button = button;
}
@Override
public void toBytes(ByteBuf buf) {
buf.writeInt(button);
}
@Override
public void fromBytes(ByteBuf buf) {
button = buf.readInt();
}
public static class Handler implements IMessageHandler<MessageActionWeapons, IMessage> {
@Override
public IMessage onMessage(MessageActionWeapons packet, MessageContext message) {
int button = packet.button;
EntityPlayerMP player = message.getServerHandler().playerEntity;
switch(button) {
case 3:ActionWeapons.onReload(player);
break;
}
return null;
}
}
}
Регистрация MessageActionWeapons и EventActionPrimaryKey:
public static SimpleNetworkWrapper network;
@EventHandler
public void preInit(FMLPreInitializationEvent event)
{
network = NetworkRegistry.INSTANCE.newSimpleChannel(ModInfo.MOD_ID);
network.registerMessage(MessageActionWeapons.Handler.class, MessageActionWeapons.class, 0, Side.SERVER);
FMLCommonHandler.instance().bus().register(new EventActionPrimaryKey());
}
ActionWeapons:
public class ActionWeapons {
public static void onReload(EntityPlayer player)
{
ItemStack current = player.getCurrentEquippedItem();
if (current != null && current.getItem() instanceof ItemGun)
((ItemGun)current.getItem()).reload(current, player.worldObj, player);
}
}
IActionPrimaryKey:
EventActionPrimaryKey:public interface IActionPrimaryKey {
@SideOnly(Side.CLIENT)
public void onReloadClient(ItemStack is, World world, EntityPlayer player, boolean isPress);
}
ActionPrimaryKey:public class EventActionPrimaryKey {
boolean rel, relLast = false;
@SubscribeEvent
public void actionReload(TickEvent.PlayerTickEvent event) {
if (event.phase == TickEvent.Phase.START && event.side == Side.CLIENT) {
if (MyKey.key[0].getIsKeyPressed()) {
rel = true;
if (!relLast && rel) ActionPrimaryKey.actionReloadClient(event.player, true);//press down
relLast = rel;
} else {
relLast = false;
if (relLast && !rel) ActionPrimaryKey.actionReloadClient(event.player, false);//unpress down
relLast = rel;
}
}
}
}
public class ActionPrimaryKey {
public static void actionReloadClient(EntityPlayer player, boolean buttonstate)
{
ItemStack current = player.getCurrentEquippedItem();
if (current != null && current.getItem() instanceof IActionPrimaryKey)
((IActionPrimaryKey)current.getItem()).onReloadClient(current, player.worldObj, player, buttonstate);
}
}