Skip to content

Commit

Permalink
Overrule stuck parents - adjust clipboard to show tree (ldtteam#7497)
Browse files Browse the repository at this point in the history
Clipboard now extends the Request Tree, will display the entire tree of requests that are "stuck".
Retrying and Player resolvers on override will also check if one of the parents can be fulfilled.
  • Loading branch information
Raycoms authored Aug 15, 2021
1 parent 041a734 commit 8bffd4f
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 197 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 26,10 @@
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.items.wrapper.InvWrapper;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;

Expand Down Expand Up @@ -67,7 69,7 @@ public abstract class AbstractWindowRequestTree extends AbstractWindowSkeleton
/**
* The building position.
*/
private final IBuildingView building;
private @Nullable final IBuildingView building;

/**
* Constructor to initiate the window request tree windows.
Expand Down Expand Up @@ -111,7 113,7 @@ public void onOpened()
{
super.onOpened();

if (building != null && resourceList != null)
if (resourceList != null)
{
updateRequests();
}
Expand All @@ -134,8 136,7 @@ private void cancel(@NotNull final Button button)
if (getOpenRequestTreeOfBuilding().size() > row && row >= 0)
{
@NotNull final IRequest<?> request = getOpenRequestTreeOfBuilding().get(row).getRequest();
building.onRequestedRequestCancelled(colony.getRequestManager(), request);
Network.getNetwork().sendToServer(new UpdateRequestStateMessage(colony, request.getId(), RequestState.CANCELLED, null));
cancel(request);
}
updateRequests();
}
Expand All @@ -145,7 146,7 @@ private void cancel(@NotNull final Button button)
*
* @param request the request to cancel.
*/
public void cancel(@NotNull final IRequest<?> request)
protected void cancel(@NotNull final IRequest<?> request)
{
building.onRequestedRequestCancelled(colony.getRequestManager(), request);
Network.getNetwork().sendToServer(new UpdateRequestStateMessage(colony, request.getId(), RequestState.CANCELLED, null));
Expand All @@ -167,12 168,9 @@ protected ImmutableList<RequestWrapper> getOpenRequestTreeOfBuilding()

final List<RequestWrapper> treeElements = new ArrayList<>();

if (building != null)
{
getOpenRequestsFromBuilding(building).forEach(r -> {
constructTreeFromRequest(building, colony.getRequestManager(), r, treeElements, 0);
});
}
getOpenRequestsFromBuilding(building).forEach(r -> {
constructTreeFromRequest(building, colony.getRequestManager(), r, treeElements, 0);
});

return ImmutableList.copyOf(treeElements);
}
Expand All @@ -187,7 185,7 @@ protected ImmutableList<RequestWrapper> getOpenRequestTreeOfBuilding()
* @param currentDepth the current depth.
*/
private void constructTreeFromRequest(
@NotNull final IBuildingView buildingView,
@Nullable final IBuildingView buildingView,
@NotNull final IRequestManager manager,
@NotNull final IRequest<?> request,
@NotNull final List<RequestWrapper> list,
Expand Down Expand Up @@ -220,6 218,10 @@ private void constructTreeFromRequest(
*/
public ImmutableList<IRequest<?>> getOpenRequestsFromBuilding(final IBuildingView building)
{
if (building == null)
{
return ImmutableList.of();
}
return building.getOpenRequestsOfBuilding();
}

Expand Down Expand Up @@ -308,7 310,11 @@ public void updateElement(final int index, final Pane rowPane)
wrapperBox.setPosition(wrapperBox.getX() 2 * wrapper.getDepth(), wrapperBox.getY());
wrapperBox.setSize(wrapperBox.getParent().getWidth() - 2 * wrapper.getDepth(), wrapperBox.getHeight());

rowPane.findPaneByID(REQUEST_FULLFIL).enable();
final Pane pane = rowPane.findPaneByID(REQUEST_FULLFIL);
if (pane != null)
{
rowPane.findPaneByID(REQUEST_FULLFIL).enable();
}

final IRequest<?> request = wrapper.getRequest();
final ItemIcon exampleStackDisplay = rowPane.findPaneOfTypeByID(LIST_ELEMENT_ID_REQUEST_STACK, ItemIcon.class);
Expand Down Expand Up @@ -351,13 357,16 @@ public void updateElement(final int index, final Pane rowPane)
rowPane.findPaneOfTypeByID(REQUEST_CANCEL, ButtonImage.class).hide();
}

if (!fulfillable(request))
if (pane != null)
{
rowPane.findPaneOfTypeByID(REQUEST_FULLFIL, ButtonImage.class).hide();
}
else
{
rowPane.findPaneOfTypeByID(REQUEST_FULLFIL, ButtonImage.class).show();
if (!fulfillable(request))
{
pane.hide();
}
else
{
pane.show();
}
}
}
});
Expand Down Expand Up @@ -480,13 489,13 @@ protected static final class RequestWrapper
* @param depth the depth.
* @param buildingView the building it belongs to.
*/
public RequestWrapper(@NotNull final IRequest<?> request, final int depth, @NotNull final IBuildingView buildingView)
public RequestWrapper(@NotNull final IRequest<?> request, final int depth, @Nullable final IBuildingView buildingView)
{
this.request = request;
this.depth = depth;
this.overruleable = request.getRequester().getId().equals(buildingView.getId())
this.overruleable = buildingView != null && (request.getRequester().getId().equals(buildingView.getId())
|| buildingView.getResolverIds().contains(request.getRequester().getId())
|| buildingView.getPosition().equals(request.getRequester().getLocation().getInDimensionLocation());
|| buildingView.getPosition().equals(request.getRequester().getLocation().getInDimensionLocation()));
}

/**
Expand Down
183 changes: 22 additions & 161 deletions src/main/java/com/minecolonies/coremod/client/gui/WindowClipBoard.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 2,9 @@

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.ldtteam.blockout.controls.Button;
import com.ldtteam.blockout.controls.Image;
import com.ldtteam.blockout.controls.ItemIcon;
import com.ldtteam.blockout.controls.Text;
import com.ldtteam.blockout.views.ScrollingList;
import com.minecolonies.api.colony.ICitizenDataView;
import com.minecolonies.api.colony.IColonyView;
import com.minecolonies.api.colony.buildings.views.IBuildingView;
import com.minecolonies.api.colony.requestsystem.manager.IRequestManager;
import com.minecolonies.api.colony.requestsystem.request.IRequest;
import com.minecolonies.api.colony.requestsystem.request.RequestState;
Expand All @@ -19,88 15,33 @@
import com.minecolonies.coremod.Network;
import com.minecolonies.coremod.network.messages.server.colony.UpdateRequestStateMessage;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.vector.Vector3i;
import org.jetbrains.annotations.NotNull;

import java.util.*;
import java.util.stream.Collectors;

import static com.minecolonies.api.util.constant.WindowConstants.CLIPBOARD_TOGGLE;
import static com.minecolonies.coremod.colony.requestsystem.requests.AbstractRequest.MISSING;

/**
* ClipBoard window.
*/
public class WindowClipBoard extends AbstractWindowSkeleton
public class WindowClipBoard extends AbstractWindowRequestTree
{
/**
* Resource suffix.
*/
private static final String BUILD_TOOL_RESOURCE_SUFFIX = ":gui/windowclipboard.xml";

/**
* Requests list id.
*/
private static final String WINDOW_ID_LIST_REQUESTS = "requests";

/**
* Requestst stack id.
*/
private static final String LIST_ELEMENT_ID_REQUEST_STACK = "requestStack";

/**
* Id of the resource add button.
*/
private static final String REQUEST_CANCEL = "cancel";

/**
* Id of the detail button.
*/
private static final String REQUEST_DETAIL = "detail";

/**
* Id of the short detail label.
*/
private static final String REQUEST_SHORT_DETAIL = "shortDetail";

/**
* Resolver string.
*/
private static final String DELIVERY_IMAGE = "deliveryImage";

/**
* Id of the requester label.
*/
private static final String REQUESTER = "requester";

/**
* The divider for the life count.
*/
private static final int LIFE_COUNT_DIVIDER = 30;

/**
* List of async request tokens.
*/
private final List<IToken<?>> asyncRequest = new ArrayList<>();

/**
* Scrollinglist of the resources.
*/
private ScrollingList resourceList;

/**
* The colony id.
*/
private final IColonyView colony;

/**
* Life count.
*/
private int lifeCount = 0;

/**
* Hide or show not important requests.
*/
Expand All @@ -113,7 54,7 @@ public class WindowClipBoard extends AbstractWindowSkeleton
*/
public WindowClipBoard(final IColonyView colony)
{
super(Constants.MOD_ID BUILD_TOOL_RESOURCE_SUFFIX);
super(null, Constants.MOD_ID BUILD_TOOL_RESOURCE_SUFFIX, colony);
this.colony = colony;
for (final ICitizenDataView view : this.colony.getCitizens().values())
{
Expand All @@ -128,61 69,10 @@ public WindowClipBoard(final IColonyView colony)
private void toggleImportant()
{
this.hide = !this.hide;
fillList();
}

/**
* Called when the window is opened. Sets up the buttons for either hut mode or decoration mode.
*/
@Override
public void onOpened()
{
fillList();
}

private void fillList()
{
resourceList = findPaneOfTypeByID(WINDOW_ID_LIST_REQUESTS, ScrollingList.class);
resourceList.setDataProvider(() -> getOpenRequests().size(), (index, rowPane) ->
{
final ImmutableList<IRequest<?>> openRequests = getOpenRequests();
if (index < 0 || index >= openRequests.size())
{
return;
}

final IRequest<?> request = openRequests.get(index);
final ItemIcon exampleStackDisplay = rowPane.findPaneOfTypeByID(LIST_ELEMENT_ID_REQUEST_STACK, ItemIcon.class);
final List<ItemStack> displayStacks = request.getDisplayStacks();

if (!displayStacks.isEmpty())
{
if (exampleStackDisplay != null)
{
exampleStackDisplay.setItem(displayStacks.get((lifeCount / LIFE_COUNT_DIVIDER) % displayStacks.size()));
}
}
else if (!request.getDisplayIcon().equals(MISSING))
{
final Image logo = rowPane.findPaneOfTypeByID(DELIVERY_IMAGE, Image.class);
logo.setVisible(true);
logo.setImage(request.getDisplayIcon());
}

rowPane.findPaneOfTypeByID(REQUESTER, Text.class)
.setText(request.getRequester().getRequesterDisplayName(colony.getRequestManager(), request));

rowPane.findPaneOfTypeByID(REQUEST_SHORT_DETAIL, Text.class)
.setText(request.getShortDisplayString().getString().replace("§f", ""));
});
}

/**
* The requests to display.
*
* @return the list of requests.
*/
public ImmutableList<IRequest<?>> getOpenRequests()
public ImmutableList<IRequest<?>> getOpenRequestsFromBuilding(final IBuildingView building)
{
final ArrayList<IRequest<?>> requests = Lists.newArrayList();

Expand All @@ -205,7 95,20 @@ public ImmutableList<IRequest<?>> getOpenRequests()
requestTokens.addAll(resolver.getAllAssignedRequests());
requestTokens.addAll(retryingRequestResolver.getAllAssignedRequests());

requests.addAll(requestTokens.stream().map(requestManager::getRequestForToken).filter(Objects::nonNull).collect(Collectors.toSet()));
for (final IToken<?> token : requestTokens)
{
IRequest<?> request = requestManager.getRequestForToken(token);

while (request != null && request.hasParent())
{
request = requestManager.getRequestForToken(request.getParent());
}

if (request != null && !requests.contains(request))
{
requests.add(request);
}
}

if (hide)
{
Expand All @@ -221,56 124,14 @@ public ImmutableList<IRequest<?>> getOpenRequests()
}

@Override
public void onUpdate()
public boolean fulfillable(final IRequest<?> tRequest)
{
super.onUpdate();
if (!Screen.hasShiftDown())
{
lifeCount ;
}
return false;
}

/**
* Called when a button in the citizen has been clicked.
*
* @param button the clicked button.
*/
@Override
public void onButtonClicked(@NotNull final Button button)
{
switch (button.getID())
{
case REQUEST_DETAIL:
detailedClicked(button);
break;
case REQUEST_CANCEL:
cancel(button);
break;
default:
super.onButtonClicked(button);
break;
}
}

private void detailedClicked(@NotNull final Button button)
{
final int row = resourceList.getListElementIndexByPane(button);

if (getOpenRequests().size() > row && row >= 0)
{
@NotNull final WindowRequestDetail window = new WindowRequestDetail(this, getOpenRequests().get(row), colony.getID());
window.open();
}
}

private void cancel(@NotNull final Button button)
protected void cancel(@NotNull final IRequest<?> request)
{
final int row = resourceList.getListElementIndexByPane(button);

if (getOpenRequests().size() > row && row >= 0)
{
@NotNull final IRequest<?> request = getOpenRequests().get(row);
Network.getNetwork().sendToServer(new UpdateRequestStateMessage(colony, request.getId(), RequestState.CANCELLED, null));
}
Network.getNetwork().sendToServer(new UpdateRequestStateMessage(colony, request.getId(), RequestState.CANCELLED, null));
}
}
Loading

0 comments on commit 8bffd4f

Please sign in to comment.