Skip to content

Commit

Permalink
Updated some things i forgot before from multiple xcom's commits
Browse files Browse the repository at this point in the history
  • Loading branch information
Dioswilson committed Feb 5, 2022
1 parent a15d56b commit 22b16e9
Show file tree
Hide file tree
Showing 3 changed files with 323 additions and 69 deletions.
76 changes: 74 additions & 2 deletions carpetmodSrc/carpet/commands/CommandChunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
import net.minecraft.command.ICommandSender;
import net.minecraft.command.WrongUsageException;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.management.PlayerChunkMapEntry;
import net.minecraft.util.HttpUtil;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.gen.ChunkProviderServer;
import net.minecraft.world.gen.IChunkGenerator;

import javax.annotation.Nullable;
import java.util.Collections;
Expand All @@ -25,7 +29,7 @@ public class CommandChunk extends CommandCarpetBase

public String getUsage(ICommandSender sender)
{
return "Usage: chunk <load | info | unload> <X> <Z>";
return "Usage: chunk <load | info | unload | regen | repop | asyncrepop> <X> <Z>";
}

public String getName()
Expand Down Expand Up @@ -58,6 +62,15 @@ public void execute(MinecraftServer server, ICommandSender sender, String[] args
case "unload":
unload(sender, chunkX, chunkZ);
return;
case "regen":
regen(sender, chunkX, chunkZ);
return;
case "repop":
repop(sender, chunkX, chunkZ);
return;
case "asyncrepop":
asyncrepop(sender, chunkX, chunkZ);
return;
case "info":
default:
info(sender, chunkX, chunkZ);
Expand All @@ -68,6 +81,65 @@ public void execute(MinecraftServer server, ICommandSender sender, String[] args
}
}

private boolean checkRepopLoaded(int x, int z){
return world.isChunkLoaded(x, z, false)
&& world.isChunkLoaded(x+1, z, false)
&& world.isChunkLoaded(x, z+1, false)
&& world.isChunkLoaded(x+1, z+1, false);
}

private void regen(ICommandSender sender, int x, int z) {
if(!checkRepopLoaded(x, z)) {
sender.sendMessage(new TextComponentString(("Area not loaded for re-population")));
}

ChunkProviderServer chunkProvider = (ChunkProviderServer) world.getChunkProvider();
long i = ChunkPos.asLong(x, z);
chunkProvider.loadedChunks.remove(i);
Chunk chunk = chunkProvider.chunkGenerator.generateChunk(x, z);
chunkProvider.loadedChunks.put(i, chunk);
chunk.onLoad();
chunk.setTerrainPopulated(true);
chunk.onTick(false);
PlayerChunkMapEntry entry = ((WorldServer)world).playerChunkMap.getEntry(x, z);
if (entry != null && entry.chunk != null) {
entry.chunk = chunk;
entry.sentToPlayers = false;
entry.sendToPlayers();
}
}

private void repop(ICommandSender sender, int x, int z) {
if(!checkRepopLoaded(x, z)) {
sender.sendMessage(new TextComponentString(("Area not loaded for re-population")));
}

ChunkProviderServer chunkProvider = (ChunkProviderServer) world.getChunkProvider();
IChunkGenerator chunkGenerator = chunkProvider.chunkGenerator;
Chunk chunk = chunkProvider.loadChunk(x, z);
chunk.setUnpopulated();
chunk.populate(chunkProvider, chunkGenerator);
}

private void asyncrepop(ICommandSender sender, int x, int z) {
if(!checkRepopLoaded(x, z)) {
sender.sendMessage(new TextComponentString(("Area not loaded for re-population")));
}

HttpUtil.DOWNLOADER_EXECUTOR.submit(() -> {
try {
ChunkProviderServer chunkProvider = (ChunkProviderServer) world.getChunkProvider();
IChunkGenerator chunkGenerator = chunkProvider.chunkGenerator;
Chunk chunk = chunkProvider.loadChunk(x, z);
chunk.setUnpopulated();
chunk.populate(chunkProvider, chunkGenerator);
System.out.println("Chunk async repop end.");
} catch(Throwable e) {
e.printStackTrace();
}
});
}

protected void info(ICommandSender sender, int x, int z) throws NoSuchFieldException, IllegalAccessException {
if(!world.isChunkLoaded(x, z, false)) {
sender.sendMessage(new TextComponentString(("Chunk is not loaded")));
Expand Down Expand Up @@ -98,7 +170,7 @@ public List<String> getTabCompletions(MinecraftServer server, ICommandSender sen
int chunkZ = sender.getPosition().getZ() >> 4;

if (args.length == 1) {
return getListOfStringsMatchingLastWord(args, "info", "load", "unload");
return getListOfStringsMatchingLastWord(args, "info", "load", "unload", "regen", "repop", "asyncrepop");
} else if (args.length == 2) {
return getListOfStringsMatchingLastWord(args, Integer.toString(chunkX), "~");
} else if (args.length == 3) {
Expand Down
123 changes: 58 additions & 65 deletions carpetmodSrc/carpet/commands/CommandZetBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,89 +75,82 @@ public void execute(MinecraftServer server, ICommandSender sender, String[] args

World world = sender.getEntityWorld();

if (!world.isBlockLoaded(blockpos))
{
throw new CommandException("commands.setblock.outOfWorld", new Object[0]);
}
else
NBTTagCompound nbttagcompound = new NBTTagCompound();
boolean flag = false;

if (args.length >= 7 && block.hasTileEntity())
{
NBTTagCompound nbttagcompound = new NBTTagCompound();
boolean flag = false;
String s = buildString(args, 6);

if (args.length >= 7 && block.hasTileEntity())
try
{
String s = buildString(args, 6);

try
{
nbttagcompound = JsonToNBT.getTagFromJson(s);
flag = true;
}
catch (NBTException nbtexception)
{
throw new CommandException("commands.setblock.tagError", new Object[] {nbtexception.getMessage()});
}
nbttagcompound = JsonToNBT.getTagFromJson(s);
flag = true;
}
catch (NBTException nbtexception)
{
throw new CommandException("commands.setblock.tagError", new Object[] {nbtexception.getMessage()});
}
}

EntityPlayerMP worldEditPlayer = sender instanceof EntityPlayerMP ? (EntityPlayerMP) sender : null;
NBTTagCompound worldEditTag = flag ? nbttagcompound : null;
EntityPlayerMP worldEditPlayer = sender instanceof EntityPlayerMP ? (EntityPlayerMP) sender : null;
NBTTagCompound worldEditTag = flag ? nbttagcompound : null;

if (args.length >= 6)
if (args.length >= 6)
{
if ("destroy".equals(args[5]))
{
if ("destroy".equals(args[5]))
WorldEditBridge.recordBlockEdit(worldEditPlayer, world, blockpos, Blocks.AIR.getDefaultState(), worldEditTag);
CapturedDrops.setCapturingDrops(true);
world.destroyBlock(blockpos, true);
CapturedDrops.setCapturingDrops(false);
for (EntityItem drop : CapturedDrops.getCapturedDrops())
WorldEditBridge.recordEntityCreation(worldEditPlayer, world, drop);
CapturedDrops.clearCapturedDrops();

if (block == Blocks.AIR)
{
WorldEditBridge.recordBlockEdit(worldEditPlayer, world, blockpos, Blocks.AIR.getDefaultState(), worldEditTag);
CapturedDrops.setCapturingDrops(true);
world.destroyBlock(blockpos, true);
CapturedDrops.setCapturingDrops(false);
for (EntityItem drop : CapturedDrops.getCapturedDrops())
WorldEditBridge.recordEntityCreation(worldEditPlayer, world, drop);
CapturedDrops.clearCapturedDrops();

if (block == Blocks.AIR)
{
notifyCommandListener(sender, this, "commands.setblock.success", new Object[0]);
return;
}
}
else if ("keep".equals(args[5]) && !world.isAirBlock(blockpos))
{
throw new CommandException("commands.setblock.noChange", new Object[0]);
notifyCommandListener(sender, this, "commands.setblock.success", new Object[0]);
return;
}
}
else if ("keep".equals(args[5]) && !world.isAirBlock(blockpos))
{
throw new CommandException("commands.setblock.noChange", new Object[0]);
}
}

WorldEditBridge.recordBlockEdit(worldEditPlayer, world, blockpos, iblockstate, worldEditTag);
WorldEditBridge.recordBlockEdit(worldEditPlayer, world, blockpos, iblockstate, worldEditTag);

TileEntity tileentity1 = world.getTileEntity(blockpos);
TileEntity tileentity1 = world.getTileEntity(blockpos);

if (tileentity1 != null && tileentity1 instanceof IInventory)
{
((IInventory)tileentity1).clear();
}
if (tileentity1 != null && tileentity1 instanceof IInventory)
{
((IInventory)tileentity1).clear();
}

if (!world.setBlockState(blockpos, iblockstate, 2))
{
throw new CommandException("commands.setblock.noChange", new Object[0]);
}
else
if (!world.setBlockState(blockpos, iblockstate, 2))
{
throw new CommandException("commands.setblock.noChange", new Object[0]);
}
else
{
if (flag)
{
if (flag)
TileEntity tileentity = world.getTileEntity(blockpos);

if (tileentity != null)
{
TileEntity tileentity = world.getTileEntity(blockpos);

if (tileentity != null)
{
nbttagcompound.setInteger("x", blockpos.getX());
nbttagcompound.setInteger("y", blockpos.getY());
nbttagcompound.setInteger("z", blockpos.getZ());
tileentity.readFromNBT(nbttagcompound);
}
nbttagcompound.setInteger("x", blockpos.getX());
nbttagcompound.setInteger("y", blockpos.getY());
nbttagcompound.setInteger("z", blockpos.getZ());
tileentity.readFromNBT(nbttagcompound);
}

world.notifyNeighborsRespectDebug(blockpos, iblockstate.getBlock(), false);
sender.setCommandStat(CommandResultStats.Type.AFFECTED_BLOCKS, 1);
notifyCommandListener(sender, this, "commands.setblock.success", new Object[0]);
}

world.notifyNeighborsRespectDebug(blockpos, iblockstate.getBlock(), false);
sender.setCommandStat(CommandResultStats.Type.AFFECTED_BLOCKS, 1);
notifyCommandListener(sender, this, "commands.setblock.success", new Object[0]);
}
}
}
Expand Down
Loading

0 comments on commit 22b16e9

Please sign in to comment.