Tracking TNT "Types"

Discussion in 'Spigot Plugin Development' started by WAS, Apr 16, 2017.

  1. WAS

    WAS

    I'm working on updating Blast Radius to have custom TNT. So I was working on a method to handle TNT being placed, primed, and eventually exploding. I'm just curious if my approach to tracking while maintaining a "type" (todo) is simple enough, and efficient enough?

    Code (Java):
    import java.util.Collection;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.UUID;

    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.entity.*;
    import org.bukkit.metadata.FixedMetadataValue;
    import org.bukkit.plugin.java.JavaPlugin;

    /*************************
    *
    *   Copyright (c) 2017 Jordan Thompson (WASasquatch)
    *  
    *   Permission is hereby granted, free of charge, to any person obtaining a copy
    *   of this software and associated documentation files (the "Software"), to deal
    *   in the Software without restriction, including without limitation the rights
    *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    *   copies of the Software, and to permit persons to whom the Software is
    *   furnished to do so, subject to the following conditions:
    *  
    *   The above copyright notice and this permission notice shall be included in all
    *   copies or substantial portions of the Software.
    *  
    *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    *   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    *   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    *   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    *   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    *   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    *   SOFTWARE.
    *  
    *************************/


    public class TNTEffectsManager {
     
       private static TNTEffectsManager instance;
       @SuppressWarnings("unused")
       private TNTEffects effects = new TNTEffects();
     
       private Map<UUID, Map<Location, String>> placedTNT = new HashMap<UUID, Map<Location, String>>();
     
       private TNTEffectsManager() {}
     
       public void addTNT(Player player, String type, World world, Location location) {
         if ( placedTNT.containsKey(player.getUniqueId()) ) {
           Map<Location, String> tnt = placedTNT.get(player.getUniqueId());
           tnt.put(location, type);
           placedTNT.put(player.getUniqueId(), tnt);
         } else {
           placedTNT.put(player.getUniqueId(), new HashMap<Location, String>(){
             private static final long serialVersionUID = 1L; {
             put(location, type);
           }});
         }
       }
     
       public TNTPrimed createPrimedTNT(String type, World world, Location location, int ticks, JavaPlugin plugin) {
         TNTPrimed tnt = world.spawn(location, TNTPrimed.class);
         tnt.setMetadata("tntType", new FixedMetadataValue(plugin, type));
         tnt.setFuseTicks(ticks);
         return tnt;
       }
     
       public static TNTEffectsManager getInstance( ) {
         return instance;
       }
     
       public Collection<Location> getLocations(UUID uuid) {
         Collection<Location> locs = Collections.emptySet();
         if ( placedTNT.containsKey(uuid) ) {
           for ( Location loc : placedTNT.get(uuid).keySet() ) {
             locs.add(loc);
           }
         }
         return locs;
       }
       
      public void removeAllPlayersTNT(UUID uuid, Location loc) {
         if ( placedTNT.containsKey(uuid) ) {
           placedTNT.put(uuid, new HashMap<Location, String>());
         }
       }
       
       public void removeAllTNT(UUID uuid, Location loc) {
         placedTNT = new HashMap<UUID, Map<Location, String>>();
       }
       
       public void removePlayersTNT(UUID uuid, Location location, String type) {
         if ( placedTNT.containsKey(uuid) ) {
           if ( type != null ) {
             placedTNT.get(uuid).remove(location, type);
           } else {
             placedTNT.get(uuid).remove(location);
           }
         }
       }
       
       public void removePlayersTNT(UUID uuid, Location location) {
         removePlayersTNT(uuid, location, null);
       }
     
    }
     
    #1 WAS, Apr 16, 2017
    Last edited: Apr 16, 2017
  2. The only problem I see is that you aren't removing the tnt from the Map after it blew up
     
  3. WAS

    WAS

    Yeah I havent even worked on that aspect of the plugin yet. Been working on tracking and effects from file. It should be just a matter of a remove method. Thanks for pointing that put though.

    Updated with those missing methods.
     
    #3 WAS, Apr 16, 2017
    Last edited: Apr 16, 2017
  4. OFF-TOPIC: Why aren't you using the constructor to initialize the private fields? Wouldn't that be considered correct in Java conventions?
     
  5. WAS

    WAS

    From what I understand that is something you would be following if you had more than one instance. For example B extends A and sets a private variable, but both are instantiated at once. Since you cannot get more than one instance of this class I see no reason to do that. Additionally I would be adding creation methods for pretty much no reason. Finally, it's up to the developers sense of logic and has no actual definitive standard and is just a means to human-readability.