Coder Social home page Coder Social logo

minecraftconnection's Introduction

MinecraftConnection

Nuget (with prereleases) Nuget GitHub

日本語版はこちら

MinecraftConnection is a library for sending commands via RCON using C# to help you learn and automate your programming. It can be run on a vanilla server as well as a Spigot server, including plugins. Before running the program, you need to start a Minecraft server that allows RCON connections.

1. Preparation

First, download the Minecraft Server software and run it in any game directory. A server configuration file called server.properties will be created. Specify the password and port number for the RCON connection, and enable the connection.

Configuration example :

rcon.port=25575
rcon.password=minecraft
enable-rcon=true

After adding it save the file and restart the server. Launch Minecraft launcher and log in to the server.

2. Create Project

This library is intended for .NET Standard 2.1 and above. This section describes how to create a .NET 6 console application.

Install MinecraftConnection with the NuGet package manager, or run the following command in the package manager console.

Install-Package MinecraftConnection

dotnet command:

dotnet add package MinecraftConnection

Detail:https://www.nuget.org/packages/MinecraftConnection

3. Sample programs

To run the program, start Minecraft Server and Minecraft itself (already logged in to the server).
Top-level statements are used here.

3.1 Set the time to 0

using MinecraftConnection;

// IP address or DNS name.
string address = "127.0.0.1";
ushort port = 25575;
string pass = "minecraft";
MinecraftCommands command = new MinecraftCommands(address, port, pass);

command.TimeSet(0);

3.2 Set off fireworks

using MinecraftConnection;
using MinecraftConnection.Entity;

string address = "127.0.0.1";
ushort port = 25575;
string pass = "minecraft";
MinecraftCommands command = new MinecraftCommands(address, port, pass);

// Define the coordinates at which you want to launch
Position pos = new Position(-516, 64, -205);
// Make a fireworks
Fireworks fireworks = new Fireworks()
{
    LifeTime = 30, // Time to explosion
    Type = FireworkType.LargeBall, // Fireworks type
    Colors = FireworkOption.RandomColor(), // Fireworks color (RandomColor() is get random color)
    FadeColors = new List<FireworkColors> { FireworkColors.WHITE }, // after explosion color
};
// Set off fireworks at defined position
command.SetOffFireworks(pos, fireworks);

Result :

Various fireworks can be set off depending on your ingenuity. If you would like to try it, please refer to this page.

https://zenn.dev/takunology/scraps/9462b03d13dd0a

3.3 Sorting items in a chest

You can retrieve items in a chest and sort them by ID or by count.
The following source code allows you to sort items by name by using the SortById() method on the retrieved item data. The MinecraftConnection.Extends directive declaration is required to use the sorting method.

using MinecraftConnection;
using MinecraftConnection.Extends;

string address = "127.0.0.1";
ushort port = 25575;
string pass = "minecraft";
MinecraftCommands command = new MinecraftCommands(address, port, pass);

// Declare the coordinates of the "Chest block" or "Shulker box".
var pos = new Position(-502, 63, -213);
// Get the items in the chest.
var chestitems = command.GetChestItems(pos);
// Sort the acquired items and overwrite them in the chest again.
command.SetChestItems(pos, chestitems.SortByIdDescending());

Result:

3.4 Play sound

The PlaySound() method can be used to play Minecraft sound effects. Sound effects are stored in the Sound enumeration.

ushort sub = 230;
ushort main = 230;

for(int i = 0; i < 3; i++)
{
    command.PlaySound(Sound.Bell);
    command.Wait(430);
}

command.PlaySound(Sound.CowBell);
command.Wait(430);

for (int i = 0; i < 4; i++)
{
    command.PlaySound(Sound.BaseDrum);
    command.Wait(sub);
    command.PlaySound(Sound.Hat);
    command.Wait(main);

    command.PlaySound(Sound.BaseDrum);
    command.PlaySound(Sound.Snare);
    command.Wait(sub);
    command.PlaySound(Sound.Hat);
    command.Wait(main);

    command.PlaySound(Sound.BaseDrum);
    command.Wait(sub);
    command.PlaySound(Sound.Hat);
    command.Wait(main);
    
    command.PlaySound(Sound.BaseDrum);
    command.PlaySound(Sound.Snare);
    command.Wait(sub);
    command.PlaySound(Sound.Hat);
    command.Wait(main);
}

Running example at X (Twitter)

https://twitter.com/takunology_net/status/1695049583615963590?s=20

4. Caution

The stop command is disabled due to the risk of stopping the server by RCON remote control. Executing the stop command in SendCommand method will raise an exception.


Project Detail: https://www.mcwithcode.com/

minecraftconnection's People

Contributors

drumath2237 avatar takunology avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

minecraftconnection's Issues

リファレンスを準備する

Google Analytics を見ると、リファレンスを参照するためにサイトアクセスしているのに全然更新がない。

...早くリファレンスを充実させたほうがいい。

RandomColors メソッドの例外がスローされる

例えば下記のように RandomColors() メソッドで複数の色を取得してインスタンスを作るまでは問題ない。

Fireworks fw = new Fireworks()
{
    LifeTime = 30,
    Type = FireworkType.LargeBall,
    Colors = FireworkOption.RandomColors(2),
};

しかし、花火を打ち上げようと SetOffFireworks() を使用すると例外がスローされる

Unhandled exception. System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'startIndex')
   at System.BitConverter.ToInt32(Byte[] value, Int32 startIndex)
   at MinecraftConnection.RCON.Encoder.DecodePacket(Byte[] bytes)
   at MinecraftConnection.RCON.MinecraftRCON.SendPacket(Packet request, Packet& response)
   at MinecraftConnection.RCON.MinecraftRCON.SendCommand(String command)
   at MinecraftConnection.MinecraftCommands.SetOffFireworks(Double x, Double y, Double z, Fireworks fireworks)
   at Program.<Main>$(String[] args) in C:\Users\takun\Desktop\App\Program.cs:line 16

チェスト内のアイテムソートに関するミス

チェスト内のアイテムの並べ替えに関するメソッド内の処理にミスがある。

 items.OrderBy(x => x.Count).ToList(); の表記が2つあるが、もう一方は OrderByDescending(x => x.Count) が正しい。

Biggest bottleneck?

Hi, first thing I want to say is thank you for making this. This library is everything I ever wanted. Super simple and does not limit you in any way.

There are a couple of things I would like to know. And it's mainly about speed.
What can be the biggest bottleneck? I'm making Conway's game of Life in Minecraft and when trying to do a 100x100 grid, it starts to get slow. When I run it in the C# console it's pretty fast. Is it the library that can only send x amount of commands to the server? Is it the server that can only receive x amount of requests? Is there a setting I can change in my server properties to increase the speed?

Also would multithreading my application work? Or would the server still only receive x amount of requests per second?
(I'm sorry if this is the wrong place to ask this, I tried to mark this as a question but can't seem to find it)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.