Skip to content

Commit

Permalink
Improve permission handling around interaction events
Browse files Browse the repository at this point in the history
  • Loading branch information
Thodor12 committed Jan 15, 2025
1 parent 6758621 commit 3b96795
Show file tree
Hide file tree
Showing 17 changed files with 405 additions and 448 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import com.minecolonies.api.tileentities.MinecoloniesTileEntities;
import com.minecolonies.core.tileentities.TileEntityColonyBuilding;
import com.minecolonies.core.Network;
import com.minecolonies.core.network.messages.server.colony.OpenInventoryMessage;
import com.minecolonies.core.network.messages.server.colony.building.OpenBuildingInventoryMessage;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.entity.player.Player;
Expand Down Expand Up @@ -99,7 +99,7 @@ public InteractionResult use(
&& building.getColony() != null
&& building.getColony().getPermissions().hasPermission(player, Action.ACCESS_HUTS))
{
Network.getNetwork().sendToServer(new OpenInventoryMessage(building));
Network.getNetwork().sendToServer(new OpenBuildingInventoryMessage(building));
}
}
return InteractionResult.SUCCESS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.minecolonies.api.colony.buildings.views.IBuildingView;
import com.minecolonies.core.Network;
import com.minecolonies.core.colony.buildings.views.AbstractBuildingView;
import com.minecolonies.core.network.messages.server.colony.OpenInventoryMessage;
import com.minecolonies.core.network.messages.server.colony.building.OpenBuildingInventoryMessage;
import com.minecolonies.core.network.messages.server.colony.building.BuildRequestMessage;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.client.resources.sounds.SimpleSoundInstance;
Expand All @@ -14,7 +14,6 @@
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.contents.TranslatableContents;
import net.minecraft.sounds.SoundEvents;
import org.jetbrains.annotations.NotNull;

import static com.minecolonies.api.util.constant.TranslationConstants.*;
import static com.minecolonies.api.util.constant.WindowConstants.*;
Expand Down Expand Up @@ -121,7 +120,7 @@ else if (buttonLabel.equalsIgnoreCase(ACTION_CANCEL_DECONSTRUCTION))
*/
private void inventoryClicked()
{
Network.getNetwork().sendToServer(new OpenInventoryMessage(building));
Network.getNetwork().sendToServer(new OpenBuildingInventoryMessage(building));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import com.minecolonies.api.util.constant.Constants;
import com.minecolonies.core.Network;
import com.minecolonies.core.colony.buildings.views.AbstractBuildingView;
import com.minecolonies.core.network.messages.server.colony.OpenInventoryMessage;
import com.minecolonies.core.network.messages.server.colony.building.OpenBuildingInventoryMessage;
import com.minecolonies.core.network.messages.server.colony.building.postbox.PostBoxRequestMessage;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.EnchantedBookItem;
Expand Down Expand Up @@ -104,7 +104,7 @@ public WindowPostBox(final AbstractBuildingView buildingView)
*/
private void inventoryClicked()
{
Network.getNetwork().sendToServer(new OpenInventoryMessage(buildingView));
Network.getNetwork().sendToServer(new OpenBuildingInventoryMessage(buildingView));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.minecolonies.core.Network;
import com.minecolonies.core.client.gui.AbstractWindowRequestTree;
import com.minecolonies.core.colony.buildings.views.AbstractBuildingView;
import com.minecolonies.core.network.messages.server.colony.OpenInventoryMessage;
import com.minecolonies.core.network.messages.server.colony.citizen.OpenCitizenInventoryMessage;
import net.minecraft.client.Minecraft;
import net.minecraft.network.chat.Component;

Expand All @@ -35,8 +35,8 @@ public AbstractWindowCitizen(final ICitizenDataView citizen, final String ui)
registerButton("requestIcon", () -> new RequestWindowCitizen(citizen).open());
PaneBuilders.tooltipBuilder().hoverPane(findPaneByID("requestIcon")).build().setText(Component.translatable("com.minecolonies.coremod.gui.citizen.requests"));

registerButton("inventoryTab", () -> Network.getNetwork().sendToServer(new OpenInventoryMessage(colony, citizen.getName(), citizen.getEntityId())));
registerButton("inventoryIcon", () -> Network.getNetwork().sendToServer(new OpenInventoryMessage(colony, citizen.getName(), citizen.getEntityId())));
registerButton("inventoryTab", () -> Network.getNetwork().sendToServer(new OpenCitizenInventoryMessage(citizen)));
registerButton("inventoryIcon", () -> Network.getNetwork().sendToServer(new OpenCitizenInventoryMessage(citizen)));
PaneBuilders.tooltipBuilder().hoverPane(findPaneByID("inventoryIcon")).build().setText(Component.translatable("com.minecolonies.coremod.gui.citizen.inventory"));

registerButton("happinessTab", () -> new HappinessWindowCitizen(citizen).open());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import com.minecolonies.core.colony.buildings.workerbuildings.BuildingTownHall;
import com.minecolonies.core.entity.citizen.EntityCitizen;
import com.minecolonies.core.network.messages.client.colony.ColonyListMessage;
import com.minecolonies.core.network.messages.server.colony.OpenInventoryMessage;
import com.minecolonies.core.network.messages.server.colony.building.OpenBuildingInventoryMessage;
import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
Expand Down Expand Up @@ -130,7 +130,7 @@ public WindowColonyMap(final IBuildingView building)
*/
private void inventoryClicked()
{
Network.getNetwork().sendToServer(new OpenInventoryMessage(building));
Network.getNetwork().sendToServer(new OpenBuildingInventoryMessage(building));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,41 +50,13 @@ public class WindowMainPage extends AbstractWindowTownHall
/**
* Is the special feature unlocked.
*/
private static AtomicBoolean isFeatureUnlocked = new AtomicBoolean(false);

/**
* Drop down list for style.
*/
private DropDownList colorDropDownList;

/**
* Drop down list for style.
*/
private DropDownList textureDropDownList;

/**
* Drop down list for name style.
*/
private DropDownList nameStyleDropDownList;

/**
* The initial texture index.
*/
private int initialTextureIndex;

/**
* The initial texture index.
*/
private int initialNamePackIndex;


private static final AtomicBoolean isFeatureUnlocked = new AtomicBoolean(false);

/**
* Label for the colony name.
*/
private final Text title;


/**
* Constructor for the town hall window.
*
Expand All @@ -93,9 +65,9 @@ public class WindowMainPage extends AbstractWindowTownHall
public WindowMainPage(final BuildingTownHall.View building)
{
super(building, "layoutactions.xml");
initDropDowns();
this.title = findPaneOfTypeByID(LABEL_BUILDING_NAME, Text.class);

title = findPaneOfTypeByID(LABEL_BUILDING_NAME, Text.class);
initDropDowns();

registerButton(BUTTON_CHANGE_SPEC, this::doNothing);
registerButton(BUTTON_RENAME, this::renameClicked);
Expand All @@ -109,13 +81,6 @@ public WindowMainPage(final BuildingTownHall.View building)
registerButton(BUTTON_BANNER_PICKER, this::openBannerPicker);
registerButton(BUTTON_RESET_TEXTURE, this::resetTextureStyle);

this.colorDropDownList.setSelectedIndex(building.getColony().getTeamColonyColor().ordinal());
this.textureDropDownList.setSelectedIndex(TEXTURE_PACKS.indexOf(building.getColony().getTextureStyleId()));
this.initialTextureIndex = textureDropDownList.getSelectedIndex();

this.nameStyleDropDownList.setSelectedIndex(building.getColony().getNameFileIds().indexOf(building.getColony().getNameStyle()));
this.initialNamePackIndex = nameStyleDropDownList.getSelectedIndex();

checkFeatureUnlock();
}

Expand All @@ -139,11 +104,8 @@ private void initDropDowns()
{
findPaneOfTypeByID(DROPDOWN_COLOR_ID, DropDownList.class).setEnabled(enabled);

colorDropDownList = findPaneOfTypeByID(DROPDOWN_COLOR_ID, DropDownList.class);
colorDropDownList.setHandler(this::onDropDownListChanged);

final List<ChatFormatting> textColors = Arrays.stream(ChatFormatting.values()).filter(ChatFormatting::isColor).toList();

final DropDownList colorDropDownList = findPaneOfTypeByID(DROPDOWN_COLOR_ID, DropDownList.class);
colorDropDownList.setDataProvider(new DropDownList.DataProvider()
{
@Override
Expand All @@ -163,9 +125,10 @@ public String getLabel(final int index)
return "";
}
});
colorDropDownList.setSelectedIndex(building.getColony().getTeamColonyColor().ordinal());
colorDropDownList.setHandler(this::onDropDownListChanged);

textureDropDownList = findPaneOfTypeByID(DROPDOWN_TEXT_ID, DropDownList.class);
textureDropDownList.setHandler(this::toggleTexture);
final DropDownList textureDropDownList = findPaneOfTypeByID(DROPDOWN_TEXT_ID, DropDownList.class);
textureDropDownList.setDataProvider(new DropDownList.DataProvider()
{
@Override
Expand All @@ -180,9 +143,10 @@ public String getLabel(final int index)
return TEXTURE_PACKS.get(index);
}
});
textureDropDownList.setSelectedIndex(TEXTURE_PACKS.indexOf(building.getColony().getTextureStyleId()));
textureDropDownList.setHandler(this::toggleTexture);

nameStyleDropDownList = findPaneOfTypeByID(DROPDOWN_NAME_ID, DropDownList.class);
nameStyleDropDownList.setHandler(this::toggleNameFile);
final DropDownList nameStyleDropDownList = findPaneOfTypeByID(DROPDOWN_NAME_ID, DropDownList.class);
nameStyleDropDownList.setDataProvider(new DropDownList.DataProvider()
{
@Override
Expand All @@ -197,6 +161,8 @@ public String getLabel(final int index)
return building.getColony().getNameFileIds().get(index);
}
});
nameStyleDropDownList.setSelectedIndex(building.getColony().getNameFileIds().indexOf(building.getColony().getNameStyle()));
nameStyleDropDownList.setHandler(this::toggleNameFile);
}

/**
Expand All @@ -206,10 +172,7 @@ public String getLabel(final int index)
*/
private void toggleTexture(final DropDownList dropDownList)
{
if (dropDownList.getSelectedIndex() != initialTextureIndex)
{
Network.getNetwork().sendToServer(new ColonyTextureStyleMessage(building.getColony(), TEXTURE_PACKS.get(dropDownList.getSelectedIndex())));
}
Network.getNetwork().sendToServer(new ColonyTextureStyleMessage(building.getColony(), TEXTURE_PACKS.get(dropDownList.getSelectedIndex())));
}

/**
Expand All @@ -219,10 +182,7 @@ private void toggleTexture(final DropDownList dropDownList)
*/
private void toggleNameFile(final DropDownList dropDownList)
{
if (dropDownList.getSelectedIndex() != initialNamePackIndex)
{
Network.getNetwork().sendToServer(new ColonyNameStyleMessage(building.getColony(), building.getColony().getNameFileIds().get(dropDownList.getSelectedIndex())));
}
Network.getNetwork().sendToServer(new ColonyNameStyleMessage(building.getColony(), building.getColony().getNameFileIds().get(dropDownList.getSelectedIndex())));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@
import com.minecolonies.core.client.gui.WindowHutMinPlaceholder;
import com.minecolonies.core.client.gui.huts.WindowHutWorkerModulePlaceholder;
import com.minecolonies.core.colony.buildings.moduleviews.WorkerBuildingModuleView;
import com.minecolonies.core.network.messages.server.colony.OpenInventoryMessage;
import com.minecolonies.core.network.messages.server.colony.building.OpenBuildingInventoryMessage;
import com.minecolonies.core.network.messages.server.colony.building.HutRenameMessage;
import it.unimi.dsi.fastutil.ints.Int2ObjectLinkedOpenHashMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf;
Expand Down Expand Up @@ -358,7 +357,7 @@ public void openGui(final boolean shouldOpenInv)
{
if (shouldOpenInv)
{
Network.getNetwork().sendToServer(new OpenInventoryMessage(this));
Network.getNetwork().sendToServer(new OpenBuildingInventoryMessage(this));
}
else
{
Expand Down
Loading

0 comments on commit 3b96795

Please sign in to comment.