Use thread groups to detect the effective side (#3644)

This commit is contained in:
kashike authored and LexManos committed 2017-04-07 15:35:58 -07:00
1 parent f0eb941abc
commit 9619be4a17
6 files changed
+129 -9

No files matched your search

@@ -64,6 +64,8 @@ import net.minecraftforge.fml.common.gameevent.PlayerEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent.Phase;
import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.fml.common.thread.SidedThreadGroup;
import net.minecraftforge.fml.common.thread.SidedThreadGroups;
import net.minecraftforge.fml.relauncher.CoreModManager;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.server.FMLServerHandler;
@@ -201,13 +203,8 @@ public class FMLCommonHandler
*/
public Side getEffectiveSide()
{
Thread thr = Thread.currentThread();
if (thr.getName().equals("Server thread") || thr.getName().startsWith("Netty Server IO"))
{
return Side.SERVER;
}
return Side.CLIENT;
final ThreadGroup group = Thread.currentThread().getThreadGroup();
return group instanceof SidedThreadGroup ? ((SidedThreadGroup) group).getSide() : Side.CLIENT;
}
/**
* Raise an exception
@@ -0,0 +1,56 @@
/*
* Minecraft Forge
* Copyright (c) 2016.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.fml.common.thread;
import net.minecraftforge.fml.relauncher.Side;
import java.util.concurrent.ThreadFactory;
import javax.annotation.Nonnull;
/**
* A thread group and factory combination which belongs to a {@link Side}.
*/
public final class SidedThreadGroup extends ThreadGroup implements ThreadFactory
{
private final Side side;
SidedThreadGroup(final Side side)
{
super(side.name());
this.side = side;
}
/**
* Gets the side this sided thread group belongs to.
*
* @return the side
*/
public Side getSide()
{
return this.side;
}
@Override
public Thread newThread(@Nonnull final Runnable runnable)
{
return new Thread(this, runnable);
}
}
@@ -0,0 +1,31 @@
/*
* Minecraft Forge
* Copyright (c) 2016.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.fml.common.thread;
import net.minecraftforge.fml.relauncher.Side;
public final class SidedThreadGroups
{
public static final SidedThreadGroup CLIENT = new SidedThreadGroup(Side.CLIENT);
public static final SidedThreadGroup SERVER = new SidedThreadGroup(Side.SERVER);
private SidedThreadGroups() {
}
}