#[RESOLVED] HOW TO FIX DISABLE CONTAINER INTERACTION
1 messages · Page 1 of 1 (latest)
import { system, server } from '@minecraft/server';
let system = server.registerSystem(0, 0);
system.initialize = function() {
this.listenForEvent("minecraft:script_logger_config", (eventData) => this.onScriptLoggerConfig(eventData));
this.listenForEvent("minecraft:script_logger_config", (eventData) => this.onInteractWithContainer(eventData));
};
system.onInteractWithContainer = function(eventData) {
let player = eventData.data.player;
let container = eventData.data.container;
if (container) {
this.cancelEvent(eventData);
}
};
system.cancelEvent = function(eventData) {
eventData.cancel = true;
};
Here the script,my version is 1.21.21 pls help to fix it
When I run it it said server in module not export
I hope the code will let player can't interact with container and some block like trapdoor and bookshelf
** also I accept use any other way to made player can't use trapdoor and bookshelf**
From the looks of it. You probably use AI to create this code, which is a mess of incorrect and nonexistent functionality.
Yeah but cause my skill problem I can't coding by myself
So have another way to disable player interaction?
Let me list out the things...
- Import of
serverdoesn't exist, it's more likely to beworld - events for block interaction is still in beta, and it's called like this:
world.beforeEvents.playerInteractWithBlock```
The rest is just inccorect
Then how to reject interaction
Sorry, I just learned Minecraft code not long time ago
Aight, imma make a quick tutorial here, let's start with a simpler one.
Here's a list of afterEvents available in the beta API
In that, you'll find a list of properties corresponding to different types of events.
Here's an example of subscribing/listening to the event, block interaction, and saying "hello world" in the chat:
( it should run whenever you open a chest, ring a bell, or use crafting table. )
import { world } from "@minecraft/server";
world.afterEvents.playerInteractWithBlock.subscribe(()=>{
world.sendMessage("Hello World!");
})
Then how if I want to cancel it
To cancel it, you need an alternate version of the events. The beforeEvents, runs before the action happens.
world.beforeEvents.playerInteractWithBlock.subscribe((evd)=>{
evd.cancel = true;
})
(evd could by named anything here)
Thanks
@frozen hazel
Can I use
if(player.Hastag(admin)) to detect different players to assign permissions
can use
@final flare
import { world } from "@minecraft/server";
world.beforeEvents.playerInteractWithBlock.subscribe((evd)=>{
evd.cancel = true;
})
world.beforeEvents.playerBreakBlock.subscribe((evd)=>{
evd.cancel = true;
world.sendMessage({
rawtext: [
{ text: "§c不要破壞地圖!" }]});
player.playSound("random.hurt");
})
How to playsound when player break block
I do like this but system said "player" is not defind
@frozen hazel
player.playsound(`random.hurt`,{pitch:1,volume:1})
No the system said player is not defind
Not the playsound code problem
you need callback player in evd
How
const player = evd.player
Oh Thanks
@final flare but it said evd is not defind
import { world } from "@minecraft/server";
const evd.player = player
world.beforeEvents.playerInteractWithBlock.subscribe((evd)=>{
if (player.hasTag('WorldBuilder')) return;
evd.cancel = true;
})
world.beforeEvents.playerBreakBlock.subscribe((evd)=>{
if (player.hasTag('WorldBuilder')) return;
evd.cancel = true;
world.sendMessage({
rawtext: [
{ text: "§c不要破壞地圖!" }]});
player.playSound("random.hurt");
})
Do like this?
Debug result for [code](#1285872096106381312 message)
Compiler found 2 errors:
[36m<REPL0>.js[0m:[33m2[0m:[33m10[0m - [31merror[0m[30m TS1005: [0m',' expected.
[7m2[0m const evd.player = player
[7m [0m [31m ~[0m
``````ansi
[36m<REPL0>.js[0m:[33m2[0m:[33m20[0m - [31merror[0m[30m TS2448: [0mBlock-scoped variable 'player' used before its declaration.
[7m2[0m const evd.player = player
[7m [0m [31m ~~~~~~[0m
[36m<REPL0>.js[0m:[33m2[0m:[33m11[0m
[7m2[0m const evd.player = player
[7m [0m [36m ~~~~~~[0m
'player' is declared here.
ESLint results:
<REPL0>.js
2:9 error Parsing error: ',' expected
import { world } from "@minecraft/server";
world.beforeEvents.playerInteractWithBlock.subscribe((evd)=>{
const player = evd.player
if (player.hasTag('WorldBuilder')) return;
evd.cancel = true;
})
world.beforeEvents.playerBreakBlock.subscribe((evd)=>{
if (player.hasTag('WorldBuilder')) return;
evd.cancel = true;
world.sendMessage({
rawtext: [
{ text: "§c不要破壞地圖!" }]});
player.playSound("random.hurt");
})
Uh ok thanks my skill have big issue
if it's this error use:
- player.playSound(`random.hurt`)
+ system.run(()=>{player.playSound(`random.hurt`)})
Ok thanks
How
import { world } from "@minecraft/server";
world.beforeEvents.playerInteractWithBlock.subscribe((evd)=>{
const player = evd.player
if (player.hasTag('WorldBuilder')) return;
evd.cancel = true;
})
world.beforeEvents.playerBreakBlock.subscribe((evd)=>{
if (player.hasTag('WorldBuilder')) return;
evd.cancel = true;
world.sendMessage({
rawtext: [
{ text: "§c不要破壞地圖!" }]});
system.run(()=>{player.playSound(`random.hurt`)}) //new code
})
It still have issue
It said "player" not defind
lel.. send your code in here
Sure
import { world, system } from "@minecraft/server";
world.beforeEvents.playerInteractWithBlock.subscribe((evd)=>{
const player = evd.player
if (evd.player.hasTag('WorldBuilder')) return;
evd.cancel = true;
})
world.beforeEvents.playerBreakBlock.subscribe((evd)=>{
if (evd.player.hasTag('WorldBuilder')) return;
evd.cancel = true;
world.sendMessage({
rawtext: [
{ text: "§c不要破壞地圖!" }]});
system.run(()=>{player.playSound(`random.hurt`)})
})
What changed
world.beforeEvents.playerInteractWithBlock.subscribe((evd)=>{
const player = evd.player
if (player.hasTag('WorldBuilder')) return;
evd.cancel = true;
})
world.beforeEvents.playerBreakBlock.subscribe((evd)=>{
const player = evd.player // new code
if (player.hasTag('WorldBuilder')) return;
evd.cancel = true;
world.sendMessage({
rawtext: [
{ text: "§c不要破壞地圖!" }]});
system.run(()=>{player.playSound(`random.hurt`)})
})