Skip to content

Commit

Permalink
Fixes #4754: Allow reporting parts to be rotated again. (#5062)
Browse files Browse the repository at this point in the history
Refactored any subclass of `AEInternalModelData` to use `hasProperty()`
and `getData()` in case something replaces them with a wrapper again.
Added a new spin property to `AEModelData` as global property for any
other `IModelData` needing a common spin property.

Co-authored-by: Sebastian Hartte <sebastian@hartte.de>
  • Loading branch information
yueh and shartte authored Mar 17, 2021
1 parent 060594e commit a65ca79
Show file tree
Hide file tree
Showing 11 changed files with 285 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,28 @@
import appeng.api.parts.IPartModel;
import appeng.api.util.AECableType;
import appeng.api.util.AEColor;
import appeng.client.render.model.AEModelData;

public class CableBusBakedModel implements IBakedModel {

// The number of quads overall that will be cached
private static final int CACHE_QUAD_COUNT = 5000;

/**
* Lookup table to match the spin of a part with an up direction.
*
* DUNSWE for the facing index, 4 spin values per facing.
*
*/
private static final Direction[] SPIN_TO_DIRECTION = new Direction[] {
Direction.NORTH, Direction.WEST, Direction.SOUTH, Direction.EAST, // DOWN
Direction.NORTH, Direction.EAST, Direction.SOUTH, Direction.WEST, // UP
Direction.UP, Direction.WEST, Direction.DOWN, Direction.EAST, // NORTH
Direction.UP, Direction.EAST, Direction.DOWN, Direction.WEST, // SOUTH
Direction.UP, Direction.SOUTH, Direction.DOWN, Direction.NORTH, // WEST
Direction.UP, Direction.NORTH, Direction.DOWN, Direction.SOUTH // EAST
};

private final LoadingCache<CableBusRenderState, List<BakedQuad>> cableModelCache;

private final CableBuilder cableBuilder;
Expand Down Expand Up @@ -134,9 +150,11 @@ public List<BakedQuad> getQuads(@Nullable BlockState state, @Nullable Direction

List<BakedQuad> partQuads = bakedModel.getQuads(state, null, rand, partModelData);

Direction spinDirection = getPartSpin(facing, partModelData);

// Rotate quads accordingly
QuadRotator rotator = new QuadRotator();
partQuads = rotator.rotateQuads(partQuads, facing, Direction.UP);
partQuads = rotator.rotateQuads(partQuads, facing, spinDirection);

quads.addAll(partQuads);
}
Expand Down Expand Up @@ -175,6 +193,15 @@ private static boolean isStraightLine(AECableType cableType, EnumMap<Direction,
return firstType == secondType && cableType == firstType && cableType == secondType;
}

private static Direction getPartSpin(Direction facing, IModelData partModelData) {
if (partModelData.hasProperty(AEModelData.SPIN)) {
byte spin = partModelData.getData(AEModelData.SPIN);
return SPIN_TO_DIRECTION[facing.ordinal() * 4 + spin];
}

return Direction.UP;
}

private void addCableQuads(CableBusRenderState renderState, List<BakedQuad> quadsOut) {
AECableType cableType = renderState.getCableType();
if (cableType == AECableType.NONE) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2021, TeamAppliedEnergistics, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/

package appeng.client.render.cablebus;

Expand Down Expand Up @@ -34,13 +51,11 @@ public P2PTunnelFrequencyBakedModel(final TextureAtlasSprite texture) {

@Override
public List<BakedQuad> getQuads(BlockState state, Direction side, Random rand, IModelData modelData) {
if (side != null || !(modelData instanceof P2PTunnelFrequencyModelData)) {
if (side != null || !modelData.hasProperty(P2PTunnelFrequencyModelData.FREQUENCY)) {
return Collections.emptyList();
}

P2PTunnelFrequencyModelData freqModelData = (P2PTunnelFrequencyModelData) modelData;

return this.getPartQuads(freqModelData.getFrequency());
return this.getPartQuads(modelData.getData(P2PTunnelFrequencyModelData.FREQUENCY));
}

private List<BakedQuad> getQuadsForFrequency(final short frequency, final boolean active) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2021, TeamAppliedEnergistics, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/

package appeng.client.render.cablebus;

import java.util.function.Function;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,76 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2021, TeamAppliedEnergistics, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/

package appeng.client.render.cablebus;

import net.minecraftforge.client.model.data.ModelProperty;
import java.util.Objects;

import javax.annotation.Nullable;

import appeng.client.render.model.AEInternalModelData;
import net.minecraftforge.client.model.data.IModelData;
import net.minecraftforge.client.model.data.ModelProperty;

public final class P2PTunnelFrequencyModelData extends AEInternalModelData {
public final class P2PTunnelFrequencyModelData implements IModelData {

public static final ModelProperty<Long> FREQUENCY = new ModelProperty<>();

private final long frequency;
private final Long frequency;

public P2PTunnelFrequencyModelData(long frequency) {
this.frequency = frequency;
}

public long getFrequency() {
return frequency;
@Override
public boolean hasProperty(ModelProperty<?> prop) {
return prop == FREQUENCY;
}

@Override
@Nullable
@SuppressWarnings("unchecked")
public <T> T getData(ModelProperty<T> prop) {
if (prop == FREQUENCY) {
return (T) this.frequency;
}

return null;
}

@Override
@Nullable
public <T> T setData(ModelProperty<T> prop, T data) {
return null;
}

@Override
public int hashCode() {
return Objects.hash(frequency);
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
P2PTunnelFrequencyModelData that = (P2PTunnelFrequencyModelData) o;
return frequency.equals(that.frequency);
}
}
27 changes: 0 additions & 27 deletions src/main/java/appeng/client/render/model/AEInternalModelData.java

This file was deleted.

1 change: 1 addition & 0 deletions src/main/java/appeng/client/render/model/AEModelData.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class AEModelData implements IModelData {
public static final ModelProperty<Direction> UP = new ModelProperty<>();
public static final ModelProperty<Direction> FORWARD = new ModelProperty<>();
public static final ModelProperty<Boolean> CACHEABLE = new ModelProperty<>();
public static final ModelProperty<Byte> SPIN = new ModelProperty<>();

private final Direction up;
private final Direction forward;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/appeng/parts/automation/PlaneBakedModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ public List<BakedQuad> getQuads(@Nullable BlockState state, @Nullable Direction
IModelData modelData) {
if (side == null) {
PlaneConnections connections = DEFAULT_PERMUTATION;
if (modelData instanceof PlaneModelData) {
connections = ((PlaneModelData) modelData).getConnections();
if (modelData.hasProperty(PlaneModelData.CONNECTIONS)) {
connections = modelData.getData(PlaneModelData.CONNECTIONS);
}
return this.quads.get(connections);
} else {
Expand Down
17 changes: 2 additions & 15 deletions src/main/java/appeng/parts/automation/PlaneConnections.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,10 @@ private static int getIndex(boolean up, boolean right, boolean down, boolean lef
+ (down ? BITMASK_DOWN : 0);
}

// Returns a suffix that expresses the connection states as a string
public String getFilenameSuffix() {
String suffix = Integer.toBinaryString(this.getIndex());
return Strings.padStart(suffix, 4, '0');
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || this.getClass() != o.getClass()) {
return false;
}

PlaneConnections that = (PlaneConnections) o;
return this.up == that.up && this.right == that.right && this.down == that.down && this.left == that.left;
// This class is final/has a private constructor, and is interned
return this == o;
}

@Override
Expand Down
69 changes: 64 additions & 5 deletions src/main/java/appeng/parts/automation/PlaneModelData.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,76 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2021, TeamAppliedEnergistics, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/

package appeng.parts.automation;

import appeng.client.render.model.AEInternalModelData;
import java.util.Objects;

import javax.annotation.Nullable;

import net.minecraftforge.client.model.data.IModelData;
import net.minecraftforge.client.model.data.ModelProperty;

public class PlaneModelData extends AEInternalModelData {
public class PlaneModelData implements IModelData {

public static final ModelProperty<PlaneConnections> CONNECTIONS = new ModelProperty<>();

private final PlaneConnections connections;

public PlaneModelData(PlaneConnections connections) {
this.connections = connections;
this.connections = Objects.requireNonNull(connections);
}

@Override
public boolean hasProperty(ModelProperty<?> prop) {
return prop == CONNECTIONS;
}

@Override
@Nullable
@SuppressWarnings("unchecked")
public <T> T getData(ModelProperty<T> prop) {
if (prop == CONNECTIONS) {
return (T) this.connections;
}
return null;
}

@Override
@Nullable
public <T> T setData(ModelProperty<T> prop, T data) {
return null;
}

@Override
public int hashCode() {
return connections.hashCode();
}

public PlaneConnections getConnections() {
return connections;
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
PlaneModelData that = (PlaneModelData) o;
return connections.equals(that.connections);
}

}
Loading

0 comments on commit a65ca79

Please sign in to comment.