Search in sources :

Example 1 with SignTranslateEvent

use of net.countercraft.movecraft.events.SignTranslateEvent in project Movecraft by APDevTeam.

the class CraftRotateCommand method sendSignEvents.

private void sendSignEvents() {
    Object2ObjectMap<String[], List<MovecraftLocation>> signs = new Object2ObjectOpenCustomHashMap<>(new Hash.Strategy<String[]>() {

        @Override
        public int hashCode(String[] strings) {
            return Arrays.hashCode(strings);
        }

        @Override
        public boolean equals(String[] a, String[] b) {
            return Arrays.equals(a, b);
        }
    });
    Map<MovecraftLocation, Sign> signStates = new HashMap<>();
    for (MovecraftLocation location : craft.getHitBox()) {
        Block block = location.toBukkit(craft.getWorld()).getBlock();
        BlockState state = block.getState();
        if (state instanceof Sign) {
            Sign sign = (Sign) block.getState();
            if (!signs.containsKey(sign.getLines()))
                signs.put(sign.getLines(), new ArrayList<>());
            signs.get(sign.getLines()).add(location);
            signStates.put(location, sign);
        }
    }
    for (Map.Entry<String[], List<MovecraftLocation>> entry : signs.entrySet()) {
        SignTranslateEvent event = new SignTranslateEvent(craft, entry.getKey(), entry.getValue());
        Bukkit.getServer().getPluginManager().callEvent(event);
        if (!event.isUpdated()) {
            continue;
        }
        for (MovecraftLocation location : entry.getValue()) {
            Block block = location.toBukkit(craft.getWorld()).getBlock();
            BlockState state = block.getState();
            BlockData data = block.getBlockData();
            if (!(state instanceof Sign)) {
                continue;
            }
            Sign sign = signStates.get(location);
            for (int i = 0; i < 4; i++) {
                sign.setLine(i, entry.getKey()[i]);
            }
            sign.update(false, false);
            block.setBlockData(data);
        }
    }
}
Also used : Object2ObjectOpenCustomHashMap(it.unimi.dsi.fastutil.objects.Object2ObjectOpenCustomHashMap) HashMap(java.util.HashMap) Object2ObjectOpenCustomHashMap(it.unimi.dsi.fastutil.objects.Object2ObjectOpenCustomHashMap) ArrayList(java.util.ArrayList) SignTranslateEvent(net.countercraft.movecraft.events.SignTranslateEvent) Hash(it.unimi.dsi.fastutil.Hash) BlockState(org.bukkit.block.BlockState) Block(org.bukkit.block.Block) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) Sign(org.bukkit.block.Sign) MovecraftLocation(net.countercraft.movecraft.MovecraftLocation) BlockData(org.bukkit.block.data.BlockData) Object2ObjectMap(it.unimi.dsi.fastutil.objects.Object2ObjectMap) HashMap(java.util.HashMap) Map(java.util.Map) Object2ObjectOpenCustomHashMap(it.unimi.dsi.fastutil.objects.Object2ObjectOpenCustomHashMap)

Example 2 with SignTranslateEvent

use of net.countercraft.movecraft.events.SignTranslateEvent in project Movecraft by APDevTeam.

the class CraftTranslateCommand method sendSignEvents.

private void sendSignEvents() {
    Object2ObjectMap<String[], List<MovecraftLocation>> signs = new Object2ObjectOpenCustomHashMap<>(new Hash.Strategy<String[]>() {

        @Override
        public int hashCode(String[] strings) {
            return Arrays.hashCode(strings);
        }

        @Override
        public boolean equals(String[] a, String[] b) {
            return Arrays.equals(a, b);
        }
    });
    Map<MovecraftLocation, Sign> signStates = new HashMap<>();
    for (MovecraftLocation location : craft.getHitBox()) {
        Block block = location.toBukkit(craft.getWorld()).getBlock();
        if (!Tag.SIGNS.isTagged(block.getType())) {
            continue;
        }
        BlockState state = block.getState();
        if (state instanceof Sign) {
            Sign sign = (Sign) state;
            if (!signs.containsKey(sign.getLines()))
                signs.put(sign.getLines(), new ArrayList<>());
            signs.get(sign.getLines()).add(location);
            signStates.put(location, sign);
        }
    }
    for (Map.Entry<String[], List<MovecraftLocation>> entry : signs.entrySet()) {
        SignTranslateEvent event = new SignTranslateEvent(craft, entry.getKey(), entry.getValue());
        Bukkit.getServer().getPluginManager().callEvent(event);
        if (!event.isUpdated()) {
            continue;
        }
        for (MovecraftLocation location : entry.getValue()) {
            Block block = location.toBukkit(craft.getWorld()).getBlock();
            BlockState state = block.getState();
            if (!(state instanceof Sign)) {
                continue;
            }
            Sign sign = signStates.get(location);
            for (int i = 0; i < 4; i++) {
                sign.setLine(i, entry.getKey()[i]);
            }
            sign.update(false, false);
        }
    }
}
Also used : Object2ObjectOpenCustomHashMap(it.unimi.dsi.fastutil.objects.Object2ObjectOpenCustomHashMap) HashMap(java.util.HashMap) Object2ObjectOpenCustomHashMap(it.unimi.dsi.fastutil.objects.Object2ObjectOpenCustomHashMap) ArrayList(java.util.ArrayList) SignTranslateEvent(net.countercraft.movecraft.events.SignTranslateEvent) Hash(it.unimi.dsi.fastutil.Hash) BlockState(org.bukkit.block.BlockState) Block(org.bukkit.block.Block) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) Sign(org.bukkit.block.Sign) MovecraftLocation(net.countercraft.movecraft.MovecraftLocation) Object2ObjectMap(it.unimi.dsi.fastutil.objects.Object2ObjectMap) HashMap(java.util.HashMap) Map(java.util.Map) Object2ObjectOpenCustomHashMap(it.unimi.dsi.fastutil.objects.Object2ObjectOpenCustomHashMap)

Aggregations

Hash (it.unimi.dsi.fastutil.Hash)2 Object2ObjectMap (it.unimi.dsi.fastutil.objects.Object2ObjectMap)2 Object2ObjectOpenCustomHashMap (it.unimi.dsi.fastutil.objects.Object2ObjectOpenCustomHashMap)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Map (java.util.Map)2 MovecraftLocation (net.countercraft.movecraft.MovecraftLocation)2 SignTranslateEvent (net.countercraft.movecraft.events.SignTranslateEvent)2 Block (org.bukkit.block.Block)2 BlockState (org.bukkit.block.BlockState)2 Sign (org.bukkit.block.Sign)2 BlockData (org.bukkit.block.data.BlockData)1