Fix CME on chunk unload in FarmlandTicketManager (#5998)

This commit is contained in:
ichttt 2019-09-03 21:59:35 +02:00 committed by LexManos
parent bdce8d0494
commit ddb90a69d3
3 changed files with 102 additions and 77 deletions

View File

@ -20,6 +20,7 @@
package net.minecraftforge.common;
import com.google.common.base.Preconditions;
import com.google.common.collect.MapMaker;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import net.minecraft.util.math.AxisAlignedBB;
@ -31,17 +32,19 @@ import net.minecraft.world.World;
import net.minecraft.world.chunk.IChunk;
import net.minecraftforge.common.ticket.AABBTicket;
import net.minecraftforge.common.ticket.ChunkTicketManager;
import net.minecraftforge.common.ticket.MultiTicketManager;
import net.minecraftforge.common.ticket.SimpleTicket;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class FarmlandWaterManager
{
private static boolean DEBUG = Boolean.parseBoolean(System.getProperty("forge.debugFarmlandWaterManager", "false"));
private static final Int2ObjectMap<Map<ChunkPos, ChunkTicketManager<Vec3d>>> customWaterHandler = new Int2ObjectOpenHashMap<>();
private static final Logger LOGGER = LogManager.getLogger();
/**
* Adds a custom ticket.
@ -51,26 +54,19 @@ public class FarmlandWaterManager
* when the region this is unloaded (e.g. your TE is unloaded or the block is removed), and validate once it is loaded
* @param world The world where the region should be marked. Only server-side worlds are allowed
* @param ticket Your ticket you want to have registered
* @param chunkPoses The chunkPoses where the ticket is located
* @param masterChunk The chunk pos that is controls when the ticket may be unloaded. The ticket should originate from here.
* @param additionalChunks The chunks in that this ticket wants to operate as well.
* @return The ticket for your requested region.
*/
@SuppressWarnings("unchecked")
public static<T extends SimpleTicket<Vec3d>> T addCustomTicket(World world, T ticket, ChunkPos... chunkPoses)
public static<T extends SimpleTicket<Vec3d>> T addCustomTicket(World world, T ticket, ChunkPos masterChunk, ChunkPos... additionalChunks)
{
Preconditions.checkArgument(!world.isRemote, "Water region is only determined server-side");
Preconditions.checkArgument(chunkPoses.length > 0, "Need at least one chunk pos");
Map<ChunkPos, ChunkTicketManager<Vec3d>> ticketMap = customWaterHandler.computeIfAbsent(world.getDimension().getType().getId(), id -> new HashMap<>());
if (chunkPoses.length == 1)
{
ticket.setBackend(ticketMap.computeIfAbsent(chunkPoses[0], ChunkTicketManager::new));
}
else
{
ChunkTicketManager<Vec3d>[] tickets = new ChunkTicketManager[chunkPoses.length];
for (int i = 0; i < chunkPoses.length; i++)
tickets[i] = ticketMap.computeIfAbsent(chunkPoses[i], ChunkTicketManager::new);
ticket.setBackend(new MultiTicketManager<>(tickets));
}
Map<ChunkPos, ChunkTicketManager<Vec3d>> ticketMap = customWaterHandler.computeIfAbsent(world.getDimension().getType().getId(), id -> new MapMaker().weakValues().makeMap());
ChunkTicketManager<Vec3d>[] additionalTickets = new ChunkTicketManager[additionalChunks.length];
for (int i = 0; i < additionalChunks.length; i++)
additionalTickets[i] = ticketMap.computeIfAbsent(additionalChunks[i], ChunkTicketManager::new);
ticket.setManager(ticketMap.computeIfAbsent(masterChunk, ChunkTicketManager::new), additionalTickets);
ticket.validate();
return ticket;
}
@ -88,6 +84,8 @@ public class FarmlandWaterManager
*/
public static AABBTicket addAABBTicket(World world, AxisAlignedBB aabb)
{
if (DEBUG)
LOGGER.info("FarmlandWaterManager: New AABBTicket, aabb={}", aabb);
//First calculate all chunks the aabb is in
ChunkPos leftUp = new ChunkPos(((int) aabb.minX) >> 4, ((int) aabb.minZ) >> 4);
ChunkPos rightDown = new ChunkPos(((int) aabb.maxX) >> 4, ((int) aabb.maxZ) >> 4);
@ -99,11 +97,37 @@ public class FarmlandWaterManager
posSet.add(new ChunkPos(x, z));
}
}
return addCustomTicket(world, new AABBTicket(aabb), posSet.toArray(new ChunkPos[0]));
ChunkPos masterPos = null;
double masterDistance = Double.MAX_VALUE;
for (ChunkPos pos : posSet) //Find the chunkPos with the lowest distance to the center and choose it as the master pos
{
double distToCenter = getDistanceSq(pos, aabb.getCenter());
if (distToCenter < masterDistance)
{
if (DEBUG)
LOGGER.info("FarmlandWaterManager: New better pos then {}: {}, prev dist {}, new dist {}", masterPos, pos, masterDistance, distToCenter);
masterPos = pos;
masterDistance = distToCenter;
}
}
posSet.remove(masterPos);
if (DEBUG)
LOGGER.info("FarmlandWaterManager: {} center pos, {} dummy posses. Dist to center {}", masterPos, posSet.toArray(new ChunkPos[0]), masterDistance);
return addCustomTicket(world, new AABBTicket(aabb), masterPos, posSet.toArray(new ChunkPos[0]));
}
private static double getDistanceSq(ChunkPos pos, Vec3d vec3d)
{
//See ChunkPos#getDistanceSq
double d0 = (double)(pos.x * 16 + 8);
double d1 = (double)(pos.z * 16 + 8);
double d2 = d0 - vec3d.x;
double d3 = d1 - vec3d.z;
return d2 * d2 + d3 * d3;
}
/**
* Tests if a block is in a region that is watered by blocks. This does not check vanilla water, see {@link net.minecraft.block.BlockFarmland#hasWater(World, BlockPos)}
* Tests if a block is in a region that is watered by blocks. This does not check vanilla water, see {@link net.minecraft.block.FarmlandBlock#hasWater(IWorldReader, BlockPos)}
* @return true if there is a ticket with an AABB that includes your block
*/
public static boolean hasBlockWaterTicket(IWorldReader world, BlockPos pos)
@ -111,7 +135,7 @@ public class FarmlandWaterManager
ChunkTicketManager<Vec3d> ticketManager = getTicketManager(new ChunkPos(pos.getX() >> 4, pos.getZ() >> 4), world);
if (ticketManager != null)
{
Vec3d posAsVec3d = new Vec3d(pos);
Vec3d posAsVec3d = new Vec3d(pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5);
for (SimpleTicket<Vec3d> ticket : ticketManager.getTickets()) {
if (ticket.matches(posAsVec3d))
return true;
@ -125,10 +149,11 @@ public class FarmlandWaterManager
ChunkTicketManager<Vec3d> ticketManager = getTicketManager(chunk.getPos(), chunk.getWorldForge());
if (ticketManager != null)
{
for (SimpleTicket<Vec3d> ticket : ticketManager.getTickets())
{
ticket.invalidate();
}
if (DEBUG)
LOGGER.info("FarmlandWaterManager: got tickets {} at {} before", ticketManager.getTickets().size(), ticketManager.pos);
ticketManager.getTickets().removeIf(next -> next.unload(ticketManager)); //remove if this is the master manager of the ticket
if (DEBUG)
LOGGER.info("FarmlandWaterManager: got tickets {} at {} after", ticketManager.getTickets().size(), ticketManager.pos);
}
}

View File

@ -1,45 +0,0 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2019.
*
* 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.common.ticket;
public class MultiTicketManager<T> implements ITicketManager<T>
{
private final ITicketGetter<T>[] ticketManagers;
@SafeVarargs
public MultiTicketManager(ITicketGetter<T>... ticketManagers)
{
this.ticketManagers = ticketManagers;
}
@Override
public void add(SimpleTicket<T> ticket)
{
for (ITicketGetter<T> manager : ticketManagers)
manager.add(ticket);
}
@Override
public void remove(SimpleTicket<T> ticket)
{
for (ITicketGetter<T> manager : ticketManagers)
manager.remove(ticket);
}
}

View File

@ -23,6 +23,7 @@ import com.google.common.base.Preconditions;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.function.Consumer;
/**
* Common class for a simple ticket based system.
@ -31,7 +32,8 @@ import javax.annotation.Nullable;
public abstract class SimpleTicket<T>
{
@Nullable
private ITicketManager<T> manager;
private ITicketManager<T> masterManager;
private ITicketManager<T>[] dummyManagers;
protected boolean isValid = false;
/**
@ -39,10 +41,12 @@ public abstract class SimpleTicket<T>
* <br>
* Should <b>not</b> be called if you just want to register a ticket to a system like the {@link net.minecraftforge.common.FarmlandWaterManager}
*/
public final void setBackend(@Nonnull ITicketManager<T> ticketManager)
@SafeVarargs
public final void setManager(@Nonnull ITicketManager<T> masterManager, @Nonnull ITicketManager<T>... dummyManagers)
{
Preconditions.checkState(this.manager == null, "Ticket is already registered to a managing system");
this.manager = ticketManager;
Preconditions.checkState(this.masterManager == null, "Ticket is already registered to a managing system");
this.masterManager = masterManager;
this.dummyManagers = dummyManagers;
}
/**
@ -59,26 +63,67 @@ public abstract class SimpleTicket<T>
*/
public void invalidate()
{
Preconditions.checkState(this.manager != null, "Ticket is not registered to a managing system");
if (this.isValid())
{
this.manager.remove(this);
forEachManager(ticketManager -> ticketManager.remove(this));
}
this.isValid = false;
}
/**
* Called by the managing system when a ticket wishes to unload all of it's tickets, e.g. on chunk unload
* <br>The ticket must not remove itself from the manager that is calling the unload!
* The ticket must ensure that it removes itself from all of it's dummies when returning true
* @param unloadingManager The manager that is unloading this ticket
* @return true if this ticket can be removed, false if not.
*/
public boolean unload(ITicketManager<T> unloadingManager)
{
if (unloadingManager == masterManager)
{
for (ITicketManager<T> manager : dummyManagers)
{
manager.remove(this); //remove ourself from all dummies
}
this.isValid = false; //and mark us as invalid
return true;
}
return false;
}
/**
* Re-adds your ticket to the system.
*/
public void validate()
{
Preconditions.checkState(this.manager != null, "Ticket is not registered to a managing system");
if (!this.isValid())
{
this.manager.add(this);
forEachManager(ticketManager -> ticketManager.add(this));
}
this.isValid = true;
}
public abstract boolean matches(T toMatch);
//Helper methods for custom tickets below
protected final void forEachManager(Consumer<ITicketManager<T>> consumer)
{
Preconditions.checkState(this.masterManager != null, "Ticket is not registered to a managing system");
consumer.accept(masterManager);
for (ITicketManager<T> manager : dummyManagers)
{
consumer.accept(manager);
}
}
protected final ITicketManager<T> getMasterManager()
{
return masterManager;
}
protected final ITicketManager<T>[] getDummyManagers()
{
return dummyManagers;
}
}