# Creating a Cable

## Recommended Posts

Hi!

I am totally new to all this

First of all i want to tell you that my English is not really good. so please don't hate

I wonder how i can check if a cable is connected to a specific block.

I don't know if there is already a way to do it or if i have to do it from scratch because i didn't found anything about this.

So maybe that doesn't make any sense but i had some ideas how i could do it:

I created a cable block and a method to get a list of the blocks next to it (to check if they are connected to another cable block)

So my idea was to get the blocks next to every cable block i find and add the blocks coordinates to a list.

I wanted to repeat this again and again till there are no more cable blocks connected that aren't in the list.

And then when this is done i wanted to check if one of this cables is next to the target block.

Does this make any sense or is there a much better and easier way to do it?

And if not how can i do the way that i explained?

I am happy about every help

- MrFincher

##### Share on other sites

Hi

Yes that basic idea should work.  You need to be a bit clever about it though.  When you find a cable, you need to check that you didn't find it before.  Otherwise you end up with an infinite loop.

This is a pretty standard flood fill algorithm and a bit of google will probably find you some efficient code you can base yours on.

-TGG

##### Share on other sites

Hi!

I am totally new to all this

First of all i want to tell you that my English is not really good. so please don't hate

I wonder how i can check if a cable is connected to a specific block.

I don't know if there is already a way to do it or if i have to do it from scratch because i didn't found anything about this.

So maybe that doesn't make any sense but i had some ideas how i could do it:

I created a cable block and a method to get a list of the blocks next to it (to check if they are connected to another cable block)

So my idea was to get the blocks next to every cable block i find and add the blocks coordinates to a list.

I wanted to repeat this again and again till there are no more cable blocks connected that aren't in the list.

And then when this is done i wanted to check if one of this cables is next to the target block.

Does this make any sense or is there a much better and easier way to do it?

And if not how can i do the way that i explained?

I am happy about every help

- MrFincher

Yes, this should work, however another option commonly used is as follows:

1: When a cable is placed, check if there are cabled next to it.

2: If no cables are next to it, create a custom TileEntity there. This will be the master tile entity for that system. Don't proceed farther down the logic.

3: If there were cables attached, get the tile entity at that location and find it's master.

4: Create a tile entity at the location of the placed cable, and set it's master to the master of the cables that are attached.

5: If the cables have different masters, we need to combine them into one system. Choose one of the systems and set all the

When a cable is broken, you need to separate the cables into separate systems:

1: Iterate through the cables attached to each of the cabled next to the one that was broken, check if they are still connected to their master. If not, set them as the master of their own system, otherwise return and stop doing the next logic.

2: Iterate through the connected cables that were made into their own system and set their master to the new one.

To check is a block is connected to the system, get the master of the cable next to the machine and see if that tile entity has certain data.

The tile entity would store the following:

TileEntity master

boolean isMaster

int powerInSystem

etc...

The code would be similar to the following:

```public void addCable(World w, int x, int y, int z)
{
// Create your tile entity here and add it to the world
...
// Rest of code
List<TileEntityPowerSystem> systems= getConnectedWires(w, x, y, z);
if(systems.size() == 0)
{
tileEntity.setIsMaster(true);
return;
}
TileEntityPowerSystem master = systems.get(0).getMaster();// Select the first system as the master system

tileEntity.setMaster(master)

for(TileEntityPowerSystem system : systems)
{
if(system != master)
{
system.getMaster().setMaster(master);// Make the master tile entities of the other systems no longer masters
// And set their master to the new master.
}
}
}

public int getPower(World w, int x, int y, int z)
{
TileEntity te = w.getTileEntityAt(x, y, z);
if(te instanceof TileEntityPowerSystem)
{
return ((TileEntityPowerSystem)te).getMaster().getPowerInSystem();
}
return 0;
}```

etc...

This method requires using more, smaller, tile entities than one single one which holds all the information. In return it makes al operations faster, instead of having the cylce through or iterate through all the connected blocks, you can use stored data to access directly the tile entity that has been put in charge of storing the main data for the system.

```public TileEntityPowerSystem getMaster()
{
if(!this.master.isMaster)
{
this.master = this.master.getMaster();// If the blocks previous master was merged with another system, update it now
}
return this.master;
}```

I don't have the development environment up atm, but this method probably isn't completely safe with chunk loading/unloading. You may want to store positions for the master instead of the actual tile entity to avoid issues, and maybe have the tile entities store a n instance of a object called System (or whatever) that stores the actual data instead of the tile entity, so that it can be access from anywhere, even if the master is unloaded.

##### Share on other sites

Woow thx for the fast and helpful replies!

I will try the technique with the master cables but i have just one more question:

How does the getConnectedWires() methods work?

##### Share on other sites

checks the 6 adjacent sides of the cable in question and finds any blocks that are of your cable type. If it's of your cable type then get the tile entity at that location via world.getTileEntity(x, y, z) and add it to a list.

## 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.

Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.

×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

• ### Posts

• हथियार व्यापारियों की चमक आज के समय में भारत और दुनिया के विभिन्न हिस्सों में तेजी से बढ़ रही है। स्वीडन की स्थित संस्था सिपरी की नवीनतम रिपोर्ट के अनुसार, विश्व भर में विभिन्न देश अब पूर्व कभी नहीं देखे गए हथियारों और सैन्य सामग्रियों पर धन का व्यय कर रहे हैं। वैश्विक संघर्षों के बढ़ते संकेतों के कारण, विश्व के कई हिस्सों में युद्ध की संभावनाएं बढ़ रही हैं। पश्चिमी एशिया में युद्ध की आग फैल रही है, जबकि दक्षिण चीन सागर और ताइवान में भी तनाव बढ़ रहा है। भारत-चीन सीमा पर भी स्थिति चिंताजनक है। इन घटनाओं के परिणामस्वरूप, विभिन्न देशों की प्राथमिकताएं बदल रही हैं, और हथियार व्यापारियों का धंधा आजकल चमक उठा है। सिपरी की रिपोर्ट के अनुसार, 2023 में वैश्विक सैन्य खर्च एक नया रिकॉर्ड स्थापित किया गया है। 2022 की तुलना में, 2023 में सैन्य खर्च 6.8 फीसदी बढ़कर 24.4 ट्रिलियन डॉलर पर पहुंच गया। इसके पीछे कुछ विशेष कारण हैं, जिनमें दुनिया भर में तनाव बढ़ना और अद्यतित सैन्य तैयारियों की आवश्यकता शामिल है। इस वृद्धि में, भारत जैसे देशों का योगदान भी अधिक है। भारत ने भी अपने सैन्य खर्चों में वृद्धि की है, और यह दुनिया के सबसे बड़े और सबसे प्रभावशाली राष्ट्रों में एक बन गया है। इसके अलावा, अमेरिका, चीन, और रूस भी सैन्य खर्च में वृद्धि की हैं, जिससे विश्व सुरक्षा में नए दायरे खुले हैं। संभावनाओं के इस नए मंच पर, दुनिया के सक्षम देश अब अपने सैन्य तैयारियों में ज्यादा ध्यान दे रहे हैं। यह वृद्धि विकास और प्रगति के बजट पर भी प्रभाव डाल रही है, जिससे विभिन्न देशों के आर्थिक प्रगति में बदलाव आ सकता है।   https://www.nayaindia.com/editorial-column/editorial1/arms-sales-increased-in-the-world-454072.html
• Add the crash-report or latest.log (logs-folder) with sites like https://paste.ee/ and paste the link to it here
• In your config folder, delete the sophisticatedcore-common.toml file and test it again

• ### Who's Online (See full list)

×

• #### Activity

×
• Create New...