JAVA编写MineCraft插件: 生成金字塔

    技术2026-04-23  17

    代码如下:

    package com.qwrdxer; import org.bukkit.Material; import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; import javax.swing.plaf.synth.SynthTextAreaUI; public class tower extends JavaPlugin{ @Override public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { if (cmd.getName().equalsIgnoreCase("CreatA")) { if (!(sender instanceof Player)) { sender.sendMessage("Only players can use this command!"); return true; } // 检测指令执行者是玩家后,我们便可以安全的进行强制转换类操作。 Player s = (Player) sender; //获取s的 x,y,z坐标 int x = (int)s.getLocation().getX(); int y = (int)s.getLocation().getY(); int z = (int)s.getLocation().getZ(); //获得接口 World world=s.getWorld(); //生成金字塔 int width=50; int high=0; while(true){ for(int a=0;a<width;a+=1){ for(int b=0;b<width;b+=1){ Block currentBlock = world.getBlockAt(x+a+high, y+high,z+b+high); currentBlock.setType(Material.GOLD_BLOCK); } } width-=2; high += 1; if(width<=0){ break; } } /* while True: for a in range(width): for b in range(width): mc.setBlock(x + a + high, pos.y + high, pos.z + b + high, block.GOLD_BLOCK.id) width -= 2 high += 1 if width <= 0: break */ return true; } return false; } }

    注:

    第一次学习Java编写MC的插件, 感觉非常有意思

    一开始使用的是mcpi(一个python编写MC插件的项目)后来发现代码写的死了,羊毛只能获取一种颜色的(因为作者使用的物品id值为int型, 而不同颜色的羊毛是id=35:x),

    项目在此https://github.com/brooksc/mcpipy

    翻了翻代码感觉改写好麻烦,就老老实找了找怎么用java编写插件,参考下面的博客配了一下环境

    https://blog.csdn.net/WYH2004_/article/details/106318325

    代码比较简单获取用户的x,y,z轴之后, 在使用for循环在用户身边不断生成金方块,最后成一个金字塔的形状, 当用户键入 CreatA 后就会在周围生成一个金字塔了!

    效果如下:

    获取用户的x,y,z轴之后, 在使用for循环在用户身边不断生成金方块,最后成一个金字塔的形状, 当用户键入 CreatA 后就会在周围生成一个金字塔了!

    效果如下:

    Processed: 0.010, SQL: 9