Skip to content

Commit

Permalink
add option to change owner
Browse files Browse the repository at this point in the history
  • Loading branch information
Pilzinsel64 committed Dec 31, 2023
1 parent fd0647c commit 1e7eeb5
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer p

if (!world.isRemote) {
TileChunkLoader tile = (TileChunkLoader) world.getTileEntity(x, y, z);
if (tile.owner == null || tile.owner.equals(player.getCommandSenderName())
if (tile.owner == null || (ChunkLoaderManager.userInteract() && tile.owner.equals(player.getCommandSenderName()))
|| ChunkLoaderManager.opInteract() && ServerUtils.isPlayerOP(player.getCommandSenderName())) {
PacketCustom packet = new PacketCustom(ChunkLoaderSPH.channel, 12);
packet.writeCoord(x, y, z);
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/codechicken/chunkloader/ChunkLoaderCPH.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,11 @@ public static void sendShapeChange(TileChunkLoader tile, ChunkLoaderShape shape,
packet.writeByte(radius);
packet.sendToServer();
}

public static void sendOwnerChange(TileChunkLoader tile, String owner) {
PacketCustom packet = new PacketCustom(channel, 3);
packet.writeCoord(tile.xCoord, tile.yCoord, tile.zCoord);
packet.writeString(owner);
packet.sendToServer();
}
}
16 changes: 16 additions & 0 deletions src/main/java/codechicken/chunkloader/ChunkLoaderManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,8 @@ public static void load() {

private static boolean reloadDimensions = false;
private static boolean opInteract = false;
private static boolean userInteract = true;
private static boolean allowOwnerChange = false;
private static int cleanupTicks;
private static int maxChunks;
private static int awayTimeout;
Expand Down Expand Up @@ -587,6 +589,12 @@ public static void initConfig(ConfigFile config) {
opInteract = config.getTag("op-interact").setComment(
"Enabling this lets OPs alter other player's chunkloaders. WARNING: If you change a chunkloader, you have no idea what may break/explode by not being chunkloaded.")
.getBooleanValue(false);
userInteract = config.getTag("user-interact").setComment(
"Disabling this lets normal non-OP player's never open any chunkloader, also not their own. WARNING: Only to be used if 'op-interact' is true to ensure only admins can control chunk loaders.")
.getBooleanValue(true);
allowOwnerChange = config.getTag("allow-owner-change").setComment(
"Enabling this enables the option to change the owner of a Chunkloader. By default only the owner can change it. In case of 'user-interact' is false, only OPs can edit them. WARNING: If you change a owner of a chunkloader, you have no idea what may break/explode.")
.getBooleanValue(false);
maxChunks = config.getTag("maxchunks").setComment("The maximum number of chunks per chunkloader")
.getIntValue(400);
awayTimeout = config.getTag("awayTimeout").setComment(
Expand Down Expand Up @@ -783,6 +791,14 @@ public static boolean opInteract() {
return opInteract;
}

public static boolean userInteract() {
return userInteract;
}

public static boolean allowOwnerChange() {
return allowOwnerChange;
}

public static void unloadWorld(World world) {
int dim = CommonUtils.getDimension(world);
for (TicketManager mgr : playerOrganisers.values()) mgr.unloadDimension(dim);
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/codechicken/chunkloader/ChunkLoaderSPH.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public void handlePacket(PacketCustom packet, EntityPlayerMP sender, INetHandler
case 2:
handleChunkLoaderChangePacket(sender.worldObj, packet);
break;

case 3:
handleChunkLoaderOwnerPackage(sender.worldObj, packet);
break;
}
}

Expand All @@ -32,4 +34,12 @@ private void handleChunkLoaderChangePacket(World world, PacketCustom packet) {
ctile.setShapeAndRadius(ChunkLoaderShape.values()[packet.readUByte()], packet.readUByte());
}
}

private void handleChunkLoaderOwnerPackage(World world, PacketCustom packet) {
TileEntity tile = world.getTileEntity(packet.readInt(), packet.readInt(), packet.readInt());
if (tile instanceof TileChunkLoader) {
TileChunkLoader ctile = (TileChunkLoader) tile;
ctile.setOwner(packet.readString());
}
}
}
32 changes: 30 additions & 2 deletions src/main/java/codechicken/chunkloader/GuiChunkLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.GuiTextField;

import org.lwjgl.opengl.GL11;

import codechicken.core.ServerUtils;
import codechicken.lib.render.CCRenderState;
import codechicken.lib.util.LangProxy;

Expand All @@ -14,6 +16,7 @@ public class GuiChunkLoader extends GuiScreen {

public GuiButton laserButton;
public GuiButton shapeButton;
public GuiTextField ownerText;
public TileChunkLoader tile;

public GuiChunkLoader(TileChunkLoader tile) {
Expand All @@ -27,6 +30,13 @@ public void initGui() {
buttonList.add(new GuiButton(2, width / 2 - 80, height / 2 - 45, 20, 20, "-"));
buttonList.add(laserButton = new GuiButton(3, width / 2 + 7, height / 2 - 60, 75, 20, "-"));
buttonList.add(shapeButton = new GuiButton(4, width / 2 + 7, height / 2 - 37, 75, 20, "-"));

if (enableOwner())
buttonList.add(new GuiButton(5, width / 2 + 7, height / 2 - 8, 75, 20, lang.translate("setowner")));

ownerText = new GuiTextField(fontRendererObj, width / 2 - 80, height / 2 - 7, 80, 18);
ownerText.setText(tile.getOwner());

updateNames();

super.initGui();
Expand All @@ -46,12 +56,16 @@ public void updateScreen() {
}
updateNames();
super.updateScreen();
ownerText.updateCursorCounter();
}

public void drawScreen(int i, int j, float f) {
drawDefaultBackground();
drawContainerBackground();

if (enableOwner())
ownerText.drawTextBox();

super.drawScreen(i, j, f);// buttons

GL11.glDisable(GL11.GL_LIGHTING);
Expand Down Expand Up @@ -85,6 +99,13 @@ protected void mouseClicked(int par1, int par2, int par3) {
button = par3;
if (par3 == 1) par3 = 0;
super.mouseClicked(par1, par2, par3);
ownerText.mouseClicked(par1, par2, par3);
}

@Override
protected void keyTyped(char par1, int par2) {
super.keyTyped(par1, par2);
ownerText.textboxKeyTyped(par1, par2);
}

protected void actionPerformed(GuiButton guibutton) {
Expand All @@ -93,14 +114,21 @@ protected void actionPerformed(GuiButton guibutton) {
if (guibutton.id == 3) tile.renderInfo.showLasers = !tile.renderInfo.showLasers;
if (guibutton.id == 4)
ChunkLoaderCPH.sendShapeChange(tile, button == 1 ? tile.shape.prev() : tile.shape.next(), tile.radius);
if (guibutton.id == 5)
ChunkLoaderCPH.sendOwnerChange(tile, ownerText.getText());
}

private void drawContainerBackground() {
boolean isOpInteract = enableOwner();
GL11.glColor4f(1, 1, 1, 1);
CCRenderState.changeTexture("chickenchunks:textures/gui/guiSmall.png");
CCRenderState.changeTexture("chickenchunks:textures/gui/" + (isOpInteract ? "guiSmall2" : "guiSmall") + ".png");
int posx = width / 2 - 88;
int posy = height / 2 - 83;
drawTexturedModalRect(posx, posy, 0, 0, 176, 166);
drawTexturedModalRect(posx, posy, 0, 0, 176, isOpInteract ? 192 : 166);
}

private boolean enableOwner() {
return ChunkLoaderManager.allowOwnerChange() && (!ChunkLoaderManager.userInteract() || (ChunkLoaderManager.opInteract() && ServerUtils.isPlayerOP(this.tile.owner)));
}

public boolean doesGuiPauseGame() {
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/codechicken/chunkloader/TileChunkLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.ChunkCoordIntPair;
import net.minecraft.world.World;

import net.minecraft.world.chunk.storage.ChunkLoader;
import codechicken.lib.packet.PacketCustom;

public class TileChunkLoader extends TileChunkLoaderBase {
Expand All @@ -29,6 +29,20 @@ public static void handleDescriptionPacket(PacketCustom packet, World world) {
}
}

public boolean setOwner(String newOwner) {
// If remote, we just remember the new owner.
if (worldObj.isRemote) {
owner = newOwner;
return true;
}

// Disable for the old owner, then set the new owner and enable again.
deactivate();
owner = newOwner;
activate();
return true;
}

public boolean setShapeAndRadius(ChunkLoaderShape newShape, int newRadius) {
if (worldObj.isRemote) {
radius = newRadius;
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/chickenchunks/lang/en_US.lang
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ chickenchunks.gui.hidelasers=Hide Lasers
chickenchunks.gui.chunk=%d Chunk
chickenchunks.gui.chunks=%d Chunks
chickenchunks.gui.radius=Radius
chickenchunks.gui.setowner=Set Owner

chickenchunks.shape.square=Square
chickenchunks.shape.circle=Circle
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 1e7eeb5

Please sign in to comment.