Brickfix Posted March 8, 2015 Posted March 8, 2015 Hi, I want to have a block that basically triggers the following command every 10 or 20 ticks: /kill @e[type=Skeleton, c=1, r=5] I think the most efficient way would be to use the server commands but I don't find a way how to trigger the commands. I looked through the TileEntity of the commandBlock as well as the commandBlockLogic and couln't find a clue on how to do this. I know this sounds rather simple but I seem to be overlooking something Quote
Anon10W1z Posted March 9, 2015 Posted March 9, 2015 MinecraftServer.getServer().getCommandManager().executeCommand(player, command); Runs a command. Set your block to tick randomly and override the on random tick method. Quote Maker of the Craft++ mod.
Brickfix Posted March 9, 2015 Author Posted March 9, 2015 Thanks for the help, I tried to set it up and it works, I just had to add a TileEntity and a kind of CommandBlockLogic like class to make it work. Only problem is that the random tick is to rare IMO, is there a chance to increase it without changing gamerules? EDIT: So I have found two things that look promissing, but both do not seem to work out that well: The is the function TickSpeed, but I got no clue what it does, and changing it doesn't seem to make a difference, and in the TileEntity I could use the update function to have it happen every tick, but I do not know if the update function is fired every tick Again thanks for any help given Quote
TheGreyGhost Posted March 9, 2015 Posted March 9, 2015 If you're using a TileEntity, you can tell vanilla to call your TileEntity update() method every tick. in 1.8 make your TileEntity implement IUpdatePlayerListBox and implement update() in 1.7.10 just override updateEntity() and make sure canUpdate() returns true -TGG Quote
Brickfix Posted March 9, 2015 Author Posted March 9, 2015 Thanks, my TileEntity is now updating every tick. But now I ran into the next problem: Without changing the trigger method in my TotemLogic, the command is simply not executed. I know that the command works as it worked just fine using the block update methods. Here is the code of the tile entity public class TileEntityTotemBlock extends TileEntity implements IUpdatePlayerListBox { public int cooldown; private final TotemBlockLogic totemLogic = new TotemBlockLogic() { @Override public String getName() { // TODO Auto-generated method stub return "totem"; } @Override public IChatComponent getDisplayName() { return null; } @Override public void addChatMessage(IChatComponent message) { //We do not want to see a chat message } @Override public boolean canUseCommand(int permLevel, String commandName) { return true; } @Override public BlockPos getPosition() { return TileEntityTotemBlock.this.pos; } @Override public Vec3 getPositionVector() { return new Vec3((double)TileEntityTotemBlock.this.pos.getX() + 0.5D, (double)TileEntityTotemBlock.this.pos.getY() + 0.5D, (double)TileEntityTotemBlock.this.pos.getZ() + 0.5D); } @Override public World getEntityWorld() { return TileEntityTotemBlock.this.worldObj; } @Override public Entity getCommandSenderEntity() { // This is no entity and not important return null; } @Override public boolean sendCommandFeedback() { //We do not need this return false; } @Override public void setCommandStat(Type type, int amount) { // We do not need this } }; public int getCooldown() { return 20; } public TotemBlockLogic getTotemLogic() { return this.totemLogic; } public void writeToNBT(NBTTagCompound compound) { super.writeToNBT(compound); compound.setInteger("cooldown", this.cooldown); } public void readFromNBT(NBTTagCompound compound) { super.readFromNBT(compound); this.cooldown = compound.getInteger("cooldown"); } static { addMapping(TileEntityTotemBlock.class, "TotemBlock"); } public void update() { if (this.cooldown==0) { this.getTotemLogic().trigger(this.worldObj); this.cooldown = getCooldown(); } else { this.cooldown--; } } } And here of my TotemLogic: public abstract class TotemBlockLogic implements ICommandSender { public void trigger(World worldIn) { try { int i = MinecraftServer.getServer().getCommandManager().executeCommand(this, "/kill @e[type=Skeleton,r=5]"); System.out.println(i); } catch(Throwable t) { System.out.println("could not execute command"); } } } But thanks again for the help EDIT: Ok this is getting really weird: Apparently my code works 100% correctly. The only problem is that on loading a world where the block is already placed, nothing happens. If I set a new block, it works without any problem. Really strange. Quote
Anon10W1z Posted March 9, 2015 Posted March 9, 2015 EDIT: Ok this is getting really weird: Apparently my code works 100% correctly. The only problem is that on loading a world where the block is already placed, nothing happens. If I set a new block, it works without any problem. Really strange. Ah, this is because if you add a tile entity to your block, then blocks placed before the tile entity was added will not have the tile entity. Tile entities are only added when the block is placed. Quote Maker of the Craft++ mod.
Brickfix Posted March 9, 2015 Author Posted March 9, 2015 Yes I know that, but for some reason, every time I load this world the block is in, including the blocks placed after I added the TileEntity, the fired command does not work. As you can see in my TotemLogic, I have the integer i printed out after each attempt. The funny thing is that i is always 0 nevermind how many skeletons are walking around, but when I replace the block again it prints out the amount of monsters killed. Really weird EDIT: And to add on to the weirdness: If I only quit the world, and than enter it again without restarting minecraft it works without a problem! Quote
Recommended Posts
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.