Jump to content

[1.11] How to cancel bucket use event?


CBUltimate

Recommended Posts

I would assume placing lava with bucket can be canceled in BlockEvent.PlaceEvent, but it doesn't, so how can cancel players placing down lava? Thanks in advance.

PlayerInteractEvent.RightClickBlock

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

I've used it here. But the lava still get placed down, even though I've set it to cancel the event. Help, Thanks in advance.

 

@SubscribeEvent
    public void onPlayerRightClickBlock(PlayerInteractEvent.RightClickBlock event){
        if (!event.getWorld().isRemote){
            System.out.println("Right clicked with " + event.getItemStack().getUnlocalizedName());

            boolean itemInteraction = false;

            if (event.getItemStack().getUnlocalizedName().equals("item.bucketLava") || event.getItemStack().getUnlocalizedName().equals("tile.chest") || event.getItemStack().getUnlocalizedName().equals("tile.bucketWater")){
                itemInteraction = true;
            }

            if (itemInteraction){
                 event.getEntityPlayer().addChatMessage(new TextComponentString("You are not authorized use that item here."));
                 event.setCanceled(true);
            }
        }
    }

 

I've shorten the code abit, these are the keyparts.

Link to comment
Share on other sites

I've made it not to use unlocalized name. But it still doesn't cancel the lava placement, I do get the message, but lava still get placed down. Help, thanks in advance.

 

@SubscribeEvent
    public void onPlayerRightClickBlock(PlayerInteractEvent.RightClickBlock event){
        if (!event.getWorld().isRemote){
            //System.out.println("Event Triggered with " + event.getWorld().getBlockState(event.getPos()).getBlock().getLocalizedName());
            System.out.println("Right clicked with " + event.getItemStack().getUnlocalizedName());

            boolean itemInteraction = false;

            if (event.getItemStack() == new ItemStack(Items.LAVA_BUCKET) || event.getItemStack() == new ItemStack(Items.WATER_BUCKET) || event.getItemStack() == new ItemStack(Blocks.CHEST)){
                itemInteraction = true;
            }

		if (itemInteraction){
			event.getEntityPlayer().addChatMessage(new TextComponentString("You are not authorized use that item here."));
			event.setCanceled(true);
		}
        }
    }

Link to comment
Share on other sites

Don't compare

ItemStack

instances. They can never be equal, especially when you create a new one directly before comparing. Compare the

Item

in the

ItemStack

:

ItemStack#getItem() == Items.LAVA_BUCKET

.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

 

package mod.cbultimate.stranded;

import mod.cbultimate.stranded.block.ModBlocks;
import mod.cbultimate.stranded.tileentity.TileEntityToolCupboard;
import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.world.World;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.event.world.WorldEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

import java.util.ArrayList;

//Created by CBU on 14/1/2017.

public class ModEventHandler {
    private final String cupboard_dataIdentifier = "ToolCupboards";
    private static ArrayList<Block> protectedBlockList = new ArrayList<Block>();
    private static ArrayList<Item> blacklistedItems = new ArrayList<Item>();

    public static void Init(){
        protectedBlockList.add(Blocks.OAK_DOOR);
        protectedBlockList.add(Blocks.IRON_DOOR);
        protectedBlockList.add(Blocks.ACACIA_DOOR);
        protectedBlockList.add(Blocks.BIRCH_DOOR);
        protectedBlockList.add(Blocks.DARK_OAK_DOOR);
        protectedBlockList.add(Blocks.JUNGLE_DOOR);
        protectedBlockList.add(Blocks.SPRUCE_DOOR);
        protectedBlockList.add(Blocks.TRAPDOOR);
        protectedBlockList.add(Blocks.IRON_TRAPDOOR);
        protectedBlockList.add(Blocks.STONE_BUTTON);
        protectedBlockList.add(Blocks.WOODEN_BUTTON);
        protectedBlockList.add(Blocks.STONE_PRESSURE_PLATE);
        protectedBlockList.add(Blocks.WOODEN_PRESSURE_PLATE);
        protectedBlockList.add(Blocks.LEVER);

        blacklistedItems.add(Items.LAVA_BUCKET);
        blacklistedItems.add(Items.WATER_BUCKET);
    }

    @SubscribeEvent
    public void onWorldLoad(WorldEvent.Load event){
        World world = event.getWorld();
        ModWorldSavedData modWorldSavedData = (ModWorldSavedData) world.getPerWorldStorage().getOrLoadData(ModWorldSavedData.class, cupboard_dataIdentifier);

        if (modWorldSavedData == null) {
            modWorldSavedData = new ModWorldSavedData(cupboard_dataIdentifier);
        }
        for (int i=0; i < modWorldSavedData.ToolCupboards.size(); i ++){
            BlockPos currentPosition = modWorldSavedData.ToolCupboards.get(i);
            if (world.isAirBlock(currentPosition) || world.getBlockState(currentPosition).getBlock() != ModBlocks.woodenToolCupboard){
                modWorldSavedData.ToolCupboards.remove(i);
            }
        }
        modWorldSavedData.markDirty();
    }

    @SubscribeEvent
    public void blockPlacedEvent(BlockEvent.PlaceEvent event){
        World world = event.getWorld();
        ModWorldSavedData modWorldSavedData = (ModWorldSavedData) world.getPerWorldStorage().getOrLoadData(ModWorldSavedData.class, cupboard_dataIdentifier);

        if (modWorldSavedData == null) {
            modWorldSavedData = new ModWorldSavedData(cupboard_dataIdentifier);
        }

        boolean cancelPlacement = false;

        for (int i=0; i < modWorldSavedData.ToolCupboards.size(); i ++){
            BlockPos currentPosition = modWorldSavedData.ToolCupboards.get(i);
            if (event.getPos().getDistance(currentPosition.getX(), currentPosition.getY(), currentPosition.getZ()) < 16){
                if (event.getPlacedBlock().getBlock() == ModBlocks.woodenToolCupboard) {
                    cancelPlacement = true;
                    event.getPlayer().addChatMessage(new TextComponentString("There is already a tool cupboard in this region."));
                    event.setCanceled(true);
                    break;
                } else {
                    TileEntity cupboardEntity = world.getTileEntity(currentPosition);

                    if (cupboardEntity instanceof TileEntityToolCupboard){
                        boolean authorized = ((TileEntityToolCupboard) cupboardEntity).checkAuthorized(event.getPlayer().getName());
                        if (!authorized){
                            cancelPlacement = true;
                            event.getPlayer().addChatMessage(new TextComponentString("You are not authorized to build here."));
                            event.setCanceled(true);
                            world.setBlockToAir(event.getPos());
                        }
                    }
                }
            }
        }

        if (event.getPlacedBlock().getBlock() == ModBlocks.woodenToolCupboard){
            if (!cancelPlacement){
                modWorldSavedData.ToolCupboards.add(new BlockPos(event.getPos().getX(), event.getPos().getY(), event.getPos().getZ()));
                world.getPerWorldStorage().setData(cupboard_dataIdentifier, modWorldSavedData);
                modWorldSavedData.markDirty();
            }
        }
    }

    @SubscribeEvent
    public void blockBreakEvent(BlockEvent.BreakEvent event){
        World world = event.getWorld();
        ModWorldSavedData modWorldSavedData = (ModWorldSavedData) world.getPerWorldStorage().getOrLoadData(ModWorldSavedData.class, cupboard_dataIdentifier);

        if (modWorldSavedData == null) {
            modWorldSavedData = new ModWorldSavedData(cupboard_dataIdentifier);
        }

        if (event.getState().getBlock() == ModBlocks.woodenToolCupboard ){
            for(int i=0; i<modWorldSavedData.ToolCupboards.size(); i++){
                BlockPos currentCupboard = modWorldSavedData.ToolCupboards.get(i);
                if (event.getPos().getX() == currentCupboard.getX() && event.getPos().getY() == currentCupboard.getY() && event.getPos().getZ() == currentCupboard.getZ()){
                    modWorldSavedData.ToolCupboards.remove(i);
                    world.getPerWorldStorage().setData(cupboard_dataIdentifier, modWorldSavedData);
                    modWorldSavedData.markDirty();
                    break;
                }
            }
        }
    }

    @SubscribeEvent
    public void onPlayerRightClickBlock(PlayerInteractEvent.RightClickBlock event){
        if (!event.getWorld().isRemote){
            System.out.println("Right clicked with " + event.getItemStack().getUnlocalizedName());

            String targetName = null;

            for (int p=0; p<protectedBlockList.size(); p++){
                if (event.getWorld().getBlockState(event.getPos()).getBlock() == protectedBlockList.get(p)){
                    targetName = protectedBlockList.get(p).getLocalizedName();
                    break;
                }
            }

            for (int q=0; q<blacklistedItems.size(); q++){
                if (event.getItemStack().getItem() == blacklistedItems.get(q)){
                    targetName = blacklistedItems.get(q).getItemStackDisplayName(new ItemStack(blacklistedItems.get(q)));
                    break;
                }
            }

            if (targetName != null){
                World world = event.getWorld();
                ModWorldSavedData modWorldSavedData = (ModWorldSavedData) world.getPerWorldStorage().getOrLoadData(ModWorldSavedData.class, cupboard_dataIdentifier);

                if (modWorldSavedData == null) {
                    modWorldSavedData = new ModWorldSavedData(cupboard_dataIdentifier);
                }

                for (int i=0; i < modWorldSavedData.ToolCupboards.size(); i ++){
                    BlockPos currentPosition = modWorldSavedData.ToolCupboards.get(i);
                    if (event.getPos().getDistance(currentPosition.getX(), currentPosition.getY(), currentPosition.getZ()) < 16){
                        TileEntity cupboardEntity = world.getTileEntity(currentPosition);

                        if (cupboardEntity instanceof TileEntityToolCupboard){
                            boolean authorized = ((TileEntityToolCupboard) cupboardEntity).checkAuthorized(event.getEntityPlayer().getName());
                            if (!authorized){
                                event.getEntityPlayer().addChatMessage(new TextComponentString("You are not authorized to use " + targetName));
                                event.setCanceled(true);
                            }
                        }
                    }
                }
            }
        }
    }
}

 

This is the full version of the code. It runs without errors, but bucket of lava, water and blocks of chests can still be placed.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hello I must download mods  
    • How I Recovered Over €72,000 from a Scam Trading Broker in London with the Help of Adrian Lamo Hacker I’d like to share my recovery experience here from London, UK, with a scam trading broker to help others avoid falling into the same trap I did. Like many, I thought I was making a smart investment by trading online. I had heard stories of people earning substantial returns, so I was excited when I found a trading platform that seemed legitimate. However, little did I know, I was dealing with a scam. The broker I encountered was highly convincing. They promised me high returns and offered a professional-looking platform. I was persuaded by their smooth talk, testimonials, and fake success stories. Over time, I started transferring funds into the trading account they set up for me. My initial investments were small, but soon I transferred a significant amount—almost €72,620—hoping to see my account grow. Unfortunately, things took a sharp turn for the worse. Despite the early promises of returns, I was unable to withdraw any of my funds. Each time I requested to withdraw, I was met with endless excuses and delays. It became clear that I was dealing with a fraudulent broker, and my money was stuck in their fake account with no way of getting it back. I felt devastated and helpless. It was hard to believe that I had been scammed. However, after doing some research, I came across Adrian Lamo Hacker a company that specializes in recovering funds lost to scams. I was skeptical at first, but after reading positive reviews and testimonials, I decided to reach out for help. From the moment I contacted them through , the team was professional, understanding, and reassuring. They guided me through the recovery process step by step, and after some time, I was overjoyed to learn that my money had been successfully recovered. I’m incredibly grateful to Adrian Lamo Hacker for their expertise and hard work. They helped me get back what I thought was lost forever. If you’re reading this and have fallen victim to a similar scam, I urge you to reach out to a reputable recovery service like ADRIAN LAMO HACKER via Email: Adrianlamo @ consultant . com/ / Telegram ID: @ADRIANLAMOHACKERTECH Don’t give up on getting your money back. There are experts out there who can help, and I am proof that recovery is possible.
    • No se me descarga la carpeta de forge. Despues de darle a install y q me salte a la pagina de publicidad, espero los 5 segundos, le doy a skip pero en vez de descargarse del archivo del forge, se me descarga un .jar que es un bloc de notas, no me aparece la carpeta con la descarga por ningun lado 
    • Alright, well before removing IC2 I changed the max chunks from 15 to 6, and that seems to have fixed most of it, BUT, the console log keeps spamming out this "[16:01:09] [File IO Thread/WARN] [FML]: Large Chunk Detected: (11, 19) Size: 578 ./servermodworld2024oct/region/r.5.2.mca", and it is still lagging a little. What exactly does this mean and by some chance is this a chunk I can find and fix? If so how can I find it? After testing the server without IC2, the same error message still shows up, and the server continues to suffer in terms of lag.
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.