上次@无敌三脚猫 大佬给我写了一份crt的使用物品触发指令的脚本,但是使用后不消耗物品,我现在需要一个使用后消耗物品的代码,需要在这个代码的基础上作出修改
复制代码
- import crafttweaker.api.world.ServerLevel;
- import crafttweaker.api.events.CTEventManager;
- import crafttweaker.api.data.MapData;
- import crafttweaker.api.event.entity.player.interact.PlayerInteractEvent;
- import crafttweaker.api.event.entity.player.interact.RightClickItemEvent;
- import crafttweaker.api.event.entity.player.interact.RightClickBlockEvent;
- public function itemInteract(e:PlayerInteractEvent):void{
- val player=e.player;
- val world=player.level;
- val item=e.itemStack;
- if(!world.isClientSide&&item.definition==<item:minecraft:fern>.definition&&item.hoverName.contents=="我是个苹果"){
- val tag=new MapData(item.tag.getAt("display").asMap());
- if(tag.contains("Lore")&&"我真的是苹果" in tag.getAt("Lore").getString()){
- (world as ServerLevel).server.executeCommand("give "+player.name.contents+" apple",true);
- e.cancellationResult=<constant:minecraft:world/interactionresult:success>;
- e.cancel();
- }
- }
- }
- CTEventManager.register<RightClickItemEvent>((e)=>{itemInteract(e);});
- CTEventManager.register<RightClickBlockEvent>((e)=>{itemInteract(e);});
在上次的帖子里你就可以问的
在e.cancel();的后面(前面也可以)加一行复制代码就好
在e.cancel();的后面(前面也可以)加一行
- player.setItemInHand(e.hand,item.shrink(1));
无敌三脚猫 发表于 2023-4-3 19:13
在上次的帖子里你就可以问的
在e.cancel();的后面(前面也可以)加一行就好
谢谢大佬
无敌三脚猫 发表于 2023-4-3 13:10
在上次的帖子里你就可以问的
在e.cancel();的后面(前面也可以)加一行就好
- import crafttweaker.api.world.ServerLevel;
- import crafttweaker.api.events.CTEventManager;
- import crafttweaker.api.data.MapData;
- import crafttweaker.api.event.entity.player.interact.PlayerInteractEvent;
- import crafttweaker.api.event.entity.player.interact.RightClickItemEvent;
- import crafttweaker.api.event.entity.player.interact.RightClickBlockEvent;
- public function itemInteract(e:PlayerInteractEvent):void{
- val player=e.player;
- val world=player.level;
- val item=e.itemStack;
- if(!world.isClientSide&&item.definition==<item:minecraft:fern>.definition&&item.hoverName.contents=="我是个苹果"){
- val tag=new MapData(item.tag.getAt("display").asMap());
- if(tag.contains("Lore")&&"我真的是苹果" in tag.getAt("Lore").getString()){
- (world as ServerLevel).server.executeCommand("give "+player.name.contents+" apple",true);
- e.cancellationResult=<constant:minecraft:world/interactionresult:success>;
- e.cancel();
- import crafttweaker.api.world.ServerLevel;
- import crafttweaker.api.events.CTEventManager;
- import crafttweaker.api.data.MapData;
- import crafttweaker.api.event.entity.player.interact.PlayerInteractEvent;
- import crafttweaker.api.event.entity.player.interact.RightClickItemEvent;
- import crafttweaker.api.event.entity.player.interact.RightClickBlockEvent;
- public function itemInteract(e:PlayerInteractEvent):void{
- val player=e.player;
- val world=player.level;
- val item=e.itemStack;
- if(!world.isClientSide&&item.definition==<item:minecraft:fern>.definition&&item.hoverName.contents=="我是个玻璃"){
- val tag=new MapData(item.tag.getAt("display").asMap());
- if(tag.contains("Lore")&&"我真的是玻璃" in tag.getAt("Lore").getString()){
- (world as ServerLevel).server.executeCommand("give "+player.name.contents+" glass",true);
- e.cancellationResult=<constant:minecraft:world/interactionresult:success>;
- e.cancel();
yaoraoxiaosa 发表于 2023-4-12 13:00
大佬,我想添加另一个,新开了一个zs脚本,但是前一个就失效了,是不是应该写在一起啊,怎么写啊?谢谢大佬 ...
我不知道你那是怎么回事
不过这样写太繁杂了,可以考虑不依赖lore和名字,而是靠别的单独的nbt,像是这样
- import crafttweaker.api.world.ServerLevel;
- import crafttweaker.api.events.CTEventManager;
- import crafttweaker.api.event.entity.player.interact.PlayerInteractEvent;
- import crafttweaker.api.event.entity.player.interact.RightClickItemEvent;
- import crafttweaker.api.event.entity.player.interact.RightClickBlockEvent;
- public function itemInteract(e:PlayerInteractEvent):void{
- val player=e.player;
- val world=player.level;
- val item=e.itemStack;
- if(!world.isClientSide&&item.definition==<item:minecraft:fern>.definition&&item.tag!=null){
- val tag=item.tag;
- if(tag.contains("give")){
- (world as ServerLevel).server.executeCommand("give "+player.name.contents+" "+tag.getAt("give").getString(),true);
- e.cancellationResult=<constant:minecraft:world/interactionresult:success>;
- e.cancel();
- player.setItemInHand(e.hand,item.shrink(1));
- }
- }
- }
- CTEventManager.register<RightClickItemEvent>((e)=>{itemInteract(e);});
- CTEventManager.register<RightClickBlockEvent>((e)=>{itemInteract(e);});
- /give @s fern{display:{Name:'{"text":"我是个苹果","italic":false,"color":"gold"}',Lore:['{"text":"我真的是苹果","italic":"false","color":"gold"}']},give:"apple"}
- /give @s fern{display:{Name:'{"text":"我是个玻璃","italic":false,"color":"gold"}',Lore:['{"text":"我真的是玻璃","italic":"false","color":"gold"}']},give:"glass"}
无敌三脚猫 发表于 2023-4-12 13:55
我不知道你那是怎么回事
不过这样写太繁杂了,可以考虑不依赖lore和名字,而是靠别的单独的nbt,像是这样 ...
还是大佬牛,我刚开始学这个,还不知道能这么写呢,谢谢大佬