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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I tried do download the essential mod to my mod pack but i didnt work. I paly on 1.21 and it should work. I use neoforge for my modding. The weird things is my friend somehow added the mod to his modpack and many others that I somehow can´t. Is there anything i can do? 
    • Thanks, I've now installed a slightly newer version and the server is at least starting up now.
    • i have the same issue. Found 1 Create mod class dependency(ies) in createdeco-1.3.3-1.19.2.jar, which are missing from the current create-1.19.2-0.5.1.i.jar Found 11 Create mod class dependency(ies) in createaddition-fabric+1.19.2-20230723a.jar, which are missing from the current create-1.19.2-0.5.1.i.jar Detailed walkthrough of mods which rely on missing Create mod classes: Mod: createaddition-fabric+1.19.2-20230723a.jar Missing classes of create: com/simibubi/create/compat/jei/category/sequencedAssembly/JeiSequencedAssemblySubCategory com/simibubi/create/compat/recipeViewerCommon/SequencedAssemblySubCategoryType com/simibubi/create/compat/rei/CreateREI com/simibubi/create/compat/rei/EmptyBackground com/simibubi/create/compat/rei/ItemIcon com/simibubi/create/compat/rei/category/CreateRecipeCategory com/simibubi/create/compat/rei/category/WidgetUtil com/simibubi/create/compat/rei/category/animations/AnimatedBlazeBurner com/simibubi/create/compat/rei/category/animations/AnimatedKinetics com/simibubi/create/compat/rei/category/sequencedAssembly/ReiSequencedAssemblySubCategory com/simibubi/create/compat/rei/display/CreateDisplay Mod: createdeco-1.3.3-1.19.2.jar Missing classes of create: com/simibubi/create/content/kinetics/fan/SplashingRecipe
    • The crash points to moonlight lib - try other builds or make a test without this mod and the mods requiring it
    • Do you have shaders enabled? There is an issue with the mod simpleclouds - remove this mod or disable shaders, if enabled  
  • Topics

  • Who's Online (See full list)

    • There are no registered users currently online
×
×
  • Create New...

Important Information

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