Solved Remove every group from a player.

Discussion in 'BungeeCord Plugin Development' started by Vytska111, Feb 11, 2019.

  1. Hi Bukkit, I am trying to remove every group that a player has, and put one.
    My code:
    Code (Text):
    player.getGroups().forEach(group->{
         player.removeGroups(group);
    });
    But it seems to give me an error.
    Code (Text):
    java.util.ConcurrentModificationException
        at gnu.trove.impl.hash.THashIterator.nextIndex(THashIterator.java:156)
        at gnu.trove.impl.hash.THashIterator.hasNext(THashIterator.java:103)
        at java.lang.Iterable.forEach(Unknown Source)
        at java.util.Collections$UnmodifiableCollection.forEach(Unknown Source)
        at net.VytskaLT.bungeeGroupChanger.main.remuv(main.java:21)
        at net.VytskaLT.bungeeGroupChanger.main.onPluginMessage(main.java:45)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at net.md_5.bungee.event.EventHandlerMethod.invoke(EventHandlerMethod.java:19)
        at net.md_5.bungee.event.EventBus.post(EventBus.java:46)
        at net.md_5.bungee.api.plugin.PluginManager.callEvent(PluginManager.java:397)
        at net.md_5.bungee.connection.DownstreamBridge.handle(DownstreamBridge.java:246)
        at net.md_5.bungee.protocol.packet.PluginMessage.handle(PluginMessage.java:88)
        at net.md_5.bungee.netty.HandlerBoss.channelRead(HandlerBoss.java:104)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)
        at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:102)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)
        at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:102)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)
        at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:323)
        at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:297)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)
        at io.netty.handler.timeout.IdleStateHandler.channelRead(IdleStateHandler.java:286)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)
        at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1434)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
        at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:965)
        at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163)
        at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:656)
        at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:591)
        at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:508)
        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:470)
        at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:909)
        at java.lang.Thread.run(Unknown Source)
    The error seems to be on that forEach.
    How can I fix this?
     
  2. A Java thing: you can't edit an array that you're using right now.
    You can create a new one with the same elements and do foreach, but remove from original array.

    For example List
    Code (Java):
     List<String> new_list = new ArrayList<>(original_list);
    then do foreach and remove from original List.
     
    #2 shoker137, Feb 11, 2019
    Last edited: Feb 11, 2019
    • Like Like x 1
  3. Are you talking about this?
    Code (Text):
    List<String> aCoolList = new ArrayList<>(p.getGroups());
    for(String group:aCoolList) {
        p.removeGroups(group);
    }
     
  4. Yes, it's right.
    You can still use aCoolList.foreach if you want (as in your original code)
     
  5. Thank you! But now there is a problem when I rejoin, the old group was set. Do I need to save the groups????? I don't know, can you help me out?
     
  6. Oh well, I will probably just add them back when a player joins, thanks!
     
  7. You're welcome.
     

Share This Page