Jump to content

Recommended Posts

Posted

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.

Posted

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.

Posted

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);
		}
        }
    }

Posted

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/

Posted

 

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.

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.

Announcements



×
×
  • Create New...

Important Information

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