Skip to content

Commit

Permalink
修改一些不成熟的同步写法
Browse files Browse the repository at this point in the history
  • Loading branch information
CHanzyLazer committed Aug 17, 2023
1 parent 83f181a commit 208d45d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private void _place(EntityPlayer aPlayer, byte aSide) {
@Override public final boolean isUsingFullBlockOverlay(ItemStack aStack, byte aSide) {
if (super.isUsingFullBlockOverlay(aStack, aSide)) return T;
TileEntity tTE = UT_CH.getItemTE(aStack);
return tTE!=null? isFullBlockTE(tTE, aSide):F;
return tTE!=null ? isFullBlockTE(tTE, aSide) : F;
}
// GTCH, 改为相同的连接性质即可
public boolean isFullBlockTE(TileEntity aHand, byte aSide) {return SIDES_VALID[aSide] && (aHand instanceof ITileEntityConnector) && UT.Code.haveOneCommonElement(((ITileEntityConnector)aHand).getConnectorTypes(OPOS[aSide]), getConnectorTypes(aSide));}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/gregtechCH/threads/TickTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ public final void run() {
} catch (Throwable e) {
mError = T;
mTE.setError(errorMessage() + e);
//noinspection SynchronizeOnNonFinalField
synchronized (ERR) {e.printStackTrace(ERR);}
e.printStackTrace(ERR);
}
}
// 重写 hashCode 以及 equals 来方便移除
Expand Down
121 changes: 62 additions & 59 deletions src/main/java/gregtechCH/util/WD_CH.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public static void onTicking(long aTimer) {
if (!DATA_GTCH.disableGTRerender) doChunkRerender(aTimer);
}

public static <WorldType> boolean isServerSide(WorldType aWorld) {
public static <W> boolean isServerSide(W aWorld) {
return (aWorld instanceof World) ? (!((World)aWorld).isRemote) : cpw.mods.fml.common.FMLCommonHandler.instance().getEffectiveSide().isServer();
}
public static <WorldType> boolean isClientSide(WorldType aWorld) {
public static <W> boolean isClientSide(W aWorld) {
return (aWorld instanceof World) ? ((World)aWorld).isRemote : cpw.mods.fml.common.FMLCommonHandler.instance().getEffectiveSide().isClient();
}

Expand All @@ -55,8 +55,7 @@ public static void doChunkRerender(long aTimer) {
formMainChunkList();
formAroundChunkList();
} catch (Exception e) {
//noinspection SynchronizeOnNonFinalField
synchronized (ERR) {e.printStackTrace(ERR);}
e.printStackTrace(ERR);
}
});
sUpdated = F;
Expand Down Expand Up @@ -86,67 +85,70 @@ public static void doChunkRerender(long aTimer) {

/* 玩家视觉的区块 */
@SideOnly(Side.CLIENT) private static void formMainChunkList() {
synchronized(sMainChunkRenderList) {sMainChunkRenderList.clear();}
// 用来构造的坐标 list
LinkedList<ChunkCoordinates> tCoordList = new LinkedList<>();
Vec3 tPlayerView = UT_CH.Code.getPlayerViewVec3(sPlayer);
ChunkCoordinates tPlayerChunkCoord = UT_CH.Code.getPlayerChunkCoord(sPlayer);
ChunkCoordinates tChunkCoord = new ChunkCoordinates(tPlayerChunkCoord);
Vec3 tNextChunkCoord = tPlayerView.addVector(tChunkCoord.posX, tChunkCoord.posY, tChunkCoord.posZ);
tCoordList.add(new ChunkCoordinates(tChunkCoord));
int tAddIdx = 0;
// 直接使用循环的方式添加,使用随机位置插入的方法来得到随机排序的 list
while (tCoordList.size()<DATA_GTCH.rerenderMainLength) {
tChunkCoord.posX = (int)Math.round(tNextChunkCoord.xCoord);
tChunkCoord.posY = (int)Math.round(tNextChunkCoord.yCoord);
tChunkCoord.posZ = (int)Math.round(tNextChunkCoord.zCoord);
if (!tChunkCoord.equals(tCoordList.get(tAddIdx))) {
tAddIdx = RNGSUS.nextInt(tCoordList.size()+1);
tCoordList.add(tAddIdx, new ChunkCoordinates(tChunkCoord));
synchronized(sMainChunkRenderList) {
sMainChunkRenderList.clear();
// 用来构造的坐标 list
LinkedList<ChunkCoordinates> tCoordList = new LinkedList<>();
Vec3 tPlayerView = UT_CH.Code.getPlayerViewVec3(sPlayer);
ChunkCoordinates tPlayerChunkCoord = UT_CH.Code.getPlayerChunkCoord(sPlayer);
ChunkCoordinates tChunkCoord = new ChunkCoordinates(tPlayerChunkCoord);
Vec3 tNextChunkCoord = tPlayerView.addVector(tChunkCoord.posX, tChunkCoord.posY, tChunkCoord.posZ);
tCoordList.add(new ChunkCoordinates(tChunkCoord));
int tAddIdx = 0;
// 直接使用循环的方式添加,使用随机位置插入的方法来得到随机排序的 list
while (tCoordList.size()<DATA_GTCH.rerenderMainLength) {
tChunkCoord.posX = (int)Math.round(tNextChunkCoord.xCoord);
tChunkCoord.posY = (int)Math.round(tNextChunkCoord.yCoord);
tChunkCoord.posZ = (int)Math.round(tNextChunkCoord.zCoord);
if (!tChunkCoord.equals(tCoordList.get(tAddIdx))) {
tAddIdx = RNGSUS.nextInt(tCoordList.size()+1);
tCoordList.add(tAddIdx, new ChunkCoordinates(tChunkCoord));
}
tNextChunkCoord.xCoord += tPlayerView.xCoord;
tNextChunkCoord.yCoord += tPlayerView.yCoord;
tNextChunkCoord.zCoord += tPlayerView.zCoord;
}
// 从中随机选取放入队列,需要满足要求并且记得移除主序列的元素
while (sMainChunkRenderList.size()<DATA_GTCH.rerenderMainMaxChunk && !tCoordList.isEmpty()) {
tChunkCoord = tCoordList.poll();
Runnable tRender = checkAndGetRerender(tChunkCoord);
if (tRender != null) sMainChunkRenderList.add(tRender);
}
tNextChunkCoord.xCoord += tPlayerView.xCoord;
tNextChunkCoord.yCoord += tPlayerView.yCoord;
tNextChunkCoord.zCoord += tPlayerView.zCoord;
}
// 从中随机选取放入队列,需要满足要求并且记得移除主序列的元素
while (sMainChunkRenderList.size()<DATA_GTCH.rerenderMainMaxChunk && !tCoordList.isEmpty()) {
tChunkCoord = tCoordList.poll();
Runnable tRender = checkAndGetRerender(tChunkCoord);
if (tRender != null) synchronized(sMainChunkRenderList) {sMainChunkRenderList.add(tRender);}
}
}

/* 玩家周边的区块 */
@SideOnly(Side.CLIENT) private static void formAroundChunkList() {
synchronized(sAroundChunkRenderList) {sAroundChunkRenderList.clear();}
// 用来构造的坐标 list
LinkedList<ChunkCoordinates> tCoordList = new LinkedList<>();
ChunkCoordinates tPlayerChunkCoord = UT_CH.Code.getPlayerChunkCoord(sPlayer);
ChunkCoordinates tChunkCoord = new ChunkCoordinates(tPlayerChunkCoord);
// 使用循环的方式添加,也使用随机位置插入的方法来得到随机排序的 list
for (int tX = -DATA_GTCH.rerenderAroundLength; tX <= DATA_GTCH.rerenderAroundLength; ++tX)
synchronized(sAroundChunkRenderList) {
sAroundChunkRenderList.clear();
// 用来构造的坐标 list
LinkedList<ChunkCoordinates> tCoordList = new LinkedList<>();
ChunkCoordinates tPlayerChunkCoord = UT_CH.Code.getPlayerChunkCoord(sPlayer);
ChunkCoordinates tChunkCoord = new ChunkCoordinates(tPlayerChunkCoord);
// 使用循环的方式添加,也使用随机位置插入的方法来得到随机排序的 list
for (int tX = -DATA_GTCH.rerenderAroundLength; tX <= DATA_GTCH.rerenderAroundLength; ++tX)
for (int tY = -DATA_GTCH.rerenderAroundLength; tY <= DATA_GTCH.rerenderAroundLength; ++tY)
for (int tZ = -DATA_GTCH.rerenderAroundLength; tZ <= DATA_GTCH.rerenderAroundLength; ++tZ) {
tChunkCoord.posX = tPlayerChunkCoord.posX + tX;
tChunkCoord.posY = tPlayerChunkCoord.posY + tY;
tChunkCoord.posZ = tPlayerChunkCoord.posZ + tZ;
tCoordList.add(RNGSUS.nextInt(tCoordList.size()+1), new ChunkCoordinates(tChunkCoord));
}
// 从中随机选取放入队列,需要满足要求并且记得移除主序列的元素
while (sAroundChunkRenderList.size()<DATA_GTCH.rerenderAroundMaxChunk && !tCoordList.isEmpty()) {
tChunkCoord = tCoordList.poll();
Runnable tRender = checkAndGetRerender(tChunkCoord);
if (tRender != null) synchronized(sAroundChunkRenderList) {sAroundChunkRenderList.add(tRender);}
for (int tZ = -DATA_GTCH.rerenderAroundLength; tZ <= DATA_GTCH.rerenderAroundLength; ++tZ) {
tChunkCoord.posX = tPlayerChunkCoord.posX + tX;
tChunkCoord.posY = tPlayerChunkCoord.posY + tY;
tChunkCoord.posZ = tPlayerChunkCoord.posZ + tZ;
tCoordList.add(RNGSUS.nextInt(tCoordList.size()+1), new ChunkCoordinates(tChunkCoord));
}
// 从中随机选取放入队列,需要满足要求并且记得移除主序列的元素
while (sAroundChunkRenderList.size()<DATA_GTCH.rerenderAroundMaxChunk && !tCoordList.isEmpty()) {
tChunkCoord = tCoordList.poll();
Runnable tRender = checkAndGetRerender(tChunkCoord);
if (tRender != null) sAroundChunkRenderList.add(tRender);
}
}
}

// 检测坐标是否需要重新渲染,不合理的返回 null,现在不再检测是否超时
@SideOnly(Side.CLIENT)
private static Runnable checkAndGetRerender(ChunkCoordinates aChunkCoordToRender) {
if (!sChunkRenderList.containsKey(aChunkCoordToRender)) return null;
Runnable tRender = sChunkRenderList.get(aChunkCoordToRender);
synchronized(sChunkRenderList) {sChunkRenderList.remove(aChunkCoordToRender);}
return tRender;
synchronized(sChunkRenderList) {
return sChunkRenderList.remove(aChunkCoordToRender);
}
}

@SideOnly(Side.CLIENT)
Expand All @@ -172,8 +174,7 @@ public ChunkRender(World aWorld, int aX, int aY, int aZ) {
// 记录是否成功更新,没有则不能刷新下次渲染队列
@SideOnly(Side.CLIENT) private static boolean sUpdated = T;
// 清空的接口,客户端世界加载或者卸载时清空渲染队列
@SideOnly(Side.CLIENT)
public static void clearStatic(boolean aIsServerSide) {
@SideOnly(Side.CLIENT) public static void clearStatic(boolean aIsServerSide) {
if (!aIsServerSide) {
sChunkRenderList.clear();
sMainChunkRenderList.clear();
Expand All @@ -185,13 +186,15 @@ public static void clearStatic(boolean aIsServerSide) {

// 标记方块(所在区块)用于计划重新渲染
@SideOnly(Side.CLIENT)
public static <WorldType> void markBlockForRerender(WorldType aWorld, int aX, int aY, int aZ, boolean aImmediate) {
public static synchronized <W> void markBlockForRerender(W aWorld, int aX, int aY, int aZ, boolean aImmediate) {
if (aWorld instanceof World) {
ChunkCoordinates tCoord = new ChunkCoordinates(aX>>4, aY>>4, aZ>>4);
synchronized(sChunkRenderList) {sChunkRenderList.put(tCoord, new ChunkRender((World)aWorld, aX, aY, aZ));}
if (aImmediate) {
Runnable tRender = checkAndGetRerender(tCoord);
if (tRender != null) tRender.run();
synchronized(sChunkRenderList) {
sChunkRenderList.put(tCoord, new ChunkRender((World)aWorld, aX, aY, aZ));
if (aImmediate) {
Runnable tRender = checkAndGetRerender(tCoord);
if (tRender != null) tRender.run();
}
}
}
}
Expand Down

0 comments on commit 208d45d

Please sign in to comment.