1.15.2 [Solved] What are the long term effects of storing a block from an event?

Discussion in 'Spigot Plugin Development' started by AlexisEvelyn, Mar 29, 2020.

  1. I'm working on a plugin that will allow syncing redstone with the internet through the use of lecterns.

    I'm currently working on simplifying my code and making it easier to understand. Before, I just stored the location of the lectern and the player that "registered" the lectern of the plugin through the use of a writable/written book. While that worked, it did mean duplicating code through each event to check if the lectern has a book and it contains the correct data.

    I'm considering replacing the location with the Lectern object itself "org.bukkit.block.Lectern" and instead I'll store the Lectern inside the tracker class instance.

    To help explain, I have two classes for tracking lecterns. One is called LecternTrackers, which is a class designed to store a ConcurrentHashMap<Location, LecternTracker> and the other class is LecternTracker, which stores information on the lectern itself. This includes the player's uuid that "registered" the lectern (so I can apply settings based on user preferences), the last known redstone power level (to prevent spamming the MQTT server with the same redstone level and instead only send an update when the level changes), and the instance of the MQTT client used to send/receive data about redstone power levels.

    I specifically chose the lectern as it can send out an arbitrary redstone power level depending on what book page the lectern is flipped to, and using "lectern.getBlock().getBlockPower()", I can get the power level being sent into the lectern.

    Now, what I want to do is store the book's validation checking code in the LecternTracker instance so I can just have one location that can validate the lectern's book's metadata instead of copying the same 15-20 lines of code into each event that tracks the lectern.

    The events I'm referring to are PlayerInteractEvent and BlockPhysicsEvent. I can use PlayerInteractEvent to track when a player right clicks a book into the lectern so a new lectern can be registered without forcing the player to block update it. I use BlockPhysicsEvent to detect when the redstone power level changes as BlockRedstoneEvent doesn't get called for sending redstone signals into lecterns.

    So, my question is, what are the long term effects of storing the Lectern and other blocks in a class and then attempting to use them elsewhere (e.g. in another event)? Will unloading the chunk cause it to generate a null pointer, or what will happen? Thanks!
     
  2. org.bukkit.block classes store a snapshot, they all extend a BlockState which returns the same thing regardless of what happens to the block stored at that location (unless you yourself modify the state)

    I don’t fully understand what you’re doing but if you’re copying exactly the same code, it sounds like you should be extracting that code into a method you call when needed...?
     
  3. Thanks! I had the sudden realization a few minutes ago to actually just create a static utility class to call and then I can pass in the lectern. I'm just trying to clean up my code and make it easier to understand. After all, code's hard to work on if it's messy.

    Anyway, the gist of what I'm doing is, I'm taking advantage of the lectern's ability to send an arbitrary signal strength based on the current page number in the lectern. So, I'm using the lectern to send/receive redstone signal strengths between the internet and the lectern that the player wants to use for this purpose. Basically, the idea is, it's redstone that is connected to the internet.