ashjack Posted January 17, 2015 Share Posted January 17, 2015 So I have been updating a mod from 1.6.4 to 1.7.10, and some code that adds a custom class to an array when you place a block seems to add said class to an array twice. Here is the code: @Override public void onBlockAdded(World world, int i, int j, int k) { hasPlaced = true; Marker ma; markers.add(ma = new Marker(i, j, k, world.provider.dimensionId)); String markerCaption = ""; String helpText = ""; if (markers.size() == 1) { markerCaption = "Front-Left"; helpText = "You can place two more markers to mark out an area for a farm or mine etc. If you wish to do this, place another marker at the front-right position now"; System.out.println(markers.size()); } else if (markers.size() == 2) { markerCaption = "Front-Right"; helpText = "Finally, place a marker at the Rear-Left position"; System.out.println(markers.size()); } else if (markers.size() == 3) { markerCaption = "Rear-Left"; helpText = "You're done, now you can place down a mining box, farming box or right-click the front-left marker to copy a structure!"; System.out.println(markers.size()); } else { System.out.println(markers.size()); markerCaption = "Too many Markers!"; } if (markers.size() < 4) { V3 pos = new V3((double)i, (double)j, (double)k, world.provider.dimensionId); pos.y += 0.01d; if (ModSimukraft.configEnableMarkerAlignmentBeams) { EntityAlignBeam beam = new EntityAlignBeam(world); ma.caption = markerCaption; beam.setLocationAndAngles(pos.x, pos.y, pos.z, 0f, 0f); beam.yaw = 0f; if (!world.isRemote) { world.spawnEntityInWorld(beam); } ma.beams.add(beam); EntityAlignBeam beam2 = new EntityAlignBeam(world); beam2.setLocationAndAngles(pos.x, pos.y, pos.z, 90f, 0f); beam2.yaw = 90f; if (!world.isRemote) { world.spawnEntityInWorld(beam2); } ma.beams.add(beam2); EntityAlignBeam beam3 = new EntityAlignBeam(world); beam3.setLocationAndAngles(pos.x, pos.y, pos.z, 180f, 0f); beam3.yaw = 180f; if (!world.isRemote) { world.spawnEntityInWorld(beam3); } ma.beams.add(beam3); EntityAlignBeam beam4 = new EntityAlignBeam(world); beam4.setLocationAndAngles(pos.x, pos.y, pos.z, 270f, 0f); beam4.yaw = 270f; if (!world.isRemote) { world.spawnEntityInWorld(beam4); } ma.beams.add(beam4); } } if (!helpText.contentEquals("")) { ModSimukraft.sendChat(helpText); } super.onBlockAdded(world, i, j, k); } Quote Link to comment Share on other sites More sharing options...
diesieben07 Posted January 17, 2015 Share Posted January 17, 2015 Uhm, that sounds like a bad design. You should never have any fields in your Block class, those are global and shared across the entire server. Your Item is being added twice, because onBlockAdded is called on both client & server. Quote Link to comment Share on other sites More sharing options...
ashjack Posted January 17, 2015 Author Share Posted January 17, 2015 So I should check to see what side is being run, and only add it if it client sided? Quote Link to comment Share on other sites More sharing options...
diesieben07 Posted January 17, 2015 Share Posted January 17, 2015 It depends on what you want to achieve. Quote Link to comment Share on other sites More sharing options...
ashjack Posted January 17, 2015 Author Share Posted January 17, 2015 Well, only the person placing the markers should see the effects of them, so I guess that's the way to go. Quote Link to comment Share on other sites More sharing options...
diesieben07 Posted January 17, 2015 Share Posted January 17, 2015 No. Are the effects permanent? onBlockAdded is not the right method to use here. Quote Link to comment Share on other sites More sharing options...
ashjack Posted January 17, 2015 Author Share Posted January 17, 2015 The effects stay for as long as the block(s) exist Quote Link to comment Share on other sites More sharing options...
diesieben07 Posted January 17, 2015 Share Posted January 17, 2015 Then you will need something serverside. What effects are those exactly? Quote Link to comment Share on other sites More sharing options...
ashjack Posted January 17, 2015 Author Share Posted January 17, 2015 The effects are the spawning of a few lines to show how big an area will be, the area is marked out by the markers that are placed. Also, it checks to see how many markers have been placed, and any more than three means it will not work, however, since it adds two to the array, it thinks I have placed three when in fact I have only placed two Quote Link to comment Share on other sites More sharing options...
diesieben07 Posted January 17, 2015 Share Posted January 17, 2015 You will need to do this serverside. Use an IExtendedEntityProperties to keep track of which blocks the player has placed. Use onBlockPlacedBy to detect when it is placed. Quote Link to comment Share on other sites More sharing options...
ashjack Posted January 17, 2015 Author Share Posted January 17, 2015 I used @SideOnly(Side.SERVER) but the code doesn't run properly. Could I consider making a packet message for this function? Quote Link to comment Share on other sites More sharing options...
larsgerrits Posted January 17, 2015 Share Posted January 17, 2015 You will need to do this serverside. Use an IExtendedEntityProperties to keep track of which blocks the player has placed. Use onBlockPlacedBy to detect when it is placed. Quote Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support. 1.12 -> 1.13 primer by williewillus. 1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support. http://www.howoldisminecraft1710.today/ Link to comment Share on other sites More sharing options...
diesieben07 Posted January 17, 2015 Share Posted January 17, 2015 Why not to use @SideOnly Quote Link to comment Share on other sites More sharing options...
ashjack Posted January 17, 2015 Author Share Posted January 17, 2015 You will need to do this serverside. Use an IExtendedEntityProperties to keep track of which blocks the player has placed. Use onBlockPlacedBy to detect when it is placed. onBlockPlacedBy does not override any method. Quote Link to comment Share on other sites More sharing options...
diesieben07 Posted January 17, 2015 Share Posted January 17, 2015 What? Quote Link to comment Share on other sites More sharing options...
ashjack Posted January 17, 2015 Author Share Posted January 17, 2015 Surely if onBlockPlacedBy errors if you give it an @Override then the method doesn't already exist, meaning it won't be called when placing a block? I may be wrong, but that's what I think. Quote Link to comment Share on other sites More sharing options...
diesieben07 Posted January 17, 2015 Share Posted January 17, 2015 onBlockPlacedBy exists, you need to give it the right parameters. Don't override manually. You have an IDE for a reason. Quote Link to comment Share on other sites More sharing options...
ashjack Posted January 17, 2015 Author Share Posted January 17, 2015 Ah, okay, I understand now, if the parameters are not correct, Java sees it as a new function. However, this function is also run on both the integrated server and the client, and I still need to add a marker to the array. Quote Link to comment Share on other sites More sharing options...
diesieben07 Posted January 17, 2015 Share Posted January 17, 2015 Yes, that is correct. Check with world.isRemote, it is true on the client and false on the server. And again: that array is in the wrong place. Quote Link to comment Share on other sites More sharing options...
ashjack Posted January 17, 2015 Author Share Posted January 17, 2015 What do you mean it's in the wrong place? I'm really sorry that I seem so stupid, it's just I'm not very experienced with Java. C# is my specialty. Quote Link to comment Share on other sites More sharing options...
diesieben07 Posted January 17, 2015 Share Posted January 17, 2015 This is not java-specific (and besides that Java and C# are very similar). There is only one instance of your Block class. In singleplayer that instance is even shared between client & server. You cannot store any mutable data in there or you will run into trouble. Quote Link to comment Share on other sites More sharing options...
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.