Skip to content

Commit

Permalink
feat(gestaltv7-eventsystem): Migration Event and @ReceiveEvent to ges…
Browse files Browse the repository at this point in the history
…talt's (#36)

Co-authored-by: Kevin Turner <83819+keturn@users.noreply.github.com>
  • Loading branch information
DarkWeird and keturn committed Dec 5, 2021
1 parent f49230b commit 3b580fa
Show file tree
Hide file tree
Showing 15 changed files with 107 additions and 257 deletions.
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
package org.terasology.fluid.component;

import com.google.common.base.Objects;
import org.joml.Vector2f;
import org.terasology.engine.network.Replicate;
import org.terasology.engine.rendering.assets.texture.TextureRegionAsset;
Expand Down Expand Up @@ -49,29 +50,19 @@ public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) {
return false;
}

FluidContainerItemComponent that = (FluidContainerItemComponent) o;
return Float.compare(that.volume, volume) == 0
&& Float.compare(that.maxVolume, maxVolume) == 0
&& Objects.equal(fluidType, that.fluidType)
&& Objects.equal(fluidMinPerc, that.fluidMinPerc)
&& Objects.equal(fluidSizePerc, that.fluidSizePerc)
&& Objects.equal(textureWithHole, that.textureWithHole)
&& Objects.equal(emptyTexture, that.emptyTexture);
}

if (Float.compare(that.volume, volume) != 0) {
return false;
}
if (emptyTexture != null ? !emptyTexture.equals(that.emptyTexture) : that.emptyTexture != null) {
return false;
}
if (fluidMinPerc != null ? !fluidMinPerc.equals(that.fluidMinPerc) : that.fluidMinPerc != null) {
return false;
}
if (fluidSizePerc != null ? !fluidSizePerc.equals(that.fluidSizePerc) : that.fluidSizePerc != null) {
return false;
}
if (fluidType != null ? !fluidType.equals(that.fluidType) : that.fluidType != null) {
return false;
}
if (textureWithHole != null ? !textureWithHole.equals(that.textureWithHole) : that.textureWithHole != null) {
return false;
}

return true;
@Override
public int hashCode() {
return Objects.hashCode(fluidType, volume, maxVolume, fluidMinPerc, fluidSizePerc, textureWithHole, emptyTexture);
}

@Override
Expand Down
Expand Up @@ -5,14 +5,15 @@
import org.terasology.engine.math.IntegerRange;
import org.terasology.gestalt.entitysystem.component.Component;

import java.util.HashMap;
import java.util.Map;

/**
* A component for integration with a Computer module.
*/
public class FluidInventoryAccessComponent implements Component<FluidInventoryAccessComponent> {
public Map<String, IntegerRange> input;
public Map<String, IntegerRange> output;
public Map<String, IntegerRange> input = new HashMap<>();
public Map<String, IntegerRange> output = new HashMap<>();

@Override
public void copyFrom(FluidInventoryAccessComponent other) {
Expand Down
@@ -1,18 +1,5 @@
/*
* Copyright 2014 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 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.fluid.event;

import org.terasology.engine.entitySystem.entity.EntityRef;
Expand Down
@@ -1,18 +1,5 @@
/*
* Copyright 2014 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 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.fluid.event;

import org.terasology.engine.entitySystem.entity.EntityRef;
Expand Down
@@ -1,22 +1,9 @@
/*
* 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 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.fluid.event;

import org.terasology.engine.entitySystem.entity.EntityRef;
import org.terasology.engine.entitySystem.event.Event;
import org.terasology.gestalt.entitysystem.event.Event;

/**
* This event indicates that the volume of a fluid was changed while it was in an inventory.
Expand Down
36 changes: 23 additions & 13 deletions src/main/java/org/terasology/fluid/system/FluidAuthoritySystem.java
Expand Up @@ -5,12 +5,10 @@
import com.google.common.collect.Sets;
import org.joml.Vector3f;
import org.joml.Vector3i;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.engine.core.ComponentSystemManager;
import org.terasology.engine.entitySystem.entity.EntityRef;
import org.terasology.engine.entitySystem.event.EventPriority;
import org.terasology.engine.entitySystem.event.ReceiveEvent;
import org.terasology.engine.entitySystem.event.Priority;
import org.terasology.engine.entitySystem.systems.BaseComponentSystem;
import org.terasology.engine.entitySystem.systems.RegisterMode;
import org.terasology.engine.entitySystem.systems.RegisterSystem;
Expand All @@ -19,7 +17,6 @@
import org.terasology.engine.logic.characters.events.AttackRequest;
import org.terasology.engine.logic.characters.events.OnItemUseEvent;
import org.terasology.engine.logic.common.ActivateEvent;
import org.terasology.module.inventory.systems.InventoryManager;
import org.terasology.engine.logic.location.LocationComponent;
import org.terasology.engine.math.Side;
import org.terasology.engine.physics.CollisionGroup;
Expand All @@ -34,6 +31,8 @@
import org.terasology.engine.world.chunks.blockdata.ExtraBlockDataManager;
import org.terasology.flowingliquids.world.block.LiquidData;
import org.terasology.fluid.component.FluidContainerItemComponent;
import org.terasology.gestalt.entitysystem.event.ReceiveEvent;
import org.terasology.module.inventory.systems.InventoryManager;

import java.util.Optional;
import java.util.Random;
Expand All @@ -47,14 +46,15 @@
@RegisterSystem(RegisterMode.AUTHORITY)
public class FluidAuthoritySystem extends BaseComponentSystem {

private static final Logger logger = LoggerFactory.getLogger(FluidAuthoritySystem.class);
public static final CollisionGroup[] PHYSICSFILTER = {
StandardCollisionGroup.LIQUID, StandardCollisionGroup.DEFAULT, StandardCollisionGroup.WORLD, StandardCollisionGroup.CHARACTER
};

/**
* If one block is 1m across, the fluid units are litres. This works reasonably
* sensibly with the pre-existing container sizes in ManualLabor.
*/
private static final float FLUID_PER_BLOCK = 1000;
public static final CollisionGroup[] PHYSICSFILTER = {StandardCollisionGroup.LIQUID, StandardCollisionGroup.DEFAULT, StandardCollisionGroup.WORLD, StandardCollisionGroup.CHARACTER};

@In
private WorldProvider worldProvider;
Expand Down Expand Up @@ -95,7 +95,7 @@ public void initialise() {
* there is no penetrable block preventing direct access.
* If the returned option is non-empty, the block is guaranteed to be a liquid block.
*
* @param start the player's location, the location to look from
* @param start the player's location, the location to look from
* @param direction the direction to search for a liquid block
* @param distance the reachable distance in number of blocks
*
Expand All @@ -121,7 +121,8 @@ private Optional<Vector3i> getLiquidInReach(final Vector3f start, final Vector3f
* @param character the player (so that they aren't targeted themself)
* @param distance the maximum distance to the targeted block
*
* @return The position to place the liquid, or empty if there's nothing within reach, there's an entity in the way, or there's a block in the way.
* @return The position to place the liquid, or empty if there's nothing within reach, there's an entity in the way,
* or there's a block in the way.
*/
private Optional<Vector3i> getPlacementPosition(final Vector3f start, final Vector3f direction, EntityRef character, float distance) {
HitResult hitResult = physics.rayTrace(start, direction, distance, Sets.newHashSet(character), PHYSICSFILTER);
Expand All @@ -143,7 +144,8 @@ private Optional<Vector3i> getPlacementPosition(final Vector3f start, final Vect
* @param character the player
* @param characterComponent the CharacterComponent of the player entity
*/
@ReceiveEvent(priority = EventPriority.PRIORITY_HIGH)
@Priority(EventPriority.PRIORITY_HIGH)
@ReceiveEvent
public void fillFluidContainerItem(AttackRequest event, EntityRef character, CharacterComponent characterComponent) {
EntityRef item = event.getItem();
FluidContainerItemComponent fluidContainer = item.getComponent(FluidContainerItemComponent.class);
Expand All @@ -157,13 +159,18 @@ public void fillFluidContainerItem(AttackRequest event, EntityRef character, Cha
}
EntityRef gaze = GazeAuthoritySystem.getGazeEntityForCharacter(character);
LocationComponent gazeLocation = gaze.getComponent(LocationComponent.class);
getLiquidInReach(gazeLocation.getWorldPosition(new Vector3f()), gazeLocation.getWorldDirection(new Vector3f()), character, characterComponent.interactionRange).ifPresent(pos -> {
getLiquidInReach(
gazeLocation.getWorldPosition(new Vector3f()),
gazeLocation.getWorldDirection(new Vector3f()),
character,
characterComponent.interactionRange).ifPresent(pos -> {
String fluidType = fluidRegistry.getCorrespondingFluid(worldProvider.getBlock(pos));
if (fluidType == null || (fluidContainer.fluidType != null && !fluidContainer.fluidType.equals(fluidType))) {
return;
}
EntityRef owner = item.getOwner();
final EntityRef removedItem = inventoryManager.removeItem(owner, event.getInstigator(), item, false, 1);
final EntityRef removedItem = inventoryManager.removeItem(owner, event.getInstigator(), item,
false, 1);
if (removedItem == null) {
return;
}
Expand Down Expand Up @@ -196,13 +203,16 @@ public void fillFluidContainerItem(AttackRequest event, EntityRef character, Cha
* @param item the item they're holding at the time
* @param fluidContainer the FluidContainerItemComponent of the item
*/
@ReceiveEvent(priority = EventPriority.PRIORITY_TRIVIAL)
@Priority(EventPriority.PRIORITY_TRIVIAL)
@ReceiveEvent
public void emptyFluidContainerItem(ActivateEvent event, EntityRef item, FluidContainerItemComponent fluidContainer) {
CharacterComponent characterComponent = event.getInstigator().getComponent(CharacterComponent.class);
if (fluidContainer.fluidType == null || characterComponent == null) {
return;
}
getPlacementPosition(event.getOrigin(), event.getDirection(), event.getInstigator(), characterComponent.interactionRange).ifPresent(pos -> {
getPlacementPosition(
event.getOrigin(), event.getDirection(), event.getInstigator(),
characterComponent.interactionRange).ifPresent(pos -> {
Block liquid = fluidRegistry.getCorrespondingLiquid(fluidContainer.fluidType);
if (liquid == null) {
return;
Expand Down
39 changes: 15 additions & 24 deletions src/main/java/org/terasology/fluid/system/FluidClientSystem.java
@@ -1,15 +1,11 @@
// Copyright 2020 The Terasology Foundation
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.fluid.system;

import org.joml.Vector2i;
import org.terasology.gestalt.assets.Asset;
import org.terasology.gestalt.assets.ResourceUrn;
import org.terasology.gestalt.assets.management.AssetManager;
import org.terasology.engine.entitySystem.entity.EntityRef;
import org.terasology.engine.entitySystem.entity.lifecycleEvents.OnActivatedComponent;
import org.terasology.engine.entitySystem.entity.lifecycleEvents.OnChangedComponent;
import org.terasology.engine.entitySystem.event.ReceiveEvent;
import org.terasology.engine.entitySystem.systems.BaseComponentSystem;
import org.terasology.engine.entitySystem.systems.RegisterMode;
import org.terasology.engine.entitySystem.systems.RegisterSystem;
Expand All @@ -19,12 +15,15 @@
import org.terasology.engine.rendering.assets.texture.TextureUtil;
import org.terasology.engine.utilities.Assets;
import org.terasology.fluid.component.FluidContainerItemComponent;
import org.terasology.gestalt.assets.ResourceUrn;
import org.terasology.gestalt.assets.management.AssetManager;
import org.terasology.gestalt.entitysystem.event.ReceiveEvent;
import org.terasology.joml.geom.Rectanglei;
import org.terasology.module.inventory.ui.GetItemTooltip;
import org.terasology.module.inventory.ui.InventoryCellRendered;
import org.terasology.nui.Canvas;
import org.terasology.nui.Color;
import org.terasology.nui.widgets.TooltipLine;
import org.terasology.module.inventory.ui.GetItemTooltip;
import org.terasology.module.inventory.ui.InventoryCellRendered;

import java.util.Optional;

Expand Down Expand Up @@ -97,27 +96,19 @@ private void setFluidContainerIcon(EntityRef container, FluidContainerItemCompon
String fluidType = fluidContainerItem.fluidType;

// If there's already some fluid in this container.
ItemComponent itemComp = container.getComponent(ItemComponent.class);
if (fluidType != null) {
if (fluidContainerItem.textureWithHole instanceof Asset) {
ItemComponent itemComp = container.getComponent(ItemComponent.class);

// Set the icon of this fluid container to show that it's filled.
String iconUrn = FluidContainerAssetResolver.getFluidContainerUri(
fluidContainerItem.textureWithHole.getUrn().toString(),
fluidType,
fluidContainerItem.fluidMinPerc.x, fluidContainerItem.fluidMinPerc.y,
fluidContainerItem.fluidSizePerc.x, fluidContainerItem.fluidSizePerc.y);
Optional<Texture> icon = assetManager.getAsset(iconUrn, Texture.class);
itemComp.icon = icon.isPresent() ? icon.get() : fluidContainerItem.emptyTexture;

container.saveComponent(itemComp);
}
// Set the icon of this fluid container to show that it's filled.
String iconUrn = FluidContainerAssetResolver.getFluidContainerUri(
fluidContainerItem.textureWithHole.getUrn().toString(), fluidType,
fluidContainerItem.fluidMinPerc.x, fluidContainerItem.fluidMinPerc.y,
fluidContainerItem.fluidSizePerc.x, fluidContainerItem.fluidSizePerc.y);
Optional<Texture> icon = assetManager.getAsset(iconUrn, Texture.class);
itemComp.icon = icon.isPresent() ? icon.get() : fluidContainerItem.emptyTexture;
} else {
ItemComponent itemComp = container.getComponent(ItemComponent.class);
itemComp.icon = fluidContainerItem.emptyTexture;

container.saveComponent(itemComp);
}
container.saveComponent(itemComp);
}
}

Expand Down
21 changes: 4 additions & 17 deletions src/main/java/org/terasology/fluid/system/FluidCommonSystem.java
@@ -1,22 +1,7 @@
/*
* Copyright 2014 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 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.fluid.system;

import org.terasology.gestalt.assets.ResourceUrn;
import org.terasology.gestalt.assets.management.AssetManager;
import org.terasology.engine.entitySystem.systems.BaseComponentSystem;
import org.terasology.engine.entitySystem.systems.RegisterSystem;
import org.terasology.engine.registry.In;
Expand All @@ -27,6 +12,8 @@
import org.terasology.engine.world.block.BlockUri;
import org.terasology.engine.world.block.loader.BlockFamilyDefinition;
import org.terasology.engine.world.block.loader.SectionDefinitionData;
import org.terasology.gestalt.assets.ResourceUrn;
import org.terasology.gestalt.assets.management.AssetManager;

import java.awt.image.BufferedImage;
import java.util.Optional;
Expand Down

0 comments on commit 3b580fa

Please sign in to comment.