Удаление предмета в мире

Версия Minecraft
1.11+
93
1
1
я при помощи setDead() удаляю предмет в мире, но он удаляет сразу стак, если кинуть стак, как это исправить?
 
Последнее редактирование:
93
1
1
Типа как-то так:
А мона код пояснить?
Java:
for (EntityItem i :worldIn.getEntitiesWithinAABB(EntityItem.class, getEntityBoundingBox().expand(2.5D, 2.6D, 2.5D).offset(new BlockPos(pos.getX(), pos.getY(), pos.getZ())))) {
            if (i.getEntityItem().getItem() == Items.APPLE) {
                for (EntityItem j :worldIn.getEntitiesWithinAABB(EntityItem.class, getEntityBoundingBox().expand(2.5D, 2.6D, 2.5D).offset(new BlockPos(pos.getX(), pos.getY(), pos.getZ())))){
                    if (j.getEntityItem().getItem() == Items.DIAMOND) {
                        Diamond1 =true;
                        j.setDead();
                        i.setDead();
                    }
                }
            }

        }
вот у меня кодик он чекает есть ли яблоко и алмаз и делетает сразу стак чего либо.. если стак кинуть
 

timaxa007

Модератор
5,831
409
672
А мона код пояснить?
Java:
EntityItem ei = ...//Твой предмет виде Entity в мире, т.е. EntityItem.
ItemStack is = ei.getEntityItem();//Берём из EntityItem объект ItemStack'а.
--is.stackSize;//Уменьшаем размер стака на одну единицу.
if (is.stackSize <= 0)//Если размер стака меньше или равно нулю, то...
    ei.setDead();//EntityItem умрёт (ну типа из мира исчезнет).
else//Если больше нуля, то...
    ei.setEntityItemStack(is);//То даём изменённый ItemStack для EntityItem.
 

timaxa007

Модератор
5,831
409
672
На 1.12.2 примерно так:
Java:
EntityItem
apple = null,
diamond = null;

for (EntityItem i :worldIn.getEntitiesWithinAABB(EntityItem.class, getEntityBoundingBox().expand(2.5D, 2.6D, 2.5D).offset(new BlockPos(pos.getX(), pos.getY(), pos.getZ())))) {
    if (apple == null && i.getItem().getItem() == Items.APPLE) apple = i;
    else if (diamond == null && i.getItem().getItem() == Items.DIAMOND) diamond = i;
    if (apple != null && diamond != null) break;
}
       
if (apple != null && diamond != null) {
           
    ItemStack is = apple.getItem();
    is.shrink(1);
    if (is.getCount() <= 0) apple.setDead(); else apple.setItem(is);

    is = diamond.getItem();
    is.shrink(1);
    if (is.getCount() <= 0) diamond.setDead(); else diamond.setItem(is);
           
}
 
93
1
1
EntityItem ei = ...//Твой предмет виде Entity в мире, т.е. EntityItem. ItemStack is = ei.getEntityItem();//Берём из EntityItem объект ItemStack'а. --is.stackSize;//Уменьшаем размер стака на одну единицу. if (is.stackSize <= 0)//Если размер стака меньше или равно нулю, то... ei.setDead();//EntityItem умрёт (ну типа из мира исчезнет). else//Если больше нуля, то... ei.setEntityItemStack(is);//То даём изменённый ItemStack для EntityItem.
Ну прям слишком подробно) я имел ввиду stackSize это откуда брать? Свою переменную пилить? Или как то принимать ее из ItemStack?
 

timaxa007

Модератор
5,831
409
672
Я бы не сказал, что я накосячил, я предупредил, что код на 1.12.2, так как у меня не стоит 1.11+ версия.
Так что типа просто место ".getItem().getItem()" на ".getEntityItem().getItem()".
---
apple.getItem(); на apple.getEntityItem();
diamond.getItem(); на diamond.getEntityItem();
 

timaxa007

Модератор
5,831
409
672
Последнее редактирование:
93
1
1
Java:
    public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
        world = worldIn;
        for(int k =-1; k<=1;k++) {
            if (worldIn.getBlockState(new BlockPos(pos.getX() + 2, pos.getY(), pos.getZ()+k)).getBlock() != ModBlocks.BLOCKCIRCLE) {
                circleIsNotIntact = false;

            }
            if (worldIn.getBlockState(new BlockPos(pos.getX() - 2, pos.getY(), pos.getZ()+k)).getBlock() != ModBlocks.BLOCKCIRCLE) {
                circleIsNotIntact = false;
            }
            if (worldIn.getBlockState(new BlockPos(pos.getX() + k, pos.getY(), pos.getZ()+2)).getBlock() != ModBlocks.BLOCKCIRCLE) {
                circleIsNotIntact = false;

            }
            if (worldIn.getBlockState(new BlockPos(pos.getX() + k, pos.getY(), pos.getZ()-2)).getBlock() != ModBlocks.BLOCKCIRCLE) {
                circleIsNotIntact = false;
            }
        }
        if(circleIsNotIntact == true) {
            for (EntityItem i : worldIn.getEntitiesWithinAABB(EntityItem.class, getEntityBoundingBox().expand(2.5D, 2.6D, 2.5D).offset(new BlockPos(pos.getX(), pos.getY(), pos.getZ())))) {
                if (i.getEntityItem().getItem() == Items.APPLE) {
                    for (EntityItem j : worldIn.getEntitiesWithinAABB(EntityItem.class, getEntityBoundingBox().expand(2.5D, 2.6D, 2.5D).offset(new BlockPos(pos.getX(), pos.getY(), pos.getZ())))) {
                        if (j.getEntityItem().getItem() == Items.DIAMOND) {
                            ItemStack is = i.getEntityItem();
                            is.shrink(1);
                            if (is.getCount() <= 0) {
                                i.setDead();
                            } else {
                                i.setEntityItemStack(is);
                            }

                            is = j.getEntityItem();
                            is.shrink(1);
                            if (is.getCount() <= 0) {
                                j.setDead();
                            } else {
                                j.setEntityItemStack(is);
                            }
                            times();

                        }
                    }
                }
            }
        }



        return super.onBlockActivated(worldIn, pos, state, playerIn, hand, facing, hitX, hitY, hitZ);
    }
    public void times(){
        world.setWorldTime(0);
    }
Спасити миня, теперь у меня время в мире при выполнении всех условий меняется на пол секунды и возвращается обратно на то, котрое было..
И я изменил код, так как всё таки твой немного неправильно работал, не так как мне надо, тут только двумя циклами.
 

timaxa007

Модератор
5,831
409
672
У тебя цикл в цикле, и даже цикл не останавливаются когда все условия есть, возможно из-за этого у тебя может, просто по несколько раз сработать times(). Тем более у тебя нету проверки на не клиентский мир, типа сначала клиентский применяется, а потом пакет от серверного для клиентского мира даётся.
 
93
1
1
Java:
    for(int k =-1; k<=1;k++) {
            if (worldIn.getBlockState(new BlockPos(pos.getX() + 2, pos.getY(), pos.getZ()+k)).getBlock() != ModBlocks.BLOCKCIRCLE) {
                circleIsNotIntact = false;

            }
            if (worldIn.getBlockState(new BlockPos(pos.getX() - 2, pos.getY(), pos.getZ()+k)).getBlock() != ModBlocks.BLOCKCIRCLE) {
                circleIsNotIntact = false;
            }
            if (worldIn.getBlockState(new BlockPos(pos.getX() + k, pos.getY(), pos.getZ()+2)).getBlock() != ModBlocks.BLOCKCIRCLE) {
                circleIsNotIntact = false;

            }
            if (worldIn.getBlockState(new BlockPos(pos.getX() + k, pos.getY(), pos.getZ()-2)).getBlock() != ModBlocks.BLOCKCIRCLE) {
                circleIsNotIntact = false;
            }
        }
        if(circleIsNotIntact == true) {
            for (EntityItem i : worldIn.getEntitiesWithinAABB(EntityItem.class, getEntityBoundingBox().expand(2.5D, 2.6D, 2.5D).offset(new BlockPos(pos.getX(), pos.getY(), pos.getZ())))) {
                if (i.getEntityItem().getItem() == Items.APPLE) {
                    for (EntityItem j : worldIn.getEntitiesWithinAABB(EntityItem.class, getEntityBoundingBox().expand(2.5D, 2.6D, 2.5D).offset(new BlockPos(pos.getX(), pos.getY(), pos.getZ())))) {
                        if (j.getEntityItem().getItem() == Items.DIAMOND) {
                            apple = i;
                            diamond = j;
                            break;
                        }
                    }
                }
            }
        }
        if(apple != null){
            ItemStack is = apple.getEntityItem();
            is.shrink(1);
            if (is.getCount() <= 0) {
                apple.setDead();
            } else {
                apple.setEntityItemStack(is);
            }

            is = diamond.getEntityItem();
            is.shrink(1);
            if (is.getCount() <= 0) {
                diamond.setDead();
            } else {
                diamond.setEntityItemStack(is);
            }
            apple =null;
            diamond = null;
            worldIn.setWorldTime(0);
        }
теперь так, всё рано время меняется и возвращается обратно(так правильней?)
 

timaxa007

Модератор
5,831
409
672
так правильней?
Не думаю, у тебя два цикла и один break, а должно два: один останавливает второй цикл, второй останавливает первый цикл.
Ах-да, у тебя ещё и третий цикл есть.
 
93
1
1
Java:
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
        for(int k =-1; k<=1;k++) {
            if (worldIn.getBlockState(new BlockPos(pos.getX() + 2, pos.getY(), pos.getZ()+k)).getBlock() != ModBlocks.BLOCKCIRCLE) {
                circleIsNotIntact = false;

            }
            if (worldIn.getBlockState(new BlockPos(pos.getX() - 2, pos.getY(), pos.getZ()+k)).getBlock() != ModBlocks.BLOCKCIRCLE) {
                circleIsNotIntact = false;
            }
            if (worldIn.getBlockState(new BlockPos(pos.getX() + k, pos.getY(), pos.getZ()+2)).getBlock() != ModBlocks.BLOCKCIRCLE) {
                circleIsNotIntact = false;

            }
            if (worldIn.getBlockState(new BlockPos(pos.getX() + k, pos.getY(), pos.getZ()-2)).getBlock() != ModBlocks.BLOCKCIRCLE) {
                circleIsNotIntact = false;
            }
        }
        if(circleIsNotIntact == true) {
            for (EntityItem i : worldIn.getEntitiesWithinAABB(EntityItem.class, getEntityBoundingBox().expand(2.5D, 2.6D, 2.5D).offset(new BlockPos(pos.getX(), pos.getY(), pos.getZ())))) {
                if (i.getEntityItem().getItem() == Items.APPLE) {
                    for (EntityItem j : worldIn.getEntitiesWithinAABB(EntityItem.class, getEntityBoundingBox().expand(2.5D, 2.6D, 2.5D).offset(new BlockPos(pos.getX(), pos.getY(), pos.getZ())))) {
                        if (j.getEntityItem().getItem() == Items.DIAMOND) {
                            apple = i;
                            diamond = j;
                            break;
                        }
                    }
                    break;
                }
            }
        }
        if(apple != null){
            ItemStack is = apple.getEntityItem();
            is.shrink(1);
            if (is.getCount() <= 0) {
                apple.setDead();
            } else {
                apple.setEntityItemStack(is);
            }

            is = diamond.getEntityItem();
            is.shrink(1);
            if (is.getCount() <= 0) {
                diamond.setDead();
            } else {
                diamond.setEntityItemStack(is);
            }
            apple = null;
            diamond = null;
            worldIn.setWorldTime(0);
        }


        return super.onBlockActivated(worldIn, pos, state, playerIn, hand, facing, hitX, hitY, hitZ);
    }
Дописал брэйк, но я понял прикол, когда кидаешь 4 адмаза и 4 яблока, они все исчезают и время меняется..
Есть идеи как исправлять? и в чём прикол?
 

timaxa007

Модератор
5,831
409
672
Не знаю, как там у тебя должно быть, но я этот код не проверял:
Java:
    @Override
    public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {

        if (worldIn.isRemote) return true;
        
        EntityItem
        apple = null,
        diamond = null;

        for (int x =-2; x <= 2; x++) {
            if (x == 0) {
                for (int z =-2; z <= 2; z++) {
                    if (z == 0) continue;
                    if (worldIn.getBlockState(new BlockPos(pos.getX(), pos.getY(), pos.getZ()+z)).getBlock() != ModBlocks.BLOCKCIRCLE)) {
                        return true;
                    }
                }
            } else {
                if (worldIn.getBlockState(new BlockPos(pos.getX() + x, pos.getY(), pos.getZ())).getBlock() != ModBlocks.BLOCKCIRCLE)) {
                    return true;
                }
            }
        }
        
        for (EntityItem i : worldIn.getEntitiesWithinAABB(EntityItem.class, new AxisAlignedBB(pos).expand(2.5D, 2.6D, 2.5D))) {
            if (apple == null && i.getEntityItem().getItem() == Items.APPLE) apple = i;
            else if (diamond == null && i.getEntityItem().getItem() == Items.DIAMOND) diamond = i;
            if (apple != null && diamond != null) break;
        }

        if (apple != null && diamond != null) {

            ItemStack is = apple.getEntityItem();
            is.shrink(1);
            if (is.getCount() <= 0) apple.setDead(); else apple.setEntityItemStack(is);

            is = diamond.getEntityItem();
            is.shrink(1);
            if (is.getCount() <= 0) diamond.setDead(); else diamond.setEntityItemStack(is);
            
            worldIn.setWorldTime(0L);

        }

        return true;
    }
 
Сверху