Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make ChangingBlocks case insensitive via BlockUri #15

Merged
merged 8 commits into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ When the last block is reached, either loop back to the first one, or send an On
Example component to add to a prefab:

"ChangingBlocks" : {
"blockFamilyStages" : {
"Crops:Corn1" : 30000,
"Crops:Corn2" : 30000,
"Crops:Corn3" : 30000,
"Crops:Corn4" : 30000,
"Crops:Corn5" : 30000,
"Crops:Corn6" : 30000,
"Crops:Corn7" : 30000 },
"blockFamilyStages" : [
{ "key": "Crops:Corn1", "value": 30000 },
{ "key": "Crops:Corn2", "value": 30000 },
{ "key": "Crops:Corn3", "value": 30000 },
{ "key": "Crops:Corn4", "value": 30000 },
{ "key": "Crops:Corn5", "value": 30000 },
{ "key": "Crops:Corn6", "value": 30000 },
{ "key": "Crops:Corn7", "value": 30000 }
],
"loops" : false
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Copyright 2021 The Terasology Foundation
// Copyright 2022 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.changingBlocks;

import org.terasology.engine.core.SimpleUri;
import org.terasology.engine.world.block.ForceBlockActive;
import org.terasology.gestalt.entitysystem.component.Component;

Expand All @@ -17,7 +18,7 @@ public final class ChangingBlocksComponent implements Component<ChangingBlocksCo
public boolean stopped;

// List of block names to cycle through
public Map<String, Long> blockFamilyStages;
public Map<SimpleUri, Long> blockFamilyStages;

// internal: used to determine time to next block change
public long timeInGameMsToNextStage;
Expand All @@ -30,7 +31,7 @@ public void copyFrom(ChangingBlocksComponent other) {
this.loops = other.loops;
this.stopped = other.stopped;
this.blockFamilyStages.clear();
other.blockFamilyStages.forEach((k,v) -> this.blockFamilyStages.put(k,v));
other.blockFamilyStages.forEach((k, v) -> this.blockFamilyStages.put(k, v));
this.timeInGameMsToNextStage = other.timeInGameMsToNextStage;
this.lastGameTimeCheck = other.lastGameTimeCheck;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
/*
* Copyright 2015 MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2022 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.changingBlocks;

import org.joml.RoundingMode;
import org.joml.Vector3f;
import org.joml.Vector3i;
import org.terasology.engine.core.SimpleUri;
import org.terasology.engine.core.Time;
import org.terasology.engine.entitySystem.entity.EntityManager;
import org.terasology.engine.entitySystem.entity.EntityRef;
Expand Down Expand Up @@ -63,7 +51,7 @@ public void onSpawn(OnAddedComponent event, EntityRef entity) {
ChangingBlocksComponent changingBlocks = entity.getComponent(ChangingBlocksComponent.class);
LocationComponent locComponent = entity.getComponent(LocationComponent.class);
Block currentBlock = worldprovider.getBlock(locComponent.getWorldPosition(new Vector3f()));
String currentBlockFamilyStage = currentBlock.getURI().toString();
SimpleUri currentBlockFamilyStage = new SimpleUri(currentBlock.getURI().getModuleName(), currentBlock.getURI().getIdentifier());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this should be available on Block or BlockUri to prevent accidental errors. 🤔 (I'd use at least a module-wide helper class to be sure that we're using the same conversion within the context of Changing Blocks). However, I'm not sure how universal (and generally helpful) this reduction of information is. So, probably the module-wide helper function would indeed be a good compromise.

(We talked about this offline, but just for the record)
The culprit here is that the block uri can contain way more information (shape, orientation, ...) than just the plain block "type" (family?).
Even more confusing, it seems like we're hiding some shapes (if the shape is hard-coded in the block definition maybe?)

changingBlocks.timeInGameMsToNextStage = changingBlocks.blockFamilyStages.get(currentBlockFamilyStage);
changingBlocks.lastGameTimeCheck = initTime;
entity.saveComponent(changingBlocks);
Expand All @@ -79,7 +67,8 @@ public void update(float delta) {
// System last time check is to try to improve performance
long gameTimeInMs = timer.getGameTimeInMs();
if (lastCheckTime + CHECK_INTERVAL < gameTimeInMs) {
for (EntityRef changingBlocks : entityManager.getEntitiesWith(ChangingBlocksComponent.class, BlockComponent.class, LocationComponent.class)) {
for (EntityRef changingBlocks : entityManager.getEntitiesWith(ChangingBlocksComponent.class,
BlockComponent.class, LocationComponent.class)) {
ChangingBlocksComponent blockAnimation = changingBlocks.getComponent(ChangingBlocksComponent.class);
if (blockAnimation.stopped) {
return;
Expand All @@ -94,8 +83,8 @@ public void update(float delta) {
LocationComponent locComponent = changingBlocks.getComponent(LocationComponent.class);
Block currentBlock = worldprovider.getBlock(locComponent.getWorldPosition(new Vector3f()));
String currentBlockFamilyStage = currentBlock.getURI().toString();
Set<String> keySet = blockAnimation.blockFamilyStages.keySet();
List<String> keyList = new ArrayList<>(keySet);
Set<SimpleUri> keySet = blockAnimation.blockFamilyStages.keySet();
List<SimpleUri> keyList = new ArrayList<>(keySet);
int currentstageIndex = keyList.indexOf(currentBlockFamilyStage);
int lastStageIndex = blockAnimation.blockFamilyStages.size() - 1;
if (lastStageIndex > currentstageIndex) {
Expand All @@ -108,8 +97,8 @@ public void update(float delta) {
changingBlocks.send(new OnBlockSequenceComplete());
}
}
String newBlockUri = keyList.get(currentstageIndex);
Block newBlock = blockManager.getBlock(newBlockUri);
SimpleUri newBlockUri = keyList.get(currentstageIndex);
Block newBlock = blockManager.getBlock(newBlockUri.toString());
if (newBlockUri.equals(newBlock.getURI().toString())) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BlockManager#getBlock cannot return null. If, before returning, the block that would be returned equals null the air block is returned instead.

worldprovider.setBlock(new Vector3i(locComponent.getWorldPosition(new Vector3f()), RoundingMode.FLOOR), newBlock);
blockAnimation.timeInGameMsToNextStage = blockAnimation.blockFamilyStages.get(currentBlockFamilyStage);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
/*
* Copyright 2013 MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2022 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.changingBlocks;

import org.terasology.engine.entitySystem.event.AbstractConsumableEvent;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
/*
* Copyright 2019 MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2022 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.changingBlocks.conditional;

import org.terasology.engine.math.Side;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 The Terasology Foundation
// Copyright 2022 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.changingBlocks.conditional;

Expand Down Expand Up @@ -81,11 +81,12 @@ public void registerTrigger(String trigger, EntityRef triggerable, Boolean isBlo
triggerCollections.compute(
trigger.toLowerCase(),
(k, v) -> {
if (v == null) {
v = new ArrayList<>();
List<EntityRef> vCopy = v;
if (vCopy == null) {
vCopy = new ArrayList<>();
}
v.add(triggerable);
return v;
vCopy.add(triggerable);
return vCopy;
}
);
}
Expand All @@ -108,7 +109,9 @@ private void checkBlockNearby(EntityRef entity, Vector3fc triggerPosition, Strin
if (distance >= change.minDistance && distance <= change.maxDistance) {
Vector3f direction = triggerPosition.sub(changeSpot, new Vector3f());
//if the change can occur when obstructed, or otherwise if there is no obstruction
if (change.throughWalls || physics.rayTrace(changeSpot, direction, change.maxDistance, StandardCollisionGroup.WORLD).getEntity() == entity) {
if (change.throughWalls || physics
.rayTrace(changeSpot, direction, change.maxDistance, StandardCollisionGroup.WORLD)
.getEntity() == entity) {
//if the random odds are in our favor
if (change.chance >= random.nextFloat()) {
worldprovider.setBlock(new Vector3i(changeSpot, RoundingMode.FLOOR), blockManager.getBlock(change.targetBlockID));
Expand Down Expand Up @@ -140,10 +143,13 @@ private void checkBlockDirected(EntityRef entity, Vector3fc triggerPosition, Str
//if the angle is within the change's field of view limit
if (direction.angle(new Vector3f(global.direction())) <= change.fieldOfView) {
//if the change can occur when obstructed, or otherwise if there is no obstruction
if (change.throughWalls || physics.rayTrace(changeSpot, direction, change.maxDistance, StandardCollisionGroup.WORLD).getEntity() == entity) {
if (change.throughWalls || physics
.rayTrace(changeSpot, direction, change.maxDistance, StandardCollisionGroup.WORLD)
.getEntity() == entity) {
//if the random odds are in our favor
if (change.chance >= random.nextFloat()) {
worldprovider.setBlock(new Vector3i(changeSpot, RoundingMode.FLOOR), blockManager.getBlock(change.targetBlockID));
worldprovider.setBlock(new Vector3i(changeSpot, RoundingMode.FLOOR),
blockManager.getBlock(change.targetBlockID));
}
}
}
Expand All @@ -168,10 +174,13 @@ private void checkEntityNearby(EntityRef entity, Vector3fc triggerPosition, Stri
if (distance >= change.minDistance && distance <= change.maxDistance) {
Vector3f direction = triggerPosition.sub(changeSpot, new Vector3f());
//if the change can occur when obstructed, or otherwise if there is no obstruction
if (change.throughWalls || physics.rayTrace(changeSpot, direction, change.maxDistance, StandardCollisionGroup.WORLD).getEntity() == entity) {
if (change.throughWalls || physics
.rayTrace(changeSpot, direction, change.maxDistance, StandardCollisionGroup.WORLD)
.getEntity() == entity) {
//if the random odds are in our favor
if (change.chance >= random.nextFloat()) {
worldprovider.setBlock(new Vector3i(changeSpot, RoundingMode.FLOOR), blockManager.getBlock(change.targetBlockID));
worldprovider.setBlock(new Vector3i(changeSpot, RoundingMode.FLOOR),
blockManager.getBlock(change.targetBlockID));
}
}
}
Expand Down Expand Up @@ -199,10 +208,13 @@ private void checkEntityDirected(EntityRef entity, Vector3fc triggerPosition, St
//if the angle is within the change's field of view limit
if (direction.angle(new Vector3f(global.direction())) <= change.fieldOfView) {
//if the change can occur when obstructed, or otherwise if there is no obstruction
if (change.throughWalls || physics.rayTrace(changeSpot, direction, change.maxDistance, StandardCollisionGroup.WORLD).getEntity() == entity) {
if (change.throughWalls || physics
.rayTrace(changeSpot, direction, change.maxDistance, StandardCollisionGroup.WORLD)
.getEntity() == entity) {
//if the random odds are in our favor
if (change.chance >= random.nextFloat()) {
worldprovider.setBlock(new Vector3i(changeSpot, RoundingMode.FLOOR), blockManager.getBlock(change.targetBlockID));
worldprovider.setBlock(new Vector3i(changeSpot, RoundingMode.FLOOR),
blockManager.getBlock(change.targetBlockID));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The Terasology Foundation
// Copyright 2022 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.changingBlocks.conditional.components;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The Terasology Foundation
// Copyright 2022 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.changingBlocks.conditional.components;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The Terasology Foundation
// Copyright 2022 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.changingBlocks.conditional.components;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The Terasology Foundation
// Copyright 2022 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.changingBlocks.conditional.components;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The Terasology Foundation
// Copyright 2022 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.changingBlocks.conditional.components;

Expand Down